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"
20 /* Memory sizes for the buffers sent to/from the ES1 controller */
21 #define ES1_GBUF_MSG_SIZE_MAX 2048
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) },
28 MODULE_DEVICE_TABLE(usb, id_table);
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);
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
41 #define NUM_CPORT_IN_URB 4
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.
46 #define NUM_CPORT_OUT_URB 8
48 /* vendor request AP message */
49 #define REQUEST_SVC 0x01
51 /* vendor request APB1 log */
52 #define REQUEST_LOG 0x02
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
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"
72 struct usb_device *usb_dev;
73 struct usb_interface *usb_intf;
74 struct greybus_host_device *hd;
76 __u8 control_endpoint;
77 __u8 cport_in_endpoint;
78 __u8 cport_out_endpoint;
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;
88 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
90 return (struct es1_ap_dev *)&hd->hd_priv;
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);
97 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
99 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
101 struct urb *urb = NULL;
105 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
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];
116 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
121 * Crap, pool is empty, complain to the syslog and go allocate one
122 * dynamically as we have to succeed.
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);
129 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
134 * See if this was an urb in our pool, if so mark it "free", otherwise
135 * we need to free it ourselves.
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;
145 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
147 /* If urb is not NULL, then we need to free this urb */
152 * We (ab)use the operation-message header pad bytes to transfer the
153 * cport id in order to minimise overhead.
156 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
158 header->pad[0] = cport_id;
161 /* Clear the pad bytes used for the CPort id */
162 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
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)
170 u16 cport_id = header->pad[0];
172 gb_message_cport_clear(header);
178 * Returns zero if the message was successfully queued, or a negative errno
181 static int message_send(struct greybus_host_device *hd, u16 cport_id,
182 struct gb_message *message, gfp_t gfp_mask)
184 struct es1_ap_dev *es1 = hd_to_es1(hd);
185 struct usb_device *udev = es1->usb_dev;
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.
196 if (!cport_id_valid(cport_id)) {
197 pr_err("invalid destination cport 0x%02x\n", cport_id);
201 /* Find a free urb */
202 urb = next_free_urb(es1, gfp_mask);
206 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
207 message->hcpriv = urb;
208 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
210 /* Pack the cport id into the message header */
211 gb_message_cport_pack(message->header, cport_id);
213 buffer_size = sizeof(*message->header) + message->payload_size;
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);
222 pr_err("error %d submitting URB\n", retval);
224 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
225 message->hcpriv = NULL;
226 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
229 gb_message_cport_clear(message->header);
238 * Can not be called in atomic context.
240 static void message_cancel(struct gb_message *message)
242 struct greybus_host_device *hd = message->operation->connection->hd;
243 struct es1_ap_dev *es1 = hd_to_es1(hd);
249 spin_lock_irq(&es1->cport_out_urb_lock);
250 urb = message->hcpriv;
252 /* Prevent dynamically allocated urb from being deallocated. */
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;
262 spin_unlock_irq(&es1->cport_out_urb_lock);
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);
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,
281 /* Common function to report consistent warnings based on URB status */
282 static int check_urb_status(struct urb *urb)
284 struct device *dev = &urb->dev->dev;
285 int status = urb->status;
292 dev_err(dev, "%s: overflow actual length is %d\n",
293 __func__, urb->actual_length);
299 /* device is gone, stop sending */
302 dev_err(dev, "%s: unknown status %d\n", __func__, status);
307 static void ap_disconnect(struct usb_interface *interface)
309 struct es1_ap_dev *es1;
310 struct usb_device *udev;
313 es1 = usb_get_intfdata(interface);
317 usb_log_disable(es1);
319 /* Tear down everything! */
320 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
321 struct urb *urb = es1->cport_out_urb[i];
327 es1->cport_out_urb[i] = NULL;
328 es1->cport_out_urb_busy[i] = false; /* just to be anal */
331 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
332 struct urb *urb = es1->cport_in_urb[i];
338 kfree(es1->cport_in_buffer[i]);
339 es1->cport_in_buffer[i] = NULL;
342 usb_set_intfdata(interface, NULL);
344 greybus_remove_hd(es1->hd);
349 static void cport_in_callback(struct urb *urb)
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);
359 if ((status == -EAGAIN) || (status == -EPROTO))
361 dev_err(dev, "urb cport in error %d (dropped)\n", status);
365 if (urb->actual_length < sizeof(*header)) {
366 dev_err(dev, "%s: short message received\n", __func__);
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);
374 if (cport_id_valid(cport_id))
375 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
378 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
381 /* put our urb back in the request pool */
382 retval = usb_submit_urb(urb, GFP_ATOMIC);
384 dev_err(dev, "%s: error %d in submitting urb.\n",
388 static void cport_out_callback(struct urb *urb)
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);
396 gb_message_cport_clear(message->header);
399 * Tell the submitter that the message send (attempt) is
400 * complete, and report the status.
402 greybus_message_sent(hd, message, status);
404 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
405 message->hcpriv = NULL;
406 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
411 #define APB1_LOG_MSG_SIZE 64
412 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
416 /* SVC messages go down our control pipe */
418 retval = usb_control_msg(es1->usb_dev,
419 usb_rcvctrlpipe(es1->usb_dev,
420 es1->control_endpoint),
422 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
428 kfifo_in(&apb1_log_fifo, buf, retval);
429 } while (retval > 0);
432 static int apb1_log_poll(void *data)
434 struct es1_ap_dev *es1 = data;
437 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
441 while (!kthread_should_stop()) {
443 apb1_log_get(es1, buf);
451 static ssize_t apb1_log_read(struct file *f, char __user *buf,
452 size_t count, loff_t *ppos)
458 if (count > APB1_LOG_SIZE)
459 count = APB1_LOG_SIZE;
461 tmp_buf = kmalloc(count, GFP_KERNEL);
465 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
466 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
473 static const struct file_operations apb1_log_fops = {
474 .read = apb1_log_read,
477 static void usb_log_enable(struct es1_ap_dev *es1)
479 if (!IS_ERR_OR_NULL(apb1_log_task))
482 /* get log from APB1 */
483 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
484 if (IS_ERR(apb1_log_task))
486 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
487 gb_debugfs_get(), NULL,
491 static void usb_log_disable(struct es1_ap_dev *es1)
493 if (IS_ERR_OR_NULL(apb1_log_task))
496 debugfs_remove(apb1_log_dentry);
497 apb1_log_dentry = NULL;
499 kthread_stop(apb1_log_task);
500 apb1_log_task = NULL;
503 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
504 size_t count, loff_t *ppos)
507 int enable = !IS_ERR_OR_NULL(apb1_log_task);
509 sprintf(tmp_buf, "%d\n", enable);
510 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
513 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
514 size_t count, loff_t *ppos)
518 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
520 retval = kstrtoint_from_user(buf, count, 10, &enable);
527 usb_log_disable(es1);
532 static const struct file_operations apb1_log_enable_fops = {
533 .read = apb1_log_enable_read,
534 .write = apb1_log_enable_write,
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
544 static int ap_probe(struct usb_interface *interface,
545 const struct usb_device_id *id)
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;
557 /* We need to fit a CPort ID in one byte of a message header */
558 BUILD_BUG_ON(CPORT_ID_MAX > U8_MAX);
560 udev = usb_get_dev(interface_to_usbdev(interface));
562 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX);
570 es1->usb_intf = interface;
572 spin_lock_init(&es1->cport_out_urb_lock);
573 usb_set_intfdata(interface, es1);
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;
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;
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;
592 "Unknown endpoint type found, address %x\n",
593 endpoint->bEndpointAddress);
596 if ((bulk_in_found == false) ||
597 (bulk_out_found == false)) {
598 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
602 /* Allocate buffers for our cport in messages and start them up */
603 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
607 urb = usb_alloc_urb(0, GFP_KERNEL);
610 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
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);
625 /* Allocate urbs for our CPort OUT messages */
626 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
629 urb = usb_alloc_urb(0, GFP_KERNEL);
633 es1->cport_out_urb[i] = urb;
634 es1->cport_out_urb_busy[i] = false; /* just to be anal */
637 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
639 gb_debugfs_get(), es1,
640 &apb1_log_enable_fops);
643 ap_disconnect(interface);
648 static struct usb_driver es1_ap_driver = {
649 .name = "es1_ap_driver",
651 .disconnect = ap_disconnect,
652 .id_table = id_table,
655 module_usb_driver(es1_ap_driver);
657 MODULE_LICENSE("GPL v2");
658 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");