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