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
61 /* vendor request to reset a cport state */
62 #define REQUEST_RESET_CPORT 0x05
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
88 * @cport_in: endpoint, urbs and buffer for cport in messages
89 * @cport_out: endpoint for for cport out messages
90 * @cport_out_urb: array of urbs for the CPort out messages
91 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
93 * @cport_out_urb_cancelled: array of flags indicating whether the
94 * corresponding @cport_out_urb is being cancelled
95 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
98 struct usb_device *usb_dev;
99 struct usb_interface *usb_intf;
100 struct greybus_host_device *hd;
102 struct es1_cport_in cport_in[NUM_BULKS];
103 struct es1_cport_out cport_out[NUM_BULKS];
104 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
105 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
106 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
107 spinlock_t cport_out_urb_lock;
113 * cport_to_ep - information about cport to endpoints mapping
114 * @cport_id: the id of cport to map to endpoints
115 * @endpoint_in: the endpoint number to use for in transfer
116 * @endpoint_out: he endpoint number to use for out transfer
124 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
126 return (struct es1_ap_dev *)&hd->hd_priv;
129 static void cport_out_callback(struct urb *urb);
130 static void usb_log_enable(struct es1_ap_dev *es1);
131 static void usb_log_disable(struct es1_ap_dev *es1);
133 /* Get the endpoints pair mapped to the cport */
134 static int cport_to_ep_pair(struct es1_ap_dev *es1, u16 cport_id)
136 if (cport_id >= es1->hd->num_cports)
138 return es1->cport_to_ep[cport_id];
141 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
143 /* Disable for now until we work all of this out to keep a warning-free build */
145 /* Test if the endpoints pair is already mapped to a cport */
146 static int ep_pair_in_use(struct es1_ap_dev *es1, int ep_pair)
150 for (i = 0; i < es1->hd->num_cports; i++) {
151 if (es1->cport_to_ep[i] == ep_pair)
157 /* Configure the endpoint mapping and send the request to APBridge */
158 static int map_cport_to_ep(struct es1_ap_dev *es1,
159 u16 cport_id, int ep_pair)
162 struct cport_to_ep *cport_to_ep;
164 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
166 if (cport_id >= es1->hd->num_cports)
168 if (ep_pair && ep_pair_in_use(es1, ep_pair))
171 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
175 es1->cport_to_ep[cport_id] = ep_pair;
176 cport_to_ep->cport_id = cpu_to_le16(cport_id);
177 cport_to_ep->endpoint_in = es1->cport_in[ep_pair].endpoint;
178 cport_to_ep->endpoint_out = es1->cport_out[ep_pair].endpoint;
180 retval = usb_control_msg(es1->usb_dev,
181 usb_sndctrlpipe(es1->usb_dev, 0),
183 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
186 sizeof(*cport_to_ep),
188 if (retval == sizeof(*cport_to_ep))
195 /* Unmap a cport: use the muxed endpoints pair */
196 static int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
198 return map_cport_to_ep(es1, cport_id, 0);
202 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
204 struct urb *urb = NULL;
208 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
210 /* Look in our pool of allocated urbs first, as that's the "fastest" */
211 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
212 if (es1->cport_out_urb_busy[i] == false &&
213 es1->cport_out_urb_cancelled[i] == false) {
214 es1->cport_out_urb_busy[i] = true;
215 urb = es1->cport_out_urb[i];
219 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
224 * Crap, pool is empty, complain to the syslog and go allocate one
225 * dynamically as we have to succeed.
227 dev_err(&es1->usb_dev->dev,
228 "No free CPort OUT urbs, having to dynamically allocate one!\n");
229 return usb_alloc_urb(0, gfp_mask);
232 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
237 * See if this was an urb in our pool, if so mark it "free", otherwise
238 * we need to free it ourselves.
240 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
241 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
242 if (urb == es1->cport_out_urb[i]) {
243 es1->cport_out_urb_busy[i] = false;
248 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
250 /* If urb is not NULL, then we need to free this urb */
255 * We (ab)use the operation-message header pad bytes to transfer the
256 * cport id in order to minimise overhead.
259 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
261 header->pad[0] = cport_id;
264 /* Clear the pad bytes used for the CPort id */
265 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
270 /* Extract the CPort id packed into the header, and clear it */
271 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
273 u16 cport_id = header->pad[0];
275 gb_message_cport_clear(header);
281 * Returns zero if the message was successfully queued, or a negative errno
284 static int message_send(struct greybus_host_device *hd, u16 cport_id,
285 struct gb_message *message, gfp_t gfp_mask)
287 struct es1_ap_dev *es1 = hd_to_es1(hd);
288 struct usb_device *udev = es1->usb_dev;
296 * The data actually transferred will include an indication
297 * of where the data should be sent. Do one last check of
298 * the target CPort id before filling it in.
300 if (!cport_id_valid(hd, cport_id)) {
301 pr_err("invalid destination cport 0x%02x\n", cport_id);
305 /* Find a free urb */
306 urb = next_free_urb(es1, gfp_mask);
310 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
311 message->hcpriv = urb;
312 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
314 /* Pack the cport id into the message header */
315 gb_message_cport_pack(message->header, cport_id);
317 buffer_size = sizeof(*message->header) + message->payload_size;
319 ep_pair = cport_to_ep_pair(es1, cport_id);
320 usb_fill_bulk_urb(urb, udev,
321 usb_sndbulkpipe(udev,
322 es1->cport_out[ep_pair].endpoint),
323 message->buffer, buffer_size,
324 cport_out_callback, message);
325 urb->transfer_flags |= URB_ZERO_PACKET;
326 trace_gb_host_device_send(hd, cport_id, buffer_size);
327 retval = usb_submit_urb(urb, gfp_mask);
329 pr_err("error %d submitting URB\n", retval);
331 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
332 message->hcpriv = NULL;
333 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
336 gb_message_cport_clear(message->header);
345 * Can not be called in atomic context.
347 static void message_cancel(struct gb_message *message)
349 struct greybus_host_device *hd = message->operation->connection->hd;
350 struct es1_ap_dev *es1 = hd_to_es1(hd);
356 spin_lock_irq(&es1->cport_out_urb_lock);
357 urb = message->hcpriv;
359 /* Prevent dynamically allocated urb from being deallocated. */
362 /* Prevent pre-allocated urb from being reused. */
363 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
364 if (urb == es1->cport_out_urb[i]) {
365 es1->cport_out_urb_cancelled[i] = true;
369 spin_unlock_irq(&es1->cport_out_urb_lock);
373 if (i < NUM_CPORT_OUT_URB) {
374 spin_lock_irq(&es1->cport_out_urb_lock);
375 es1->cport_out_urb_cancelled[i] = false;
376 spin_unlock_irq(&es1->cport_out_urb_lock);
382 static int cport_reset(struct greybus_host_device *hd, u16 cport_id)
384 struct es1_ap_dev *es1 = hd_to_es1(hd);
385 struct usb_device *udev = es1->usb_dev;
388 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
390 USB_DIR_OUT | USB_TYPE_VENDOR |
391 USB_RECIP_INTERFACE, 0, cport_id,
392 NULL, 0, ES1_TIMEOUT);
394 dev_err(&udev->dev, "failed to reset cport %hu: %d\n", cport_id,
402 static int cport_enable(struct greybus_host_device *hd, u16 cport_id)
406 if (cport_id != GB_SVC_CPORT_ID) {
407 retval = cport_reset(hd, cport_id);
415 static struct greybus_host_driver es1_driver = {
416 .hd_priv_size = sizeof(struct es1_ap_dev),
417 .message_send = message_send,
418 .message_cancel = message_cancel,
419 .cport_enable = cport_enable,
422 /* Common function to report consistent warnings based on URB status */
423 static int check_urb_status(struct urb *urb)
425 struct device *dev = &urb->dev->dev;
426 int status = urb->status;
433 dev_err(dev, "%s: overflow actual length is %d\n",
434 __func__, urb->actual_length);
440 /* device is gone, stop sending */
443 dev_err(dev, "%s: unknown status %d\n", __func__, status);
448 static void ap_disconnect(struct usb_interface *interface)
450 struct es1_ap_dev *es1;
451 struct usb_device *udev;
455 es1 = usb_get_intfdata(interface);
459 usb_log_disable(es1);
461 /* Tear down everything! */
462 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
463 struct urb *urb = es1->cport_out_urb[i];
469 es1->cport_out_urb[i] = NULL;
470 es1->cport_out_urb_busy[i] = false; /* just to be anal */
473 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
474 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
475 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
476 struct urb *urb = cport_in->urb[i];
482 kfree(cport_in->buffer[i]);
483 cport_in->buffer[i] = NULL;
487 usb_set_intfdata(interface, NULL);
489 greybus_remove_hd(es1->hd);
490 kfree(es1->cport_to_ep);
495 static void cport_in_callback(struct urb *urb)
497 struct greybus_host_device *hd = urb->context;
498 struct device *dev = &urb->dev->dev;
499 struct gb_operation_msg_hdr *header;
500 int status = check_urb_status(urb);
505 if ((status == -EAGAIN) || (status == -EPROTO))
507 dev_err(dev, "urb cport in error %d (dropped)\n", status);
511 if (urb->actual_length < sizeof(*header)) {
512 dev_err(dev, "%s: short message received\n", __func__);
516 /* Extract the CPort id, which is packed in the message header */
517 header = urb->transfer_buffer;
518 cport_id = gb_message_cport_unpack(header);
520 if (cport_id_valid(hd, cport_id)) {
521 trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
522 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
525 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
529 /* put our urb back in the request pool */
530 retval = usb_submit_urb(urb, GFP_ATOMIC);
532 dev_err(dev, "%s: error %d in submitting urb.\n",
536 static void cport_out_callback(struct urb *urb)
538 struct gb_message *message = urb->context;
539 struct greybus_host_device *hd = message->operation->connection->hd;
540 struct es1_ap_dev *es1 = hd_to_es1(hd);
541 int status = check_urb_status(urb);
544 gb_message_cport_clear(message->header);
546 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
547 message->hcpriv = NULL;
548 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
551 * Tell the submitter that the message send (attempt) is
552 * complete, and report the status.
554 greybus_message_sent(hd, message, status);
559 #define APB1_LOG_MSG_SIZE 64
560 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
564 /* SVC messages go down our control pipe */
566 retval = usb_control_msg(es1->usb_dev,
567 usb_rcvctrlpipe(es1->usb_dev, 0),
569 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
575 kfifo_in(&apb1_log_fifo, buf, retval);
576 } while (retval > 0);
579 static int apb1_log_poll(void *data)
581 struct es1_ap_dev *es1 = data;
584 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
588 while (!kthread_should_stop()) {
590 apb1_log_get(es1, buf);
598 static ssize_t apb1_log_read(struct file *f, char __user *buf,
599 size_t count, loff_t *ppos)
605 if (count > APB1_LOG_SIZE)
606 count = APB1_LOG_SIZE;
608 tmp_buf = kmalloc(count, GFP_KERNEL);
612 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
613 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
620 static const struct file_operations apb1_log_fops = {
621 .read = apb1_log_read,
624 static void usb_log_enable(struct es1_ap_dev *es1)
626 if (!IS_ERR_OR_NULL(apb1_log_task))
629 /* get log from APB1 */
630 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
631 if (IS_ERR(apb1_log_task))
633 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
634 gb_debugfs_get(), NULL,
638 static void usb_log_disable(struct es1_ap_dev *es1)
640 if (IS_ERR_OR_NULL(apb1_log_task))
643 debugfs_remove(apb1_log_dentry);
644 apb1_log_dentry = NULL;
646 kthread_stop(apb1_log_task);
647 apb1_log_task = NULL;
650 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
651 size_t count, loff_t *ppos)
654 int enable = !IS_ERR_OR_NULL(apb1_log_task);
656 sprintf(tmp_buf, "%d\n", enable);
657 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
660 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
661 size_t count, loff_t *ppos)
665 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
667 retval = kstrtoint_from_user(buf, count, 10, &enable);
674 usb_log_disable(es1);
679 static const struct file_operations apb1_log_enable_fops = {
680 .read = apb1_log_enable_read,
681 .write = apb1_log_enable_write,
684 static int apb1_get_cport_count(struct usb_device *udev)
689 cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL);
693 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
695 USB_DIR_IN | USB_TYPE_VENDOR |
696 USB_RECIP_INTERFACE, 0, 0, cport_count,
697 sizeof(*cport_count), ES1_TIMEOUT);
699 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
704 retval = le16_to_cpu(*cport_count);
706 /* We need to fit a CPort ID in one byte of a message header */
707 if (retval > U8_MAX) {
709 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
718 * The ES1 USB Bridge device contains 4 endpoints
719 * 1 Control - usual USB stuff + AP -> SVC messages
720 * 1 Interrupt IN - SVC -> AP messages
721 * 1 Bulk IN - CPort data in
722 * 1 Bulk OUT - CPort data out
724 static int ap_probe(struct usb_interface *interface,
725 const struct usb_device_id *id)
727 struct es1_ap_dev *es1;
728 struct greybus_host_device *hd;
729 struct usb_device *udev;
730 struct usb_host_interface *iface_desc;
731 struct usb_endpoint_descriptor *endpoint;
734 int retval = -ENOMEM;
738 udev = usb_get_dev(interface_to_usbdev(interface));
740 num_cports = apb1_get_cport_count(udev);
741 if (num_cports < 0) {
743 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
748 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX,
757 es1->usb_intf = interface;
759 spin_lock_init(&es1->cport_out_urb_lock);
760 usb_set_intfdata(interface, es1);
762 es1->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es1->cport_to_ep),
764 if (!es1->cport_to_ep) {
769 /* find all 3 of our endpoints */
770 iface_desc = interface->cur_altsetting;
771 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
772 endpoint = &iface_desc->endpoint[i].desc;
774 if (usb_endpoint_is_bulk_in(endpoint)) {
775 es1->cport_in[bulk_in++].endpoint =
776 endpoint->bEndpointAddress;
777 } else if (usb_endpoint_is_bulk_out(endpoint)) {
778 es1->cport_out[bulk_out++].endpoint =
779 endpoint->bEndpointAddress;
782 "Unknown endpoint type found, address %x\n",
783 endpoint->bEndpointAddress);
786 if ((bulk_in == 0) ||
788 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
792 /* Allocate buffers for our cport in messages and start them up */
793 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
794 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
795 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
799 urb = usb_alloc_urb(0, GFP_KERNEL);
802 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
806 usb_fill_bulk_urb(urb, udev,
807 usb_rcvbulkpipe(udev,
809 buffer, ES1_GBUF_MSG_SIZE_MAX,
810 cport_in_callback, hd);
811 cport_in->urb[i] = urb;
812 cport_in->buffer[i] = buffer;
813 retval = usb_submit_urb(urb, GFP_KERNEL);
819 /* Allocate urbs for our CPort OUT messages */
820 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
823 urb = usb_alloc_urb(0, GFP_KERNEL);
827 es1->cport_out_urb[i] = urb;
828 es1->cport_out_urb_busy[i] = false; /* just to be anal */
831 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
833 gb_debugfs_get(), es1,
834 &apb1_log_enable_fops);
837 ap_disconnect(interface);
842 static struct usb_driver es1_ap_driver = {
843 .name = "es2_ap_driver",
845 .disconnect = ap_disconnect,
846 .id_table = id_table,
849 module_usb_driver(es1_ap_driver);
851 MODULE_LICENSE("GPL v2");
852 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");