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