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