greybus: es2: Allow proper release of greybus host device
[cascardo/linux.git] / drivers / staging / greybus / es2.c
1 /*
2  * Greybus "AP" USB driver for "ES2" controller chips
3  *
4  * Copyright 2014-2015 Google Inc.
5  * Copyright 2014-2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9 #include <linux/kthread.h>
10 #include <linux/sizes.h>
11 #include <linux/usb.h>
12 #include <linux/kfifo.h>
13 #include <linux/debugfs.h>
14 #include <asm/unaligned.h>
15
16 #include "greybus.h"
17 #include "kernel_ver.h"
18 #include "connection.h"
19 #include "greybus_trace.h"
20
21 /* Memory sizes for the buffers sent to/from the ES2 controller */
22 #define ES2_GBUF_MSG_SIZE_MAX   2048
23
24 static const struct usb_device_id id_table[] = {
25         { USB_DEVICE(0x18d1, 0x1eaf) },
26         { },
27 };
28 MODULE_DEVICE_TABLE(usb, id_table);
29
30 #define APB1_LOG_SIZE           SZ_16K
31
32 /* Number of bulk in and bulk out couple */
33 #define NUM_BULKS               7
34
35 /*
36  * Number of CPort IN urbs in flight at any point in time.
37  * Adjust if we are having stalls in the USB buffer due to not enough urbs in
38  * flight.
39  */
40 #define NUM_CPORT_IN_URB        4
41
42 /* Number of CPort OUT urbs in flight at any point in time.
43  * Adjust if we get messages saying we are out of urbs in the system log.
44  */
45 #define NUM_CPORT_OUT_URB       (8 * NUM_BULKS)
46
47 /*
48  * @endpoint: bulk in endpoint for CPort data
49  * @urb: array of urbs for the CPort in messages
50  * @buffer: array of buffers for the @cport_in_urb urbs
51  */
52 struct es2_cport_in {
53         __u8 endpoint;
54         struct urb *urb[NUM_CPORT_IN_URB];
55         u8 *buffer[NUM_CPORT_IN_URB];
56 };
57
58 /*
59  * @endpoint: bulk out endpoint for CPort data
60  */
61 struct es2_cport_out {
62         __u8 endpoint;
63 };
64
65 /**
66  * es2_ap_dev - ES2 USB Bridge to AP structure
67  * @usb_dev: pointer to the USB device we are.
68  * @usb_intf: pointer to the USB interface we are bound to.
69  * @hd: pointer to our gb_host_device structure
70
71  * @cport_in: endpoint, urbs and buffer for cport in messages
72  * @cport_out: endpoint for for cport out messages
73  * @cport_out_urb: array of urbs for the CPort out messages
74  * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
75  *                      not.
76  * @cport_out_urb_cancelled: array of flags indicating whether the
77  *                      corresponding @cport_out_urb is being cancelled
78  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
79  *
80  * @apb_log_task: task pointer for logging thread
81  * @apb_log_dentry: file system entry for the log file interface
82  * @apb_log_enable_dentry: file system entry for enabling logging
83  * @apb_log_fifo: kernel FIFO to carry logged data
84  */
85 struct es2_ap_dev {
86         struct usb_device *usb_dev;
87         struct usb_interface *usb_intf;
88         struct gb_host_device *hd;
89
90         struct es2_cport_in cport_in[NUM_BULKS];
91         struct es2_cport_out cport_out[NUM_BULKS];
92         struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
93         bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
94         bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
95         spinlock_t cport_out_urb_lock;
96
97         int *cport_to_ep;
98
99         struct task_struct *apb_log_task;
100         struct dentry *apb_log_dentry;
101         struct dentry *apb_log_enable_dentry;
102         DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
103 };
104
105 /**
106  * cport_to_ep - information about cport to endpoints mapping
107  * @cport_id: the id of cport to map to endpoints
108  * @endpoint_in: the endpoint number to use for in transfer
109  * @endpoint_out: he endpoint number to use for out transfer
110  */
111 struct cport_to_ep {
112         __le16 cport_id;
113         __u8 endpoint_in;
114         __u8 endpoint_out;
115 };
116
117 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
118 {
119         return (struct es2_ap_dev *)&hd->hd_priv;
120 }
121
122 static void cport_out_callback(struct urb *urb);
123 static void usb_log_enable(struct es2_ap_dev *es2);
124 static void usb_log_disable(struct es2_ap_dev *es2);
125
126 /* Get the endpoints pair mapped to the cport */
127 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
128 {
129         if (cport_id >= es2->hd->num_cports)
130                 return 0;
131         return es2->cport_to_ep[cport_id];
132 }
133
134 #define ES2_TIMEOUT     500     /* 500 ms for the SVC to do something */
135
136 /* Disable for now until we work all of this out to keep a warning-free build */
137 #if 0
138 /* Test if the endpoints pair is already mapped to a cport */
139 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
140 {
141         int i;
142
143         for (i = 0; i < es2->hd->num_cports; i++) {
144                 if (es2->cport_to_ep[i] == ep_pair)
145                         return 1;
146         }
147         return 0;
148 }
149
150 /* Configure the endpoint mapping and send the request to APBridge */
151 static int map_cport_to_ep(struct es2_ap_dev *es2,
152                                 u16 cport_id, int ep_pair)
153 {
154         int retval;
155         struct cport_to_ep *cport_to_ep;
156
157         if (ep_pair < 0 || ep_pair >= NUM_BULKS)
158                 return -EINVAL;
159         if (cport_id >= es2->hd->num_cports)
160                 return -EINVAL;
161         if (ep_pair && ep_pair_in_use(es2, ep_pair))
162                 return -EINVAL;
163
164         cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
165         if (!cport_to_ep)
166                 return -ENOMEM;
167
168         es2->cport_to_ep[cport_id] = ep_pair;
169         cport_to_ep->cport_id = cpu_to_le16(cport_id);
170         cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
171         cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
172
173         retval = usb_control_msg(es2->usb_dev,
174                                  usb_sndctrlpipe(es2->usb_dev, 0),
175                                  GB_APB_REQUEST_EP_MAPPING,
176                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
177                                  0x00, 0x00,
178                                  (char *)cport_to_ep,
179                                  sizeof(*cport_to_ep),
180                                  ES2_TIMEOUT);
181         if (retval == sizeof(*cport_to_ep))
182                 retval = 0;
183         kfree(cport_to_ep);
184
185         return retval;
186 }
187
188 /* Unmap a cport: use the muxed endpoints pair */
189 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
190 {
191         return map_cport_to_ep(es2, cport_id, 0);
192 }
193 #endif
194
195 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
196 {
197         struct usb_device *udev = es2->usb_dev;
198         u8 *data;
199         int retval;
200
201         data = kmalloc(size, GFP_KERNEL);
202         if (!data)
203                 return -ENOMEM;
204         memcpy(data, req, size);
205
206         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
207                                  cmd,
208                                  USB_DIR_OUT | USB_TYPE_VENDOR |
209                                  USB_RECIP_INTERFACE,
210                                  0, 0, data, size, ES2_TIMEOUT);
211         if (retval < 0)
212                 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
213         else
214                 retval = 0;
215
216         kfree(data);
217         return retval;
218 }
219
220 static void ap_urb_complete(struct urb *urb)
221 {
222         struct usb_ctrlrequest *dr = urb->context;
223
224         kfree(dr);
225         usb_free_urb(urb);
226 }
227
228 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
229 {
230         struct usb_device *udev = es2->usb_dev;
231         struct urb *urb;
232         struct usb_ctrlrequest *dr;
233         u8 *buf;
234         int retval;
235
236         urb = usb_alloc_urb(0, GFP_ATOMIC);
237         if (!urb)
238                 return -ENOMEM;
239
240         dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
241         if (!dr) {
242                 usb_free_urb(urb);
243                 return -ENOMEM;
244         }
245
246         buf = (u8 *)dr + sizeof(*dr);
247         memcpy(buf, req, size);
248
249         dr->bRequest = cmd;
250         dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
251         dr->wValue = 0;
252         dr->wIndex = 0;
253         dr->wLength = cpu_to_le16(size);
254
255         usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
256                              (unsigned char *)dr, buf, size,
257                              ap_urb_complete, dr);
258         retval = usb_submit_urb(urb, GFP_ATOMIC);
259         if (retval) {
260                 usb_free_urb(urb);
261                 kfree(dr);
262         }
263         return retval;
264 }
265
266 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
267                      bool async)
268 {
269         struct es2_ap_dev *es2 = hd_to_es2(hd);
270
271         if (async)
272                 return output_async(es2, req, size, cmd);
273
274         return output_sync(es2, req, size, cmd);
275 }
276
277 static int es2_cport_in_enable(struct es2_ap_dev *es2,
278                                 struct es2_cport_in *cport_in)
279 {
280         struct urb *urb;
281         int ret;
282         int i;
283
284         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
285                 urb = cport_in->urb[i];
286
287                 ret = usb_submit_urb(urb, GFP_KERNEL);
288                 if (ret) {
289                         dev_err(&es2->usb_dev->dev,
290                                         "failed to submit in-urb: %d\n", ret);
291                         goto err_kill_urbs;
292                 }
293         }
294
295         return 0;
296
297 err_kill_urbs:
298         for (--i; i >= 0; --i) {
299                 urb = cport_in->urb[i];
300                 usb_kill_urb(urb);
301         }
302
303         return ret;
304 }
305
306 static void es2_cport_in_disable(struct es2_ap_dev *es2,
307                                 struct es2_cport_in *cport_in)
308 {
309         struct urb *urb;
310         int i;
311
312         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
313                 urb = cport_in->urb[i];
314                 usb_kill_urb(urb);
315         }
316 }
317
318 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
319 {
320         struct urb *urb = NULL;
321         unsigned long flags;
322         int i;
323
324         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
325
326         /* Look in our pool of allocated urbs first, as that's the "fastest" */
327         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
328                 if (es2->cport_out_urb_busy[i] == false &&
329                                 es2->cport_out_urb_cancelled[i] == false) {
330                         es2->cport_out_urb_busy[i] = true;
331                         urb = es2->cport_out_urb[i];
332                         break;
333                 }
334         }
335         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
336         if (urb)
337                 return urb;
338
339         /*
340          * Crap, pool is empty, complain to the syslog and go allocate one
341          * dynamically as we have to succeed.
342          */
343         dev_dbg(&es2->usb_dev->dev,
344                 "No free CPort OUT urbs, having to dynamically allocate one!\n");
345         return usb_alloc_urb(0, gfp_mask);
346 }
347
348 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
349 {
350         unsigned long flags;
351         int i;
352         /*
353          * See if this was an urb in our pool, if so mark it "free", otherwise
354          * we need to free it ourselves.
355          */
356         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
357         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
358                 if (urb == es2->cport_out_urb[i]) {
359                         es2->cport_out_urb_busy[i] = false;
360                         urb = NULL;
361                         break;
362                 }
363         }
364         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
365
366         /* If urb is not NULL, then we need to free this urb */
367         usb_free_urb(urb);
368 }
369
370 /*
371  * We (ab)use the operation-message header pad bytes to transfer the
372  * cport id in order to minimise overhead.
373  */
374 static void
375 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
376 {
377         header->pad[0] = cport_id;
378 }
379
380 /* Clear the pad bytes used for the CPort id */
381 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
382 {
383         header->pad[0] = 0;
384 }
385
386 /* Extract the CPort id packed into the header, and clear it */
387 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
388 {
389         u16 cport_id = header->pad[0];
390
391         gb_message_cport_clear(header);
392
393         return cport_id;
394 }
395
396 /*
397  * Returns zero if the message was successfully queued, or a negative errno
398  * otherwise.
399  */
400 static int message_send(struct gb_host_device *hd, u16 cport_id,
401                         struct gb_message *message, gfp_t gfp_mask)
402 {
403         struct es2_ap_dev *es2 = hd_to_es2(hd);
404         struct usb_device *udev = es2->usb_dev;
405         size_t buffer_size;
406         int retval;
407         struct urb *urb;
408         int ep_pair;
409         unsigned long flags;
410
411         /*
412          * The data actually transferred will include an indication
413          * of where the data should be sent.  Do one last check of
414          * the target CPort id before filling it in.
415          */
416         if (!cport_id_valid(hd, cport_id)) {
417                 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
418                 return -EINVAL;
419         }
420
421         /* Find a free urb */
422         urb = next_free_urb(es2, gfp_mask);
423         if (!urb)
424                 return -ENOMEM;
425
426         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
427         message->hcpriv = urb;
428         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
429
430         /* Pack the cport id into the message header */
431         gb_message_cport_pack(message->header, cport_id);
432
433         buffer_size = sizeof(*message->header) + message->payload_size;
434
435         ep_pair = cport_to_ep_pair(es2, cport_id);
436         usb_fill_bulk_urb(urb, udev,
437                           usb_sndbulkpipe(udev,
438                                           es2->cport_out[ep_pair].endpoint),
439                           message->buffer, buffer_size,
440                           cport_out_callback, message);
441         urb->transfer_flags |= URB_ZERO_PACKET;
442         trace_gb_host_device_send(hd, cport_id, buffer_size);
443         retval = usb_submit_urb(urb, gfp_mask);
444         if (retval) {
445                 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
446
447                 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
448                 message->hcpriv = NULL;
449                 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
450
451                 free_urb(es2, urb);
452                 gb_message_cport_clear(message->header);
453
454                 return retval;
455         }
456
457         return 0;
458 }
459
460 /*
461  * Can not be called in atomic context.
462  */
463 static void message_cancel(struct gb_message *message)
464 {
465         struct gb_host_device *hd = message->operation->connection->hd;
466         struct es2_ap_dev *es2 = hd_to_es2(hd);
467         struct urb *urb;
468         int i;
469
470         might_sleep();
471
472         spin_lock_irq(&es2->cport_out_urb_lock);
473         urb = message->hcpriv;
474
475         /* Prevent dynamically allocated urb from being deallocated. */
476         usb_get_urb(urb);
477
478         /* Prevent pre-allocated urb from being reused. */
479         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
480                 if (urb == es2->cport_out_urb[i]) {
481                         es2->cport_out_urb_cancelled[i] = true;
482                         break;
483                 }
484         }
485         spin_unlock_irq(&es2->cport_out_urb_lock);
486
487         usb_kill_urb(urb);
488
489         if (i < NUM_CPORT_OUT_URB) {
490                 spin_lock_irq(&es2->cport_out_urb_lock);
491                 es2->cport_out_urb_cancelled[i] = false;
492                 spin_unlock_irq(&es2->cport_out_urb_lock);
493         }
494
495         usb_free_urb(urb);
496 }
497
498 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
499 {
500         struct es2_ap_dev *es2 = hd_to_es2(hd);
501         struct usb_device *udev = es2->usb_dev;
502         int retval;
503
504         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
505                                  GB_APB_REQUEST_RESET_CPORT,
506                                  USB_DIR_OUT | USB_TYPE_VENDOR |
507                                  USB_RECIP_INTERFACE, cport_id, 0,
508                                  NULL, 0, ES2_TIMEOUT);
509         if (retval < 0) {
510                 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
511                         retval);
512                 return retval;
513         }
514
515         return 0;
516 }
517
518 static int cport_enable(struct gb_host_device *hd, u16 cport_id)
519 {
520         int retval;
521
522         if (cport_id != GB_SVC_CPORT_ID) {
523                 retval = cport_reset(hd, cport_id);
524                 if (retval)
525                         return retval;
526         }
527
528         return 0;
529 }
530
531 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
532 {
533         int retval;
534         struct es2_ap_dev *es2 = hd_to_es2(hd);
535         struct usb_device *udev = es2->usb_dev;
536
537         if (!cport_id_valid(hd, cport_id)) {
538                 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
539                 return -EINVAL;
540         }
541
542         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
543                                  GB_APB_REQUEST_LATENCY_TAG_EN,
544                                  USB_DIR_OUT | USB_TYPE_VENDOR |
545                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
546                                  0, ES2_TIMEOUT);
547
548         if (retval < 0)
549                 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
550                         cport_id);
551         return retval;
552 }
553
554 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
555 {
556         int retval;
557         struct es2_ap_dev *es2 = hd_to_es2(hd);
558         struct usb_device *udev = es2->usb_dev;
559
560         if (!cport_id_valid(hd, cport_id)) {
561                 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
562                 return -EINVAL;
563         }
564
565         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
566                                  GB_APB_REQUEST_LATENCY_TAG_DIS,
567                                  USB_DIR_OUT | USB_TYPE_VENDOR |
568                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
569                                  0, ES2_TIMEOUT);
570
571         if (retval < 0)
572                 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
573                         cport_id);
574         return retval;
575 }
576
577 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
578 {
579         int retval;
580         struct es2_ap_dev *es2 = hd_to_es2(hd);
581         struct usb_device *udev = es2->usb_dev;
582
583         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
584                                  GB_APB_REQUEST_CPORT_FEAT_EN,
585                                  USB_DIR_OUT | USB_TYPE_VENDOR |
586                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
587                                  0, ES2_TIMEOUT);
588         if (retval < 0)
589                 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
590                         cport_id, retval);
591         return retval;
592 }
593
594 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
595 {
596         int retval;
597         struct es2_ap_dev *es2 = hd_to_es2(hd);
598         struct usb_device *udev = es2->usb_dev;
599
600         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
601                                  GB_APB_REQUEST_CPORT_FEAT_DIS,
602                                  USB_DIR_OUT | USB_TYPE_VENDOR |
603                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
604                                  0, ES2_TIMEOUT);
605         if (retval < 0)
606                 dev_err(&udev->dev,
607                         "Cannot disable CPort features for cport %u: %d\n",
608                         cport_id, retval);
609         return retval;
610 }
611
612 static struct gb_hd_driver es2_driver = {
613         .hd_priv_size           = sizeof(struct es2_ap_dev),
614         .message_send           = message_send,
615         .message_cancel         = message_cancel,
616         .cport_enable           = cport_enable,
617         .latency_tag_enable     = latency_tag_enable,
618         .latency_tag_disable    = latency_tag_disable,
619         .output                 = output,
620         .cport_features_enable  = cport_features_enable,
621         .cport_features_disable = cport_features_disable,
622 };
623
624 /* Common function to report consistent warnings based on URB status */
625 static int check_urb_status(struct urb *urb)
626 {
627         struct device *dev = &urb->dev->dev;
628         int status = urb->status;
629
630         switch (status) {
631         case 0:
632                 return 0;
633
634         case -EOVERFLOW:
635                 dev_err(dev, "%s: overflow actual length is %d\n",
636                         __func__, urb->actual_length);
637         case -ECONNRESET:
638         case -ENOENT:
639         case -ESHUTDOWN:
640         case -EILSEQ:
641         case -EPROTO:
642                 /* device is gone, stop sending */
643                 return status;
644         }
645         dev_err(dev, "%s: unknown status %d\n", __func__, status);
646
647         return -EAGAIN;
648 }
649
650 static void es2_destroy(struct es2_ap_dev *es2)
651 {
652         struct usb_device *udev;
653         int bulk_in;
654         int i;
655
656         debugfs_remove(es2->apb_log_enable_dentry);
657         usb_log_disable(es2);
658
659         /* Tear down everything! */
660         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
661                 struct urb *urb = es2->cport_out_urb[i];
662
663                 if (!urb)
664                         break;
665                 usb_kill_urb(urb);
666                 usb_free_urb(urb);
667                 es2->cport_out_urb[i] = NULL;
668                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
669         }
670
671         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
672                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
673
674                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
675                         struct urb *urb = cport_in->urb[i];
676
677                         if (!urb)
678                                 break;
679                         usb_free_urb(urb);
680                         kfree(cport_in->buffer[i]);
681                         cport_in->buffer[i] = NULL;
682                 }
683         }
684
685         kfree(es2->cport_to_ep);
686
687         udev = es2->usb_dev;
688         gb_hd_put(es2->hd);
689
690         usb_put_dev(udev);
691 }
692
693 static void ap_disconnect(struct usb_interface *interface)
694 {
695         struct es2_ap_dev *es2 = usb_get_intfdata(interface);
696         int i;
697
698         for (i = 0; i < NUM_BULKS; ++i)
699                 es2_cport_in_disable(es2, &es2->cport_in[i]);
700
701         gb_hd_del(es2->hd);
702
703         es2_destroy(es2);
704 }
705
706 static void cport_in_callback(struct urb *urb)
707 {
708         struct gb_host_device *hd = urb->context;
709         struct device *dev = &urb->dev->dev;
710         struct gb_operation_msg_hdr *header;
711         int status = check_urb_status(urb);
712         int retval;
713         u16 cport_id;
714
715         if (status) {
716                 if ((status == -EAGAIN) || (status == -EPROTO))
717                         goto exit;
718                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
719                 return;
720         }
721
722         if (urb->actual_length < sizeof(*header)) {
723                 dev_err(dev, "short message received\n");
724                 goto exit;
725         }
726
727         /* Extract the CPort id, which is packed in the message header */
728         header = urb->transfer_buffer;
729         cport_id = gb_message_cport_unpack(header);
730
731         if (cport_id_valid(hd, cport_id)) {
732                 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
733                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
734                                                         urb->actual_length);
735         } else {
736                 dev_err(dev, "invalid cport id %u received\n", cport_id);
737         }
738 exit:
739         /* put our urb back in the request pool */
740         retval = usb_submit_urb(urb, GFP_ATOMIC);
741         if (retval)
742                 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
743 }
744
745 static void cport_out_callback(struct urb *urb)
746 {
747         struct gb_message *message = urb->context;
748         struct gb_host_device *hd = message->operation->connection->hd;
749         struct es2_ap_dev *es2 = hd_to_es2(hd);
750         int status = check_urb_status(urb);
751         unsigned long flags;
752
753         gb_message_cport_clear(message->header);
754
755         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
756         message->hcpriv = NULL;
757         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
758
759         /*
760          * Tell the submitter that the message send (attempt) is
761          * complete, and report the status.
762          */
763         greybus_message_sent(hd, message, status);
764
765         free_urb(es2, urb);
766 }
767
768 #define APB1_LOG_MSG_SIZE       64
769 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
770 {
771         int retval;
772
773         /* SVC messages go down our control pipe */
774         do {
775                 retval = usb_control_msg(es2->usb_dev,
776                                         usb_rcvctrlpipe(es2->usb_dev, 0),
777                                         GB_APB_REQUEST_LOG,
778                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
779                                         0x00, 0x00,
780                                         buf,
781                                         APB1_LOG_MSG_SIZE,
782                                         ES2_TIMEOUT);
783                 if (retval > 0)
784                         kfifo_in(&es2->apb_log_fifo, buf, retval);
785         } while (retval > 0);
786 }
787
788 static int apb_log_poll(void *data)
789 {
790         struct es2_ap_dev *es2 = data;
791         char *buf;
792
793         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
794         if (!buf)
795                 return -ENOMEM;
796
797         while (!kthread_should_stop()) {
798                 msleep(1000);
799                 apb_log_get(es2, buf);
800         }
801
802         kfree(buf);
803
804         return 0;
805 }
806
807 static ssize_t apb_log_read(struct file *f, char __user *buf,
808                                 size_t count, loff_t *ppos)
809 {
810         struct es2_ap_dev *es2 = f->f_inode->i_private;
811         ssize_t ret;
812         size_t copied;
813         char *tmp_buf;
814
815         if (count > APB1_LOG_SIZE)
816                 count = APB1_LOG_SIZE;
817
818         tmp_buf = kmalloc(count, GFP_KERNEL);
819         if (!tmp_buf)
820                 return -ENOMEM;
821
822         copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
823         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
824
825         kfree(tmp_buf);
826
827         return ret;
828 }
829
830 static const struct file_operations apb_log_fops = {
831         .read   = apb_log_read,
832 };
833
834 static void usb_log_enable(struct es2_ap_dev *es2)
835 {
836         if (!IS_ERR_OR_NULL(es2->apb_log_task))
837                 return;
838
839         /* get log from APB1 */
840         es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
841         if (IS_ERR(es2->apb_log_task))
842                 return;
843         /* XXX We will need to rename this per APB */
844         es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
845                                                 gb_debugfs_get(), es2,
846                                                 &apb_log_fops);
847 }
848
849 static void usb_log_disable(struct es2_ap_dev *es2)
850 {
851         if (IS_ERR_OR_NULL(es2->apb_log_task))
852                 return;
853
854         debugfs_remove(es2->apb_log_dentry);
855         es2->apb_log_dentry = NULL;
856
857         kthread_stop(es2->apb_log_task);
858         es2->apb_log_task = NULL;
859 }
860
861 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
862                                 size_t count, loff_t *ppos)
863 {
864         struct es2_ap_dev *es2 = f->f_inode->i_private;
865         int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
866         char tmp_buf[3];
867
868         sprintf(tmp_buf, "%d\n", enable);
869         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
870 }
871
872 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
873                                 size_t count, loff_t *ppos)
874 {
875         int enable;
876         ssize_t retval;
877         struct es2_ap_dev *es2 = f->f_inode->i_private;
878
879         retval = kstrtoint_from_user(buf, count, 10, &enable);
880         if (retval)
881                 return retval;
882
883         if (enable)
884                 usb_log_enable(es2);
885         else
886                 usb_log_disable(es2);
887
888         return count;
889 }
890
891 static const struct file_operations apb_log_enable_fops = {
892         .read   = apb_log_enable_read,
893         .write  = apb_log_enable_write,
894 };
895
896 static int apb_get_cport_count(struct usb_device *udev)
897 {
898         int retval;
899         __le16 *cport_count;
900
901         cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
902         if (!cport_count)
903                 return -ENOMEM;
904
905         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
906                                  GB_APB_REQUEST_CPORT_COUNT,
907                                  USB_DIR_IN | USB_TYPE_VENDOR |
908                                  USB_RECIP_INTERFACE, 0, 0, cport_count,
909                                  sizeof(*cport_count), ES2_TIMEOUT);
910         if (retval != sizeof(*cport_count)) {
911                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
912                         retval);
913
914                 if (retval >= 0)
915                         retval = -EIO;
916
917                 goto out;
918         }
919
920         retval = le16_to_cpu(*cport_count);
921
922         /* We need to fit a CPort ID in one byte of a message header */
923         if (retval > U8_MAX) {
924                 retval = U8_MAX;
925                 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
926         }
927
928 out:
929         kfree(cport_count);
930         return retval;
931 }
932
933 /*
934  * The ES2 USB Bridge device has 15 endpoints
935  * 1 Control - usual USB stuff + AP -> APBridgeA messages
936  * 7 Bulk IN - CPort data in
937  * 7 Bulk OUT - CPort data out
938  */
939 static int ap_probe(struct usb_interface *interface,
940                     const struct usb_device_id *id)
941 {
942         struct es2_ap_dev *es2;
943         struct gb_host_device *hd;
944         struct usb_device *udev;
945         struct usb_host_interface *iface_desc;
946         struct usb_endpoint_descriptor *endpoint;
947         int bulk_in = 0;
948         int bulk_out = 0;
949         int retval = -ENOMEM;
950         int i;
951         int num_cports;
952         int cport_id;
953
954         udev = usb_get_dev(interface_to_usbdev(interface));
955
956         num_cports = apb_get_cport_count(udev);
957         if (num_cports < 0) {
958                 usb_put_dev(udev);
959                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
960                         num_cports);
961                 return num_cports;
962         }
963
964         hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
965                                 num_cports);
966         if (IS_ERR(hd)) {
967                 usb_put_dev(udev);
968                 return PTR_ERR(hd);
969         }
970
971         /*
972          * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
973          * won't be allocated dynamically.
974          */
975         do {
976                 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
977         } while (cport_id > 0);
978
979         es2 = hd_to_es2(hd);
980         es2->hd = hd;
981         es2->usb_intf = interface;
982         es2->usb_dev = udev;
983         spin_lock_init(&es2->cport_out_urb_lock);
984         INIT_KFIFO(es2->apb_log_fifo);
985         usb_set_intfdata(interface, es2);
986
987         es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
988                                    GFP_KERNEL);
989         if (!es2->cport_to_ep) {
990                 retval = -ENOMEM;
991                 goto error;
992         }
993
994         /* find all bulk endpoints */
995         iface_desc = interface->cur_altsetting;
996         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
997                 endpoint = &iface_desc->endpoint[i].desc;
998
999                 if (usb_endpoint_is_bulk_in(endpoint)) {
1000                         es2->cport_in[bulk_in++].endpoint =
1001                                 endpoint->bEndpointAddress;
1002                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1003                         es2->cport_out[bulk_out++].endpoint =
1004                                 endpoint->bEndpointAddress;
1005                 } else {
1006                         dev_err(&udev->dev,
1007                                 "Unknown endpoint type found, address 0x%02x\n",
1008                                 endpoint->bEndpointAddress);
1009                 }
1010         }
1011         if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
1012                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1013                 goto error;
1014         }
1015
1016         /* Allocate buffers for our cport in messages */
1017         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1018                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1019
1020                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1021                         struct urb *urb;
1022                         u8 *buffer;
1023
1024                         urb = usb_alloc_urb(0, GFP_KERNEL);
1025                         if (!urb)
1026                                 goto error;
1027                         buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1028                         if (!buffer)
1029                                 goto error;
1030
1031                         usb_fill_bulk_urb(urb, udev,
1032                                           usb_rcvbulkpipe(udev,
1033                                                           cport_in->endpoint),
1034                                           buffer, ES2_GBUF_MSG_SIZE_MAX,
1035                                           cport_in_callback, hd);
1036                         cport_in->urb[i] = urb;
1037                         cport_in->buffer[i] = buffer;
1038                 }
1039         }
1040
1041         /* Allocate urbs for our CPort OUT messages */
1042         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1043                 struct urb *urb;
1044
1045                 urb = usb_alloc_urb(0, GFP_KERNEL);
1046                 if (!urb)
1047                         goto error;
1048
1049                 es2->cport_out_urb[i] = urb;
1050                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
1051         }
1052
1053         /* XXX We will need to rename this per APB */
1054         es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1055                                                         (S_IWUSR | S_IRUGO),
1056                                                         gb_debugfs_get(), es2,
1057                                                         &apb_log_enable_fops);
1058
1059         retval = gb_hd_add(hd);
1060         if (retval)
1061                 goto error;
1062
1063         for (i = 0; i < NUM_BULKS; ++i) {
1064                 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1065                 if (retval)
1066                         goto err_disable_cport_in;
1067         }
1068
1069         return 0;
1070
1071 err_disable_cport_in:
1072         for (--i; i >= 0; --i)
1073                 es2_cport_in_disable(es2, &es2->cport_in[i]);
1074         gb_hd_del(hd);
1075 error:
1076         es2_destroy(es2);
1077
1078         return retval;
1079 }
1080
1081 static struct usb_driver es2_ap_driver = {
1082         .name =         "es2_ap_driver",
1083         .probe =        ap_probe,
1084         .disconnect =   ap_disconnect,
1085         .id_table =     id_table,
1086         .soft_unbind =  1,
1087 };
1088
1089 module_usb_driver(es2_ap_driver);
1090
1091 MODULE_LICENSE("GPL v2");
1092 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");