greybus: es2.c: add a control request for endpoints mapping
[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 "svc_msg.h"
18 #include "kernel_ver.h"
19
20 /* Memory sizes for the buffers sent to/from the ES1 controller */
21 #define ES1_SVC_MSG_SIZE        (sizeof(struct svc_msg) + SZ_64K)
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 cport present on USB bridge */
38 #define CPORT_MAX               44
39
40 /* Number of bulk in and bulk out couple */
41 #define NUM_BULKS               7
42
43 /*
44  * Number of CPort IN urbs in flight at any point in time.
45  * Adjust if we are having stalls in the USB buffer due to not enough urbs in
46  * flight.
47  */
48 #define NUM_CPORT_IN_URB        4
49
50 /* Number of CPort OUT urbs in flight at any point in time.
51  * Adjust if we get messages saying we are out of urbs in the system log.
52  */
53 #define NUM_CPORT_OUT_URB       (8 * NUM_BULKS)
54
55 /* vendor request AP message */
56 #define REQUEST_SVC             0x01
57
58 /* vendor request APB1 log */
59 #define REQUEST_LOG             0x02
60
61 /* vendor request to map a cport to bulk in and bulk out endpoints */
62 #define REQUEST_EP_MAPPING      0x03
63
64 /*
65  * @endpoint: bulk in endpoint for CPort data
66  * @urb: array of urbs for the CPort in messages
67  * @buffer: array of buffers for the @cport_in_urb urbs
68  */
69 struct es1_cport_in {
70         __u8 endpoint;
71         struct urb *urb[NUM_CPORT_IN_URB];
72         u8 *buffer[NUM_CPORT_IN_URB];
73 };
74
75 /*
76  * @endpoint: bulk out endpoint for CPort data
77  */
78 struct es1_cport_out {
79         __u8 endpoint;
80 };
81
82 /**
83  * es1_ap_dev - ES1 USB Bridge to AP structure
84  * @usb_dev: pointer to the USB device we are.
85  * @usb_intf: pointer to the USB interface we are bound to.
86  * @hd: pointer to our greybus_host_device structure
87  * @control_endpoint: endpoint to send data to SVC
88  * @svc_endpoint: endpoint for SVC data in
89
90  * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
91  * @svc_urb: urb for SVC messages coming in on @svc_endpoint
92  * @cport_in: endpoint, urbs and buffer for cport in messages
93  * @cport_out: endpoint for for cport out messages
94  * @cport_out_urb: array of urbs for the CPort out messages
95  * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
96  *                      not.
97  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
98  */
99 struct es1_ap_dev {
100         struct usb_device *usb_dev;
101         struct usb_interface *usb_intf;
102         struct greybus_host_device *hd;
103
104         __u8 control_endpoint;
105         __u8 svc_endpoint;
106
107         u8 *svc_buffer;
108         struct urb *svc_urb;
109
110         struct es1_cport_in cport_in[NUM_BULKS];
111         struct es1_cport_out cport_out[NUM_BULKS];
112         struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
113         bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
114         spinlock_t cport_out_urb_lock;
115
116         int cport_to_ep[CPORT_MAX];
117 };
118
119 struct cport_to_ep {
120         __le16 cport_id;
121         __u8 endpoint_in;
122         __u8 endpoint_out;
123 };
124
125 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
126 {
127         return (struct es1_ap_dev *)&hd->hd_priv;
128 }
129
130 static void cport_out_callback(struct urb *urb);
131 static void usb_log_enable(struct es1_ap_dev *es1);
132 static void usb_log_disable(struct es1_ap_dev *es1);
133
134 static int cport_to_ep(struct es1_ap_dev *es1, u16 cport_id)
135 {
136         if (cport_id >= CPORT_MAX)
137                 return 0;
138         return es1->cport_to_ep[cport_id];
139 }
140
141 #define ES1_TIMEOUT     500     /* 500 ms for the SVC to do something */
142 static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
143 {
144         struct es1_ap_dev *es1 = hd_to_es1(hd);
145         int retval;
146
147         /* SVC messages go down our control pipe */
148         retval = usb_control_msg(es1->usb_dev,
149                                  usb_sndctrlpipe(es1->usb_dev,
150                                                  es1->control_endpoint),
151                                  REQUEST_SVC,
152                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
153                                  0x00, 0x00,
154                                  (char *)svc_msg,
155                                  sizeof(*svc_msg),
156                                  ES1_TIMEOUT);
157         if (retval != sizeof(*svc_msg))
158                 return retval;
159
160         return 0;
161 }
162
163 static int ep_in_use(struct es1_ap_dev *es1, int bulk_ep_set)
164 {
165         int i;
166
167         for (i = 0; i < CPORT_MAX; i++) {
168                 if (es1->cport_to_ep[i] == bulk_ep_set)
169                         return 1;
170         }
171         return 0;
172 }
173
174 int map_cport_to_ep(struct es1_ap_dev *es1,
175                                 u16 cport_id, int bulk_ep_set)
176 {
177         int retval;
178         struct cport_to_ep *cport_to_ep;
179
180         if (bulk_ep_set == 0 || bulk_ep_set >= NUM_BULKS)
181                 return -EINVAL;
182         if (cport_id >= CPORT_MAX)
183                 return -EINVAL;
184         if (bulk_ep_set && ep_in_use(es1, bulk_ep_set))
185                 return -EINVAL;
186
187         cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
188         if (!cport_to_ep)
189                 return -ENOMEM;
190
191         es1->cport_to_ep[cport_id] = bulk_ep_set;
192         cport_to_ep->cport_id = cpu_to_le16(cport_id);
193         cport_to_ep->endpoint_in = es1->cport_in[bulk_ep_set].endpoint;
194         cport_to_ep->endpoint_out = es1->cport_out[bulk_ep_set].endpoint;
195
196         retval = usb_control_msg(es1->usb_dev,
197                                  usb_sndctrlpipe(es1->usb_dev,
198                                                  es1->control_endpoint),
199                                  REQUEST_EP_MAPPING,
200                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
201                                  0x00, 0x00,
202                                  (char *)cport_to_ep,
203                                  sizeof(*cport_to_ep),
204                                  ES1_TIMEOUT);
205         if (retval == sizeof(*cport_to_ep))
206                 retval = 0;
207         kfree(cport_to_ep);
208
209         return retval;
210 }
211
212 int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
213 {
214         return map_cport_to_ep(es1, cport_id, 0);
215 }
216
217 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
218 {
219         struct urb *urb = NULL;
220         unsigned long flags;
221         int i;
222
223         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
224
225         /* Look in our pool of allocated urbs first, as that's the "fastest" */
226         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
227                 if (es1->cport_out_urb_busy[i] == false) {
228                         es1->cport_out_urb_busy[i] = true;
229                         urb = es1->cport_out_urb[i];
230                         break;
231                 }
232         }
233         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
234         if (urb)
235                 return urb;
236
237         /*
238          * Crap, pool is empty, complain to the syslog and go allocate one
239          * dynamically as we have to succeed.
240          */
241         dev_err(&es1->usb_dev->dev,
242                 "No free CPort OUT urbs, having to dynamically allocate one!\n");
243         return usb_alloc_urb(0, gfp_mask);
244 }
245
246 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
247 {
248         unsigned long flags;
249         int i;
250         /*
251          * See if this was an urb in our pool, if so mark it "free", otherwise
252          * we need to free it ourselves.
253          */
254         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
255         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
256                 if (urb == es1->cport_out_urb[i]) {
257                         es1->cport_out_urb_busy[i] = false;
258                         urb = NULL;
259                         break;
260                 }
261         }
262         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
263
264         /* If urb is not NULL, then we need to free this urb */
265         usb_free_urb(urb);
266 }
267
268 /*
269  * Returns an opaque cookie value if successful, or a pointer coded
270  * error otherwise.  If the caller wishes to cancel the in-flight
271  * buffer, it must supply the returned cookie to the cancel routine.
272  */
273 static void *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         void *buffer;
279         size_t buffer_size;
280         int retval;
281         struct urb *urb;
282         int bulk_ep_set;
283
284         buffer = message->buffer;
285         buffer_size = sizeof(*message->header) + message->payload_size;
286
287         /*
288          * The data actually transferred will include an indication
289          * of where the data should be sent.  Do one last check of
290          * the target CPort id before filling it in.
291          */
292         if (cport_id == CPORT_ID_BAD) {
293                 pr_err("request to send inbound data buffer\n");
294                 return ERR_PTR(-EINVAL);
295         }
296
297         /* Find a free urb */
298         urb = next_free_urb(es1, gfp_mask);
299         if (!urb)
300                 return ERR_PTR(-ENOMEM);
301
302         /*
303          * We (ab)use the operation-message header pad bytes to transfer the
304          * cport id in order to minimise overhead.
305          */
306         put_unaligned_le16(cport_id, message->header->pad);
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                           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                 free_urb(es1, urb);
318                 put_unaligned_le16(0, message->header->pad);
319                 return ERR_PTR(retval);
320         }
321
322         return urb;
323 }
324
325 /*
326  * The cookie value supplied is the value that message_send()
327  * returned to its caller.  It identifies the message that should be
328  * canceled.  This function must also handle (which is to say,
329  * ignore) a null cookie value.
330  */
331 static void message_cancel(void *cookie)
332 {
333
334         /*
335          * We really should be defensive and track all outstanding
336          * (sent) messages rather than trusting the cookie provided
337          * is valid.  For the time being, this will do.
338          */
339         if (cookie)
340                 usb_kill_urb(cookie);
341 }
342
343 static struct greybus_host_driver es1_driver = {
344         .hd_priv_size           = sizeof(struct es1_ap_dev),
345         .message_send           = message_send,
346         .message_cancel         = message_cancel,
347         .submit_svc             = submit_svc,
348 };
349
350 /* Common function to report consistent warnings based on URB status */
351 static int check_urb_status(struct urb *urb)
352 {
353         struct device *dev = &urb->dev->dev;
354         int status = urb->status;
355
356         switch (status) {
357         case 0:
358                 return 0;
359
360         case -EOVERFLOW:
361                 dev_err(dev, "%s: overflow actual length is %d\n",
362                         __func__, urb->actual_length);
363         case -ECONNRESET:
364         case -ENOENT:
365         case -ESHUTDOWN:
366         case -EILSEQ:
367         case -EPROTO:
368                 /* device is gone, stop sending */
369                 return status;
370         }
371         dev_err(dev, "%s: unknown status %d\n", __func__, status);
372
373         return -EAGAIN;
374 }
375
376 static void ap_disconnect(struct usb_interface *interface)
377 {
378         struct es1_ap_dev *es1;
379         struct usb_device *udev;
380         int bulk_in;
381         int i;
382
383         es1 = usb_get_intfdata(interface);
384         if (!es1)
385                 return;
386
387         usb_log_disable(es1);
388
389         /* Tear down everything! */
390         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
391                 struct urb *urb = es1->cport_out_urb[i];
392
393                 if (!urb)
394                         break;
395                 usb_kill_urb(urb);
396                 usb_free_urb(urb);
397                 es1->cport_out_urb[i] = NULL;
398                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
399         }
400
401         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
402                 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
403                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
404                         struct urb *urb = cport_in->urb[i];
405
406                         if (!urb)
407                                 break;
408                         usb_kill_urb(urb);
409                         usb_free_urb(urb);
410                         kfree(cport_in->buffer[i]);
411                         cport_in->buffer[i] = NULL;
412                 }
413         }
414
415         usb_kill_urb(es1->svc_urb);
416         usb_free_urb(es1->svc_urb);
417         es1->svc_urb = NULL;
418         kfree(es1->svc_buffer);
419         es1->svc_buffer = NULL;
420
421         usb_set_intfdata(interface, NULL);
422         udev = es1->usb_dev;
423         greybus_remove_hd(es1->hd);
424
425         usb_put_dev(udev);
426 }
427
428 /* Callback for when we get a SVC message */
429 static void svc_in_callback(struct urb *urb)
430 {
431         struct greybus_host_device *hd = urb->context;
432         struct device *dev = &urb->dev->dev;
433         int status = check_urb_status(urb);
434         int retval;
435
436         if (status) {
437                 if ((status == -EAGAIN) || (status == -EPROTO))
438                         goto exit;
439                 dev_err(dev, "urb svc in error %d (dropped)\n", status);
440                 return;
441         }
442
443         /* We have a message, create a new message structure, add it to the
444          * list, and wake up our thread that will process the messages.
445          */
446         greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
447
448 exit:
449         /* resubmit the urb to get more messages */
450         retval = usb_submit_urb(urb, GFP_ATOMIC);
451         if (retval)
452                 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
453 }
454
455 static void cport_in_callback(struct urb *urb)
456 {
457         struct greybus_host_device *hd = urb->context;
458         struct device *dev = &urb->dev->dev;
459         struct gb_operation_msg_hdr *header;
460         int status = check_urb_status(urb);
461         int retval;
462         u16 cport_id;
463
464         if (status) {
465                 if ((status == -EAGAIN) || (status == -EPROTO))
466                         goto exit;
467                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
468                 return;
469         }
470
471         if (urb->actual_length < sizeof(*header)) {
472                 dev_err(dev, "%s: short message received\n", __func__);
473                 goto exit;
474         }
475
476         header = urb->transfer_buffer;
477         cport_id = get_unaligned_le16(header->pad);
478         put_unaligned_le16(0, header->pad);
479
480         greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
481                                                         urb->actual_length);
482 exit:
483         /* put our urb back in the request pool */
484         retval = usb_submit_urb(urb, GFP_ATOMIC);
485         if (retval)
486                 dev_err(dev, "%s: error %d in submitting urb.\n",
487                         __func__, retval);
488 }
489
490 static void cport_out_callback(struct urb *urb)
491 {
492         struct gb_message *message = urb->context;
493         struct greybus_host_device *hd = message->operation->connection->hd;
494         struct es1_ap_dev *es1 = hd_to_es1(hd);
495         int status = check_urb_status(urb);
496
497         /* Clear the pad bytes used for the cport id */
498         put_unaligned_le16(0, message->header->pad);
499
500         /*
501          * Tell the submitter that the message send (attempt) is
502          * complete, and report the status.
503          */
504         greybus_message_sent(hd, message, status);
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         bool int_in_found = false;
651         int bulk_in = 0;
652         int bulk_out = 0;
653         int retval = -ENOMEM;
654         int i;
655         u16 endo_id = 0x4755;   // FIXME - get endo "ID" from the SVC
656         u8 ap_intf_id = 0x01;   // FIXME - get endo "ID" from the SVC
657         u8 svc_interval = 0;
658
659         udev = usb_get_dev(interface_to_usbdev(interface));
660
661         hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX);
662         if (IS_ERR(hd)) {
663                 usb_put_dev(udev);
664                 return PTR_ERR(hd);
665         }
666
667         es1 = hd_to_es1(hd);
668         es1->hd = hd;
669         es1->usb_intf = interface;
670         es1->usb_dev = udev;
671         spin_lock_init(&es1->cport_out_urb_lock);
672         usb_set_intfdata(interface, es1);
673
674         /* Control endpoint is the pipe to talk to this AP, so save it off */
675         endpoint = &udev->ep0.desc;
676         es1->control_endpoint = endpoint->bEndpointAddress;
677
678         /* find all 3 of our endpoints */
679         iface_desc = interface->cur_altsetting;
680         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
681                 endpoint = &iface_desc->endpoint[i].desc;
682
683                 if (usb_endpoint_is_int_in(endpoint)) {
684                         es1->svc_endpoint = endpoint->bEndpointAddress;
685                         svc_interval = endpoint->bInterval;
686                         int_in_found = true;
687                 } else if (usb_endpoint_is_bulk_in(endpoint)) {
688                         es1->cport_in[bulk_in++].endpoint =
689                                 endpoint->bEndpointAddress;
690                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
691                         es1->cport_out[bulk_out++].endpoint =
692                                 endpoint->bEndpointAddress;
693                 } else {
694                         dev_err(&udev->dev,
695                                 "Unknown endpoint type found, address %x\n",
696                                 endpoint->bEndpointAddress);
697                 }
698         }
699         if ((int_in_found == false) ||
700             (bulk_in == 0) ||
701             (bulk_out == 0)) {
702                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
703                 goto error;
704         }
705
706         /* Create our buffer and URB to get SVC messages, and start it up */
707         es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
708         if (!es1->svc_buffer)
709                 goto error;
710
711         es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
712         if (!es1->svc_urb)
713                 goto error;
714
715         usb_fill_int_urb(es1->svc_urb, udev,
716                          usb_rcvintpipe(udev, es1->svc_endpoint),
717                          es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
718                          hd, svc_interval);
719
720         /* Allocate buffers for our cport in messages and start them up */
721         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
722                 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
723                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
724                         struct urb *urb;
725                         u8 *buffer;
726
727                         urb = usb_alloc_urb(0, GFP_KERNEL);
728                         if (!urb)
729                                 goto error;
730                         buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
731                         if (!buffer)
732                                 goto error;
733
734                         usb_fill_bulk_urb(urb, udev,
735                                           usb_rcvbulkpipe(udev,
736                                                           cport_in->endpoint),
737                                           buffer, ES1_GBUF_MSG_SIZE_MAX,
738                                           cport_in_callback, hd);
739                         cport_in->urb[i] = urb;
740                         cport_in->buffer[i] = buffer;
741                         retval = usb_submit_urb(urb, GFP_KERNEL);
742                         if (retval)
743                                 goto error;
744                 }
745         }
746
747         /* Allocate urbs for our CPort OUT messages */
748         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
749                 struct urb *urb;
750
751                 urb = usb_alloc_urb(0, GFP_KERNEL);
752                 if (!urb)
753                         goto error;
754
755                 es1->cport_out_urb[i] = urb;
756                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
757         }
758
759         /* Start up our svc urb, which allows events to start flowing */
760         retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
761         if (retval)
762                 goto error;
763
764         apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
765                                                         (S_IWUSR | S_IRUGO),
766                                                         gb_debugfs_get(), es1,
767                                                         &apb1_log_enable_fops);
768
769         /*
770          * XXX Soon this will be initiated later, with a combination
771          * XXX of a Control protocol probe operation and a
772          * XXX subsequent Control protocol connected operation for
773          * XXX the SVC connection.  At that point we know we're
774          * XXX properly connected to an Endo.
775          */
776         retval = greybus_endo_setup(hd, endo_id, ap_intf_id);
777         if (retval)
778                 goto error;
779
780         return 0;
781 error:
782         ap_disconnect(interface);
783
784         return retval;
785 }
786
787 static struct usb_driver es1_ap_driver = {
788         .name =         "es2_ap_driver",
789         .probe =        ap_probe,
790         .disconnect =   ap_disconnect,
791         .id_table =     id_table,
792 };
793
794 module_usb_driver(es1_ap_driver);
795
796 MODULE_LICENSE("GPL v2");
797 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");