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>
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 ES2 */
25 { USB_DEVICE(0xffff, 0x0002) },
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);
36 /* Number of bulk in and bulk out couple */
40 * Number of CPort IN urbs in flight at any point in time.
41 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
44 #define NUM_CPORT_IN_URB 4
46 /* Number of CPort OUT urbs in flight at any point in time.
47 * Adjust if we get messages saying we are out of urbs in the system log.
49 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
51 /* vendor request AP message */
52 #define REQUEST_SVC 0x01
54 /* vendor request APB1 log */
55 #define REQUEST_LOG 0x02
57 /* vendor request to map a cport to bulk in and bulk out endpoints */
58 #define REQUEST_EP_MAPPING 0x03
60 /* vendor request to get the number of cports available */
61 #define REQUEST_CPORT_COUNT 0x04
64 * @endpoint: bulk in endpoint for CPort data
65 * @urb: array of urbs for the CPort in messages
66 * @buffer: array of buffers for the @cport_in_urb urbs
70 struct urb *urb[NUM_CPORT_IN_URB];
71 u8 *buffer[NUM_CPORT_IN_URB];
75 * @endpoint: bulk out endpoint for CPort data
77 struct es1_cport_out {
82 * es1_ap_dev - ES1 USB Bridge to AP structure
83 * @usb_dev: pointer to the USB device we are.
84 * @usb_intf: pointer to the USB interface we are bound to.
85 * @hd: pointer to our greybus_host_device structure
87 * @cport_in: endpoint, urbs and buffer for cport in messages
88 * @cport_out: endpoint for for cport out messages
89 * @cport_out_urb: array of urbs for the CPort out messages
90 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
92 * @cport_out_urb_cancelled: array of flags indicating whether the
93 * corresponding @cport_out_urb is being cancelled
94 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
97 struct usb_device *usb_dev;
98 struct usb_interface *usb_intf;
99 struct greybus_host_device *hd;
101 struct es1_cport_in cport_in[NUM_BULKS];
102 struct es1_cport_out cport_out[NUM_BULKS];
103 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
104 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
105 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
106 spinlock_t cport_out_urb_lock;
117 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
119 return (struct es1_ap_dev *)&hd->hd_priv;
122 static void cport_out_callback(struct urb *urb);
123 static void usb_log_enable(struct es1_ap_dev *es1);
124 static void usb_log_disable(struct es1_ap_dev *es1);
126 static int cport_to_ep_pair(struct es1_ap_dev *es1, u16 cport_id)
128 if (cport_id >= es1->hd->num_cports)
130 return es1->cport_to_ep[cport_id];
133 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
135 static int ep_pair_in_use(struct es1_ap_dev *es1, int ep_pair)
139 for (i = 0; i < es1->hd->num_cports; i++) {
140 if (es1->cport_to_ep[i] == ep_pair)
146 static int map_cport_to_ep(struct es1_ap_dev *es1,
147 u16 cport_id, int ep_pair)
150 struct cport_to_ep *cport_to_ep;
152 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
154 if (cport_id >= es1->hd->num_cports)
156 if (ep_pair && ep_pair_in_use(es1, ep_pair))
159 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
163 es1->cport_to_ep[cport_id] = ep_pair;
164 cport_to_ep->cport_id = cpu_to_le16(cport_id);
165 cport_to_ep->endpoint_in = es1->cport_in[ep_pair].endpoint;
166 cport_to_ep->endpoint_out = es1->cport_out[ep_pair].endpoint;
168 retval = usb_control_msg(es1->usb_dev,
169 usb_sndctrlpipe(es1->usb_dev, 0),
171 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
174 sizeof(*cport_to_ep),
176 if (retval == sizeof(*cport_to_ep))
183 static int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
185 return map_cport_to_ep(es1, cport_id, 0);
188 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
190 struct urb *urb = NULL;
194 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
196 /* Look in our pool of allocated urbs first, as that's the "fastest" */
197 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
198 if (es1->cport_out_urb_busy[i] == false &&
199 es1->cport_out_urb_cancelled[i] == false) {
200 es1->cport_out_urb_busy[i] = true;
201 urb = es1->cport_out_urb[i];
205 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
210 * Crap, pool is empty, complain to the syslog and go allocate one
211 * dynamically as we have to succeed.
213 dev_err(&es1->usb_dev->dev,
214 "No free CPort OUT urbs, having to dynamically allocate one!\n");
215 return usb_alloc_urb(0, gfp_mask);
218 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
223 * See if this was an urb in our pool, if so mark it "free", otherwise
224 * we need to free it ourselves.
226 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
227 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
228 if (urb == es1->cport_out_urb[i]) {
229 es1->cport_out_urb_busy[i] = false;
234 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
236 /* If urb is not NULL, then we need to free this urb */
241 * We (ab)use the operation-message header pad bytes to transfer the
242 * cport id in order to minimise overhead.
245 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
247 header->pad[0] = cport_id;
250 /* Clear the pad bytes used for the CPort id */
251 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
256 /* Extract the CPort id packed into the header, and clear it */
257 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
259 u16 cport_id = header->pad[0];
261 gb_message_cport_clear(header);
267 * Returns zero if the message was successfully queued, or a negative errno
270 static int message_send(struct greybus_host_device *hd, u16 cport_id,
271 struct gb_message *message, gfp_t gfp_mask)
273 struct es1_ap_dev *es1 = hd_to_es1(hd);
274 struct usb_device *udev = es1->usb_dev;
282 * The data actually transferred will include an indication
283 * of where the data should be sent. Do one last check of
284 * the target CPort id before filling it in.
286 if (!cport_id_valid(hd, cport_id)) {
287 pr_err("invalid destination cport 0x%02x\n", cport_id);
291 /* Find a free urb */
292 urb = next_free_urb(es1, gfp_mask);
296 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
297 message->hcpriv = urb;
298 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
300 /* Pack the cport id into the message header */
301 gb_message_cport_pack(message->header, cport_id);
303 buffer_size = sizeof(*message->header) + message->payload_size;
305 ep_pair = cport_to_ep_pair(es1, cport_id);
306 usb_fill_bulk_urb(urb, udev,
307 usb_sndbulkpipe(udev,
308 es1->cport_out[ep_pair].endpoint),
309 message->buffer, buffer_size,
310 cport_out_callback, message);
311 urb->transfer_flags |= URB_ZERO_PACKET;
312 gb_connection_push_timestamp(message->operation->connection);
313 retval = usb_submit_urb(urb, gfp_mask);
315 pr_err("error %d submitting URB\n", retval);
317 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
318 message->hcpriv = NULL;
319 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
322 gb_message_cport_clear(message->header);
331 * Can not be called in atomic context.
333 static void message_cancel(struct gb_message *message)
335 struct greybus_host_device *hd = message->operation->connection->hd;
336 struct es1_ap_dev *es1 = hd_to_es1(hd);
342 spin_lock_irq(&es1->cport_out_urb_lock);
343 urb = message->hcpriv;
345 /* Prevent dynamically allocated urb from being deallocated. */
348 /* Prevent pre-allocated urb from being reused. */
349 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
350 if (urb == es1->cport_out_urb[i]) {
351 es1->cport_out_urb_cancelled[i] = true;
355 spin_unlock_irq(&es1->cport_out_urb_lock);
359 if (i < NUM_CPORT_OUT_URB) {
360 spin_lock_irq(&es1->cport_out_urb_lock);
361 es1->cport_out_urb_cancelled[i] = false;
362 spin_unlock_irq(&es1->cport_out_urb_lock);
368 static struct greybus_host_driver es1_driver = {
369 .hd_priv_size = sizeof(struct es1_ap_dev),
370 .message_send = message_send,
371 .message_cancel = message_cancel,
374 /* Common function to report consistent warnings based on URB status */
375 static int check_urb_status(struct urb *urb)
377 struct device *dev = &urb->dev->dev;
378 int status = urb->status;
385 dev_err(dev, "%s: overflow actual length is %d\n",
386 __func__, urb->actual_length);
392 /* device is gone, stop sending */
395 dev_err(dev, "%s: unknown status %d\n", __func__, status);
400 static void ap_disconnect(struct usb_interface *interface)
402 struct es1_ap_dev *es1;
403 struct usb_device *udev;
407 es1 = usb_get_intfdata(interface);
411 usb_log_disable(es1);
413 /* Tear down everything! */
414 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
415 struct urb *urb = es1->cport_out_urb[i];
421 es1->cport_out_urb[i] = NULL;
422 es1->cport_out_urb_busy[i] = false; /* just to be anal */
425 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
426 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
427 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
428 struct urb *urb = cport_in->urb[i];
434 kfree(cport_in->buffer[i]);
435 cport_in->buffer[i] = NULL;
439 usb_set_intfdata(interface, NULL);
441 greybus_remove_hd(es1->hd);
442 kfree(es1->cport_to_ep);
447 static void cport_in_callback(struct urb *urb)
449 struct greybus_host_device *hd = urb->context;
450 struct device *dev = &urb->dev->dev;
451 struct gb_operation_msg_hdr *header;
452 int status = check_urb_status(urb);
457 if ((status == -EAGAIN) || (status == -EPROTO))
459 dev_err(dev, "urb cport in error %d (dropped)\n", status);
463 if (urb->actual_length < sizeof(*header)) {
464 dev_err(dev, "%s: short message received\n", __func__);
468 /* Extract the CPort id, which is packed in the message header */
469 header = urb->transfer_buffer;
470 cport_id = gb_message_cport_unpack(header);
472 if (cport_id_valid(hd, cport_id))
473 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
476 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
479 /* put our urb back in the request pool */
480 retval = usb_submit_urb(urb, GFP_ATOMIC);
482 dev_err(dev, "%s: error %d in submitting urb.\n",
486 static void cport_out_callback(struct urb *urb)
488 struct gb_message *message = urb->context;
489 struct greybus_host_device *hd = message->operation->connection->hd;
490 struct es1_ap_dev *es1 = hd_to_es1(hd);
491 int status = check_urb_status(urb);
494 gb_message_cport_clear(message->header);
497 * Tell the submitter that the message send (attempt) is
498 * complete, and report the status.
500 greybus_message_sent(hd, message, status);
502 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
503 message->hcpriv = NULL;
504 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
509 #define APB1_LOG_MSG_SIZE 64
510 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
514 /* SVC messages go down our control pipe */
516 retval = usb_control_msg(es1->usb_dev,
517 usb_rcvctrlpipe(es1->usb_dev, 0),
519 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
525 kfifo_in(&apb1_log_fifo, buf, retval);
526 } while (retval > 0);
529 static int apb1_log_poll(void *data)
531 struct es1_ap_dev *es1 = data;
534 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
538 while (!kthread_should_stop()) {
540 apb1_log_get(es1, buf);
548 static ssize_t apb1_log_read(struct file *f, char __user *buf,
549 size_t count, loff_t *ppos)
555 if (count > APB1_LOG_SIZE)
556 count = APB1_LOG_SIZE;
558 tmp_buf = kmalloc(count, GFP_KERNEL);
562 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
563 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
570 static const struct file_operations apb1_log_fops = {
571 .read = apb1_log_read,
574 static void usb_log_enable(struct es1_ap_dev *es1)
576 if (!IS_ERR_OR_NULL(apb1_log_task))
579 /* get log from APB1 */
580 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
581 if (IS_ERR(apb1_log_task))
583 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
584 gb_debugfs_get(), NULL,
588 static void usb_log_disable(struct es1_ap_dev *es1)
590 if (IS_ERR_OR_NULL(apb1_log_task))
593 debugfs_remove(apb1_log_dentry);
594 apb1_log_dentry = NULL;
596 kthread_stop(apb1_log_task);
597 apb1_log_task = NULL;
600 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
601 size_t count, loff_t *ppos)
604 int enable = !IS_ERR_OR_NULL(apb1_log_task);
606 sprintf(tmp_buf, "%d\n", enable);
607 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
610 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
611 size_t count, loff_t *ppos)
615 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
617 retval = kstrtoint_from_user(buf, count, 10, &enable);
624 usb_log_disable(es1);
629 static const struct file_operations apb1_log_enable_fops = {
630 .read = apb1_log_enable_read,
631 .write = apb1_log_enable_write,
634 static int apb1_get_cport_count(struct usb_device *udev)
639 cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
643 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
645 USB_DIR_IN | USB_TYPE_VENDOR |
646 USB_RECIP_INTERFACE, 0, 0, cport_count,
647 sizeof(*cport_count), ES1_TIMEOUT);
649 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
654 retval = le16_to_cpu(*cport_count);
656 /* We need to fit a CPort ID in one byte of a message header */
657 if (retval > U8_MAX) {
659 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
668 * The ES1 USB Bridge device contains 4 endpoints
669 * 1 Control - usual USB stuff + AP -> SVC messages
670 * 1 Interrupt IN - SVC -> AP messages
671 * 1 Bulk IN - CPort data in
672 * 1 Bulk OUT - CPort data out
674 static int ap_probe(struct usb_interface *interface,
675 const struct usb_device_id *id)
677 struct es1_ap_dev *es1;
678 struct greybus_host_device *hd;
679 struct usb_device *udev;
680 struct usb_host_interface *iface_desc;
681 struct usb_endpoint_descriptor *endpoint;
684 int retval = -ENOMEM;
688 udev = usb_get_dev(interface_to_usbdev(interface));
690 num_cports = apb1_get_cport_count(udev);
691 if (num_cports < 0) {
693 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
698 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX,
707 es1->usb_intf = interface;
709 spin_lock_init(&es1->cport_out_urb_lock);
710 usb_set_intfdata(interface, es1);
712 es1->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es1->cport_to_ep),
714 if (!es1->cport_to_ep) {
719 /* find all 3 of our endpoints */
720 iface_desc = interface->cur_altsetting;
721 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
722 endpoint = &iface_desc->endpoint[i].desc;
724 if (usb_endpoint_is_bulk_in(endpoint)) {
725 es1->cport_in[bulk_in++].endpoint =
726 endpoint->bEndpointAddress;
727 } else if (usb_endpoint_is_bulk_out(endpoint)) {
728 es1->cport_out[bulk_out++].endpoint =
729 endpoint->bEndpointAddress;
732 "Unknown endpoint type found, address %x\n",
733 endpoint->bEndpointAddress);
736 if ((bulk_in == 0) ||
738 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
742 /* Allocate buffers for our cport in messages and start them up */
743 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
744 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
745 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
749 urb = usb_alloc_urb(0, GFP_KERNEL);
752 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
756 usb_fill_bulk_urb(urb, udev,
757 usb_rcvbulkpipe(udev,
759 buffer, ES1_GBUF_MSG_SIZE_MAX,
760 cport_in_callback, hd);
761 cport_in->urb[i] = urb;
762 cport_in->buffer[i] = buffer;
763 retval = usb_submit_urb(urb, GFP_KERNEL);
769 /* Allocate urbs for our CPort OUT messages */
770 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
773 urb = usb_alloc_urb(0, GFP_KERNEL);
777 es1->cport_out_urb[i] = urb;
778 es1->cport_out_urb_busy[i] = false; /* just to be anal */
781 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
783 gb_debugfs_get(), es1,
784 &apb1_log_enable_fops);
787 ap_disconnect(interface);
792 static struct usb_driver es1_ap_driver = {
793 .name = "es2_ap_driver",
795 .disconnect = ap_disconnect,
796 .id_table = id_table,
799 module_usb_driver(es1_ap_driver);
801 MODULE_LICENSE("GPL v2");
802 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");