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