greybus: Remove "gb-" prefix from .c files
[cascardo/linux.git] / drivers / staging / greybus / es2.c
1 /*
2  * Greybus "AP" USB driver for "ES2" controller chips
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
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
20 /*
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.
23  */
24 #define conceal_urb(urb)        ((void *)((uintptr_t)(urb) ^ 0xbad))
25 #define reveal_urb(cookie)      ((void *)((uintptr_t)(cookie) ^ 0xbad))
26
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
30
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) },
34         { },
35 };
36 MODULE_DEVICE_TABLE(usb, id_table);
37
38 /*
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
41  * flight.
42  */
43 #define NUM_CPORT_IN_URB        4
44
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.
47  */
48 #define NUM_CPORT_OUT_URB       8
49
50 /**
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
65  *                      not.
66  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
67  */
68 struct es1_ap_dev {
69         struct usb_device *usb_dev;
70         struct usb_interface *usb_intf;
71         struct greybus_host_device *hd;
72
73         __u8 control_endpoint;
74         __u8 svc_endpoint;
75         __u8 cport_in_endpoint;
76         __u8 cport_out_endpoint;
77
78         u8 *svc_buffer;
79         struct urb *svc_urb;
80
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;
86 };
87
88 static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
89 {
90         return (struct es1_ap_dev *)&hd->hd_priv;
91 }
92
93 static void cport_out_callback(struct urb *urb);
94
95 /*
96  * Buffer constraints for the host driver.
97  *
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.
105  *
106  *                      +------------------+
107  *                      |    Host driver   | \
108  *                      |   reserved area  |  }- headroom
109  *                      |      . . .       | /
110  *  buffer pointer ---> +------------------+
111  *                      | Buffer space for | \
112  *                      | transferred data |  }- buffer size
113  *                      |      . . .       | /   (limited to size_max)
114  *                      +------------------+
115  *
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.
122  */
123 static void hd_buffer_constraints(struct greybus_host_device *hd)
124 {
125         /*
126          * Only one byte is required, but this produces a result
127          * that's better aligned for the user.
128          */
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);
132 }
133
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)
136 {
137         struct es1_ap_dev *es1 = hd_to_es1(hd);
138         int retval;
139
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,
146                                  0x00, 0x00,
147                                  (char *)svc_msg,
148                                  sizeof(*svc_msg),
149                                  ES1_TIMEOUT);
150         if (retval != sizeof(*svc_msg))
151                 return retval;
152
153         return 0;
154 }
155
156 static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask)
157 {
158         struct urb *urb = NULL;
159         unsigned long flags;
160         int i;
161
162         spin_lock_irqsave(&es1->cport_out_urb_lock, flags);
163
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];
169                         break;
170                 }
171         }
172         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
173         if (urb)
174                 return urb;
175
176         /*
177          * Crap, pool is empty, complain to the syslog and go allocate one
178          * dynamically as we have to succeed.
179          */
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);
183 }
184
185 static void free_urb(struct es1_ap_dev *es1, struct urb *urb)
186 {
187         unsigned long flags;
188         int i;
189         /*
190          * See if this was an urb in our pool, if so mark it "free", otherwise
191          * we need to free it ourselves.
192          */
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;
197                         urb = NULL;
198                         break;
199                 }
200         }
201         spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags);
202
203         /* If urb is not NULL, then we need to free this urb */
204         usb_free_urb(urb);
205 }
206
207 /*
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.
211  */
212 static void *buffer_send(struct greybus_host_device *hd, u16 cport_id,
213                         void *buffer, size_t buffer_size, gfp_t gfp_mask)
214 {
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;
219         int retval;
220         struct urb *urb;
221
222         if (!buffer) {
223                 pr_err("null buffer supplied to send\n");
224                 return ERR_PTR(-EINVAL);
225         }
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);
229         }
230         transfer_buffer--;
231         transfer_buffer_size = buffer_size + 1;
232
233         /*
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.
237          */
238         if (cport_id == CPORT_ID_BAD) {
239                 pr_err("request to send inbound data buffer\n");
240                 return ERR_PTR(-EINVAL);
241         }
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);
245         }
246         /* OK, the destination is fine; record it in the transfer buffer */
247         *transfer_buffer = cport_id;
248
249         /* Find a free urb */
250         urb = next_free_urb(es1, gfp_mask);
251         if (!urb)
252                 return ERR_PTR(-ENOMEM);
253
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);
259         if (retval) {
260                 pr_err("error %d submitting URB\n", retval);
261                 free_urb(es1, urb);
262                 return ERR_PTR(retval);
263         }
264
265         return conceal_urb(urb);
266 }
267
268 /*
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.
273  */
274 static void buffer_cancel(void *cookie)
275 {
276
277         /*
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.
281          */
282         if (cookie)
283                 usb_kill_urb(reveal_urb(cookie));
284 }
285
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,
291 };
292
293 /* Common function to report consistent warnings based on URB status */
294 static int check_urb_status(struct urb *urb)
295 {
296         struct device *dev = &urb->dev->dev;
297         int status = urb->status;
298
299         switch (status) {
300         case 0:
301                 return 0;
302
303         case -EOVERFLOW:
304                 dev_err(dev, "%s: overflow actual length is %d\n",
305                         __func__, urb->actual_length);
306         case -ECONNRESET:
307         case -ENOENT:
308         case -ESHUTDOWN:
309         case -EILSEQ:
310         case -EPROTO:
311                 /* device is gone, stop sending */
312                 return status;
313         }
314         dev_err(dev, "%s: unknown status %d\n", __func__, status);
315
316         return -EAGAIN;
317 }
318
319 static void ap_disconnect(struct usb_interface *interface)
320 {
321         struct es1_ap_dev *es1;
322         struct usb_device *udev;
323         int i;
324
325         es1 = usb_get_intfdata(interface);
326         if (!es1)
327                 return;
328
329         /* Tear down everything! */
330         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
331                 struct urb *urb = es1->cport_out_urb[i];
332
333                 if (!urb)
334                         break;
335                 usb_kill_urb(urb);
336                 usb_free_urb(urb);
337                 es1->cport_out_urb[i] = NULL;
338                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
339         }
340
341         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
342                 struct urb *urb = es1->cport_in_urb[i];
343
344                 if (!urb)
345                         break;
346                 usb_kill_urb(urb);
347                 usb_free_urb(urb);
348                 kfree(es1->cport_in_buffer[i]);
349                 es1->cport_in_buffer[i] = NULL;
350         }
351
352         usb_kill_urb(es1->svc_urb);
353         usb_free_urb(es1->svc_urb);
354         es1->svc_urb = NULL;
355         kfree(es1->svc_buffer);
356         es1->svc_buffer = NULL;
357
358         usb_set_intfdata(interface, NULL);
359         udev = es1->usb_dev;
360         greybus_remove_hd(es1->hd);
361
362         usb_put_dev(udev);
363 }
364
365 /* Callback for when we get a SVC message */
366 static void svc_in_callback(struct urb *urb)
367 {
368         struct greybus_host_device *hd = urb->context;
369         struct device *dev = &urb->dev->dev;
370         int status = check_urb_status(urb);
371         int retval;
372
373         if (status) {
374                 if ((status == -EAGAIN) || (status == -EPROTO))
375                         goto exit;
376                 dev_err(dev, "urb svc in error %d (dropped)\n", status);
377                 return;
378         }
379
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.
382          */
383         greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length);
384
385 exit:
386         /* resubmit the urb to get more messages */
387         retval = usb_submit_urb(urb, GFP_ATOMIC);
388         if (retval)
389                 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
390 }
391
392 static void cport_in_callback(struct urb *urb)
393 {
394         struct greybus_host_device *hd = urb->context;
395         struct device *dev = &urb->dev->dev;
396         int status = check_urb_status(urb);
397         int retval;
398         u16 cport_id;
399         u8 *data;
400
401         if (status) {
402                 if ((status == -EAGAIN) || (status == -EPROTO))
403                         goto exit;
404                 dev_err(dev, "urb cport in error %d (dropped)\n", status);
405                 return;
406         }
407
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__);
411                 goto exit;
412         }
413
414         /*
415          * Our CPort number is the first byte of the data stream,
416          * the rest of the stream is "real" data
417          */
418         data = urb->transfer_buffer;
419         cport_id = (u16)data[0];
420         data = &data[1];
421
422         /* Pass this data to the greybus core */
423         greybus_data_rcvd(hd, cport_id, data, urb->actual_length - 1);
424
425 exit:
426         /* put our urb back in the request pool */
427         retval = usb_submit_urb(urb, GFP_ATOMIC);
428         if (retval)
429                 dev_err(dev, "%s: error %d in submitting urb.\n",
430                         __func__, retval);
431 }
432
433 static void cport_out_callback(struct urb *urb)
434 {
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;
439
440         /*
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.
444          */
445         data = urb->transfer_buffer + 1;
446         greybus_data_sent(hd, data, status);
447
448         free_urb(es1, urb);
449         /*
450          * Rest assured Greg, this craziness is getting fixed.
451          *
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.
460          *
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.
468          *
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.
473          * </rant>
474          */
475 }
476
477 /*
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
483  */
484 static int ap_probe(struct usb_interface *interface,
485                     const struct usb_device_id *id)
486 {
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;
496         int i;
497         u8 svc_interval = 0;
498
499         udev = usb_get_dev(interface_to_usbdev(interface));
500
501         hd = greybus_create_hd(&es1_driver, &udev->dev);
502         if (!hd) {
503                 usb_put_dev(udev);
504                 return -ENOMEM;
505         }
506
507         /* Fill in the buffer allocation constraints */
508         hd_buffer_constraints(hd);
509
510         es1 = hd_to_es1(hd);
511         es1->hd = hd;
512         es1->usb_intf = interface;
513         es1->usb_dev = udev;
514         spin_lock_init(&es1->cport_out_urb_lock);
515         usb_set_intfdata(interface, es1);
516
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;
520
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;
525
526                 if (usb_endpoint_is_int_in(endpoint)) {
527                         es1->svc_endpoint = endpoint->bEndpointAddress;
528                         svc_interval = endpoint->bInterval;
529                         int_in_found = true;
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;
536                 } else {
537                         dev_err(&udev->dev,
538                                 "Unknown endpoint type found, address %x\n",
539                                 endpoint->bEndpointAddress);
540                 }
541         }
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");
546                 goto error;
547         }
548
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)
552                 goto error;
553
554         es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL);
555         if (!es1->svc_urb)
556                 goto error;
557
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,
561                          hd, svc_interval);
562         retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL);
563         if (retval)
564                 goto error;
565
566         /* Allocate buffers for our cport in messages and start them up */
567         for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
568                 struct urb *urb;
569                 u8 *buffer;
570
571                 urb = usb_alloc_urb(0, GFP_KERNEL);
572                 if (!urb)
573                         goto error;
574                 buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
575                 if (!buffer)
576                         goto error;
577
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);
585                 if (retval)
586                         goto error;
587         }
588
589         /* Allocate urbs for our CPort OUT messages */
590         for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
591                 struct urb *urb;
592
593                 urb = usb_alloc_urb(0, GFP_KERNEL);
594                 if (!urb)
595                         goto error;
596
597                 es1->cport_out_urb[i] = urb;
598                 es1->cport_out_urb_busy[i] = false;     /* just to be anal */
599         }
600
601         return 0;
602 error:
603         ap_disconnect(interface);
604
605         return retval;
606 }
607
608 static struct usb_driver es1_ap_driver = {
609         .name =         "es1_ap_driver",
610         .probe =        ap_probe,
611         .disconnect =   ap_disconnect,
612         .id_table =     id_table,
613 };
614
615 module_usb_driver(es1_ap_driver);
616
617 MODULE_LICENSE("GPL");
618 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");