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