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"
19 /* Memory sizes for the buffers sent to/from the ES1 controller */
20 #define ES1_GBUF_MSG_SIZE_MAX 2048
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) },
27 MODULE_DEVICE_TABLE(usb, id_table);
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);
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
40 #define NUM_CPORT_IN_URB 4
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.
45 #define NUM_CPORT_OUT_URB 8
47 /* vendor request AP message */
48 #define REQUEST_SVC 0x01
50 /* vendor request APB1 log */
51 #define REQUEST_LOG 0x02
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
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"
71 struct usb_device *usb_dev;
72 struct usb_interface *usb_intf;
73 struct greybus_host_device *hd;
75 __u8 control_endpoint;
76 __u8 cport_in_endpoint;
77 __u8 cport_out_endpoint;
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;
87 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
89 return (struct es1_ap_dev *)&hd->hd_priv;
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);
96 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
98 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
100 struct urb *urb = NULL;
104 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
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];
115 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
120 * Crap, pool is empty, complain to the syslog and go allocate one
121 * dynamically as we have to succeed.
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);
128 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
133 * See if this was an urb in our pool, if so mark it "free", otherwise
134 * we need to free it ourselves.
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;
144 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
146 /* If urb is not NULL, then we need to free this urb */
151 * We (ab)use the operation-message header pad bytes to transfer the
152 * cport id in order to minimise overhead.
155 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
157 header->pad[0] = cport_id;
160 /* Clear the pad bytes used for the CPort id */
161 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
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)
169 u16 cport_id = header->pad[0];
171 gb_message_cport_clear(header);
177 * Returns zero if the message was successfully queued, or a negative errno
180 static int message_send(struct greybus_host_device *hd, u16 cport_id,
181 struct gb_message *message, gfp_t gfp_mask)
183 struct es1_ap_dev *es1 = hd_to_es1(hd);
184 struct usb_device *udev = es1->usb_dev;
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.
195 if (!cport_id_valid(cport_id)) {
196 pr_err("invalid destination cport 0x%02x\n", cport_id);
200 /* Find a free urb */
201 urb = next_free_urb(es1, gfp_mask);
205 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
206 message->hcpriv = urb;
207 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
209 /* Pack the cport id into the message header */
210 gb_message_cport_pack(message->header, cport_id);
212 buffer_size = sizeof(*message->header) + message->payload_size;
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);
220 pr_err("error %d submitting URB\n", retval);
222 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
223 message->hcpriv = NULL;
224 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
227 gb_message_cport_clear(message->header);
236 * Can not be called in atomic context.
238 static void message_cancel(struct gb_message *message)
240 struct greybus_host_device *hd = message->operation->connection->hd;
241 struct es1_ap_dev *es1 = hd_to_es1(hd);
247 spin_lock_irq(&es1->cport_out_urb_lock);
248 urb = message->hcpriv;
250 /* Prevent dynamically allocated urb from being deallocated. */
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;
260 spin_unlock_irq(&es1->cport_out_urb_lock);
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);
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,
279 /* Common function to report consistent warnings based on URB status */
280 static int check_urb_status(struct urb *urb)
282 struct device *dev = &urb->dev->dev;
283 int status = urb->status;
290 dev_err(dev, "%s: overflow actual length is %d\n",
291 __func__, urb->actual_length);
297 /* device is gone, stop sending */
300 dev_err(dev, "%s: unknown status %d\n", __func__, status);
305 static void ap_disconnect(struct usb_interface *interface)
307 struct es1_ap_dev *es1;
308 struct usb_device *udev;
311 es1 = usb_get_intfdata(interface);
315 usb_log_disable(es1);
317 /* Tear down everything! */
318 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
319 struct urb *urb = es1->cport_out_urb[i];
325 es1->cport_out_urb[i] = NULL;
326 es1->cport_out_urb_busy[i] = false; /* just to be anal */
329 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
330 struct urb *urb = es1->cport_in_urb[i];
336 kfree(es1->cport_in_buffer[i]);
337 es1->cport_in_buffer[i] = NULL;
340 usb_set_intfdata(interface, NULL);
342 greybus_remove_hd(es1->hd);
347 static void cport_in_callback(struct urb *urb)
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);
357 if ((status == -EAGAIN) || (status == -EPROTO))
359 dev_err(dev, "urb cport in error %d (dropped)\n", status);
363 if (urb->actual_length < sizeof(*header)) {
364 dev_err(dev, "%s: short message received\n", __func__);
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);
372 if (cport_id_valid(cport_id))
373 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
376 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
379 /* put our urb back in the request pool */
380 retval = usb_submit_urb(urb, GFP_ATOMIC);
382 dev_err(dev, "%s: error %d in submitting urb.\n",
386 static void cport_out_callback(struct urb *urb)
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);
394 gb_message_cport_clear(message->header);
397 * Tell the submitter that the message send (attempt) is
398 * complete, and report the status.
400 greybus_message_sent(hd, message, status);
402 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
403 message->hcpriv = NULL;
404 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
409 #define APB1_LOG_MSG_SIZE 64
410 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
414 /* SVC messages go down our control pipe */
416 retval = usb_control_msg(es1->usb_dev,
417 usb_rcvctrlpipe(es1->usb_dev,
418 es1->control_endpoint),
420 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
426 kfifo_in(&apb1_log_fifo, buf, retval);
427 } while (retval > 0);
430 static int apb1_log_poll(void *data)
432 struct es1_ap_dev *es1 = data;
435 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
439 while (!kthread_should_stop()) {
441 apb1_log_get(es1, buf);
449 static ssize_t apb1_log_read(struct file *f, char __user *buf,
450 size_t count, loff_t *ppos)
456 if (count > APB1_LOG_SIZE)
457 count = APB1_LOG_SIZE;
459 tmp_buf = kmalloc(count, GFP_KERNEL);
463 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
464 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
471 static const struct file_operations apb1_log_fops = {
472 .read = apb1_log_read,
475 static void usb_log_enable(struct es1_ap_dev *es1)
477 if (!IS_ERR_OR_NULL(apb1_log_task))
480 /* get log from APB1 */
481 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
482 if (IS_ERR(apb1_log_task))
484 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
485 gb_debugfs_get(), NULL,
489 static void usb_log_disable(struct es1_ap_dev *es1)
491 if (IS_ERR_OR_NULL(apb1_log_task))
494 debugfs_remove(apb1_log_dentry);
495 apb1_log_dentry = NULL;
497 kthread_stop(apb1_log_task);
498 apb1_log_task = NULL;
501 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
502 size_t count, loff_t *ppos)
505 int enable = !IS_ERR_OR_NULL(apb1_log_task);
507 sprintf(tmp_buf, "%d\n", enable);
508 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
511 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
512 size_t count, loff_t *ppos)
516 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
518 retval = kstrtoint_from_user(buf, count, 10, &enable);
525 usb_log_disable(es1);
530 static const struct file_operations apb1_log_enable_fops = {
531 .read = apb1_log_enable_read,
532 .write = apb1_log_enable_write,
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
542 static int ap_probe(struct usb_interface *interface,
543 const struct usb_device_id *id)
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;
555 /* We need to fit a CPort ID in one byte of a message header */
556 BUILD_BUG_ON(CPORT_ID_MAX > U8_MAX);
558 udev = usb_get_dev(interface_to_usbdev(interface));
560 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX);
566 /* Initialize AP's greybus interface */
567 if (!gb_ap_svc_connection_create(hd)) {
574 es1->usb_intf = interface;
576 spin_lock_init(&es1->cport_out_urb_lock);
577 usb_set_intfdata(interface, es1);
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;
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;
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;
596 "Unknown endpoint type found, address %x\n",
597 endpoint->bEndpointAddress);
600 if ((bulk_in_found == false) ||
601 (bulk_out_found == false)) {
602 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
606 /* Allocate buffers for our cport in messages and start them up */
607 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
611 urb = usb_alloc_urb(0, GFP_KERNEL);
614 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
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);
629 /* Allocate urbs for our CPort OUT messages */
630 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
633 urb = usb_alloc_urb(0, GFP_KERNEL);
637 es1->cport_out_urb[i] = urb;
638 es1->cport_out_urb_busy[i] = false; /* just to be anal */
641 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
643 gb_debugfs_get(), es1,
644 &apb1_log_enable_fops);
647 ap_disconnect(interface);
652 static struct usb_driver es1_ap_driver = {
653 .name = "es1_ap_driver",
655 .disconnect = ap_disconnect,
656 .id_table = id_table,
659 module_usb_driver(es1_ap_driver);
661 MODULE_LICENSE("GPL v2");
662 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");