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;
112 * cport_to_ep - information about cport to endpoints mapping
113 * @cport_id: the id of cport to map to endpoints
114 * @endpoint_in: the endpoint number to use for in transfer
115 * @endpoint_out: he endpoint number to use for out transfer
123 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
125 return (struct es1_ap_dev *)&hd->hd_priv;
128 static void cport_out_callback(struct urb *urb);
129 static void usb_log_enable(struct es1_ap_dev *es1);
130 static void usb_log_disable(struct es1_ap_dev *es1);
132 /* Get the endpoints pair mapped to the cport */
133 static int cport_to_ep_pair(struct es1_ap_dev *es1, u16 cport_id)
135 if (cport_id >= es1->hd->num_cports)
137 return es1->cport_to_ep[cport_id];
140 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
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);
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 gb_connection_push_timestamp(message->operation->connection);
323 retval = usb_submit_urb(urb, gfp_mask);
325 pr_err("error %d submitting URB\n", retval);
327 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
328 message->hcpriv = NULL;
329 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
332 gb_message_cport_clear(message->header);
341 * Can not be called in atomic context.
343 static void message_cancel(struct gb_message *message)
345 struct greybus_host_device *hd = message->operation->connection->hd;
346 struct es1_ap_dev *es1 = hd_to_es1(hd);
352 spin_lock_irq(&es1->cport_out_urb_lock);
353 urb = message->hcpriv;
355 /* Prevent dynamically allocated urb from being deallocated. */
358 /* Prevent pre-allocated urb from being reused. */
359 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
360 if (urb == es1->cport_out_urb[i]) {
361 es1->cport_out_urb_cancelled[i] = true;
365 spin_unlock_irq(&es1->cport_out_urb_lock);
369 if (i < NUM_CPORT_OUT_URB) {
370 spin_lock_irq(&es1->cport_out_urb_lock);
371 es1->cport_out_urb_cancelled[i] = false;
372 spin_unlock_irq(&es1->cport_out_urb_lock);
378 static struct greybus_host_driver es1_driver = {
379 .hd_priv_size = sizeof(struct es1_ap_dev),
380 .message_send = message_send,
381 .message_cancel = message_cancel,
384 /* Common function to report consistent warnings based on URB status */
385 static int check_urb_status(struct urb *urb)
387 struct device *dev = &urb->dev->dev;
388 int status = urb->status;
395 dev_err(dev, "%s: overflow actual length is %d\n",
396 __func__, urb->actual_length);
402 /* device is gone, stop sending */
405 dev_err(dev, "%s: unknown status %d\n", __func__, status);
410 static void ap_disconnect(struct usb_interface *interface)
412 struct es1_ap_dev *es1;
413 struct usb_device *udev;
417 es1 = usb_get_intfdata(interface);
421 usb_log_disable(es1);
423 /* Tear down everything! */
424 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
425 struct urb *urb = es1->cport_out_urb[i];
431 es1->cport_out_urb[i] = NULL;
432 es1->cport_out_urb_busy[i] = false; /* just to be anal */
435 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
436 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
437 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
438 struct urb *urb = cport_in->urb[i];
444 kfree(cport_in->buffer[i]);
445 cport_in->buffer[i] = NULL;
449 usb_set_intfdata(interface, NULL);
451 greybus_remove_hd(es1->hd);
452 kfree(es1->cport_to_ep);
457 static void cport_in_callback(struct urb *urb)
459 struct greybus_host_device *hd = urb->context;
460 struct device *dev = &urb->dev->dev;
461 struct gb_operation_msg_hdr *header;
462 int status = check_urb_status(urb);
467 if ((status == -EAGAIN) || (status == -EPROTO))
469 dev_err(dev, "urb cport in error %d (dropped)\n", status);
473 if (urb->actual_length < sizeof(*header)) {
474 dev_err(dev, "%s: short message received\n", __func__);
478 /* Extract the CPort id, which is packed in the message header */
479 header = urb->transfer_buffer;
480 cport_id = gb_message_cport_unpack(header);
482 if (cport_id_valid(hd, cport_id))
483 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
486 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
489 /* put our urb back in the request pool */
490 retval = usb_submit_urb(urb, GFP_ATOMIC);
492 dev_err(dev, "%s: error %d in submitting urb.\n",
496 static void cport_out_callback(struct urb *urb)
498 struct gb_message *message = urb->context;
499 struct greybus_host_device *hd = message->operation->connection->hd;
500 struct es1_ap_dev *es1 = hd_to_es1(hd);
501 int status = check_urb_status(urb);
504 gb_message_cport_clear(message->header);
507 * Tell the submitter that the message send (attempt) is
508 * complete, and report the status.
510 greybus_message_sent(hd, message, status);
512 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
513 message->hcpriv = NULL;
514 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
519 #define APB1_LOG_MSG_SIZE 64
520 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
524 /* SVC messages go down our control pipe */
526 retval = usb_control_msg(es1->usb_dev,
527 usb_rcvctrlpipe(es1->usb_dev, 0),
529 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
535 kfifo_in(&apb1_log_fifo, buf, retval);
536 } while (retval > 0);
539 static int apb1_log_poll(void *data)
541 struct es1_ap_dev *es1 = data;
544 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
548 while (!kthread_should_stop()) {
550 apb1_log_get(es1, buf);
558 static ssize_t apb1_log_read(struct file *f, char __user *buf,
559 size_t count, loff_t *ppos)
565 if (count > APB1_LOG_SIZE)
566 count = APB1_LOG_SIZE;
568 tmp_buf = kmalloc(count, GFP_KERNEL);
572 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
573 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
580 static const struct file_operations apb1_log_fops = {
581 .read = apb1_log_read,
584 static void usb_log_enable(struct es1_ap_dev *es1)
586 if (!IS_ERR_OR_NULL(apb1_log_task))
589 /* get log from APB1 */
590 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
591 if (IS_ERR(apb1_log_task))
593 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
594 gb_debugfs_get(), NULL,
598 static void usb_log_disable(struct es1_ap_dev *es1)
600 if (IS_ERR_OR_NULL(apb1_log_task))
603 debugfs_remove(apb1_log_dentry);
604 apb1_log_dentry = NULL;
606 kthread_stop(apb1_log_task);
607 apb1_log_task = NULL;
610 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
611 size_t count, loff_t *ppos)
614 int enable = !IS_ERR_OR_NULL(apb1_log_task);
616 sprintf(tmp_buf, "%d\n", enable);
617 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
620 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
621 size_t count, loff_t *ppos)
625 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
627 retval = kstrtoint_from_user(buf, count, 10, &enable);
634 usb_log_disable(es1);
639 static const struct file_operations apb1_log_enable_fops = {
640 .read = apb1_log_enable_read,
641 .write = apb1_log_enable_write,
644 static int apb1_get_cport_count(struct usb_device *udev)
649 cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
653 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
655 USB_DIR_IN | USB_TYPE_VENDOR |
656 USB_RECIP_INTERFACE, 0, 0, cport_count,
657 sizeof(*cport_count), ES1_TIMEOUT);
659 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
664 retval = le16_to_cpu(*cport_count);
666 /* We need to fit a CPort ID in one byte of a message header */
667 if (retval > U8_MAX) {
669 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
678 * The ES1 USB Bridge device contains 4 endpoints
679 * 1 Control - usual USB stuff + AP -> SVC messages
680 * 1 Interrupt IN - SVC -> AP messages
681 * 1 Bulk IN - CPort data in
682 * 1 Bulk OUT - CPort data out
684 static int ap_probe(struct usb_interface *interface,
685 const struct usb_device_id *id)
687 struct es1_ap_dev *es1;
688 struct greybus_host_device *hd;
689 struct usb_device *udev;
690 struct usb_host_interface *iface_desc;
691 struct usb_endpoint_descriptor *endpoint;
694 int retval = -ENOMEM;
698 udev = usb_get_dev(interface_to_usbdev(interface));
700 num_cports = apb1_get_cport_count(udev);
701 if (num_cports < 0) {
703 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
708 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX,
717 es1->usb_intf = interface;
719 spin_lock_init(&es1->cport_out_urb_lock);
720 usb_set_intfdata(interface, es1);
722 es1->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es1->cport_to_ep),
724 if (!es1->cport_to_ep) {
729 /* find all 3 of our endpoints */
730 iface_desc = interface->cur_altsetting;
731 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
732 endpoint = &iface_desc->endpoint[i].desc;
734 if (usb_endpoint_is_bulk_in(endpoint)) {
735 es1->cport_in[bulk_in++].endpoint =
736 endpoint->bEndpointAddress;
737 } else if (usb_endpoint_is_bulk_out(endpoint)) {
738 es1->cport_out[bulk_out++].endpoint =
739 endpoint->bEndpointAddress;
742 "Unknown endpoint type found, address %x\n",
743 endpoint->bEndpointAddress);
746 if ((bulk_in == 0) ||
748 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
752 /* Allocate buffers for our cport in messages and start them up */
753 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
754 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
755 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
759 urb = usb_alloc_urb(0, GFP_KERNEL);
762 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
766 usb_fill_bulk_urb(urb, udev,
767 usb_rcvbulkpipe(udev,
769 buffer, ES1_GBUF_MSG_SIZE_MAX,
770 cport_in_callback, hd);
771 cport_in->urb[i] = urb;
772 cport_in->buffer[i] = buffer;
773 retval = usb_submit_urb(urb, GFP_KERNEL);
779 /* Allocate urbs for our CPort OUT messages */
780 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
783 urb = usb_alloc_urb(0, GFP_KERNEL);
787 es1->cport_out_urb[i] = urb;
788 es1->cport_out_urb_busy[i] = false; /* just to be anal */
791 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
793 gb_debugfs_get(), es1,
794 &apb1_log_enable_fops);
797 ap_disconnect(interface);
802 static struct usb_driver es1_ap_driver = {
803 .name = "es2_ap_driver",
805 .disconnect = ap_disconnect,
806 .id_table = id_table,
809 module_usb_driver(es1_ap_driver);
811 MODULE_LICENSE("GPL v2");
812 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");