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