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