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"
20 /* Memory sizes for the buffers sent to/from the ES1 controller */
21 #define ES1_SVC_MSG_SIZE (sizeof(struct svc_msg) + SZ_64K)
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 ES2 */
26 { USB_DEVICE(0xffff, 0x0002) },
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 cport present on USB bridge */
40 /* Number of bulk in and bulk out couple */
44 * Number of CPort IN urbs in flight at any point in time.
45 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
48 #define NUM_CPORT_IN_URB 4
50 /* Number of CPort OUT urbs in flight at any point in time.
51 * Adjust if we get messages saying we are out of urbs in the system log.
53 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
55 /* vendor request AP message */
56 #define REQUEST_SVC 0x01
58 /* vendor request APB1 log */
59 #define REQUEST_LOG 0x02
61 /* vendor request to map a cport to bulk in and bulk out endpoints */
62 #define REQUEST_EP_MAPPING 0x03
65 * @endpoint: bulk in endpoint for CPort data
66 * @urb: array of urbs for the CPort in messages
67 * @buffer: array of buffers for the @cport_in_urb urbs
71 struct urb *urb[NUM_CPORT_IN_URB];
72 u8 *buffer[NUM_CPORT_IN_URB];
76 * @endpoint: bulk out endpoint for CPort data
78 struct es1_cport_out {
83 * es1_ap_dev - ES1 USB Bridge to AP structure
84 * @usb_dev: pointer to the USB device we are.
85 * @usb_intf: pointer to the USB interface we are bound to.
86 * @hd: pointer to our greybus_host_device structure
87 * @control_endpoint: endpoint to send data to SVC
88 * @svc_endpoint: endpoint for SVC data in
90 * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
91 * @svc_urb: urb for SVC messages coming in on @svc_endpoint
92 * @cport_in: endpoint, urbs and buffer for cport in messages
93 * @cport_out: endpoint for for cport out messages
94 * @cport_out_urb: array of urbs for the CPort out messages
95 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
97 * @cport_out_urb_cancelled: array of flags indicating whether the
98 * corresponding @cport_out_urb is being cancelled
99 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
102 struct usb_device *usb_dev;
103 struct usb_interface *usb_intf;
104 struct greybus_host_device *hd;
106 __u8 control_endpoint;
112 struct es1_cport_in cport_in[NUM_BULKS];
113 struct es1_cport_out cport_out[NUM_BULKS];
114 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
115 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
116 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
117 spinlock_t cport_out_urb_lock;
119 int cport_to_ep[CPORT_MAX];
128 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
130 return (struct es1_ap_dev *)&hd->hd_priv;
133 static void cport_out_callback(struct urb *urb);
134 static void usb_log_enable(struct es1_ap_dev *es1);
135 static void usb_log_disable(struct es1_ap_dev *es1);
137 static int cport_to_ep(struct es1_ap_dev *es1, u16 cport_id)
139 if (cport_id >= CPORT_MAX)
141 return es1->cport_to_ep[cport_id];
144 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
145 static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
147 struct es1_ap_dev *es1 = hd_to_es1(hd);
150 /* SVC messages go down our control pipe */
151 retval = usb_control_msg(es1->usb_dev,
152 usb_sndctrlpipe(es1->usb_dev,
153 es1->control_endpoint),
155 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
160 if (retval != sizeof(*svc_msg))
166 static int ep_in_use(struct es1_ap_dev *es1, int bulk_ep_set)
170 for (i = 0; i < CPORT_MAX; i++) {
171 if (es1->cport_to_ep[i] == bulk_ep_set)
177 int map_cport_to_ep(struct es1_ap_dev *es1,
178 u16 cport_id, int bulk_ep_set)
181 struct cport_to_ep *cport_to_ep;
183 if (bulk_ep_set == 0 || bulk_ep_set >= NUM_BULKS)
185 if (cport_id >= CPORT_MAX)
187 if (bulk_ep_set && ep_in_use(es1, bulk_ep_set))
190 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
194 es1->cport_to_ep[cport_id] = bulk_ep_set;
195 cport_to_ep->cport_id = cpu_to_le16(cport_id);
196 cport_to_ep->endpoint_in = es1->cport_in[bulk_ep_set].endpoint;
197 cport_to_ep->endpoint_out = es1->cport_out[bulk_ep_set].endpoint;
199 retval = usb_control_msg(es1->usb_dev,
200 usb_sndctrlpipe(es1->usb_dev,
201 es1->control_endpoint),
203 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
206 sizeof(*cport_to_ep),
208 if (retval == sizeof(*cport_to_ep))
215 int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
217 return map_cport_to_ep(es1, cport_id, 0);
220 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
222 struct urb *urb = NULL;
226 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
228 /* Look in our pool of allocated urbs first, as that's the "fastest" */
229 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
230 if (es1->cport_out_urb_busy[i] == false &&
231 es1->cport_out_urb_cancelled[i] == false) {
232 es1->cport_out_urb_busy[i] = true;
233 urb = es1->cport_out_urb[i];
237 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
242 * Crap, pool is empty, complain to the syslog and go allocate one
243 * dynamically as we have to succeed.
245 dev_err(&es1->usb_dev->dev,
246 "No free CPort OUT urbs, having to dynamically allocate one!\n");
247 return usb_alloc_urb(0, gfp_mask);
250 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
255 * See if this was an urb in our pool, if so mark it "free", otherwise
256 * we need to free it ourselves.
258 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
259 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
260 if (urb == es1->cport_out_urb[i]) {
261 es1->cport_out_urb_busy[i] = false;
266 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
268 /* If urb is not NULL, then we need to free this urb */
273 * We (ab)use the operation-message header pad bytes to transfer the
274 * cport id in order to minimise overhead.
277 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
279 header->pad[0] = cport_id;
282 /* Clear the pad bytes used for the CPort id */
283 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
288 /* Extract the CPort id packed into the header, and clear it */
289 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
291 u16 cport_id = header->pad[0];
293 gb_message_cport_clear(header);
299 * Returns zero if the message was successfully queued, or a negative errno
302 static int message_send(struct greybus_host_device *hd, u16 cport_id,
303 struct gb_message *message, gfp_t gfp_mask)
305 struct es1_ap_dev *es1 = hd_to_es1(hd);
306 struct usb_device *udev = es1->usb_dev;
314 * The data actually transferred will include an indication
315 * of where the data should be sent. Do one last check of
316 * the target CPort id before filling it in.
318 if (!cport_id_valid(cport_id)) {
319 pr_err("invalid destination cport 0x%02x\n", cport_id);
323 /* Find a free urb */
324 urb = next_free_urb(es1, gfp_mask);
328 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
329 message->hcpriv = urb;
330 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
332 /* Pack the cport id into the message header */
333 gb_message_cport_pack(message->header, cport_id);
335 buffer_size = sizeof(*message->header) + message->payload_size;
337 bulk_ep_set = cport_to_ep(es1, cport_id);
338 usb_fill_bulk_urb(urb, udev,
339 usb_sndbulkpipe(udev,
340 es1->cport_out[bulk_ep_set].endpoint),
341 message->buffer, buffer_size,
342 cport_out_callback, message);
343 retval = usb_submit_urb(urb, gfp_mask);
345 pr_err("error %d submitting URB\n", retval);
347 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
348 message->hcpriv = NULL;
349 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
352 gb_message_cport_clear(message->header);
361 * Can not be called in atomic context.
363 static void message_cancel(struct gb_message *message)
365 struct greybus_host_device *hd = message->operation->connection->hd;
366 struct es1_ap_dev *es1 = hd_to_es1(hd);
372 spin_lock_irq(&es1->cport_out_urb_lock);
373 urb = message->hcpriv;
375 /* Prevent dynamically allocated urb from being deallocated. */
378 /* Prevent pre-allocated urb from being reused. */
379 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
380 if (urb == es1->cport_out_urb[i]) {
381 es1->cport_out_urb_cancelled[i] = true;
385 spin_unlock_irq(&es1->cport_out_urb_lock);
389 if (i < NUM_CPORT_OUT_URB) {
390 spin_lock_irq(&es1->cport_out_urb_lock);
391 es1->cport_out_urb_cancelled[i] = false;
392 spin_unlock_irq(&es1->cport_out_urb_lock);
398 static struct greybus_host_driver es1_driver = {
399 .hd_priv_size = sizeof(struct es1_ap_dev),
400 .message_send = message_send,
401 .message_cancel = message_cancel,
402 .submit_svc = submit_svc,
405 /* Common function to report consistent warnings based on URB status */
406 static int check_urb_status(struct urb *urb)
408 struct device *dev = &urb->dev->dev;
409 int status = urb->status;
416 dev_err(dev, "%s: overflow actual length is %d\n",
417 __func__, urb->actual_length);
423 /* device is gone, stop sending */
426 dev_err(dev, "%s: unknown status %d\n", __func__, status);
431 static void ap_disconnect(struct usb_interface *interface)
433 struct es1_ap_dev *es1;
434 struct usb_device *udev;
438 es1 = usb_get_intfdata(interface);
442 usb_log_disable(es1);
444 /* Tear down everything! */
445 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
446 struct urb *urb = es1->cport_out_urb[i];
452 es1->cport_out_urb[i] = NULL;
453 es1->cport_out_urb_busy[i] = false; /* just to be anal */
456 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
457 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
458 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
459 struct urb *urb = cport_in->urb[i];
465 kfree(cport_in->buffer[i]);
466 cport_in->buffer[i] = NULL;
470 usb_kill_urb(es1->svc_urb);
471 usb_free_urb(es1->svc_urb);
473 kfree(es1->svc_buffer);
474 es1->svc_buffer = NULL;
476 usb_set_intfdata(interface, NULL);
478 greybus_remove_hd(es1->hd);
483 /* Callback for when we get a SVC message */
484 static void svc_in_callback(struct urb *urb)
486 struct greybus_host_device *hd = urb->context;
487 struct device *dev = &urb->dev->dev;
488 int status = check_urb_status(urb);
492 if ((status == -EAGAIN) || (status == -EPROTO))
494 dev_err(dev, "urb svc in error %d (dropped)\n", status);
498 /* We have a message, create a new message structure, add it to the
499 * list, and wake up our thread that will process the messages.
501 greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
504 /* resubmit the urb to get more messages */
505 retval = usb_submit_urb(urb, GFP_ATOMIC);
507 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
510 static void cport_in_callback(struct urb *urb)
512 struct greybus_host_device *hd = urb->context;
513 struct device *dev = &urb->dev->dev;
514 struct gb_operation_msg_hdr *header;
515 int status = check_urb_status(urb);
520 if ((status == -EAGAIN) || (status == -EPROTO))
522 dev_err(dev, "urb cport in error %d (dropped)\n", status);
526 if (urb->actual_length < sizeof(*header)) {
527 dev_err(dev, "%s: short message received\n", __func__);
531 /* Extract the CPort id, which is packed in the message header */
532 header = urb->transfer_buffer;
533 cport_id = gb_message_cport_unpack(header);
535 if (cport_id_valid(cport_id))
536 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
539 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
542 /* put our urb back in the request pool */
543 retval = usb_submit_urb(urb, GFP_ATOMIC);
545 dev_err(dev, "%s: error %d in submitting urb.\n",
549 static void cport_out_callback(struct urb *urb)
551 struct gb_message *message = urb->context;
552 struct greybus_host_device *hd = message->operation->connection->hd;
553 struct es1_ap_dev *es1 = hd_to_es1(hd);
554 int status = check_urb_status(urb);
557 gb_message_cport_clear(message->header);
560 * Tell the submitter that the message send (attempt) is
561 * complete, and report the status.
563 greybus_message_sent(hd, message, status);
565 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
566 message->hcpriv = NULL;
567 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
572 #define APB1_LOG_MSG_SIZE 64
573 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
577 /* SVC messages go down our control pipe */
579 retval = usb_control_msg(es1->usb_dev,
580 usb_rcvctrlpipe(es1->usb_dev,
581 es1->control_endpoint),
583 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
589 kfifo_in(&apb1_log_fifo, buf, retval);
590 } while (retval > 0);
593 static int apb1_log_poll(void *data)
595 struct es1_ap_dev *es1 = data;
598 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
602 while (!kthread_should_stop()) {
604 apb1_log_get(es1, buf);
612 static ssize_t apb1_log_read(struct file *f, char __user *buf,
613 size_t count, loff_t *ppos)
619 if (count > APB1_LOG_SIZE)
620 count = APB1_LOG_SIZE;
622 tmp_buf = kmalloc(count, GFP_KERNEL);
626 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
627 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
634 static const struct file_operations apb1_log_fops = {
635 .read = apb1_log_read,
638 static void usb_log_enable(struct es1_ap_dev *es1)
640 if (!IS_ERR_OR_NULL(apb1_log_task))
643 /* get log from APB1 */
644 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
645 if (IS_ERR(apb1_log_task))
647 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
648 gb_debugfs_get(), NULL,
652 static void usb_log_disable(struct es1_ap_dev *es1)
654 if (IS_ERR_OR_NULL(apb1_log_task))
657 debugfs_remove(apb1_log_dentry);
658 apb1_log_dentry = NULL;
660 kthread_stop(apb1_log_task);
661 apb1_log_task = NULL;
664 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
665 size_t count, loff_t *ppos)
668 int enable = !IS_ERR_OR_NULL(apb1_log_task);
670 sprintf(tmp_buf, "%d\n", enable);
671 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
674 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
675 size_t count, loff_t *ppos)
679 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
681 retval = kstrtoint_from_user(buf, count, 10, &enable);
688 usb_log_disable(es1);
693 static const struct file_operations apb1_log_enable_fops = {
694 .read = apb1_log_enable_read,
695 .write = apb1_log_enable_write,
699 * The ES1 USB Bridge device contains 4 endpoints
700 * 1 Control - usual USB stuff + AP -> SVC messages
701 * 1 Interrupt IN - SVC -> AP messages
702 * 1 Bulk IN - CPort data in
703 * 1 Bulk OUT - CPort data out
705 static int ap_probe(struct usb_interface *interface,
706 const struct usb_device_id *id)
708 struct es1_ap_dev *es1;
709 struct greybus_host_device *hd;
710 struct usb_device *udev;
711 struct usb_host_interface *iface_desc;
712 struct usb_endpoint_descriptor *endpoint;
713 bool int_in_found = false;
716 int retval = -ENOMEM;
720 /* We need to fit a CPort ID in one byte of a message header */
721 BUILD_BUG_ON(CPORT_ID_MAX > U8_MAX);
723 udev = usb_get_dev(interface_to_usbdev(interface));
725 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX);
733 es1->usb_intf = interface;
735 spin_lock_init(&es1->cport_out_urb_lock);
736 usb_set_intfdata(interface, es1);
738 /* Control endpoint is the pipe to talk to this AP, so save it off */
739 endpoint = &udev->ep0.desc;
740 es1->control_endpoint = endpoint->bEndpointAddress;
742 /* find all 3 of our endpoints */
743 iface_desc = interface->cur_altsetting;
744 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
745 endpoint = &iface_desc->endpoint[i].desc;
747 if (usb_endpoint_is_int_in(endpoint)) {
748 es1->svc_endpoint = endpoint->bEndpointAddress;
749 svc_interval = endpoint->bInterval;
751 } else if (usb_endpoint_is_bulk_in(endpoint)) {
752 es1->cport_in[bulk_in++].endpoint =
753 endpoint->bEndpointAddress;
754 } else if (usb_endpoint_is_bulk_out(endpoint)) {
755 es1->cport_out[bulk_out++].endpoint =
756 endpoint->bEndpointAddress;
759 "Unknown endpoint type found, address %x\n",
760 endpoint->bEndpointAddress);
763 if ((int_in_found == false) ||
766 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
770 /* Create our buffer and URB to get SVC messages, and start it up */
771 es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
772 if (!es1->svc_buffer)
775 es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
779 usb_fill_int_urb(es1->svc_urb, udev,
780 usb_rcvintpipe(udev, es1->svc_endpoint),
781 es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
784 /* Allocate buffers for our cport in messages and start them up */
785 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
786 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
787 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
791 urb = usb_alloc_urb(0, GFP_KERNEL);
794 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
798 usb_fill_bulk_urb(urb, udev,
799 usb_rcvbulkpipe(udev,
801 buffer, ES1_GBUF_MSG_SIZE_MAX,
802 cport_in_callback, hd);
803 cport_in->urb[i] = urb;
804 cport_in->buffer[i] = buffer;
805 retval = usb_submit_urb(urb, GFP_KERNEL);
811 /* Allocate urbs for our CPort OUT messages */
812 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
815 urb = usb_alloc_urb(0, GFP_KERNEL);
819 es1->cport_out_urb[i] = urb;
820 es1->cport_out_urb_busy[i] = false; /* just to be anal */
823 /* Initialize AP's greybus interface */
824 if (!gb_ap_svc_connection_create(hd)) {
829 /* Start up our svc urb, which allows events to start flowing */
830 retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
834 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
836 gb_debugfs_get(), es1,
837 &apb1_log_enable_fops);
840 ap_disconnect(interface);
845 static struct usb_driver es1_ap_driver = {
846 .name = "es2_ap_driver",
848 .disconnect = ap_disconnect,
849 .id_table = id_table,
852 module_usb_driver(es1_ap_driver);
854 MODULE_LICENSE("GPL v2");
855 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");