blob: a73a268c87595ed16dc9b7c47526b30259bf3940 [file] [log] [blame]
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08001/*
2 * Greybus "AP" USB driver for "ES2" controller chips
3 *
Alex Elder142f8dd2015-03-26 21:25:06 -05004 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +08006 *
7 * Released under the GPLv2 only.
8 */
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>
15
16#include "greybus.h"
17#include "svc_msg.h"
18#include "kernel_ver.h"
19
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080020/* Memory sizes for the buffers sent to/from the ES1 controller */
21#define ES1_SVC_MSG_SIZE (sizeof(struct svc_msg) + SZ_64K)
22#define ES1_GBUF_MSG_SIZE_MAX PAGE_SIZE
23
24static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman2bf4c872015-03-02 08:52:07 -080025 /* Made up numbers for the SVC USB Bridge in ES2 */
26 { USB_DEVICE(0xffff, 0x0002) },
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +080027 { },
28};
29MODULE_DEVICE_TABLE(usb, id_table);
30
31/*
32 * Number of CPort IN urbs in flight at any point in time.
33 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
34 * flight.
35 */
36#define NUM_CPORT_IN_URB 4
37
38/* Number of CPort OUT urbs in flight at any point in time.
39 * Adjust if we get messages saying we are out of urbs in the system log.
40 */
41#define NUM_CPORT_OUT_URB 8
42
43/**
44 * es1_ap_dev - ES1 USB Bridge to AP structure
45 * @usb_dev: pointer to the USB device we are.
46 * @usb_intf: pointer to the USB interface we are bound to.
47 * @hd: pointer to our greybus_host_device structure
48 * @control_endpoint: endpoint to send data to SVC
49 * @svc_endpoint: endpoint for SVC data in
50 * @cport_in_endpoint: bulk in endpoint for CPort data
51 * @cport-out_endpoint: bulk out endpoint for CPort data
52 * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint
53 * @svc_urb: urb for SVC messages coming in on @svc_endpoint
54 * @cport_in_urb: array of urbs for the CPort in messages
55 * @cport_in_buffer: array of buffers for the @cport_in_urb urbs
56 * @cport_out_urb: array of urbs for the CPort out messages
57 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
58 * not.
59 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
60 */
61struct es1_ap_dev {
62 struct usb_device *usb_dev;
63 struct usb_interface *usb_intf;
64 struct greybus_host_device *hd;
65
66 __u8 control_endpoint;
67 __u8 svc_endpoint;
68 __u8 cport_in_endpoint;
69 __u8 cport_out_endpoint;
70
71 u8 *svc_buffer;
72 struct urb *svc_urb;
73
74 struct urb *cport_in_urb[NUM_CPORT_IN_URB];
75 u8 *cport_in_buffer[NUM_CPORT_IN_URB];
76 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
77 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
78 spinlock_t cport_out_urb_lock;
79};
80
81static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
82{
83 return (struct es1_ap_dev *)&hd->hd_priv;
84}
85
86static void cport_out_callback(struct urb *urb);
87
88/*
89 * Buffer constraints for the host driver.
90 *
91 * A "buffer" is used to hold data to be transferred for Greybus by
92 * the host driver. A buffer is represented by a "buffer pointer",
93 * which defines a region of memory used by the host driver for
94 * transferring the data. When Greybus allocates a buffer, it must
95 * do so subject to the constraints associated with the host driver.
96 * These constraints are specified by two parameters: the
97 * headroom; and the maximum buffer size.
98 *
99 * +------------------+
100 * | Host driver | \
101 * | reserved area | }- headroom
102 * | . . . | /
103 * buffer pointer ---> +------------------+
104 * | Buffer space for | \
105 * | transferred data | }- buffer size
106 * | . . . | / (limited to size_max)
107 * +------------------+
108 *
109 * headroom: Every buffer must have at least this much space
110 * *before* the buffer pointer, reserved for use by the
111 * host driver. I.e., ((char *)buffer - headroom) must
112 * point to valid memory, usable only by the host driver.
113 * size_max: The maximum size of a buffer (not including the
114 * headroom) must not exceed this.
115 */
116static void hd_buffer_constraints(struct greybus_host_device *hd)
117{
118 /*
119 * Only one byte is required, but this produces a result
120 * that's better aligned for the user.
121 */
122 hd->buffer_headroom = sizeof(u32); /* For cport id */
123 hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
124 BUILD_BUG_ON(hd->buffer_headroom > GB_BUFFER_HEADROOM_MAX);
125}
126
127#define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */
128static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd)
129{
130 struct es1_ap_dev *es1 = hd_to_es1(hd);
131 int retval;
132
133 /* SVC messages go down our control pipe */
134 retval = usb_control_msg(es1->usb_dev,
135 usb_sndctrlpipe(es1->usb_dev,
136 es1->control_endpoint),
137 0x01, /* vendor request AP message */
138 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
139 0x00, 0x00,
140 (char *)svc_msg,
141 sizeof(*svc_msg),
142 ES1_TIMEOUT);
143 if (retval != sizeof(*svc_msg))
144 return retval;
145
146 return 0;
147}
148
149static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
150{
151 struct urb *urb = NULL;
152 unsigned long flags;
153 int i;
154
155 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
156
157 /* Look in our pool of allocated urbs first, as that's the "fastest" */
158 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
159 if (es1->cport_out_urb_busy[i] == false) {
160 es1->cport_out_urb_busy[i] = true;
161 urb = es1->cport_out_urb[i];
162 break;
163 }
164 }
165 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
166 if (urb)
167 return urb;
168
169 /*
170 * Crap, pool is empty, complain to the syslog and go allocate one
171 * dynamically as we have to succeed.
172 */
173 dev_err(&es1->usb_dev->dev,
174 "No free CPort OUT urbs, having to dynamically allocate one!\n");
175 return usb_alloc_urb(0, gfp_mask);
176}
177
178static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
179{
180 unsigned long flags;
181 int i;
182 /*
183 * See if this was an urb in our pool, if so mark it "free", otherwise
184 * we need to free it ourselves.
185 */
186 spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
187 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
188 if (urb == es1->cport_out_urb[i]) {
189 es1->cport_out_urb_busy[i] = false;
190 urb = NULL;
191 break;
192 }
193 }
194 spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
195
196 /* If urb is not NULL, then we need to free this urb */
197 usb_free_urb(urb);
198}
199
200/*
201 * Returns an opaque cookie value if successful, or a pointer coded
202 * error otherwise. If the caller wishes to cancel the in-flight
203 * buffer, it must supply the returned cookie to the cancel routine.
204 */
205static void *buffer_send(struct greybus_host_device *hd, u16 cport_id,
206 void *buffer, size_t buffer_size, gfp_t gfp_mask)
207{
208 struct es1_ap_dev *es1 = hd_to_es1(hd);
209 struct usb_device *udev = es1->usb_dev;
210 u8 *transfer_buffer = buffer;
211 int transfer_buffer_size;
212 int retval;
213 struct urb *urb;
214
215 if (!buffer) {
216 pr_err("null buffer supplied to send\n");
217 return ERR_PTR(-EINVAL);
218 }
219 if (buffer_size > (size_t)INT_MAX) {
220 pr_err("bad buffer size (%zu) supplied to send\n", buffer_size);
221 return ERR_PTR(-EINVAL);
222 }
223 transfer_buffer--;
224 transfer_buffer_size = buffer_size + 1;
225
226 /*
227 * The data actually transferred will include an indication
228 * of where the data should be sent. Do one last check of
229 * the target CPort id before filling it in.
230 */
231 if (cport_id == CPORT_ID_BAD) {
232 pr_err("request to send inbound data buffer\n");
233 return ERR_PTR(-EINVAL);
234 }
235 if (cport_id > (u16)U8_MAX) {
236 pr_err("cport_id (%hd) is out of range for ES1\n", cport_id);
237 return ERR_PTR(-EINVAL);
238 }
239 /* OK, the destination is fine; record it in the transfer buffer */
240 *transfer_buffer = cport_id;
241
242 /* Find a free urb */
243 urb = next_free_urb(es1, gfp_mask);
244 if (!urb)
245 return ERR_PTR(-ENOMEM);
246
247 usb_fill_bulk_urb(urb, udev,
248 usb_sndbulkpipe(udev, es1->cport_out_endpoint),
249 transfer_buffer, transfer_buffer_size,
250 cport_out_callback, hd);
251 retval = usb_submit_urb(urb, gfp_mask);
252 if (retval) {
253 pr_err("error %d submitting URB\n", retval);
254 free_urb(es1, urb);
255 return ERR_PTR(retval);
256 }
257
Alex Elder142f8dd2015-03-26 21:25:06 -0500258 return urb;
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800259}
260
261/*
262 * The cookie value supplied is the value that buffer_send()
263 * returned to its caller. It identifies the buffer that should be
264 * canceled. This function must also handle (which is to say,
265 * ignore) a null cookie value.
266 */
267static void buffer_cancel(void *cookie)
268{
269
270 /*
271 * We really should be defensive and track all outstanding
272 * (sent) buffers rather than trusting the cookie provided
273 * is valid. For the time being, this will do.
274 */
275 if (cookie)
Alex Elder142f8dd2015-03-26 21:25:06 -0500276 usb_kill_urb(cookie);
Greg Kroah-Hartmanf5870272015-01-21 10:24:15 +0800277}
278
279static struct greybus_host_driver es1_driver = {
280 .hd_priv_size = sizeof(struct es1_ap_dev),
281 .buffer_send = buffer_send,
282 .buffer_cancel = buffer_cancel,
283 .submit_svc = submit_svc,
284};
285
286/* Common function to report consistent warnings based on URB status */
287static int check_urb_status(struct urb *urb)
288{
289 struct device *dev = &urb->dev->dev;
290 int status = urb->status;
291
292 switch (status) {
293 case 0:
294 return 0;
295
296 case -EOVERFLOW:
297 dev_err(dev, "%s: overflow actual length is %d\n",
298 __func__, urb->actual_length);
299 case -ECONNRESET:
300 case -ENOENT:
301 case -ESHUTDOWN:
302 case -EILSEQ:
303 case -EPROTO:
304 /* device is gone, stop sending */
305 return status;
306 }
307 dev_err(dev, "%s: unknown status %d\n", __func__, status);
308
309 return -EAGAIN;
310}
311
312static void ap_disconnect(struct usb_interface *interface)
313{
314 struct es1_ap_dev *es1;
315 struct usb_device *udev;
316 int i;
317
318 es1 = usb_get_intfdata(interface);
319 if (!es1)
320 return;
321
322 /* Tear down everything! */
323 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
324 struct urb *urb = es1->cport_out_urb[i];
325
326 if (!urb)
327 break;
328 usb_kill_urb(urb);
329 usb_free_urb(urb);
330 es1->cport_out_urb[i] = NULL;
331 es1->cport_out_urb_busy[i] = false; /* just to be anal */
332 }
333
334 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
335 struct urb *urb = es1->cport_in_urb[i];
336
337 if (!urb)
338 break;
339 usb_kill_urb(urb);
340 usb_free_urb(urb);
341 kfree(es1->cport_in_buffer[i]);
342 es1->cport_in_buffer[i] = NULL;
343 }
344
345 usb_kill_urb(es1->svc_urb);
346 usb_free_urb(es1->svc_urb);
347 es1->svc_urb = NULL;
348 kfree(es1->svc_buffer);
349 es1->svc_buffer = NULL;
350
351 usb_set_intfdata(interface, NULL);
352 udev = es1->usb_dev;
353 greybus_remove_hd(es1->hd);
354
355 usb_put_dev(udev);
356}
357
358/* Callback for when we get a SVC message */
359static void svc_in_callback(struct urb *urb)
360{
361 struct greybus_host_device *hd = urb->context;
362 struct device *dev = &urb->dev->dev;
363 int status = check_urb_status(urb);
364 int retval;
365
366 if (status) {
367 if ((status == -EAGAIN) || (status == -EPROTO))
368 goto exit;
369 dev_err(dev, "urb svc in error %d (dropped)\n", status);
370 return;
371 }
372
373 /* We have a message, create a new message structure, add it to the
374 * list, and wake up our thread that will process the messages.
375 */
376 greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
377
378exit:
379 /* resubmit the urb to get more messages */
380 retval = usb_submit_urb(urb, GFP_ATOMIC);
381 if (retval)
382 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
383}
384
385static void cport_in_callback(struct urb *urb)
386{
387 struct greybus_host_device *hd = urb->context;
388 struct device *dev = &urb->dev->dev;
389 int status = check_urb_status(urb);
390 int retval;
391 u16 cport_id;
392 u8 *data;
393
394 if (status) {
395 if ((status == -EAGAIN) || (status == -EPROTO))
396 goto exit;
397 dev_err(dev, "urb cport in error %d (dropped)\n", status);
398 return;
399 }
400
401 /* The size has to be at least one, for the cport id */
402 if (!urb->actual_length) {
403 dev_err(dev, "%s: no cport id in input buffer?\n", __func__);
404 goto exit;
405 }
406
407 /*
408 * Our CPort number is the first byte of the data stream,
409 * the rest of the stream is "real" data
410 */
411 data = urb->transfer_buffer;
412 cport_id = (u16)data[0];
413 data = &data[1];
414
415 /* Pass this data to the greybus core */
416 greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
417
418exit:
419 /* put our urb back in the request pool */
420 retval = usb_submit_urb(urb, GFP_ATOMIC);
421 if (retval)
422 dev_err(dev, "%s: error %d in submitting urb.\n",
423 __func__, retval);
424}
425
426static void cport_out_callback(struct urb *urb)
427{
428 struct greybus_host_device *hd = urb->context;
429 struct es1_ap_dev *es1 = hd_to_es1(hd);
430 int status = check_urb_status(urb);
431 u8 *data = urb->transfer_buffer + 1;
432
433 /*
434 * Tell the submitter that the buffer send (attempt) is
435 * complete, and report the status. The submitter's buffer
436 * starts after the one-byte CPort id we inserted.
437 */
438 data = urb->transfer_buffer + 1;
439 greybus_data_sent(hd, data, status);
440
441 free_urb(es1, urb);
442 /*
443 * Rest assured Greg, this craziness is getting fixed.
444 *
445 * Yes, you are right, we aren't telling anyone that the urb finished.
446 * "That's crazy! How does this all even work?" you might be saying.
447 * The "magic" is the idea that greybus works on the "operation" level,
448 * not the "send a buffer" level. All operations are "round-trip" with
449 * a response from the device that the operation finished, or it will
450 * time out. Because of that, we don't care that this urb finished, or
451 * failed, or did anything else, as higher levels of the protocol stack
452 * will handle completions and timeouts and the rest.
453 *
454 * This protocol is "needed" due to some hardware restrictions on the
455 * current generation of Unipro controllers. Think about it for a
456 * minute, this is a USB driver, talking to a Unipro bridge, impedance
457 * mismatch is huge, yet the Unipro controller are even more
458 * underpowered than this little USB controller. We rely on the round
459 * trip to keep stalls in the Unipro controllers from happening so that
460 * we can keep data flowing properly, no matter how slow it might be.
461 *
462 * Once again, a wonderful bus protocol cut down in its prime by a naive
463 * controller chip. We dream of the day we have a "real" HCD for
464 * Unipro. Until then, we suck it up and make the hardware work, as
465 * that's the job of the firmware and kernel.
466 * </rant>
467 */
468}
469
470/*
471 * The ES1 USB Bridge device contains 4 endpoints
472 * 1 Control - usual USB stuff + AP -> SVC messages
473 * 1 Interrupt IN - SVC -> AP messages
474 * 1 Bulk IN - CPort data in
475 * 1 Bulk OUT - CPort data out
476 */
477static int ap_probe(struct usb_interface *interface,
478 const struct usb_device_id *id)
479{
480 struct es1_ap_dev *es1;
481 struct greybus_host_device *hd;
482 struct usb_device *udev;
483 struct usb_host_interface *iface_desc;
484 struct usb_endpoint_descriptor *endpoint;
485 bool int_in_found = false;
486 bool bulk_in_found = false;
487 bool bulk_out_found = false;
488 int retval = -ENOMEM;
489 int i;
490 u8 svc_interval = 0;
491
492 udev = usb_get_dev(interface_to_usbdev(interface));
493
494 hd = greybus_create_hd(&es1_driver, &udev->dev);
495 if (!hd) {
496 usb_put_dev(udev);
497 return -ENOMEM;
498 }
499
500 /* Fill in the buffer allocation constraints */
501 hd_buffer_constraints(hd);
502
503 es1 = hd_to_es1(hd);
504 es1->hd = hd;
505 es1->usb_intf = interface;
506 es1->usb_dev = udev;
507 spin_lock_init(&es1->cport_out_urb_lock);
508 usb_set_intfdata(interface, es1);
509
510 /* Control endpoint is the pipe to talk to this AP, so save it off */
511 endpoint = &udev->ep0.desc;
512 es1->control_endpoint = endpoint->bEndpointAddress;
513
514 /* find all 3 of our endpoints */
515 iface_desc = interface->cur_altsetting;
516 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
517 endpoint = &iface_desc->endpoint[i].desc;
518
519 if (usb_endpoint_is_int_in(endpoint)) {
520 es1->svc_endpoint = endpoint->bEndpointAddress;
521 svc_interval = endpoint->bInterval;
522 int_in_found = true;
523 } else if (usb_endpoint_is_bulk_in(endpoint)) {
524 es1->cport_in_endpoint = endpoint->bEndpointAddress;
525 bulk_in_found = true;
526 } else if (usb_endpoint_is_bulk_out(endpoint)) {
527 es1->cport_out_endpoint = endpoint->bEndpointAddress;
528 bulk_out_found = true;
529 } else {
530 dev_err(&udev->dev,
531 "Unknown endpoint type found, address %x\n",
532 endpoint->bEndpointAddress);
533 }
534 }
535 if ((int_in_found == false) ||
536 (bulk_in_found == false) ||
537 (bulk_out_found == false)) {
538 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
539 goto error;
540 }
541
542 /* Create our buffer and URB to get SVC messages, and start it up */
543 es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL);
544 if (!es1->svc_buffer)
545 goto error;
546
547 es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
548 if (!es1->svc_urb)
549 goto error;
550
551 usb_fill_int_urb(es1->svc_urb, udev,
552 usb_rcvintpipe(udev, es1->svc_endpoint),
553 es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback,
554 hd, svc_interval);
555 retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
556 if (retval)
557 goto error;
558
559 /* Allocate buffers for our cport in messages and start them up */
560 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
561 struct urb *urb;
562 u8 *buffer;
563
564 urb = usb_alloc_urb(0, GFP_KERNEL);
565 if (!urb)
566 goto error;
567 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
568 if (!buffer)
569 goto error;
570
571 usb_fill_bulk_urb(urb, udev,
572 usb_rcvbulkpipe(udev, es1->cport_in_endpoint),
573 buffer, ES1_GBUF_MSG_SIZE_MAX,
574 cport_in_callback, hd);
575 es1->cport_in_urb[i] = urb;
576 es1->cport_in_buffer[i] = buffer;
577 retval = usb_submit_urb(urb, GFP_KERNEL);
578 if (retval)
579 goto error;
580 }
581
582 /* Allocate urbs for our CPort OUT messages */
583 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
584 struct urb *urb;
585
586 urb = usb_alloc_urb(0, GFP_KERNEL);
587 if (!urb)
588 goto error;
589
590 es1->cport_out_urb[i] = urb;
591 es1->cport_out_urb_busy[i] = false; /* just to be anal */
592 }
593
594 return 0;
595error:
596 ap_disconnect(interface);
597
598 return retval;
599}
600
601static struct usb_driver es1_ap_driver = {
602 .name = "es1_ap_driver",
603 .probe = ap_probe,
604 .disconnect = ap_disconnect,
605 .id_table = id_table,
606};
607
608module_usb_driver(es1_ap_driver);
609
610MODULE_LICENSE("GPL");
611MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");