greybus: es2: fix cport-count error handling
[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 struct gb_hd_driver es2_driver = {
578         .hd_priv_size           = sizeof(struct es2_ap_dev),
579         .message_send           = message_send,
580         .message_cancel         = message_cancel,
581         .cport_enable           = cport_enable,
582         .latency_tag_enable     = latency_tag_enable,
583         .latency_tag_disable    = latency_tag_disable,
584         .output                 = output,
585 };
586
587 /* Common function to report consistent warnings based on URB status */
588 static int check_urb_status(struct urb *urb)
589 {
590         struct device *dev = &urb->dev->dev;
591         int status = urb->status;
592
593         switch (status) {
594         case 0:
595                 return 0;
596
597         case -EOVERFLOW:
598                 dev_err(dev, "%s: overflow actual length is %d\n",
599                         __func__, urb->actual_length);
600         case -ECONNRESET:
601         case -ENOENT:
602         case -ESHUTDOWN:
603         case -EILSEQ:
604         case -EPROTO:
605                 /* device is gone, stop sending */
606                 return status;
607         }
608         dev_err(dev, "%s: unknown status %d\n", __func__, status);
609
610         return -EAGAIN;
611 }
612
613 static void es2_destroy(struct es2_ap_dev *es2)
614 {
615         struct usb_device *udev;
616         int bulk_in;
617         int i;
618
619         debugfs_remove(es2->apb_log_enable_dentry);
620         usb_log_disable(es2);
621
622         /* Tear down everything! */
623         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
624                 struct urb *urb = es2->cport_out_urb[i];
625
626                 if (!urb)
627                         break;
628                 usb_kill_urb(urb);
629                 usb_free_urb(urb);
630                 es2->cport_out_urb[i] = NULL;
631                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
632         }
633
634         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
635                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
636
637                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
638                         struct urb *urb = cport_in->urb[i];
639
640                         if (!urb)
641                                 break;
642                         usb_free_urb(urb);
643                         kfree(cport_in->buffer[i]);
644                         cport_in->buffer[i] = NULL;
645                 }
646         }
647
648         kfree(es2->cport_to_ep);
649
650         udev = es2->usb_dev;
651         gb_hd_put(es2->hd);
652
653         usb_put_dev(udev);
654 }
655
656 static void ap_disconnect(struct usb_interface *interface)
657 {
658         struct es2_ap_dev *es2 = usb_get_intfdata(interface);
659         int i;
660
661         for (i = 0; i < NUM_BULKS; ++i)
662                 es2_cport_in_disable(es2, &es2->cport_in[i]);
663
664         gb_hd_del(es2->hd);
665
666         es2_destroy(es2);
667 }
668
669 static void cport_in_callback(struct urb *urb)
670 {
671         struct gb_host_device *hd = urb->context;
672         struct device *dev = &urb->dev->dev;
673         struct gb_operation_msg_hdr *header;
674         int status = check_urb_status(urb);
675         int retval;
676         u16 cport_id;
677
678         if (status) {
679                 if ((status == -EAGAIN) || (status == -EPROTO))
680                         goto exit;
681                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
682                 return;
683         }
684
685         if (urb->actual_length < sizeof(*header)) {
686                 dev_err(dev, "short message received\n");
687                 goto exit;
688         }
689
690         /* Extract the CPort id, which is packed in the message header */
691         header = urb->transfer_buffer;
692         cport_id = gb_message_cport_unpack(header);
693
694         if (cport_id_valid(hd, cport_id)) {
695                 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
696                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
697                                                         urb->actual_length);
698         } else {
699                 dev_err(dev, "invalid cport id %u received\n", cport_id);
700         }
701 exit:
702         /* put our urb back in the request pool */
703         retval = usb_submit_urb(urb, GFP_ATOMIC);
704         if (retval)
705                 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
706 }
707
708 static void cport_out_callback(struct urb *urb)
709 {
710         struct gb_message *message = urb->context;
711         struct gb_host_device *hd = message->operation->connection->hd;
712         struct es2_ap_dev *es2 = hd_to_es2(hd);
713         int status = check_urb_status(urb);
714         unsigned long flags;
715
716         gb_message_cport_clear(message->header);
717
718         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
719         message->hcpriv = NULL;
720         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
721
722         /*
723          * Tell the submitter that the message send (attempt) is
724          * complete, and report the status.
725          */
726         greybus_message_sent(hd, message, status);
727
728         free_urb(es2, urb);
729 }
730
731 #define APB1_LOG_MSG_SIZE       64
732 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
733 {
734         int retval;
735
736         /* SVC messages go down our control pipe */
737         do {
738                 retval = usb_control_msg(es2->usb_dev,
739                                         usb_rcvctrlpipe(es2->usb_dev, 0),
740                                         GB_APB_REQUEST_LOG,
741                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
742                                         0x00, 0x00,
743                                         buf,
744                                         APB1_LOG_MSG_SIZE,
745                                         ES2_TIMEOUT);
746                 if (retval > 0)
747                         kfifo_in(&es2->apb_log_fifo, buf, retval);
748         } while (retval > 0);
749 }
750
751 static int apb_log_poll(void *data)
752 {
753         struct es2_ap_dev *es2 = data;
754         char *buf;
755
756         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
757         if (!buf)
758                 return -ENOMEM;
759
760         while (!kthread_should_stop()) {
761                 msleep(1000);
762                 apb_log_get(es2, buf);
763         }
764
765         kfree(buf);
766
767         return 0;
768 }
769
770 static ssize_t apb_log_read(struct file *f, char __user *buf,
771                                 size_t count, loff_t *ppos)
772 {
773         struct es2_ap_dev *es2 = f->f_inode->i_private;
774         ssize_t ret;
775         size_t copied;
776         char *tmp_buf;
777
778         if (count > APB1_LOG_SIZE)
779                 count = APB1_LOG_SIZE;
780
781         tmp_buf = kmalloc(count, GFP_KERNEL);
782         if (!tmp_buf)
783                 return -ENOMEM;
784
785         copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
786         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
787
788         kfree(tmp_buf);
789
790         return ret;
791 }
792
793 static const struct file_operations apb_log_fops = {
794         .read   = apb_log_read,
795 };
796
797 static void usb_log_enable(struct es2_ap_dev *es2)
798 {
799         if (!IS_ERR_OR_NULL(es2->apb_log_task))
800                 return;
801
802         /* get log from APB1 */
803         es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
804         if (IS_ERR(es2->apb_log_task))
805                 return;
806         /* XXX We will need to rename this per APB */
807         es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
808                                                 gb_debugfs_get(), NULL,
809                                                 &apb_log_fops);
810 }
811
812 static void usb_log_disable(struct es2_ap_dev *es2)
813 {
814         if (IS_ERR_OR_NULL(es2->apb_log_task))
815                 return;
816
817         debugfs_remove(es2->apb_log_dentry);
818         es2->apb_log_dentry = NULL;
819
820         kthread_stop(es2->apb_log_task);
821         es2->apb_log_task = NULL;
822 }
823
824 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
825                                 size_t count, loff_t *ppos)
826 {
827         struct es2_ap_dev *es2 = f->f_inode->i_private;
828         int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
829         char tmp_buf[3];
830
831         sprintf(tmp_buf, "%d\n", enable);
832         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
833 }
834
835 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
836                                 size_t count, loff_t *ppos)
837 {
838         int enable;
839         ssize_t retval;
840         struct es2_ap_dev *es2 = f->f_inode->i_private;
841
842         retval = kstrtoint_from_user(buf, count, 10, &enable);
843         if (retval)
844                 return retval;
845
846         if (enable)
847                 usb_log_enable(es2);
848         else
849                 usb_log_disable(es2);
850
851         return count;
852 }
853
854 static const struct file_operations apb_log_enable_fops = {
855         .read   = apb_log_enable_read,
856         .write  = apb_log_enable_write,
857 };
858
859 static int apb_get_cport_count(struct usb_device *udev)
860 {
861         int retval;
862         __le16 *cport_count;
863
864         cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
865         if (!cport_count)
866                 return -ENOMEM;
867
868         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
869                                  GB_APB_REQUEST_CPORT_COUNT,
870                                  USB_DIR_IN | USB_TYPE_VENDOR |
871                                  USB_RECIP_INTERFACE, 0, 0, cport_count,
872                                  sizeof(*cport_count), ES2_TIMEOUT);
873         if (retval != sizeof(*cport_count)) {
874                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
875                         retval);
876
877                 if (retval >= 0)
878                         retval = -EIO;
879
880                 goto out;
881         }
882
883         retval = le16_to_cpu(*cport_count);
884
885         /* We need to fit a CPort ID in one byte of a message header */
886         if (retval > U8_MAX) {
887                 retval = U8_MAX;
888                 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
889         }
890
891 out:
892         kfree(cport_count);
893         return retval;
894 }
895
896 /*
897  * The ES2 USB Bridge device has 15 endpoints
898  * 1 Control - usual USB stuff + AP -> APBridgeA messages
899  * 7 Bulk IN - CPort data in
900  * 7 Bulk OUT - CPort data out
901  */
902 static int ap_probe(struct usb_interface *interface,
903                     const struct usb_device_id *id)
904 {
905         struct es2_ap_dev *es2;
906         struct gb_host_device *hd;
907         struct usb_device *udev;
908         struct usb_host_interface *iface_desc;
909         struct usb_endpoint_descriptor *endpoint;
910         int bulk_in = 0;
911         int bulk_out = 0;
912         int retval = -ENOMEM;
913         int i;
914         int num_cports;
915         int cport_id;
916
917         udev = usb_get_dev(interface_to_usbdev(interface));
918
919         num_cports = apb_get_cport_count(udev);
920         if (num_cports < 0) {
921                 usb_put_dev(udev);
922                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
923                         num_cports);
924                 return num_cports;
925         }
926
927         hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
928                                 num_cports);
929         if (IS_ERR(hd)) {
930                 usb_put_dev(udev);
931                 return PTR_ERR(hd);
932         }
933
934         /*
935          * CPorts 16 and 17 are reserved for CDSI0 and CDSI1, make sure they
936          * won't be allocated dynamically.
937          */
938         do {
939                 cport_id = ida_simple_get(&hd->cport_id_map, 16, 18, GFP_KERNEL);
940         } while (cport_id > 0);
941
942         es2 = hd_to_es2(hd);
943         es2->hd = hd;
944         es2->usb_intf = interface;
945         es2->usb_dev = udev;
946         spin_lock_init(&es2->cport_out_urb_lock);
947         INIT_KFIFO(es2->apb_log_fifo);
948         usb_set_intfdata(interface, es2);
949
950         es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
951                                    GFP_KERNEL);
952         if (!es2->cport_to_ep) {
953                 retval = -ENOMEM;
954                 goto error;
955         }
956
957         /* find all bulk endpoints */
958         iface_desc = interface->cur_altsetting;
959         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
960                 endpoint = &iface_desc->endpoint[i].desc;
961
962                 if (usb_endpoint_is_bulk_in(endpoint)) {
963                         es2->cport_in[bulk_in++].endpoint =
964                                 endpoint->bEndpointAddress;
965                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
966                         es2->cport_out[bulk_out++].endpoint =
967                                 endpoint->bEndpointAddress;
968                 } else {
969                         dev_err(&udev->dev,
970                                 "Unknown endpoint type found, address 0x%02x\n",
971                                 endpoint->bEndpointAddress);
972                 }
973         }
974         if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
975                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
976                 goto error;
977         }
978
979         /* Allocate buffers for our cport in messages */
980         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
981                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
982
983                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
984                         struct urb *urb;
985                         u8 *buffer;
986
987                         urb = usb_alloc_urb(0, GFP_KERNEL);
988                         if (!urb)
989                                 goto error;
990                         buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
991                         if (!buffer)
992                                 goto error;
993
994                         usb_fill_bulk_urb(urb, udev,
995                                           usb_rcvbulkpipe(udev,
996                                                           cport_in->endpoint),
997                                           buffer, ES2_GBUF_MSG_SIZE_MAX,
998                                           cport_in_callback, hd);
999                         cport_in->urb[i] = urb;
1000                         cport_in->buffer[i] = buffer;
1001                 }
1002         }
1003
1004         /* Allocate urbs for our CPort OUT messages */
1005         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1006                 struct urb *urb;
1007
1008                 urb = usb_alloc_urb(0, GFP_KERNEL);
1009                 if (!urb)
1010                         goto error;
1011
1012                 es2->cport_out_urb[i] = urb;
1013                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
1014         }
1015
1016         /* XXX We will need to rename this per APB */
1017         es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1018                                                         (S_IWUSR | S_IRUGO),
1019                                                         gb_debugfs_get(), es2,
1020                                                         &apb_log_enable_fops);
1021
1022         retval = gb_hd_add(hd);
1023         if (retval)
1024                 goto error;
1025
1026         for (i = 0; i < NUM_BULKS; ++i) {
1027                 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1028                 if (retval)
1029                         goto err_disable_cport_in;
1030         }
1031
1032         return 0;
1033
1034 err_disable_cport_in:
1035         for (--i; i >= 0; --i)
1036                 es2_cport_in_disable(es2, &es2->cport_in[i]);
1037         gb_hd_del(hd);
1038 error:
1039         es2_destroy(es2);
1040
1041         return retval;
1042 }
1043
1044 static struct usb_driver es2_ap_driver = {
1045         .name =         "es2_ap_driver",
1046         .probe =        ap_probe,
1047         .disconnect =   ap_disconnect,
1048         .id_table =     id_table,
1049 };
1050
1051 module_usb_driver(es2_ap_driver);
1052
1053 MODULE_LICENSE("GPL v2");
1054 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");