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