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