greybus: es2: add some documentation about endpoints mapping
[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 AP message */
52 #define REQUEST_SVC             0x01
53
54 /* vendor request APB1 log */
55 #define REQUEST_LOG             0x02
56
57 /* vendor request to map a cport to bulk in and bulk out endpoints */
58 #define REQUEST_EP_MAPPING      0x03
59
60 /* vendor request to get the number of cports available */
61 #define REQUEST_CPORT_COUNT     0x04
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
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         struct es1_cport_in cport_in[NUM_BULKS];
102         struct es1_cport_out cport_out[NUM_BULKS];
103         struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
104         bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
105         bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
106         spinlock_t cport_out_urb_lock;
107
108         int *cport_to_ep;
109 };
110
111 /**
112  * cport_to_ep - information about cport to endpoints mapping
113  * @cport_id: the id of cport to map to endpoints
114  * @endpoint_in: the endpoint number to use for in transfer
115  * @endpoint_out: he endpoint number to use for out transfer
116  */
117 struct cport_to_ep {
118         __le16 cport_id;
119         __u8 endpoint_in;
120         __u8 endpoint_out;
121 };
122
123 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
124 {
125         return (struct es1_ap_dev *)&hd->hd_priv;
126 }
127
128 static void cport_out_callback(struct urb *urb);
129 static void usb_log_enable(struct es1_ap_dev *es1);
130 static void usb_log_disable(struct es1_ap_dev *es1);
131
132 /* Get the endpoints pair mapped to the cport */
133 static int cport_to_ep_pair(struct es1_ap_dev *es1, u16 cport_id)
134 {
135         if (cport_id >= es1->hd->num_cports)
136                 return 0;
137         return es1->cport_to_ep[cport_id];
138 }
139
140 #define ES1_TIMEOUT     500     /* 500 ms for the SVC to do something */
141
142 /* Test if the endpoints pair is already mapped to a cport */
143 static int ep_pair_in_use(struct es1_ap_dev *es1, int ep_pair)
144 {
145         int i;
146
147         for (i = 0; i < es1->hd->num_cports; i++) {
148                 if (es1->cport_to_ep[i] == ep_pair)
149                         return 1;
150         }
151         return 0;
152 }
153
154 /* Configure the endpoint mapping and send the request to APBridge */
155 static int map_cport_to_ep(struct es1_ap_dev *es1,
156                                 u16 cport_id, int ep_pair)
157 {
158         int retval;
159         struct cport_to_ep *cport_to_ep;
160
161         if (ep_pair < 0 || ep_pair >= NUM_BULKS)
162                 return -EINVAL;
163         if (cport_id >= es1->hd->num_cports)
164                 return -EINVAL;
165         if (ep_pair && ep_pair_in_use(es1, ep_pair))
166                 return -EINVAL;
167
168         cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
169         if (!cport_to_ep)
170                 return -ENOMEM;
171
172         es1->cport_to_ep[cport_id] = ep_pair;
173         cport_to_ep->cport_id = cpu_to_le16(cport_id);
174         cport_to_ep->endpoint_in = es1->cport_in[ep_pair].endpoint;
175         cport_to_ep->endpoint_out = es1->cport_out[ep_pair].endpoint;
176
177         retval = usb_control_msg(es1->usb_dev,
178                                  usb_sndctrlpipe(es1->usb_dev, 0),
179                                  REQUEST_EP_MAPPING,
180                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
181                                  0x00, 0x00,
182                                  (char *)cport_to_ep,
183                                  sizeof(*cport_to_ep),
184                                  ES1_TIMEOUT);
185         if (retval == sizeof(*cport_to_ep))
186                 retval = 0;
187         kfree(cport_to_ep);
188
189         return retval;
190 }
191
192 /* Unmap a cport: use the muxed endpoints pair */
193 static int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
194 {
195         return map_cport_to_ep(es1, cport_id, 0);
196 }
197
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         gb_connection_push_timestamp(message->operation->connection);
323         retval = usb_submit_urb(urb, gfp_mask);
324         if (retval) {
325                 pr_err("error %d submitting URB\n", retval);
326
327                 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
328                 message->hcpriv = NULL;
329                 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
330
331                 free_urb(es1, urb);
332                 gb_message_cport_clear(message->header);
333
334                 return retval;
335         }
336
337         return 0;
338 }
339
340 /*
341  * Can not be called in atomic context.
342  */
343 static void message_cancel(struct gb_message *message)
344 {
345         struct greybus_host_device *hd = message->operation->connection->hd;
346         struct es1_ap_dev *es1 = hd_to_es1(hd);
347         struct urb *urb;
348         int i;
349
350         might_sleep();
351
352         spin_lock_irq(&es1->cport_out_urb_lock);
353         urb = message->hcpriv;
354
355         /* Prevent dynamically allocated urb from being deallocated. */
356         usb_get_urb(urb);
357
358         /* Prevent pre-allocated urb from being reused. */
359         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
360                 if (urb == es1->cport_out_urb[i]) {
361                         es1->cport_out_urb_cancelled[i] = true;
362                         break;
363                 }
364         }
365         spin_unlock_irq(&es1->cport_out_urb_lock);
366
367         usb_kill_urb(urb);
368
369         if (i < NUM_CPORT_OUT_URB) {
370                 spin_lock_irq(&es1->cport_out_urb_lock);
371                 es1->cport_out_urb_cancelled[i] = false;
372                 spin_unlock_irq(&es1->cport_out_urb_lock);
373         }
374
375         usb_free_urb(urb);
376 }
377
378 static struct greybus_host_driver es1_driver = {
379         .hd_priv_size           = sizeof(struct es1_ap_dev),
380         .message_send           = message_send,
381         .message_cancel         = message_cancel,
382 };
383
384 /* Common function to report consistent warnings based on URB status */
385 static int check_urb_status(struct urb *urb)
386 {
387         struct device *dev = &urb->dev->dev;
388         int status = urb->status;
389
390         switch (status) {
391         case 0:
392                 return 0;
393
394         case -EOVERFLOW:
395                 dev_err(dev, "%s: overflow actual length is %d\n",
396                         __func__, urb->actual_length);
397         case -ECONNRESET:
398         case -ENOENT:
399         case -ESHUTDOWN:
400         case -EILSEQ:
401         case -EPROTO:
402                 /* device is gone, stop sending */
403                 return status;
404         }
405         dev_err(dev, "%s: unknown status %d\n", __func__, status);
406
407         return -EAGAIN;
408 }
409
410 static void ap_disconnect(struct usb_interface *interface)
411 {
412         struct es1_ap_dev *es1;
413         struct usb_device *udev;
414         int bulk_in;
415         int i;
416
417         es1 = usb_get_intfdata(interface);
418         if (!es1)
419                 return;
420
421         usb_log_disable(es1);
422
423         /* Tear down everything! */
424         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
425                 struct urb *urb = es1->cport_out_urb[i];
426
427                 if (!urb)
428                         break;
429                 usb_kill_urb(urb);
430                 usb_free_urb(urb);
431                 es1->cport_out_urb[i] = NULL;
432                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
433         }
434
435         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
436                 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
437                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
438                         struct urb *urb = cport_in->urb[i];
439
440                         if (!urb)
441                                 break;
442                         usb_kill_urb(urb);
443                         usb_free_urb(urb);
444                         kfree(cport_in->buffer[i]);
445                         cport_in->buffer[i] = NULL;
446                 }
447         }
448
449         usb_set_intfdata(interface, NULL);
450         udev = es1->usb_dev;
451         greybus_remove_hd(es1->hd);
452         kfree(es1->cport_to_ep);
453
454         usb_put_dev(udev);
455 }
456
457 static void cport_in_callback(struct urb *urb)
458 {
459         struct greybus_host_device *hd = urb->context;
460         struct device *dev = &urb->dev->dev;
461         struct gb_operation_msg_hdr *header;
462         int status = check_urb_status(urb);
463         int retval;
464         u16 cport_id;
465
466         if (status) {
467                 if ((status == -EAGAIN) || (status == -EPROTO))
468                         goto exit;
469                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
470                 return;
471         }
472
473         if (urb->actual_length < sizeof(*header)) {
474                 dev_err(dev, "%s: short message received\n", __func__);
475                 goto exit;
476         }
477
478         /* Extract the CPort id, which is packed in the message header */
479         header = urb->transfer_buffer;
480         cport_id = gb_message_cport_unpack(header);
481
482         if (cport_id_valid(hd, cport_id))
483                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
484                                                         urb->actual_length);
485         else
486                 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
487                                 __func__, cport_id);
488 exit:
489         /* put our urb back in the request pool */
490         retval = usb_submit_urb(urb, GFP_ATOMIC);
491         if (retval)
492                 dev_err(dev, "%s: error %d in submitting urb.\n",
493                         __func__, retval);
494 }
495
496 static void cport_out_callback(struct urb *urb)
497 {
498         struct gb_message *message = urb->context;
499         struct greybus_host_device *hd = message->operation->connection->hd;
500         struct es1_ap_dev *es1 = hd_to_es1(hd);
501         int status = check_urb_status(urb);
502         unsigned long flags;
503
504         gb_message_cport_clear(message->header);
505
506         /*
507          * Tell the submitter that the message send (attempt) is
508          * complete, and report the status.
509          */
510         greybus_message_sent(hd, message, status);
511
512         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
513         message->hcpriv = NULL;
514         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
515
516         free_urb(es1, urb);
517 }
518
519 #define APB1_LOG_MSG_SIZE       64
520 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
521 {
522         int retval;
523
524         /* SVC messages go down our control pipe */
525         do {
526                 retval = usb_control_msg(es1->usb_dev,
527                                         usb_rcvctrlpipe(es1->usb_dev, 0),
528                                         REQUEST_LOG,
529                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
530                                         0x00, 0x00,
531                                         buf,
532                                         APB1_LOG_MSG_SIZE,
533                                         ES1_TIMEOUT);
534                 if (retval > 0)
535                         kfifo_in(&apb1_log_fifo, buf, retval);
536         } while (retval > 0);
537 }
538
539 static int apb1_log_poll(void *data)
540 {
541         struct es1_ap_dev *es1 = data;
542         char *buf;
543
544         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
545         if (!buf)
546                 return -ENOMEM;
547
548         while (!kthread_should_stop()) {
549                 msleep(1000);
550                 apb1_log_get(es1, buf);
551         }
552
553         kfree(buf);
554
555         return 0;
556 }
557
558 static ssize_t apb1_log_read(struct file *f, char __user *buf,
559                                 size_t count, loff_t *ppos)
560 {
561         ssize_t ret;
562         size_t copied;
563         char *tmp_buf;
564
565         if (count > APB1_LOG_SIZE)
566                 count = APB1_LOG_SIZE;
567
568         tmp_buf = kmalloc(count, GFP_KERNEL);
569         if (!tmp_buf)
570                 return -ENOMEM;
571
572         copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
573         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
574
575         kfree(tmp_buf);
576
577         return ret;
578 }
579
580 static const struct file_operations apb1_log_fops = {
581         .read   = apb1_log_read,
582 };
583
584 static void usb_log_enable(struct es1_ap_dev *es1)
585 {
586         if (!IS_ERR_OR_NULL(apb1_log_task))
587                 return;
588
589         /* get log from APB1 */
590         apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
591         if (IS_ERR(apb1_log_task))
592                 return;
593         apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
594                                                 gb_debugfs_get(), NULL,
595                                                 &apb1_log_fops);
596 }
597
598 static void usb_log_disable(struct es1_ap_dev *es1)
599 {
600         if (IS_ERR_OR_NULL(apb1_log_task))
601                 return;
602
603         debugfs_remove(apb1_log_dentry);
604         apb1_log_dentry = NULL;
605
606         kthread_stop(apb1_log_task);
607         apb1_log_task = NULL;
608 }
609
610 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
611                                 size_t count, loff_t *ppos)
612 {
613         char tmp_buf[3];
614         int enable = !IS_ERR_OR_NULL(apb1_log_task);
615
616         sprintf(tmp_buf, "%d\n", enable);
617         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
618 }
619
620 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
621                                 size_t count, loff_t *ppos)
622 {
623         int enable;
624         ssize_t retval;
625         struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
626
627         retval = kstrtoint_from_user(buf, count, 10, &enable);
628         if (retval)
629                 return retval;
630
631         if (enable)
632                 usb_log_enable(es1);
633         else
634                 usb_log_disable(es1);
635
636         return count;
637 }
638
639 static const struct file_operations apb1_log_enable_fops = {
640         .read   = apb1_log_enable_read,
641         .write  = apb1_log_enable_write,
642 };
643
644 static int apb1_get_cport_count(struct usb_device *udev)
645 {
646         int retval;
647         __le16 *cport_count;
648
649         cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
650         if (!cport_count)
651                 return -ENOMEM;
652
653         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
654                                  REQUEST_CPORT_COUNT,
655                                  USB_DIR_IN | USB_TYPE_VENDOR |
656                                  USB_RECIP_INTERFACE, 0, 0, cport_count,
657                                  sizeof(*cport_count), ES1_TIMEOUT);
658         if (retval < 0) {
659                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
660                         retval);
661                 goto out;
662         }
663
664         retval = le16_to_cpu(*cport_count);
665
666         /* We need to fit a CPort ID in one byte of a message header */
667         if (retval > U8_MAX) {
668                 retval = U8_MAX;
669                 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
670         }
671
672 out:
673         kfree(cport_count);
674         return retval;
675 }
676
677 /*
678  * The ES1 USB Bridge device contains 4 endpoints
679  * 1 Control - usual USB stuff + AP -> SVC messages
680  * 1 Interrupt IN - SVC -> AP messages
681  * 1 Bulk IN - CPort data in
682  * 1 Bulk OUT - CPort data out
683  */
684 static int ap_probe(struct usb_interface *interface,
685                     const struct usb_device_id *id)
686 {
687         struct es1_ap_dev *es1;
688         struct greybus_host_device *hd;
689         struct usb_device *udev;
690         struct usb_host_interface *iface_desc;
691         struct usb_endpoint_descriptor *endpoint;
692         int bulk_in = 0;
693         int bulk_out = 0;
694         int retval = -ENOMEM;
695         int i;
696         int num_cports;
697
698         udev = usb_get_dev(interface_to_usbdev(interface));
699
700         num_cports = apb1_get_cport_count(udev);
701         if (num_cports < 0) {
702                 usb_put_dev(udev);
703                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
704                         num_cports);
705                 return num_cports;
706         }
707
708         hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX,
709                                num_cports);
710         if (IS_ERR(hd)) {
711                 usb_put_dev(udev);
712                 return PTR_ERR(hd);
713         }
714
715         es1 = hd_to_es1(hd);
716         es1->hd = hd;
717         es1->usb_intf = interface;
718         es1->usb_dev = udev;
719         spin_lock_init(&es1->cport_out_urb_lock);
720         usb_set_intfdata(interface, es1);
721
722         es1->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es1->cport_to_ep),
723                                    GFP_KERNEL);
724         if (!es1->cport_to_ep) {
725                 retval = -ENOMEM;
726                 goto error;
727         }
728
729         /* find all 3 of our endpoints */
730         iface_desc = interface->cur_altsetting;
731         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
732                 endpoint = &iface_desc->endpoint[i].desc;
733
734                 if (usb_endpoint_is_bulk_in(endpoint)) {
735                         es1->cport_in[bulk_in++].endpoint =
736                                 endpoint->bEndpointAddress;
737                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
738                         es1->cport_out[bulk_out++].endpoint =
739                                 endpoint->bEndpointAddress;
740                 } else {
741                         dev_err(&udev->dev,
742                                 "Unknown endpoint type found, address %x\n",
743                                 endpoint->bEndpointAddress);
744                 }
745         }
746         if ((bulk_in == 0) ||
747             (bulk_out == 0)) {
748                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
749                 goto error;
750         }
751
752         /* Allocate buffers for our cport in messages and start them up */
753         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
754                 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
755                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
756                         struct urb *urb;
757                         u8 *buffer;
758
759                         urb = usb_alloc_urb(0, GFP_KERNEL);
760                         if (!urb)
761                                 goto error;
762                         buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
763                         if (!buffer)
764                                 goto error;
765
766                         usb_fill_bulk_urb(urb, udev,
767                                           usb_rcvbulkpipe(udev,
768                                                           cport_in->endpoint),
769                                           buffer, ES1_GBUF_MSG_SIZE_MAX,
770                                           cport_in_callback, hd);
771                         cport_in->urb[i] = urb;
772                         cport_in->buffer[i] = buffer;
773                         retval = usb_submit_urb(urb, GFP_KERNEL);
774                         if (retval)
775                                 goto error;
776                 }
777         }
778
779         /* Allocate urbs for our CPort OUT messages */
780         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
781                 struct urb *urb;
782
783                 urb = usb_alloc_urb(0, GFP_KERNEL);
784                 if (!urb)
785                         goto error;
786
787                 es1->cport_out_urb[i] = urb;
788                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
789         }
790
791         apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
792                                                         (S_IWUSR | S_IRUGO),
793                                                         gb_debugfs_get(), es1,
794                                                         &apb1_log_enable_fops);
795         return 0;
796 error:
797         ap_disconnect(interface);
798
799         return retval;
800 }
801
802 static struct usb_driver es1_ap_driver = {
803         .name =         "es2_ap_driver",
804         .probe =        ap_probe,
805         .disconnect =   ap_disconnect,
806         .id_table =     id_table,
807 };
808
809 module_usb_driver(es1_ap_driver);
810
811 MODULE_LICENSE("GPL v2");
812 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");