2 * Greybus "AP" USB driver for "ES2" controller chips
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/errno.h>
13 #include <linux/sizes.h>
14 #include <linux/usb.h>
18 #include "kernel_ver.h"
21 * Macros for making pointers explicitly opaque, such that the result
22 * isn't valid but also can't be mistaken for an ERR_PTR() value.
24 #define conceal_urb(urb) ((void *)((uintptr_t)(urb) ^ 0xbad))
25 #define reveal_urb(cookie) ((void *)((uintptr_t)(cookie) ^ 0xbad))
27 /* Memory sizes for the buffers sent to/from the ES1 controller */
28 #define ES1_SVC_MSG_SIZE (sizeof(struct svc_msg) + SZ_64K)
29 #define ES1_GBUF_MSG_SIZE_MAX PAGE_SIZE
31 static const struct usb_device_id id_table[] = {
32 /* Made up numbers for the SVC USB Bridge in ES1 */
33 { USB_DEVICE(0xffff, 0x0001) },
36 MODULE_DEVICE_TABLE(usb, id_table);
39 * Number of CPort IN urbs in flight at any point in time.
40 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
43 #define NUM_CPORT_IN_URB 4
45 /* Number of CPort OUT urbs in flight at any point in time.
46 * Adjust if we get messages saying we are out of urbs in the system log.
48 #define NUM_CPORT_OUT_URB 8
51 * es1_ap_dev - ES1 USB Bridge to AP structure
52 * @usb_dev: pointer to the USB device we are.
53 * @usb_intf: pointer to the USB interface we are bound to.
54 * @hd: pointer to our greybus_host_device structure
55 * @control_endpoint: endpoint to send data to SVC
56 * @svc_endpoint: endpoint for SVC data in
57 * @cport_in_endpoint: bulk in endpoint for CPort data
58 * @cport-out_endpoint: bulk out endpoint for CPort data
59 * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
60 * @svc_urb: urb for SVC messages coming in on @svc_endpoint
61 * @cport_in_urb: array of urbs for the CPort in messages
62 * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
63 * @cport_out_urb: array of urbs for the CPort out messages
64 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
66 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
69 struct usb_device *usb_dev;
70 struct usb_interface *usb_intf;
71 struct greybus_host_device *hd;
73 __u8 control_endpoint;
75 __u8 cport_in_endpoint;
76 __u8 cport_out_endpoint;
81 struct urb *cport_in_urb[NUM_CPORT_IN_URB];
82 u8 *cport_in_buffer[NUM_CPORT_IN_URB];
83 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
84 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
85 spinlock_t cport_out_urb_lock;
88 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
90 return (struct es1_ap_dev *)&hd->hd_priv;
93 static void cport_out_callback(struct urb *urb);
96 * Buffer constraints for the host driver.
98 * A "buffer" is used to hold data to be transferred for Greybus by
99 * the host driver. A buffer is represented by a "buffer pointer",
100 * which defines a region of memory used by the host driver for
101 * transferring the data. When Greybus allocates a buffer, it must
102 * do so subject to the constraints associated with the host driver.
103 * These constraints are specified by two parameters: the
104 * headroom; and the maximum buffer size.
106 * +------------------+
108 * | reserved area | }- headroom
110 * buffer pointer ---> +------------------+
111 * | Buffer space for | \
112 * | transferred data | }- buffer size
113 * | . . . | / (limited to size_max)
114 * +------------------+
116 * headroom: Every buffer must have at least this much space
117 * *before* the buffer pointer, reserved for use by the
118 * host driver. I.e., ((char *)buffer - headroom) must
119 * point to valid memory, usable only by the host driver.
120 * size_max: The maximum size of a buffer (not including the
121 * headroom) must not exceed this.
123 static void hd_buffer_constraints(struct greybus_host_device *hd)
126 * Only one byte is required, but this produces a result
127 * that's better aligned for the user.
129 hd->buffer_headroom = sizeof(u32); /* For cport id */
130 hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
131 BUILD_BUG_ON(hd->buffer_headroom > GB_BUFFER_HEADROOM_MAX);
134 #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
135 static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
137 struct es1_ap_dev *es1 = hd_to_es1(hd);
140 /* SVC messages go down our control pipe */
141 retval = usb_control_msg(es1->usb_dev,
142 usb_sndctrlpipe(es1->usb_dev,
143 es1->control_endpoint),
144 0x01, /* vendor request AP message */
145 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
150 if (retval != sizeof(*svc_msg))
156 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
158 struct urb *urb = NULL;
162 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
164 /* Look in our pool of allocated urbs first, as that's the "fastest" */
165 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
166 if (es1->cport_out_urb_busy[i] == false) {
167 es1->cport_out_urb_busy[i] = true;
168 urb = es1->cport_out_urb[i];
172 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
177 * Crap, pool is empty, complain to the syslog and go allocate one
178 * dynamically as we have to succeed.
180 dev_err(&es1->usb_dev->dev,
181 "No free CPort OUT urbs, having to dynamically allocate one!\n");
182 return usb_alloc_urb(0, gfp_mask);
185 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
190 * See if this was an urb in our pool, if so mark it "free", otherwise
191 * we need to free it ourselves.
193 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
194 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
195 if (urb == es1->cport_out_urb[i]) {
196 es1->cport_out_urb_busy[i] = false;
201 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
203 /* If urb is not NULL, then we need to free this urb */
208 * Returns an opaque cookie value if successful, or a pointer coded
209 * error otherwise. If the caller wishes to cancel the in-flight
210 * buffer, it must supply the returned cookie to the cancel routine.
212 static void *buffer_send(struct greybus_host_device *hd, u16 cport_id,
213 void *buffer, size_t buffer_size, gfp_t gfp_mask)
215 struct es1_ap_dev *es1 = hd_to_es1(hd);
216 struct usb_device *udev = es1->usb_dev;
217 u8 *transfer_buffer = buffer;
218 int transfer_buffer_size;
223 pr_err("null buffer supplied to send\n");
224 return ERR_PTR(-EINVAL);
226 if (buffer_size > (size_t)INT_MAX) {
227 pr_err("bad buffer size (%zu) supplied to send\n", buffer_size);
228 return ERR_PTR(-EINVAL);
231 transfer_buffer_size = buffer_size + 1;
234 * The data actually transferred will include an indication
235 * of where the data should be sent. Do one last check of
236 * the target CPort id before filling it in.
238 if (cport_id == CPORT_ID_BAD) {
239 pr_err("request to send inbound data buffer\n");
240 return ERR_PTR(-EINVAL);
242 if (cport_id > (u16)U8_MAX) {
243 pr_err("cport_id (%hd) is out of range for ES1\n", cport_id);
244 return ERR_PTR(-EINVAL);
246 /* OK, the destination is fine; record it in the transfer buffer */
247 *transfer_buffer = cport_id;
249 /* Find a free urb */
250 urb = next_free_urb(es1, gfp_mask);
252 return ERR_PTR(-ENOMEM);
254 usb_fill_bulk_urb(urb, udev,
255 usb_sndbulkpipe(udev, es1->cport_out_endpoint),
256 transfer_buffer, transfer_buffer_size,
257 cport_out_callback, hd);
258 retval = usb_submit_urb(urb, gfp_mask);
260 pr_err("error %d submitting URB\n", retval);
262 return ERR_PTR(retval);
265 return conceal_urb(urb);
269 * The cookie value supplied is the value that buffer_send()
270 * returned to its caller. It identifies the buffer that should be
271 * canceled. This function must also handle (which is to say,
272 * ignore) a null cookie value.
274 static void buffer_cancel(void *cookie)
278 * We really should be defensive and track all outstanding
279 * (sent) buffers rather than trusting the cookie provided
280 * is valid. For the time being, this will do.
283 usb_kill_urb(reveal_urb(cookie));
286 static struct greybus_host_driver es1_driver = {
287 .hd_priv_size = sizeof(struct es1_ap_dev),
288 .buffer_send = buffer_send,
289 .buffer_cancel = buffer_cancel,
290 .submit_svc = submit_svc,
293 /* Common function to report consistent warnings based on URB status */
294 static int check_urb_status(struct urb *urb)
296 struct device *dev = &urb->dev->dev;
297 int status = urb->status;
304 dev_err(dev, "%s: overflow actual length is %d\n",
305 __func__, urb->actual_length);
311 /* device is gone, stop sending */
314 dev_err(dev, "%s: unknown status %d\n", __func__, status);
319 static void ap_disconnect(struct usb_interface *interface)
321 struct es1_ap_dev *es1;
322 struct usb_device *udev;
325 es1 = usb_get_intfdata(interface);
329 /* Tear down everything! */
330 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
331 struct urb *urb = es1->cport_out_urb[i];
337 es1->cport_out_urb[i] = NULL;
338 es1->cport_out_urb_busy[i] = false; /* just to be anal */
341 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
342 struct urb *urb = es1->cport_in_urb[i];
348 kfree(es1->cport_in_buffer[i]);
349 es1->cport_in_buffer[i] = NULL;
352 usb_kill_urb(es1->svc_urb);
353 usb_free_urb(es1->svc_urb);
355 kfree(es1->svc_buffer);
356 es1->svc_buffer = NULL;
358 usb_set_intfdata(interface, NULL);
360 greybus_remove_hd(es1->hd);
365 /* Callback for when we get a SVC message */
366 static void svc_in_callback(struct urb *urb)
368 struct greybus_host_device *hd = urb->context;
369 struct device *dev = &urb->dev->dev;
370 int status = check_urb_status(urb);
374 if ((status == -EAGAIN) || (status == -EPROTO))
376 dev_err(dev, "urb svc in error %d (dropped)\n", status);
380 /* We have a message, create a new message structure, add it to the
381 * list, and wake up our thread that will process the messages.
383 greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
386 /* resubmit the urb to get more messages */
387 retval = usb_submit_urb(urb, GFP_ATOMIC);
389 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
392 static void cport_in_callback(struct urb *urb)
394 struct greybus_host_device *hd = urb->context;
395 struct device *dev = &urb->dev->dev;
396 int status = check_urb_status(urb);
402 if ((status == -EAGAIN) || (status == -EPROTO))
404 dev_err(dev, "urb cport in error %d (dropped)\n", status);
408 /* The size has to be at least one, for the cport id */
409 if (!urb->actual_length) {
410 dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
415 * Our CPort number is the first byte of the data stream,
416 * the rest of the stream is "real" data
418 data = urb->transfer_buffer;
419 cport_id = (u16)data[0];
422 /* Pass this data to the greybus core */
423 greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
426 /* put our urb back in the request pool */
427 retval = usb_submit_urb(urb, GFP_ATOMIC);
429 dev_err(dev, "%s: error %d in submitting urb.\n",
433 static void cport_out_callback(struct urb *urb)
435 struct greybus_host_device *hd = urb->context;
436 struct es1_ap_dev *es1 = hd_to_es1(hd);
437 int status = check_urb_status(urb);
438 u8 *data = urb->transfer_buffer + 1;
441 * Tell the submitter that the buffer send (attempt) is
442 * complete, and report the status. The submitter's buffer
443 * starts after the one-byte CPort id we inserted.
445 data = urb->transfer_buffer + 1;
446 greybus_data_sent(hd, data, status);
450 * Rest assured Greg, this craziness is getting fixed.
452 * Yes, you are right, we aren't telling anyone that the urb finished.
453 * "That's crazy! How does this all even work?" you might be saying.
454 * The "magic" is the idea that greybus works on the "operation" level,
455 * not the "send a buffer" level. All operations are "round-trip" with
456 * a response from the device that the operation finished, or it will
457 * time out. Because of that, we don't care that this urb finished, or
458 * failed, or did anything else, as higher levels of the protocol stack
459 * will handle completions and timeouts and the rest.
461 * This protocol is "needed" due to some hardware restrictions on the
462 * current generation of Unipro controllers. Think about it for a
463 * minute, this is a USB driver, talking to a Unipro bridge, impedance
464 * mismatch is huge, yet the Unipro controller are even more
465 * underpowered than this little USB controller. We rely on the round
466 * trip to keep stalls in the Unipro controllers from happening so that
467 * we can keep data flowing properly, no matter how slow it might be.
469 * Once again, a wonderful bus protocol cut down in its prime by a naive
470 * controller chip. We dream of the day we have a "real" HCD for
471 * Unipro. Until then, we suck it up and make the hardware work, as
472 * that's the job of the firmware and kernel.
478 * The ES1 USB Bridge device contains 4 endpoints
479 * 1 Control - usual USB stuff + AP -> SVC messages
480 * 1 Interrupt IN - SVC -> AP messages
481 * 1 Bulk IN - CPort data in
482 * 1 Bulk OUT - CPort data out
484 static int ap_probe(struct usb_interface *interface,
485 const struct usb_device_id *id)
487 struct es1_ap_dev *es1;
488 struct greybus_host_device *hd;
489 struct usb_device *udev;
490 struct usb_host_interface *iface_desc;
491 struct usb_endpoint_descriptor *endpoint;
492 bool int_in_found = false;
493 bool bulk_in_found = false;
494 bool bulk_out_found = false;
495 int retval = -ENOMEM;
499 udev = usb_get_dev(interface_to_usbdev(interface));
501 hd = greybus_create_hd(&es1_driver, &udev->dev);
507 /* Fill in the buffer allocation constraints */
508 hd_buffer_constraints(hd);
512 es1->usb_intf = interface;
514 spin_lock_init(&es1->cport_out_urb_lock);
515 usb_set_intfdata(interface, es1);
517 /* Control endpoint is the pipe to talk to this AP, so save it off */
518 endpoint = &udev->ep0.desc;
519 es1->control_endpoint = endpoint->bEndpointAddress;
521 /* find all 3 of our endpoints */
522 iface_desc = interface->cur_altsetting;
523 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
524 endpoint = &iface_desc->endpoint[i].desc;
526 if (usb_endpoint_is_int_in(endpoint)) {
527 es1->svc_endpoint = endpoint->bEndpointAddress;
528 svc_interval = endpoint->bInterval;
530 } else if (usb_endpoint_is_bulk_in(endpoint)) {
531 es1->cport_in_endpoint = endpoint->bEndpointAddress;
532 bulk_in_found = true;
533 } else if (usb_endpoint_is_bulk_out(endpoint)) {
534 es1->cport_out_endpoint = endpoint->bEndpointAddress;
535 bulk_out_found = true;
538 "Unknown endpoint type found, address %x\n",
539 endpoint->bEndpointAddress);
542 if ((int_in_found == false) ||
543 (bulk_in_found == false) ||
544 (bulk_out_found == false)) {
545 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
549 /* Create our buffer and URB to get SVC messages, and start it up */
550 es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
551 if (!es1->svc_buffer)
554 es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
558 usb_fill_int_urb(es1->svc_urb, udev,
559 usb_rcvintpipe(udev, es1->svc_endpoint),
560 es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
562 retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
566 /* Allocate buffers for our cport in messages and start them up */
567 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
571 urb = usb_alloc_urb(0, GFP_KERNEL);
574 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
578 usb_fill_bulk_urb(urb, udev,
579 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
580 buffer, ES1_GBUF_MSG_SIZE_MAX,
581 cport_in_callback, hd);
582 es1->cport_in_urb[i] = urb;
583 es1->cport_in_buffer[i] = buffer;
584 retval = usb_submit_urb(urb, GFP_KERNEL);
589 /* Allocate urbs for our CPort OUT messages */
590 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
593 urb = usb_alloc_urb(0, GFP_KERNEL);
597 es1->cport_out_urb[i] = urb;
598 es1->cport_out_urb_busy[i] = false; /* just to be anal */
603 ap_disconnect(interface);
608 static struct usb_driver es1_ap_driver = {
609 .name = "es1_ap_driver",
611 .disconnect = ap_disconnect,
612 .id_table = id_table,
615 module_usb_driver(es1_ap_driver);
617 MODULE_LICENSE("GPL");
618 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");