2 * Greybus "AP" USB driver
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/kthread.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/sizes.h>
15 #include <linux/usb.h>
16 #include <linux/kfifo.h>
17 #include <linux/debugfs.h>
18 #include <linux/uaccess.h>
19 #include <asm/unaligned.h>
23 #include "kernel_ver.h"
25 /* Memory sizes for the buffers sent to/from the ES1 controller */
26 #define ES1_SVC_MSG_SIZE (sizeof(struct svc_msg) + SZ_64K)
27 #define ES1_GBUF_MSG_SIZE_MAX PAGE_SIZE
29 static const struct usb_device_id id_table[] = {
30 /* Made up numbers for the SVC USB Bridge in ES1 */
31 { USB_DEVICE(0xffff, 0x0001) },
34 MODULE_DEVICE_TABLE(usb, id_table);
36 #define APB1_LOG_SIZE SZ_16K
37 static struct dentry *apb1_log_dentry;
38 static struct dentry *apb1_log_enable_dentry;
39 static struct task_struct *apb1_log_task;
40 static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE);
43 * Number of CPort IN urbs in flight at any point in time.
44 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
47 #define NUM_CPORT_IN_URB 4
49 /* Number of CPort OUT urbs in flight at any point in time.
50 * Adjust if we get messages saying we are out of urbs in the system log.
52 #define NUM_CPORT_OUT_URB 8
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 * @svc_endpoint: endpoint for SVC data in
61 * @cport_in_endpoint: bulk in endpoint for CPort data
62 * @cport-out_endpoint: bulk out endpoint for CPort data
63 * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
64 * @svc_urb: urb for SVC messages coming in on @svc_endpoint
65 * @cport_in_urb: array of urbs for the CPort in messages
66 * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
67 * @cport_out_urb: array of urbs for the CPort out messages
68 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
70 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
73 struct usb_device *usb_dev;
74 struct usb_interface *usb_intf;
75 struct greybus_host_device *hd;
77 __u8 control_endpoint;
79 __u8 cport_in_endpoint;
80 __u8 cport_out_endpoint;
85 struct urb *cport_in_urb[NUM_CPORT_IN_URB];
86 u8 *cport_in_buffer[NUM_CPORT_IN_URB];
87 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
88 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
89 spinlock_t cport_out_urb_lock;
92 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
94 return (struct es1_ap_dev *)&hd->hd_priv;
97 static void cport_out_callback(struct urb *urb);
98 static void usb_log_enable(struct es1_ap_dev *es1);
99 static void usb_log_disable(struct es1_ap_dev *es1);
102 * Buffer constraints for the host driver.
104 * A "buffer" is used to hold data to be transferred for Greybus by
105 * the host driver. A buffer is represented by a "buffer pointer",
106 * which defines a region of memory used by the host driver for
107 * transferring the data. When Greybus allocates a buffer, it must
108 * do so subject to the constraints associated with the host driver.
110 * size_max: The maximum size of a buffer
112 static void hd_buffer_constraints(struct greybus_host_device *hd)
114 hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
117 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
118 static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
120 struct es1_ap_dev *es1 = hd_to_es1(hd);
123 /* SVC messages go down our control pipe */
124 retval = usb_control_msg(es1->usb_dev,
125 usb_sndctrlpipe(es1->usb_dev,
126 es1->control_endpoint),
127 0x01, /* vendor request AP message */
128 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
133 if (retval != sizeof(*svc_msg))
139 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
141 struct urb *urb = NULL;
145 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
147 /* Look in our pool of allocated urbs first, as that's the "fastest" */
148 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
149 if (es1->cport_out_urb_busy[i] == false) {
150 es1->cport_out_urb_busy[i] = true;
151 urb = es1->cport_out_urb[i];
155 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
160 * Crap, pool is empty, complain to the syslog and go allocate one
161 * dynamically as we have to succeed.
163 dev_err(&es1->usb_dev->dev,
164 "No free CPort OUT urbs, having to dynamically allocate one!\n");
165 return usb_alloc_urb(0, gfp_mask);
168 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
173 * See if this was an urb in our pool, if so mark it "free", otherwise
174 * we need to free it ourselves.
176 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
177 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
178 if (urb == es1->cport_out_urb[i]) {
179 es1->cport_out_urb_busy[i] = false;
184 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
186 /* If urb is not NULL, then we need to free this urb */
191 * Returns an opaque cookie value if successful, or a pointer coded
192 * error otherwise. If the caller wishes to cancel the in-flight
193 * buffer, it must supply the returned cookie to the cancel routine.
195 static void *message_send(struct greybus_host_device *hd, u16 cport_id,
196 struct gb_message *message, gfp_t gfp_mask)
198 struct es1_ap_dev *es1 = hd_to_es1(hd);
199 struct usb_device *udev = es1->usb_dev;
205 buffer = message->buffer;
206 buffer_size = sizeof(*message->header) + message->payload_size;
209 * The data actually transferred will include an indication
210 * of where the data should be sent. Do one last check of
211 * the target CPort id before filling it in.
213 if (cport_id == CPORT_ID_BAD) {
214 pr_err("request to send inbound data buffer\n");
215 return ERR_PTR(-EINVAL);
218 /* Find a free urb */
219 urb = next_free_urb(es1, gfp_mask);
221 return ERR_PTR(-ENOMEM);
224 * We (ab)use the operation-message header pad bytes to transfer the
225 * cport id in order to minimise overhead.
227 put_unaligned_le16(cport_id, message->header->pad);
229 usb_fill_bulk_urb(urb, udev,
230 usb_sndbulkpipe(udev, es1->cport_out_endpoint),
232 cport_out_callback, message);
233 retval = usb_submit_urb(urb, gfp_mask);
235 pr_err("error %d submitting URB\n", retval);
237 put_unaligned_le16(0, message->header->pad);
238 return ERR_PTR(retval);
245 * The cookie value supplied is the value that message_send()
246 * returned to its caller. It identifies the message that should be
247 * canceled. This function must also handle (which is to say,
248 * ignore) a null cookie value.
250 static void message_cancel(void *cookie)
254 * We really should be defensive and track all outstanding
255 * (sent) messages rather than trusting the cookie provided
256 * is valid. For the time being, this will do.
259 usb_kill_urb(cookie);
262 static struct greybus_host_driver es1_driver = {
263 .hd_priv_size = sizeof(struct es1_ap_dev),
264 .message_send = message_send,
265 .message_cancel = message_cancel,
266 .submit_svc = submit_svc,
269 /* Common function to report consistent warnings based on URB status */
270 static int check_urb_status(struct urb *urb)
272 struct device *dev = &urb->dev->dev;
273 int status = urb->status;
280 dev_err(dev, "%s: overflow actual length is %d\n",
281 __func__, urb->actual_length);
287 /* device is gone, stop sending */
290 dev_err(dev, "%s: unknown status %d\n", __func__, status);
295 static void ap_disconnect(struct usb_interface *interface)
297 struct es1_ap_dev *es1;
298 struct usb_device *udev;
301 es1 = usb_get_intfdata(interface);
305 usb_log_disable(es1);
307 /* Tear down everything! */
308 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
309 struct urb *urb = es1->cport_out_urb[i];
315 es1->cport_out_urb[i] = NULL;
316 es1->cport_out_urb_busy[i] = false; /* just to be anal */
319 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
320 struct urb *urb = es1->cport_in_urb[i];
326 kfree(es1->cport_in_buffer[i]);
327 es1->cport_in_buffer[i] = NULL;
330 usb_kill_urb(es1->svc_urb);
331 usb_free_urb(es1->svc_urb);
333 kfree(es1->svc_buffer);
334 es1->svc_buffer = NULL;
336 usb_set_intfdata(interface, NULL);
338 greybus_remove_hd(es1->hd);
343 /* Callback for when we get a SVC message */
344 static void svc_in_callback(struct urb *urb)
346 struct greybus_host_device *hd = urb->context;
347 struct device *dev = &urb->dev->dev;
348 int status = check_urb_status(urb);
352 if ((status == -EAGAIN) || (status == -EPROTO))
354 dev_err(dev, "urb svc in error %d (dropped)\n", status);
358 /* We have a message, create a new message structure, add it to the
359 * list, and wake up our thread that will process the messages.
361 greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
364 /* resubmit the urb to get more messages */
365 retval = usb_submit_urb(urb, GFP_ATOMIC);
367 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
370 static void cport_in_callback(struct urb *urb)
372 struct greybus_host_device *hd = urb->context;
373 struct device *dev = &urb->dev->dev;
374 struct gb_operation_msg_hdr *header;
375 int status = check_urb_status(urb);
380 if ((status == -EAGAIN) || (status == -EPROTO))
382 dev_err(dev, "urb cport in error %d (dropped)\n", status);
386 if (urb->actual_length < sizeof(*header)) {
387 dev_err(dev, "%s: short message received\n", __func__);
391 header = urb->transfer_buffer;
392 cport_id = get_unaligned_le16(header->pad);
393 put_unaligned_le16(0, header->pad);
395 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
398 /* put our urb back in the request pool */
399 retval = usb_submit_urb(urb, GFP_ATOMIC);
401 dev_err(dev, "%s: error %d in submitting urb.\n",
405 static void cport_out_callback(struct urb *urb)
407 struct gb_message *message = urb->context;
408 struct greybus_host_device *hd = message->operation->connection->hd;
409 struct es1_ap_dev *es1 = hd_to_es1(hd);
410 int status = check_urb_status(urb);
412 /* Clear the pad bytes used for the cport id */
413 put_unaligned_le16(0, message->header->pad);
416 * Tell the submitter that the message send (attempt) is
417 * complete, and report the status.
419 greybus_message_sent(hd, message, status);
424 #define APB1_LOG_MSG_SIZE 64
425 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
429 /* SVC messages go down our control pipe */
431 retval = usb_control_msg(es1->usb_dev,
432 usb_rcvctrlpipe(es1->usb_dev,
433 es1->control_endpoint),
434 0x02, /* vendor request APB1 log */
435 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
441 kfifo_in(&apb1_log_fifo, buf, retval);
442 } while (retval > 0);
445 static int apb1_log_poll(void *data)
447 struct es1_ap_dev *es1 = data;
450 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
454 while (!kthread_should_stop()) {
456 apb1_log_get(es1, buf);
464 static ssize_t apb1_log_read(struct file *f, char __user *buf,
465 size_t count, loff_t *ppos)
471 if (count > APB1_LOG_SIZE)
472 count = APB1_LOG_SIZE;
474 tmp_buf = kmalloc(count, GFP_KERNEL);
478 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
479 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
486 static const struct file_operations apb1_log_fops = {
487 .read = apb1_log_read,
490 static void usb_log_enable(struct es1_ap_dev *es1)
492 if (!IS_ERR_OR_NULL(apb1_log_task))
495 /* get log from APB1 */
496 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
497 if (IS_ERR(apb1_log_task))
499 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
500 gb_debugfs_get(), NULL,
504 static void usb_log_disable(struct es1_ap_dev *es1)
506 if (IS_ERR_OR_NULL(apb1_log_task))
509 debugfs_remove(apb1_log_dentry);
510 apb1_log_dentry = NULL;
512 kthread_stop(apb1_log_task);
513 apb1_log_task = NULL;
516 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
517 size_t count, loff_t *ppos)
520 int enable = !IS_ERR_OR_NULL(apb1_log_task);
522 sprintf(tmp_buf, "%d\n", enable);
523 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
526 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
527 size_t count, loff_t *ppos)
531 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
533 retval = kstrtoint_from_user(buf, count, 10, &enable);
540 usb_log_disable(es1);
545 static const struct file_operations apb1_log_enable_fops = {
546 .read = apb1_log_enable_read,
547 .write = apb1_log_enable_write,
551 * The ES1 USB Bridge device contains 4 endpoints
552 * 1 Control - usual USB stuff + AP -> SVC messages
553 * 1 Interrupt IN - SVC -> AP messages
554 * 1 Bulk IN - CPort data in
555 * 1 Bulk OUT - CPort data out
557 static int ap_probe(struct usb_interface *interface,
558 const struct usb_device_id *id)
560 struct es1_ap_dev *es1;
561 struct greybus_host_device *hd;
562 struct usb_device *udev;
563 struct usb_host_interface *iface_desc;
564 struct usb_endpoint_descriptor *endpoint;
565 bool int_in_found = false;
566 bool bulk_in_found = false;
567 bool bulk_out_found = false;
568 int retval = -ENOMEM;
572 udev = usb_get_dev(interface_to_usbdev(interface));
574 hd = greybus_create_hd(&es1_driver, &udev->dev);
580 /* Fill in the buffer allocation constraints */
581 hd_buffer_constraints(hd);
585 es1->usb_intf = interface;
587 spin_lock_init(&es1->cport_out_urb_lock);
588 usb_set_intfdata(interface, es1);
590 /* Control endpoint is the pipe to talk to this AP, so save it off */
591 endpoint = &udev->ep0.desc;
592 es1->control_endpoint = endpoint->bEndpointAddress;
594 /* find all 3 of our endpoints */
595 iface_desc = interface->cur_altsetting;
596 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
597 endpoint = &iface_desc->endpoint[i].desc;
599 if (usb_endpoint_is_int_in(endpoint)) {
600 es1->svc_endpoint = endpoint->bEndpointAddress;
601 svc_interval = endpoint->bInterval;
603 } else if (usb_endpoint_is_bulk_in(endpoint)) {
604 es1->cport_in_endpoint = endpoint->bEndpointAddress;
605 bulk_in_found = true;
606 } else if (usb_endpoint_is_bulk_out(endpoint)) {
607 es1->cport_out_endpoint = endpoint->bEndpointAddress;
608 bulk_out_found = true;
611 "Unknown endpoint type found, address %x\n",
612 endpoint->bEndpointAddress);
615 if ((int_in_found == false) ||
616 (bulk_in_found == false) ||
617 (bulk_out_found == false)) {
618 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
622 /* Create our buffer and URB to get SVC messages, and start it up */
623 es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
624 if (!es1->svc_buffer)
627 es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
631 usb_fill_int_urb(es1->svc_urb, udev,
632 usb_rcvintpipe(udev, es1->svc_endpoint),
633 es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
636 /* Allocate buffers for our cport in messages and start them up */
637 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
641 urb = usb_alloc_urb(0, GFP_KERNEL);
644 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
648 usb_fill_bulk_urb(urb, udev,
649 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
650 buffer, ES1_GBUF_MSG_SIZE_MAX,
651 cport_in_callback, hd);
652 es1->cport_in_urb[i] = urb;
653 es1->cport_in_buffer[i] = buffer;
654 retval = usb_submit_urb(urb, GFP_KERNEL);
659 /* Allocate urbs for our CPort OUT messages */
660 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
663 urb = usb_alloc_urb(0, GFP_KERNEL);
667 es1->cport_out_urb[i] = urb;
668 es1->cport_out_urb_busy[i] = false; /* just to be anal */
671 /* Start up our svc urb, which allows events to start flowing */
672 retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
676 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
678 gb_debugfs_get(), es1,
679 &apb1_log_enable_fops);
683 ap_disconnect(interface);
688 static struct usb_driver es1_ap_driver = {
689 .name = "es1_ap_driver",
691 .disconnect = ap_disconnect,
692 .id_table = id_table,
695 module_usb_driver(es1_ap_driver);
697 MODULE_LICENSE("GPL v2");
698 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");