greybus: es1, es2: hook tracepoints to hardware send/recv operations
[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 ES1 controller */
22 #define ES1_GBUF_MSG_SIZE_MAX   2048
23
24 static const struct usb_device_id id_table[] = {
25         /* Made up numbers for the SVC USB Bridge in ES2 */
26         { USB_DEVICE(0xffff, 0x0002) },
27         { },
28 };
29 MODULE_DEVICE_TABLE(usb, id_table);
30
31 #define APB1_LOG_SIZE           SZ_16K
32 static struct dentry *apb1_log_dentry;
33 static struct dentry *apb1_log_enable_dentry;
34 static struct task_struct *apb1_log_task;
35 static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE);
36
37 /* Number of bulk in and bulk out couple */
38 #define NUM_BULKS               7
39
40 /*
41  * Number of CPort IN urbs in flight at any point in time.
42  * Adjust if we are having stalls in the USB buffer due to not enough urbs in
43  * flight.
44  */
45 #define NUM_CPORT_IN_URB        4
46
47 /* Number of CPort OUT urbs in flight at any point in time.
48  * Adjust if we get messages saying we are out of urbs in the system log.
49  */
50 #define NUM_CPORT_OUT_URB       (8 * NUM_BULKS)
51
52 /* vendor request APB1 log */
53 #define REQUEST_LOG             0x02
54
55 /* vendor request to map a cport to bulk in and bulk out endpoints */
56 #define REQUEST_EP_MAPPING      0x03
57
58 /* vendor request to get the number of cports available */
59 #define REQUEST_CPORT_COUNT     0x04
60
61 /*
62  * @endpoint: bulk in endpoint for CPort data
63  * @urb: array of urbs for the CPort in messages
64  * @buffer: array of buffers for the @cport_in_urb urbs
65  */
66 struct es1_cport_in {
67         __u8 endpoint;
68         struct urb *urb[NUM_CPORT_IN_URB];
69         u8 *buffer[NUM_CPORT_IN_URB];
70 };
71
72 /*
73  * @endpoint: bulk out endpoint for CPort data
74  */
75 struct es1_cport_out {
76         __u8 endpoint;
77 };
78
79 /**
80  * es1_ap_dev - ES1 USB Bridge to AP structure
81  * @usb_dev: pointer to the USB device we are.
82  * @usb_intf: pointer to the USB interface we are bound to.
83  * @hd: pointer to our greybus_host_device structure
84
85  * @cport_in: endpoint, urbs and buffer for cport in messages
86  * @cport_out: endpoint for for cport out messages
87  * @cport_out_urb: array of urbs for the CPort out messages
88  * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
89  *                      not.
90  * @cport_out_urb_cancelled: array of flags indicating whether the
91  *                      corresponding @cport_out_urb is being cancelled
92  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
93  */
94 struct es1_ap_dev {
95         struct usb_device *usb_dev;
96         struct usb_interface *usb_intf;
97         struct greybus_host_device *hd;
98
99         struct es1_cport_in cport_in[NUM_BULKS];
100         struct es1_cport_out cport_out[NUM_BULKS];
101         struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
102         bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
103         bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
104         spinlock_t cport_out_urb_lock;
105
106         int *cport_to_ep;
107 };
108
109 /**
110  * cport_to_ep - information about cport to endpoints mapping
111  * @cport_id: the id of cport to map to endpoints
112  * @endpoint_in: the endpoint number to use for in transfer
113  * @endpoint_out: he endpoint number to use for out transfer
114  */
115 struct cport_to_ep {
116         __le16 cport_id;
117         __u8 endpoint_in;
118         __u8 endpoint_out;
119 };
120
121 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
122 {
123         return (struct es1_ap_dev *)&hd->hd_priv;
124 }
125
126 static void cport_out_callback(struct urb *urb);
127 static void usb_log_enable(struct es1_ap_dev *es1);
128 static void usb_log_disable(struct es1_ap_dev *es1);
129
130 /* Get the endpoints pair mapped to the cport */
131 static int cport_to_ep_pair(struct es1_ap_dev *es1, u16 cport_id)
132 {
133         if (cport_id >= es1->hd->num_cports)
134                 return 0;
135         return es1->cport_to_ep[cport_id];
136 }
137
138 #define ES1_TIMEOUT     500     /* 500 ms for the SVC to do something */
139
140 /* Disable for now until we work all of this out to keep a warning-free build */
141 #if 0
142 /* Test if the endpoints pair is already mapped to a cport */
143 static int ep_pair_in_use(struct es1_ap_dev *es1, int ep_pair)
144 {
145         int i;
146
147         for (i = 0; i < es1->hd->num_cports; i++) {
148                 if (es1->cport_to_ep[i] == ep_pair)
149                         return 1;
150         }
151         return 0;
152 }
153
154 /* Configure the endpoint mapping and send the request to APBridge */
155 static int map_cport_to_ep(struct es1_ap_dev *es1,
156                                 u16 cport_id, int ep_pair)
157 {
158         int retval;
159         struct cport_to_ep *cport_to_ep;
160
161         if (ep_pair < 0 || ep_pair >= NUM_BULKS)
162                 return -EINVAL;
163         if (cport_id >= es1->hd->num_cports)
164                 return -EINVAL;
165         if (ep_pair && ep_pair_in_use(es1, ep_pair))
166                 return -EINVAL;
167
168         cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
169         if (!cport_to_ep)
170                 return -ENOMEM;
171
172         es1->cport_to_ep[cport_id] = ep_pair;
173         cport_to_ep->cport_id = cpu_to_le16(cport_id);
174         cport_to_ep->endpoint_in = es1->cport_in[ep_pair].endpoint;
175         cport_to_ep->endpoint_out = es1->cport_out[ep_pair].endpoint;
176
177         retval = usb_control_msg(es1->usb_dev,
178                                  usb_sndctrlpipe(es1->usb_dev, 0),
179                                  REQUEST_EP_MAPPING,
180                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
181                                  0x00, 0x00,
182                                  (char *)cport_to_ep,
183                                  sizeof(*cport_to_ep),
184                                  ES1_TIMEOUT);
185         if (retval == sizeof(*cport_to_ep))
186                 retval = 0;
187         kfree(cport_to_ep);
188
189         return retval;
190 }
191
192 /* Unmap a cport: use the muxed endpoints pair */
193 static int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
194 {
195         return map_cport_to_ep(es1, cport_id, 0);
196 }
197 #endif
198
199 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
200 {
201         struct urb *urb = NULL;
202         unsigned long flags;
203         int i;
204
205         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
206
207         /* Look in our pool of allocated urbs first, as that's the "fastest" */
208         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
209                 if (es1->cport_out_urb_busy[i] == false &&
210                                 es1->cport_out_urb_cancelled[i] == false) {
211                         es1->cport_out_urb_busy[i] = true;
212                         urb = es1->cport_out_urb[i];
213                         break;
214                 }
215         }
216         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
217         if (urb)
218                 return urb;
219
220         /*
221          * Crap, pool is empty, complain to the syslog and go allocate one
222          * dynamically as we have to succeed.
223          */
224         dev_err(&es1->usb_dev->dev,
225                 "No free CPort OUT urbs, having to dynamically allocate one!\n");
226         return usb_alloc_urb(0, gfp_mask);
227 }
228
229 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
230 {
231         unsigned long flags;
232         int i;
233         /*
234          * See if this was an urb in our pool, if so mark it "free", otherwise
235          * we need to free it ourselves.
236          */
237         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
238         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
239                 if (urb == es1->cport_out_urb[i]) {
240                         es1->cport_out_urb_busy[i] = false;
241                         urb = NULL;
242                         break;
243                 }
244         }
245         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
246
247         /* If urb is not NULL, then we need to free this urb */
248         usb_free_urb(urb);
249 }
250
251 /*
252  * We (ab)use the operation-message header pad bytes to transfer the
253  * cport id in order to minimise overhead.
254  */
255 static void
256 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
257 {
258         header->pad[0] = cport_id;
259 }
260
261 /* Clear the pad bytes used for the CPort id */
262 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
263 {
264         header->pad[0] = 0;
265 }
266
267 /* Extract the CPort id packed into the header, and clear it */
268 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
269 {
270         u16 cport_id = header->pad[0];
271
272         gb_message_cport_clear(header);
273
274         return cport_id;
275 }
276
277 /*
278  * Returns zero if the message was successfully queued, or a negative errno
279  * otherwise.
280  */
281 static int message_send(struct greybus_host_device *hd, u16 cport_id,
282                         struct gb_message *message, gfp_t gfp_mask)
283 {
284         struct es1_ap_dev *es1 = hd_to_es1(hd);
285         struct usb_device *udev = es1->usb_dev;
286         size_t buffer_size;
287         int retval;
288         struct urb *urb;
289         int ep_pair;
290         unsigned long flags;
291
292         /*
293          * The data actually transferred will include an indication
294          * of where the data should be sent.  Do one last check of
295          * the target CPort id before filling it in.
296          */
297         if (!cport_id_valid(hd, cport_id)) {
298                 pr_err("invalid destination cport 0x%02x\n", cport_id);
299                 return -EINVAL;
300         }
301
302         /* Find a free urb */
303         urb = next_free_urb(es1, gfp_mask);
304         if (!urb)
305                 return -ENOMEM;
306
307         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
308         message->hcpriv = urb;
309         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
310
311         /* Pack the cport id into the message header */
312         gb_message_cport_pack(message->header, cport_id);
313
314         buffer_size = sizeof(*message->header) + message->payload_size;
315
316         ep_pair = cport_to_ep_pair(es1, cport_id);
317         usb_fill_bulk_urb(urb, udev,
318                           usb_sndbulkpipe(udev,
319                                           es1->cport_out[ep_pair].endpoint),
320                           message->buffer, buffer_size,
321                           cport_out_callback, message);
322         urb->transfer_flags |= URB_ZERO_PACKET;
323         trace_gb_host_device_send(hd, cport_id, buffer_size);
324         retval = usb_submit_urb(urb, gfp_mask);
325         if (retval) {
326                 pr_err("error %d submitting URB\n", retval);
327
328                 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
329                 message->hcpriv = NULL;
330                 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
331
332                 free_urb(es1, urb);
333                 gb_message_cport_clear(message->header);
334
335                 return retval;
336         }
337
338         return 0;
339 }
340
341 /*
342  * Can not be called in atomic context.
343  */
344 static void message_cancel(struct gb_message *message)
345 {
346         struct greybus_host_device *hd = message->operation->connection->hd;
347         struct es1_ap_dev *es1 = hd_to_es1(hd);
348         struct urb *urb;
349         int i;
350
351         might_sleep();
352
353         spin_lock_irq(&es1->cport_out_urb_lock);
354         urb = message->hcpriv;
355
356         /* Prevent dynamically allocated urb from being deallocated. */
357         usb_get_urb(urb);
358
359         /* Prevent pre-allocated urb from being reused. */
360         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
361                 if (urb == es1->cport_out_urb[i]) {
362                         es1->cport_out_urb_cancelled[i] = true;
363                         break;
364                 }
365         }
366         spin_unlock_irq(&es1->cport_out_urb_lock);
367
368         usb_kill_urb(urb);
369
370         if (i < NUM_CPORT_OUT_URB) {
371                 spin_lock_irq(&es1->cport_out_urb_lock);
372                 es1->cport_out_urb_cancelled[i] = false;
373                 spin_unlock_irq(&es1->cport_out_urb_lock);
374         }
375
376         usb_free_urb(urb);
377 }
378
379 static struct greybus_host_driver es1_driver = {
380         .hd_priv_size           = sizeof(struct es1_ap_dev),
381         .message_send           = message_send,
382         .message_cancel         = message_cancel,
383 };
384
385 /* Common function to report consistent warnings based on URB status */
386 static int check_urb_status(struct urb *urb)
387 {
388         struct device *dev = &urb->dev->dev;
389         int status = urb->status;
390
391         switch (status) {
392         case 0:
393                 return 0;
394
395         case -EOVERFLOW:
396                 dev_err(dev, "%s: overflow actual length is %d\n",
397                         __func__, urb->actual_length);
398         case -ECONNRESET:
399         case -ENOENT:
400         case -ESHUTDOWN:
401         case -EILSEQ:
402         case -EPROTO:
403                 /* device is gone, stop sending */
404                 return status;
405         }
406         dev_err(dev, "%s: unknown status %d\n", __func__, status);
407
408         return -EAGAIN;
409 }
410
411 static void ap_disconnect(struct usb_interface *interface)
412 {
413         struct es1_ap_dev *es1;
414         struct usb_device *udev;
415         int bulk_in;
416         int i;
417
418         es1 = usb_get_intfdata(interface);
419         if (!es1)
420                 return;
421
422         usb_log_disable(es1);
423
424         /* Tear down everything! */
425         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
426                 struct urb *urb = es1->cport_out_urb[i];
427
428                 if (!urb)
429                         break;
430                 usb_kill_urb(urb);
431                 usb_free_urb(urb);
432                 es1->cport_out_urb[i] = NULL;
433                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
434         }
435
436         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
437                 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
438                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
439                         struct urb *urb = cport_in->urb[i];
440
441                         if (!urb)
442                                 break;
443                         usb_kill_urb(urb);
444                         usb_free_urb(urb);
445                         kfree(cport_in->buffer[i]);
446                         cport_in->buffer[i] = NULL;
447                 }
448         }
449
450         usb_set_intfdata(interface, NULL);
451         udev = es1->usb_dev;
452         greybus_remove_hd(es1->hd);
453         kfree(es1->cport_to_ep);
454
455         usb_put_dev(udev);
456 }
457
458 static void cport_in_callback(struct urb *urb)
459 {
460         struct greybus_host_device *hd = urb->context;
461         struct device *dev = &urb->dev->dev;
462         struct gb_operation_msg_hdr *header;
463         int status = check_urb_status(urb);
464         int retval;
465         u16 cport_id;
466
467         if (status) {
468                 if ((status == -EAGAIN) || (status == -EPROTO))
469                         goto exit;
470                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
471                 return;
472         }
473
474         if (urb->actual_length < sizeof(*header)) {
475                 dev_err(dev, "%s: short message received\n", __func__);
476                 goto exit;
477         }
478
479         /* Extract the CPort id, which is packed in the message header */
480         header = urb->transfer_buffer;
481         cport_id = gb_message_cport_unpack(header);
482
483         if (cport_id_valid(hd, cport_id)) {
484                 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
485                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
486                                                         urb->actual_length);
487         } else {
488                 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
489                                 __func__, cport_id);
490         }
491 exit:
492         /* put our urb back in the request pool */
493         retval = usb_submit_urb(urb, GFP_ATOMIC);
494         if (retval)
495                 dev_err(dev, "%s: error %d in submitting urb.\n",
496                         __func__, retval);
497 }
498
499 static void cport_out_callback(struct urb *urb)
500 {
501         struct gb_message *message = urb->context;
502         struct greybus_host_device *hd = message->operation->connection->hd;
503         struct es1_ap_dev *es1 = hd_to_es1(hd);
504         int status = check_urb_status(urb);
505         unsigned long flags;
506
507         gb_message_cport_clear(message->header);
508
509         /*
510          * Tell the submitter that the message send (attempt) is
511          * complete, and report the status.
512          */
513         greybus_message_sent(hd, message, status);
514
515         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
516         message->hcpriv = NULL;
517         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
518
519         free_urb(es1, urb);
520 }
521
522 #define APB1_LOG_MSG_SIZE       64
523 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
524 {
525         int retval;
526
527         /* SVC messages go down our control pipe */
528         do {
529                 retval = usb_control_msg(es1->usb_dev,
530                                         usb_rcvctrlpipe(es1->usb_dev, 0),
531                                         REQUEST_LOG,
532                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
533                                         0x00, 0x00,
534                                         buf,
535                                         APB1_LOG_MSG_SIZE,
536                                         ES1_TIMEOUT);
537                 if (retval > 0)
538                         kfifo_in(&apb1_log_fifo, buf, retval);
539         } while (retval > 0);
540 }
541
542 static int apb1_log_poll(void *data)
543 {
544         struct es1_ap_dev *es1 = data;
545         char *buf;
546
547         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
548         if (!buf)
549                 return -ENOMEM;
550
551         while (!kthread_should_stop()) {
552                 msleep(1000);
553                 apb1_log_get(es1, buf);
554         }
555
556         kfree(buf);
557
558         return 0;
559 }
560
561 static ssize_t apb1_log_read(struct file *f, char __user *buf,
562                                 size_t count, loff_t *ppos)
563 {
564         ssize_t ret;
565         size_t copied;
566         char *tmp_buf;
567
568         if (count > APB1_LOG_SIZE)
569                 count = APB1_LOG_SIZE;
570
571         tmp_buf = kmalloc(count, GFP_KERNEL);
572         if (!tmp_buf)
573                 return -ENOMEM;
574
575         copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
576         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
577
578         kfree(tmp_buf);
579
580         return ret;
581 }
582
583 static const struct file_operations apb1_log_fops = {
584         .read   = apb1_log_read,
585 };
586
587 static void usb_log_enable(struct es1_ap_dev *es1)
588 {
589         if (!IS_ERR_OR_NULL(apb1_log_task))
590                 return;
591
592         /* get log from APB1 */
593         apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
594         if (IS_ERR(apb1_log_task))
595                 return;
596         apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
597                                                 gb_debugfs_get(), NULL,
598                                                 &apb1_log_fops);
599 }
600
601 static void usb_log_disable(struct es1_ap_dev *es1)
602 {
603         if (IS_ERR_OR_NULL(apb1_log_task))
604                 return;
605
606         debugfs_remove(apb1_log_dentry);
607         apb1_log_dentry = NULL;
608
609         kthread_stop(apb1_log_task);
610         apb1_log_task = NULL;
611 }
612
613 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
614                                 size_t count, loff_t *ppos)
615 {
616         char tmp_buf[3];
617         int enable = !IS_ERR_OR_NULL(apb1_log_task);
618
619         sprintf(tmp_buf, "%d\n", enable);
620         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
621 }
622
623 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
624                                 size_t count, loff_t *ppos)
625 {
626         int enable;
627         ssize_t retval;
628         struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
629
630         retval = kstrtoint_from_user(buf, count, 10, &enable);
631         if (retval)
632                 return retval;
633
634         if (enable)
635                 usb_log_enable(es1);
636         else
637                 usb_log_disable(es1);
638
639         return count;
640 }
641
642 static const struct file_operations apb1_log_enable_fops = {
643         .read   = apb1_log_enable_read,
644         .write  = apb1_log_enable_write,
645 };
646
647 static int apb1_get_cport_count(struct usb_device *udev)
648 {
649         int retval;
650         __le16 *cport_count;
651
652         cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
653         if (!cport_count)
654                 return -ENOMEM;
655
656         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
657                                  REQUEST_CPORT_COUNT,
658                                  USB_DIR_IN | USB_TYPE_VENDOR |
659                                  USB_RECIP_INTERFACE, 0, 0, cport_count,
660                                  sizeof(*cport_count), ES1_TIMEOUT);
661         if (retval < 0) {
662                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
663                         retval);
664                 goto out;
665         }
666
667         retval = le16_to_cpu(*cport_count);
668
669         /* We need to fit a CPort ID in one byte of a message header */
670         if (retval > U8_MAX) {
671                 retval = U8_MAX;
672                 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
673         }
674
675 out:
676         kfree(cport_count);
677         return retval;
678 }
679
680 /*
681  * The ES1 USB Bridge device contains 4 endpoints
682  * 1 Control - usual USB stuff + AP -> SVC messages
683  * 1 Interrupt IN - SVC -> AP messages
684  * 1 Bulk IN - CPort data in
685  * 1 Bulk OUT - CPort data out
686  */
687 static int ap_probe(struct usb_interface *interface,
688                     const struct usb_device_id *id)
689 {
690         struct es1_ap_dev *es1;
691         struct greybus_host_device *hd;
692         struct usb_device *udev;
693         struct usb_host_interface *iface_desc;
694         struct usb_endpoint_descriptor *endpoint;
695         int bulk_in = 0;
696         int bulk_out = 0;
697         int retval = -ENOMEM;
698         int i;
699         int num_cports;
700
701         udev = usb_get_dev(interface_to_usbdev(interface));
702
703         num_cports = apb1_get_cport_count(udev);
704         if (num_cports < 0) {
705                 usb_put_dev(udev);
706                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
707                         num_cports);
708                 return num_cports;
709         }
710
711         hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX,
712                                num_cports);
713         if (IS_ERR(hd)) {
714                 usb_put_dev(udev);
715                 return PTR_ERR(hd);
716         }
717
718         es1 = hd_to_es1(hd);
719         es1->hd = hd;
720         es1->usb_intf = interface;
721         es1->usb_dev = udev;
722         spin_lock_init(&es1->cport_out_urb_lock);
723         usb_set_intfdata(interface, es1);
724
725         es1->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es1->cport_to_ep),
726                                    GFP_KERNEL);
727         if (!es1->cport_to_ep) {
728                 retval = -ENOMEM;
729                 goto error;
730         }
731
732         /* find all 3 of our endpoints */
733         iface_desc = interface->cur_altsetting;
734         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
735                 endpoint = &iface_desc->endpoint[i].desc;
736
737                 if (usb_endpoint_is_bulk_in(endpoint)) {
738                         es1->cport_in[bulk_in++].endpoint =
739                                 endpoint->bEndpointAddress;
740                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
741                         es1->cport_out[bulk_out++].endpoint =
742                                 endpoint->bEndpointAddress;
743                 } else {
744                         dev_err(&udev->dev,
745                                 "Unknown endpoint type found, address %x\n",
746                                 endpoint->bEndpointAddress);
747                 }
748         }
749         if ((bulk_in == 0) ||
750             (bulk_out == 0)) {
751                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
752                 goto error;
753         }
754
755         /* Allocate buffers for our cport in messages and start them up */
756         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
757                 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
758                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
759                         struct urb *urb;
760                         u8 *buffer;
761
762                         urb = usb_alloc_urb(0, GFP_KERNEL);
763                         if (!urb)
764                                 goto error;
765                         buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
766                         if (!buffer)
767                                 goto error;
768
769                         usb_fill_bulk_urb(urb, udev,
770                                           usb_rcvbulkpipe(udev,
771                                                           cport_in->endpoint),
772                                           buffer, ES1_GBUF_MSG_SIZE_MAX,
773                                           cport_in_callback, hd);
774                         cport_in->urb[i] = urb;
775                         cport_in->buffer[i] = buffer;
776                         retval = usb_submit_urb(urb, GFP_KERNEL);
777                         if (retval)
778                                 goto error;
779                 }
780         }
781
782         /* Allocate urbs for our CPort OUT messages */
783         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
784                 struct urb *urb;
785
786                 urb = usb_alloc_urb(0, GFP_KERNEL);
787                 if (!urb)
788                         goto error;
789
790                 es1->cport_out_urb[i] = urb;
791                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
792         }
793
794         apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
795                                                         (S_IWUSR | S_IRUGO),
796                                                         gb_debugfs_get(), es1,
797                                                         &apb1_log_enable_fops);
798         return 0;
799 error:
800         ap_disconnect(interface);
801
802         return retval;
803 }
804
805 static struct usb_driver es1_ap_driver = {
806         .name =         "es2_ap_driver",
807         .probe =        ap_probe,
808         .disconnect =   ap_disconnect,
809         .id_table =     id_table,
810 };
811
812 module_usb_driver(es1_ap_driver);
813
814 MODULE_LICENSE("GPL v2");
815 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");