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 cport present on USB bridge */
39 /* Number of bulk in and bulk out couple */
43 * Number of CPort IN urbs in flight at any point in time.
44 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
47 #define NUM_CPORT_IN_URB 4
49 /* Number of CPort OUT urbs in flight at any point in time.
50 * Adjust if we get messages saying we are out of urbs in the system log.
52 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
54 /* vendor request AP message */
55 #define REQUEST_SVC 0x01
57 /* vendor request APB1 log */
58 #define REQUEST_LOG 0x02
60 /* vendor request to map a cport to bulk in and bulk out endpoints */
61 #define REQUEST_EP_MAPPING 0x03
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
86 * @control_endpoint: endpoint to send data to SVC
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 __u8 control_endpoint;
104 struct es1_cport_in cport_in[NUM_BULKS];
105 struct es1_cport_out cport_out[NUM_BULKS];
106 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
107 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
108 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
109 spinlock_t cport_out_urb_lock;
111 int cport_to_ep[CPORT_MAX];
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 static int cport_to_ep(struct es1_ap_dev *es1, u16 cport_id)
131 if (cport_id >= CPORT_MAX)
133 return es1->cport_to_ep[cport_id];
136 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
138 static int ep_in_use(struct es1_ap_dev *es1, int bulk_ep_set)
142 for (i = 0; i < CPORT_MAX; i++) {
143 if (es1->cport_to_ep[i] == bulk_ep_set)
149 int map_cport_to_ep(struct es1_ap_dev *es1,
150 u16 cport_id, int bulk_ep_set)
153 struct cport_to_ep *cport_to_ep;
155 if (bulk_ep_set == 0 || bulk_ep_set >= NUM_BULKS)
157 if (cport_id >= CPORT_MAX)
159 if (bulk_ep_set && ep_in_use(es1, bulk_ep_set))
162 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
166 es1->cport_to_ep[cport_id] = bulk_ep_set;
167 cport_to_ep->cport_id = cpu_to_le16(cport_id);
168 cport_to_ep->endpoint_in = es1->cport_in[bulk_ep_set].endpoint;
169 cport_to_ep->endpoint_out = es1->cport_out[bulk_ep_set].endpoint;
171 retval = usb_control_msg(es1->usb_dev,
172 usb_sndctrlpipe(es1->usb_dev,
173 es1->control_endpoint),
175 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
178 sizeof(*cport_to_ep),
180 if (retval == sizeof(*cport_to_ep))
187 int unmap_cport(struct es1_ap_dev *es1, u16 cport_id)
189 return map_cport_to_ep(es1, cport_id, 0);
192 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
194 struct urb *urb = NULL;
198 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
200 /* Look in our pool of allocated urbs first, as that's the "fastest" */
201 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
202 if (es1->cport_out_urb_busy[i] == false &&
203 es1->cport_out_urb_cancelled[i] == false) {
204 es1->cport_out_urb_busy[i] = true;
205 urb = es1->cport_out_urb[i];
209 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
214 * Crap, pool is empty, complain to the syslog and go allocate one
215 * dynamically as we have to succeed.
217 dev_err(&es1->usb_dev->dev,
218 "No free CPort OUT urbs, having to dynamically allocate one!\n");
219 return usb_alloc_urb(0, gfp_mask);
222 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
227 * See if this was an urb in our pool, if so mark it "free", otherwise
228 * we need to free it ourselves.
230 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
231 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
232 if (urb == es1->cport_out_urb[i]) {
233 es1->cport_out_urb_busy[i] = false;
238 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
240 /* If urb is not NULL, then we need to free this urb */
245 * We (ab)use the operation-message header pad bytes to transfer the
246 * cport id in order to minimise overhead.
249 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
251 header->pad[0] = cport_id;
254 /* Clear the pad bytes used for the CPort id */
255 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
260 /* Extract the CPort id packed into the header, and clear it */
261 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
263 u16 cport_id = header->pad[0];
265 gb_message_cport_clear(header);
271 * Returns zero if the message was successfully queued, or a negative errno
274 static int message_send(struct greybus_host_device *hd, u16 cport_id,
275 struct gb_message *message, gfp_t gfp_mask)
277 struct es1_ap_dev *es1 = hd_to_es1(hd);
278 struct usb_device *udev = es1->usb_dev;
286 * The data actually transferred will include an indication
287 * of where the data should be sent. Do one last check of
288 * the target CPort id before filling it in.
290 if (!cport_id_valid(cport_id)) {
291 pr_err("invalid destination cport 0x%02x\n", cport_id);
295 /* Find a free urb */
296 urb = next_free_urb(es1, gfp_mask);
300 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
301 message->hcpriv = urb;
302 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
304 /* Pack the cport id into the message header */
305 gb_message_cport_pack(message->header, cport_id);
307 buffer_size = sizeof(*message->header) + message->payload_size;
309 bulk_ep_set = cport_to_ep(es1, cport_id);
310 usb_fill_bulk_urb(urb, udev,
311 usb_sndbulkpipe(udev,
312 es1->cport_out[bulk_ep_set].endpoint),
313 message->buffer, buffer_size,
314 cport_out_callback, message);
315 gb_connection_push_timestamp(message->operation->connection);
316 retval = usb_submit_urb(urb, gfp_mask);
318 pr_err("error %d submitting URB\n", retval);
320 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
321 message->hcpriv = NULL;
322 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
325 gb_message_cport_clear(message->header);
334 * Can not be called in atomic context.
336 static void message_cancel(struct gb_message *message)
338 struct greybus_host_device *hd = message->operation->connection->hd;
339 struct es1_ap_dev *es1 = hd_to_es1(hd);
345 spin_lock_irq(&es1->cport_out_urb_lock);
346 urb = message->hcpriv;
348 /* Prevent dynamically allocated urb from being deallocated. */
351 /* Prevent pre-allocated urb from being reused. */
352 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
353 if (urb == es1->cport_out_urb[i]) {
354 es1->cport_out_urb_cancelled[i] = true;
358 spin_unlock_irq(&es1->cport_out_urb_lock);
362 if (i < NUM_CPORT_OUT_URB) {
363 spin_lock_irq(&es1->cport_out_urb_lock);
364 es1->cport_out_urb_cancelled[i] = false;
365 spin_unlock_irq(&es1->cport_out_urb_lock);
371 static struct greybus_host_driver es1_driver = {
372 .hd_priv_size = sizeof(struct es1_ap_dev),
373 .message_send = message_send,
374 .message_cancel = message_cancel,
377 /* Common function to report consistent warnings based on URB status */
378 static int check_urb_status(struct urb *urb)
380 struct device *dev = &urb->dev->dev;
381 int status = urb->status;
388 dev_err(dev, "%s: overflow actual length is %d\n",
389 __func__, urb->actual_length);
395 /* device is gone, stop sending */
398 dev_err(dev, "%s: unknown status %d\n", __func__, status);
403 static void ap_disconnect(struct usb_interface *interface)
405 struct es1_ap_dev *es1;
406 struct usb_device *udev;
410 es1 = usb_get_intfdata(interface);
414 usb_log_disable(es1);
416 /* Tear down everything! */
417 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
418 struct urb *urb = es1->cport_out_urb[i];
424 es1->cport_out_urb[i] = NULL;
425 es1->cport_out_urb_busy[i] = false; /* just to be anal */
428 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
429 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
430 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
431 struct urb *urb = cport_in->urb[i];
437 kfree(cport_in->buffer[i]);
438 cport_in->buffer[i] = NULL;
442 usb_set_intfdata(interface, NULL);
444 greybus_remove_hd(es1->hd);
449 static void cport_in_callback(struct urb *urb)
451 struct greybus_host_device *hd = urb->context;
452 struct device *dev = &urb->dev->dev;
453 struct gb_operation_msg_hdr *header;
454 int status = check_urb_status(urb);
459 if ((status == -EAGAIN) || (status == -EPROTO))
461 dev_err(dev, "urb cport in error %d (dropped)\n", status);
465 if (urb->actual_length < sizeof(*header)) {
466 dev_err(dev, "%s: short message received\n", __func__);
470 /* Extract the CPort id, which is packed in the message header */
471 header = urb->transfer_buffer;
472 cport_id = gb_message_cport_unpack(header);
474 if (cport_id_valid(cport_id))
475 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
478 dev_err(dev, "%s: invalid cport id 0x%02x received\n",
481 /* put our urb back in the request pool */
482 retval = usb_submit_urb(urb, GFP_ATOMIC);
484 dev_err(dev, "%s: error %d in submitting urb.\n",
488 static void cport_out_callback(struct urb *urb)
490 struct gb_message *message = urb->context;
491 struct greybus_host_device *hd = message->operation->connection->hd;
492 struct es1_ap_dev *es1 = hd_to_es1(hd);
493 int status = check_urb_status(urb);
496 gb_message_cport_clear(message->header);
499 * Tell the submitter that the message send (attempt) is
500 * complete, and report the status.
502 greybus_message_sent(hd, message, status);
504 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
505 message->hcpriv = NULL;
506 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
511 #define APB1_LOG_MSG_SIZE 64
512 static void apb1_log_get(struct es1_ap_dev *es1, char *buf)
516 /* SVC messages go down our control pipe */
518 retval = usb_control_msg(es1->usb_dev,
519 usb_rcvctrlpipe(es1->usb_dev,
520 es1->control_endpoint),
522 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
528 kfifo_in(&apb1_log_fifo, buf, retval);
529 } while (retval > 0);
532 static int apb1_log_poll(void *data)
534 struct es1_ap_dev *es1 = data;
537 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
541 while (!kthread_should_stop()) {
543 apb1_log_get(es1, buf);
551 static ssize_t apb1_log_read(struct file *f, char __user *buf,
552 size_t count, loff_t *ppos)
558 if (count > APB1_LOG_SIZE)
559 count = APB1_LOG_SIZE;
561 tmp_buf = kmalloc(count, GFP_KERNEL);
565 copied = kfifo_out(&apb1_log_fifo, tmp_buf, count);
566 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
573 static const struct file_operations apb1_log_fops = {
574 .read = apb1_log_read,
577 static void usb_log_enable(struct es1_ap_dev *es1)
579 if (!IS_ERR_OR_NULL(apb1_log_task))
582 /* get log from APB1 */
583 apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log");
584 if (IS_ERR(apb1_log_task))
586 apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO,
587 gb_debugfs_get(), NULL,
591 static void usb_log_disable(struct es1_ap_dev *es1)
593 if (IS_ERR_OR_NULL(apb1_log_task))
596 debugfs_remove(apb1_log_dentry);
597 apb1_log_dentry = NULL;
599 kthread_stop(apb1_log_task);
600 apb1_log_task = NULL;
603 static ssize_t apb1_log_enable_read(struct file *f, char __user *buf,
604 size_t count, loff_t *ppos)
607 int enable = !IS_ERR_OR_NULL(apb1_log_task);
609 sprintf(tmp_buf, "%d\n", enable);
610 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
613 static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf,
614 size_t count, loff_t *ppos)
618 struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private;
620 retval = kstrtoint_from_user(buf, count, 10, &enable);
627 usb_log_disable(es1);
632 static const struct file_operations apb1_log_enable_fops = {
633 .read = apb1_log_enable_read,
634 .write = apb1_log_enable_write,
638 * The ES1 USB Bridge device contains 4 endpoints
639 * 1 Control - usual USB stuff + AP -> SVC messages
640 * 1 Interrupt IN - SVC -> AP messages
641 * 1 Bulk IN - CPort data in
642 * 1 Bulk OUT - CPort data out
644 static int ap_probe(struct usb_interface *interface,
645 const struct usb_device_id *id)
647 struct es1_ap_dev *es1;
648 struct greybus_host_device *hd;
649 struct usb_device *udev;
650 struct usb_host_interface *iface_desc;
651 struct usb_endpoint_descriptor *endpoint;
654 int retval = -ENOMEM;
657 /* We need to fit a CPort ID in one byte of a message header */
658 BUILD_BUG_ON(CPORT_ID_MAX > U8_MAX);
660 udev = usb_get_dev(interface_to_usbdev(interface));
662 hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX);
670 es1->usb_intf = interface;
672 spin_lock_init(&es1->cport_out_urb_lock);
673 usb_set_intfdata(interface, es1);
675 /* Control endpoint is the pipe to talk to this AP, so save it off */
676 endpoint = &udev->ep0.desc;
677 es1->control_endpoint = endpoint->bEndpointAddress;
679 /* find all 3 of our endpoints */
680 iface_desc = interface->cur_altsetting;
681 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
682 endpoint = &iface_desc->endpoint[i].desc;
684 if (usb_endpoint_is_bulk_in(endpoint)) {
685 es1->cport_in[bulk_in++].endpoint =
686 endpoint->bEndpointAddress;
687 } else if (usb_endpoint_is_bulk_out(endpoint)) {
688 es1->cport_out[bulk_out++].endpoint =
689 endpoint->bEndpointAddress;
692 "Unknown endpoint type found, address %x\n",
693 endpoint->bEndpointAddress);
696 if ((bulk_in == 0) ||
698 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
702 /* Allocate buffers for our cport in messages and start them up */
703 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
704 struct es1_cport_in *cport_in = &es1->cport_in[bulk_in];
705 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
709 urb = usb_alloc_urb(0, GFP_KERNEL);
712 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
716 usb_fill_bulk_urb(urb, udev,
717 usb_rcvbulkpipe(udev,
719 buffer, ES1_GBUF_MSG_SIZE_MAX,
720 cport_in_callback, hd);
721 cport_in->urb[i] = urb;
722 cport_in->buffer[i] = buffer;
723 retval = usb_submit_urb(urb, GFP_KERNEL);
729 /* Allocate urbs for our CPort OUT messages */
730 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
733 urb = usb_alloc_urb(0, GFP_KERNEL);
737 es1->cport_out_urb[i] = urb;
738 es1->cport_out_urb_busy[i] = false; /* just to be anal */
741 apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable",
743 gb_debugfs_get(), es1,
744 &apb1_log_enable_fops);
747 ap_disconnect(interface);
752 static struct usb_driver es1_ap_driver = {
753 .name = "es2_ap_driver",
755 .disconnect = ap_disconnect,
756 .id_table = id_table,
759 module_usb_driver(es1_ap_driver);
761 MODULE_LICENSE("GPL v2");
762 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");