]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/es1-ap-usb.c
greybus: Random spell fixes
[karo-tx-linux.git] / drivers / staging / greybus / es1-ap-usb.c
1 /*
2  * Greybus "AP" USB driver
3  *
4  * Copyright 2014 Google Inc.
5  *
6  * Released under the GPLv2 only.
7  */
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>
14
15 #include "greybus.h"
16 #include "svc_msg.h"
17 #include "kernel_ver.h"
18
19 /*
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.
22  */
23 #define conceal_urb(urb)        ((void *)((uintptr_t)(urb) ^ 0xbad))
24 #define reveal_urb(cookie)      ((void *)((uintptr_t)(cookie) ^ 0xbad))
25
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
29
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) },
33         { },
34 };
35 MODULE_DEVICE_TABLE(usb, id_table);
36
37 /*
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
40  * flight.
41  */
42 #define NUM_CPORT_IN_URB        4
43
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.
46  */
47 #define NUM_CPORT_OUT_URB       8
48
49 /**
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
64  *                      not.
65  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
66  */
67 struct es1_ap_dev {
68         struct usb_device *usb_dev;
69         struct usb_interface *usb_intf;
70         struct greybus_host_device *hd;
71
72         __u8 control_endpoint;
73         __u8 svc_endpoint;
74         __u8 cport_in_endpoint;
75         __u8 cport_out_endpoint;
76
77         u8 *svc_buffer;
78         struct urb *svc_urb;
79
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;
85 };
86
87 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
88 {
89         return (struct es1_ap_dev *)&hd->hd_priv;
90 }
91
92 static void cport_out_callback(struct urb *urb);
93
94 /*
95  * Buffer constraints for the host driver.
96  *
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.
104  *
105  *                      +------------------+
106  *                      |    Host driver   | \
107  *                      |   reserved area  |  }- headroom
108  *                      |      . . .       | /
109  *  buffer pointer ---> +------------------+
110  *                      | Buffer space for | \
111  *                      | transferred data |  }- buffer size
112  *                      |      . . .       | /   (limited to size_max)
113  *                      +------------------+
114  *
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.
121  */
122 static void hd_buffer_constraints(struct greybus_host_device *hd)
123 {
124         /*
125          * Only one byte is required, but this produces a result
126          * that's better aligned for the user.
127          */
128         hd->buffer_headroom = sizeof(u32);      /* For cport id */
129         hd->buffer_size_max = ES1_GBUF_MSG_SIZE_MAX;
130 }
131
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)
134 {
135         struct es1_ap_dev *es1 = hd_to_es1(hd);
136         int retval;
137
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,
144                                  0x00, 0x00,
145                                  (char *)svc_msg,
146                                  sizeof(*svc_msg),
147                                  ES1_TIMEOUT);
148         if (retval != sizeof(*svc_msg))
149                 return retval;
150
151         return 0;
152 }
153
154 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
155 {
156         struct urb *urb = NULL;
157         unsigned long flags;
158         int i;
159
160         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
161
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];
167                         break;
168                 }
169         }
170         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
171         if (urb)
172                 return urb;
173
174         /*
175          * Crap, pool is empty, complain to the syslog and go allocate one
176          * dynamically as we have to succeed.
177          */
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);
181         if (!urb)
182                 return NULL;
183
184         return urb;
185 }
186
187 /*
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.
191  */
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)
194 {
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;
199         int retval;
200         struct urb *urb;
201
202         if (!buffer) {
203                 pr_err("null buffer supplied to send\n");
204                 return ERR_PTR(-EINVAL);
205         }
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);
209         }
210         transfer_buffer--;
211         transfer_buffer_size = buffer_size + 1;
212
213         /*
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.
217          */
218         if (dest_cport_id == CPORT_ID_BAD) {
219                 pr_err("request to send inbound data buffer\n");
220                 return ERR_PTR(-EINVAL);
221         }
222         if (dest_cport_id > (u16)U8_MAX) {
223                 pr_err("dest_cport_id (%hd) is out of range for ES1\n",
224                         dest_cport_id);
225                 return ERR_PTR(-EINVAL);
226         }
227         /* OK, the destination is fine; record it in the transfer buffer */
228         *transfer_buffer = dest_cport_id;
229
230         /* Find a free urb */
231         urb = next_free_urb(es1, gfp_mask);
232         if (!urb)
233                 return ERR_PTR(-ENOMEM);
234
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);
240         if (retval) {
241                 pr_err("error %d submitting URB\n", retval);
242                 return ERR_PTR(retval);
243         }
244
245         return conceal_urb(urb);
246 }
247
248 static void buffer_cancel(void *cookie)
249 {
250         struct urb *urb = reveal_urb(cookie);
251
252         /*
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.
256          */
257         usb_kill_urb(urb);
258 }
259
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,
265 };
266
267 /* Common function to report consistent warnings based on URB status */
268 static int check_urb_status(struct urb *urb)
269 {
270         struct device *dev = &urb->dev->dev;
271         int status = urb->status;
272
273         switch (status) {
274         case 0:
275                 return 0;
276
277         case -EOVERFLOW:
278                 dev_err(dev, "%s: overflow actual length is %d\n",
279                         __func__, urb->actual_length);
280         case -ECONNRESET:
281         case -ENOENT:
282         case -ESHUTDOWN:
283         case -EILSEQ:
284         case -EPROTO:
285                 /* device is gone, stop sending */
286                 return status;
287         }
288         dev_err(dev, "%s: unknown status %d\n", __func__, status);
289
290         return -EAGAIN;
291 }
292
293 static void ap_disconnect(struct usb_interface *interface)
294 {
295         struct es1_ap_dev *es1;
296         int i;
297
298         es1 = usb_get_intfdata(interface);
299         if (!es1)
300                 return;
301
302         /* Tear down everything! */
303         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
304                 struct urb *urb = es1->cport_out_urb[i];
305
306                 if (!urb)
307                         break;
308                 usb_kill_urb(urb);
309                 usb_free_urb(urb);
310                 es1->cport_out_urb[i] = NULL;
311                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
312         }
313
314         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
315                 struct urb *urb = es1->cport_in_urb[i];
316
317                 if (!urb)
318                         break;
319                 usb_kill_urb(urb);
320                 usb_free_urb(urb);
321                 kfree(es1->cport_in_buffer[i]);
322                 es1->cport_in_buffer[i] = NULL;
323         }
324
325         usb_kill_urb(es1->svc_urb);
326         usb_free_urb(es1->svc_urb);
327         es1->svc_urb = NULL;
328         kfree(es1->svc_buffer);
329         es1->svc_buffer = NULL;
330
331         usb_set_intfdata(interface, NULL);
332         greybus_remove_hd(es1->hd);
333
334         usb_put_dev(es1->usb_dev);
335 }
336
337 /* Callback for when we get a SVC message */
338 static void svc_in_callback(struct urb *urb)
339 {
340         struct greybus_host_device *hd = urb->context;
341         struct device *dev = &urb->dev->dev;
342         int status = check_urb_status(urb);
343         int retval;
344
345         if (status) {
346                 if (status == -EAGAIN)
347                         goto exit;
348                 dev_err(dev, "urb svc in error %d (dropped)\n", status);
349                 return;
350         }
351
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.
354          */
355         greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
356
357 exit:
358         /* resubmit the urb to get more messages */
359         retval = usb_submit_urb(urb, GFP_ATOMIC);
360         if (retval)
361                 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
362 }
363
364 static void cport_in_callback(struct urb *urb)
365 {
366         struct greybus_host_device *hd = urb->context;
367         struct device *dev = &urb->dev->dev;
368         int status = check_urb_status(urb);
369         int retval;
370         u16 cport_id;
371         u8 *data;
372
373         if (status) {
374                 if (status == -EAGAIN)
375                         goto exit;
376                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
377                 return;
378         }
379
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__);
383                 goto exit;
384         }
385
386         /*
387          * The CPort number is the first byte of the data stream, the rest of
388          * the stream is "real" data
389          */
390         data = urb->transfer_buffer;
391         cport_id = (u16)data[0];
392         data = &data[1];
393
394         /* Pass this data to the greybus core */
395         greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
396
397 exit:
398         /* put our urb back in the request pool */
399         retval = usb_submit_urb(urb, GFP_ATOMIC);
400         if (retval)
401                 dev_err(dev, "%s: error %d in submitting urb.\n",
402                         __func__, retval);
403 }
404
405 static void cport_out_callback(struct urb *urb)
406 {
407         struct greybus_host_device *hd = urb->context;
408         struct es1_ap_dev *es1 = hd_to_es1(hd);
409         unsigned long flags;
410         int status = check_urb_status(urb);
411         u8 *data = urb->transfer_buffer + 1;
412         int i;
413
414         /*
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.
418          */
419         data = urb->transfer_buffer + 1;
420         greybus_data_sent(hd, data, status);
421
422         /*
423          * See if this was an urb in our pool, if so mark it "free", otherwise
424          * we need to free it ourselves.
425          */
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;
430                         urb = NULL;
431                         break;
432                 }
433         }
434         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
435
436         /* If urb is not NULL, then we need to free this urb */
437         usb_free_urb(urb);
438
439         /*
440          * Rest assured Greg, this craziness is getting fixed.
441          *
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.
450          *
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.
458          *
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.
463          * </rant>
464          */
465 }
466
467 /*
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
473  */
474 static int ap_probe(struct usb_interface *interface,
475                     const struct usb_device_id *id)
476 {
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;
486         int i;
487         u8 svc_interval = 0;
488
489         udev = usb_get_dev(interface_to_usbdev(interface));
490
491         hd = greybus_create_hd(&es1_driver, &udev->dev);
492         if (!hd) {
493                 usb_put_dev(udev);
494                 return -ENOMEM;
495         }
496
497         /* Fill in the buffer allocation constraints */
498         hd_buffer_constraints(hd);
499
500         es1 = hd_to_es1(hd);
501         es1->hd = hd;
502         es1->usb_intf = interface;
503         es1->usb_dev = udev;
504         spin_lock_init(&es1->cport_out_urb_lock);
505         usb_set_intfdata(interface, es1);
506
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;
510
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;
515
516                 if (usb_endpoint_is_int_in(endpoint)) {
517                         es1->svc_endpoint = endpoint->bEndpointAddress;
518                         svc_interval = endpoint->bInterval;
519                         int_in_found = true;
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;
526                 } else {
527                         dev_err(&udev->dev,
528                                 "Unknown endpoint type found, address %x\n",
529                                 endpoint->bEndpointAddress);
530                 }
531         }
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");
536                 goto error;
537         }
538
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)
542                 goto error;
543
544         es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
545         if (!es1->svc_urb)
546                 goto error;
547
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,
551                          hd, svc_interval);
552         retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
553         if (retval)
554                 goto error;
555
556         /* Allocate buffers for our cport in messages and start them up */
557         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
558                 struct urb *urb;
559                 u8 *buffer;
560
561                 urb = usb_alloc_urb(0, GFP_KERNEL);
562                 if (!urb)
563                         goto error;
564                 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
565                 if (!buffer)
566                         goto error;
567
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);
575                 if (retval)
576                         goto error;
577         }
578
579         /* Allocate urbs for our CPort OUT messages */
580         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
581                 struct urb *urb;
582
583                 urb = usb_alloc_urb(0, GFP_KERNEL);
584                 if (!urb)
585                         goto error;
586
587                 es1->cport_out_urb[i] = urb;
588                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
589         }
590
591         return 0;
592 error:
593         ap_disconnect(interface);
594
595         return retval;
596 }
597
598 static struct usb_driver es1_ap_driver = {
599         .name =         "es1_ap_driver",
600         .probe =        ap_probe,
601         .disconnect =   ap_disconnect,
602         .id_table =     id_table,
603 };
604
605 module_usb_driver(es1_ap_driver);
606
607 MODULE_LICENSE("GPL");
608 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");