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