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