7e612cbd813e936b2871db193f6eb6daedfa1bb0
[cascardo/linux.git] / drivers / staging / greybus / es1.c
1 /*
2  * Greybus "AP" USB driver
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/kthread.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/sizes.h>
15 #include <linux/usb.h>
16 #include <linux/kfifo.h>
17 #include <linux/debugfs.h>
18 #include <linux/uaccess.h>
19
20 #include "greybus.h"
21 #include "svc_msg.h"
22 #include "kernel_ver.h"
23
24 /*
25  * Macros for making pointers explicitly opaque, such that the result
26  * isn't valid but also can't be mistaken for an ERR_PTR() value.
27  */
28 #define conceal_urb(urb)        ((void *)((uintptr_t)(urb) ^ 0xbad))
29 #define reveal_urb(cookie)      ((void *)((uintptr_t)(cookie) ^ 0xbad))
30
31 /* Memory sizes for the buffers sent to/from the ES1 controller */
32 #define ES1_SVC_MSG_SIZE        (sizeof(struct svc_msg) + SZ_64K)
33 #define ES1_GBUF_MSG_SIZE_MAX   PAGE_SIZE
34
35 static const struct usb_device_id id_table[] = {
36         /* Made up numbers for the SVC USB Bridge in ES1 */
37         { USB_DEVICE(0xffff, 0x0001) },
38         { },
39 };
40 MODULE_DEVICE_TABLE(usb, id_table);
41
42 #define APB1_LOG_SIZE           SZ_16K
43 static struct dentry *apb1_log_dentry;
44 static struct dentry *apb1_log_enable_dentry;
45 static struct task_struct *apb1_log_task;
46 static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE);
47
48 /*
49  * Number of CPort IN urbs in flight at any point in time.
50  * Adjust if we are having stalls in the USB buffer due to not enough urbs in
51  * flight.
52  */
53 #define NUM_CPORT_IN_URB        4
54
55 /* Number of CPort OUT urbs in flight at any point in time.
56  * Adjust if we get messages saying we are out of urbs in the system log.
57  */
58 #define NUM_CPORT_OUT_URB       8
59
60 /**
61  * es1_ap_dev - ES1 USB Bridge to AP structure
62  * @usb_dev: pointer to the USB device we are.
63  * @usb_intf: pointer to the USB interface we are bound to.
64  * @hd: pointer to our greybus_host_device structure
65  * @control_endpoint: endpoint to send data to SVC
66  * @svc_endpoint: endpoint for SVC data in
67  * @cport_in_endpoint: bulk in endpoint for CPort data
68  * @cport-out_endpoint: bulk out endpoint for CPort data
69  * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
70  * @svc_urb: urb for SVC messages coming in on @svc_endpoint
71  * @cport_in_urb: array of urbs for the CPort in messages
72  * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
73  * @cport_out_urb: array of urbs for the CPort out messages
74  * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
75  *                      not.
76  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
77  */
78 struct es1_ap_dev {
79         struct usb_device *usb_dev;
80         struct usb_interface *usb_intf;
81         struct greybus_host_device *hd;
82
83         __u8 control_endpoint;
84         __u8 svc_endpoint;
85         __u8 cport_in_endpoint;
86         __u8 cport_out_endpoint;
87
88         u8 *svc_buffer;
89         struct urb *svc_urb;
90
91         struct urb *cport_in_urb[NUM_CPORT_IN_URB];
92         u8 *cport_in_buffer[NUM_CPORT_IN_URB];
93         struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
94         bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
95         spinlock_t cport_out_urb_lock;
96 };
97
98 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
99 {
100         return (struct es1_ap_dev *)&hd->hd_priv;
101 }
102
103 static void cport_out_callback(struct urb *urb);
104 static void usb_log_enable(struct es1_ap_dev *es1, int enable);
105
106 /*
107  * Buffer constraints for the host driver.
108  *
109  * A "buffer" is used to hold data to be transferred for Greybus by
110  * the host driver.  A buffer is represented by a "buffer pointer",
111  * which defines a region of memory used by the host driver for
112  * transferring the data.  When Greybus allocates a buffer, it must
113  * do so subject to the constraints associated with the host driver.
114  * These constraints are specified by two parameters: the
115  * headroom; and the maximum buffer size.
116  *
117  *                      +------------------+
118  *                      |    Host driver   | \
119  *                      |   reserved area  |  }- headroom
120  *                      |      . . .       | /
121  *  buffer pointer ---> +------------------+
122  *                      | Buffer space for | \
123  *                      | transferred data |  }- buffer size
124  *                      |      . . .       | /   (limited to size_max)
125  *                      +------------------+
126  *
127  *  headroom:   Every buffer must have at least this much space
128  *              *before* the buffer pointer, reserved for use by the
129  *              host driver.  I.e., ((char *)buffer - headroom) must
130  *              point to valid memory, usable only by the host driver.
131  *  size_max:   The maximum size of a buffer (not including the
132  *              headroom) must not exceed this.
133  */
134 static void hd_buffer_constraints(struct greybus_host_device *hd)
135 {
136         /*
137          * Only one byte is required, but this produces a result
138          * that's better aligned for the user.
139          */
140         hd->buffer_headroom = sizeof(u32);      /* For cport id */
141         hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
142         BUILD_BUG_ON(hd->buffer_headroom > GB_BUFFER_HEADROOM_MAX);
143 }
144
145 #define ES1_TIMEOUT     500     /* 500 ms for the SVC to do something */
146 static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
147 {
148         struct es1_ap_dev *es1 = hd_to_es1(hd);
149         int retval;
150
151         /* SVC messages go down our control pipe */
152         retval = usb_control_msg(es1->usb_dev,
153                                  usb_sndctrlpipe(es1->usb_dev,
154                                                  es1->control_endpoint),
155                                  0x01,  /* vendor request AP message */
156                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
157                                  0x00, 0x00,
158                                  (char *)svc_msg,
159                                  sizeof(*svc_msg),
160                                  ES1_TIMEOUT);
161         if (retval != sizeof(*svc_msg))
162                 return retval;
163
164         return 0;
165 }
166
167 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
168 {
169         struct urb *urb = NULL;
170         unsigned long flags;
171         int i;
172
173         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
174
175         /* Look in our pool of allocated urbs first, as that's the "fastest" */
176         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
177                 if (es1->cport_out_urb_busy[i] == false) {
178                         es1->cport_out_urb_busy[i] = true;
179                         urb = es1->cport_out_urb[i];
180                         break;
181                 }
182         }
183         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
184         if (urb)
185                 return urb;
186
187         /*
188          * Crap, pool is empty, complain to the syslog and go allocate one
189          * dynamically as we have to succeed.
190          */
191         dev_err(&es1->usb_dev->dev,
192                 "No free CPort OUT urbs, having to dynamically allocate one!\n");
193         return usb_alloc_urb(0, gfp_mask);
194 }
195
196 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
197 {
198         unsigned long flags;
199         int i;
200         /*
201          * See if this was an urb in our pool, if so mark it "free", otherwise
202          * we need to free it ourselves.
203          */
204         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
205         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
206                 if (urb == es1->cport_out_urb[i]) {
207                         es1->cport_out_urb_busy[i] = false;
208                         urb = NULL;
209                         break;
210                 }
211         }
212         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
213
214         /* If urb is not NULL, then we need to free this urb */
215         usb_free_urb(urb);
216 }
217
218 /*
219  * Returns an opaque cookie value if successful, or a pointer coded
220  * error otherwise.  If the caller wishes to cancel the in-flight
221  * buffer, it must supply the returned cookie to the cancel routine.
222  */
223 static void *buffer_send(struct greybus_host_device *hd, u16 cport_id,
224                         void *buffer, size_t buffer_size, gfp_t gfp_mask)
225 {
226         struct es1_ap_dev *es1 = hd_to_es1(hd);
227         struct usb_device *udev = es1->usb_dev;
228         u8 *transfer_buffer = buffer;
229         int transfer_buffer_size;
230         int retval;
231         struct urb *urb;
232
233         if (!buffer) {
234                 pr_err("null buffer supplied to send\n");
235                 return ERR_PTR(-EINVAL);
236         }
237         if (buffer_size > (size_t)INT_MAX) {
238                 pr_err("bad buffer size (%zu) supplied to send\n", buffer_size);
239                 return ERR_PTR(-EINVAL);
240         }
241         transfer_buffer--;
242         transfer_buffer_size = buffer_size + 1;
243
244         /*
245          * The data actually transferred will include an indication
246          * of where the data should be sent.  Do one last check of
247          * the target CPort id before filling it in.
248          */
249         if (cport_id == CPORT_ID_BAD) {
250                 pr_err("request to send inbound data buffer\n");
251                 return ERR_PTR(-EINVAL);
252         }
253         if (cport_id > (u16)U8_MAX) {
254                 pr_err("cport_id (%hd) is out of range for ES1\n", cport_id);
255                 return ERR_PTR(-EINVAL);
256         }
257         /* OK, the destination is fine; record it in the transfer buffer */
258         *transfer_buffer = cport_id;
259
260         /* Find a free urb */
261         urb = next_free_urb(es1, gfp_mask);
262         if (!urb)
263                 return ERR_PTR(-ENOMEM);
264
265         usb_fill_bulk_urb(urb, udev,
266                           usb_sndbulkpipe(udev, es1->cport_out_endpoint),
267                           transfer_buffer, transfer_buffer_size,
268                           cport_out_callback, hd);
269         retval = usb_submit_urb(urb, gfp_mask);
270         if (retval) {
271                 pr_err("error %d submitting URB\n", retval);
272                 free_urb(es1, urb);
273                 return ERR_PTR(retval);
274         }
275
276         return conceal_urb(urb);
277 }
278
279 /*
280  * The cookie value supplied is the value that buffer_send()
281  * returned to its caller.  It identifies the buffer that should be
282  * canceled.  This function must also handle (which is to say,
283  * ignore) a null cookie value.
284  */
285 static void buffer_cancel(void *cookie)
286 {
287
288         /*
289          * We really should be defensive and track all outstanding
290          * (sent) buffers rather than trusting the cookie provided
291          * is valid.  For the time being, this will do.
292          */
293         if (cookie)
294                 usb_kill_urb(reveal_urb(cookie));
295 }
296
297 static struct greybus_host_driver es1_driver = {
298         .hd_priv_size           = sizeof(struct es1_ap_dev),
299         .buffer_send            = buffer_send,
300         .buffer_cancel          = buffer_cancel,
301         .submit_svc             = submit_svc,
302 };
303
304 /* Common function to report consistent warnings based on URB status */
305 static int check_urb_status(struct urb *urb)
306 {
307         struct device *dev = &urb->dev->dev;
308         int status = urb->status;
309
310         switch (status) {
311         case 0:
312                 return 0;
313
314         case -EOVERFLOW:
315                 dev_err(dev, "%s: overflow actual length is %d\n",
316                         __func__, urb->actual_length);
317         case -ECONNRESET:
318         case -ENOENT:
319         case -ESHUTDOWN:
320         case -EILSEQ:
321         case -EPROTO:
322                 /* device is gone, stop sending */
323                 return status;
324         }
325         dev_err(dev, "%s: unknown status %d\n", __func__, status);
326
327         return -EAGAIN;
328 }
329
330 static void ap_disconnect(struct usb_interface *interface)
331 {
332         struct es1_ap_dev *es1;
333         struct usb_device *udev;
334         int i;
335
336         es1 = usb_get_intfdata(interface);
337         if (!es1)
338                 return;
339
340         usb_log_enable(es1, 0);
341
342         /* Tear down everything! */
343         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
344                 struct urb *urb = es1->cport_out_urb[i];
345
346                 if (!urb)
347                         break;
348                 usb_kill_urb(urb);
349                 usb_free_urb(urb);
350                 es1->cport_out_urb[i] = NULL;
351                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
352         }
353
354         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
355                 struct urb *urb = es1->cport_in_urb[i];
356
357                 if (!urb)
358                         break;
359                 usb_kill_urb(urb);
360                 usb_free_urb(urb);
361                 kfree(es1->cport_in_buffer[i]);
362                 es1->cport_in_buffer[i] = NULL;
363         }
364
365         usb_kill_urb(es1->svc_urb);
366         usb_free_urb(es1->svc_urb);
367         es1->svc_urb = NULL;
368         kfree(es1->svc_buffer);
369         es1->svc_buffer = NULL;
370
371         usb_set_intfdata(interface, NULL);
372         udev = es1->usb_dev;
373         greybus_remove_hd(es1->hd);
374
375         usb_put_dev(udev);
376 }
377
378 /* Callback for when we get a SVC message */
379 static void svc_in_callback(struct urb *urb)
380 {
381         struct greybus_host_device *hd = urb->context;
382         struct device *dev = &urb->dev->dev;
383         int status = check_urb_status(urb);
384         int retval;
385
386         if (status) {
387                 if ((status == -EAGAIN) || (status == -EPROTO))
388                         goto exit;
389                 dev_err(dev, "urb svc in error %d (dropped)\n", status);
390                 return;
391         }
392
393         /* We have a message, create a new message structure, add it to the
394          * list, and wake up our thread that will process the messages.
395          */
396         greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
397
398 exit:
399         /* resubmit the urb to get more messages */
400         retval = usb_submit_urb(urb, GFP_ATOMIC);
401         if (retval)
402                 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
403 }
404
405 static void cport_in_callback(struct urb *urb)
406 {
407         struct greybus_host_device *hd = urb->context;
408         struct device *dev = &urb->dev->dev;
409         int status = check_urb_status(urb);
410         int retval;
411         u16 cport_id;
412         u8 *data;
413
414         if (status) {
415                 if ((status == -EAGAIN) || (status == -EPROTO))
416                         goto exit;
417                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
418                 return;
419         }
420
421         /* The size has to be at least one, for the cport id */
422         if (!urb->actual_length) {
423                 dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
424                 goto exit;
425         }
426
427         /*
428          * Our CPort number is the first byte of the data stream,
429          * the rest of the stream is "real" data
430          */
431         data = urb->transfer_buffer;
432         cport_id = (u16)data[0];
433         data = &data[1];
434
435         /* Pass this data to the greybus core */
436         greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
437
438 exit:
439         /* put our urb back in the request pool */
440         retval = usb_submit_urb(urb, GFP_ATOMIC);
441         if (retval)
442                 dev_err(dev, "%s: error %d in submitting urb.\n",
443                         __func__, retval);
444 }
445
446 static void cport_out_callback(struct urb *urb)
447 {
448         struct greybus_host_device *hd = urb->context;
449         struct es1_ap_dev *es1 = hd_to_es1(hd);
450         int status = check_urb_status(urb);
451         u8 *data = urb->transfer_buffer + 1;
452
453         /*
454          * Tell the submitter that the buffer send (attempt) is
455          * complete, and report the status.  The submitter's buffer
456          * starts after the one-byte CPort id we inserted.
457          */
458         data = urb->transfer_buffer + 1;
459         greybus_data_sent(hd, data, status);
460
461         free_urb(es1, urb);
462         /*
463          * Rest assured Greg, this craziness is getting fixed.
464          *
465          * Yes, you are right, we aren't telling anyone that the urb finished.
466          * "That's crazy!  How does this all even work?" you might be saying.
467          * The "magic" is the idea that greybus works on the "operation" level,
468          * not the "send a buffer" level.  All operations are "round-trip" with
469          * a response from the device that the operation finished, or it will
470          * time out.  Because of that, we don't care that this urb finished, or
471          * failed, or did anything else, as higher levels of the protocol stack
472          * will handle completions and timeouts and the rest.
473          *
474          * This protocol is "needed" due to some hardware restrictions on the
475          * current generation of Unipro controllers.  Think about it for a
476          * minute, this is a USB driver, talking to a Unipro bridge, impedance
477          * mismatch is huge, yet the Unipro controller are even more
478          * underpowered than this little USB controller.  We rely on the round
479          * trip to keep stalls in the Unipro controllers from happening so that
480          * we can keep data flowing properly, no matter how slow it might be.
481          *
482          * Once again, a wonderful bus protocol cut down in its prime by a naive
483          * controller chip.  We dream of the day we have a "real" HCD for
484          * Unipro.  Until then, we suck it up and make the hardware work, as
485          * that's the job of the firmware and kernel.
486          * </rant>
487          */
488 }
489
490 static void apb1_log_get(struct es1_ap_dev *es1)
491 {
492         char buf[65];
493         int retval;
494
495         /* SVC messages go down our control pipe */
496         do {
497                 memset(buf, 0, 65);
498                 retval = usb_control_msg(es1->usb_dev,
499                                         usb_rcvctrlpipe(es1->usb_dev,
500                                                         es1->control_endpoint),
501                                         0x02,   /* vendor request APB1 log */
502                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
503                                         0x00, 0x00,
504                                         buf,
505                                         64,
506                                         ES1_TIMEOUT);
507                 if (retval > 0)
508                         kfifo_in(&apb1_log_fifo, buf, retval);
509         } while (retval > 0);
510 }
511
512 static int apb1_log_poll(void *data)
513 {
514         while (!kthread_should_stop()) {
515                 msleep(1000);
516                 apb1_log_get((struct es1_ap_dev *)data);
517         }
518         return 0;
519 }
520
521 static ssize_t apb1_log_read(struct file *f, char __user *buf,
522                                 size_t count, loff_t *ppos)
523 {
524         ssize_t ret;
525         size_t copied;
526         char *tmp_buf;
527
528         if (count > APB1_LOG_SIZE)
529                 count = APB1_LOG_SIZE;
530
531         tmp_buf = kmalloc(count, GFP_KERNEL);
532         if (!tmp_buf)
533                 return -ENOMEM;
534
535         copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
536         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
537
538         kfree(tmp_buf);
539
540         return ret;
541 }
542
543 static const struct file_operations apb1_log_fops = {
544         .read   = apb1_log_read,
545 };
546
547 static void usb_log_enable(struct es1_ap_dev *es1, int enable)
548 {
549         if (enable && apb1_log_task != NULL)
550                 return;
551
552         if (enable) {
553                 /* get log from APB1 */
554                 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
555                 if (apb1_log_task == ERR_PTR(-ENOMEM))
556                         return;
557                 apb1_log_dentry = debugfs_create_file("apb1_log", 444,
558                                                         gb_debugfs_get(), NULL,
559                                                         &apb1_log_fops);
560         } else {
561                 debugfs_remove(apb1_log_dentry);
562                 apb1_log_dentry = NULL;
563
564                 if (apb1_log_task) {
565                         kthread_stop(apb1_log_task);
566                         apb1_log_task = NULL;
567                 }
568         }
569 }
570
571 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
572                                 size_t count, loff_t *ppos)
573 {
574         char tmp_buf[3];
575         int enable = apb1_log_task != NULL;
576         sprintf(tmp_buf, "%d\n", enable);
577         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
578 }
579
580 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
581                                 size_t count, loff_t *ppos)
582 {
583         int enable;
584         char *tmp_buf;
585         ssize_t retval = -EINVAL;
586         struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
587
588         tmp_buf = kmalloc(count, GFP_KERNEL);
589         if (!tmp_buf)
590                 return -ENOMEM;
591
592         copy_from_user(tmp_buf, buf, count);
593         if (sscanf(tmp_buf, "%d", &enable) == 1) {
594                 usb_log_enable(es1, enable);
595                 retval = count;
596         }
597         kfree(tmp_buf);
598
599         return retval;
600 }
601
602 static const struct file_operations apb1_log_enable_fops = {
603         .read   = apb1_log_enable_read,
604         .write  = apb1_log_enable_write,
605 };
606
607 /*
608  * The ES1 USB Bridge device contains 4 endpoints
609  * 1 Control - usual USB stuff + AP -> SVC messages
610  * 1 Interrupt IN - SVC -> AP messages
611  * 1 Bulk IN - CPort data in
612  * 1 Bulk OUT - CPort data out
613  */
614 static int ap_probe(struct usb_interface *interface,
615                     const struct usb_device_id *id)
616 {
617         struct es1_ap_dev *es1;
618         struct greybus_host_device *hd;
619         struct usb_device *udev;
620         struct usb_host_interface *iface_desc;
621         struct usb_endpoint_descriptor *endpoint;
622         bool int_in_found = false;
623         bool bulk_in_found = false;
624         bool bulk_out_found = false;
625         int retval = -ENOMEM;
626         int i;
627         u8 svc_interval = 0;
628
629         udev = usb_get_dev(interface_to_usbdev(interface));
630
631         hd = greybus_create_hd(&es1_driver, &udev->dev);
632         if (!hd) {
633                 usb_put_dev(udev);
634                 return -ENOMEM;
635         }
636
637         /* Fill in the buffer allocation constraints */
638         hd_buffer_constraints(hd);
639
640         es1 = hd_to_es1(hd);
641         es1->hd = hd;
642         es1->usb_intf = interface;
643         es1->usb_dev = udev;
644         spin_lock_init(&es1->cport_out_urb_lock);
645         usb_set_intfdata(interface, es1);
646
647         /* Control endpoint is the pipe to talk to this AP, so save it off */
648         endpoint = &udev->ep0.desc;
649         es1->control_endpoint = endpoint->bEndpointAddress;
650
651         /* find all 3 of our endpoints */
652         iface_desc = interface->cur_altsetting;
653         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
654                 endpoint = &iface_desc->endpoint[i].desc;
655
656                 if (usb_endpoint_is_int_in(endpoint)) {
657                         es1->svc_endpoint = endpoint->bEndpointAddress;
658                         svc_interval = endpoint->bInterval;
659                         int_in_found = true;
660                 } else if (usb_endpoint_is_bulk_in(endpoint)) {
661                         es1->cport_in_endpoint = endpoint->bEndpointAddress;
662                         bulk_in_found = true;
663                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
664                         es1->cport_out_endpoint = endpoint->bEndpointAddress;
665                         bulk_out_found = true;
666                 } else {
667                         dev_err(&udev->dev,
668                                 "Unknown endpoint type found, address %x\n",
669                                 endpoint->bEndpointAddress);
670                 }
671         }
672         if ((int_in_found == false) ||
673             (bulk_in_found == false) ||
674             (bulk_out_found == false)) {
675                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
676                 goto error;
677         }
678
679         /* Create our buffer and URB to get SVC messages, and start it up */
680         es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
681         if (!es1->svc_buffer)
682                 goto error;
683
684         es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
685         if (!es1->svc_urb)
686                 goto error;
687
688         usb_fill_int_urb(es1->svc_urb, udev,
689                          usb_rcvintpipe(udev, es1->svc_endpoint),
690                          es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
691                          hd, svc_interval);
692
693         /* Allocate buffers for our cport in messages and start them up */
694         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
695                 struct urb *urb;
696                 u8 *buffer;
697
698                 urb = usb_alloc_urb(0, GFP_KERNEL);
699                 if (!urb)
700                         goto error;
701                 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
702                 if (!buffer)
703                         goto error;
704
705                 usb_fill_bulk_urb(urb, udev,
706                                   usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
707                                   buffer, ES1_GBUF_MSG_SIZE_MAX,
708                                   cport_in_callback, hd);
709                 es1->cport_in_urb[i] = urb;
710                 es1->cport_in_buffer[i] = buffer;
711                 retval = usb_submit_urb(urb, GFP_KERNEL);
712                 if (retval)
713                         goto error;
714         }
715
716         /* Allocate urbs for our CPort OUT messages */
717         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
718                 struct urb *urb;
719
720                 urb = usb_alloc_urb(0, GFP_KERNEL);
721                 if (!urb)
722                         goto error;
723
724                 es1->cport_out_urb[i] = urb;
725                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
726         }
727
728         /* Start up our svc urb, which allows events to start flowing */
729         retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
730         if (retval)
731                 goto error;
732
733         apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable", 666,
734                                                         gb_debugfs_get(), es1,
735                                                         &apb1_log_enable_fops);
736
737         return 0;
738 error:
739         ap_disconnect(interface);
740
741         return retval;
742 }
743
744 static struct usb_driver es1_ap_driver = {
745         .name =         "es1_ap_driver",
746         .probe =        ap_probe,
747         .disconnect =   ap_disconnect,
748         .id_table =     id_table,
749 };
750
751 module_usb_driver(es1_ap_driver);
752
753 MODULE_LICENSE("GPL");
754 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");