[efi] Run at TPL_CALLBACK to protect against UEFI timers
[ipxe.git] / src / interface / efi / efi_usb.c
1 /*
2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <ipxe/efi/efi.h>
33 #include <ipxe/efi/efi_utils.h>
34 #include <ipxe/efi/efi_driver.h>
35 #include <ipxe/efi/efi_usb.h>
36 #include <ipxe/usb.h>
37
38 /** @file
39 *
40 * EFI USB I/O PROTOCOL
41 *
42 */
43
44 /**
45 * Transcribe data direction (for debugging)
46 *
47 * @v direction Data direction
48 * @ret text Transcribed data direction
49 */
50 static const char * efi_usb_direction_name ( EFI_USB_DATA_DIRECTION direction ){
51
52 switch ( direction ) {
53 case EfiUsbDataIn: return "in";
54 case EfiUsbDataOut: return "out";
55 case EfiUsbNoData: return "none";
56 default: return "<UNKNOWN>";
57 }
58 }
59
60 /******************************************************************************
61 *
62 * Endpoints
63 *
64 ******************************************************************************
65 */
66
67 /**
68 * Poll USB bus (from endpoint event timer)
69 *
70 * @v event EFI event
71 * @v context EFI USB endpoint
72 */
73 static VOID EFIAPI efi_usb_timer ( EFI_EVENT event __unused,
74 VOID *context ) {
75 struct efi_usb_endpoint *usbep = context;
76 struct usb_bus *bus = usbep->usbintf->usbdev->usb->port->hub->bus;
77
78 /* Poll bus */
79 usb_poll ( bus );
80
81 /* Refill endpoint */
82 usb_refill ( &usbep->ep );
83 }
84
85 /**
86 * Get endpoint MTU
87 *
88 * @v usbintf EFI USB interface
89 * @v endpoint Endpoint address
90 * @ret mtu Endpoint MTU, or negative error
91 */
92 static int efi_usb_mtu ( struct efi_usb_interface *usbintf,
93 unsigned int endpoint ) {
94 struct efi_usb_device *usbdev = usbintf->usbdev;
95 struct usb_interface_descriptor *interface;
96 struct usb_endpoint_descriptor *desc;
97
98 /* Locate cached interface descriptor */
99 interface = usb_interface_descriptor ( usbdev->config,
100 usbintf->interface,
101 usbintf->alternate );
102 if ( ! interface ) {
103 DBGC ( usbdev, "USBDEV %s alt %d has no interface descriptor\n",
104 usbintf->name, usbintf->alternate );
105 return -ENOENT;
106 }
107
108 /* Locate and copy cached endpoint descriptor */
109 for_each_interface_descriptor ( desc, usbdev->config, interface ) {
110 if ( ( desc->header.type == USB_ENDPOINT_DESCRIPTOR ) &&
111 ( desc->endpoint == endpoint ) )
112 return USB_ENDPOINT_MTU ( le16_to_cpu ( desc->sizes ) );
113 }
114
115 DBGC ( usbdev, "USBDEV %s alt %d ep %02x has no descriptor\n",
116 usbintf->name, usbintf->alternate, endpoint );
117 return -ENOENT;
118 }
119
120 /**
121 * Open endpoint
122 *
123 * @v usbintf EFI USB interface
124 * @v endpoint Endpoint address
125 * @v attributes Endpoint attributes
126 * @v interval Interval (in milliseconds)
127 * @v driver Driver operations
128 * @ret rc Return status code
129 */
130 static int efi_usb_open ( struct efi_usb_interface *usbintf,
131 unsigned int endpoint, unsigned int attributes,
132 unsigned int interval,
133 struct usb_endpoint_driver_operations *driver ) {
134 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
135 struct efi_usb_device *usbdev = usbintf->usbdev;
136 struct efi_usb_endpoint *usbep;
137 unsigned int index = USB_ENDPOINT_IDX ( endpoint );
138 int mtu;
139 EFI_STATUS efirc;
140 int rc;
141
142 /* Get endpoint MTU */
143 mtu = efi_usb_mtu ( usbintf, endpoint );
144 if ( mtu < 0 ) {
145 rc = mtu;
146 goto err_mtu;
147 }
148
149 /* Allocate and initialise structure */
150 usbep = zalloc ( sizeof ( *usbep ) );
151 if ( ! usbep ) {
152 rc = -ENOMEM;
153 goto err_alloc;
154 }
155 usbep->usbintf = usbintf;
156 usb_endpoint_init ( &usbep->ep, usbdev->usb, driver );
157 usb_endpoint_describe ( &usbep->ep, endpoint, attributes, mtu, 0,
158 ( interval << 3 /* microframes */ ) );
159
160 /* Open endpoint */
161 if ( ( rc = usb_endpoint_open ( &usbep->ep ) ) != 0 ) {
162 DBGC ( usbdev, "USBDEV %s %s could not open: %s\n",
163 usbintf->name, usb_endpoint_name ( &usbep->ep ),
164 strerror ( rc ) );
165 goto err_open;
166 }
167
168 /* Record opened endpoint */
169 usbintf->endpoint[index] = usbep;
170 DBGC ( usbdev, "USBDEV %s %s opened\n",
171 usbintf->name, usb_endpoint_name ( &usbep->ep ) );
172
173 /* Create event */
174 if ( ( efirc = bs->CreateEvent ( ( EVT_TIMER | EVT_NOTIFY_SIGNAL ),
175 TPL_CALLBACK, efi_usb_timer, usbep,
176 &usbep->event ) ) != 0 ) {
177 rc = -EEFI ( efirc );
178 DBGC ( usbdev, "USBDEV %s %s could not create event: %s\n",
179 usbintf->name, usb_endpoint_name ( &usbep->ep ),
180 strerror ( rc ) );
181 goto err_event;
182 }
183
184 return 0;
185
186 bs->CloseEvent ( usbep->event );
187 err_event:
188 usbintf->endpoint[index] = usbep;
189 usb_endpoint_close ( &usbep->ep );
190 err_open:
191 free ( usbep );
192 err_alloc:
193 err_mtu:
194 return rc;
195 }
196
197 /**
198 * Close endpoint
199 *
200 * @v usbep EFI USB endpoint
201 */
202 static void efi_usb_close ( struct efi_usb_endpoint *usbep ) {
203 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
204 struct efi_usb_interface *usbintf = usbep->usbintf;
205 struct efi_usb_device *usbdev = usbintf->usbdev;
206 unsigned int index = USB_ENDPOINT_IDX ( usbep->ep.address );
207
208 /* Sanity check */
209 assert ( usbintf->endpoint[index] == usbep );
210
211 /* Cancel timer (if applicable) and close event */
212 bs->SetTimer ( usbep->event, TimerCancel, 0 );
213 bs->CloseEvent ( usbep->event );
214
215 /* Close endpoint */
216 usb_endpoint_close ( &usbep->ep );
217 DBGC ( usbdev, "USBDEV %s %s closed\n",
218 usbintf->name, usb_endpoint_name ( &usbep->ep ) );
219
220 /* Free endpoint */
221 free ( usbep );
222
223 /* Record closed endpoint */
224 usbintf->endpoint[index] = NULL;
225 }
226
227 /**
228 * Close all endpoints
229 *
230 * @v usbintf EFI USB interface
231 */
232 static void efi_usb_close_all ( struct efi_usb_interface *usbintf ) {
233 struct efi_usb_endpoint *usbep;
234 unsigned int i;
235
236 for ( i = 0 ; i < ( sizeof ( usbintf->endpoint ) /
237 sizeof ( usbintf->endpoint[0] ) ) ; i++ ) {
238 usbep = usbintf->endpoint[i];
239 if ( usbep )
240 efi_usb_close ( usbep );
241 }
242 }
243
244 /**
245 * Complete synchronous transfer
246 *
247 * @v ep USB endpoint
248 * @v iobuf I/O buffer
249 * @v rc Completion status code
250 */
251 static void efi_usb_sync_complete ( struct usb_endpoint *ep,
252 struct io_buffer *iobuf __unused, int rc ) {
253 struct efi_usb_endpoint *usbep =
254 container_of ( ep, struct efi_usb_endpoint, ep );
255
256 /* Record completion status */
257 usbep->rc = rc;
258 }
259
260 /** Synchronous endpoint operations */
261 static struct usb_endpoint_driver_operations efi_usb_sync_driver = {
262 .complete = efi_usb_sync_complete,
263 };
264
265 /**
266 * Perform synchronous transfer
267 *
268 * @v usbintf USB endpoint
269 * @v endpoint Endpoint address
270 * @v attributes Endpoint attributes
271 * @v timeout Timeout (in milliseconds)
272 * @v data Data buffer
273 * @v len Length of data buffer
274 * @ret rc Return status code
275 */
276 static int efi_usb_sync_transfer ( struct efi_usb_interface *usbintf,
277 unsigned int endpoint,
278 unsigned int attributes,
279 unsigned int timeout,
280 void *data, size_t *len ) {
281 struct efi_usb_device *usbdev = usbintf->usbdev;
282 struct efi_usb_endpoint *usbep;
283 struct io_buffer *iobuf;
284 unsigned int index = USB_ENDPOINT_IDX ( endpoint );
285 unsigned int i;
286 int rc;
287
288 /* Open endpoint, if applicable */
289 if ( ( ! usbintf->endpoint[index] ) &&
290 ( ( rc = efi_usb_open ( usbintf, endpoint, attributes, 0,
291 &efi_usb_sync_driver ) ) != 0 ) ) {
292 goto err_open;
293 }
294 usbep = usbintf->endpoint[index];
295
296 /* Allocate and construct I/O buffer */
297 iobuf = alloc_iob ( *len );
298 if ( ! iobuf ) {
299 rc = -ENOMEM;
300 goto err_alloc;
301 }
302 iob_put ( iobuf, *len );
303 if ( ! ( endpoint & USB_ENDPOINT_IN ) )
304 memcpy ( iobuf->data, data, *len );
305
306 /* Initialise completion status */
307 usbep->rc = -EINPROGRESS;
308
309 /* Enqueue transfer */
310 if ( ( rc = usb_stream ( &usbep->ep, iobuf, 0 ) ) != 0 ) {
311 DBGC ( usbdev, "USBDEV %s %s could not enqueue: %s\n",
312 usbintf->name, usb_endpoint_name ( &usbep->ep ),
313 strerror ( rc ) );
314 goto err_stream;
315 }
316
317 /* Wait for completion */
318 rc = -ETIMEDOUT;
319 for ( i = 0 ; ( ( timeout == 0 ) || ( i < timeout ) ) ; i++ ) {
320
321 /* Poll bus */
322 usb_poll ( usbdev->usb->port->hub->bus );
323
324 /* Check for completion */
325 if ( usbep->rc != -EINPROGRESS ) {
326 rc = usbep->rc;
327 break;
328 }
329
330 /* Delay */
331 mdelay ( 1 );
332 }
333
334 /* Check for errors */
335 if ( rc != 0 ) {
336 DBGC ( usbdev, "USBDEV %s %s failed: %s\n", usbintf->name,
337 usb_endpoint_name ( &usbep->ep ), strerror ( rc ) );
338 goto err_completion;
339 }
340
341 /* Copy completion to data buffer, if applicable */
342 assert ( iob_len ( iobuf ) <= *len );
343 if ( endpoint & USB_ENDPOINT_IN )
344 memcpy ( data, iobuf->data, iob_len ( iobuf ) );
345 *len = iob_len ( iobuf );
346
347 /* Free I/O buffer */
348 free_iob ( iobuf );
349
350 /* Leave endpoint open */
351 return 0;
352
353 err_completion:
354 err_stream:
355 free_iob ( iobuf );
356 err_alloc:
357 efi_usb_close ( usbep );
358 err_open:
359 return EFIRC ( rc );
360 }
361
362 /**
363 * Complete asynchronous transfer
364 *
365 * @v ep USB endpoint
366 * @v iobuf I/O buffer
367 * @v rc Completion status code
368 */
369 static void efi_usb_async_complete ( struct usb_endpoint *ep,
370 struct io_buffer *iobuf, int rc ) {
371 struct efi_usb_endpoint *usbep =
372 container_of ( ep, struct efi_usb_endpoint, ep );
373 UINT32 status;
374
375 /* Ignore packets cancelled when the endpoint closes */
376 if ( ! ep->open )
377 goto drop;
378
379 /* Construct status */
380 status = ( ( rc == 0 ) ? 0 : EFI_USB_ERR_STALL );
381
382 /* Report completion */
383 usbep->callback ( iobuf->data, iob_len ( iobuf ), usbep->context,
384 status );
385
386 drop:
387 /* Recycle I/O buffer */
388 usb_recycle ( &usbep->ep, iobuf );
389 }
390
391 /** Asynchronous endpoint operations */
392 static struct usb_endpoint_driver_operations efi_usb_async_driver = {
393 .complete = efi_usb_async_complete,
394 };
395
396 /**
397 * Start asynchronous transfer
398 *
399 * @v usbintf EFI USB interface
400 * @v endpoint Endpoint address
401 * @v interval Interval (in milliseconds)
402 * @v len Transfer length
403 * @v callback Callback function
404 * @v context Context for callback function
405 * @ret rc Return status code
406 */
407 static int efi_usb_async_start ( struct efi_usb_interface *usbintf,
408 unsigned int endpoint, unsigned int interval,
409 size_t len,
410 EFI_ASYNC_USB_TRANSFER_CALLBACK callback,
411 void *context ) {
412 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
413 struct efi_usb_device *usbdev = usbintf->usbdev;
414 struct efi_usb_endpoint *usbep;
415 unsigned int index = USB_ENDPOINT_IDX ( endpoint );
416 EFI_STATUS efirc;
417 int rc;
418
419 /* Open endpoint */
420 if ( ( rc = efi_usb_open ( usbintf, endpoint,
421 USB_ENDPOINT_ATTR_INTERRUPT, interval,
422 &efi_usb_async_driver ) ) != 0 )
423 goto err_open;
424 usbep = usbintf->endpoint[index];
425
426 /* Record callback parameters */
427 usbep->callback = callback;
428 usbep->context = context;
429
430 /* Prefill endpoint */
431 usb_refill_init ( &usbep->ep, 0, len, EFI_USB_ASYNC_FILL );
432 if ( ( rc = usb_prefill ( &usbep->ep ) ) != 0 ) {
433 DBGC ( usbdev, "USBDEV %s %s could not prefill: %s\n",
434 usbintf->name, usb_endpoint_name ( &usbep->ep ),
435 strerror ( rc ) );
436 goto err_prefill;
437 }
438
439 /* Start timer */
440 if ( ( efirc = bs->SetTimer ( usbep->event, TimerPeriodic,
441 ( interval * 10000 ) ) ) != 0 ) {
442 rc = -EEFI ( efirc );
443 DBGC ( usbdev, "USBDEV %s %s could not set timer: %s\n",
444 usbintf->name, usb_endpoint_name ( &usbep->ep ),
445 strerror ( rc ) );
446 goto err_timer;
447 }
448
449 return 0;
450
451 bs->SetTimer ( usbep->event, TimerCancel, 0 );
452 err_timer:
453 err_prefill:
454 efi_usb_close ( usbep );
455 err_open:
456 return rc;
457 }
458
459 /**
460 * Stop asynchronous transfer
461 *
462 * @v usbintf EFI USB interface
463 * @v endpoint Endpoint address
464 */
465 static void efi_usb_async_stop ( struct efi_usb_interface *usbintf,
466 unsigned int endpoint ) {
467 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
468 struct efi_usb_endpoint *usbep;
469 unsigned int index = USB_ENDPOINT_IDX ( endpoint );
470
471 /* Do nothing if endpoint is already closed */
472 usbep = usbintf->endpoint[index];
473 if ( ! usbep )
474 return;
475
476 /* Stop timer */
477 bs->SetTimer ( usbep->event, TimerCancel, 0 );
478
479 /* Close endpoint */
480 efi_usb_close ( usbep );
481 }
482
483 /******************************************************************************
484 *
485 * USB I/O protocol
486 *
487 ******************************************************************************
488 */
489
490 /**
491 * Perform control transfer
492 *
493 * @v usbio USB I/O protocol
494 * @v packet Setup packet
495 * @v direction Data direction
496 * @v timeout Timeout (in milliseconds)
497 * @v data Data buffer
498 * @v len Length of data
499 * @ret status Transfer status
500 * @ret efirc EFI status code
501 */
502 static EFI_STATUS EFIAPI
503 efi_usb_control_transfer ( EFI_USB_IO_PROTOCOL *usbio,
504 EFI_USB_DEVICE_REQUEST *packet,
505 EFI_USB_DATA_DIRECTION direction,
506 UINT32 timeout, VOID *data, UINTN len,
507 UINT32 *status ) {
508 struct efi_usb_interface *usbintf =
509 container_of ( usbio, struct efi_usb_interface, usbio );
510 struct efi_usb_device *usbdev = usbintf->usbdev;
511 unsigned int request = ( packet->RequestType |
512 USB_REQUEST_TYPE ( packet->Request ) );
513 unsigned int value = le16_to_cpu ( packet->Value );
514 unsigned int index = le16_to_cpu ( packet->Index );
515 int rc;
516
517 DBGC2 ( usbdev, "USBDEV %s control %04x:%04x:%04x:%04x %s %dms "
518 "%p+%zx\n", usbintf->name, request, value, index,
519 le16_to_cpu ( packet->Length ),
520 efi_usb_direction_name ( direction ), timeout, data,
521 ( ( size_t ) len ) );
522
523 /* Clear status */
524 *status = 0;
525
526 /* Block attempts to change the device configuration, since
527 * this is logically impossible to do given the constraints of
528 * the EFI_USB_IO_PROTOCOL design.
529 */
530 if ( ( request == USB_SET_CONFIGURATION ) &&
531 ( value != usbdev->config->config ) ) {
532 DBGC ( usbdev, "USBDEV %s cannot set configuration %d: not "
533 "logically possible\n", usbintf->name, index );
534 rc = -ENOTSUP;
535 goto err_change_config;
536 }
537
538 /* If we are selecting a new alternate setting then close all
539 * open endpoints.
540 */
541 if ( ( request == USB_SET_INTERFACE ) &&
542 ( value != usbintf->alternate ) )
543 efi_usb_close_all ( usbintf );
544
545 /* Issue control transfer */
546 if ( ( rc = usb_control ( usbdev->usb, request, value, index,
547 data, len ) ) != 0 ) {
548 DBGC ( usbdev, "USBDEV %s control %04x:%04x:%04x:%04x %p+%zx "
549 "failed: %s\n", usbintf->name, request, value, index,
550 le16_to_cpu ( packet->Length ), data, ( ( size_t ) len ),
551 strerror ( rc ) );
552 /* Assume that any error represents a stall */
553 *status = EFI_USB_ERR_STALL;
554 goto err_control;
555 }
556
557 /* Update alternate setting, if applicable */
558 if ( request == USB_SET_INTERFACE ) {
559 usbintf->alternate = value;
560 DBGC ( usbdev, "USBDEV %s alt %d selected\n",
561 usbintf->name, usbintf->alternate );
562 }
563
564 err_control:
565 err_change_config:
566 return EFIRC ( rc );
567 }
568
569 /**
570 * Perform bulk transfer
571 *
572 * @v usbio USB I/O protocol
573 * @v endpoint Endpoint address
574 * @v data Data buffer
575 * @v len Length of data
576 * @v timeout Timeout (in milliseconds)
577 * @ret status Transfer status
578 * @ret efirc EFI status code
579 */
580 static EFI_STATUS EFIAPI
581 efi_usb_bulk_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint, VOID *data,
582 UINTN *len, UINTN timeout, UINT32 *status ) {
583 struct efi_usb_interface *usbintf =
584 container_of ( usbio, struct efi_usb_interface, usbio );
585 struct efi_usb_device *usbdev = usbintf->usbdev;
586 size_t actual = *len;
587 int rc;
588
589 DBGC2 ( usbdev, "USBDEV %s bulk %s %p+%zx %dms\n", usbintf->name,
590 ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
591 ( ( size_t ) *len ), ( ( unsigned int ) timeout ) );
592
593 /* Clear status */
594 *status = 0;
595
596 /* Perform synchronous transfer */
597 if ( ( rc = efi_usb_sync_transfer ( usbintf, endpoint,
598 USB_ENDPOINT_ATTR_BULK, timeout,
599 data, &actual ) ) != 0 ) {
600 /* Assume that any error represents a timeout */
601 *status = EFI_USB_ERR_TIMEOUT;
602 return rc;
603 }
604
605 return 0;
606 }
607
608 /**
609 * Perform synchronous interrupt transfer
610 *
611 * @v usbio USB I/O protocol
612 * @v endpoint Endpoint address
613 * @v data Data buffer
614 * @v len Length of data
615 * @v timeout Timeout (in milliseconds)
616 * @ret status Transfer status
617 * @ret efirc EFI status code
618 */
619 static EFI_STATUS EFIAPI
620 efi_usb_sync_interrupt_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
621 VOID *data, UINTN *len, UINTN timeout,
622 UINT32 *status ) {
623 struct efi_usb_interface *usbintf =
624 container_of ( usbio, struct efi_usb_interface, usbio );
625 struct efi_usb_device *usbdev = usbintf->usbdev;
626 size_t actual = *len;
627 int rc;
628
629 DBGC2 ( usbdev, "USBDEV %s sync intr %s %p+%zx %dms\n", usbintf->name,
630 ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
631 ( ( size_t ) *len ), ( ( unsigned int ) timeout ) );
632
633 /* Clear status */
634 *status = 0;
635
636 /* Perform synchronous transfer */
637 if ( ( rc = efi_usb_sync_transfer ( usbintf, endpoint,
638 USB_ENDPOINT_ATTR_INTERRUPT,
639 timeout, data, &actual ) ) != 0 ) {
640 /* Assume that any error represents a timeout */
641 *status = EFI_USB_ERR_TIMEOUT;
642 return rc;
643 }
644
645 return 0;
646 }
647
648 /**
649 * Perform asynchronous interrupt transfer
650 *
651 * @v usbio USB I/O protocol
652 * @v endpoint Endpoint address
653 * @v start Start (rather than stop) transfer
654 * @v interval Polling interval (in milliseconds)
655 * @v len Data length
656 * @v callback Callback function
657 * @v context Context for callback function
658 * @ret efirc EFI status code
659 */
660 static EFI_STATUS EFIAPI
661 efi_usb_async_interrupt_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
662 BOOLEAN start, UINTN interval, UINTN len,
663 EFI_ASYNC_USB_TRANSFER_CALLBACK callback,
664 VOID *context ) {
665 struct efi_usb_interface *usbintf =
666 container_of ( usbio, struct efi_usb_interface, usbio );
667 struct efi_usb_device *usbdev = usbintf->usbdev;
668 int rc;
669
670 DBGC2 ( usbdev, "USBDEV %s async intr %s len %#zx int %d %p/%p\n",
671 usbintf->name,
672 ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ),
673 ( ( size_t ) len ), ( ( unsigned int ) interval ),
674 callback, context );
675
676 /* Start/stop transfer as applicable */
677 if ( start ) {
678
679 /* Start new transfer */
680 if ( ( rc = efi_usb_async_start ( usbintf, endpoint, interval,
681 len, callback,
682 context ) ) != 0 )
683 goto err_start;
684
685 } else {
686
687 /* Stop transfer */
688 efi_usb_async_stop ( usbintf, endpoint );
689
690 }
691
692 return 0;
693
694 err_start:
695 return EFIRC ( rc );
696 }
697
698 /**
699 * Perform synchronous isochronous transfer
700 *
701 * @v usbio USB I/O protocol
702 * @v endpoint Endpoint address
703 * @v data Data buffer
704 * @v len Length of data
705 * @ret status Transfer status
706 * @ret efirc EFI status code
707 */
708 static EFI_STATUS EFIAPI
709 efi_usb_isochronous_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
710 VOID *data, UINTN len, UINT32 *status ) {
711 struct efi_usb_interface *usbintf =
712 container_of ( usbio, struct efi_usb_interface, usbio );
713 struct efi_usb_device *usbdev = usbintf->usbdev;
714
715 DBGC2 ( usbdev, "USBDEV %s sync iso %s %p+%zx\n", usbintf->name,
716 ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
717 ( ( size_t ) len ) );
718
719 /* Clear status */
720 *status = 0;
721
722 /* Not supported */
723 return EFI_UNSUPPORTED;
724 }
725
726 /**
727 * Perform asynchronous isochronous transfers
728 *
729 * @v usbio USB I/O protocol
730 * @v endpoint Endpoint address
731 * @v data Data buffer
732 * @v len Length of data
733 * @v callback Callback function
734 * @v context Context for callback function
735 * @ret status Transfer status
736 * @ret efirc EFI status code
737 */
738 static EFI_STATUS EFIAPI
739 efi_usb_async_isochronous_transfer ( EFI_USB_IO_PROTOCOL *usbio, UINT8 endpoint,
740 VOID *data, UINTN len,
741 EFI_ASYNC_USB_TRANSFER_CALLBACK callback,
742 VOID *context ) {
743 struct efi_usb_interface *usbintf =
744 container_of ( usbio, struct efi_usb_interface, usbio );
745 struct efi_usb_device *usbdev = usbintf->usbdev;
746
747 DBGC2 ( usbdev, "USBDEV %s async iso %s %p+%zx %p/%p\n", usbintf->name,
748 ( ( endpoint & USB_ENDPOINT_IN ) ? "IN" : "OUT" ), data,
749 ( ( size_t ) len ), callback, context );
750
751 /* Not supported */
752 return EFI_UNSUPPORTED;
753 }
754
755 /**
756 * Get device descriptor
757 *
758 * @v usbio USB I/O protocol
759 * @ret efidesc EFI device descriptor
760 * @ret efirc EFI status code
761 */
762 static EFI_STATUS EFIAPI
763 efi_usb_get_device_descriptor ( EFI_USB_IO_PROTOCOL *usbio,
764 EFI_USB_DEVICE_DESCRIPTOR *efidesc ) {
765 struct efi_usb_interface *usbintf =
766 container_of ( usbio, struct efi_usb_interface, usbio );
767 struct efi_usb_device *usbdev = usbintf->usbdev;
768
769 DBGC2 ( usbdev, "USBDEV %s get device descriptor\n", usbintf->name );
770
771 /* Copy cached device descriptor */
772 memcpy ( efidesc, &usbdev->usb->device, sizeof ( *efidesc ) );
773
774 return 0;
775 }
776
777 /**
778 * Get configuration descriptor
779 *
780 * @v usbio USB I/O protocol
781 * @ret efidesc EFI interface descriptor
782 * @ret efirc EFI status code
783 */
784 static EFI_STATUS EFIAPI
785 efi_usb_get_config_descriptor ( EFI_USB_IO_PROTOCOL *usbio,
786 EFI_USB_CONFIG_DESCRIPTOR *efidesc ) {
787 struct efi_usb_interface *usbintf =
788 container_of ( usbio, struct efi_usb_interface, usbio );
789 struct efi_usb_device *usbdev = usbintf->usbdev;
790
791 DBGC2 ( usbdev, "USBDEV %s get configuration descriptor\n",
792 usbintf->name );
793
794 /* Copy cached configuration descriptor */
795 memcpy ( efidesc, usbdev->config, sizeof ( *efidesc ) );
796
797 return 0;
798 }
799
800 /**
801 * Get interface descriptor
802 *
803 * @v usbio USB I/O protocol
804 * @ret efidesc EFI interface descriptor
805 * @ret efirc EFI status code
806 */
807 static EFI_STATUS EFIAPI
808 efi_usb_get_interface_descriptor ( EFI_USB_IO_PROTOCOL *usbio,
809 EFI_USB_INTERFACE_DESCRIPTOR *efidesc ) {
810 struct efi_usb_interface *usbintf =
811 container_of ( usbio, struct efi_usb_interface, usbio );
812 struct efi_usb_device *usbdev = usbintf->usbdev;
813 struct usb_interface_descriptor *desc;
814
815 DBGC2 ( usbdev, "USBDEV %s get interface descriptor\n", usbintf->name );
816
817 /* Locate cached interface descriptor */
818 desc = usb_interface_descriptor ( usbdev->config, usbintf->interface,
819 usbintf->alternate );
820 if ( ! desc ) {
821 DBGC ( usbdev, "USBDEV %s alt %d has no interface descriptor\n",
822 usbintf->name, usbintf->alternate );
823 return -ENOENT;
824 }
825
826 /* Copy cached interface descriptor */
827 memcpy ( efidesc, desc, sizeof ( *efidesc ) );
828
829 return 0;
830 }
831
832 /**
833 * Get endpoint descriptor
834 *
835 * @v usbio USB I/O protocol
836 * @v address Endpoint index
837 * @ret efidesc EFI interface descriptor
838 * @ret efirc EFI status code
839 */
840 static EFI_STATUS EFIAPI
841 efi_usb_get_endpoint_descriptor ( EFI_USB_IO_PROTOCOL *usbio, UINT8 index,
842 EFI_USB_ENDPOINT_DESCRIPTOR *efidesc ) {
843 struct efi_usb_interface *usbintf =
844 container_of ( usbio, struct efi_usb_interface, usbio );
845 struct efi_usb_device *usbdev = usbintf->usbdev;
846 struct usb_interface_descriptor *interface;
847 struct usb_endpoint_descriptor *desc;
848
849 DBGC2 ( usbdev, "USBDEV %s get endpoint %d descriptor\n",
850 usbintf->name, index );
851
852 /* Locate cached interface descriptor */
853 interface = usb_interface_descriptor ( usbdev->config,
854 usbintf->interface,
855 usbintf->alternate );
856 if ( ! interface ) {
857 DBGC ( usbdev, "USBDEV %s alt %d has no interface descriptor\n",
858 usbintf->name, usbintf->alternate );
859 return -ENOENT;
860 }
861
862 /* Locate and copy cached endpoint descriptor */
863 for_each_interface_descriptor ( desc, usbdev->config, interface ) {
864 if ( ( desc->header.type == USB_ENDPOINT_DESCRIPTOR ) &&
865 ( index-- == 0 ) ) {
866 memcpy ( efidesc, desc, sizeof ( *efidesc ) );
867 return 0;
868 }
869 }
870 return -ENOENT;
871 }
872
873 /**
874 * Get string descriptor
875 *
876 * @v usbio USB I/O protocol
877 * @v language Language ID
878 * @v index String index
879 * @ret string String
880 * @ret efirc EFI status code
881 */
882 static EFI_STATUS EFIAPI
883 efi_usb_get_string_descriptor ( EFI_USB_IO_PROTOCOL *usbio, UINT16 language,
884 UINT8 index, CHAR16 **string ) {
885 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
886 struct efi_usb_interface *usbintf =
887 container_of ( usbio, struct efi_usb_interface, usbio );
888 struct efi_usb_device *usbdev = usbintf->usbdev;
889 struct usb_descriptor_header header;
890 VOID *buffer;
891 size_t len;
892 EFI_STATUS efirc;
893 int rc;
894
895 DBGC2 ( usbdev, "USBDEV %s get string %d:%d descriptor\n",
896 usbintf->name, language, index );
897
898 /* Read descriptor header */
899 if ( ( rc = usb_get_descriptor ( usbdev->usb, 0, USB_STRING_DESCRIPTOR,
900 index, language, &header,
901 sizeof ( header ) ) ) != 0 ) {
902 DBGC ( usbdev, "USBDEV %s could not get string %d:%d "
903 "descriptor header: %s\n", usbintf->name, language,
904 index, strerror ( rc ) );
905 goto err_get_header;
906 }
907 len = header.len;
908
909 /* Allocate buffer */
910 if ( ( efirc = bs->AllocatePool ( EfiBootServicesData, len,
911 &buffer ) ) != 0 ) {
912 rc = -EEFI ( efirc );
913 goto err_alloc;
914 }
915
916 /* Read whole descriptor */
917 if ( ( rc = usb_get_descriptor ( usbdev->usb, 0, USB_STRING_DESCRIPTOR,
918 index, language, buffer,
919 len ) ) != 0 ) {
920 DBGC ( usbdev, "USBDEV %s could not get string %d:%d "
921 "descriptor: %s\n", usbintf->name, language,
922 index, strerror ( rc ) );
923 goto err_get_descriptor;
924 }
925
926 /* Shuffle down and terminate string */
927 memmove ( buffer, ( buffer + sizeof ( header ) ),
928 ( len - sizeof ( header ) ) );
929 memset ( ( buffer + len - sizeof ( header ) ), 0, sizeof ( **string ) );
930
931 /* Return allocated string */
932 *string = buffer;
933 return 0;
934
935 err_get_descriptor:
936 bs->FreePool ( buffer );
937 err_alloc:
938 err_get_header:
939 return EFIRC ( rc );
940 }
941
942 /**
943 * Get supported languages
944 *
945 * @v usbio USB I/O protocol
946 * @ret languages Language ID table
947 * @ret len Length of language ID table
948 * @ret efirc EFI status code
949 */
950 static EFI_STATUS EFIAPI
951 efi_usb_get_supported_languages ( EFI_USB_IO_PROTOCOL *usbio,
952 UINT16 **languages, UINT16 *len ) {
953 struct efi_usb_interface *usbintf =
954 container_of ( usbio, struct efi_usb_interface, usbio );
955 struct efi_usb_device *usbdev = usbintf->usbdev;
956
957 DBGC2 ( usbdev, "USBDEV %s get supported languages\n", usbintf->name );
958
959 /* Return cached supported languages */
960 *languages = ( ( ( void * ) usbdev->languages ) +
961 sizeof ( *(usbdev->languages) ) );
962 *len = usbdev->languages->len;
963
964 return 0;
965 }
966
967 /**
968 * Reset port
969 *
970 * @v usbio USB I/O protocol
971 * @ret efirc EFI status code
972 */
973 static EFI_STATUS EFIAPI
974 efi_usb_port_reset ( EFI_USB_IO_PROTOCOL *usbio ) {
975 struct efi_usb_interface *usbintf =
976 container_of ( usbio, struct efi_usb_interface, usbio );
977 struct efi_usb_device *usbdev = usbintf->usbdev;
978
979 DBGC2 ( usbdev, "USBDEV %s reset port\n", usbintf->name );
980
981 /* This is logically impossible to do, since resetting the
982 * port may destroy state belonging to other
983 * EFI_USB_IO_PROTOCOL instances belonging to the same USB
984 * device. (This is yet another artifact of the incredibly
985 * poor design of the EFI_USB_IO_PROTOCOL.)
986 */
987 return EFI_INVALID_PARAMETER;
988 }
989
990 /** USB I/O protocol */
991 static EFI_USB_IO_PROTOCOL efi_usb_io_protocol = {
992 .UsbControlTransfer = efi_usb_control_transfer,
993 .UsbBulkTransfer = efi_usb_bulk_transfer,
994 .UsbAsyncInterruptTransfer = efi_usb_async_interrupt_transfer,
995 .UsbSyncInterruptTransfer = efi_usb_sync_interrupt_transfer,
996 .UsbIsochronousTransfer = efi_usb_isochronous_transfer,
997 .UsbAsyncIsochronousTransfer = efi_usb_async_isochronous_transfer,
998 .UsbGetDeviceDescriptor = efi_usb_get_device_descriptor,
999 .UsbGetConfigDescriptor = efi_usb_get_config_descriptor,
1000 .UsbGetInterfaceDescriptor = efi_usb_get_interface_descriptor,
1001 .UsbGetEndpointDescriptor = efi_usb_get_endpoint_descriptor,
1002 .UsbGetStringDescriptor = efi_usb_get_string_descriptor,
1003 .UsbGetSupportedLanguages = efi_usb_get_supported_languages,
1004 .UsbPortReset = efi_usb_port_reset,
1005 };
1006
1007 /******************************************************************************
1008 *
1009 * USB driver
1010 *
1011 ******************************************************************************
1012 */
1013
1014 /**
1015 * Install interface
1016 *
1017 * @v usbdev EFI USB device
1018 * @v interface Interface number
1019 * @ret rc Return status code
1020 */
1021 static int efi_usb_install ( struct efi_usb_device *usbdev,
1022 unsigned int interface ) {
1023 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1024 struct efi_device *efidev = usbdev->efidev;
1025 struct efi_usb_interface *usbintf;
1026 struct usb_device *usb;
1027 EFI_DEVICE_PATH_PROTOCOL *path_end;
1028 USB_DEVICE_PATH *usbpath;
1029 unsigned int path_count;
1030 size_t path_prefix_len;
1031 size_t path_len;
1032 EFI_STATUS efirc;
1033 int rc;
1034
1035 /* Calculate device path length */
1036 path_count = ( usb_depth ( usbdev->usb ) + 1 );
1037 path_prefix_len = efi_devpath_len ( efidev->path );
1038 path_len = ( path_prefix_len + ( path_count * sizeof ( *usbpath ) ) +
1039 sizeof ( *path_end ) );
1040
1041 /* Allocate and initialise structure */
1042 usbintf = zalloc ( sizeof ( *usbintf ) + path_len );
1043 if ( ! usbintf ) {
1044 rc = -ENOMEM;
1045 goto err_alloc;
1046 }
1047 snprintf ( usbintf->name, sizeof ( usbintf->name ), "%s[%d]",
1048 usbdev->name, interface );
1049 usbintf->usbdev = usbdev;
1050 usbintf->interface = interface;
1051 memcpy ( &usbintf->usbio, &efi_usb_io_protocol,
1052 sizeof ( usbintf->usbio ) );
1053 usbintf->path = ( ( ( void * ) usbintf ) + sizeof ( *usbintf ) );
1054
1055 /* Construct device path */
1056 memcpy ( usbintf->path, efidev->path, path_prefix_len );
1057 path_end = ( ( ( void * ) usbintf->path ) + path_len -
1058 sizeof ( *path_end ) );
1059 path_end->Type = END_DEVICE_PATH_TYPE;
1060 path_end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
1061 path_end->Length[0] = sizeof ( *path_end );
1062 usbpath = ( ( ( void * ) path_end ) - sizeof ( *usbpath ) );
1063 usbpath->InterfaceNumber = interface;
1064 for ( usb = usbdev->usb ; usb ; usbpath--, usb = usb->port->hub->usb ) {
1065 usbpath->Header.Type = MESSAGING_DEVICE_PATH;
1066 usbpath->Header.SubType = MSG_USB_DP;
1067 usbpath->Header.Length[0] = sizeof ( *usbpath );
1068 usbpath->ParentPortNumber = usb->port->address;
1069 }
1070
1071 /* Add to list of interfaces */
1072 list_add_tail ( &usbintf->list, &usbdev->interfaces );
1073
1074 /* Install protocols */
1075 if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
1076 &usbintf->handle,
1077 &efi_usb_io_protocol_guid, &usbintf->usbio,
1078 &efi_device_path_protocol_guid, usbintf->path,
1079 NULL ) ) != 0 ) {
1080 rc = -EEFI ( efirc );
1081 DBGC ( usbdev, "USBDEV %s could not install protocols: %s\n",
1082 usbintf->name, strerror ( rc ) );
1083 goto err_install_protocol;
1084 }
1085
1086 DBGC ( usbdev, "USBDEV %s installed as %s\n",
1087 usbintf->name, efi_handle_name ( usbintf->handle ) );
1088 return 0;
1089
1090 efi_usb_close_all ( usbintf );
1091 bs->UninstallMultipleProtocolInterfaces (
1092 usbintf->handle,
1093 &efi_usb_io_protocol_guid, &usbintf->usbio,
1094 &efi_device_path_protocol_guid, usbintf->path,
1095 NULL );
1096 err_install_protocol:
1097 list_del ( &usbintf->list );
1098 free ( usbintf );
1099 err_alloc:
1100 return rc;
1101 }
1102
1103 /**
1104 * Uninstall interface
1105 *
1106 * @v usbintf EFI USB interface
1107 */
1108 static void efi_usb_uninstall ( struct efi_usb_interface *usbintf ) {
1109 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1110
1111 /* Close all endpoints */
1112 efi_usb_close_all ( usbintf );
1113
1114 /* Uninstall protocols */
1115 bs->UninstallMultipleProtocolInterfaces (
1116 usbintf->handle,
1117 &efi_usb_io_protocol_guid, &usbintf->usbio,
1118 &efi_device_path_protocol_guid, usbintf->path,
1119 NULL );
1120
1121 /* Remove from list of interfaces */
1122 list_del ( &usbintf->list );
1123
1124 /* Free interface */
1125 free ( usbintf );
1126 }
1127
1128 /**
1129 * Uninstall all interfaces
1130 *
1131 * @v usbdev EFI USB device
1132 */
1133 static void efi_usb_uninstall_all ( struct efi_usb_device *efiusb ) {
1134 struct efi_usb_interface *usbintf;
1135
1136 /* Uninstall all interfaces */
1137 while ( ( usbintf = list_first_entry ( &efiusb->interfaces,
1138 struct efi_usb_interface,
1139 list ) ) ) {
1140 efi_usb_uninstall ( usbintf );
1141 }
1142 }
1143
1144 /**
1145 * Probe device
1146 *
1147 * @v func USB function
1148 * @v config Configuration descriptor
1149 * @ret rc Return status code
1150 */
1151 static int efi_usb_probe ( struct usb_function *func,
1152 struct usb_configuration_descriptor *config ) {
1153 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1154 struct usb_device *usb = func->usb;
1155 struct efi_usb_device *usbdev;
1156 struct efi_usb_interface *usbintf;
1157 struct efi_device *efidev;
1158 struct usb_descriptor_header header;
1159 size_t config_len;
1160 unsigned int i;
1161 int rc;
1162
1163 /* Find parent EFI device */
1164 efidev = efidev_parent ( &func->dev );
1165 if ( ! efidev ) {
1166 rc = -ENOTTY;
1167 goto err_no_efidev;
1168 }
1169
1170 /* Get configuration length */
1171 config_len = le16_to_cpu ( config->len );
1172
1173 /* Get supported languages descriptor header */
1174 if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, 0, 0,
1175 &header, sizeof ( header ) ) ) != 0 ) {
1176 /* Assume no strings are present */
1177 header.len = 0;
1178 }
1179
1180 /* Allocate and initialise structure */
1181 usbdev = zalloc ( sizeof ( *usbdev ) + config_len + header.len );
1182 if ( ! usbdev ) {
1183 rc = -ENOMEM;
1184 goto err_alloc;
1185 }
1186 usb_func_set_drvdata ( func, usbdev );
1187 usbdev->name = func->name;
1188 usbdev->usb = usb;
1189 usbdev->efidev = efidev;
1190 usbdev->config = ( ( ( void * ) usbdev ) + sizeof ( *usbdev ) );
1191 memcpy ( usbdev->config, config, config_len );
1192 usbdev->languages = ( ( ( void * ) usbdev->config ) + config_len );
1193 INIT_LIST_HEAD ( &usbdev->interfaces );
1194
1195 /* Get supported languages descriptor */
1196 if ( header.len &&
1197 ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, 0, 0,
1198 usbdev->languages,
1199 header.len ) ) != 0 ) {
1200 DBGC ( usbdev, "USBDEV %s could not get supported languages: "
1201 "%s\n", usbdev->name, strerror ( rc ) );
1202 goto err_get_languages;
1203 }
1204
1205 /* Install interfaces */
1206 for ( i = 0 ; i < func->desc.count ; i++ ) {
1207 if ( ( rc = efi_usb_install ( usbdev,
1208 func->interface[i] ) ) != 0 )
1209 goto err_install;
1210 }
1211
1212 /* Connect any external drivers */
1213 list_for_each_entry ( usbintf, &usbdev->interfaces, list )
1214 bs->ConnectController ( usbintf->handle, NULL, NULL, TRUE );
1215
1216 return 0;
1217
1218 err_install:
1219 efi_usb_uninstall_all ( usbdev );
1220 assert ( list_empty ( &usbdev->interfaces ) );
1221 err_get_languages:
1222 free ( usbdev );
1223 err_alloc:
1224 err_no_efidev:
1225 return rc;
1226 }
1227
1228 /**
1229 * Remove device
1230 *
1231 * @v func USB function
1232 */
1233 static void efi_usb_remove ( struct usb_function *func ) {
1234 struct efi_usb_device *usbdev = usb_func_get_drvdata ( func );
1235
1236 /* Uninstall all interfaces */
1237 efi_usb_uninstall_all ( usbdev );
1238 assert ( list_empty ( &usbdev->interfaces ) );
1239
1240 /* Free device */
1241 free ( usbdev );
1242 }
1243
1244 /** USB I/O protocol device IDs */
1245 static struct usb_device_id efi_usb_ids[] = {
1246 {
1247 .name = "usbio",
1248 .vendor = USB_ANY_ID,
1249 .product = USB_ANY_ID,
1250 },
1251 };
1252
1253 /** USB I/O protocol driver */
1254 struct usb_driver usbio_driver __usb_driver = {
1255 .ids = efi_usb_ids,
1256 .id_count = ( sizeof ( efi_usb_ids ) / sizeof ( efi_usb_ids[0] ) ),
1257 .class = USB_CLASS_ID ( USB_ANY_ID, USB_ANY_ID, USB_ANY_ID ),
1258 .score = USB_SCORE_FALLBACK,
1259 .probe = efi_usb_probe,
1260 .remove = efi_usb_remove,
1261 };