Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / drivers / usb / gadget / core.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2003
4  * Gerry Hamel, geh@ti.com, Texas Instruments
5  *
6  * Based on
7  * linux/drivers/usbd/usbd.c.c - USB Device Core Layer
8  *
9  * Copyright (c) 2000, 2001, 2002 Lineo
10  * Copyright (c) 2001 Hewlett Packard
11  *
12  * By:
13  *      Stuart Lynne <sl@lineo.com>,
14  *      Tom Rushworth <tbr@lineo.com>,
15  *      Bruce Balden <balden@lineo.com>
16  */
17
18 #include <log.h>
19 #include <malloc.h>
20 #include <serial.h>
21 #include <usbdevice.h>
22
23 #define MAX_INTERFACES 2
24
25
26 int maxstrings = 20;
27
28 /* Global variables ************************************************************************** */
29
30 struct usb_string_descriptor **usb_strings;
31
32 int usb_devices;
33
34 extern struct usb_function_driver ep0_driver;
35
36 int registered_functions;
37 int registered_devices;
38
39 char *usbd_device_events[] = {
40         "DEVICE_UNKNOWN",
41         "DEVICE_INIT",
42         "DEVICE_CREATE",
43         "DEVICE_HUB_CONFIGURED",
44         "DEVICE_RESET",
45         "DEVICE_ADDRESS_ASSIGNED",
46         "DEVICE_CONFIGURED",
47         "DEVICE_SET_INTERFACE",
48         "DEVICE_SET_FEATURE",
49         "DEVICE_CLEAR_FEATURE",
50         "DEVICE_DE_CONFIGURED",
51         "DEVICE_BUS_INACTIVE",
52         "DEVICE_BUS_ACTIVITY",
53         "DEVICE_POWER_INTERRUPTION",
54         "DEVICE_HUB_RESET",
55         "DEVICE_DESTROY",
56         "DEVICE_FUNCTION_PRIVATE",
57 };
58
59 char *usbd_device_states[] = {
60         "STATE_INIT",
61         "STATE_CREATED",
62         "STATE_ATTACHED",
63         "STATE_POWERED",
64         "STATE_DEFAULT",
65         "STATE_ADDRESSED",
66         "STATE_CONFIGURED",
67         "STATE_UNKNOWN",
68 };
69
70 char *usbd_device_requests[] = {
71         "GET STATUS",           /* 0 */
72         "CLEAR FEATURE",        /* 1 */
73         "RESERVED",             /* 2 */
74         "SET FEATURE",          /* 3 */
75         "RESERVED",             /* 4 */
76         "SET ADDRESS",          /* 5 */
77         "GET DESCRIPTOR",       /* 6 */
78         "SET DESCRIPTOR",       /* 7 */
79         "GET CONFIGURATION",    /* 8 */
80         "SET CONFIGURATION",    /* 9 */
81         "GET INTERFACE",        /* 10 */
82         "SET INTERFACE",        /* 11 */
83         "SYNC FRAME",           /* 12 */
84 };
85
86 char *usbd_device_descriptors[] = {
87         "UNKNOWN",              /* 0 */
88         "DEVICE",               /* 1 */
89         "CONFIG",               /* 2 */
90         "STRING",               /* 3 */
91         "INTERFACE",            /* 4 */
92         "ENDPOINT",             /* 5 */
93         "DEVICE QUALIFIER",     /* 6 */
94         "OTHER SPEED",          /* 7 */
95         "INTERFACE POWER",      /* 8 */
96 };
97
98 char *usbd_device_status[] = {
99         "USBD_OPENING",
100         "USBD_OK",
101         "USBD_SUSPENDED",
102         "USBD_CLOSING",
103 };
104
105
106 /* Descriptor support functions ************************************************************** */
107
108
109 /**
110  * usbd_get_string - find and return a string descriptor
111  * @index: string index to return
112  *
113  * Find an indexed string and return a pointer to a it.
114  */
115 struct usb_string_descriptor *usbd_get_string (__u8 index)
116 {
117         if (index >= maxstrings) {
118                 return NULL;
119         }
120         return usb_strings[index];
121 }
122
123
124 /* Access to device descriptor functions ***************************************************** */
125
126
127 /* *
128  * usbd_device_configuration_instance - find a configuration instance for this device
129  * @device:
130  * @configuration: index to configuration, 0 - N-1
131  *
132  * Get specifed device configuration. Index should be bConfigurationValue-1.
133  */
134 static struct usb_configuration_instance *usbd_device_configuration_instance (struct usb_device_instance *device,
135                 unsigned int port, unsigned int configuration)
136 {
137         if (configuration >= device->configurations)
138                 return NULL;
139
140         return device->configuration_instance_array + configuration;
141 }
142
143
144 /* *
145  * usbd_device_interface_instance
146  * @device:
147  * @configuration: index to configuration, 0 - N-1
148  * @interface: index to interface
149  *
150  * Return the specified interface descriptor for the specified device.
151  */
152 struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *device, int port, int configuration, int interface)
153 {
154         struct usb_configuration_instance *configuration_instance;
155
156         if ((configuration_instance = usbd_device_configuration_instance (device, port, configuration)) == NULL) {
157                 return NULL;
158         }
159         if (interface >= configuration_instance->interfaces) {
160                 return NULL;
161         }
162         return configuration_instance->interface_instance_array + interface;
163 }
164
165 /* *
166  * usbd_device_alternate_descriptor_list
167  * @device:
168  * @configuration: index to configuration, 0 - N-1
169  * @interface: index to interface
170  * @alternate: alternate setting
171  *
172  * Return the specified alternate descriptor for the specified device.
173  */
174 struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *device, int port, int configuration, int interface, int alternate)
175 {
176         struct usb_interface_instance *interface_instance;
177
178         if ((interface_instance = usbd_device_interface_instance (device, port, configuration, interface)) == NULL) {
179                 return NULL;
180         }
181
182         if (alternate >= interface_instance->alternates) {
183                 return NULL;
184         }
185
186         return interface_instance->alternates_instance_array + alternate;
187 }
188
189
190 /* *
191  * usbd_device_device_descriptor
192  * @device: which device
193  * @configuration: index to configuration, 0 - N-1
194  * @port: which port
195  *
196  * Return the specified configuration descriptor for the specified device.
197  */
198 struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *device, int port)
199 {
200         return (device->device_descriptor);
201 }
202
203 /**
204  * usbd_device_configuration_descriptor
205  * @device: which device
206  * @port: which port
207  * @configuration: index to configuration, 0 - N-1
208  *
209  * Return the specified configuration descriptor for the specified device.
210  */
211 struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct
212                                                                            usb_device_instance
213                                                                            *device, int port, int configuration)
214 {
215         struct usb_configuration_instance *configuration_instance;
216         if (!(configuration_instance = usbd_device_configuration_instance (device, port, configuration))) {
217                 return NULL;
218         }
219         return (configuration_instance->configuration_descriptor);
220 }
221
222
223 /**
224  * usbd_device_interface_descriptor
225  * @device: which device
226  * @port: which port
227  * @configuration: index to configuration, 0 - N-1
228  * @interface: index to interface
229  * @alternate: alternate setting
230  *
231  * Return the specified interface descriptor for the specified device.
232  */
233 struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance
234                                                                    *device, int port, int configuration, int interface, int alternate)
235 {
236         struct usb_interface_instance *interface_instance;
237         if (!(interface_instance = usbd_device_interface_instance (device, port, configuration, interface))) {
238                 return NULL;
239         }
240         if ((alternate < 0) || (alternate >= interface_instance->alternates)) {
241                 return NULL;
242         }
243         return (interface_instance->alternates_instance_array[alternate].interface_descriptor);
244 }
245
246 /**
247  * usbd_device_endpoint_descriptor_index
248  * @device: which device
249  * @port: which port
250  * @configuration: index to configuration, 0 - N-1
251  * @interface: index to interface
252  * @alternate: index setting
253  * @index: which index
254  *
255  * Return the specified endpoint descriptor for the specified device.
256  */
257 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance
258                                                                        *device, int port, int configuration, int interface, int alternate, int index)
259 {
260         struct usb_alternate_instance *alternate_instance;
261
262         if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
263                 return NULL;
264         }
265         if (index >= alternate_instance->endpoints) {
266                 return NULL;
267         }
268         return *(alternate_instance->endpoints_descriptor_array + index);
269 }
270
271
272 /**
273  * usbd_device_endpoint_transfersize
274  * @device: which device
275  * @port: which port
276  * @configuration: index to configuration, 0 - N-1
277  * @interface: index to interface
278  * @index: which index
279  *
280  * Return the specified endpoint transfer size;
281  */
282 int usbd_device_endpoint_transfersize (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int index)
283 {
284         struct usb_alternate_instance *alternate_instance;
285
286         if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
287                 return 0;
288         }
289         if (index >= alternate_instance->endpoints) {
290                 return 0;
291         }
292         return *(alternate_instance->endpoint_transfersize_array + index);
293 }
294
295
296 /**
297  * usbd_device_endpoint_descriptor
298  * @device: which device
299  * @port: which port
300  * @configuration: index to configuration, 0 - N-1
301  * @interface: index to interface
302  * @alternate: alternate setting
303  * @endpoint: which endpoint
304  *
305  * Return the specified endpoint descriptor for the specified device.
306  */
307 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int endpoint)
308 {
309         struct usb_endpoint_descriptor *endpoint_descriptor;
310         int i;
311
312         for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
313                 if (endpoint_descriptor->bEndpointAddress == endpoint) {
314                         return endpoint_descriptor;
315                 }
316         }
317         return NULL;
318 }
319
320 /**
321  * usbd_endpoint_halted
322  * @device: point to struct usb_device_instance
323  * @endpoint: endpoint to check
324  *
325  * Return non-zero if endpoint is halted.
326  */
327 int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint)
328 {
329         return (device->status == USB_STATUS_HALT);
330 }
331
332
333 /**
334  * usbd_rcv_complete - complete a receive
335  * @endpoint:
336  * @len:
337  * @urb_bad:
338  *
339  * Called from rcv interrupt to complete.
340  */
341 void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad)
342 {
343         if (endpoint) {
344                 struct urb *rcv_urb;
345
346                 /*usbdbg("len: %d urb: %p\n", len, endpoint->rcv_urb); */
347
348                 /* if we had an urb then update actual_length, dispatch if neccessary */
349                 if ((rcv_urb = endpoint->rcv_urb)) {
350
351                         /*usbdbg("actual: %d buffer: %d\n", */
352                         /*rcv_urb->actual_length, rcv_urb->buffer_length); */
353
354                         /* check the urb is ok, are we adding data less than the packetsize */
355                         if (!urb_bad && (len <= endpoint->rcv_packetSize)) {
356                           /*usbdbg("updating actual_length by %d\n",len); */
357
358                                 /* increment the received data size */
359                                 rcv_urb->actual_length += len;
360
361                         } else {
362                                 usberr(" RECV_ERROR actual: %d buffer: %d urb_bad: %d\n",
363                                        rcv_urb->actual_length, rcv_urb->buffer_length, urb_bad);
364
365                                 rcv_urb->actual_length = 0;
366                                 rcv_urb->status = RECV_ERROR;
367                         }
368                 } else {
369                         usberr("no rcv_urb!");
370                 }
371         } else {
372                 usberr("no endpoint!");
373         }
374
375 }
376
377 /**
378  * usbd_tx_complete - complete a transmit
379  * @endpoint:
380  * @resetart:
381  *
382  * Called from tx interrupt to complete.
383  */
384 void usbd_tx_complete (struct usb_endpoint_instance *endpoint)
385 {
386         if (endpoint) {
387                 struct urb *tx_urb;
388
389                 /* if we have a tx_urb advance or reset, finish if complete */
390                 if ((tx_urb = endpoint->tx_urb)) {
391                         int sent = endpoint->last;
392                         endpoint->sent += sent;
393                         endpoint->last -= sent;
394
395                         if( (endpoint->tx_urb->actual_length - endpoint->sent) <= 0 ) {
396                                 tx_urb->actual_length = 0;
397                                 endpoint->sent = 0;
398                                 endpoint->last = 0;
399
400                                 /* Remove from active, save for re-use */
401                                 urb_detach(tx_urb);
402                                 urb_append(&endpoint->done, tx_urb);
403                                 /*usbdbg("done->next %p, tx_urb %p, done %p", */
404                                 /*       endpoint->done.next, tx_urb, &endpoint->done); */
405
406                                 endpoint->tx_urb = first_urb_detached(&endpoint->tx);
407                                 if( endpoint->tx_urb ) {
408                                         endpoint->tx_queue--;
409                                         usbdbg("got urb from tx list");
410                                 }
411                                 if( !endpoint->tx_urb ) {
412                                         /*usbdbg("taking urb from done list"); */
413                                         endpoint->tx_urb = first_urb_detached(&endpoint->done);
414                                 }
415                                 if( !endpoint->tx_urb ) {
416                                         usbdbg("allocating new urb for tx_urb");
417                                         endpoint->tx_urb = usbd_alloc_urb(tx_urb->device, endpoint);
418                                 }
419                         }
420                 }
421         }
422 }
423
424 /* URB linked list functions ***************************************************** */
425
426 /*
427  * Initialize an urb_link to be a single element list.
428  * If the urb_link is being used as a distinguished list head
429  * the list is empty when the head is the only link in the list.
430  */
431 void urb_link_init (urb_link * ul)
432 {
433         if (ul) {
434                 ul->prev = ul->next = ul;
435         }
436 }
437
438 /*
439  * Detach an urb_link from a list, and set it
440  * up as a single element list, so no dangling
441  * pointers can be followed, and so it can be
442  * joined to another list if so desired.
443  */
444 void urb_detach (struct urb *urb)
445 {
446         if (urb) {
447                 urb_link *ul = &urb->link;
448                 ul->next->prev = ul->prev;
449                 ul->prev->next = ul->next;
450                 urb_link_init (ul);
451         }
452 }
453
454 /*
455  * Return the first urb_link in a list with a distinguished
456  * head "hd", or NULL if the list is empty.  This will also
457  * work as a predicate, returning NULL if empty, and non-NULL
458  * otherwise.
459  */
460 urb_link *first_urb_link (urb_link * hd)
461 {
462         urb_link *nx;
463         if (NULL != hd && NULL != (nx = hd->next) && nx != hd) {
464                 /* There is at least one element in the list */
465                 /* (besides the distinguished head). */
466                 return (nx);
467         }
468         /* The list is empty */
469         return (NULL);
470 }
471
472 /*
473  * Return the first urb in a list with a distinguished
474  * head "hd", or NULL if the list is empty.
475  */
476 struct urb *first_urb (urb_link * hd)
477 {
478         urb_link *nx;
479         if (NULL == (nx = first_urb_link (hd))) {
480                 /* The list is empty */
481                 return (NULL);
482         }
483         return (p2surround (struct urb, link, nx));
484 }
485
486 /*
487  * Detach and return the first urb in a list with a distinguished
488  * head "hd", or NULL if the list is empty.
489  *
490  */
491 struct urb *first_urb_detached (urb_link * hd)
492 {
493         struct urb *urb;
494         if ((urb = first_urb (hd))) {
495                 urb_detach (urb);
496         }
497         return urb;
498 }
499
500
501 /*
502  * Append an urb_link (or a whole list of
503  * urb_links) to the tail of another list
504  * of urb_links.
505  */
506 void urb_append (urb_link * hd, struct urb *urb)
507 {
508         if (hd && urb) {
509                 urb_link *new = &urb->link;
510
511                 /* This allows the new urb to be a list of urbs, */
512                 /* with new pointing at the first, but the link */
513                 /* must be initialized. */
514                 /* Order is important here... */
515                 urb_link *pul = hd->prev;
516                 new->prev->next = hd;
517                 hd->prev = new->prev;
518                 new->prev = pul;
519                 pul->next = new;
520         }
521 }
522
523 /* URB create/destroy functions ***************************************************** */
524
525 /**
526  * usbd_alloc_urb - allocate an URB appropriate for specified endpoint
527  * @device: device instance
528  * @endpoint: endpoint
529  *
530  * Allocate an urb structure. The usb device urb structure is used to
531  * contain all data associated with a transfer, including a setup packet for
532  * control transfers.
533  *
534  * NOTE: endpoint_address MUST contain a direction flag.
535  */
536 struct urb *usbd_alloc_urb (struct usb_device_instance *device,
537                             struct usb_endpoint_instance *endpoint)
538 {
539         struct urb *urb;
540
541         if (!(urb = (struct urb *) malloc (sizeof (struct urb)))) {
542                 usberr (" F A T A L:  malloc(%zu) FAILED!!!!",
543                         sizeof (struct urb));
544                 return NULL;
545         }
546
547         /* Fill in known fields */
548         memset (urb, 0, sizeof (struct urb));
549         urb->endpoint = endpoint;
550         urb->device = device;
551         urb->buffer = (u8 *) urb->buffer_data;
552         urb->buffer_length = sizeof (urb->buffer_data);
553
554         urb_link_init (&urb->link);
555
556         return urb;
557 }
558
559 /**
560  * usbd_dealloc_urb - deallocate an URB and associated buffer
561  * @urb: pointer to an urb structure
562  *
563  * Deallocate an urb structure and associated data.
564  */
565 void usbd_dealloc_urb (struct urb *urb)
566 {
567         if (urb) {
568                 free (urb);
569         }
570 }
571
572 /* Event signaling functions ***************************************************** */
573
574 /**
575  * usbd_device_event - called to respond to various usb events
576  * @device: pointer to struct device
577  * @event: event to respond to
578  *
579  * Used by a Bus driver to indicate an event.
580  */
581 void usbd_device_event_irq (struct usb_device_instance *device, usb_device_event_t event, int data)
582 {
583         usb_device_state_t state;
584
585         if (!device || !device->bus) {
586                 usberr("(%p,%d) NULL device or device->bus", device, event);
587                 return;
588         }
589
590         state = device->device_state;
591
592         usbinfo("%s", usbd_device_events[event]);
593
594         switch (event) {
595         case DEVICE_UNKNOWN:
596                 break;
597         case DEVICE_INIT:
598                 device->device_state = STATE_INIT;
599                 break;
600
601         case DEVICE_CREATE:
602                 device->device_state = STATE_ATTACHED;
603                 break;
604
605         case DEVICE_HUB_CONFIGURED:
606                 device->device_state = STATE_POWERED;
607                 break;
608
609         case DEVICE_RESET:
610                 device->device_state = STATE_DEFAULT;
611                 device->address = 0;
612                 break;
613
614         case DEVICE_ADDRESS_ASSIGNED:
615                 device->device_state = STATE_ADDRESSED;
616                 break;
617
618         case DEVICE_CONFIGURED:
619                 device->device_state = STATE_CONFIGURED;
620                 break;
621
622         case DEVICE_DE_CONFIGURED:
623                 device->device_state = STATE_ADDRESSED;
624                 break;
625
626         case DEVICE_BUS_INACTIVE:
627                 if (device->status != USBD_CLOSING) {
628                         device->status = USBD_SUSPENDED;
629                 }
630                 break;
631         case DEVICE_BUS_ACTIVITY:
632                 if (device->status != USBD_CLOSING) {
633                         device->status = USBD_OK;
634                 }
635                 break;
636
637         case DEVICE_SET_INTERFACE:
638                 break;
639         case DEVICE_SET_FEATURE:
640                 break;
641         case DEVICE_CLEAR_FEATURE:
642                 break;
643
644         case DEVICE_POWER_INTERRUPTION:
645                 device->device_state = STATE_POWERED;
646                 break;
647         case DEVICE_HUB_RESET:
648                 device->device_state = STATE_ATTACHED;
649                 break;
650         case DEVICE_DESTROY:
651                 device->device_state = STATE_UNKNOWN;
652                 break;
653
654         case DEVICE_FUNCTION_PRIVATE:
655                 break;
656
657         default:
658                 usbdbg("event %d - not handled",event);
659                 break;
660         }
661         debug("%s event: %d oldstate: %d newstate: %d status: %d address: %d",
662                 device->name, event, state,
663                 device->device_state, device->status, device->address);
664
665         /* tell the bus interface driver */
666         if( device->event ) {
667                 /* usbdbg("calling device->event"); */
668                 device->event(device, event, data);
669         }
670 }