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 APB1 log */
52 #define REQUEST_LOG 0x02
54 /* vendor request to map a cport to bulk in and bulk out endpoints */
55 #define REQUEST_EP_MAPPING 0x03
57 /* vendor request to get the number of cports available */
58 #define REQUEST_CPORT_COUNT 0x04
61 * @endpoint: bulk in endpoint for CPort data
62 * @urb: array of urbs for the CPort in messages
63 * @buffer: array of buffers for the @cport_in_urb urbs
67 struct urb *urb[NUM_CPORT_IN_URB];
68 u8 *buffer[NUM_CPORT_IN_URB];
72 * @endpoint: bulk out endpoint for CPort data
74 struct es1_cport_out {
79 * es1_ap_dev - ES1 USB Bridge to AP structure
80 * @usb_dev: pointer to the USB device we are.
81 * @usb_intf: pointer to the USB interface we are bound to.
82 * @hd: pointer to our greybus_host_device structure
84 * @cport_in: endpoint, urbs and buffer for cport in messages
85 * @cport_out: endpoint for for cport out messages
86 * @cport_out_urb: array of urbs for the CPort out messages
87 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
89 * @cport_out_urb_cancelled: array of flags indicating whether the
90 * corresponding @cport_out_urb is being cancelled
91 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
94 struct usb_device *usb_dev;
95 struct usb_interface *usb_intf;
96 struct greybus_host_device *hd;
98 struct es1_cport_in cport_in[NUM_BULKS];
99 struct es1_cport_out cport_out[NUM_BULKS];
100 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
101 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
102 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
103 spinlock_t cport_out_urb_lock;
109 * cport_to_ep - information about cport to endpoints mapping
110 * @cport_id: the id of cport to map to endpoints
111 * @endpoint_in: the endpoint number to use for in transfer
112 * @endpoint_out: he endpoint number to use for out transfer
120 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
122 return (struct es1_ap_dev *)&hd->hd_priv;
125 static void cport_out_callback(struct urb *urb);
126 static void usb_log_enable(struct es1_ap_dev *es1);
127 static void usb_log_disable(struct es1_ap_dev *es1);
129 /* Get the endpoints pair mapped to the cport */
130 static int cport_to_ep_pair(struct es1_ap_dev *es1, u16 cport_id)
132 if (cport_id >= es1->hd->num_cports)
134 return es1->cport_to_ep[cport_id];
137 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
139 /* Disable for now until we work all of this out to keep a warning-free build */
141 /* Test if the endpoints pair is already mapped to a cport */
142 static int ep_pair_in_use(struct es1_ap_dev *es1, int ep_pair)
146 for (i = 0; i < es1->hd->num_cports; i++) {
147 if (es1->cport_to_ep[i] == ep_pair)
153 /* Configure the endpoint mapping and send the request to APBridge */
154 static int map_cport_to_ep(struct es1_ap_dev *es1,
155 u16 cport_id, int ep_pair)
158 struct cport_to_ep *cport_to_ep;
160 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
162 if (cport_id >= es1->hd->num_cports)
164 if (ep_pair && ep_pair_in_use(es1, ep_pair))
167 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
171 es1->cport_to_ep[cport_id] = ep_pair;
172 cport_to_ep->cport_id = cpu_to_le16(cport_id);
173 cport_to_ep->endpoint_in = es1->cport_in[ep_pair].endpoint;
174 cport_to_ep->endpoint_out = es1->cport_out[ep_pair].endpoint;
176 retval = usb_control_msg(es1->usb_dev,
177 usb_sndctrlpipe(es1->usb_dev, 0),
179 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
182 sizeof(*cport_to_ep),
184 if (retval == sizeof(*cport_to_ep))
191 /* Unmap a cport: use the muxed endpoints pair */
192 static int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
194 return map_cport_to_ep(es1, cport_id, 0);
198 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
200 struct urb *urb = NULL;
204 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
206 /* Look in our pool of allocated urbs first, as that's the "fastest" */
207 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
208 if (es1->cport_out_urb_busy[i] == false &&
209 es1->cport_out_urb_cancelled[i] == false) {
210 es1->cport_out_urb_busy[i] = true;
211 urb = es1->cport_out_urb[i];
215 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
220 * Crap, pool is empty, complain to the syslog and go allocate one
221 * dynamically as we have to succeed.
223 dev_err(&es1->usb_dev->dev,
224 "No free CPort OUT urbs, having to dynamically allocate one!\n");
225 return usb_alloc_urb(0, gfp_mask);
228 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
233 * See if this was an urb in our pool, if so mark it "free", otherwise
234 * we need to free it ourselves.
236 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
237 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
238 if (urb == es1->cport_out_urb[i]) {
239 es1->cport_out_urb_busy[i] = false;
244 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
246 /* If urb is not NULL, then we need to free this urb */
251 * We (ab)use the operation-message header pad bytes to transfer the
252 * cport id in order to minimise overhead.
255 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
257 header->pad[0] = cport_id;
260 /* Clear the pad bytes used for the CPort id */
261 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
266 /* Extract the CPort id packed into the header, and clear it */
267 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
269 u16 cport_id = header->pad[0];
271 gb_message_cport_clear(header);
277 * Returns zero if the message was successfully queued, or a negative errno
280 static int message_send(struct greybus_host_device *hd, u16 cport_id,
281 struct gb_message *message, gfp_t gfp_mask)
283 struct es1_ap_dev *es1 = hd_to_es1(hd);
284 struct usb_device *udev = es1->usb_dev;
292 * The data actually transferred will include an indication
293 * of where the data should be sent. Do one last check of
294 * the target CPort id before filling it in.
296 if (!cport_id_valid(hd, cport_id)) {
297 pr_err("invalid destination cport 0x%02x\n", cport_id);
301 /* Find a free urb */
302 urb = next_free_urb(es1, gfp_mask);
306 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
307 message->hcpriv = urb;
308 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
310 /* Pack the cport id into the message header */
311 gb_message_cport_pack(message->header, cport_id);
313 buffer_size = sizeof(*message->header) + message->payload_size;
315 ep_pair = cport_to_ep_pair(es1, cport_id);
316 usb_fill_bulk_urb(urb, udev,
317 usb_sndbulkpipe(udev,
318 es1->cport_out[ep_pair].endpoint),
319 message->buffer, buffer_size,
320 cport_out_callback, message);
321 urb->transfer_flags |= URB_ZERO_PACKET;
322 retval = usb_submit_urb(urb, gfp_mask);
324 pr_err("error %d submitting URB\n", retval);
326 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
327 message->hcpriv = NULL;
328 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
331 gb_message_cport_clear(message->header);
340 * Can not be called in atomic context.
342 static void message_cancel(struct gb_message *message)
344 struct greybus_host_device *hd = message->operation->connection->hd;
345 struct es1_ap_dev *es1 = hd_to_es1(hd);
351 spin_lock_irq(&es1->cport_out_urb_lock);
352 urb = message->hcpriv;
354 /* Prevent dynamically allocated urb from being deallocated. */
357 /* Prevent pre-allocated urb from being reused. */
358 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
359 if (urb == es1->cport_out_urb[i]) {
360 es1->cport_out_urb_cancelled[i] = true;
364 spin_unlock_irq(&es1->cport_out_urb_lock);
368 if (i < NUM_CPORT_OUT_URB) {
369 spin_lock_irq(&es1->cport_out_urb_lock);
370 es1->cport_out_urb_cancelled[i] = false;
371 spin_unlock_irq(&es1->cport_out_urb_lock);
377 static struct greybus_host_driver es1_driver = {
378 .hd_priv_size = sizeof(struct es1_ap_dev),
379 .message_send = message_send,
380 .message_cancel = message_cancel,
383 /* Common function to report consistent warnings based on URB status */
384 static int check_urb_status(struct urb *urb)
386 struct device *dev = &urb->dev->dev;
387 int status = urb->status;
394 dev_err(dev, "%s: overflow actual length is %d\n",
395 __func__, urb->actual_length);
401 /* device is gone, stop sending */
404 dev_err(dev, "%s: unknown status %d\n", __func__, status);
409 static void ap_disconnect(struct usb_interface *interface)
411 struct es1_ap_dev *es1;
412 struct usb_device *udev;
416 es1 = usb_get_intfdata(interface);
420 usb_log_disable(es1);
422 /* Tear down everything! */
423 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
424 struct urb *urb = es1->cport_out_urb[i];
430 es1->cport_out_urb[i] = NULL;
431 es1->cport_out_urb_busy[i] = false; /* just to be anal */
434 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
435 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
436 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
437 struct urb *urb = cport_in->urb[i];
443 kfree(cport_in->buffer[i]);
444 cport_in->buffer[i] = NULL;
448 usb_set_intfdata(interface, NULL);
450 greybus_remove_hd(es1->hd);
451 kfree(es1->cport_to_ep);
456 static void cport_in_callback(struct urb *urb)
458 struct greybus_host_device *hd = urb->context;
459 struct device *dev = &urb->dev->dev;
460 struct gb_operation_msg_hdr *header;
461 int status = check_urb_status(urb);
466 if ((status == -EAGAIN) || (status == -EPROTO))
468 dev_err(dev, "urb cport in error %d (dropped)\n", status);
472 if (urb->actual_length < sizeof(*header)) {
473 dev_err(dev, "%s: short message received\n", __func__);
477 /* Extract the CPort id, which is packed in the message header */
478 header = urb->transfer_buffer;
479 cport_id = gb_message_cport_unpack(header);
481 if (cport_id_valid(hd, cport_id))
482 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
485 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
488 /* put our urb back in the request pool */
489 retval = usb_submit_urb(urb, GFP_ATOMIC);
491 dev_err(dev, "%s: error %d in submitting urb.\n",
495 static void cport_out_callback(struct urb *urb)
497 struct gb_message *message = urb->context;
498 struct greybus_host_device *hd = message->operation->connection->hd;
499 struct es1_ap_dev *es1 = hd_to_es1(hd);
500 int status = check_urb_status(urb);
503 gb_message_cport_clear(message->header);
506 * Tell the submitter that the message send (attempt) is
507 * complete, and report the status.
509 greybus_message_sent(hd, message, status);
511 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
512 message->hcpriv = NULL;
513 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
518 #define APB1_LOG_MSG_SIZE 64
519 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
523 /* SVC messages go down our control pipe */
525 retval = usb_control_msg(es1->usb_dev,
526 usb_rcvctrlpipe(es1->usb_dev, 0),
528 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
534 kfifo_in(&apb1_log_fifo, buf, retval);
535 } while (retval > 0);
538 static int apb1_log_poll(void *data)
540 struct es1_ap_dev *es1 = data;
543 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
547 while (!kthread_should_stop()) {
549 apb1_log_get(es1, buf);
557 static ssize_t apb1_log_read(struct file *f, char __user *buf,
558 size_t count, loff_t *ppos)
564 if (count > APB1_LOG_SIZE)
565 count = APB1_LOG_SIZE;
567 tmp_buf = kmalloc(count, GFP_KERNEL);
571 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
572 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
579 static const struct file_operations apb1_log_fops = {
580 .read = apb1_log_read,
583 static void usb_log_enable(struct es1_ap_dev *es1)
585 if (!IS_ERR_OR_NULL(apb1_log_task))
588 /* get log from APB1 */
589 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
590 if (IS_ERR(apb1_log_task))
592 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
593 gb_debugfs_get(), NULL,
597 static void usb_log_disable(struct es1_ap_dev *es1)
599 if (IS_ERR_OR_NULL(apb1_log_task))
602 debugfs_remove(apb1_log_dentry);
603 apb1_log_dentry = NULL;
605 kthread_stop(apb1_log_task);
606 apb1_log_task = NULL;
609 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
610 size_t count, loff_t *ppos)
613 int enable = !IS_ERR_OR_NULL(apb1_log_task);
615 sprintf(tmp_buf, "%d\n", enable);
616 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
619 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
620 size_t count, loff_t *ppos)
624 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
626 retval = kstrtoint_from_user(buf, count, 10, &enable);
633 usb_log_disable(es1);
638 static const struct file_operations apb1_log_enable_fops = {
639 .read = apb1_log_enable_read,
640 .write = apb1_log_enable_write,
643 static int apb1_get_cport_count(struct usb_device *udev)
648 cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
652 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
654 USB_DIR_IN | USB_TYPE_VENDOR |
655 USB_RECIP_INTERFACE, 0, 0, cport_count,
656 sizeof(*cport_count), ES1_TIMEOUT);
658 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
663 retval = le16_to_cpu(*cport_count);
665 /* We need to fit a CPort ID in one byte of a message header */
666 if (retval > U8_MAX) {
668 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
677 * The ES1 USB Bridge device contains 4 endpoints
678 * 1 Control - usual USB stuff + AP -> SVC messages
679 * 1 Interrupt IN - SVC -> AP messages
680 * 1 Bulk IN - CPort data in
681 * 1 Bulk OUT - CPort data out
683 static int ap_probe(struct usb_interface *interface,
684 const struct usb_device_id *id)
686 struct es1_ap_dev *es1;
687 struct greybus_host_device *hd;
688 struct usb_device *udev;
689 struct usb_host_interface *iface_desc;
690 struct usb_endpoint_descriptor *endpoint;
693 int retval = -ENOMEM;
697 udev = usb_get_dev(interface_to_usbdev(interface));
699 num_cports = apb1_get_cport_count(udev);
700 if (num_cports < 0) {
702 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
707 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX,
716 es1->usb_intf = interface;
718 spin_lock_init(&es1->cport_out_urb_lock);
719 usb_set_intfdata(interface, es1);
721 es1->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es1->cport_to_ep),
723 if (!es1->cport_to_ep) {
728 /* find all 3 of our endpoints */
729 iface_desc = interface->cur_altsetting;
730 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
731 endpoint = &iface_desc->endpoint[i].desc;
733 if (usb_endpoint_is_bulk_in(endpoint)) {
734 es1->cport_in[bulk_in++].endpoint =
735 endpoint->bEndpointAddress;
736 } else if (usb_endpoint_is_bulk_out(endpoint)) {
737 es1->cport_out[bulk_out++].endpoint =
738 endpoint->bEndpointAddress;
741 "Unknown endpoint type found, address %x\n",
742 endpoint->bEndpointAddress);
745 if ((bulk_in == 0) ||
747 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
751 /* Allocate buffers for our cport in messages and start them up */
752 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
753 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
754 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
758 urb = usb_alloc_urb(0, GFP_KERNEL);
761 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
765 usb_fill_bulk_urb(urb, udev,
766 usb_rcvbulkpipe(udev,
768 buffer, ES1_GBUF_MSG_SIZE_MAX,
769 cport_in_callback, hd);
770 cport_in->urb[i] = urb;
771 cport_in->buffer[i] = buffer;
772 retval = usb_submit_urb(urb, GFP_KERNEL);
778 /* Allocate urbs for our CPort OUT messages */
779 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
782 urb = usb_alloc_urb(0, GFP_KERNEL);
786 es1->cport_out_urb[i] = urb;
787 es1->cport_out_urb_busy[i] = false; /* just to be anal */
790 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
792 gb_debugfs_get(), es1,
793 &apb1_log_enable_fops);
796 ap_disconnect(interface);
801 static struct usb_driver es1_ap_driver = {
802 .name = "es2_ap_driver",
804 .disconnect = ap_disconnect,
805 .id_table = id_table,
808 module_usb_driver(es1_ap_driver);
810 MODULE_LICENSE("GPL v2");
811 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");