greybus: Merge branch 'master' into branch 'svc'.
[cascardo/linux.git] / drivers / staging / greybus / es1.c
1 /*
2  * Greybus "AP" USB driver for "ES1" 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
20 /* Memory sizes for the buffers sent to/from the ES1 controller */
21 #define ES1_GBUF_MSG_SIZE_MAX   2048
22
23 static const struct usb_device_id id_table[] = {
24         /* Made up numbers for the SVC USB Bridge in ES1 */
25         { USB_DEVICE(0xffff, 0x0001) },
26         { },
27 };
28 MODULE_DEVICE_TABLE(usb, id_table);
29
30 #define APB1_LOG_SIZE           SZ_16K
31 static struct dentry *apb1_log_dentry;
32 static struct dentry *apb1_log_enable_dentry;
33 static struct task_struct *apb1_log_task;
34 static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE);
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
47
48 /* vendor request AP message */
49 #define REQUEST_SVC             0x01
50
51 /* vendor request APB1 log */
52 #define REQUEST_LOG             0x02
53
54 /**
55  * es1_ap_dev - ES1 USB Bridge to AP structure
56  * @usb_dev: pointer to the USB device we are.
57  * @usb_intf: pointer to the USB interface we are bound to.
58  * @hd: pointer to our greybus_host_device structure
59  * @control_endpoint: endpoint to send data to SVC
60  * @cport_in_endpoint: bulk in endpoint for CPort data
61  * @cport-out_endpoint: bulk out endpoint for CPort data
62  * @cport_in_urb: array of urbs for the CPort in messages
63  * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
64  * @cport_out_urb: array of urbs for the CPort out messages
65  * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
66  *                      not.
67  * @cport_out_urb_cancelled: array of flags indicating whether the
68  *                      corresponding @cport_out_urb is being cancelled
69  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
70  */
71 struct es1_ap_dev {
72         struct usb_device *usb_dev;
73         struct usb_interface *usb_intf;
74         struct greybus_host_device *hd;
75
76         __u8 control_endpoint;
77         __u8 cport_in_endpoint;
78         __u8 cport_out_endpoint;
79
80         struct urb *cport_in_urb[NUM_CPORT_IN_URB];
81         u8 *cport_in_buffer[NUM_CPORT_IN_URB];
82         struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
83         bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
84         bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
85         spinlock_t cport_out_urb_lock;
86 };
87
88 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
89 {
90         return (struct es1_ap_dev *)&hd->hd_priv;
91 }
92
93 static void cport_out_callback(struct urb *urb);
94 static void usb_log_enable(struct es1_ap_dev *es1);
95 static void usb_log_disable(struct es1_ap_dev *es1);
96
97 #define ES1_TIMEOUT     500     /* 500 ms for the SVC to do something */
98
99 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
100 {
101         struct urb *urb = NULL;
102         unsigned long flags;
103         int i;
104
105         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
106
107         /* Look in our pool of allocated urbs first, as that's the "fastest" */
108         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
109                 if (es1->cport_out_urb_busy[i] == false &&
110                                 es1->cport_out_urb_cancelled[i] == false) {
111                         es1->cport_out_urb_busy[i] = true;
112                         urb = es1->cport_out_urb[i];
113                         break;
114                 }
115         }
116         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
117         if (urb)
118                 return urb;
119
120         /*
121          * Crap, pool is empty, complain to the syslog and go allocate one
122          * dynamically as we have to succeed.
123          */
124         dev_err(&es1->usb_dev->dev,
125                 "No free CPort OUT urbs, having to dynamically allocate one!\n");
126         return usb_alloc_urb(0, gfp_mask);
127 }
128
129 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
130 {
131         unsigned long flags;
132         int i;
133         /*
134          * See if this was an urb in our pool, if so mark it "free", otherwise
135          * we need to free it ourselves.
136          */
137         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
138         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
139                 if (urb == es1->cport_out_urb[i]) {
140                         es1->cport_out_urb_busy[i] = false;
141                         urb = NULL;
142                         break;
143                 }
144         }
145         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
146
147         /* If urb is not NULL, then we need to free this urb */
148         usb_free_urb(urb);
149 }
150
151 /*
152  * We (ab)use the operation-message header pad bytes to transfer the
153  * cport id in order to minimise overhead.
154  */
155 static void
156 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
157 {
158         header->pad[0] = cport_id;
159 }
160
161 /* Clear the pad bytes used for the CPort id */
162 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
163 {
164         header->pad[0] = 0;
165 }
166
167 /* Extract the CPort id packed into the header, and clear it */
168 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
169 {
170         u16 cport_id = header->pad[0];
171
172         gb_message_cport_clear(header);
173
174         return cport_id;
175 }
176
177 /*
178  * Returns zero if the message was successfully queued, or a negative errno
179  * otherwise.
180  */
181 static int message_send(struct greybus_host_device *hd, u16 cport_id,
182                         struct gb_message *message, gfp_t gfp_mask)
183 {
184         struct es1_ap_dev *es1 = hd_to_es1(hd);
185         struct usb_device *udev = es1->usb_dev;
186         size_t buffer_size;
187         int retval;
188         struct urb *urb;
189         unsigned long flags;
190
191         /*
192          * The data actually transferred will include an indication
193          * of where the data should be sent.  Do one last check of
194          * the target CPort id before filling it in.
195          */
196         if (!cport_id_valid(cport_id)) {
197                 pr_err("invalid destination cport 0x%02x\n", cport_id);
198                 return -EINVAL;
199         }
200
201         /* Find a free urb */
202         urb = next_free_urb(es1, gfp_mask);
203         if (!urb)
204                 return -ENOMEM;
205
206         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
207         message->hcpriv = urb;
208         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
209
210         /* Pack the cport id into the message header */
211         gb_message_cport_pack(message->header, cport_id);
212
213         buffer_size = sizeof(*message->header) + message->payload_size;
214
215         usb_fill_bulk_urb(urb, udev,
216                           usb_sndbulkpipe(udev, es1->cport_out_endpoint),
217                           message->buffer, buffer_size,
218                           cport_out_callback, message);
219         gb_connection_push_timestamp(message->operation->connection);
220         retval = usb_submit_urb(urb, gfp_mask);
221         if (retval) {
222                 pr_err("error %d submitting URB\n", retval);
223
224                 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
225                 message->hcpriv = NULL;
226                 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
227
228                 free_urb(es1, urb);
229                 gb_message_cport_clear(message->header);
230
231                 return retval;
232         }
233
234         return 0;
235 }
236
237 /*
238  * Can not be called in atomic context.
239  */
240 static void message_cancel(struct gb_message *message)
241 {
242         struct greybus_host_device *hd = message->operation->connection->hd;
243         struct es1_ap_dev *es1 = hd_to_es1(hd);
244         struct urb *urb;
245         int i;
246
247         might_sleep();
248
249         spin_lock_irq(&es1->cport_out_urb_lock);
250         urb = message->hcpriv;
251
252         /* Prevent dynamically allocated urb from being deallocated. */
253         usb_get_urb(urb);
254
255         /* Prevent pre-allocated urb from being reused. */
256         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
257                 if (urb == es1->cport_out_urb[i]) {
258                         es1->cport_out_urb_cancelled[i] = true;
259                         break;
260                 }
261         }
262         spin_unlock_irq(&es1->cport_out_urb_lock);
263
264         usb_kill_urb(urb);
265
266         if (i < NUM_CPORT_OUT_URB) {
267                 spin_lock_irq(&es1->cport_out_urb_lock);
268                 es1->cport_out_urb_cancelled[i] = false;
269                 spin_unlock_irq(&es1->cport_out_urb_lock);
270         }
271
272         usb_free_urb(urb);
273 }
274
275 static struct greybus_host_driver es1_driver = {
276         .hd_priv_size           = sizeof(struct es1_ap_dev),
277         .message_send           = message_send,
278         .message_cancel         = message_cancel,
279 };
280
281 /* Common function to report consistent warnings based on URB status */
282 static int check_urb_status(struct urb *urb)
283 {
284         struct device *dev = &urb->dev->dev;
285         int status = urb->status;
286
287         switch (status) {
288         case 0:
289                 return 0;
290
291         case -EOVERFLOW:
292                 dev_err(dev, "%s: overflow actual length is %d\n",
293                         __func__, urb->actual_length);
294         case -ECONNRESET:
295         case -ENOENT:
296         case -ESHUTDOWN:
297         case -EILSEQ:
298         case -EPROTO:
299                 /* device is gone, stop sending */
300                 return status;
301         }
302         dev_err(dev, "%s: unknown status %d\n", __func__, status);
303
304         return -EAGAIN;
305 }
306
307 static void ap_disconnect(struct usb_interface *interface)
308 {
309         struct es1_ap_dev *es1;
310         struct usb_device *udev;
311         int i;
312
313         es1 = usb_get_intfdata(interface);
314         if (!es1)
315                 return;
316
317         usb_log_disable(es1);
318
319         /* Tear down everything! */
320         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
321                 struct urb *urb = es1->cport_out_urb[i];
322
323                 if (!urb)
324                         break;
325                 usb_kill_urb(urb);
326                 usb_free_urb(urb);
327                 es1->cport_out_urb[i] = NULL;
328                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
329         }
330
331         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
332                 struct urb *urb = es1->cport_in_urb[i];
333
334                 if (!urb)
335                         break;
336                 usb_kill_urb(urb);
337                 usb_free_urb(urb);
338                 kfree(es1->cport_in_buffer[i]);
339                 es1->cport_in_buffer[i] = NULL;
340         }
341
342         usb_set_intfdata(interface, NULL);
343         udev = es1->usb_dev;
344         greybus_remove_hd(es1->hd);
345
346         usb_put_dev(udev);
347 }
348
349 static void cport_in_callback(struct urb *urb)
350 {
351         struct greybus_host_device *hd = urb->context;
352         struct device *dev = &urb->dev->dev;
353         struct gb_operation_msg_hdr *header;
354         int status = check_urb_status(urb);
355         int retval;
356         u16 cport_id;
357
358         if (status) {
359                 if ((status == -EAGAIN) || (status == -EPROTO))
360                         goto exit;
361                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
362                 return;
363         }
364
365         if (urb->actual_length < sizeof(*header)) {
366                 dev_err(dev, "%s: short message received\n", __func__);
367                 goto exit;
368         }
369
370         /* Extract the CPort id, which is packed in the message header */
371         header = urb->transfer_buffer;
372         cport_id = gb_message_cport_unpack(header);
373
374         if (cport_id_valid(cport_id))
375                 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
376                                                         urb->actual_length);
377         else
378                 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
379                                 __func__, cport_id);
380 exit:
381         /* put our urb back in the request pool */
382         retval = usb_submit_urb(urb, GFP_ATOMIC);
383         if (retval)
384                 dev_err(dev, "%s: error %d in submitting urb.\n",
385                         __func__, retval);
386 }
387
388 static void cport_out_callback(struct urb *urb)
389 {
390         struct gb_message *message = urb->context;
391         struct greybus_host_device *hd = message->operation->connection->hd;
392         struct es1_ap_dev *es1 = hd_to_es1(hd);
393         int status = check_urb_status(urb);
394         unsigned long flags;
395
396         gb_message_cport_clear(message->header);
397
398         /*
399          * Tell the submitter that the message send (attempt) is
400          * complete, and report the status.
401          */
402         greybus_message_sent(hd, message, status);
403
404         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
405         message->hcpriv = NULL;
406         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
407
408         free_urb(es1, urb);
409 }
410
411 #define APB1_LOG_MSG_SIZE       64
412 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
413 {
414         int retval;
415
416         /* SVC messages go down our control pipe */
417         do {
418                 retval = usb_control_msg(es1->usb_dev,
419                                         usb_rcvctrlpipe(es1->usb_dev,
420                                                         es1->control_endpoint),
421                                         REQUEST_LOG,
422                                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
423                                         0x00, 0x00,
424                                         buf,
425                                         APB1_LOG_MSG_SIZE,
426                                         ES1_TIMEOUT);
427                 if (retval > 0)
428                         kfifo_in(&apb1_log_fifo, buf, retval);
429         } while (retval > 0);
430 }
431
432 static int apb1_log_poll(void *data)
433 {
434         struct es1_ap_dev *es1 = data;
435         char *buf;
436
437         buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
438         if (!buf)
439                 return -ENOMEM;
440
441         while (!kthread_should_stop()) {
442                 msleep(1000);
443                 apb1_log_get(es1, buf);
444         }
445
446         kfree(buf);
447
448         return 0;
449 }
450
451 static ssize_t apb1_log_read(struct file *f, char __user *buf,
452                                 size_t count, loff_t *ppos)
453 {
454         ssize_t ret;
455         size_t copied;
456         char *tmp_buf;
457
458         if (count > APB1_LOG_SIZE)
459                 count = APB1_LOG_SIZE;
460
461         tmp_buf = kmalloc(count, GFP_KERNEL);
462         if (!tmp_buf)
463                 return -ENOMEM;
464
465         copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
466         ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
467
468         kfree(tmp_buf);
469
470         return ret;
471 }
472
473 static const struct file_operations apb1_log_fops = {
474         .read   = apb1_log_read,
475 };
476
477 static void usb_log_enable(struct es1_ap_dev *es1)
478 {
479         if (!IS_ERR_OR_NULL(apb1_log_task))
480                 return;
481
482         /* get log from APB1 */
483         apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
484         if (IS_ERR(apb1_log_task))
485                 return;
486         apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
487                                                 gb_debugfs_get(), NULL,
488                                                 &apb1_log_fops);
489 }
490
491 static void usb_log_disable(struct es1_ap_dev *es1)
492 {
493         if (IS_ERR_OR_NULL(apb1_log_task))
494                 return;
495
496         debugfs_remove(apb1_log_dentry);
497         apb1_log_dentry = NULL;
498
499         kthread_stop(apb1_log_task);
500         apb1_log_task = NULL;
501 }
502
503 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
504                                 size_t count, loff_t *ppos)
505 {
506         char tmp_buf[3];
507         int enable = !IS_ERR_OR_NULL(apb1_log_task);
508
509         sprintf(tmp_buf, "%d\n", enable);
510         return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
511 }
512
513 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
514                                 size_t count, loff_t *ppos)
515 {
516         int enable;
517         ssize_t retval;
518         struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
519
520         retval = kstrtoint_from_user(buf, count, 10, &enable);
521         if (retval)
522                 return retval;
523
524         if (enable)
525                 usb_log_enable(es1);
526         else
527                 usb_log_disable(es1);
528
529         return count;
530 }
531
532 static const struct file_operations apb1_log_enable_fops = {
533         .read   = apb1_log_enable_read,
534         .write  = apb1_log_enable_write,
535 };
536
537 /*
538  * The ES1 USB Bridge device contains 4 endpoints
539  * 1 Control - usual USB stuff + AP -> SVC messages
540  * 1 Interrupt IN - SVC -> AP messages
541  * 1 Bulk IN - CPort data in
542  * 1 Bulk OUT - CPort data out
543  */
544 static int ap_probe(struct usb_interface *interface,
545                     const struct usb_device_id *id)
546 {
547         struct es1_ap_dev *es1;
548         struct greybus_host_device *hd;
549         struct usb_device *udev;
550         struct usb_host_interface *iface_desc;
551         struct usb_endpoint_descriptor *endpoint;
552         bool bulk_in_found = false;
553         bool bulk_out_found = false;
554         int retval = -ENOMEM;
555         int i;
556
557         /* We need to fit a CPort ID in one byte of a message header */
558         BUILD_BUG_ON(CPORT_ID_MAX > U8_MAX);
559
560         udev = usb_get_dev(interface_to_usbdev(interface));
561
562         hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX);
563         if (IS_ERR(hd)) {
564                 usb_put_dev(udev);
565                 return PTR_ERR(hd);
566         }
567
568         es1 = hd_to_es1(hd);
569         es1->hd = hd;
570         es1->usb_intf = interface;
571         es1->usb_dev = udev;
572         spin_lock_init(&es1->cport_out_urb_lock);
573         usb_set_intfdata(interface, es1);
574
575         /* Control endpoint is the pipe to talk to this AP, so save it off */
576         endpoint = &udev->ep0.desc;
577         es1->control_endpoint = endpoint->bEndpointAddress;
578
579         /* find all 3 of our endpoints */
580         iface_desc = interface->cur_altsetting;
581         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
582                 endpoint = &iface_desc->endpoint[i].desc;
583
584                 if (usb_endpoint_is_bulk_in(endpoint)) {
585                         es1->cport_in_endpoint = endpoint->bEndpointAddress;
586                         bulk_in_found = true;
587                 } else if (usb_endpoint_is_bulk_out(endpoint)) {
588                         es1->cport_out_endpoint = endpoint->bEndpointAddress;
589                         bulk_out_found = true;
590                 } else {
591                         dev_err(&udev->dev,
592                                 "Unknown endpoint type found, address %x\n",
593                                 endpoint->bEndpointAddress);
594                 }
595         }
596         if ((bulk_in_found == false) ||
597             (bulk_out_found == false)) {
598                 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
599                 goto error;
600         }
601
602         /* Allocate buffers for our cport in messages and start them up */
603         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
604                 struct urb *urb;
605                 u8 *buffer;
606
607                 urb = usb_alloc_urb(0, GFP_KERNEL);
608                 if (!urb)
609                         goto error;
610                 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
611                 if (!buffer)
612                         goto error;
613
614                 usb_fill_bulk_urb(urb, udev,
615                                   usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
616                                   buffer, ES1_GBUF_MSG_SIZE_MAX,
617                                   cport_in_callback, hd);
618                 es1->cport_in_urb[i] = urb;
619                 es1->cport_in_buffer[i] = buffer;
620                 retval = usb_submit_urb(urb, GFP_KERNEL);
621                 if (retval)
622                         goto error;
623         }
624
625         /* Allocate urbs for our CPort OUT messages */
626         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
627                 struct urb *urb;
628
629                 urb = usb_alloc_urb(0, GFP_KERNEL);
630                 if (!urb)
631                         goto error;
632
633                 es1->cport_out_urb[i] = urb;
634                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
635         }
636
637         apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
638                                                         (S_IWUSR | S_IRUGO),
639                                                         gb_debugfs_get(), es1,
640                                                         &apb1_log_enable_fops);
641         return 0;
642 error:
643         ap_disconnect(interface);
644
645         return retval;
646 }
647
648 static struct usb_driver es1_ap_driver = {
649         .name =         "es1_ap_driver",
650         .probe =        ap_probe,
651         .disconnect =   ap_disconnect,
652         .id_table =     id_table,
653 };
654
655 module_usb_driver(es1_ap_driver);
656
657 MODULE_LICENSE("GPL v2");
658 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");