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