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