2 * Greybus "AP" USB driver for "ES2" 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>
18 #include "kernel_ver.h"
19 #include "connection.h"
21 /* Memory sizes for the buffers sent to/from the ES1 controller */
22 #define ES1_SVC_MSG_SIZE (sizeof(struct svc_msg) + SZ_64K)
23 #define ES1_GBUF_MSG_SIZE_MAX 2048
25 static const struct usb_device_id id_table[] = {
26 /* Made up numbers for the SVC USB Bridge in ES2 */
27 { USB_DEVICE(0xffff, 0x0002) },
30 MODULE_DEVICE_TABLE(usb, id_table);
32 #define APB1_LOG_SIZE SZ_16K
33 static struct dentry *apb1_log_dentry;
34 static struct dentry *apb1_log_enable_dentry;
35 static struct task_struct *apb1_log_task;
36 static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE);
38 /* Number of cport present on USB bridge */
41 /* Number of bulk in and bulk out couple */
45 * Number of CPort IN urbs in flight at any point in time.
46 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
49 #define NUM_CPORT_IN_URB 4
51 /* Number of CPort OUT urbs in flight at any point in time.
52 * Adjust if we get messages saying we are out of urbs in the system log.
54 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
56 /* vendor request AP message */
57 #define REQUEST_SVC 0x01
59 /* vendor request APB1 log */
60 #define REQUEST_LOG 0x02
62 /* vendor request to map a cport to bulk in and bulk out endpoints */
63 #define REQUEST_EP_MAPPING 0x03
66 * @endpoint: bulk in endpoint for CPort data
67 * @urb: array of urbs for the CPort in messages
68 * @buffer: array of buffers for the @cport_in_urb urbs
72 struct urb *urb[NUM_CPORT_IN_URB];
73 u8 *buffer[NUM_CPORT_IN_URB];
77 * @endpoint: bulk out endpoint for CPort data
79 struct es1_cport_out {
84 * es1_ap_dev - ES1 USB Bridge to AP structure
85 * @usb_dev: pointer to the USB device we are.
86 * @usb_intf: pointer to the USB interface we are bound to.
87 * @hd: pointer to our greybus_host_device structure
88 * @control_endpoint: endpoint to send data to SVC
89 * @svc_endpoint: endpoint for SVC data in
91 * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
92 * @svc_urb: urb for SVC messages coming in on @svc_endpoint
93 * @cport_in: endpoint, urbs and buffer for cport in messages
94 * @cport_out: endpoint for for cport out messages
95 * @cport_out_urb: array of urbs for the CPort out messages
96 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
98 * @cport_out_urb_cancelled: array of flags indicating whether the
99 * corresponding @cport_out_urb is being cancelled
100 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
103 struct usb_device *usb_dev;
104 struct usb_interface *usb_intf;
105 struct greybus_host_device *hd;
107 __u8 control_endpoint;
113 struct es1_cport_in cport_in[NUM_BULKS];
114 struct es1_cport_out cport_out[NUM_BULKS];
115 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
116 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
117 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
118 spinlock_t cport_out_urb_lock;
120 int cport_to_ep[CPORT_MAX];
129 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
131 return (struct es1_ap_dev *)&hd->hd_priv;
134 static void cport_out_callback(struct urb *urb);
135 static void usb_log_enable(struct es1_ap_dev *es1);
136 static void usb_log_disable(struct es1_ap_dev *es1);
138 static int cport_to_ep(struct es1_ap_dev *es1, u16 cport_id)
140 if (cport_id >= CPORT_MAX)
142 return es1->cport_to_ep[cport_id];
145 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
146 static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
148 struct es1_ap_dev *es1 = hd_to_es1(hd);
151 /* SVC messages go down our control pipe */
152 retval = usb_control_msg(es1->usb_dev,
153 usb_sndctrlpipe(es1->usb_dev,
154 es1->control_endpoint),
156 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
161 if (retval != sizeof(*svc_msg))
167 static int ep_in_use(struct es1_ap_dev *es1, int bulk_ep_set)
171 for (i = 0; i < CPORT_MAX; i++) {
172 if (es1->cport_to_ep[i] == bulk_ep_set)
178 int map_cport_to_ep(struct es1_ap_dev *es1,
179 u16 cport_id, int bulk_ep_set)
182 struct cport_to_ep *cport_to_ep;
184 if (bulk_ep_set == 0 || bulk_ep_set >= NUM_BULKS)
186 if (cport_id >= CPORT_MAX)
188 if (bulk_ep_set && ep_in_use(es1, bulk_ep_set))
191 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
195 es1->cport_to_ep[cport_id] = bulk_ep_set;
196 cport_to_ep->cport_id = cpu_to_le16(cport_id);
197 cport_to_ep->endpoint_in = es1->cport_in[bulk_ep_set].endpoint;
198 cport_to_ep->endpoint_out = es1->cport_out[bulk_ep_set].endpoint;
200 retval = usb_control_msg(es1->usb_dev,
201 usb_sndctrlpipe(es1->usb_dev,
202 es1->control_endpoint),
204 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
207 sizeof(*cport_to_ep),
209 if (retval == sizeof(*cport_to_ep))
216 int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
218 return map_cport_to_ep(es1, cport_id, 0);
221 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
223 struct urb *urb = NULL;
227 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
229 /* Look in our pool of allocated urbs first, as that's the "fastest" */
230 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
231 if (es1->cport_out_urb_busy[i] == false &&
232 es1->cport_out_urb_cancelled[i] == false) {
233 es1->cport_out_urb_busy[i] = true;
234 urb = es1->cport_out_urb[i];
238 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
243 * Crap, pool is empty, complain to the syslog and go allocate one
244 * dynamically as we have to succeed.
246 dev_err(&es1->usb_dev->dev,
247 "No free CPort OUT urbs, having to dynamically allocate one!\n");
248 return usb_alloc_urb(0, gfp_mask);
251 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
256 * See if this was an urb in our pool, if so mark it "free", otherwise
257 * we need to free it ourselves.
259 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
260 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
261 if (urb == es1->cport_out_urb[i]) {
262 es1->cport_out_urb_busy[i] = false;
267 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
269 /* If urb is not NULL, then we need to free this urb */
274 * We (ab)use the operation-message header pad bytes to transfer the
275 * cport id in order to minimise overhead.
278 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
280 header->pad[0] = cport_id;
283 /* Clear the pad bytes used for the CPort id */
284 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
289 /* Extract the CPort id packed into the header, and clear it */
290 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
292 u16 cport_id = header->pad[0];
294 gb_message_cport_clear(header);
300 * Returns zero if the message was successfully queued, or a negative errno
303 static int message_send(struct greybus_host_device *hd, u16 cport_id,
304 struct gb_message *message, gfp_t gfp_mask)
306 struct es1_ap_dev *es1 = hd_to_es1(hd);
307 struct usb_device *udev = es1->usb_dev;
315 * The data actually transferred will include an indication
316 * of where the data should be sent. Do one last check of
317 * the target CPort id before filling it in.
319 if (!cport_id_valid(cport_id)) {
320 pr_err("invalid destination cport 0x%02x\n", cport_id);
324 /* Find a free urb */
325 urb = next_free_urb(es1, gfp_mask);
329 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
330 message->hcpriv = urb;
331 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
333 /* Pack the cport id into the message header */
334 gb_message_cport_pack(message->header, cport_id);
336 buffer_size = sizeof(*message->header) + message->payload_size;
338 bulk_ep_set = cport_to_ep(es1, cport_id);
339 usb_fill_bulk_urb(urb, udev,
340 usb_sndbulkpipe(udev,
341 es1->cport_out[bulk_ep_set].endpoint),
342 message->buffer, buffer_size,
343 cport_out_callback, message);
344 gb_connection_push_timestamp(message->operation->connection);
345 retval = usb_submit_urb(urb, gfp_mask);
347 pr_err("error %d submitting URB\n", retval);
349 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
350 message->hcpriv = NULL;
351 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
354 gb_message_cport_clear(message->header);
363 * Can not be called in atomic context.
365 static void message_cancel(struct gb_message *message)
367 struct greybus_host_device *hd = message->operation->connection->hd;
368 struct es1_ap_dev *es1 = hd_to_es1(hd);
374 spin_lock_irq(&es1->cport_out_urb_lock);
375 urb = message->hcpriv;
377 /* Prevent dynamically allocated urb from being deallocated. */
380 /* Prevent pre-allocated urb from being reused. */
381 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
382 if (urb == es1->cport_out_urb[i]) {
383 es1->cport_out_urb_cancelled[i] = true;
387 spin_unlock_irq(&es1->cport_out_urb_lock);
391 if (i < NUM_CPORT_OUT_URB) {
392 spin_lock_irq(&es1->cport_out_urb_lock);
393 es1->cport_out_urb_cancelled[i] = false;
394 spin_unlock_irq(&es1->cport_out_urb_lock);
400 static struct greybus_host_driver es1_driver = {
401 .hd_priv_size = sizeof(struct es1_ap_dev),
402 .message_send = message_send,
403 .message_cancel = message_cancel,
404 .submit_svc = submit_svc,
407 /* Common function to report consistent warnings based on URB status */
408 static int check_urb_status(struct urb *urb)
410 struct device *dev = &urb->dev->dev;
411 int status = urb->status;
418 dev_err(dev, "%s: overflow actual length is %d\n",
419 __func__, urb->actual_length);
425 /* device is gone, stop sending */
428 dev_err(dev, "%s: unknown status %d\n", __func__, status);
433 static void ap_disconnect(struct usb_interface *interface)
435 struct es1_ap_dev *es1;
436 struct usb_device *udev;
440 es1 = usb_get_intfdata(interface);
444 usb_log_disable(es1);
446 /* Tear down everything! */
447 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
448 struct urb *urb = es1->cport_out_urb[i];
454 es1->cport_out_urb[i] = NULL;
455 es1->cport_out_urb_busy[i] = false; /* just to be anal */
458 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
459 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
460 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
461 struct urb *urb = cport_in->urb[i];
467 kfree(cport_in->buffer[i]);
468 cport_in->buffer[i] = NULL;
472 usb_kill_urb(es1->svc_urb);
473 usb_free_urb(es1->svc_urb);
475 kfree(es1->svc_buffer);
476 es1->svc_buffer = NULL;
478 usb_set_intfdata(interface, NULL);
480 greybus_remove_hd(es1->hd);
485 /* Callback for when we get a SVC message */
486 static void svc_in_callback(struct urb *urb)
488 struct greybus_host_device *hd = urb->context;
489 struct device *dev = &urb->dev->dev;
490 int status = check_urb_status(urb);
494 if ((status == -EAGAIN) || (status == -EPROTO))
496 dev_err(dev, "urb svc in error %d (dropped)\n", status);
500 /* We have a message, create a new message structure, add it to the
501 * list, and wake up our thread that will process the messages.
503 greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
506 /* resubmit the urb to get more messages */
507 retval = usb_submit_urb(urb, GFP_ATOMIC);
509 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
512 static void cport_in_callback(struct urb *urb)
514 struct greybus_host_device *hd = urb->context;
515 struct device *dev = &urb->dev->dev;
516 struct gb_operation_msg_hdr *header;
517 int status = check_urb_status(urb);
522 if ((status == -EAGAIN) || (status == -EPROTO))
524 dev_err(dev, "urb cport in error %d (dropped)\n", status);
528 if (urb->actual_length < sizeof(*header)) {
529 dev_err(dev, "%s: short message received\n", __func__);
533 /* Extract the CPort id, which is packed in the message header */
534 header = urb->transfer_buffer;
535 cport_id = gb_message_cport_unpack(header);
537 if (cport_id_valid(cport_id))
538 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
541 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
544 /* put our urb back in the request pool */
545 retval = usb_submit_urb(urb, GFP_ATOMIC);
547 dev_err(dev, "%s: error %d in submitting urb.\n",
551 static void cport_out_callback(struct urb *urb)
553 struct gb_message *message = urb->context;
554 struct greybus_host_device *hd = message->operation->connection->hd;
555 struct es1_ap_dev *es1 = hd_to_es1(hd);
556 int status = check_urb_status(urb);
559 gb_message_cport_clear(message->header);
562 * Tell the submitter that the message send (attempt) is
563 * complete, and report the status.
565 greybus_message_sent(hd, message, status);
567 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
568 message->hcpriv = NULL;
569 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
574 #define APB1_LOG_MSG_SIZE 64
575 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
579 /* SVC messages go down our control pipe */
581 retval = usb_control_msg(es1->usb_dev,
582 usb_rcvctrlpipe(es1->usb_dev,
583 es1->control_endpoint),
585 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
591 kfifo_in(&apb1_log_fifo, buf, retval);
592 } while (retval > 0);
595 static int apb1_log_poll(void *data)
597 struct es1_ap_dev *es1 = data;
600 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
604 while (!kthread_should_stop()) {
606 apb1_log_get(es1, buf);
614 static ssize_t apb1_log_read(struct file *f, char __user *buf,
615 size_t count, loff_t *ppos)
621 if (count > APB1_LOG_SIZE)
622 count = APB1_LOG_SIZE;
624 tmp_buf = kmalloc(count, GFP_KERNEL);
628 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
629 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
636 static const struct file_operations apb1_log_fops = {
637 .read = apb1_log_read,
640 static void usb_log_enable(struct es1_ap_dev *es1)
642 if (!IS_ERR_OR_NULL(apb1_log_task))
645 /* get log from APB1 */
646 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
647 if (IS_ERR(apb1_log_task))
649 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
650 gb_debugfs_get(), NULL,
654 static void usb_log_disable(struct es1_ap_dev *es1)
656 if (IS_ERR_OR_NULL(apb1_log_task))
659 debugfs_remove(apb1_log_dentry);
660 apb1_log_dentry = NULL;
662 kthread_stop(apb1_log_task);
663 apb1_log_task = NULL;
666 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
667 size_t count, loff_t *ppos)
670 int enable = !IS_ERR_OR_NULL(apb1_log_task);
672 sprintf(tmp_buf, "%d\n", enable);
673 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
676 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
677 size_t count, loff_t *ppos)
681 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
683 retval = kstrtoint_from_user(buf, count, 10, &enable);
690 usb_log_disable(es1);
695 static const struct file_operations apb1_log_enable_fops = {
696 .read = apb1_log_enable_read,
697 .write = apb1_log_enable_write,
701 * The ES1 USB Bridge device contains 4 endpoints
702 * 1 Control - usual USB stuff + AP -> SVC messages
703 * 1 Interrupt IN - SVC -> AP messages
704 * 1 Bulk IN - CPort data in
705 * 1 Bulk OUT - CPort data out
707 static int ap_probe(struct usb_interface *interface,
708 const struct usb_device_id *id)
710 struct es1_ap_dev *es1;
711 struct greybus_host_device *hd;
712 struct usb_device *udev;
713 struct usb_host_interface *iface_desc;
714 struct usb_endpoint_descriptor *endpoint;
715 bool int_in_found = false;
718 int retval = -ENOMEM;
720 u16 endo_id = 0x4755; // FIXME - get endo "ID" from the SVC
721 u8 ap_intf_id = 0x01; // FIXME - get endo "ID" from the SVC
724 /* We need to fit a CPort ID in one byte of a message header */
725 BUILD_BUG_ON(CPORT_ID_MAX > U8_MAX);
727 udev = usb_get_dev(interface_to_usbdev(interface));
729 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX);
737 es1->usb_intf = interface;
739 spin_lock_init(&es1->cport_out_urb_lock);
740 usb_set_intfdata(interface, es1);
742 /* Control endpoint is the pipe to talk to this AP, so save it off */
743 endpoint = &udev->ep0.desc;
744 es1->control_endpoint = endpoint->bEndpointAddress;
746 /* find all 3 of our endpoints */
747 iface_desc = interface->cur_altsetting;
748 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
749 endpoint = &iface_desc->endpoint[i].desc;
751 if (usb_endpoint_is_int_in(endpoint)) {
752 es1->svc_endpoint = endpoint->bEndpointAddress;
753 svc_interval = endpoint->bInterval;
755 } else if (usb_endpoint_is_bulk_in(endpoint)) {
756 es1->cport_in[bulk_in++].endpoint =
757 endpoint->bEndpointAddress;
758 } else if (usb_endpoint_is_bulk_out(endpoint)) {
759 es1->cport_out[bulk_out++].endpoint =
760 endpoint->bEndpointAddress;
763 "Unknown endpoint type found, address %x\n",
764 endpoint->bEndpointAddress);
767 if ((int_in_found == false) ||
770 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
774 /* Create our buffer and URB to get SVC messages, and start it up */
775 es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
776 if (!es1->svc_buffer)
779 es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
783 usb_fill_int_urb(es1->svc_urb, udev,
784 usb_rcvintpipe(udev, es1->svc_endpoint),
785 es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
788 /* Allocate buffers for our cport in messages and start them up */
789 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
790 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
791 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
795 urb = usb_alloc_urb(0, GFP_KERNEL);
798 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
802 usb_fill_bulk_urb(urb, udev,
803 usb_rcvbulkpipe(udev,
805 buffer, ES1_GBUF_MSG_SIZE_MAX,
806 cport_in_callback, hd);
807 cport_in->urb[i] = urb;
808 cport_in->buffer[i] = buffer;
809 retval = usb_submit_urb(urb, GFP_KERNEL);
815 /* Allocate urbs for our CPort OUT messages */
816 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
819 urb = usb_alloc_urb(0, GFP_KERNEL);
823 es1->cport_out_urb[i] = urb;
824 es1->cport_out_urb_busy[i] = false; /* just to be anal */
828 * XXX Soon this will be initiated later, with a combination
829 * XXX of a Control protocol probe operation and a
830 * XXX subsequent Control protocol connected operation for
831 * XXX the SVC connection. At that point we know we're
832 * XXX properly connected to an Endo.
834 retval = greybus_endo_setup(hd, endo_id, ap_intf_id);
838 /* Start up our svc urb, which allows events to start flowing */
839 retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
843 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
845 gb_debugfs_get(), es1,
846 &apb1_log_enable_fops);
849 ap_disconnect(interface);
854 static struct usb_driver es1_ap_driver = {
855 .name = "es2_ap_driver",
857 .disconnect = ap_disconnect,
858 .id_table = id_table,
861 module_usb_driver(es1_ap_driver);
863 MODULE_LICENSE("GPL v2");
864 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");