greybus: es2: Add a new bulk in endpoint for APBridgeA RPC
[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 "greybus_trace.h"
18 #include "kernel_ver.h"
19 #include "connection.h"
20
21 /* Fixed CPort numbers */
22 #define ES2_CPORT_CDSI0         16
23 #define ES2_CPORT_CDSI1         17
24
25 /* Memory sizes for the buffers sent to/from the ES2 controller */
26 #define ES2_GBUF_MSG_SIZE_MAX   2048
27
28 /* Memory sizes for the ARPC buffers */
29 #define ARPC_IN_SIZE_MAX        128
30
31 static const struct usb_device_id id_table[] = {
32         { USB_DEVICE(0x18d1, 0x1eaf) },
33         { },
34 };
35 MODULE_DEVICE_TABLE(usb, id_table);
36
37 #define APB1_LOG_SIZE           SZ_16K
38
39 /* Number of bulk in and bulk out couple */
40 #define NUM_BULKS               7
41
42 /* Expected number of bulk out endpoints */
43 #define NUM_BULKS_OUT           NUM_BULKS
44
45 /* Expected number of bulk in endpoints (including ARPC endpoint) */
46 #define NUM_BULKS_IN            (NUM_BULKS + 1)
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 * NUM_BULKS)
59
60 /*
61  * Number of ARPC in urbs in flight at any point in time.
62  */
63 #define NUM_ARPC_IN_URB         2
64
65 /*
66  * @endpoint: bulk in endpoint for CPort data
67  * @urb: array of urbs for the CPort in messages
68  * @buffer: array of buffers for the @cport_in_urb urbs
69  */
70 struct es2_cport_in {
71         __u8 endpoint;
72         struct urb *urb[NUM_CPORT_IN_URB];
73         u8 *buffer[NUM_CPORT_IN_URB];
74 };
75
76 /*
77  * @endpoint: bulk out endpoint for CPort data
78  */
79 struct es2_cport_out {
80         __u8 endpoint;
81 };
82
83 /**
84  * es2_ap_dev - ES2 USB Bridge to AP structure
85  * @usb_dev: pointer to the USB device we are.
86  * @usb_intf: pointer to the USB interface we are bound to.
87  * @hd: pointer to our gb_host_device structure
88
89  * @cport_in: endpoint, urbs and buffer for cport in messages
90  * @cport_out: endpoint for for cport out messages
91  * @cport_out_urb: array of urbs for the CPort out messages
92  * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
93  *                      not.
94  * @cport_out_urb_cancelled: array of flags indicating whether the
95  *                      corresponding @cport_out_urb is being cancelled
96  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
97  *
98  * @apb_log_task: task pointer for logging thread
99  * @apb_log_dentry: file system entry for the log file interface
100  * @apb_log_enable_dentry: file system entry for enabling logging
101  * @apb_log_fifo: kernel FIFO to carry logged data
102  * @arpc_urb: array of urbs for the ARPC in messages
103  * @arpc_buffer: array of buffers for the @arpc_urb urbs
104  * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
105  */
106 struct es2_ap_dev {
107         struct usb_device *usb_dev;
108         struct usb_interface *usb_intf;
109         struct gb_host_device *hd;
110
111         struct es2_cport_in cport_in[NUM_BULKS];
112         struct es2_cport_out cport_out[NUM_BULKS];
113         struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
114         bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
115         bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
116         spinlock_t cport_out_urb_lock;
117
118         bool cdsi1_in_use;
119
120         int *cport_to_ep;
121
122         struct task_struct *apb_log_task;
123         struct dentry *apb_log_dentry;
124         struct dentry *apb_log_enable_dentry;
125         DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
126
127         __u8 arpc_endpoint_in;
128         struct urb *arpc_urb[NUM_ARPC_IN_URB];
129         u8 *arpc_buffer[NUM_ARPC_IN_URB];
130 };
131
132 /**
133  * cport_to_ep - information about cport to endpoints mapping
134  * @cport_id: the id of cport to map to endpoints
135  * @endpoint_in: the endpoint number to use for in transfer
136  * @endpoint_out: he endpoint number to use for out transfer
137  */
138 struct cport_to_ep {
139         __le16 cport_id;
140         __u8 endpoint_in;
141         __u8 endpoint_out;
142 };
143
144 /**
145  * timesync_enable_request - Enable timesync in an APBridge
146  * @count: number of TimeSync Pulses to expect
147  * @frame_time: the initial FrameTime at the first TimeSync Pulse
148  * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
149  * @refclk: The AP mandated reference clock to run FrameTime at
150  */
151 struct timesync_enable_request {
152         __u8    count;
153         __le64  frame_time;
154         __le32  strobe_delay;
155         __le32  refclk;
156 } __packed;
157
158 /**
159  * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
160  * @frame_time: An array of authoritative FrameTimes provided by the SVC
161  *              and relayed to the APBridge by the AP
162  */
163 struct timesync_authoritative_request {
164         __le64  frame_time[GB_TIMESYNC_MAX_STROBES];
165 } __packed;
166
167 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
168 {
169         return (struct es2_ap_dev *)&hd->hd_priv;
170 }
171
172 static void cport_out_callback(struct urb *urb);
173 static void usb_log_enable(struct es2_ap_dev *es2);
174 static void usb_log_disable(struct es2_ap_dev *es2);
175
176 /* Get the endpoints pair mapped to the cport */
177 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
178 {
179         if (cport_id >= es2->hd->num_cports)
180                 return 0;
181         return es2->cport_to_ep[cport_id];
182 }
183
184 #define ES2_TIMEOUT     500     /* 500 ms for the SVC to do something */
185
186 /* Disable for now until we work all of this out to keep a warning-free build */
187 #if 0
188 /* Test if the endpoints pair is already mapped to a cport */
189 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
190 {
191         int i;
192
193         for (i = 0; i < es2->hd->num_cports; i++) {
194                 if (es2->cport_to_ep[i] == ep_pair)
195                         return 1;
196         }
197         return 0;
198 }
199
200 /* Configure the endpoint mapping and send the request to APBridge */
201 static int map_cport_to_ep(struct es2_ap_dev *es2,
202                                 u16 cport_id, int ep_pair)
203 {
204         int retval;
205         struct cport_to_ep *cport_to_ep;
206
207         if (ep_pair < 0 || ep_pair >= NUM_BULKS)
208                 return -EINVAL;
209         if (cport_id >= es2->hd->num_cports)
210                 return -EINVAL;
211         if (ep_pair && ep_pair_in_use(es2, ep_pair))
212                 return -EINVAL;
213
214         cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
215         if (!cport_to_ep)
216                 return -ENOMEM;
217
218         es2->cport_to_ep[cport_id] = ep_pair;
219         cport_to_ep->cport_id = cpu_to_le16(cport_id);
220         cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
221         cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
222
223         retval = usb_control_msg(es2->usb_dev,
224                                  usb_sndctrlpipe(es2->usb_dev, 0),
225                                  GB_APB_REQUEST_EP_MAPPING,
226                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
227                                  0x00, 0x00,
228                                  (char *)cport_to_ep,
229                                  sizeof(*cport_to_ep),
230                                  ES2_TIMEOUT);
231         if (retval == sizeof(*cport_to_ep))
232                 retval = 0;
233         kfree(cport_to_ep);
234
235         return retval;
236 }
237
238 /* Unmap a cport: use the muxed endpoints pair */
239 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
240 {
241         return map_cport_to_ep(es2, cport_id, 0);
242 }
243 #endif
244
245 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
246 {
247         struct usb_device *udev = es2->usb_dev;
248         u8 *data;
249         int retval;
250
251         data = kmalloc(size, GFP_KERNEL);
252         if (!data)
253                 return -ENOMEM;
254         memcpy(data, req, size);
255
256         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
257                                  cmd,
258                                  USB_DIR_OUT | USB_TYPE_VENDOR |
259                                  USB_RECIP_INTERFACE,
260                                  0, 0, data, size, ES2_TIMEOUT);
261         if (retval < 0)
262                 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
263         else
264                 retval = 0;
265
266         kfree(data);
267         return retval;
268 }
269
270 static void ap_urb_complete(struct urb *urb)
271 {
272         struct usb_ctrlrequest *dr = urb->context;
273
274         kfree(dr);
275         usb_free_urb(urb);
276 }
277
278 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
279 {
280         struct usb_device *udev = es2->usb_dev;
281         struct urb *urb;
282         struct usb_ctrlrequest *dr;
283         u8 *buf;
284         int retval;
285
286         urb = usb_alloc_urb(0, GFP_ATOMIC);
287         if (!urb)
288                 return -ENOMEM;
289
290         dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
291         if (!dr) {
292                 usb_free_urb(urb);
293                 return -ENOMEM;
294         }
295
296         buf = (u8 *)dr + sizeof(*dr);
297         memcpy(buf, req, size);
298
299         dr->bRequest = cmd;
300         dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
301         dr->wValue = 0;
302         dr->wIndex = 0;
303         dr->wLength = cpu_to_le16(size);
304
305         usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
306                              (unsigned char *)dr, buf, size,
307                              ap_urb_complete, dr);
308         retval = usb_submit_urb(urb, GFP_ATOMIC);
309         if (retval) {
310                 usb_free_urb(urb);
311                 kfree(dr);
312         }
313         return retval;
314 }
315
316 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
317                      bool async)
318 {
319         struct es2_ap_dev *es2 = hd_to_es2(hd);
320
321         if (async)
322                 return output_async(es2, req, size, cmd);
323
324         return output_sync(es2, req, size, cmd);
325 }
326
327 static int es2_cport_in_enable(struct es2_ap_dev *es2,
328                                 struct es2_cport_in *cport_in)
329 {
330         struct urb *urb;
331         int ret;
332         int i;
333
334         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
335                 urb = cport_in->urb[i];
336
337                 ret = usb_submit_urb(urb, GFP_KERNEL);
338                 if (ret) {
339                         dev_err(&es2->usb_dev->dev,
340                                         "failed to submit in-urb: %d\n", ret);
341                         goto err_kill_urbs;
342                 }
343         }
344
345         return 0;
346
347 err_kill_urbs:
348         for (--i; i >= 0; --i) {
349                 urb = cport_in->urb[i];
350                 usb_kill_urb(urb);
351         }
352
353         return ret;
354 }
355
356 static void es2_cport_in_disable(struct es2_ap_dev *es2,
357                                 struct es2_cport_in *cport_in)
358 {
359         struct urb *urb;
360         int i;
361
362         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
363                 urb = cport_in->urb[i];
364                 usb_kill_urb(urb);
365         }
366 }
367
368 static int es2_arpc_in_enable(struct es2_ap_dev *es2)
369 {
370         struct urb *urb;
371         int ret;
372         int i;
373
374         for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
375                 urb = es2->arpc_urb[i];
376
377                 ret = usb_submit_urb(urb, GFP_KERNEL);
378                 if (ret) {
379                         dev_err(&es2->usb_dev->dev,
380                                 "failed to submit arpc in-urb: %d\n", ret);
381                         goto err_kill_urbs;
382                 }
383         }
384
385         return 0;
386
387 err_kill_urbs:
388         for (--i; i >= 0; --i) {
389                 urb = es2->arpc_urb[i];
390                 usb_kill_urb(urb);
391         }
392
393         return ret;
394 }
395
396 static void es2_arpc_in_disable(struct es2_ap_dev *es2)
397 {
398         struct urb *urb;
399         int i;
400
401         for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
402                 urb = es2->arpc_urb[i];
403                 usb_kill_urb(urb);
404         }
405 }
406
407 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
408 {
409         struct urb *urb = NULL;
410         unsigned long flags;
411         int i;
412
413         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
414
415         /* Look in our pool of allocated urbs first, as that's the "fastest" */
416         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
417                 if (es2->cport_out_urb_busy[i] == false &&
418                                 es2->cport_out_urb_cancelled[i] == false) {
419                         es2->cport_out_urb_busy[i] = true;
420                         urb = es2->cport_out_urb[i];
421                         break;
422                 }
423         }
424         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
425         if (urb)
426                 return urb;
427
428         /*
429          * Crap, pool is empty, complain to the syslog and go allocate one
430          * dynamically as we have to succeed.
431          */
432         dev_dbg(&es2->usb_dev->dev,
433                 "No free CPort OUT urbs, having to dynamically allocate one!\n");
434         return usb_alloc_urb(0, gfp_mask);
435 }
436
437 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
438 {
439         unsigned long flags;
440         int i;
441         /*
442          * See if this was an urb in our pool, if so mark it "free", otherwise
443          * we need to free it ourselves.
444          */
445         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
446         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
447                 if (urb == es2->cport_out_urb[i]) {
448                         es2->cport_out_urb_busy[i] = false;
449                         urb = NULL;
450                         break;
451                 }
452         }
453         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
454
455         /* If urb is not NULL, then we need to free this urb */
456         usb_free_urb(urb);
457 }
458
459 /*
460  * We (ab)use the operation-message header pad bytes to transfer the
461  * cport id in order to minimise overhead.
462  */
463 static void
464 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
465 {
466         header->pad[0] = cport_id;
467 }
468
469 /* Clear the pad bytes used for the CPort id */
470 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
471 {
472         header->pad[0] = 0;
473 }
474
475 /* Extract the CPort id packed into the header, and clear it */
476 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
477 {
478         u16 cport_id = header->pad[0];
479
480         gb_message_cport_clear(header);
481
482         return cport_id;
483 }
484
485 /*
486  * Returns zero if the message was successfully queued, or a negative errno
487  * otherwise.
488  */
489 static int message_send(struct gb_host_device *hd, u16 cport_id,
490                         struct gb_message *message, gfp_t gfp_mask)
491 {
492         struct es2_ap_dev *es2 = hd_to_es2(hd);
493         struct usb_device *udev = es2->usb_dev;
494         size_t buffer_size;
495         int retval;
496         struct urb *urb;
497         int ep_pair;
498         unsigned long flags;
499
500         /*
501          * The data actually transferred will include an indication
502          * of where the data should be sent.  Do one last check of
503          * the target CPort id before filling it in.
504          */
505         if (!cport_id_valid(hd, cport_id)) {
506                 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
507                 return -EINVAL;
508         }
509
510         /* Find a free urb */
511         urb = next_free_urb(es2, gfp_mask);
512         if (!urb)
513                 return -ENOMEM;
514
515         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
516         message->hcpriv = urb;
517         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
518
519         /* Pack the cport id into the message header */
520         gb_message_cport_pack(message->header, cport_id);
521
522         buffer_size = sizeof(*message->header) + message->payload_size;
523
524         ep_pair = cport_to_ep_pair(es2, cport_id);
525         usb_fill_bulk_urb(urb, udev,
526                           usb_sndbulkpipe(udev,
527                                           es2->cport_out[ep_pair].endpoint),
528                           message->buffer, buffer_size,
529                           cport_out_callback, message);
530         urb->transfer_flags |= URB_ZERO_PACKET;
531
532         trace_gb_message_submit(message);
533
534         retval = usb_submit_urb(urb, gfp_mask);
535         if (retval) {
536                 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
537
538                 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
539                 message->hcpriv = NULL;
540                 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
541
542                 free_urb(es2, urb);
543                 gb_message_cport_clear(message->header);
544
545                 return retval;
546         }
547
548         return 0;
549 }
550
551 /*
552  * Can not be called in atomic context.
553  */
554 static void message_cancel(struct gb_message *message)
555 {
556         struct gb_host_device *hd = message->operation->connection->hd;
557         struct es2_ap_dev *es2 = hd_to_es2(hd);
558         struct urb *urb;
559         int i;
560
561         might_sleep();
562
563         spin_lock_irq(&es2->cport_out_urb_lock);
564         urb = message->hcpriv;
565
566         /* Prevent dynamically allocated urb from being deallocated. */
567         usb_get_urb(urb);
568
569         /* Prevent pre-allocated urb from being reused. */
570         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
571                 if (urb == es2->cport_out_urb[i]) {
572                         es2->cport_out_urb_cancelled[i] = true;
573                         break;
574                 }
575         }
576         spin_unlock_irq(&es2->cport_out_urb_lock);
577
578         usb_kill_urb(urb);
579
580         if (i < NUM_CPORT_OUT_URB) {
581                 spin_lock_irq(&es2->cport_out_urb_lock);
582                 es2->cport_out_urb_cancelled[i] = false;
583                 spin_unlock_irq(&es2->cport_out_urb_lock);
584         }
585
586         usb_free_urb(urb);
587 }
588
589 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
590 {
591         struct es2_ap_dev *es2 = hd_to_es2(hd);
592         struct usb_device *udev = es2->usb_dev;
593         int retval;
594
595         switch (cport_id) {
596         case GB_SVC_CPORT_ID:
597         case ES2_CPORT_CDSI0:
598         case ES2_CPORT_CDSI1:
599                 return 0;
600         }
601
602         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
603                                  GB_APB_REQUEST_RESET_CPORT,
604                                  USB_DIR_OUT | USB_TYPE_VENDOR |
605                                  USB_RECIP_INTERFACE, cport_id, 0,
606                                  NULL, 0, ES2_TIMEOUT);
607         if (retval < 0) {
608                 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
609                         retval);
610                 return retval;
611         }
612
613         return 0;
614 }
615
616 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
617                                 unsigned long flags)
618 {
619         struct es2_ap_dev *es2 = hd_to_es2(hd);
620         struct ida *id_map = &hd->cport_id_map;
621         int ida_start, ida_end;
622
623         switch (cport_id) {
624         case ES2_CPORT_CDSI0:
625         case ES2_CPORT_CDSI1:
626                 dev_err(&hd->dev, "cport %d not available\n", cport_id);
627                 return -EBUSY;
628         }
629
630         if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
631                         flags & GB_CONNECTION_FLAG_CDSI1) {
632                 if (es2->cdsi1_in_use) {
633                         dev_err(&hd->dev, "CDSI1 already in use\n");
634                         return -EBUSY;
635                 }
636
637                 es2->cdsi1_in_use = true;
638
639                 return ES2_CPORT_CDSI1;
640         }
641
642         if (cport_id < 0) {
643                 ida_start = 0;
644                 ida_end = hd->num_cports;
645         } else if (cport_id < hd->num_cports) {
646                 ida_start = cport_id;
647                 ida_end = cport_id + 1;
648         } else {
649                 dev_err(&hd->dev, "cport %d not available\n", cport_id);
650                 return -EINVAL;
651         }
652
653         return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
654 }
655
656 static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
657 {
658         struct es2_ap_dev *es2 = hd_to_es2(hd);
659
660         switch (cport_id) {
661         case ES2_CPORT_CDSI1:
662                 es2->cdsi1_in_use = false;
663                 return;
664         }
665
666         ida_simple_remove(&hd->cport_id_map, cport_id);
667 }
668
669 static int cport_enable(struct gb_host_device *hd, u16 cport_id,
670                         unsigned long flags)
671 {
672         struct es2_ap_dev *es2 = hd_to_es2(hd);
673         struct usb_device *udev = es2->usb_dev;
674         struct gb_apb_request_cport_flags *req;
675         int ret;
676
677         req = kzalloc(sizeof(*req), GFP_KERNEL);
678         if (!req)
679                 return -ENOMEM;
680
681         if (flags & GB_CONNECTION_FLAG_CONTROL)
682                 req->flags |= GB_APB_CPORT_FLAG_CONTROL;
683         if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
684                 req->flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
685
686         dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
687                         cport_id, req->flags);
688
689         ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
690                                 GB_APB_REQUEST_CPORT_FLAGS,
691                                 USB_DIR_OUT | USB_TYPE_VENDOR |
692                                 USB_RECIP_INTERFACE, cport_id, 0,
693                                 req, sizeof(*req), ES2_TIMEOUT);
694         if (ret != sizeof(*req)) {
695                 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
696                                 cport_id);
697                 if (ret >= 0)
698                         ret = -EIO;
699
700                 goto out;
701         }
702
703         ret = 0;
704 out:
705         kfree(req);
706
707         return ret;
708 }
709
710 static int cport_disable(struct gb_host_device *hd, u16 cport_id)
711 {
712         int retval;
713
714         retval = cport_reset(hd, cport_id);
715         if (retval)
716                 return retval;
717
718         return 0;
719 }
720
721 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
722 {
723         int retval;
724         struct es2_ap_dev *es2 = hd_to_es2(hd);
725         struct usb_device *udev = es2->usb_dev;
726
727         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
728                                  GB_APB_REQUEST_LATENCY_TAG_EN,
729                                  USB_DIR_OUT | USB_TYPE_VENDOR |
730                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
731                                  0, ES2_TIMEOUT);
732
733         if (retval < 0)
734                 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
735                         cport_id);
736         return retval;
737 }
738
739 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
740 {
741         int retval;
742         struct es2_ap_dev *es2 = hd_to_es2(hd);
743         struct usb_device *udev = es2->usb_dev;
744
745         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
746                                  GB_APB_REQUEST_LATENCY_TAG_DIS,
747                                  USB_DIR_OUT | USB_TYPE_VENDOR |
748                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
749                                  0, ES2_TIMEOUT);
750
751         if (retval < 0)
752                 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
753                         cport_id);
754         return retval;
755 }
756
757 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
758 {
759         int retval;
760         struct es2_ap_dev *es2 = hd_to_es2(hd);
761         struct usb_device *udev = es2->usb_dev;
762
763         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
764                                  GB_APB_REQUEST_CPORT_FEAT_EN,
765                                  USB_DIR_OUT | USB_TYPE_VENDOR |
766                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
767                                  0, ES2_TIMEOUT);
768         if (retval < 0)
769                 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
770                         cport_id, retval);
771         return retval;
772 }
773
774 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
775 {
776         int retval;
777         struct es2_ap_dev *es2 = hd_to_es2(hd);
778         struct usb_device *udev = es2->usb_dev;
779
780         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
781                                  GB_APB_REQUEST_CPORT_FEAT_DIS,
782                                  USB_DIR_OUT | USB_TYPE_VENDOR |
783                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
784                                  0, ES2_TIMEOUT);
785         if (retval < 0)
786                 dev_err(&udev->dev,
787                         "Cannot disable CPort features for cport %u: %d\n",
788                         cport_id, retval);
789         return retval;
790 }
791
792 static int timesync_enable(struct gb_host_device *hd, u8 count,
793                            u64 frame_time, u32 strobe_delay, u32 refclk)
794 {
795         int retval;
796         struct es2_ap_dev *es2 = hd_to_es2(hd);
797         struct usb_device *udev = es2->usb_dev;
798         struct gb_control_timesync_enable_request *request;
799
800         request = kzalloc(sizeof(*request), GFP_KERNEL);
801         if (!request)
802                 return -ENOMEM;
803
804         request->count = count;
805         request->frame_time = cpu_to_le64(frame_time);
806         request->strobe_delay = cpu_to_le32(strobe_delay);
807         request->refclk = cpu_to_le32(refclk);
808         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
809                                  REQUEST_TIMESYNC_ENABLE,
810                                  USB_DIR_OUT | USB_TYPE_VENDOR |
811                                  USB_RECIP_INTERFACE, 0, 0, request,
812                                  sizeof(*request), ES2_TIMEOUT);
813         if (retval < 0)
814                 dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
815
816         kfree(request);
817         return retval;
818 }
819
820 static int timesync_disable(struct gb_host_device *hd)
821 {
822         int retval;
823         struct es2_ap_dev *es2 = hd_to_es2(hd);
824         struct usb_device *udev = es2->usb_dev;
825
826         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
827                                  REQUEST_TIMESYNC_DISABLE,
828                                  USB_DIR_OUT | USB_TYPE_VENDOR |
829                                  USB_RECIP_INTERFACE, 0, 0, NULL,
830                                  0, ES2_TIMEOUT);
831         if (retval < 0)
832                 dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
833
834         return retval;
835 }
836
837 static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
838 {
839         int retval, i;
840         struct es2_ap_dev *es2 = hd_to_es2(hd);
841         struct usb_device *udev = es2->usb_dev;
842         struct timesync_authoritative_request *request;
843
844         request = kzalloc(sizeof(*request), GFP_KERNEL);
845         if (!request)
846                 return -ENOMEM;
847
848         for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
849                 request->frame_time[i] = cpu_to_le64(frame_time[i]);
850
851         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
852                                  REQUEST_TIMESYNC_AUTHORITATIVE,
853                                  USB_DIR_OUT | USB_TYPE_VENDOR |
854                                  USB_RECIP_INTERFACE, 0, 0, request,
855                                  sizeof(*request), ES2_TIMEOUT);
856         if (retval < 0)
857                 dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
858
859         kfree(request);
860         return retval;
861 }
862
863 static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
864 {
865         int retval;
866         struct es2_ap_dev *es2 = hd_to_es2(hd);
867         struct usb_device *udev = es2->usb_dev;
868         __le64 *response_frame_time;
869
870         response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
871         if (!response_frame_time)
872                 return -ENOMEM;
873
874         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
875                                  REQUEST_TIMESYNC_GET_LAST_EVENT,
876                                  USB_DIR_IN | USB_TYPE_VENDOR |
877                                  USB_RECIP_INTERFACE, 0, 0, response_frame_time,
878                                  sizeof(*response_frame_time), ES2_TIMEOUT);
879
880         if (retval != sizeof(*response_frame_time)) {
881                 dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
882                         retval);
883
884                 if (retval >= 0)
885                         retval = -EIO;
886
887                 goto out;
888         }
889         *frame_time = le64_to_cpu(*response_frame_time);
890         retval = 0;
891 out:
892         kfree(response_frame_time);
893         return retval;
894 }
895
896 static struct gb_hd_driver es2_driver = {
897         .hd_priv_size                   = sizeof(struct es2_ap_dev),
898         .message_send                   = message_send,
899         .message_cancel                 = message_cancel,
900         .cport_allocate                 = es2_cport_allocate,
901         .cport_release                  = es2_cport_release,
902         .cport_enable                   = cport_enable,
903         .cport_disable                  = cport_disable,
904         .latency_tag_enable             = latency_tag_enable,
905         .latency_tag_disable            = latency_tag_disable,
906         .output                         = output,
907         .cport_features_enable          = cport_features_enable,
908         .cport_features_disable         = cport_features_disable,
909         .timesync_enable                = timesync_enable,
910         .timesync_disable               = timesync_disable,
911         .timesync_authoritative         = timesync_authoritative,
912         .timesync_get_last_event        = timesync_get_last_event,
913 };
914
915 /* Common function to report consistent warnings based on URB status */
916 static int check_urb_status(struct urb *urb)
917 {
918         struct device *dev = &urb->dev->dev;
919         int status = urb->status;
920
921         switch (status) {
922         case 0:
923                 return 0;
924
925         case -EOVERFLOW:
926                 dev_err(dev, "%s: overflow actual length is %d\n",
927                         __func__, urb->actual_length);
928         case -ECONNRESET:
929         case -ENOENT:
930         case -ESHUTDOWN:
931         case -EILSEQ:
932         case -EPROTO:
933                 /* device is gone, stop sending */
934                 return status;
935         }
936         dev_err(dev, "%s: unknown status %d\n", __func__, status);
937
938         return -EAGAIN;
939 }
940
941 static void es2_destroy(struct es2_ap_dev *es2)
942 {
943         struct usb_device *udev;
944         int bulk_in;
945         int i;
946
947         debugfs_remove(es2->apb_log_enable_dentry);
948         usb_log_disable(es2);
949
950         /* Tear down everything! */
951         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
952                 struct urb *urb = es2->cport_out_urb[i];
953
954                 if (!urb)
955                         break;
956                 usb_kill_urb(urb);
957                 usb_free_urb(urb);
958                 es2->cport_out_urb[i] = NULL;
959                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
960         }
961
962         for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
963                 struct urb *urb = es2->arpc_urb[i];
964
965                 if (!urb)
966                         break;
967                 usb_free_urb(urb);
968                 kfree(es2->arpc_buffer[i]);
969                 es2->arpc_buffer[i] = NULL;
970         }
971
972         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
973                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
974
975                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
976                         struct urb *urb = cport_in->urb[i];
977
978                         if (!urb)
979                                 break;
980                         usb_free_urb(urb);
981                         kfree(cport_in->buffer[i]);
982                         cport_in->buffer[i] = NULL;
983                 }
984         }
985
986         kfree(es2->cport_to_ep);
987
988         /* release reserved CDSI0 and CDSI1 cports */
989         gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
990         gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
991
992         udev = es2->usb_dev;
993         gb_hd_put(es2->hd);
994
995         usb_put_dev(udev);
996 }
997
998 static void cport_in_callback(struct urb *urb)
999 {
1000         struct gb_host_device *hd = urb->context;
1001         struct device *dev = &urb->dev->dev;
1002         struct gb_operation_msg_hdr *header;
1003         int status = check_urb_status(urb);
1004         int retval;
1005         u16 cport_id;
1006
1007         if (status) {
1008                 if ((status == -EAGAIN) || (status == -EPROTO))
1009                         goto exit;
1010
1011                 /* The urb is being unlinked */
1012                 if (status == -ENOENT || status == -ESHUTDOWN)
1013                         return;
1014
1015                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
1016                 return;
1017         }
1018
1019         if (urb->actual_length < sizeof(*header)) {
1020                 dev_err(dev, "short message received\n");
1021                 goto exit;
1022         }
1023
1024         /* Extract the CPort id, which is packed in the message header */
1025         header = urb->transfer_buffer;
1026         cport_id = gb_message_cport_unpack(header);
1027
1028         if (cport_id_valid(hd, cport_id)) {
1029                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
1030                                                         urb->actual_length);
1031         } else {
1032                 dev_err(dev, "invalid cport id %u received\n", cport_id);
1033         }
1034 exit:
1035         /* put our urb back in the request pool */
1036         retval = usb_submit_urb(urb, GFP_ATOMIC);
1037         if (retval)
1038                 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
1039 }
1040
1041 static void cport_out_callback(struct urb *urb)
1042 {
1043         struct gb_message *message = urb->context;
1044         struct gb_host_device *hd = message->operation->connection->hd;
1045         struct es2_ap_dev *es2 = hd_to_es2(hd);
1046         int status = check_urb_status(urb);
1047         unsigned long flags;
1048
1049         gb_message_cport_clear(message->header);
1050
1051         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
1052         message->hcpriv = NULL;
1053         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
1054
1055         /*
1056          * Tell the submitter that the message send (attempt) is
1057          * complete, and report the status.
1058          */
1059         greybus_message_sent(hd, message, status);
1060
1061         free_urb(es2, urb);
1062 }
1063
1064 static void arpc_in_callback(struct urb *urb)
1065 {
1066         struct device *dev = &urb->dev->dev;
1067         int status = check_urb_status(urb);
1068         int retval;
1069
1070         if (status) {
1071                 if ((status == -EAGAIN) || (status == -EPROTO))
1072                         goto exit;
1073
1074                 /* The urb is being unlinked */
1075                 if (status == -ENOENT || status == -ESHUTDOWN)
1076                         return;
1077
1078                 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1079                 return;
1080         }
1081
1082 exit:
1083         /* put our urb back in the request pool */
1084         retval = usb_submit_urb(urb, GFP_ATOMIC);
1085         if (retval)
1086                 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1087 }
1088
1089 #define APB1_LOG_MSG_SIZE       64
1090 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1091 {
1092         int retval;
1093
1094         /* SVC messages go down our control pipe */
1095         do {
1096                 retval = usb_control_msg(es2->usb_dev,
1097                                         usb_rcvctrlpipe(es2->usb_dev, 0),
1098                                         GB_APB_REQUEST_LOG,
1099                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1100                                         0x00, 0x00,
1101                                         buf,
1102                                         APB1_LOG_MSG_SIZE,
1103                                         ES2_TIMEOUT);
1104                 if (retval > 0)
1105                         kfifo_in(&es2->apb_log_fifo, buf, retval);
1106         } while (retval > 0);
1107 }
1108
1109 static int apb_log_poll(void *data)
1110 {
1111         struct es2_ap_dev *es2 = data;
1112         char *buf;
1113
1114         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1115         if (!buf)
1116                 return -ENOMEM;
1117
1118         while (!kthread_should_stop()) {
1119                 msleep(1000);
1120                 apb_log_get(es2, buf);
1121         }
1122
1123         kfree(buf);
1124
1125         return 0;
1126 }
1127
1128 static ssize_t apb_log_read(struct file *f, char __user *buf,
1129                                 size_t count, loff_t *ppos)
1130 {
1131         struct es2_ap_dev *es2 = f->f_inode->i_private;
1132         ssize_t ret;
1133         size_t copied;
1134         char *tmp_buf;
1135
1136         if (count > APB1_LOG_SIZE)
1137                 count = APB1_LOG_SIZE;
1138
1139         tmp_buf = kmalloc(count, GFP_KERNEL);
1140         if (!tmp_buf)
1141                 return -ENOMEM;
1142
1143         copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1144         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1145
1146         kfree(tmp_buf);
1147
1148         return ret;
1149 }
1150
1151 static const struct file_operations apb_log_fops = {
1152         .read   = apb_log_read,
1153 };
1154
1155 static void usb_log_enable(struct es2_ap_dev *es2)
1156 {
1157         if (!IS_ERR_OR_NULL(es2->apb_log_task))
1158                 return;
1159
1160         /* get log from APB1 */
1161         es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1162         if (IS_ERR(es2->apb_log_task))
1163                 return;
1164         /* XXX We will need to rename this per APB */
1165         es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1166                                                 gb_debugfs_get(), es2,
1167                                                 &apb_log_fops);
1168 }
1169
1170 static void usb_log_disable(struct es2_ap_dev *es2)
1171 {
1172         if (IS_ERR_OR_NULL(es2->apb_log_task))
1173                 return;
1174
1175         debugfs_remove(es2->apb_log_dentry);
1176         es2->apb_log_dentry = NULL;
1177
1178         kthread_stop(es2->apb_log_task);
1179         es2->apb_log_task = NULL;
1180 }
1181
1182 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1183                                 size_t count, loff_t *ppos)
1184 {
1185         struct es2_ap_dev *es2 = f->f_inode->i_private;
1186         int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1187         char tmp_buf[3];
1188
1189         sprintf(tmp_buf, "%d\n", enable);
1190         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1191 }
1192
1193 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1194                                 size_t count, loff_t *ppos)
1195 {
1196         int enable;
1197         ssize_t retval;
1198         struct es2_ap_dev *es2 = f->f_inode->i_private;
1199
1200         retval = kstrtoint_from_user(buf, count, 10, &enable);
1201         if (retval)
1202                 return retval;
1203
1204         if (enable)
1205                 usb_log_enable(es2);
1206         else
1207                 usb_log_disable(es2);
1208
1209         return count;
1210 }
1211
1212 static const struct file_operations apb_log_enable_fops = {
1213         .read   = apb_log_enable_read,
1214         .write  = apb_log_enable_write,
1215 };
1216
1217 static int apb_get_cport_count(struct usb_device *udev)
1218 {
1219         int retval;
1220         __le16 *cport_count;
1221
1222         cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1223         if (!cport_count)
1224                 return -ENOMEM;
1225
1226         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1227                                  GB_APB_REQUEST_CPORT_COUNT,
1228                                  USB_DIR_IN | USB_TYPE_VENDOR |
1229                                  USB_RECIP_INTERFACE, 0, 0, cport_count,
1230                                  sizeof(*cport_count), ES2_TIMEOUT);
1231         if (retval != sizeof(*cport_count)) {
1232                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1233                         retval);
1234
1235                 if (retval >= 0)
1236                         retval = -EIO;
1237
1238                 goto out;
1239         }
1240
1241         retval = le16_to_cpu(*cport_count);
1242
1243         /* We need to fit a CPort ID in one byte of a message header */
1244         if (retval > U8_MAX) {
1245                 retval = U8_MAX;
1246                 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1247         }
1248
1249 out:
1250         kfree(cport_count);
1251         return retval;
1252 }
1253
1254 /*
1255  * The ES2 USB Bridge device has 15 endpoints
1256  * 1 Control - usual USB stuff + AP -> APBridgeA messages
1257  * 7 Bulk IN - CPort data in
1258  * 7 Bulk OUT - CPort data out
1259  */
1260 static int ap_probe(struct usb_interface *interface,
1261                     const struct usb_device_id *id)
1262 {
1263         struct es2_ap_dev *es2;
1264         struct gb_host_device *hd;
1265         struct usb_device *udev;
1266         struct usb_host_interface *iface_desc;
1267         struct usb_endpoint_descriptor *endpoint;
1268         int bulk_in = 0;
1269         int bulk_out = 0;
1270         int retval;
1271         int i;
1272         int num_cports;
1273
1274         udev = usb_get_dev(interface_to_usbdev(interface));
1275
1276         num_cports = apb_get_cport_count(udev);
1277         if (num_cports < 0) {
1278                 usb_put_dev(udev);
1279                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1280                         num_cports);
1281                 return num_cports;
1282         }
1283
1284         hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1285                                 num_cports);
1286         if (IS_ERR(hd)) {
1287                 usb_put_dev(udev);
1288                 return PTR_ERR(hd);
1289         }
1290
1291         es2 = hd_to_es2(hd);
1292         es2->hd = hd;
1293         es2->usb_intf = interface;
1294         es2->usb_dev = udev;
1295         spin_lock_init(&es2->cport_out_urb_lock);
1296         INIT_KFIFO(es2->apb_log_fifo);
1297         usb_set_intfdata(interface, es2);
1298
1299         /*
1300          * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1301          * dynamically.
1302          */
1303         retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1304         if (retval)
1305                 goto error;
1306         retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1307         if (retval)
1308                 goto error;
1309
1310         es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
1311                                    GFP_KERNEL);
1312         if (!es2->cport_to_ep) {
1313                 retval = -ENOMEM;
1314                 goto error;
1315         }
1316
1317         /* find all bulk endpoints */
1318         iface_desc = interface->cur_altsetting;
1319         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1320                 endpoint = &iface_desc->endpoint[i].desc;
1321
1322                 if (usb_endpoint_is_bulk_in(endpoint)) {
1323                         if (bulk_in < NUM_BULKS)
1324                                 es2->cport_in[bulk_in].endpoint =
1325                                         endpoint->bEndpointAddress;
1326                         else
1327                                 es2->arpc_endpoint_in =
1328                                         endpoint->bEndpointAddress;
1329                         bulk_in++;
1330                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1331                         es2->cport_out[bulk_out++].endpoint =
1332                                 endpoint->bEndpointAddress;
1333                 } else {
1334                         dev_err(&udev->dev,
1335                                 "Unknown endpoint type found, address 0x%02x\n",
1336                                 endpoint->bEndpointAddress);
1337                 }
1338         }
1339         if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) {
1340                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1341                 retval = -ENODEV;
1342                 goto error;
1343         }
1344
1345         /* Allocate buffers for our cport in messages */
1346         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1347                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1348
1349                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1350                         struct urb *urb;
1351                         u8 *buffer;
1352
1353                         urb = usb_alloc_urb(0, GFP_KERNEL);
1354                         if (!urb) {
1355                                 retval = -ENOMEM;
1356                                 goto error;
1357                         }
1358                         buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1359                         if (!buffer) {
1360                                 retval = -ENOMEM;
1361                                 goto error;
1362                         }
1363
1364                         usb_fill_bulk_urb(urb, udev,
1365                                           usb_rcvbulkpipe(udev,
1366                                                           cport_in->endpoint),
1367                                           buffer, ES2_GBUF_MSG_SIZE_MAX,
1368                                           cport_in_callback, hd);
1369                         cport_in->urb[i] = urb;
1370                         cport_in->buffer[i] = buffer;
1371                 }
1372         }
1373
1374         /* Allocate buffers for ARPC in messages */
1375         for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1376                 struct urb *urb;
1377                 u8 *buffer;
1378
1379                 urb = usb_alloc_urb(0, GFP_KERNEL);
1380                 if (!urb) {
1381                         retval = -ENOMEM;
1382                         goto error;
1383                 }
1384                 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1385                 if (!buffer) {
1386                         retval = -ENOMEM;
1387                         goto error;
1388                 }
1389
1390                 usb_fill_bulk_urb(urb, udev,
1391                                   usb_rcvbulkpipe(udev,
1392                                                   es2->arpc_endpoint_in),
1393                                   buffer, ARPC_IN_SIZE_MAX,
1394                                   arpc_in_callback, es2);
1395
1396                 es2->arpc_urb[i] = urb;
1397                 es2->arpc_buffer[i] = buffer;
1398         }
1399
1400         /* Allocate urbs for our CPort OUT messages */
1401         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1402                 struct urb *urb;
1403
1404                 urb = usb_alloc_urb(0, GFP_KERNEL);
1405                 if (!urb) {
1406                         retval = -ENOMEM;
1407                         goto error;
1408                 }
1409
1410                 es2->cport_out_urb[i] = urb;
1411                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
1412         }
1413
1414         /* XXX We will need to rename this per APB */
1415         es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1416                                                         (S_IWUSR | S_IRUGO),
1417                                                         gb_debugfs_get(), es2,
1418                                                         &apb_log_enable_fops);
1419
1420         if (es2_arpc_in_enable(es2))
1421                 goto error;
1422
1423         retval = gb_hd_add(hd);
1424         if (retval)
1425                 goto err_disable_arpc_in;
1426
1427         for (i = 0; i < NUM_BULKS; ++i) {
1428                 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1429                 if (retval)
1430                         goto err_disable_cport_in;
1431         }
1432
1433         return 0;
1434
1435 err_disable_cport_in:
1436         for (--i; i >= 0; --i)
1437                 es2_cport_in_disable(es2, &es2->cport_in[i]);
1438         gb_hd_del(hd);
1439 err_disable_arpc_in:
1440         es2_arpc_in_disable(es2);
1441 error:
1442         es2_destroy(es2);
1443
1444         return retval;
1445 }
1446
1447 static void ap_disconnect(struct usb_interface *interface)
1448 {
1449         struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1450         int i;
1451
1452         gb_hd_del(es2->hd);
1453
1454         for (i = 0; i < NUM_BULKS; ++i)
1455                 es2_cport_in_disable(es2, &es2->cport_in[i]);
1456         es2_arpc_in_disable(es2);
1457
1458         es2_destroy(es2);
1459 }
1460
1461 static struct usb_driver es2_ap_driver = {
1462         .name =         "es2_ap_driver",
1463         .probe =        ap_probe,
1464         .disconnect =   ap_disconnect,
1465         .id_table =     id_table,
1466         .soft_unbind =  1,
1467 };
1468
1469 module_usb_driver(es2_ap_driver);
1470
1471 MODULE_LICENSE("GPL v2");
1472 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");