2 * Greybus "AP" USB driver
4 * Copyright 2014 Google Inc.
6 * Released under the GPLv2 only.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/errno.h>
12 #include <linux/sizes.h>
13 #include <linux/usb.h>
17 #include "kernel_ver.h"
20 * Macros for making pointers explicitly opaque, such that the result
21 * isn't valid but also can't be mistaken for an ERR_PTR() value.
23 #define conceal_urb(urb) ((void *)((uintptr_t)(urb) ^ 0xbad))
24 #define reveal_urb(cookie) ((void *)((uintptr_t)(cookie) ^ 0xbad))
26 /* Memory sizes for the buffers sent to/from the ES1 controller */
27 #define ES1_SVC_MSG_SIZE (sizeof(struct svc_msg) + SZ_64K)
28 #define ES1_GBUF_MSG_SIZE_MAX PAGE_SIZE
30 static const struct usb_device_id id_table[] = {
31 /* Made up numbers for the SVC USB Bridge in ES1 */
32 { USB_DEVICE(0xffff, 0x0001) },
35 MODULE_DEVICE_TABLE(usb, id_table);
38 * Number of CPort IN urbs in flight at any point in time.
39 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
42 #define NUM_CPORT_IN_URB 4
44 /* Number of CPort OUT urbs in flight at any point in time.
45 * Adjust if we get messages saying we are out of urbs in the system log.
47 #define NUM_CPORT_OUT_URB 8
50 * es1_ap_dev - ES1 USB Bridge to AP structure
51 * @usb_dev: pointer to the USB device we are.
52 * @usb_intf: pointer to the USB interface we are bound to.
53 * @hd: pointer to our greybus_host_device structure
54 * @control_endpoint: endpoint to send data to SVC
55 * @svc_endpoint: endpoint for SVC data in
56 * @cport_in_endpoint: bulk in endpoint for CPort data
57 * @cport-out_endpoint: bulk out endpoint for CPort data
58 * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
59 * @svc_urb: urb for SVC messages coming in on @svc_endpoint
60 * @cport_in_urb: array of urbs for the CPort in messages
61 * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
62 * @cport_out_urb: array of urbs for the CPort out messages
63 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
65 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
68 struct usb_device *usb_dev;
69 struct usb_interface *usb_intf;
70 struct greybus_host_device *hd;
72 __u8 control_endpoint;
74 __u8 cport_in_endpoint;
75 __u8 cport_out_endpoint;
80 struct urb *cport_in_urb[NUM_CPORT_IN_URB];
81 u8 *cport_in_buffer[NUM_CPORT_IN_URB];
82 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
83 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
84 spinlock_t cport_out_urb_lock;
87 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
89 return (struct es1_ap_dev *)&hd->hd_priv;
92 static void cport_out_callback(struct urb *urb);
95 * Buffer constraints for the host driver.
97 * A "buffer" is used to hold data to be transferred for Greybus by
98 * the host driver. A buffer is represented by a "buffer pointer",
99 * which defines a region of memory used by the host driver for
100 * transferring the data. When Greybus allocates a buffer, it must
101 * do so subject to the constraints associated with the host driver.
102 * These constraints are specified by two parameters: the
103 * headroom; and the maximum buffer size.
105 * +------------------+
107 * | reserved area | }- headroom
109 * buffer pointer ---> +------------------+
110 * | Buffer space for | \
111 * | transferred data | }- buffer size
112 * | . . . | / (limited to size_max)
113 * +------------------+
115 * headroom: Every buffer must have at least this much space
116 * *before* the buffer pointer, reserved for use by the
117 * host driver. I.e., ((char *)buffer - headroom) must
118 * point to valid memory, usable only by the host driver.
119 * size_max: The maximum size of a buffer (not including the
120 * headroom) must not exceed this.
122 static void hd_buffer_constraints(struct greybus_host_device *hd)
125 * Only one byte is required, but this produces a result
126 * that's better aligned for the user.
128 hd->buffer_headroom = sizeof(u32); /* For cport id */
129 hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
132 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
133 static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
135 struct es1_ap_dev *es1 = hd_to_es1(hd);
138 /* SVC messages go down our control pipe */
139 retval = usb_control_msg(es1->usb_dev,
140 usb_sndctrlpipe(es1->usb_dev,
141 es1->control_endpoint),
142 0x01, /* vendor request AP message */
143 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
148 if (retval != sizeof(*svc_msg))
154 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
156 struct urb *urb = NULL;
160 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
162 /* Look in our pool of allocated urbs first, as that's the "fastest" */
163 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
164 if (es1->cport_out_urb_busy[i] == false) {
165 es1->cport_out_urb_busy[i] = true;
166 urb = es1->cport_out_urb[i];
170 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
175 * Crap, pool is empty, complain to the syslog and go allocate one
176 * dynamically as we have to succeed.
178 dev_err(&es1->usb_dev->dev,
179 "No free CPort OUT urbs, having to dynamically allocate one!\n");
180 urb = usb_alloc_urb(0, gfp_mask);
188 * Returns an opaque cookie value if successful, or a pointer coded
189 * error otherwise. If the caller wishes to cancel the in-flight
190 * buffer, it must supply the returned cookie to the cancel routine.
192 static void *buffer_send(struct greybus_host_device *hd, u16 dest_cport_id,
193 void *buffer, size_t buffer_size, gfp_t gfp_mask)
195 struct es1_ap_dev *es1 = hd_to_es1(hd);
196 struct usb_device *udev = es1->usb_dev;
197 u8 *transfer_buffer = buffer;
198 int transfer_buffer_size;
203 pr_err("null buffer supplied to send\n");
204 return ERR_PTR(-EINVAL);
206 if (buffer_size > (size_t)INT_MAX) {
207 pr_err("bad buffer size (%zu) supplied to send\n", buffer_size);
208 return ERR_PTR(-EINVAL);
211 transfer_buffer_size = buffer_size + 1;
214 * The data actually transferred will include an indication
215 * of where the data should be sent. Do one last check of
216 * the target CPort id before filling it in.
218 if (dest_cport_id == CPORT_ID_BAD) {
219 pr_err("request to send inbound data buffer\n");
220 return ERR_PTR(-EINVAL);
222 if (dest_cport_id > (u16)U8_MAX) {
223 pr_err("dest_cport_id (%hd) is out of range for ES1\n",
225 return ERR_PTR(-EINVAL);
227 /* OK, the destination is fine; record it in the transfer buffer */
228 *transfer_buffer = dest_cport_id;
230 /* Find a free urb */
231 urb = next_free_urb(es1, gfp_mask);
233 return ERR_PTR(-ENOMEM);
235 usb_fill_bulk_urb(urb, udev,
236 usb_sndbulkpipe(udev, es1->cport_out_endpoint),
237 transfer_buffer, transfer_buffer_size,
238 cport_out_callback, hd);
239 retval = usb_submit_urb(urb, gfp_mask);
241 pr_err("error %d submitting URB\n", retval);
242 return ERR_PTR(retval);
245 return conceal_urb(urb);
248 static void buffer_cancel(void *cookie)
250 struct urb *urb = reveal_urb(cookie);
253 * We really should be defensive and track all outstanding
254 * (sent) buffers rather than trusting the cookie provided
255 * is valid. For the time being, this will do.
260 static struct greybus_host_driver es1_driver = {
261 .hd_priv_size = sizeof(struct es1_ap_dev),
262 .buffer_send = buffer_send,
263 .buffer_cancel = buffer_cancel,
264 .submit_svc = submit_svc,
267 /* Common function to report consistent warnings based on URB status */
268 static int check_urb_status(struct urb *urb)
270 struct device *dev = &urb->dev->dev;
271 int status = urb->status;
278 dev_err(dev, "%s: overflow actual length is %d\n",
279 __func__, urb->actual_length);
285 /* device is gone, stop sending */
288 dev_err(dev, "%s: unknown status %d\n", __func__, status);
293 static void ap_disconnect(struct usb_interface *interface)
295 struct es1_ap_dev *es1;
298 es1 = usb_get_intfdata(interface);
302 /* Tear down everything! */
303 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
304 struct urb *urb = es1->cport_out_urb[i];
310 es1->cport_out_urb[i] = NULL;
311 es1->cport_out_urb_busy[i] = false; /* just to be anal */
314 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
315 struct urb *urb = es1->cport_in_urb[i];
321 kfree(es1->cport_in_buffer[i]);
322 es1->cport_in_buffer[i] = NULL;
325 usb_kill_urb(es1->svc_urb);
326 usb_free_urb(es1->svc_urb);
328 kfree(es1->svc_buffer);
329 es1->svc_buffer = NULL;
331 usb_set_intfdata(interface, NULL);
332 greybus_remove_hd(es1->hd);
334 usb_put_dev(es1->usb_dev);
337 /* Callback for when we get a SVC message */
338 static void svc_in_callback(struct urb *urb)
340 struct greybus_host_device *hd = urb->context;
341 struct device *dev = &urb->dev->dev;
342 int status = check_urb_status(urb);
346 if (status == -EAGAIN)
348 dev_err(dev, "urb svc in error %d (dropped)\n", status);
352 /* We have a message, create a new message structure, add it to the
353 * list, and wake up our thread that will process the messages.
355 greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
358 /* resubmit the urb to get more messages */
359 retval = usb_submit_urb(urb, GFP_ATOMIC);
361 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
364 static void cport_in_callback(struct urb *urb)
366 struct greybus_host_device *hd = urb->context;
367 struct device *dev = &urb->dev->dev;
368 int status = check_urb_status(urb);
374 if (status == -EAGAIN)
376 dev_err(dev, "urb cport in error %d (dropped)\n", status);
380 /* The size has to be at least one, for the cport id */
381 if (!urb->actual_length) {
382 dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
387 * The CPort number is the first byte of the data stream, the rest of
388 * the stream is "real" data
390 data = urb->transfer_buffer;
391 cport_id = (u16)data[0];
394 /* Pass this data to the greybus core */
395 greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
398 /* put our urb back in the request pool */
399 retval = usb_submit_urb(urb, GFP_ATOMIC);
401 dev_err(dev, "%s: error %d in submitting urb.\n",
405 static void cport_out_callback(struct urb *urb)
407 struct greybus_host_device *hd = urb->context;
408 struct es1_ap_dev *es1 = hd_to_es1(hd);
410 int status = check_urb_status(urb);
411 u8 *data = urb->transfer_buffer + 1;
415 * Tell the submitter that the buffer send (attempt) is
416 * complete, and report the status. The submitter's buffer
417 * starts after the one-byte CPort id we inserted.
419 data = urb->transfer_buffer + 1;
420 greybus_data_sent(hd, data, status);
423 * See if this was an urb in our pool, if so mark it "free", otherwise
424 * we need to free it ourselves.
426 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
427 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
428 if (urb == es1->cport_out_urb[i]) {
429 es1->cport_out_urb_busy[i] = false;
434 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
436 /* If urb is not NULL, then we need to free this urb */
440 * Rest assured Greg, this craziness is getting fixed.
442 * Yes, you are right, we aren't telling anyone that the urb finished.
443 * "That's crazy! How does this all even work?" you might be saying.
444 * The "magic" is the idea that greybus works on the "operation" level,
445 * not the "send a buffer" level. All operations are "round-trip" with
446 * a response from the device that the operation finished, or it will
447 * time out. Because of that, we don't care that this urb finished, or
448 * failed, or did anything else, as higher levels of the protocol stack
449 * will handle completions and timeouts and the rest.
451 * This protocol is "needed" due to some hardware restrictions on the
452 * current generation of Unipro controllers. Think about it for a
453 * minute, this is a USB driver, talking to a Unipro bridge, impedance
454 * mismatch is huge, yet the Unipro controller are even more
455 * underpowered than this little USB controller. We rely on the round
456 * trip to keep stalls in the Unipro controllers from happening so that
457 * we can keep data flowing properly, no matter how slow it might be.
459 * Once again, a wonderful bus protocol cut down in its prime by a naive
460 * controller chip. We dream of the day we have a "real" HCD for
461 * Unipro. Until then, we suck it up and make the hardware work, as
462 * that's the job of the firmware and kernel.
468 * The ES1 USB Bridge device contains 4 endpoints
469 * 1 Control - usual USB stuff + AP -> SVC messages
470 * 1 Interrupt IN - SVC -> AP messages
471 * 1 Bulk IN - CPort data in
472 * 1 Bulk OUT - CPort data out
474 static int ap_probe(struct usb_interface *interface,
475 const struct usb_device_id *id)
477 struct es1_ap_dev *es1;
478 struct greybus_host_device *hd;
479 struct usb_device *udev;
480 struct usb_host_interface *iface_desc;
481 struct usb_endpoint_descriptor *endpoint;
482 bool int_in_found = false;
483 bool bulk_in_found = false;
484 bool bulk_out_found = false;
485 int retval = -ENOMEM;
489 udev = usb_get_dev(interface_to_usbdev(interface));
491 hd = greybus_create_hd(&es1_driver, &udev->dev);
497 /* Fill in the buffer allocation constraints */
498 hd_buffer_constraints(hd);
502 es1->usb_intf = interface;
504 spin_lock_init(&es1->cport_out_urb_lock);
505 usb_set_intfdata(interface, es1);
507 /* Control endpoint is the pipe to talk to this AP, so save it off */
508 endpoint = &udev->ep0.desc;
509 es1->control_endpoint = endpoint->bEndpointAddress;
511 /* find all 3 of our endpoints */
512 iface_desc = interface->cur_altsetting;
513 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
514 endpoint = &iface_desc->endpoint[i].desc;
516 if (usb_endpoint_is_int_in(endpoint)) {
517 es1->svc_endpoint = endpoint->bEndpointAddress;
518 svc_interval = endpoint->bInterval;
520 } else if (usb_endpoint_is_bulk_in(endpoint)) {
521 es1->cport_in_endpoint = endpoint->bEndpointAddress;
522 bulk_in_found = true;
523 } else if (usb_endpoint_is_bulk_out(endpoint)) {
524 es1->cport_out_endpoint = endpoint->bEndpointAddress;
525 bulk_out_found = true;
528 "Unknown endpoint type found, address %x\n",
529 endpoint->bEndpointAddress);
532 if ((int_in_found == false) ||
533 (bulk_in_found == false) ||
534 (bulk_out_found == false)) {
535 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
539 /* Create our buffer and URB to get SVC messages, and start it up */
540 es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
541 if (!es1->svc_buffer)
544 es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
548 usb_fill_int_urb(es1->svc_urb, udev,
549 usb_rcvintpipe(udev, es1->svc_endpoint),
550 es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
552 retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
556 /* Allocate buffers for our cport in messages and start them up */
557 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
561 urb = usb_alloc_urb(0, GFP_KERNEL);
564 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
568 usb_fill_bulk_urb(urb, udev,
569 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
570 buffer, ES1_GBUF_MSG_SIZE_MAX,
571 cport_in_callback, hd);
572 es1->cport_in_urb[i] = urb;
573 es1->cport_in_buffer[i] = buffer;
574 retval = usb_submit_urb(urb, GFP_KERNEL);
579 /* Allocate urbs for our CPort OUT messages */
580 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
583 urb = usb_alloc_urb(0, GFP_KERNEL);
587 es1->cport_out_urb[i] = urb;
588 es1->cport_out_urb_busy[i] = false; /* just to be anal */
593 ap_disconnect(interface);
598 static struct usb_driver es1_ap_driver = {
599 .name = "es1_ap_driver",
601 .disconnect = ap_disconnect,
602 .id_table = id_table,
605 module_usb_driver(es1_ap_driver);
607 MODULE_LICENSE("GPL");
608 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");