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