greybus: es2: clean up CDSI CPort reservation
[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         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
510                                  GB_APB_REQUEST_RESET_CPORT,
511                                  USB_DIR_OUT | USB_TYPE_VENDOR |
512                                  USB_RECIP_INTERFACE, cport_id, 0,
513                                  NULL, 0, ES2_TIMEOUT);
514         if (retval < 0) {
515                 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
516                         retval);
517                 return retval;
518         }
519
520         return 0;
521 }
522
523 static int cport_enable(struct gb_host_device *hd, u16 cport_id)
524 {
525         int retval;
526
527         if (cport_id != GB_SVC_CPORT_ID) {
528                 retval = cport_reset(hd, cport_id);
529                 if (retval)
530                         return retval;
531         }
532
533         return 0;
534 }
535
536 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
537 {
538         int retval;
539         struct es2_ap_dev *es2 = hd_to_es2(hd);
540         struct usb_device *udev = es2->usb_dev;
541
542         if (!cport_id_valid(hd, cport_id)) {
543                 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
544                 return -EINVAL;
545         }
546
547         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
548                                  GB_APB_REQUEST_LATENCY_TAG_EN,
549                                  USB_DIR_OUT | USB_TYPE_VENDOR |
550                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
551                                  0, ES2_TIMEOUT);
552
553         if (retval < 0)
554                 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
555                         cport_id);
556         return retval;
557 }
558
559 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
560 {
561         int retval;
562         struct es2_ap_dev *es2 = hd_to_es2(hd);
563         struct usb_device *udev = es2->usb_dev;
564
565         if (!cport_id_valid(hd, cport_id)) {
566                 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
567                 return -EINVAL;
568         }
569
570         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
571                                  GB_APB_REQUEST_LATENCY_TAG_DIS,
572                                  USB_DIR_OUT | USB_TYPE_VENDOR |
573                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
574                                  0, ES2_TIMEOUT);
575
576         if (retval < 0)
577                 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
578                         cport_id);
579         return retval;
580 }
581
582 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
583 {
584         int retval;
585         struct es2_ap_dev *es2 = hd_to_es2(hd);
586         struct usb_device *udev = es2->usb_dev;
587
588         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
589                                  GB_APB_REQUEST_CPORT_FEAT_EN,
590                                  USB_DIR_OUT | USB_TYPE_VENDOR |
591                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
592                                  0, ES2_TIMEOUT);
593         if (retval < 0)
594                 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
595                         cport_id, retval);
596         return retval;
597 }
598
599 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
600 {
601         int retval;
602         struct es2_ap_dev *es2 = hd_to_es2(hd);
603         struct usb_device *udev = es2->usb_dev;
604
605         retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
606                                  GB_APB_REQUEST_CPORT_FEAT_DIS,
607                                  USB_DIR_OUT | USB_TYPE_VENDOR |
608                                  USB_RECIP_INTERFACE, cport_id, 0, NULL,
609                                  0, ES2_TIMEOUT);
610         if (retval < 0)
611                 dev_err(&udev->dev,
612                         "Cannot disable CPort features for cport %u: %d\n",
613                         cport_id, retval);
614         return retval;
615 }
616
617 static struct gb_hd_driver es2_driver = {
618         .hd_priv_size           = sizeof(struct es2_ap_dev),
619         .message_send           = message_send,
620         .message_cancel         = message_cancel,
621         .cport_enable           = cport_enable,
622         .latency_tag_enable     = latency_tag_enable,
623         .latency_tag_disable    = latency_tag_disable,
624         .output                 = output,
625         .cport_features_enable  = cport_features_enable,
626         .cport_features_disable = cport_features_disable,
627 };
628
629 /* Common function to report consistent warnings based on URB status */
630 static int check_urb_status(struct urb *urb)
631 {
632         struct device *dev = &urb->dev->dev;
633         int status = urb->status;
634
635         switch (status) {
636         case 0:
637                 return 0;
638
639         case -EOVERFLOW:
640                 dev_err(dev, "%s: overflow actual length is %d\n",
641                         __func__, urb->actual_length);
642         case -ECONNRESET:
643         case -ENOENT:
644         case -ESHUTDOWN:
645         case -EILSEQ:
646         case -EPROTO:
647                 /* device is gone, stop sending */
648                 return status;
649         }
650         dev_err(dev, "%s: unknown status %d\n", __func__, status);
651
652         return -EAGAIN;
653 }
654
655 static void es2_destroy(struct es2_ap_dev *es2)
656 {
657         struct usb_device *udev;
658         int bulk_in;
659         int i;
660
661         debugfs_remove(es2->apb_log_enable_dentry);
662         usb_log_disable(es2);
663
664         /* Tear down everything! */
665         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
666                 struct urb *urb = es2->cport_out_urb[i];
667
668                 if (!urb)
669                         break;
670                 usb_kill_urb(urb);
671                 usb_free_urb(urb);
672                 es2->cport_out_urb[i] = NULL;
673                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
674         }
675
676         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
677                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
678
679                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
680                         struct urb *urb = cport_in->urb[i];
681
682                         if (!urb)
683                                 break;
684                         usb_free_urb(urb);
685                         kfree(cport_in->buffer[i]);
686                         cport_in->buffer[i] = NULL;
687                 }
688         }
689
690         kfree(es2->cport_to_ep);
691
692         udev = es2->usb_dev;
693         gb_hd_put(es2->hd);
694
695         usb_put_dev(udev);
696 }
697
698 static void cport_in_callback(struct urb *urb)
699 {
700         struct gb_host_device *hd = urb->context;
701         struct device *dev = &urb->dev->dev;
702         struct gb_operation_msg_hdr *header;
703         int status = check_urb_status(urb);
704         int retval;
705         u16 cport_id;
706
707         if (status) {
708                 if ((status == -EAGAIN) || (status == -EPROTO))
709                         goto exit;
710
711                 /* The urb is being unlinked */
712                 if (status == -ENOENT || status == -ESHUTDOWN)
713                         return;
714
715                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
716                 return;
717         }
718
719         if (urb->actual_length < sizeof(*header)) {
720                 dev_err(dev, "short message received\n");
721                 goto exit;
722         }
723
724         /* Extract the CPort id, which is packed in the message header */
725         header = urb->transfer_buffer;
726         cport_id = gb_message_cport_unpack(header);
727
728         if (cport_id_valid(hd, cport_id)) {
729                 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
730                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
731                                                         urb->actual_length);
732         } else {
733                 dev_err(dev, "invalid cport id %u received\n", cport_id);
734         }
735 exit:
736         /* put our urb back in the request pool */
737         retval = usb_submit_urb(urb, GFP_ATOMIC);
738         if (retval)
739                 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
740 }
741
742 static void cport_out_callback(struct urb *urb)
743 {
744         struct gb_message *message = urb->context;
745         struct gb_host_device *hd = message->operation->connection->hd;
746         struct es2_ap_dev *es2 = hd_to_es2(hd);
747         int status = check_urb_status(urb);
748         unsigned long flags;
749
750         gb_message_cport_clear(message->header);
751
752         spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
753         message->hcpriv = NULL;
754         spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
755
756         /*
757          * Tell the submitter that the message send (attempt) is
758          * complete, and report the status.
759          */
760         greybus_message_sent(hd, message, status);
761
762         free_urb(es2, urb);
763 }
764
765 #define APB1_LOG_MSG_SIZE       64
766 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
767 {
768         int retval;
769
770         /* SVC messages go down our control pipe */
771         do {
772                 retval = usb_control_msg(es2->usb_dev,
773                                         usb_rcvctrlpipe(es2->usb_dev, 0),
774                                         GB_APB_REQUEST_LOG,
775                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
776                                         0x00, 0x00,
777                                         buf,
778                                         APB1_LOG_MSG_SIZE,
779                                         ES2_TIMEOUT);
780                 if (retval > 0)
781                         kfifo_in(&es2->apb_log_fifo, buf, retval);
782         } while (retval > 0);
783 }
784
785 static int apb_log_poll(void *data)
786 {
787         struct es2_ap_dev *es2 = data;
788         char *buf;
789
790         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
791         if (!buf)
792                 return -ENOMEM;
793
794         while (!kthread_should_stop()) {
795                 msleep(1000);
796                 apb_log_get(es2, buf);
797         }
798
799         kfree(buf);
800
801         return 0;
802 }
803
804 static ssize_t apb_log_read(struct file *f, char __user *buf,
805                                 size_t count, loff_t *ppos)
806 {
807         struct es2_ap_dev *es2 = f->f_inode->i_private;
808         ssize_t ret;
809         size_t copied;
810         char *tmp_buf;
811
812         if (count > APB1_LOG_SIZE)
813                 count = APB1_LOG_SIZE;
814
815         tmp_buf = kmalloc(count, GFP_KERNEL);
816         if (!tmp_buf)
817                 return -ENOMEM;
818
819         copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
820         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
821
822         kfree(tmp_buf);
823
824         return ret;
825 }
826
827 static const struct file_operations apb_log_fops = {
828         .read   = apb_log_read,
829 };
830
831 static void usb_log_enable(struct es2_ap_dev *es2)
832 {
833         if (!IS_ERR_OR_NULL(es2->apb_log_task))
834                 return;
835
836         /* get log from APB1 */
837         es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
838         if (IS_ERR(es2->apb_log_task))
839                 return;
840         /* XXX We will need to rename this per APB */
841         es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
842                                                 gb_debugfs_get(), es2,
843                                                 &apb_log_fops);
844 }
845
846 static void usb_log_disable(struct es2_ap_dev *es2)
847 {
848         if (IS_ERR_OR_NULL(es2->apb_log_task))
849                 return;
850
851         debugfs_remove(es2->apb_log_dentry);
852         es2->apb_log_dentry = NULL;
853
854         kthread_stop(es2->apb_log_task);
855         es2->apb_log_task = NULL;
856 }
857
858 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
859                                 size_t count, loff_t *ppos)
860 {
861         struct es2_ap_dev *es2 = f->f_inode->i_private;
862         int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
863         char tmp_buf[3];
864
865         sprintf(tmp_buf, "%d\n", enable);
866         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
867 }
868
869 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
870                                 size_t count, loff_t *ppos)
871 {
872         int enable;
873         ssize_t retval;
874         struct es2_ap_dev *es2 = f->f_inode->i_private;
875
876         retval = kstrtoint_from_user(buf, count, 10, &enable);
877         if (retval)
878                 return retval;
879
880         if (enable)
881                 usb_log_enable(es2);
882         else
883                 usb_log_disable(es2);
884
885         return count;
886 }
887
888 static const struct file_operations apb_log_enable_fops = {
889         .read   = apb_log_enable_read,
890         .write  = apb_log_enable_write,
891 };
892
893 static int apb_get_cport_count(struct usb_device *udev)
894 {
895         int retval;
896         __le16 *cport_count;
897
898         cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
899         if (!cport_count)
900                 return -ENOMEM;
901
902         retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
903                                  GB_APB_REQUEST_CPORT_COUNT,
904                                  USB_DIR_IN | USB_TYPE_VENDOR |
905                                  USB_RECIP_INTERFACE, 0, 0, cport_count,
906                                  sizeof(*cport_count), ES2_TIMEOUT);
907         if (retval != sizeof(*cport_count)) {
908                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
909                         retval);
910
911                 if (retval >= 0)
912                         retval = -EIO;
913
914                 goto out;
915         }
916
917         retval = le16_to_cpu(*cport_count);
918
919         /* We need to fit a CPort ID in one byte of a message header */
920         if (retval > U8_MAX) {
921                 retval = U8_MAX;
922                 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
923         }
924
925 out:
926         kfree(cport_count);
927         return retval;
928 }
929
930 /*
931  * The ES2 USB Bridge device has 15 endpoints
932  * 1 Control - usual USB stuff + AP -> APBridgeA messages
933  * 7 Bulk IN - CPort data in
934  * 7 Bulk OUT - CPort data out
935  */
936 static int ap_probe(struct usb_interface *interface,
937                     const struct usb_device_id *id)
938 {
939         struct es2_ap_dev *es2;
940         struct gb_host_device *hd;
941         struct usb_device *udev;
942         struct usb_host_interface *iface_desc;
943         struct usb_endpoint_descriptor *endpoint;
944         int bulk_in = 0;
945         int bulk_out = 0;
946         int retval;
947         int i;
948         int num_cports;
949
950         udev = usb_get_dev(interface_to_usbdev(interface));
951
952         num_cports = apb_get_cport_count(udev);
953         if (num_cports < 0) {
954                 usb_put_dev(udev);
955                 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
956                         num_cports);
957                 return num_cports;
958         }
959
960         hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
961                                 num_cports);
962         if (IS_ERR(hd)) {
963                 usb_put_dev(udev);
964                 return PTR_ERR(hd);
965         }
966
967         es2 = hd_to_es2(hd);
968         es2->hd = hd;
969         es2->usb_intf = interface;
970         es2->usb_dev = udev;
971         spin_lock_init(&es2->cport_out_urb_lock);
972         INIT_KFIFO(es2->apb_log_fifo);
973         usb_set_intfdata(interface, es2);
974
975         /*
976          * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
977          * dynamically.
978          */
979         retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
980         if (retval)
981                 goto error;
982         retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
983         if (retval)
984                 goto error;
985
986         es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
987                                    GFP_KERNEL);
988         if (!es2->cport_to_ep) {
989                 retval = -ENOMEM;
990                 goto error;
991         }
992
993         /* find all bulk endpoints */
994         iface_desc = interface->cur_altsetting;
995         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
996                 endpoint = &iface_desc->endpoint[i].desc;
997
998                 if (usb_endpoint_is_bulk_in(endpoint)) {
999                         es2->cport_in[bulk_in++].endpoint =
1000                                 endpoint->bEndpointAddress;
1001                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1002                         es2->cport_out[bulk_out++].endpoint =
1003                                 endpoint->bEndpointAddress;
1004                 } else {
1005                         dev_err(&udev->dev,
1006                                 "Unknown endpoint type found, address 0x%02x\n",
1007                                 endpoint->bEndpointAddress);
1008                 }
1009         }
1010         if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
1011                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1012                 retval = -ENODEV;
1013                 goto error;
1014         }
1015
1016         /* Allocate buffers for our cport in messages */
1017         for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1018                 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1019
1020                 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1021                         struct urb *urb;
1022                         u8 *buffer;
1023
1024                         urb = usb_alloc_urb(0, GFP_KERNEL);
1025                         if (!urb) {
1026                                 retval = -ENOMEM;
1027                                 goto error;
1028                         }
1029                         buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1030                         if (!buffer) {
1031                                 retval = -ENOMEM;
1032                                 goto error;
1033                         }
1034
1035                         usb_fill_bulk_urb(urb, udev,
1036                                           usb_rcvbulkpipe(udev,
1037                                                           cport_in->endpoint),
1038                                           buffer, ES2_GBUF_MSG_SIZE_MAX,
1039                                           cport_in_callback, hd);
1040                         cport_in->urb[i] = urb;
1041                         cport_in->buffer[i] = buffer;
1042                 }
1043         }
1044
1045         /* Allocate urbs for our CPort OUT messages */
1046         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1047                 struct urb *urb;
1048
1049                 urb = usb_alloc_urb(0, GFP_KERNEL);
1050                 if (!urb) {
1051                         retval = -ENOMEM;
1052                         goto error;
1053                 }
1054
1055                 es2->cport_out_urb[i] = urb;
1056                 es2->cport_out_urb_busy[i] = false;     /* just to be anal */
1057         }
1058
1059         /* XXX We will need to rename this per APB */
1060         es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1061                                                         (S_IWUSR | S_IRUGO),
1062                                                         gb_debugfs_get(), es2,
1063                                                         &apb_log_enable_fops);
1064
1065         retval = gb_hd_add(hd);
1066         if (retval)
1067                 goto error;
1068
1069         for (i = 0; i < NUM_BULKS; ++i) {
1070                 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1071                 if (retval)
1072                         goto err_disable_cport_in;
1073         }
1074
1075         return 0;
1076
1077 err_disable_cport_in:
1078         for (--i; i >= 0; --i)
1079                 es2_cport_in_disable(es2, &es2->cport_in[i]);
1080         gb_hd_del(hd);
1081 error:
1082         es2_destroy(es2);
1083
1084         return retval;
1085 }
1086
1087 static void ap_disconnect(struct usb_interface *interface)
1088 {
1089         struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1090         int i;
1091
1092         gb_hd_del(es2->hd);
1093
1094         for (i = 0; i < NUM_BULKS; ++i)
1095                 es2_cport_in_disable(es2, &es2->cport_in[i]);
1096
1097         es2_destroy(es2);
1098 }
1099
1100 static struct usb_driver es2_ap_driver = {
1101         .name =         "es2_ap_driver",
1102         .probe =        ap_probe,
1103         .disconnect =   ap_disconnect,
1104         .id_table =     id_table,
1105         .soft_unbind =  1,
1106 };
1107
1108 module_usb_driver(es2_ap_driver);
1109
1110 MODULE_LICENSE("GPL v2");
1111 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");