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