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