2 * Greybus "AP" USB driver for "ES1" controller chips
4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
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>
17 #include "kernel_ver.h"
18 #include "connection.h"
19 #include "greybus_trace.h"
21 /* Memory sizes for the buffers sent to/from the ES1 controller */
22 #define ES1_GBUF_MSG_SIZE_MAX 2048
24 static const struct usb_device_id id_table[] = {
25 /* Made up numbers for the SVC USB Bridge in ES1 */
26 { USB_DEVICE(0xffff, 0x0001) },
29 MODULE_DEVICE_TABLE(usb, id_table);
31 #define APB1_LOG_SIZE SZ_16K
32 static struct dentry *apb1_log_dentry;
33 static struct dentry *apb1_log_enable_dentry;
34 static struct task_struct *apb1_log_task;
35 static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE);
37 /* Number of CPorts supported by es1 */
38 #define CPORT_COUNT 256
41 * Number of CPort IN urbs in flight at any point in time.
42 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
45 #define NUM_CPORT_IN_URB 4
47 /* Number of CPort OUT urbs in flight at any point in time.
48 * Adjust if we get messages saying we are out of urbs in the system log.
50 #define NUM_CPORT_OUT_URB 8
52 /* vendor request APB1 log */
53 #define REQUEST_LOG 0x02
55 /* vendor request to time the latency of messages on a given cport */
56 #define REQUEST_LATENCY_TAG_EN 0x06
57 #define REQUEST_LATENCY_TAG_DIS 0x07
60 * es1_ap_dev - ES1 USB Bridge to AP structure
61 * @usb_dev: pointer to the USB device we are.
62 * @usb_intf: pointer to the USB interface we are bound to.
63 * @hd: pointer to our greybus_host_device structure
64 * @cport_in_endpoint: bulk in endpoint for CPort data
65 * @cport-out_endpoint: bulk out endpoint for CPort data
66 * @cport_in_urb: array of urbs for the CPort in messages
67 * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
68 * @cport_out_urb: array of urbs for the CPort out messages
69 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
71 * @cport_out_urb_cancelled: array of flags indicating whether the
72 * corresponding @cport_out_urb is being cancelled
73 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
76 struct usb_device *usb_dev;
77 struct usb_interface *usb_intf;
78 struct greybus_host_device *hd;
80 __u8 cport_in_endpoint;
81 __u8 cport_out_endpoint;
83 struct urb *cport_in_urb[NUM_CPORT_IN_URB];
84 u8 *cport_in_buffer[NUM_CPORT_IN_URB];
85 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
86 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
87 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
88 spinlock_t cport_out_urb_lock;
91 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
93 return (struct es1_ap_dev *)&hd->hd_priv;
96 static void cport_out_callback(struct urb *urb);
97 static void usb_log_enable(struct es1_ap_dev *es1);
98 static void usb_log_disable(struct es1_ap_dev *es1);
100 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
102 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
104 struct urb *urb = NULL;
108 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
110 /* Look in our pool of allocated urbs first, as that's the "fastest" */
111 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
112 if (es1->cport_out_urb_busy[i] == false &&
113 es1->cport_out_urb_cancelled[i] == false) {
114 es1->cport_out_urb_busy[i] = true;
115 urb = es1->cport_out_urb[i];
119 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
124 * Crap, pool is empty, complain to the syslog and go allocate one
125 * dynamically as we have to succeed.
127 dev_err(&es1->usb_dev->dev,
128 "No free CPort OUT urbs, having to dynamically allocate one!\n");
129 return usb_alloc_urb(0, gfp_mask);
132 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
137 * See if this was an urb in our pool, if so mark it "free", otherwise
138 * we need to free it ourselves.
140 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
141 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
142 if (urb == es1->cport_out_urb[i]) {
143 es1->cport_out_urb_busy[i] = false;
148 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
150 /* If urb is not NULL, then we need to free this urb */
155 * We (ab)use the operation-message header pad bytes to transfer the
156 * cport id in order to minimise overhead.
159 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
161 header->pad[0] = cport_id;
164 /* Clear the pad bytes used for the CPort id */
165 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
170 /* Extract the CPort id packed into the header, and clear it */
171 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
173 u16 cport_id = header->pad[0];
175 gb_message_cport_clear(header);
181 * Returns zero if the message was successfully queued, or a negative errno
184 static int message_send(struct greybus_host_device *hd, u16 cport_id,
185 struct gb_message *message, gfp_t gfp_mask)
187 struct es1_ap_dev *es1 = hd_to_es1(hd);
188 struct usb_device *udev = es1->usb_dev;
195 * The data actually transferred will include an indication
196 * of where the data should be sent. Do one last check of
197 * the target CPort id before filling it in.
199 if (!cport_id_valid(hd, cport_id)) {
200 dev_err(&udev->dev, "invalid destination cport 0x%02x\n",
205 /* Find a free urb */
206 urb = next_free_urb(es1, gfp_mask);
210 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
211 message->hcpriv = urb;
212 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
214 /* Pack the cport id into the message header */
215 gb_message_cport_pack(message->header, cport_id);
217 buffer_size = sizeof(*message->header) + message->payload_size;
219 usb_fill_bulk_urb(urb, udev,
220 usb_sndbulkpipe(udev, es1->cport_out_endpoint),
221 message->buffer, buffer_size,
222 cport_out_callback, message);
223 urb->transfer_flags |= URB_ZERO_PACKET;
224 trace_gb_host_device_send(hd, cport_id, buffer_size);
225 retval = usb_submit_urb(urb, gfp_mask);
227 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
229 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
230 message->hcpriv = NULL;
231 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
234 gb_message_cport_clear(message->header);
243 * Can not be called in atomic context.
245 static void message_cancel(struct gb_message *message)
247 struct greybus_host_device *hd = message->operation->connection->hd;
248 struct es1_ap_dev *es1 = hd_to_es1(hd);
254 spin_lock_irq(&es1->cport_out_urb_lock);
255 urb = message->hcpriv;
257 /* Prevent dynamically allocated urb from being deallocated. */
260 /* Prevent pre-allocated urb from being reused. */
261 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
262 if (urb == es1->cport_out_urb[i]) {
263 es1->cport_out_urb_cancelled[i] = true;
267 spin_unlock_irq(&es1->cport_out_urb_lock);
271 if (i < NUM_CPORT_OUT_URB) {
272 spin_lock_irq(&es1->cport_out_urb_lock);
273 es1->cport_out_urb_cancelled[i] = false;
274 spin_unlock_irq(&es1->cport_out_urb_lock);
280 static int latency_tag_enable(struct greybus_host_device *hd, u16 cport_id)
283 struct es1_ap_dev *es1 = hd_to_es1(hd);
284 struct usb_device *udev = es1->usb_dev;
286 if (!cport_id_valid(hd, cport_id)) {
287 dev_err(&udev->dev, "invalid destination cport 0x%02x\n",
292 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
293 REQUEST_LATENCY_TAG_EN,
294 USB_DIR_OUT | USB_TYPE_VENDOR |
295 USB_RECIP_INTERFACE, cport_id, 0, NULL,
299 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
304 static int latency_tag_disable(struct greybus_host_device *hd, u16 cport_id)
307 struct es1_ap_dev *es1 = hd_to_es1(hd);
308 struct usb_device *udev = es1->usb_dev;
310 if (!cport_id_valid(hd, cport_id)) {
311 dev_err(&udev->dev, "invalid destination cport 0x%02x\n",
316 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
317 REQUEST_LATENCY_TAG_DIS,
318 USB_DIR_OUT | USB_TYPE_VENDOR |
319 USB_RECIP_INTERFACE, cport_id, 0, NULL,
323 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
328 static struct greybus_host_driver es1_driver = {
329 .hd_priv_size = sizeof(struct es1_ap_dev),
330 .message_send = message_send,
331 .message_cancel = message_cancel,
332 .latency_tag_enable = latency_tag_enable,
333 .latency_tag_disable = latency_tag_disable,
336 /* Common function to report consistent warnings based on URB status */
337 static int check_urb_status(struct urb *urb)
339 struct device *dev = &urb->dev->dev;
340 int status = urb->status;
347 dev_err(dev, "%s: overflow actual length is %d\n",
348 __func__, urb->actual_length);
354 /* device is gone, stop sending */
357 dev_err(dev, "%s: unknown status %d\n", __func__, status);
362 static void ap_disconnect(struct usb_interface *interface)
364 struct es1_ap_dev *es1;
365 struct usb_device *udev;
368 es1 = usb_get_intfdata(interface);
372 usb_log_disable(es1);
374 /* Tear down everything! */
375 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
376 struct urb *urb = es1->cport_out_urb[i];
382 es1->cport_out_urb[i] = NULL;
383 es1->cport_out_urb_busy[i] = false; /* just to be anal */
386 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
387 struct urb *urb = es1->cport_in_urb[i];
393 kfree(es1->cport_in_buffer[i]);
394 es1->cport_in_buffer[i] = NULL;
397 usb_set_intfdata(interface, NULL);
399 greybus_remove_hd(es1->hd);
404 static void cport_in_callback(struct urb *urb)
406 struct greybus_host_device *hd = urb->context;
407 struct device *dev = &urb->dev->dev;
408 struct gb_operation_msg_hdr *header;
409 int status = check_urb_status(urb);
414 if ((status == -EAGAIN) || (status == -EPROTO))
416 dev_err(dev, "urb cport in error %d (dropped)\n", status);
420 if (urb->actual_length < sizeof(*header)) {
421 dev_err(dev, "short message received\n");
425 /* Extract the CPort id, which is packed in the message header */
426 header = urb->transfer_buffer;
427 cport_id = gb_message_cport_unpack(header);
429 if (cport_id_valid(hd, cport_id)) {
430 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
431 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
434 dev_err(dev, "invalid cport id 0x%02x received\n", cport_id);
437 /* put our urb back in the request pool */
438 retval = usb_submit_urb(urb, GFP_ATOMIC);
440 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
443 static void cport_out_callback(struct urb *urb)
445 struct gb_message *message = urb->context;
446 struct greybus_host_device *hd = message->operation->connection->hd;
447 struct es1_ap_dev *es1 = hd_to_es1(hd);
448 int status = check_urb_status(urb);
451 gb_message_cport_clear(message->header);
453 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
454 message->hcpriv = NULL;
455 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
458 * Tell the submitter that the message send (attempt) is
459 * complete, and report the status.
461 greybus_message_sent(hd, message, status);
466 #define APB1_LOG_MSG_SIZE 64
467 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
471 /* SVC messages go down our control pipe */
473 retval = usb_control_msg(es1->usb_dev,
474 usb_rcvctrlpipe(es1->usb_dev, 0),
476 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
482 kfifo_in(&apb1_log_fifo, buf, retval);
483 } while (retval > 0);
486 static int apb1_log_poll(void *data)
488 struct es1_ap_dev *es1 = data;
491 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
495 while (!kthread_should_stop()) {
497 apb1_log_get(es1, buf);
505 static ssize_t apb1_log_read(struct file *f, char __user *buf,
506 size_t count, loff_t *ppos)
512 if (count > APB1_LOG_SIZE)
513 count = APB1_LOG_SIZE;
515 tmp_buf = kmalloc(count, GFP_KERNEL);
519 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
520 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
527 static const struct file_operations apb1_log_fops = {
528 .read = apb1_log_read,
531 static void usb_log_enable(struct es1_ap_dev *es1)
533 if (!IS_ERR_OR_NULL(apb1_log_task))
536 /* get log from APB1 */
537 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
538 if (IS_ERR(apb1_log_task))
540 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
541 gb_debugfs_get(), NULL,
545 static void usb_log_disable(struct es1_ap_dev *es1)
547 if (IS_ERR_OR_NULL(apb1_log_task))
550 debugfs_remove(apb1_log_dentry);
551 apb1_log_dentry = NULL;
553 kthread_stop(apb1_log_task);
554 apb1_log_task = NULL;
557 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
558 size_t count, loff_t *ppos)
561 int enable = !IS_ERR_OR_NULL(apb1_log_task);
563 sprintf(tmp_buf, "%d\n", enable);
564 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
567 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
568 size_t count, loff_t *ppos)
572 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
574 retval = kstrtoint_from_user(buf, count, 10, &enable);
581 usb_log_disable(es1);
586 static const struct file_operations apb1_log_enable_fops = {
587 .read = apb1_log_enable_read,
588 .write = apb1_log_enable_write,
592 * The ES1 USB Bridge device contains 4 endpoints
593 * 1 Control - usual USB stuff + AP -> SVC messages
594 * 1 Interrupt IN - SVC -> AP messages
595 * 1 Bulk IN - CPort data in
596 * 1 Bulk OUT - CPort data out
598 static int ap_probe(struct usb_interface *interface,
599 const struct usb_device_id *id)
601 struct es1_ap_dev *es1;
602 struct greybus_host_device *hd;
603 struct usb_device *udev;
604 struct usb_host_interface *iface_desc;
605 struct usb_endpoint_descriptor *endpoint;
606 bool bulk_in_found = false;
607 bool bulk_out_found = false;
608 int retval = -ENOMEM;
611 /* We need to fit a CPort ID in one byte of a message header */
612 BUILD_BUG_ON(CPORT_COUNT > U8_MAX + 1);
614 udev = usb_get_dev(interface_to_usbdev(interface));
616 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX,
625 es1->usb_intf = interface;
627 spin_lock_init(&es1->cport_out_urb_lock);
628 usb_set_intfdata(interface, es1);
630 /* find all 3 of our endpoints */
631 iface_desc = interface->cur_altsetting;
632 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
633 endpoint = &iface_desc->endpoint[i].desc;
635 if (usb_endpoint_is_bulk_in(endpoint)) {
636 es1->cport_in_endpoint = endpoint->bEndpointAddress;
637 bulk_in_found = true;
638 } else if (usb_endpoint_is_bulk_out(endpoint)) {
639 es1->cport_out_endpoint = endpoint->bEndpointAddress;
640 bulk_out_found = true;
643 "Unknown endpoint type found, address %x\n",
644 endpoint->bEndpointAddress);
647 if ((bulk_in_found == false) ||
648 (bulk_out_found == false)) {
649 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
653 /* Allocate buffers for our cport in messages and start them up */
654 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
658 urb = usb_alloc_urb(0, GFP_KERNEL);
661 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
665 usb_fill_bulk_urb(urb, udev,
666 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
667 buffer, ES1_GBUF_MSG_SIZE_MAX,
668 cport_in_callback, hd);
669 es1->cport_in_urb[i] = urb;
670 es1->cport_in_buffer[i] = buffer;
671 retval = usb_submit_urb(urb, GFP_KERNEL);
676 /* Allocate urbs for our CPort OUT messages */
677 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
680 urb = usb_alloc_urb(0, GFP_KERNEL);
684 es1->cport_out_urb[i] = urb;
685 es1->cport_out_urb_busy[i] = false; /* just to be anal */
688 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
690 gb_debugfs_get(), es1,
691 &apb1_log_enable_fops);
694 ap_disconnect(interface);
699 static struct usb_driver es1_ap_driver = {
700 .name = "es1_ap_driver",
702 .disconnect = ap_disconnect,
703 .id_table = id_table,
706 module_usb_driver(es1_ap_driver);
708 MODULE_LICENSE("GPL v2");
709 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");