greybus: es2: fix arpc error message
[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 = 0;
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         u32 connection_flags;
694         int ret;
695
696         req = kzalloc(sizeof(*req), GFP_KERNEL);
697         if (!req)
698                 return -ENOMEM;
699
700         connection_flags = 0;
701         if (flags & GB_CONNECTION_FLAG_CONTROL)
702                 connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
703         if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
704                 connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
705
706         req->flags = cpu_to_le32(connection_flags);
707
708         dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
709                         cport_id, connection_flags);
710
711         ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
712                                 GB_APB_REQUEST_CPORT_FLAGS,
713                                 USB_DIR_OUT | USB_TYPE_VENDOR |
714                                 USB_RECIP_INTERFACE, cport_id, 0,
715                                 req, sizeof(*req), ES2_TIMEOUT);
716         if (ret != sizeof(*req)) {
717                 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
718                                 cport_id);
719                 if (ret >= 0)
720                         ret = -EIO;
721
722                 goto out;
723         }
724
725         ret = 0;
726 out:
727         kfree(req);
728
729         return ret;
730 }
731
732 static int cport_disable(struct gb_host_device *hd, u16 cport_id)
733 {
734         int retval;
735
736         retval = cport_reset(hd, cport_id);
737         if (retval)
738                 return retval;
739
740         return 0;
741 }
742
743 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
744 {
745         int retval;
746         struct es2_ap_dev *es2 = hd_to_es2(hd);
747         struct usb_device *udev = es2->usb_dev;
748
749         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
750                                  GB_APB_REQUEST_LATENCY_TAG_EN,
751                                  USB_DIR_OUT | USB_TYPE_VENDOR |
752                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
753                                  0, ES2_TIMEOUT);
754
755         if (retval < 0)
756                 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
757                         cport_id);
758         return retval;
759 }
760
761 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
762 {
763         int retval;
764         struct es2_ap_dev *es2 = hd_to_es2(hd);
765         struct usb_device *udev = es2->usb_dev;
766
767         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
768                                  GB_APB_REQUEST_LATENCY_TAG_DIS,
769                                  USB_DIR_OUT | USB_TYPE_VENDOR |
770                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
771                                  0, ES2_TIMEOUT);
772
773         if (retval < 0)
774                 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
775                         cport_id);
776         return retval;
777 }
778
779 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
780 {
781         int retval;
782         struct es2_ap_dev *es2 = hd_to_es2(hd);
783         struct usb_device *udev = es2->usb_dev;
784
785         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
786                                  GB_APB_REQUEST_CPORT_FEAT_EN,
787                                  USB_DIR_OUT | USB_TYPE_VENDOR |
788                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
789                                  0, ES2_TIMEOUT);
790         if (retval < 0)
791                 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
792                         cport_id, retval);
793         return retval;
794 }
795
796 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
797 {
798         int retval;
799         struct es2_ap_dev *es2 = hd_to_es2(hd);
800         struct usb_device *udev = es2->usb_dev;
801
802         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
803                                  GB_APB_REQUEST_CPORT_FEAT_DIS,
804                                  USB_DIR_OUT | USB_TYPE_VENDOR |
805                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
806                                  0, ES2_TIMEOUT);
807         if (retval < 0)
808                 dev_err(&udev->dev,
809                         "Cannot disable CPort features for cport %u: %d\n",
810                         cport_id, retval);
811         return retval;
812 }
813
814 static int timesync_enable(struct gb_host_device *hd, u8 count,
815                            u64 frame_time, u32 strobe_delay, u32 refclk)
816 {
817         int retval;
818         struct es2_ap_dev *es2 = hd_to_es2(hd);
819         struct usb_device *udev = es2->usb_dev;
820         struct gb_control_timesync_enable_request *request;
821
822         request = kzalloc(sizeof(*request), GFP_KERNEL);
823         if (!request)
824                 return -ENOMEM;
825
826         request->count = count;
827         request->frame_time = cpu_to_le64(frame_time);
828         request->strobe_delay = cpu_to_le32(strobe_delay);
829         request->refclk = cpu_to_le32(refclk);
830         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
831                                  REQUEST_TIMESYNC_ENABLE,
832                                  USB_DIR_OUT | USB_TYPE_VENDOR |
833                                  USB_RECIP_INTERFACE, 0, 0, request,
834                                  sizeof(*request), ES2_TIMEOUT);
835         if (retval < 0)
836                 dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
837
838         kfree(request);
839         return retval;
840 }
841
842 static int timesync_disable(struct gb_host_device *hd)
843 {
844         int retval;
845         struct es2_ap_dev *es2 = hd_to_es2(hd);
846         struct usb_device *udev = es2->usb_dev;
847
848         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
849                                  REQUEST_TIMESYNC_DISABLE,
850                                  USB_DIR_OUT | USB_TYPE_VENDOR |
851                                  USB_RECIP_INTERFACE, 0, 0, NULL,
852                                  0, ES2_TIMEOUT);
853         if (retval < 0)
854                 dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
855
856         return retval;
857 }
858
859 static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
860 {
861         int retval, i;
862         struct es2_ap_dev *es2 = hd_to_es2(hd);
863         struct usb_device *udev = es2->usb_dev;
864         struct timesync_authoritative_request *request;
865
866         request = kzalloc(sizeof(*request), GFP_KERNEL);
867         if (!request)
868                 return -ENOMEM;
869
870         for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
871                 request->frame_time[i] = cpu_to_le64(frame_time[i]);
872
873         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
874                                  REQUEST_TIMESYNC_AUTHORITATIVE,
875                                  USB_DIR_OUT | USB_TYPE_VENDOR |
876                                  USB_RECIP_INTERFACE, 0, 0, request,
877                                  sizeof(*request), ES2_TIMEOUT);
878         if (retval < 0)
879                 dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
880
881         kfree(request);
882         return retval;
883 }
884
885 static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
886 {
887         int retval;
888         struct es2_ap_dev *es2 = hd_to_es2(hd);
889         struct usb_device *udev = es2->usb_dev;
890         __le64 *response_frame_time;
891
892         response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
893         if (!response_frame_time)
894                 return -ENOMEM;
895
896         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
897                                  REQUEST_TIMESYNC_GET_LAST_EVENT,
898                                  USB_DIR_IN | USB_TYPE_VENDOR |
899                                  USB_RECIP_INTERFACE, 0, 0, response_frame_time,
900                                  sizeof(*response_frame_time), ES2_TIMEOUT);
901
902         if (retval != sizeof(*response_frame_time)) {
903                 dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
904                         retval);
905
906                 if (retval >= 0)
907                         retval = -EIO;
908
909                 goto out;
910         }
911         *frame_time = le64_to_cpu(*response_frame_time);
912         retval = 0;
913 out:
914         kfree(response_frame_time);
915         return retval;
916 }
917
918 static struct gb_hd_driver es2_driver = {
919         .hd_priv_size                   = sizeof(struct es2_ap_dev),
920         .message_send                   = message_send,
921         .message_cancel                 = message_cancel,
922         .cport_allocate                 = es2_cport_allocate,
923         .cport_release                  = es2_cport_release,
924         .cport_enable                   = cport_enable,
925         .cport_disable                  = cport_disable,
926         .latency_tag_enable             = latency_tag_enable,
927         .latency_tag_disable            = latency_tag_disable,
928         .output                         = output,
929         .cport_features_enable          = cport_features_enable,
930         .cport_features_disable         = cport_features_disable,
931         .timesync_enable                = timesync_enable,
932         .timesync_disable               = timesync_disable,
933         .timesync_authoritative         = timesync_authoritative,
934         .timesync_get_last_event        = timesync_get_last_event,
935 };
936
937 /* Common function to report consistent warnings based on URB status */
938 static int check_urb_status(struct urb *urb)
939 {
940         struct device *dev = &urb->dev->dev;
941         int status = urb->status;
942
943         switch (status) {
944         case 0:
945                 return 0;
946
947         case -EOVERFLOW:
948                 dev_err(dev, "%s: overflow actual length is %d\n",
949                         __func__, urb->actual_length);
950         case -ECONNRESET:
951         case -ENOENT:
952         case -ESHUTDOWN:
953         case -EILSEQ:
954         case -EPROTO:
955                 /* device is gone, stop sending */
956                 return status;
957         }
958         dev_err(dev, "%s: unknown status %d\n", __func__, status);
959
960         return -EAGAIN;
961 }
962
963 static void es2_destroy(struct es2_ap_dev *es2)
964 {
965         struct usb_device *udev;
966         int bulk_in;
967         int i;
968
969         debugfs_remove(es2->apb_log_enable_dentry);
970         usb_log_disable(es2);
971
972         /* Tear down everything! */
973         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
974                 struct urb *urb = es2->cport_out_urb[i];
975
976                 if (!urb)
977                         break;
978                 usb_kill_urb(urb);
979                 usb_free_urb(urb);
980                 es2->cport_out_urb[i] = NULL;
981                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
982         }
983
984         for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
985                 struct urb *urb = es2->arpc_urb[i];
986
987                 if (!urb)
988                         break;
989                 usb_free_urb(urb);
990                 kfree(es2->arpc_buffer[i]);
991                 es2->arpc_buffer[i] = NULL;
992         }
993
994         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
995                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
996
997                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
998                         struct urb *urb = cport_in->urb[i];
999
1000                         if (!urb)
1001                                 break;
1002                         usb_free_urb(urb);
1003                         kfree(cport_in->buffer[i]);
1004                         cport_in->buffer[i] = NULL;
1005                 }
1006         }
1007
1008         kfree(es2->cport_to_ep);
1009
1010         /* release reserved CDSI0 and CDSI1 cports */
1011         gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
1012         gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
1013
1014         udev = es2->usb_dev;
1015         gb_hd_put(es2->hd);
1016
1017         usb_put_dev(udev);
1018 }
1019
1020 static void cport_in_callback(struct urb *urb)
1021 {
1022         struct gb_host_device *hd = urb->context;
1023         struct device *dev = &urb->dev->dev;
1024         struct gb_operation_msg_hdr *header;
1025         int status = check_urb_status(urb);
1026         int retval;
1027         u16 cport_id;
1028
1029         if (status) {
1030                 if ((status == -EAGAIN) || (status == -EPROTO))
1031                         goto exit;
1032
1033                 /* The urb is being unlinked */
1034                 if (status == -ENOENT || status == -ESHUTDOWN)
1035                         return;
1036
1037                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
1038                 return;
1039         }
1040
1041         if (urb->actual_length < sizeof(*header)) {
1042                 dev_err(dev, "short message received\n");
1043                 goto exit;
1044         }
1045
1046         /* Extract the CPort id, which is packed in the message header */
1047         header = urb->transfer_buffer;
1048         cport_id = gb_message_cport_unpack(header);
1049
1050         if (cport_id_valid(hd, cport_id)) {
1051                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
1052                                                         urb->actual_length);
1053         } else {
1054                 dev_err(dev, "invalid cport id %u received\n", cport_id);
1055         }
1056 exit:
1057         /* put our urb back in the request pool */
1058         retval = usb_submit_urb(urb, GFP_ATOMIC);
1059         if (retval)
1060                 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
1061 }
1062
1063 static void cport_out_callback(struct urb *urb)
1064 {
1065         struct gb_message *message = urb->context;
1066         struct gb_host_device *hd = message->operation->connection->hd;
1067         struct es2_ap_dev *es2 = hd_to_es2(hd);
1068         int status = check_urb_status(urb);
1069         unsigned long flags;
1070
1071         gb_message_cport_clear(message->header);
1072
1073         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
1074         message->hcpriv = NULL;
1075         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
1076
1077         /*
1078          * Tell the submitter that the message send (attempt) is
1079          * complete, and report the status.
1080          */
1081         greybus_message_sent(hd, message, status);
1082
1083         free_urb(es2, urb);
1084 }
1085
1086 static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
1087 {
1088         struct arpc *rpc;
1089
1090         if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
1091                 return NULL;
1092
1093         rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
1094         if (!rpc)
1095                 return NULL;
1096
1097         INIT_LIST_HEAD(&rpc->list);
1098         rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
1099         if (!rpc->req)
1100                 goto err_free_rpc;
1101
1102         rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
1103         if (!rpc->resp)
1104                 goto err_free_req;
1105
1106         rpc->req->type = type;
1107         rpc->req->size = cpu_to_le16(sizeof(rpc->req) + size);
1108         memcpy(rpc->req->data, payload, size);
1109
1110         init_completion(&rpc->response_received);
1111
1112         return rpc;
1113
1114 err_free_req:
1115         kfree(rpc->req);
1116 err_free_rpc:
1117         kfree(rpc);
1118
1119         return NULL;
1120 }
1121
1122 static void arpc_free(struct arpc *rpc)
1123 {
1124         kfree(rpc->req);
1125         kfree(rpc->resp);
1126         kfree(rpc);
1127 }
1128
1129 static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
1130 {
1131         struct arpc *rpc;
1132
1133         list_for_each_entry(rpc, &es2->arpcs, list) {
1134                 if (rpc->req->id == id)
1135                         return rpc;
1136         }
1137
1138         return NULL;
1139 }
1140
1141 static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
1142 {
1143         rpc->active = true;
1144         rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
1145         list_add_tail(&es2->arpcs, &rpc->list);
1146 }
1147
1148 static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
1149 {
1150         if (rpc->active) {
1151                 rpc->active = false;
1152                 list_del(&rpc->list);
1153         }
1154 }
1155
1156 static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
1157 {
1158         struct usb_device *udev = es2->usb_dev;
1159         int retval;
1160
1161         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1162                                  APBA_REQUEST_ARPC_RUN,
1163                                  USB_DIR_OUT | USB_TYPE_VENDOR |
1164                                  USB_RECIP_INTERFACE,
1165                                  0, 0,
1166                                  rpc->req, le16_to_cpu(rpc->req->size),
1167                                  ES2_TIMEOUT);
1168         if (retval != le16_to_cpu(rpc->req->size)) {
1169                 dev_err(&udev->dev,
1170                         "failed to send ARPC request %d: %d\n",
1171                         rpc->req->type, retval);
1172                 if (retval > 0)
1173                         retval = -EIO;
1174                 return retval;
1175         }
1176
1177         return 0;
1178 }
1179
1180 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
1181                      size_t size, int *result, unsigned int timeout)
1182 {
1183         struct arpc *rpc;
1184         unsigned long flags;
1185         int retval;
1186
1187         rpc = arpc_alloc(payload, size, type);
1188         if (!rpc)
1189                 return -ENOMEM;
1190
1191         spin_lock_irqsave(&es2->arpc_lock, flags);
1192         arpc_add(es2, rpc);
1193         spin_unlock_irqrestore(&es2->arpc_lock, flags);
1194
1195         retval = arpc_send(es2, rpc, timeout);
1196         if (retval)
1197                 goto out_arpc_del;
1198
1199         retval = wait_for_completion_interruptible_timeout(
1200                                                 &rpc->response_received,
1201                                                 msecs_to_jiffies(timeout));
1202         if (retval <= 0) {
1203                 if (!retval)
1204                         retval = -ETIMEDOUT;
1205                 goto out_arpc_del;
1206         }
1207
1208         *result = rpc->resp->result;
1209         if (*result)
1210                 retval = -EREMOTEIO;
1211         else
1212                 retval = 0;
1213
1214 out_arpc_del:
1215         spin_lock_irqsave(&es2->arpc_lock, flags);
1216         arpc_del(es2, rpc);
1217         spin_unlock_irqrestore(&es2->arpc_lock, flags);
1218         arpc_free(rpc);
1219
1220         if (retval < 0 && retval != -EREMOTEIO) {
1221                 dev_err(&es2->usb_dev->dev,
1222                         "failed to execute ARPC: %d\n", retval);
1223         }
1224
1225         return retval;
1226 }
1227
1228 static void arpc_in_callback(struct urb *urb)
1229 {
1230         struct es2_ap_dev *es2 = urb->context;
1231         struct device *dev = &urb->dev->dev;
1232         int status = check_urb_status(urb);
1233         struct arpc *rpc;
1234         struct arpc_response_message *resp;
1235         unsigned long flags;
1236         int retval;
1237
1238         if (status) {
1239                 if ((status == -EAGAIN) || (status == -EPROTO))
1240                         goto exit;
1241
1242                 /* The urb is being unlinked */
1243                 if (status == -ENOENT || status == -ESHUTDOWN)
1244                         return;
1245
1246                 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1247                 return;
1248         }
1249
1250         if (urb->actual_length < sizeof(*resp)) {
1251                 dev_err(dev, "short aprc response received\n");
1252                 goto exit;
1253         }
1254
1255         resp = urb->transfer_buffer;
1256         spin_lock_irqsave(&es2->arpc_lock, flags);
1257         rpc = arpc_find(es2, resp->id);
1258         if (!rpc) {
1259                 dev_err(dev, "invalid arpc response id received: %u\n",
1260                         le16_to_cpu(resp->id));
1261                 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1262                 goto exit;
1263         }
1264
1265         arpc_del(es2, rpc);
1266         memcpy(rpc->resp, resp, sizeof(*resp));
1267         complete(&rpc->response_received);
1268         spin_unlock_irqrestore(&es2->arpc_lock, flags);
1269
1270 exit:
1271         /* put our urb back in the request pool */
1272         retval = usb_submit_urb(urb, GFP_ATOMIC);
1273         if (retval)
1274                 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1275 }
1276
1277 #define APB1_LOG_MSG_SIZE       64
1278 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1279 {
1280         int retval;
1281
1282         /* SVC messages go down our control pipe */
1283         do {
1284                 retval = usb_control_msg(es2->usb_dev,
1285                                         usb_rcvctrlpipe(es2->usb_dev, 0),
1286                                         GB_APB_REQUEST_LOG,
1287                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1288                                         0x00, 0x00,
1289                                         buf,
1290                                         APB1_LOG_MSG_SIZE,
1291                                         ES2_TIMEOUT);
1292                 if (retval > 0)
1293                         kfifo_in(&es2->apb_log_fifo, buf, retval);
1294         } while (retval > 0);
1295 }
1296
1297 static int apb_log_poll(void *data)
1298 {
1299         struct es2_ap_dev *es2 = data;
1300         char *buf;
1301
1302         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1303         if (!buf)
1304                 return -ENOMEM;
1305
1306         while (!kthread_should_stop()) {
1307                 msleep(1000);
1308                 apb_log_get(es2, buf);
1309         }
1310
1311         kfree(buf);
1312
1313         return 0;
1314 }
1315
1316 static ssize_t apb_log_read(struct file *f, char __user *buf,
1317                                 size_t count, loff_t *ppos)
1318 {
1319         struct es2_ap_dev *es2 = f->f_inode->i_private;
1320         ssize_t ret;
1321         size_t copied;
1322         char *tmp_buf;
1323
1324         if (count > APB1_LOG_SIZE)
1325                 count = APB1_LOG_SIZE;
1326
1327         tmp_buf = kmalloc(count, GFP_KERNEL);
1328         if (!tmp_buf)
1329                 return -ENOMEM;
1330
1331         copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1332         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1333
1334         kfree(tmp_buf);
1335
1336         return ret;
1337 }
1338
1339 static const struct file_operations apb_log_fops = {
1340         .read   = apb_log_read,
1341 };
1342
1343 static void usb_log_enable(struct es2_ap_dev *es2)
1344 {
1345         if (!IS_ERR_OR_NULL(es2->apb_log_task))
1346                 return;
1347
1348         /* get log from APB1 */
1349         es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1350         if (IS_ERR(es2->apb_log_task))
1351                 return;
1352         /* XXX We will need to rename this per APB */
1353         es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1354                                                 gb_debugfs_get(), es2,
1355                                                 &apb_log_fops);
1356 }
1357
1358 static void usb_log_disable(struct es2_ap_dev *es2)
1359 {
1360         if (IS_ERR_OR_NULL(es2->apb_log_task))
1361                 return;
1362
1363         debugfs_remove(es2->apb_log_dentry);
1364         es2->apb_log_dentry = NULL;
1365
1366         kthread_stop(es2->apb_log_task);
1367         es2->apb_log_task = NULL;
1368 }
1369
1370 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1371                                 size_t count, loff_t *ppos)
1372 {
1373         struct es2_ap_dev *es2 = f->f_inode->i_private;
1374         int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1375         char tmp_buf[3];
1376
1377         sprintf(tmp_buf, "%d\n", enable);
1378         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1379 }
1380
1381 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1382                                 size_t count, loff_t *ppos)
1383 {
1384         int enable;
1385         ssize_t retval;
1386         struct es2_ap_dev *es2 = f->f_inode->i_private;
1387
1388         retval = kstrtoint_from_user(buf, count, 10, &enable);
1389         if (retval)
1390                 return retval;
1391
1392         if (enable)
1393                 usb_log_enable(es2);
1394         else
1395                 usb_log_disable(es2);
1396
1397         return count;
1398 }
1399
1400 static const struct file_operations apb_log_enable_fops = {
1401         .read   = apb_log_enable_read,
1402         .write  = apb_log_enable_write,
1403 };
1404
1405 static int apb_get_cport_count(struct usb_device *udev)
1406 {
1407         int retval;
1408         __le16 *cport_count;
1409
1410         cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1411         if (!cport_count)
1412                 return -ENOMEM;
1413
1414         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1415                                  GB_APB_REQUEST_CPORT_COUNT,
1416                                  USB_DIR_IN | USB_TYPE_VENDOR |
1417                                  USB_RECIP_INTERFACE, 0, 0, cport_count,
1418                                  sizeof(*cport_count), ES2_TIMEOUT);
1419         if (retval != sizeof(*cport_count)) {
1420                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1421                         retval);
1422
1423                 if (retval >= 0)
1424                         retval = -EIO;
1425
1426                 goto out;
1427         }
1428
1429         retval = le16_to_cpu(*cport_count);
1430
1431         /* We need to fit a CPort ID in one byte of a message header */
1432         if (retval > U8_MAX) {
1433                 retval = U8_MAX;
1434                 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1435         }
1436
1437 out:
1438         kfree(cport_count);
1439         return retval;
1440 }
1441
1442 /*
1443  * The ES2 USB Bridge device has 15 endpoints
1444  * 1 Control - usual USB stuff + AP -> APBridgeA messages
1445  * 7 Bulk IN - CPort data in
1446  * 7 Bulk OUT - CPort data out
1447  */
1448 static int ap_probe(struct usb_interface *interface,
1449                     const struct usb_device_id *id)
1450 {
1451         struct es2_ap_dev *es2;
1452         struct gb_host_device *hd;
1453         struct usb_device *udev;
1454         struct usb_host_interface *iface_desc;
1455         struct usb_endpoint_descriptor *endpoint;
1456         int bulk_in = 0;
1457         int bulk_out = 0;
1458         int retval;
1459         int i;
1460         int num_cports;
1461
1462         udev = usb_get_dev(interface_to_usbdev(interface));
1463
1464         num_cports = apb_get_cport_count(udev);
1465         if (num_cports < 0) {
1466                 usb_put_dev(udev);
1467                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1468                         num_cports);
1469                 return num_cports;
1470         }
1471
1472         hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1473                                 num_cports);
1474         if (IS_ERR(hd)) {
1475                 usb_put_dev(udev);
1476                 return PTR_ERR(hd);
1477         }
1478
1479         es2 = hd_to_es2(hd);
1480         es2->hd = hd;
1481         es2->usb_intf = interface;
1482         es2->usb_dev = udev;
1483         spin_lock_init(&es2->cport_out_urb_lock);
1484         INIT_KFIFO(es2->apb_log_fifo);
1485         usb_set_intfdata(interface, es2);
1486
1487         /*
1488          * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1489          * dynamically.
1490          */
1491         retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1492         if (retval)
1493                 goto error;
1494         retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1495         if (retval)
1496                 goto error;
1497
1498         es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
1499                                    GFP_KERNEL);
1500         if (!es2->cport_to_ep) {
1501                 retval = -ENOMEM;
1502                 goto error;
1503         }
1504
1505         /* find all bulk endpoints */
1506         iface_desc = interface->cur_altsetting;
1507         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1508                 endpoint = &iface_desc->endpoint[i].desc;
1509
1510                 if (usb_endpoint_is_bulk_in(endpoint)) {
1511                         if (bulk_in < NUM_BULKS)
1512                                 es2->cport_in[bulk_in].endpoint =
1513                                         endpoint->bEndpointAddress;
1514                         else
1515                                 es2->arpc_endpoint_in =
1516                                         endpoint->bEndpointAddress;
1517                         bulk_in++;
1518                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1519                         es2->cport_out[bulk_out++].endpoint =
1520                                 endpoint->bEndpointAddress;
1521                 } else {
1522                         dev_err(&udev->dev,
1523                                 "Unknown endpoint type found, address 0x%02x\n",
1524                                 endpoint->bEndpointAddress);
1525                 }
1526         }
1527         if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) {
1528                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1529                 retval = -ENODEV;
1530                 goto error;
1531         }
1532
1533         /* Allocate buffers for our cport in messages */
1534         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1535                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1536
1537                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1538                         struct urb *urb;
1539                         u8 *buffer;
1540
1541                         urb = usb_alloc_urb(0, GFP_KERNEL);
1542                         if (!urb) {
1543                                 retval = -ENOMEM;
1544                                 goto error;
1545                         }
1546                         buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1547                         if (!buffer) {
1548                                 retval = -ENOMEM;
1549                                 goto error;
1550                         }
1551
1552                         usb_fill_bulk_urb(urb, udev,
1553                                           usb_rcvbulkpipe(udev,
1554                                                           cport_in->endpoint),
1555                                           buffer, ES2_GBUF_MSG_SIZE_MAX,
1556                                           cport_in_callback, hd);
1557                         cport_in->urb[i] = urb;
1558                         cport_in->buffer[i] = buffer;
1559                 }
1560         }
1561
1562         /* Allocate buffers for ARPC in messages */
1563         for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1564                 struct urb *urb;
1565                 u8 *buffer;
1566
1567                 urb = usb_alloc_urb(0, GFP_KERNEL);
1568                 if (!urb) {
1569                         retval = -ENOMEM;
1570                         goto error;
1571                 }
1572                 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1573                 if (!buffer) {
1574                         retval = -ENOMEM;
1575                         goto error;
1576                 }
1577
1578                 usb_fill_bulk_urb(urb, udev,
1579                                   usb_rcvbulkpipe(udev,
1580                                                   es2->arpc_endpoint_in),
1581                                   buffer, ARPC_IN_SIZE_MAX,
1582                                   arpc_in_callback, es2);
1583
1584                 es2->arpc_urb[i] = urb;
1585                 es2->arpc_buffer[i] = buffer;
1586         }
1587
1588         /* Allocate urbs for our CPort OUT messages */
1589         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1590                 struct urb *urb;
1591
1592                 urb = usb_alloc_urb(0, GFP_KERNEL);
1593                 if (!urb) {
1594                         retval = -ENOMEM;
1595                         goto error;
1596                 }
1597
1598                 es2->cport_out_urb[i] = urb;
1599                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
1600         }
1601
1602         /* XXX We will need to rename this per APB */
1603         es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1604                                                         (S_IWUSR | S_IRUGO),
1605                                                         gb_debugfs_get(), es2,
1606                                                         &apb_log_enable_fops);
1607
1608         INIT_LIST_HEAD(&es2->arpcs);
1609         spin_lock_init(&es2->arpc_lock);
1610
1611         if (es2_arpc_in_enable(es2))
1612                 goto error;
1613
1614         retval = gb_hd_add(hd);
1615         if (retval)
1616                 goto err_disable_arpc_in;
1617
1618         for (i = 0; i < NUM_BULKS; ++i) {
1619                 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1620                 if (retval)
1621                         goto err_disable_cport_in;
1622         }
1623
1624         return 0;
1625
1626 err_disable_cport_in:
1627         for (--i; i >= 0; --i)
1628                 es2_cport_in_disable(es2, &es2->cport_in[i]);
1629         gb_hd_del(hd);
1630 err_disable_arpc_in:
1631         es2_arpc_in_disable(es2);
1632 error:
1633         es2_destroy(es2);
1634
1635         return retval;
1636 }
1637
1638 static void ap_disconnect(struct usb_interface *interface)
1639 {
1640         struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1641         int i;
1642
1643         gb_hd_del(es2->hd);
1644
1645         for (i = 0; i < NUM_BULKS; ++i)
1646                 es2_cport_in_disable(es2, &es2->cport_in[i]);
1647         es2_arpc_in_disable(es2);
1648
1649         es2_destroy(es2);
1650 }
1651
1652 static struct usb_driver es2_ap_driver = {
1653         .name =         "es2_ap_driver",
1654         .probe =        ap_probe,
1655         .disconnect =   ap_disconnect,
1656         .id_table =     id_table,
1657         .soft_unbind =  1,
1658 };
1659
1660 module_usb_driver(es2_ap_driver);
1661
1662 MODULE_LICENSE("GPL v2");
1663 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");