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"
19 #include "greybus_trace.h"
21 /* Memory sizes for the buffers sent to/from the ES1 controller */
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 bulk in and bulk out couple */
41 * Number of CPort IN urbs in flight at any point in time.
42 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
45 #define NUM_CPORT_IN_URB 4
47 /* Number of CPort OUT urbs in flight at any point in time.
48 * Adjust if we get messages saying we are out of urbs in the system log.
50 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
52 /* vendor request APB1 log */
53 #define REQUEST_LOG 0x02
55 /* vendor request to map a cport to bulk in and bulk out endpoints */
56 #define REQUEST_EP_MAPPING 0x03
58 /* vendor request to get the number of cports available */
59 #define REQUEST_CPORT_COUNT 0x04
62 * @endpoint: bulk in endpoint for CPort data
63 * @urb: array of urbs for the CPort in messages
64 * @buffer: array of buffers for the @cport_in_urb urbs
68 struct urb *urb[NUM_CPORT_IN_URB];
69 u8 *buffer[NUM_CPORT_IN_URB];
73 * @endpoint: bulk out endpoint for CPort data
75 struct es1_cport_out {
80 * es1_ap_dev - ES1 USB Bridge to AP structure
81 * @usb_dev: pointer to the USB device we are.
82 * @usb_intf: pointer to the USB interface we are bound to.
83 * @hd: pointer to our greybus_host_device structure
85 * @cport_in: endpoint, urbs and buffer for cport in messages
86 * @cport_out: endpoint for for cport out messages
87 * @cport_out_urb: array of urbs for the CPort out messages
88 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
90 * @cport_out_urb_cancelled: array of flags indicating whether the
91 * corresponding @cport_out_urb is being cancelled
92 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
95 struct usb_device *usb_dev;
96 struct usb_interface *usb_intf;
97 struct greybus_host_device *hd;
99 struct es1_cport_in cport_in[NUM_BULKS];
100 struct es1_cport_out cport_out[NUM_BULKS];
101 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
102 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
103 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
104 spinlock_t cport_out_urb_lock;
110 * cport_to_ep - information about cport to endpoints mapping
111 * @cport_id: the id of cport to map to endpoints
112 * @endpoint_in: the endpoint number to use for in transfer
113 * @endpoint_out: he endpoint number to use for out transfer
121 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
123 return (struct es1_ap_dev *)&hd->hd_priv;
126 static void cport_out_callback(struct urb *urb);
127 static void usb_log_enable(struct es1_ap_dev *es1);
128 static void usb_log_disable(struct es1_ap_dev *es1);
130 /* Get the endpoints pair mapped to the cport */
131 static int cport_to_ep_pair(struct es1_ap_dev *es1, u16 cport_id)
133 if (cport_id >= es1->hd->num_cports)
135 return es1->cport_to_ep[cport_id];
138 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
140 /* Disable for now until we work all of this out to keep a warning-free build */
142 /* Test if the endpoints pair is already mapped to a cport */
143 static int ep_pair_in_use(struct es1_ap_dev *es1, int ep_pair)
147 for (i = 0; i < es1->hd->num_cports; i++) {
148 if (es1->cport_to_ep[i] == ep_pair)
154 /* Configure the endpoint mapping and send the request to APBridge */
155 static int map_cport_to_ep(struct es1_ap_dev *es1,
156 u16 cport_id, int ep_pair)
159 struct cport_to_ep *cport_to_ep;
161 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
163 if (cport_id >= es1->hd->num_cports)
165 if (ep_pair && ep_pair_in_use(es1, ep_pair))
168 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
172 es1->cport_to_ep[cport_id] = ep_pair;
173 cport_to_ep->cport_id = cpu_to_le16(cport_id);
174 cport_to_ep->endpoint_in = es1->cport_in[ep_pair].endpoint;
175 cport_to_ep->endpoint_out = es1->cport_out[ep_pair].endpoint;
177 retval = usb_control_msg(es1->usb_dev,
178 usb_sndctrlpipe(es1->usb_dev, 0),
180 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
183 sizeof(*cport_to_ep),
185 if (retval == sizeof(*cport_to_ep))
192 /* Unmap a cport: use the muxed endpoints pair */
193 static int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
195 return map_cport_to_ep(es1, cport_id, 0);
199 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
201 struct urb *urb = NULL;
205 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
207 /* Look in our pool of allocated urbs first, as that's the "fastest" */
208 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
209 if (es1->cport_out_urb_busy[i] == false &&
210 es1->cport_out_urb_cancelled[i] == false) {
211 es1->cport_out_urb_busy[i] = true;
212 urb = es1->cport_out_urb[i];
216 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
221 * Crap, pool is empty, complain to the syslog and go allocate one
222 * dynamically as we have to succeed.
224 dev_err(&es1->usb_dev->dev,
225 "No free CPort OUT urbs, having to dynamically allocate one!\n");
226 return usb_alloc_urb(0, gfp_mask);
229 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
234 * See if this was an urb in our pool, if so mark it "free", otherwise
235 * we need to free it ourselves.
237 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
238 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
239 if (urb == es1->cport_out_urb[i]) {
240 es1->cport_out_urb_busy[i] = false;
245 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
247 /* If urb is not NULL, then we need to free this urb */
252 * We (ab)use the operation-message header pad bytes to transfer the
253 * cport id in order to minimise overhead.
256 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
258 header->pad[0] = cport_id;
261 /* Clear the pad bytes used for the CPort id */
262 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
267 /* Extract the CPort id packed into the header, and clear it */
268 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
270 u16 cport_id = header->pad[0];
272 gb_message_cport_clear(header);
278 * Returns zero if the message was successfully queued, or a negative errno
281 static int message_send(struct greybus_host_device *hd, u16 cport_id,
282 struct gb_message *message, gfp_t gfp_mask)
284 struct es1_ap_dev *es1 = hd_to_es1(hd);
285 struct usb_device *udev = es1->usb_dev;
293 * The data actually transferred will include an indication
294 * of where the data should be sent. Do one last check of
295 * the target CPort id before filling it in.
297 if (!cport_id_valid(hd, cport_id)) {
298 pr_err("invalid destination cport 0x%02x\n", cport_id);
302 /* Find a free urb */
303 urb = next_free_urb(es1, gfp_mask);
307 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
308 message->hcpriv = urb;
309 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
311 /* Pack the cport id into the message header */
312 gb_message_cport_pack(message->header, cport_id);
314 buffer_size = sizeof(*message->header) + message->payload_size;
316 ep_pair = cport_to_ep_pair(es1, cport_id);
317 usb_fill_bulk_urb(urb, udev,
318 usb_sndbulkpipe(udev,
319 es1->cport_out[ep_pair].endpoint),
320 message->buffer, buffer_size,
321 cport_out_callback, message);
322 urb->transfer_flags |= URB_ZERO_PACKET;
323 trace_gb_host_device_send(hd, cport_id, buffer_size);
324 retval = usb_submit_urb(urb, gfp_mask);
326 pr_err("error %d submitting URB\n", retval);
328 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
329 message->hcpriv = NULL;
330 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
333 gb_message_cport_clear(message->header);
342 * Can not be called in atomic context.
344 static void message_cancel(struct gb_message *message)
346 struct greybus_host_device *hd = message->operation->connection->hd;
347 struct es1_ap_dev *es1 = hd_to_es1(hd);
353 spin_lock_irq(&es1->cport_out_urb_lock);
354 urb = message->hcpriv;
356 /* Prevent dynamically allocated urb from being deallocated. */
359 /* Prevent pre-allocated urb from being reused. */
360 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
361 if (urb == es1->cport_out_urb[i]) {
362 es1->cport_out_urb_cancelled[i] = true;
366 spin_unlock_irq(&es1->cport_out_urb_lock);
370 if (i < NUM_CPORT_OUT_URB) {
371 spin_lock_irq(&es1->cport_out_urb_lock);
372 es1->cport_out_urb_cancelled[i] = false;
373 spin_unlock_irq(&es1->cport_out_urb_lock);
379 static struct greybus_host_driver es1_driver = {
380 .hd_priv_size = sizeof(struct es1_ap_dev),
381 .message_send = message_send,
382 .message_cancel = message_cancel,
385 /* Common function to report consistent warnings based on URB status */
386 static int check_urb_status(struct urb *urb)
388 struct device *dev = &urb->dev->dev;
389 int status = urb->status;
396 dev_err(dev, "%s: overflow actual length is %d\n",
397 __func__, urb->actual_length);
403 /* device is gone, stop sending */
406 dev_err(dev, "%s: unknown status %d\n", __func__, status);
411 static void ap_disconnect(struct usb_interface *interface)
413 struct es1_ap_dev *es1;
414 struct usb_device *udev;
418 es1 = usb_get_intfdata(interface);
422 usb_log_disable(es1);
424 /* Tear down everything! */
425 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
426 struct urb *urb = es1->cport_out_urb[i];
432 es1->cport_out_urb[i] = NULL;
433 es1->cport_out_urb_busy[i] = false; /* just to be anal */
436 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
437 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
438 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
439 struct urb *urb = cport_in->urb[i];
445 kfree(cport_in->buffer[i]);
446 cport_in->buffer[i] = NULL;
450 usb_set_intfdata(interface, NULL);
452 greybus_remove_hd(es1->hd);
453 kfree(es1->cport_to_ep);
458 static void cport_in_callback(struct urb *urb)
460 struct greybus_host_device *hd = urb->context;
461 struct device *dev = &urb->dev->dev;
462 struct gb_operation_msg_hdr *header;
463 int status = check_urb_status(urb);
468 if ((status == -EAGAIN) || (status == -EPROTO))
470 dev_err(dev, "urb cport in error %d (dropped)\n", status);
474 if (urb->actual_length < sizeof(*header)) {
475 dev_err(dev, "%s: short message received\n", __func__);
479 /* Extract the CPort id, which is packed in the message header */
480 header = urb->transfer_buffer;
481 cport_id = gb_message_cport_unpack(header);
483 if (cport_id_valid(hd, cport_id)) {
484 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
485 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
488 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
492 /* put our urb back in the request pool */
493 retval = usb_submit_urb(urb, GFP_ATOMIC);
495 dev_err(dev, "%s: error %d in submitting urb.\n",
499 static void cport_out_callback(struct urb *urb)
501 struct gb_message *message = urb->context;
502 struct greybus_host_device *hd = message->operation->connection->hd;
503 struct es1_ap_dev *es1 = hd_to_es1(hd);
504 int status = check_urb_status(urb);
507 gb_message_cport_clear(message->header);
510 * Tell the submitter that the message send (attempt) is
511 * complete, and report the status.
513 greybus_message_sent(hd, message, status);
515 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
516 message->hcpriv = NULL;
517 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
522 #define APB1_LOG_MSG_SIZE 64
523 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
527 /* SVC messages go down our control pipe */
529 retval = usb_control_msg(es1->usb_dev,
530 usb_rcvctrlpipe(es1->usb_dev, 0),
532 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
538 kfifo_in(&apb1_log_fifo, buf, retval);
539 } while (retval > 0);
542 static int apb1_log_poll(void *data)
544 struct es1_ap_dev *es1 = data;
547 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
551 while (!kthread_should_stop()) {
553 apb1_log_get(es1, buf);
561 static ssize_t apb1_log_read(struct file *f, char __user *buf,
562 size_t count, loff_t *ppos)
568 if (count > APB1_LOG_SIZE)
569 count = APB1_LOG_SIZE;
571 tmp_buf = kmalloc(count, GFP_KERNEL);
575 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
576 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
583 static const struct file_operations apb1_log_fops = {
584 .read = apb1_log_read,
587 static void usb_log_enable(struct es1_ap_dev *es1)
589 if (!IS_ERR_OR_NULL(apb1_log_task))
592 /* get log from APB1 */
593 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
594 if (IS_ERR(apb1_log_task))
596 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
597 gb_debugfs_get(), NULL,
601 static void usb_log_disable(struct es1_ap_dev *es1)
603 if (IS_ERR_OR_NULL(apb1_log_task))
606 debugfs_remove(apb1_log_dentry);
607 apb1_log_dentry = NULL;
609 kthread_stop(apb1_log_task);
610 apb1_log_task = NULL;
613 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
614 size_t count, loff_t *ppos)
617 int enable = !IS_ERR_OR_NULL(apb1_log_task);
619 sprintf(tmp_buf, "%d\n", enable);
620 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
623 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
624 size_t count, loff_t *ppos)
628 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
630 retval = kstrtoint_from_user(buf, count, 10, &enable);
637 usb_log_disable(es1);
642 static const struct file_operations apb1_log_enable_fops = {
643 .read = apb1_log_enable_read,
644 .write = apb1_log_enable_write,
647 static int apb1_get_cport_count(struct usb_device *udev)
652 cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
656 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
658 USB_DIR_IN | USB_TYPE_VENDOR |
659 USB_RECIP_INTERFACE, 0, 0, cport_count,
660 sizeof(*cport_count), ES1_TIMEOUT);
662 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
667 retval = le16_to_cpu(*cport_count);
669 /* We need to fit a CPort ID in one byte of a message header */
670 if (retval > U8_MAX) {
672 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
681 * The ES1 USB Bridge device contains 4 endpoints
682 * 1 Control - usual USB stuff + AP -> SVC messages
683 * 1 Interrupt IN - SVC -> AP messages
684 * 1 Bulk IN - CPort data in
685 * 1 Bulk OUT - CPort data out
687 static int ap_probe(struct usb_interface *interface,
688 const struct usb_device_id *id)
690 struct es1_ap_dev *es1;
691 struct greybus_host_device *hd;
692 struct usb_device *udev;
693 struct usb_host_interface *iface_desc;
694 struct usb_endpoint_descriptor *endpoint;
697 int retval = -ENOMEM;
701 udev = usb_get_dev(interface_to_usbdev(interface));
703 num_cports = apb1_get_cport_count(udev);
704 if (num_cports < 0) {
706 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
711 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX,
720 es1->usb_intf = interface;
722 spin_lock_init(&es1->cport_out_urb_lock);
723 usb_set_intfdata(interface, es1);
725 es1->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es1->cport_to_ep),
727 if (!es1->cport_to_ep) {
732 /* find all 3 of our endpoints */
733 iface_desc = interface->cur_altsetting;
734 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
735 endpoint = &iface_desc->endpoint[i].desc;
737 if (usb_endpoint_is_bulk_in(endpoint)) {
738 es1->cport_in[bulk_in++].endpoint =
739 endpoint->bEndpointAddress;
740 } else if (usb_endpoint_is_bulk_out(endpoint)) {
741 es1->cport_out[bulk_out++].endpoint =
742 endpoint->bEndpointAddress;
745 "Unknown endpoint type found, address %x\n",
746 endpoint->bEndpointAddress);
749 if ((bulk_in == 0) ||
751 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
755 /* Allocate buffers for our cport in messages and start them up */
756 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
757 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
758 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
762 urb = usb_alloc_urb(0, GFP_KERNEL);
765 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
769 usb_fill_bulk_urb(urb, udev,
770 usb_rcvbulkpipe(udev,
772 buffer, ES1_GBUF_MSG_SIZE_MAX,
773 cport_in_callback, hd);
774 cport_in->urb[i] = urb;
775 cport_in->buffer[i] = buffer;
776 retval = usb_submit_urb(urb, GFP_KERNEL);
782 /* Allocate urbs for our CPort OUT messages */
783 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
786 urb = usb_alloc_urb(0, GFP_KERNEL);
790 es1->cport_out_urb[i] = urb;
791 es1->cport_out_urb_busy[i] = false; /* just to be anal */
794 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
796 gb_debugfs_get(), es1,
797 &apb1_log_enable_fops);
800 ap_disconnect(interface);
805 static struct usb_driver es1_ap_driver = {
806 .name = "es2_ap_driver",
808 .disconnect = ap_disconnect,
809 .id_table = id_table,
812 module_usb_driver(es1_ap_driver);
814 MODULE_LICENSE("GPL v2");
815 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");