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