[usb] Select preferred USB device configuration based on driver score
[ipxe.git] / src / drivers / usb / usbio.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 <string.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <ipxe/efi/efi.h>
31 #include <ipxe/efi/efi_driver.h>
32 #include <ipxe/efi/efi_utils.h>
33 #include <ipxe/efi/Protocol/UsbIo.h>
34 #include <ipxe/usb.h>
35 #include "usbio.h"
36
37 /** @file
38 *
39 * EFI_USB_IO_PROTOCOL pseudo Host Controller Interface driver
40 *
41 *
42 * The EFI_USB_IO_PROTOCOL is an almost unbelievably poorly designed
43 * abstraction of a USB device. It would be just about forgivable for
44 * an API to support only synchronous operation for bulk OUT
45 * endpoints. It is imbecilic to support only synchronous operation
46 * for bulk IN endpoints. This apparently intern-designed API
47 * throttles a typical NIC down to 1.5% of its maximum throughput.
48 * That isn't a typo. It really is that slow.
49 *
50 * We can't even work around this stupidity by talking to the host
51 * controller abstraction directly, because an identical limitation
52 * exists in the EFI_USB2_HC_PROTOCOL.
53 *
54 * Unless you derive therapeutic value from watching download progress
55 * indicators lethargically creep through every single integer from 0
56 * to 100, you should use iPXE's native USB host controller drivers
57 * instead. (Or just upgrade from UEFI to "legacy" BIOS, which will
58 * produce a similar speed increase.)
59 *
60 *
61 * For added excitement, the EFI_USB_IO_PROTOCOL makes the
62 * (demonstrably incorrect) assumption that a USB driver needs to
63 * attach to exactly one interface within a USB device, and provides a
64 * helper method to retrieve "the" interface descriptor. Since pretty
65 * much every USB network device requires binding to a pair of
66 * control+data interfaces, this aspect of EFI_USB_IO_PROTOCOL is of
67 * no use to us.
68 *
69 * We have our own existing code for reading USB descriptors, so we
70 * don't actually care that the UsbGetInterfaceDescriptor() method
71 * provided by EFI_USB_IO_PROTOCOL is useless for network devices. We
72 * can read the descriptors ourselves (via UsbControlTransfer()) and
73 * get all of the information we need this way. We can even work
74 * around the fact that EFI_USB_IO_PROTOCOL provides separate handles
75 * for each of the two interfaces comprising our network device.
76 *
77 * However, if we discover that we need to select an alternative
78 * device configuration (e.g. for devices exposing both RNDIS and
79 * ECM), then all hell breaks loose. EFI_USB_IO_PROTOCOL starts to
80 * panic because its cached interface and endpoint descriptors will no
81 * longer be valid. As mentioned above, the cached descriptors are
82 * useless for network devices anyway so we _really_ don't care about
83 * this, but EFI_USB_IO_PROTOCOL certainly cares. It prints out a
84 * manic warning message containing no fewer than six exclamation
85 * marks and then literally commits seppuku in the middle of the
86 * UsbControlTransfer() method by attempting to uninstall itself.
87 * Quite how the caller is supposed to react when asked to stop using
88 * the EFI_USB_IO_PROTOCOL instance while in the middle of an
89 * uninterruptible call to said instance is left as an exercise for
90 * the interested reader.
91 *
92 * There is no sensible way to work around this, so we just
93 * preemptively fail if asked to change the device configuration, on
94 * the basis that reporting a sarcastic error message is often
95 * preferable to jumping through a NULL pointer and crashing the
96 * system.
97 */
98
99 /* Disambiguate the various error causes */
100 #define ENOTSUP_MORONIC_SPECIFICATION \
101 __einfo_error ( EINFO_ENOTSUP_MORONIC_SPECIFICATION )
102 #define EINFO_ENOTSUP_MORONIC_SPECIFICATION \
103 __einfo_uniqify ( EINFO_ENOTSUP, 0x01, \
104 "EFI_USB_IO_PROTOCOL was designed by morons" )
105
106 /******************************************************************************
107 *
108 * Device model
109 *
110 ******************************************************************************
111 */
112
113 /**
114 * Determine endpoint interface number
115 *
116 * @v usbio USB I/O device
117 * @v ep USB Endpoint
118 * @ret interface Interface number, or negative error
119 */
120 static int usbio_interface ( struct usbio_device *usbio,
121 struct usb_endpoint *ep ) {
122 EFI_HANDLE handle = usbio->handle;
123 struct usb_device *usb = ep->usb;
124 struct usb_configuration_descriptor *config;
125 struct usb_interface_descriptor *interface;
126 struct usb_endpoint_descriptor *endpoint;
127 struct usb_function *func;
128 unsigned int i;
129
130 /* The control endpoint is not part of a described interface */
131 if ( ep->address == USB_EP0_ADDRESS )
132 return 0;
133
134 /* Iterate over all interface descriptors looking for a match */
135 config = usbio->config;
136 for_each_config_descriptor ( interface, config ) {
137
138 /* Skip non-interface descriptors */
139 if ( interface->header.type != USB_INTERFACE_DESCRIPTOR )
140 continue;
141
142 /* Iterate over all endpoint descriptors looking for a match */
143 for_each_interface_descriptor ( endpoint, config, interface ) {
144
145 /* Skip non-endpoint descriptors */
146 if ( endpoint->header.type != USB_ENDPOINT_DESCRIPTOR )
147 continue;
148
149 /* Check endpoint address */
150 if ( endpoint->endpoint != ep->address )
151 continue;
152
153 /* Check interface belongs to this function */
154 list_for_each_entry ( func, &usb->functions, list ) {
155
156 /* Skip non-matching functions */
157 if ( func->interface[0] != usbio->first )
158 continue;
159
160 /* Iterate over all interfaces for a match */
161 for ( i = 0 ; i < func->desc.count ; i++ ) {
162 if ( interface->interface ==
163 func->interface[i] )
164 return interface->interface;
165 }
166 }
167 }
168 }
169
170 DBGC ( usbio, "USBIO %s cannot find interface for %s",
171 efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
172 return -ENOENT;
173 }
174
175 /**
176 * Open USB I/O interface
177 *
178 * @v usbio USB I/O device
179 * @v interface Interface number
180 * @ret rc Return status code
181 */
182 static int usbio_open ( struct usbio_device *usbio, unsigned int interface ) {
183 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
184 EFI_HANDLE handle = usbio->handle;
185 struct usbio_interface *intf = &usbio->interface[interface];
186 EFI_DEVICE_PATH_PROTOCOL *path;
187 EFI_DEVICE_PATH_PROTOCOL *end;
188 USB_DEVICE_PATH *usbpath;
189 union {
190 void *interface;
191 EFI_USB_IO_PROTOCOL *io;
192 } u;
193 EFI_STATUS efirc;
194 int rc;
195
196 /* Sanity check */
197 assert ( interface < usbio->config->interfaces );
198
199 /* If interface is already open, just increment the usage count */
200 if ( intf->count ) {
201 intf->count++;
202 return 0;
203 }
204
205 /* Construct device path for this interface */
206 path = usbio->path;
207 usbpath = usbio->usbpath;
208 usbpath->InterfaceNumber = interface;
209 end = efi_devpath_end ( path );
210
211 /* Locate handle for this endpoint's interface */
212 if ( ( efirc = bs->LocateDevicePath ( &efi_usb_io_protocol_guid, &path,
213 &intf->handle ) ) != 0 ) {
214 rc = -EEFI ( efirc );
215 DBGC ( usbio, "USBIO %s could not locate ",
216 efi_handle_name ( handle ) );
217 DBGC ( usbio, "%s: %s\n",
218 efi_devpath_text ( usbio->path ), strerror ( rc ) );
219 return rc;
220 }
221
222 /* Check that expected path was located */
223 if ( path != end ) {
224 DBGC ( usbio, "USBIO %s located incomplete ",
225 efi_handle_name ( handle ) );
226 DBGC ( usbio, "%s\n", efi_handle_name ( intf->handle ) );
227 return -EXDEV;
228 }
229
230 /* Open USB I/O protocol on this handle */
231 if ( ( efirc = bs->OpenProtocol ( intf->handle,
232 &efi_usb_io_protocol_guid,
233 &u.interface, efi_image_handle,
234 intf->handle,
235 ( EFI_OPEN_PROTOCOL_BY_DRIVER |
236 EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
237 rc = -EEFI ( efirc );
238 DBGC ( usbio, "USBIO %s cannot open ",
239 efi_handle_name ( handle ) );
240 DBGC ( usbio, "%s: %s\n",
241 efi_handle_name ( intf->handle ), strerror ( rc ) );
242 DBGC_EFI_OPENERS ( usbio, intf->handle,
243 &efi_usb_io_protocol_guid );
244 return rc;
245 }
246 intf->io = u.io;
247
248 /* Increment usage count */
249 intf->count++;
250
251 return 0;
252 }
253
254 /**
255 * Close USB I/O interface
256 *
257 * @v usbio USB I/O device
258 * @v interface Interface number
259 */
260 static void usbio_close ( struct usbio_device *usbio, unsigned int interface ) {
261 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
262 struct usbio_interface *intf = &usbio->interface[interface];
263
264 /* Sanity checks */
265 assert ( interface < usbio->config->interfaces );
266 assert ( intf->count > 0 );
267
268 /* Decrement usage count */
269 intf->count--;
270
271 /* Do nothing if interface is still in use */
272 if ( intf->count )
273 return;
274
275 /* Close USB I/O protocol */
276 bs->CloseProtocol ( intf->handle, &efi_usb_io_protocol_guid,
277 efi_image_handle, intf->handle );
278 }
279
280 /******************************************************************************
281 *
282 * Control endpoints
283 *
284 ******************************************************************************
285 */
286
287 /**
288 * Open control endpoint
289 *
290 * @v endpoint Endpoint
291 * @ret rc Return status code
292 */
293 static int usbio_control_open ( struct usbio_endpoint *endpoint __unused ) {
294
295 /* Nothing to do */
296 return 0;
297 }
298
299 /**
300 * Close control endpoint
301 *
302 * @v endpoint Endpoint
303 */
304 static void usbio_control_close ( struct usbio_endpoint *endpoint __unused ) {
305
306 /* Nothing to do */
307 }
308
309 /**
310 * Poll control endpoint
311 *
312 * @v endpoint Endpoint
313 */
314 static void usbio_control_poll ( struct usbio_endpoint *endpoint ) {
315 struct usbio_device *usbio = endpoint->usbio;
316 struct usb_endpoint *ep = endpoint->ep;
317 EFI_HANDLE handle = usbio->handle;
318 EFI_USB_IO_PROTOCOL *io;
319 union {
320 struct usb_setup_packet setup;
321 EFI_USB_DEVICE_REQUEST efi;
322 } *msg;
323 EFI_USB_DATA_DIRECTION direction;
324 struct io_buffer *iobuf;
325 unsigned int index;
326 unsigned int flags;
327 unsigned int recipient;
328 unsigned int interface;
329 uint16_t request;
330 void *data;
331 size_t len;
332 UINT32 status;
333 EFI_STATUS efirc;
334 int rc;
335
336 /* Do nothing if ring is empty */
337 if ( endpoint->cons == endpoint->prod )
338 return;
339
340 /* Consume next transfer */
341 index = ( endpoint->cons++ % USBIO_RING_COUNT );
342 iobuf = endpoint->iobuf[index];
343 flags = endpoint->flags[index];
344
345 /* Sanity check */
346 if ( ! ( flags & USBIO_MESSAGE ) ) {
347 DBGC ( usbio, "USBIO %s %s non-message transfer\n",
348 efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
349 rc = -ENOTSUP;
350 goto err_not_message;
351 }
352
353 /* Construct transfer */
354 assert ( iob_len ( iobuf ) >= sizeof ( *msg ) );
355 msg = iobuf->data;
356 iob_pull ( iobuf, sizeof ( *msg ) );
357 request = le16_to_cpu ( msg->setup.request );
358 len = iob_len ( iobuf );
359 if ( len ) {
360 data = iobuf->data;
361 direction = ( ( request & USB_DIR_IN ) ?
362 EfiUsbDataIn : EfiUsbDataOut );
363 } else {
364 data = NULL;
365 direction = EfiUsbNoData;
366 }
367
368 /* Determine interface for this transfer */
369 recipient = ( request & USB_RECIP_MASK );
370 if ( recipient == USB_RECIP_INTERFACE ) {
371 /* Recipient is an interface: use interface number directly */
372 interface = le16_to_cpu ( msg->setup.index );
373 } else {
374 /* Route all other requests through the first interface */
375 interface = 0;
376 }
377
378 /* Open interface */
379 if ( ( rc = usbio_open ( usbio, interface ) ) != 0 )
380 goto err_open;
381 io = usbio->interface[interface].io;
382
383 /* Due to the design of EFI_USB_IO_PROTOCOL, attempting to set
384 * the configuration to a non-default value is basically a
385 * self-destruct button.
386 */
387 if ( ( request == USB_SET_CONFIGURATION ) &&
388 ( le16_to_cpu ( msg->setup.value ) != usbio->config->config ) ) {
389 rc = -ENOTSUP_MORONIC_SPECIFICATION;
390 DBGC ( usbio, "USBIO %s cannot change configuration: %s\n",
391 efi_handle_name ( handle ), strerror ( rc ) );
392 goto err_moronic_specification;
393 }
394
395 /* Submit transfer */
396 if ( ( efirc = io->UsbControlTransfer ( io, &msg->efi, direction, 0,
397 data, len, &status ) ) != 0 ) {
398 rc = -EEFI ( efirc );
399 DBGC ( usbio, "USBIO %s %s could not submit control transfer ",
400 efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
401 DBGC ( usbio, "via %s: %s (status %04x)\n",
402 efi_handle_name ( usbio->interface[interface].handle ),
403 strerror ( rc ), status );
404 goto err_transfer;
405 }
406
407 /* Close interface */
408 usbio_close ( usbio, interface );
409
410 /* Complete transfer */
411 usb_complete ( ep, iobuf );
412
413 return;
414
415 err_transfer:
416 err_moronic_specification:
417 usbio_close ( usbio, interface );
418 err_open:
419 err_not_message:
420 usb_complete_err ( ep, iobuf, rc );
421 }
422
423 /** Control endpoint operations */
424 static struct usbio_operations usbio_control_operations = {
425 .open = usbio_control_open,
426 .close = usbio_control_close,
427 .poll = usbio_control_poll,
428 };
429
430 /******************************************************************************
431 *
432 * Bulk IN endpoints
433 *
434 ******************************************************************************
435 */
436
437 /**
438 * Open bulk IN endpoint
439 *
440 * @v endpoint Endpoint
441 * @ret rc Return status code
442 */
443 static int usbio_bulk_in_open ( struct usbio_endpoint *endpoint __unused ) {
444
445 /* Nothing to do */
446 return 0;
447 }
448
449 /**
450 * Close bulk IN endpoint
451 *
452 * @v endpoint Endpoint
453 */
454 static void usbio_bulk_in_close ( struct usbio_endpoint *endpoint __unused ) {
455
456 /* Nothing to do */
457 }
458
459 /**
460 * Poll bulk IN endpoint
461 *
462 * @v endpoint Endpoint
463 */
464 static void usbio_bulk_in_poll ( struct usbio_endpoint *endpoint ) {
465 struct usbio_device *usbio = endpoint->usbio;
466 struct usb_endpoint *ep = endpoint->ep;
467 EFI_USB_IO_PROTOCOL *io = endpoint->io;
468 EFI_HANDLE handle = usbio->handle;
469 struct io_buffer *iobuf;
470 unsigned int index;
471 UINTN len;
472 UINT32 status;
473 EFI_STATUS efirc;
474 int rc;
475
476 /* Do nothing if ring is empty */
477 if ( endpoint->cons == endpoint->prod )
478 return;
479
480 /* Attempt (but do not yet consume) next transfer */
481 index = ( endpoint->cons % USBIO_RING_COUNT );
482 iobuf = endpoint->iobuf[index];
483
484 /* Construct transfer */
485 len = iob_len ( iobuf );
486
487 /* Upon being turned on, the EFI_USB_IO_PROTOCOL did nothing
488 * for several minutes before firing a small ARP packet a few
489 * millimetres into the ether.
490 */
491 efirc = io->UsbBulkTransfer ( io, ep->address, iobuf->data,
492 &len, 1, &status );
493 if ( efirc == EFI_TIMEOUT )
494 return;
495
496 /* Consume transfer */
497 endpoint->cons++;
498
499 /* Check for failure */
500 if ( efirc != 0 ) {
501 rc = -EEFI ( efirc );
502 DBGC2 ( usbio, "USBIO %s %s could not submit bulk IN transfer: "
503 "%s (status %04x)\n", efi_handle_name ( handle ),
504 usb_endpoint_name ( ep ), strerror ( rc ), status );
505 usb_complete_err ( ep, iobuf, rc );
506 return;
507 }
508
509 /* Update length */
510 iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
511
512 /* Complete transfer */
513 usb_complete ( ep, iobuf );
514 }
515
516 /** Bulk endpoint operations */
517 static struct usbio_operations usbio_bulk_in_operations = {
518 .open = usbio_bulk_in_open,
519 .close = usbio_bulk_in_close,
520 .poll = usbio_bulk_in_poll,
521 };
522
523 /******************************************************************************
524 *
525 * Bulk OUT endpoints
526 *
527 ******************************************************************************
528 */
529
530 /**
531 * Open bulk OUT endpoint
532 *
533 * @v endpoint Endpoint
534 * @ret rc Return status code
535 */
536 static int usbio_bulk_out_open ( struct usbio_endpoint *endpoint __unused ) {
537
538 /* Nothing to do */
539 return 0;
540 }
541
542 /**
543 * Close bulk OUT endpoint
544 *
545 * @v endpoint Endpoint
546 */
547 static void usbio_bulk_out_close ( struct usbio_endpoint *endpoint __unused ) {
548
549 /* Nothing to do */
550 }
551
552 /**
553 * Poll bulk OUT endpoint
554 *
555 * @v endpoint Endpoint
556 */
557 static void usbio_bulk_out_poll ( struct usbio_endpoint *endpoint ) {
558 struct usbio_device *usbio = endpoint->usbio;
559 struct usb_endpoint *ep = endpoint->ep;
560 EFI_USB_IO_PROTOCOL *io = endpoint->io;
561 EFI_HANDLE handle = usbio->handle;
562 struct io_buffer *iobuf;
563 unsigned int index;
564 unsigned int flags;
565 UINTN len;
566 UINT32 status;
567 EFI_STATUS efirc;
568 int rc;
569
570 /* Do nothing if ring is empty */
571 if ( endpoint->cons == endpoint->prod )
572 return;
573
574 /* Consume next transfer */
575 index = ( endpoint->cons++ % USBIO_RING_COUNT );
576 iobuf = endpoint->iobuf[index];
577 flags = endpoint->flags[index];
578
579 /* Construct transfer */
580 len = iob_len ( iobuf );
581
582 /* Submit transfer */
583 if ( ( efirc = io->UsbBulkTransfer ( io, ep->address, iobuf->data,
584 &len, 0, &status ) ) != 0 ) {
585 rc = -EEFI ( efirc );
586 DBGC ( usbio, "USBIO %s %s could not submit bulk OUT transfer: "
587 "%s (status %04x)\n", efi_handle_name ( handle ),
588 usb_endpoint_name ( ep ), strerror ( rc ), status );
589 goto err;
590 }
591
592 /* Update length */
593 iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
594
595 /* Submit zero-length transfer if required */
596 len = 0;
597 if ( ( flags & USBIO_ZLEN ) &&
598 ( efirc = io->UsbBulkTransfer ( io, ep->address, NULL, &len, 0,
599 &status ) ) != 0 ) {
600 rc = -EEFI ( efirc );
601 DBGC ( usbio, "USBIO %s %s could not submit zero-length "
602 "transfer: %s (status %04x)\n",
603 efi_handle_name ( handle ), usb_endpoint_name ( ep ),
604 strerror ( rc ), status );
605 goto err;
606 }
607
608 /* Complete transfer */
609 usb_complete ( ep, iobuf );
610
611 return;
612
613 err:
614 usb_complete_err ( ep, iobuf, rc );
615 }
616
617 /** Bulk endpoint operations */
618 static struct usbio_operations usbio_bulk_out_operations = {
619 .open = usbio_bulk_out_open,
620 .close = usbio_bulk_out_close,
621 .poll = usbio_bulk_out_poll,
622 };
623
624 /******************************************************************************
625 *
626 * Interrupt endpoints
627 *
628 ******************************************************************************
629 *
630 * The EFI_USB_IO_PROTOCOL provides two ways to interact with
631 * interrupt endpoints, neither of which naturally model the hardware
632 * interaction. The UsbSyncInterruptTransfer() method allows imposes
633 * a 1ms overhead for every interrupt transfer (which could result in
634 * up to a 50% decrease in overall throughput for the device). The
635 * UsbAsyncInterruptTransfer() method provides no way for us to
636 * prevent transfers when no I/O buffers are available.
637 *
638 * We work around this design by utilising a small, fixed ring buffer
639 * into which the interrupt callback delivers the data. This aims to
640 * provide buffer space even if no I/O buffers have yet been enqueued.
641 * The scheme is not guaranteed since the fixed ring buffer may also
642 * become full. However:
643 *
644 * - devices which send a constant stream of interrupts (and which
645 * therefore might exhaust the fixed ring buffer) tend to be
646 * responding to every interrupt request, and can tolerate lost
647 * packets, and
648 *
649 * - devices which cannot tolerate lost interrupt packets tend to send
650 * only a few small messages.
651 *
652 * The scheme should therefore work in practice.
653 */
654
655 /**
656 * Interrupt endpoint callback
657 *
658 * @v data Received data
659 * @v len Length of received data
660 * @v context Callback context
661 * @v status Transfer status
662 * @ret efirc EFI status code
663 */
664 static EFI_STATUS EFIAPI usbio_interrupt_callback ( VOID *data, UINTN len,
665 VOID *context,
666 UINT32 status ) {
667 struct usbio_interrupt_ring *intr = context;
668 struct usbio_endpoint *endpoint = intr->endpoint;
669 struct usbio_device *usbio = endpoint->usbio;
670 struct usb_endpoint *ep = endpoint->ep;
671 EFI_HANDLE handle = usbio->handle;
672 unsigned int fill;
673 unsigned int index;
674
675 /* Sanity check */
676 assert ( len <= ep->mtu );
677
678 /* Do nothing if ring is empty */
679 fill = ( intr->prod - intr->cons );
680 if ( fill >= USBIO_INTR_COUNT ) {
681 DBGC ( usbio, "USBIO %s %s dropped interrupt completion\n",
682 efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
683 return 0;
684 }
685
686 /* Do nothing if transfer was unsuccessful */
687 if ( status != 0 ) {
688 DBGC ( usbio, "USBIO %s %s interrupt completion status %04x\n",
689 efi_handle_name ( handle ), usb_endpoint_name ( ep ),
690 status );
691 return 0; /* Unclear what failure actually means here */
692 }
693
694 /* Copy data to buffer and increment producer counter */
695 index = ( intr->prod % USBIO_INTR_COUNT );
696 memcpy ( intr->data[index], data, len );
697 intr->len[index] = len;
698 intr->prod++;
699
700 return 0;
701 }
702
703 /**
704 * Open interrupt endpoint
705 *
706 * @v endpoint Endpoint
707 * @ret rc Return status code
708 */
709 static int usbio_interrupt_open ( struct usbio_endpoint *endpoint ) {
710 struct usbio_device *usbio = endpoint->usbio;
711 struct usbio_interrupt_ring *intr;
712 struct usb_endpoint *ep = endpoint->ep;
713 EFI_USB_IO_PROTOCOL *io = endpoint->io;
714 EFI_HANDLE handle = usbio->handle;
715 unsigned int interval;
716 unsigned int i;
717 void *data;
718 EFI_STATUS efirc;
719 int rc;
720
721 /* Allocate interrupt ring buffer */
722 intr = zalloc ( sizeof ( *intr ) + ( USBIO_INTR_COUNT * ep->mtu ) );
723 if ( ! intr ) {
724 rc = -ENOMEM;
725 goto err_alloc;
726 }
727 endpoint->intr = intr;
728 intr->endpoint = endpoint;
729 data = ( ( ( void * ) intr ) + sizeof ( *intr ) );
730 for ( i = 0 ; i < USBIO_INTR_COUNT ; i++ ) {
731 intr->data[i] = data;
732 data += ep->mtu;
733 }
734
735 /* Determine polling interval */
736 interval = ( ep->interval >> 3 /* microframes -> milliseconds */ );
737 if ( ! interval )
738 interval = 1; /* May not be zero */
739
740 /* Add to periodic schedule */
741 if ( ( efirc = io->UsbAsyncInterruptTransfer ( io, ep->address, TRUE,
742 interval, ep->mtu,
743 usbio_interrupt_callback,
744 intr ) ) != 0 ) {
745 rc = -EEFI ( efirc );
746 DBGC ( usbio, "USBIO %s %s could not schedule interrupt "
747 "transfer: %s\n", efi_handle_name ( handle ),
748 usb_endpoint_name ( ep ), strerror ( rc ) );
749 goto err_schedule;
750 }
751
752 return 0;
753
754 io->UsbAsyncInterruptTransfer ( io, ep->address, FALSE, 0, 0,
755 NULL, NULL );
756 err_schedule:
757 free ( intr );
758 err_alloc:
759 return rc;
760 }
761
762 /**
763 * Close interrupt endpoint
764 *
765 * @v endpoint Endpoint
766 */
767 static void usbio_interrupt_close ( struct usbio_endpoint *endpoint ) {
768 struct usb_endpoint *ep = endpoint->ep;
769 EFI_USB_IO_PROTOCOL *io = endpoint->io;
770
771 /* Remove from periodic schedule */
772 io->UsbAsyncInterruptTransfer ( io, ep->address, FALSE, 0, 0,
773 NULL, NULL );
774
775 /* Free interrupt ring buffer */
776 free ( endpoint->intr );
777 }
778
779 /**
780 * Poll interrupt endpoint
781 *
782 * @v endpoint Endpoint
783 */
784 static void usbio_interrupt_poll ( struct usbio_endpoint *endpoint ) {
785 struct usbio_interrupt_ring *intr = endpoint->intr;
786 struct usb_endpoint *ep = endpoint->ep;
787 struct io_buffer *iobuf;
788 unsigned int index;
789 unsigned int intr_index;
790 size_t len;
791
792 /* Do nothing if ring is empty */
793 if ( endpoint->cons == endpoint->prod )
794 return;
795
796 /* Do nothing if interrupt ring is empty */
797 if ( intr->cons == intr->prod )
798 return;
799
800 /* Consume next transfer */
801 index = ( endpoint->cons++ % USBIO_RING_COUNT );
802 iobuf = endpoint->iobuf[index];
803
804 /* Populate I/O buffer */
805 intr_index = ( intr->cons++ % USBIO_INTR_COUNT );
806 len = intr->len[intr_index];
807 assert ( len <= iob_len ( iobuf ) );
808 iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
809 memcpy ( iobuf->data, intr->data[intr_index], len );
810
811 /* Complete transfer */
812 usb_complete ( ep, iobuf );
813 }
814
815 /** Interrupt endpoint operations */
816 static struct usbio_operations usbio_interrupt_operations = {
817 .open = usbio_interrupt_open,
818 .close = usbio_interrupt_close,
819 .poll = usbio_interrupt_poll,
820 };
821
822 /******************************************************************************
823 *
824 * Endpoint operations
825 *
826 ******************************************************************************
827 */
828
829 /**
830 * Open endpoint
831 *
832 * @v ep USB endpoint
833 * @ret rc Return status code
834 */
835 static int usbio_endpoint_open ( struct usb_endpoint *ep ) {
836 struct usb_bus *bus = ep->usb->port->hub->bus;
837 struct usbio_device *usbio = usb_bus_get_hostdata ( bus );
838 struct usbio_endpoint *endpoint;
839 EFI_HANDLE handle = usbio->handle;
840 unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
841 int interface;
842 int rc;
843
844 /* Allocate and initialise structure */
845 endpoint = zalloc ( sizeof ( *endpoint ) );
846 if ( ! endpoint ) {
847 rc = -ENOMEM;
848 goto err_alloc;
849 }
850 usb_endpoint_set_hostdata ( ep, endpoint );
851 endpoint->usbio = usbio;
852 endpoint->ep = ep;
853
854 /* Identify endpoint operations */
855 if ( attr == USB_ENDPOINT_ATTR_CONTROL ) {
856 endpoint->op = &usbio_control_operations;
857 } else if ( attr == USB_ENDPOINT_ATTR_BULK ) {
858 endpoint->op = ( ( ep->address & USB_DIR_IN ) ?
859 &usbio_bulk_in_operations :
860 &usbio_bulk_out_operations );
861 } else if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
862 endpoint->op = &usbio_interrupt_operations;
863 } else {
864 rc = -ENOTSUP;
865 goto err_operations;
866 }
867
868 /* Identify interface for this endpoint */
869 interface = usbio_interface ( usbio, ep );
870 if ( interface < 0 ) {
871 rc = interface;
872 goto err_interface;
873 }
874 endpoint->interface = interface;
875
876 /* Open interface */
877 if ( ( rc = usbio_open ( usbio, interface ) ) != 0 )
878 goto err_open_interface;
879 endpoint->handle = usbio->interface[interface].handle;
880 endpoint->io = usbio->interface[interface].io;
881 DBGC ( usbio, "USBIO %s %s using ",
882 efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
883 DBGC ( usbio, "%s\n", efi_handle_name ( endpoint->handle ) );
884
885 /* Open endpoint */
886 if ( ( rc = endpoint->op->open ( endpoint ) ) != 0 )
887 goto err_open_endpoint;
888
889 /* Add to list of endpoints */
890 list_add_tail ( &endpoint->list, &usbio->endpoints );
891
892 return 0;
893
894 list_del ( &endpoint->list );
895 endpoint->op->close ( endpoint );
896 err_open_endpoint:
897 usbio_close ( usbio, interface );
898 err_open_interface:
899 err_interface:
900 err_operations:
901 free ( endpoint );
902 err_alloc:
903 return rc;
904 }
905
906 /**
907 * Close endpoint
908 *
909 * @v ep USB endpoint
910 */
911 static void usbio_endpoint_close ( struct usb_endpoint *ep ) {
912 struct usbio_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
913 struct usbio_device *usbio = endpoint->usbio;
914 struct io_buffer *iobuf;
915 unsigned int index;
916
917 /* Remove from list of endpoints */
918 list_del ( &endpoint->list );
919
920 /* Close endpoint */
921 endpoint->op->close ( endpoint );
922
923 /* Close interface */
924 usbio_close ( usbio, endpoint->interface );
925
926 /* Cancel any incomplete transfers */
927 while ( endpoint->cons != endpoint->prod ) {
928 index = ( endpoint->cons++ % USBIO_RING_COUNT );
929 iobuf = endpoint->iobuf[index];
930 usb_complete_err ( ep, iobuf, -ECANCELED );
931 }
932
933 /* Free endpoint */
934 free ( endpoint );
935 }
936
937 /**
938 * Reset endpoint
939 *
940 * @v ep USB endpoint
941 * @ret rc Return status code
942 */
943 static int usbio_endpoint_reset ( struct usb_endpoint *ep __unused ) {
944
945 /* Nothing to do */
946 return 0;
947 }
948
949 /**
950 * Update MTU
951 *
952 * @v ep USB endpoint
953 * @ret rc Return status code
954 */
955 static int usbio_endpoint_mtu ( struct usb_endpoint *ep __unused ) {
956
957 /* Nothing to do */
958 return 0;
959 }
960
961 /**
962 * Enqueue transfer
963 *
964 * @v ep USB endpoint
965 * @v iobuf I/O buffer
966 * @v flags Transfer flags
967 * @ret rc Return status code
968 */
969 static int usbio_endpoint_enqueue ( struct usb_endpoint *ep,
970 struct io_buffer *iobuf,
971 unsigned int flags ) {
972 struct usbio_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
973 unsigned int fill;
974 unsigned int index;
975
976 /* Fail if transfer ring is full */
977 fill = ( endpoint->prod - endpoint->cons );
978 if ( fill >= USBIO_RING_COUNT )
979 return -ENOBUFS;
980
981 /* Add to ring */
982 index = ( endpoint->prod++ % USBIO_RING_COUNT );
983 endpoint->iobuf[index] = iobuf;
984 endpoint->flags[index] = flags;
985
986 return 0;
987 }
988
989 /**
990 * Enqueue message transfer
991 *
992 * @v ep USB endpoint
993 * @v iobuf I/O buffer
994 * @ret rc Return status code
995 */
996 static int usbio_endpoint_message ( struct usb_endpoint *ep,
997 struct io_buffer *iobuf ) {
998
999 /* Enqueue transfer */
1000 return usbio_endpoint_enqueue ( ep, iobuf, USBIO_MESSAGE );
1001 }
1002
1003 /**
1004 * Enqueue stream transfer
1005 *
1006 * @v ep USB endpoint
1007 * @v iobuf I/O buffer
1008 * @v zlp Append a zero-length packet
1009 * @ret rc Return status code
1010 */
1011 static int usbio_endpoint_stream ( struct usb_endpoint *ep,
1012 struct io_buffer *iobuf, int zlp ) {
1013
1014 /* Enqueue transfer */
1015 return usbio_endpoint_enqueue ( ep, iobuf, ( zlp ? USBIO_ZLEN : 0 ) );
1016 }
1017
1018 /**
1019 * Poll for completions
1020 *
1021 * @v endpoint Endpoint
1022 */
1023 static void usbio_endpoint_poll ( struct usbio_endpoint *endpoint ) {
1024
1025 /* Poll endpoint */
1026 endpoint->op->poll ( endpoint );
1027 }
1028
1029 /******************************************************************************
1030 *
1031 * Device operations
1032 *
1033 ******************************************************************************
1034 */
1035
1036 /**
1037 * Open device
1038 *
1039 * @v usb USB device
1040 * @ret rc Return status code
1041 */
1042 static int usbio_device_open ( struct usb_device *usb ) {
1043 struct usbio_device *usbio =
1044 usb_bus_get_hostdata ( usb->port->hub->bus );
1045
1046 usb_set_hostdata ( usb, usbio );
1047 return 0;
1048 }
1049
1050 /**
1051 * Close device
1052 *
1053 * @v usb USB device
1054 */
1055 static void usbio_device_close ( struct usb_device *usb __unused ) {
1056
1057 /* Nothing to do */
1058 }
1059
1060 /**
1061 * Assign device address
1062 *
1063 * @v usb USB device
1064 * @ret rc Return status code
1065 */
1066 static int usbio_device_address ( struct usb_device *usb __unused ) {
1067
1068 /* Nothing to do */
1069 return 0;
1070 }
1071
1072 /******************************************************************************
1073 *
1074 * Hub operations
1075 *
1076 ******************************************************************************
1077 */
1078
1079 /**
1080 * Open hub
1081 *
1082 * @v hub USB hub
1083 * @ret rc Return status code
1084 */
1085 static int usbio_hub_open ( struct usb_hub *hub ) {
1086
1087 /* Disallow non-root hubs */
1088 if ( hub->usb )
1089 return -ENOTSUP;
1090
1091 /* Nothing to do */
1092 return 0;
1093 }
1094
1095 /**
1096 * Close hub
1097 *
1098 * @v hub USB hub
1099 */
1100 static void usbio_hub_close ( struct usb_hub *hub __unused ) {
1101
1102 /* Nothing to do */
1103 }
1104
1105 /******************************************************************************
1106 *
1107 * Root hub operations
1108 *
1109 ******************************************************************************
1110 */
1111
1112 /**
1113 * Open root hub
1114 *
1115 * @v hub USB hub
1116 * @ret rc Return status code
1117 */
1118 static int usbio_root_open ( struct usb_hub *hub __unused ) {
1119
1120 /* Nothing to do */
1121 return 0;
1122 }
1123
1124 /**
1125 * Close root hub
1126 *
1127 * @v hub USB hub
1128 */
1129 static void usbio_root_close ( struct usb_hub *hub __unused ) {
1130
1131 /* Nothing to do */
1132 }
1133
1134 /**
1135 * Enable port
1136 *
1137 * @v hub USB hub
1138 * @v port USB port
1139 * @ret rc Return status code
1140 */
1141 static int usbio_root_enable ( struct usb_hub *hub __unused,
1142 struct usb_port *port __unused ) {
1143
1144 /* Nothing to do */
1145 return 0;
1146 }
1147
1148 /**
1149 * Disable port
1150 *
1151 * @v hub USB hub
1152 * @v port USB port
1153 * @ret rc Return status code
1154 */
1155 static int usbio_root_disable ( struct usb_hub *hub __unused,
1156 struct usb_port *port __unused ) {
1157
1158 /* Nothing to do */
1159 return 0;
1160 }
1161
1162 /**
1163 * Update root hub port speed
1164 *
1165 * @v hub USB hub
1166 * @v port USB port
1167 * @ret rc Return status code
1168 */
1169 static int usbio_root_speed ( struct usb_hub *hub __unused,
1170 struct usb_port *port ) {
1171
1172 /* Not actually exposed via EFI_USB_IO_PROTOCOL */
1173 port->speed = USB_SPEED_HIGH;
1174 return 0;
1175 }
1176
1177 /**
1178 * Clear transaction translator buffer
1179 *
1180 * @v hub USB hub
1181 * @v port USB port
1182 * @v ep USB endpoint
1183 * @ret rc Return status code
1184 */
1185 static int usbio_root_clear_tt ( struct usb_hub *hub __unused,
1186 struct usb_port *port __unused,
1187 struct usb_endpoint *ep __unused ) {
1188
1189 /* Should never be called; this is a root hub */
1190 return -ENOTSUP;
1191 }
1192
1193 /******************************************************************************
1194 *
1195 * Bus operations
1196 *
1197 ******************************************************************************
1198 */
1199
1200 /**
1201 * Open USB bus
1202 *
1203 * @v bus USB bus
1204 * @ret rc Return status code
1205 */
1206 static int usbio_bus_open ( struct usb_bus *bus __unused ) {
1207
1208 /* Nothing to do */
1209 return 0;
1210 }
1211
1212 /**
1213 * Close USB bus
1214 *
1215 * @v bus USB bus
1216 */
1217 static void usbio_bus_close ( struct usb_bus *bus __unused ) {
1218
1219 /* Nothing to do */
1220 }
1221
1222 /**
1223 * Poll USB bus
1224 *
1225 * @v bus USB bus
1226 */
1227 static void usbio_bus_poll ( struct usb_bus *bus ) {
1228 struct usbio_device *usbio = usb_bus_get_hostdata ( bus );
1229 struct usbio_endpoint *endpoint;
1230
1231 /* Poll all endpoints. We trust that completion handlers are
1232 * minimal and will not do anything that could plausibly
1233 * affect the endpoint list itself.
1234 */
1235 list_for_each_entry ( endpoint, &usbio->endpoints, list )
1236 usbio_endpoint_poll ( endpoint );
1237 }
1238
1239 /******************************************************************************
1240 *
1241 * EFI driver interface
1242 *
1243 ******************************************************************************
1244 */
1245
1246 /** USB I/O host controller driver operations */
1247 static struct usb_host_operations usbio_operations = {
1248 .endpoint = {
1249 .open = usbio_endpoint_open,
1250 .close = usbio_endpoint_close,
1251 .reset = usbio_endpoint_reset,
1252 .mtu = usbio_endpoint_mtu,
1253 .message = usbio_endpoint_message,
1254 .stream = usbio_endpoint_stream,
1255 },
1256 .device = {
1257 .open = usbio_device_open,
1258 .close = usbio_device_close,
1259 .address = usbio_device_address,
1260 },
1261 .bus = {
1262 .open = usbio_bus_open,
1263 .close = usbio_bus_close,
1264 .poll = usbio_bus_poll,
1265 },
1266 .hub = {
1267 .open = usbio_hub_open,
1268 .close = usbio_hub_close,
1269 },
1270 .root = {
1271 .open = usbio_root_open,
1272 .close = usbio_root_close,
1273 .enable = usbio_root_enable,
1274 .disable = usbio_root_disable,
1275 .speed = usbio_root_speed,
1276 .clear_tt = usbio_root_clear_tt,
1277 },
1278 };
1279
1280 /**
1281 * Check to see if driver supports a device
1282 *
1283 * @v handle EFI device handle
1284 * @ret rc Return status code
1285 */
1286 static int usbio_supported ( EFI_HANDLE handle ) {
1287 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1288 EFI_USB_DEVICE_DESCRIPTOR device;
1289 EFI_USB_INTERFACE_DESCRIPTOR interface;
1290 struct usb_function_descriptor desc;
1291 struct usb_driver *driver;
1292 struct usb_device_id *id;
1293 union {
1294 void *interface;
1295 EFI_USB_IO_PROTOCOL *io;
1296 } usb;
1297 EFI_STATUS efirc;
1298 int rc;
1299
1300 /* Get protocol */
1301 if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
1302 &usb.interface, efi_image_handle,
1303 handle,
1304 EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
1305 rc = -EEFI ( efirc );
1306 DBGCP ( handle, "USB %s is not a USB device\n",
1307 efi_handle_name ( handle ) );
1308 goto err_open_protocol;
1309 }
1310
1311 /* Get device descriptor */
1312 if ( ( efirc = usb.io->UsbGetDeviceDescriptor ( usb.io,
1313 &device ) ) != 0 ) {
1314 rc = -EEFI ( efirc );
1315 DBGC ( handle, "USB %s could not get device descriptor: "
1316 "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
1317 goto err_get_device_descriptor;
1318 }
1319 desc.vendor = device.IdVendor;
1320 desc.product = device.IdProduct;
1321
1322 /* Get interface descriptor */
1323 if ( ( efirc = usb.io->UsbGetInterfaceDescriptor ( usb.io,
1324 &interface ) ) !=0){
1325 rc = -EEFI ( efirc );
1326 DBGC ( handle, "USB %s could not get interface descriptor: "
1327 "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
1328 goto err_get_interface_descriptor;
1329 }
1330 desc.class.class = interface.InterfaceClass;
1331 desc.class.subclass = interface.InterfaceSubClass;
1332 desc.class.protocol = interface.InterfaceProtocol;
1333
1334 /* Look for a driver for this interface */
1335 driver = usb_find_driver ( &desc, &id );
1336 if ( ! driver ) {
1337 rc = -ENOTSUP;
1338 goto err_unsupported;
1339 }
1340
1341 /* Success */
1342 rc = 0;
1343
1344 err_unsupported:
1345 err_get_interface_descriptor:
1346 err_get_device_descriptor:
1347 bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
1348 efi_image_handle, handle );
1349 err_open_protocol:
1350 return rc;
1351 }
1352
1353 /**
1354 * Fetch configuration descriptor
1355 *
1356 * @v usbio USB I/O device
1357 * @ret rc Return status code
1358 */
1359 static int usbio_config ( struct usbio_device *usbio ) {
1360 EFI_HANDLE handle = usbio->handle;
1361 EFI_USB_IO_PROTOCOL *io = usbio->io;
1362 EFI_USB_DEVICE_DESCRIPTOR device;
1363 EFI_USB_CONFIG_DESCRIPTOR partial;
1364 union {
1365 struct usb_setup_packet setup;
1366 EFI_USB_DEVICE_REQUEST efi;
1367 } msg;
1368 UINT32 status;
1369 size_t len;
1370 unsigned int count;
1371 unsigned int value;
1372 unsigned int i;
1373 EFI_STATUS efirc;
1374 int rc;
1375
1376 /* Get device descriptor */
1377 if ( ( efirc = io->UsbGetDeviceDescriptor ( io, &device ) ) != 0 ) {
1378 rc = -EEFI ( efirc );
1379 DBGC ( usbio, "USB %s could not get device descriptor: "
1380 "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
1381 goto err_get_device_descriptor;
1382 }
1383 count = device.NumConfigurations;
1384
1385 /* Get current partial configuration descriptor */
1386 if ( ( efirc = io->UsbGetConfigDescriptor ( io, &partial ) ) != 0 ) {
1387 rc = -EEFI ( efirc );
1388 DBGC ( usbio, "USB %s could not get partial configuration "
1389 "descriptor: %s\n", efi_handle_name ( handle ),
1390 strerror ( rc ) );
1391 goto err_get_configuration_descriptor;
1392 }
1393 len = le16_to_cpu ( partial.TotalLength );
1394
1395 /* Allocate configuration descriptor */
1396 usbio->config = malloc ( len );
1397 if ( ! usbio->config ) {
1398 rc = -ENOMEM;
1399 goto err_alloc;
1400 }
1401
1402 /* There is, naturally, no way to retrieve the entire device
1403 * configuration descriptor via EFI_USB_IO_PROTOCOL. Worse,
1404 * there is no way to even retrieve the index of the current
1405 * configuration descriptor. We have to iterate over all
1406 * possible configuration descriptors looking for the
1407 * descriptor that matches the current configuration value.
1408 */
1409 for ( i = 0 ; i < count ; i++ ) {
1410
1411 /* Construct request */
1412 msg.setup.request = cpu_to_le16 ( USB_GET_DESCRIPTOR );
1413 value = ( ( USB_CONFIGURATION_DESCRIPTOR << 8 ) | i );
1414 msg.setup.value = cpu_to_le16 ( value );
1415 msg.setup.index = 0;
1416 msg.setup.len = cpu_to_le16 ( len );
1417
1418 /* Get full configuration descriptor */
1419 if ( ( efirc = io->UsbControlTransfer ( io, &msg.efi,
1420 EfiUsbDataIn, 0,
1421 usbio->config, len,
1422 &status ) ) != 0 ) {
1423 rc = -EEFI ( efirc );
1424 DBGC ( usbio, "USB %s could not get configuration %d "
1425 "descriptor: %s\n", efi_handle_name ( handle ),
1426 i, strerror ( rc ) );
1427 goto err_control_transfer;
1428 }
1429
1430 /* Ignore unless this is the current configuration */
1431 if ( usbio->config->config != partial.ConfigurationValue )
1432 continue;
1433
1434 /* Check length */
1435 if ( le16_to_cpu ( usbio->config->len ) != len ) {
1436 DBGC ( usbio, "USB %s configuration descriptor length "
1437 "mismatch\n", efi_handle_name ( handle ) );
1438 rc = -EINVAL;
1439 goto err_len;
1440 }
1441
1442 return 0;
1443 }
1444
1445 /* No match found */
1446 DBGC ( usbio, "USB %s could not find current configuration "
1447 "descriptor\n", efi_handle_name ( handle ) );
1448 rc = -ENOENT;
1449
1450 err_len:
1451 err_control_transfer:
1452 free ( usbio->config );
1453 err_alloc:
1454 err_get_configuration_descriptor:
1455 err_get_device_descriptor:
1456 return rc;
1457 }
1458
1459 /**
1460 * Construct device path for opening other interfaces
1461 *
1462 * @v usbio USB I/O device
1463 * @ret rc Return status code
1464 */
1465 static int usbio_path ( struct usbio_device *usbio ) {
1466 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1467 EFI_HANDLE handle = usbio->handle;
1468 EFI_DEVICE_PATH_PROTOCOL *path;
1469 EFI_DEVICE_PATH_PROTOCOL *end;
1470 USB_DEVICE_PATH *usbpath;
1471 union {
1472 void *interface;
1473 EFI_DEVICE_PATH_PROTOCOL *path;
1474 } u;
1475 size_t len;
1476 EFI_STATUS efirc;
1477 int rc;
1478
1479 /* Open device path protocol */
1480 if ( ( efirc = bs->OpenProtocol ( handle,
1481 &efi_device_path_protocol_guid,
1482 &u.interface, efi_image_handle,
1483 handle,
1484 EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
1485 rc = -EEFI ( efirc );
1486 DBGC ( usbio, "USBIO %s cannot open device path protocol: "
1487 "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
1488 goto err_open_protocol;
1489 }
1490 path = u.interface;
1491
1492 /* Locate end of device path and sanity check */
1493 len = efi_devpath_len ( path );
1494 if ( len < sizeof ( *usbpath ) ) {
1495 DBGC ( usbio, "USBIO %s underlength device path\n",
1496 efi_handle_name ( handle ) );
1497 rc = -EINVAL;
1498 goto err_underlength;
1499 }
1500 usbpath = ( ( ( void * ) path ) + len - sizeof ( *usbpath ) );
1501 if ( ! ( ( usbpath->Header.Type == MESSAGING_DEVICE_PATH ) &&
1502 ( usbpath->Header.SubType == MSG_USB_DP ) ) ) {
1503 DBGC ( usbio, "USBIO %s not a USB device path: ",
1504 efi_handle_name ( handle ) );
1505 DBGC ( usbio, "%s\n", efi_devpath_text ( path ) );
1506 rc = -EINVAL;
1507 goto err_non_usb;
1508 }
1509
1510 /* Allocate copy of device path */
1511 usbio->path = malloc ( len + sizeof ( *end ) );
1512 if ( ! usbio->path ) {
1513 rc = -ENOMEM;
1514 goto err_alloc;
1515 }
1516 memcpy ( usbio->path, path, ( len + sizeof ( *end ) ) );
1517 usbio->usbpath = ( ( ( void * ) usbio->path ) + len -
1518 sizeof ( *usbpath ) );
1519
1520 /* Close protocol */
1521 bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
1522 efi_image_handle, handle );
1523
1524 return 0;
1525
1526 free ( usbio->path );
1527 err_alloc:
1528 err_non_usb:
1529 err_underlength:
1530 bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
1531 efi_image_handle, handle );
1532 err_open_protocol:
1533 return rc;
1534 }
1535
1536 /**
1537 * Construct interface list
1538 *
1539 * @v usbio USB I/O device
1540 * @ret rc Return status code
1541 */
1542 static int usbio_interfaces ( struct usbio_device *usbio ) {
1543 EFI_HANDLE handle = usbio->handle;
1544 EFI_USB_IO_PROTOCOL *io = usbio->io;
1545 EFI_USB_INTERFACE_DESCRIPTOR interface;
1546 unsigned int first;
1547 unsigned int count;
1548 EFI_STATUS efirc;
1549 int rc;
1550
1551 /* Get interface descriptor */
1552 if ( ( efirc = io->UsbGetInterfaceDescriptor ( io, &interface ) ) != 0){
1553 rc = -EEFI ( efirc );
1554 DBGC ( usbio, "USB %s could not get interface descriptor: "
1555 "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
1556 goto err_get_interface_descriptor;
1557 }
1558
1559 /* Record first interface number */
1560 first = interface.InterfaceNumber;
1561 count = usbio->config->interfaces;
1562 assert ( first < count );
1563 usbio->first = first;
1564
1565 /* Allocate interface list */
1566 usbio->interface = zalloc ( count * sizeof ( usbio->interface[0] ) );
1567 if ( ! usbio->interface ) {
1568 rc = -ENOMEM;
1569 goto err_alloc;
1570 }
1571
1572 /* Use already-opened protocol for control transfers and for
1573 * the first interface.
1574 */
1575 usbio->interface[0].handle = handle;
1576 usbio->interface[0].io = io;
1577 usbio->interface[0].count = 1;
1578 usbio->interface[first].handle = handle;
1579 usbio->interface[first].io = io;
1580 usbio->interface[first].count = 1;
1581
1582 return 0;
1583
1584 free ( usbio->interface );
1585 err_alloc:
1586 err_get_interface_descriptor:
1587 return rc;
1588 }
1589
1590 /**
1591 * Attach driver to device
1592 *
1593 * @v efidev EFI device
1594 * @ret rc Return status code
1595 */
1596 static int usbio_start ( struct efi_device *efidev ) {
1597 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1598 EFI_HANDLE handle = efidev->device;
1599 struct usbio_device *usbio;
1600 struct usb_port *port;
1601 union {
1602 void *interface;
1603 EFI_USB_IO_PROTOCOL *io;
1604 } u;
1605 EFI_STATUS efirc;
1606 int rc;
1607
1608 /* Allocate and initialise structure */
1609 usbio = zalloc ( sizeof ( *usbio ) );
1610 if ( ! usbio ) {
1611 rc = -ENOMEM;
1612 goto err_alloc;
1613 }
1614 efidev_set_drvdata ( efidev, usbio );
1615 usbio->handle = handle;
1616 INIT_LIST_HEAD ( &usbio->endpoints );
1617
1618 /* Open USB I/O protocol */
1619 if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
1620 &u.interface, efi_image_handle,
1621 handle,
1622 ( EFI_OPEN_PROTOCOL_BY_DRIVER |
1623 EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
1624 rc = -EEFI ( efirc );
1625 DBGC ( usbio, "USBIO %s cannot open USB I/O protocol: %s\n",
1626 efi_handle_name ( handle ), strerror ( rc ) );
1627 DBGC_EFI_OPENERS ( usbio, handle, &efi_usb_io_protocol_guid );
1628 goto err_open_usbio;
1629 }
1630 usbio->io = u.io;
1631
1632 /* Describe generic device */
1633 efi_device_info ( handle, "USB", &usbio->dev );
1634 usbio->dev.parent = &efidev->dev;
1635 list_add ( &usbio->dev.siblings, &efidev->dev.children );
1636 INIT_LIST_HEAD ( &usbio->dev.children );
1637
1638 /* Fetch configuration descriptor */
1639 if ( ( rc = usbio_config ( usbio ) ) != 0 )
1640 goto err_config;
1641
1642 /* Construct device path */
1643 if ( ( rc = usbio_path ( usbio ) ) != 0 )
1644 goto err_path;
1645
1646 /* Construct interface list */
1647 if ( ( rc = usbio_interfaces ( usbio ) ) != 0 )
1648 goto err_interfaces;
1649
1650 /* Allocate USB bus */
1651 usbio->bus = alloc_usb_bus ( &usbio->dev, 1 /* single "port" */,
1652 USBIO_MTU, &usbio_operations );
1653 if ( ! usbio->bus ) {
1654 rc = -ENOMEM;
1655 goto err_alloc_bus;
1656 }
1657 usb_bus_set_hostdata ( usbio->bus, usbio );
1658 usb_hub_set_drvdata ( usbio->bus->hub, usbio );
1659
1660 /* Set port protocol */
1661 port = usb_port ( usbio->bus->hub, 1 );
1662 port->protocol = USB_PROTO_2_0;
1663
1664 /* Register USB bus */
1665 if ( ( rc = register_usb_bus ( usbio->bus ) ) != 0 )
1666 goto err_register;
1667
1668 return 0;
1669
1670 unregister_usb_bus ( usbio->bus );
1671 err_register:
1672 free_usb_bus ( usbio->bus );
1673 err_alloc_bus:
1674 free ( usbio->interface );
1675 err_interfaces:
1676 free ( usbio->path );
1677 err_path:
1678 free ( usbio->config );
1679 err_config:
1680 list_del ( &usbio->dev.siblings );
1681 bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
1682 efi_image_handle, handle );
1683 err_open_usbio:
1684 free ( usbio );
1685 err_alloc:
1686 return rc;
1687 }
1688
1689 /**
1690 * Detach driver from device
1691 *
1692 * @v efidev EFI device
1693 */
1694 static void usbio_stop ( struct efi_device *efidev ) {
1695 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1696 EFI_HANDLE handle = efidev->device;
1697 struct usbio_device *usbio = efidev_get_drvdata ( efidev );
1698
1699 unregister_usb_bus ( usbio->bus );
1700 free_usb_bus ( usbio->bus );
1701 free ( usbio->interface );
1702 free ( usbio->path );
1703 free ( usbio->config );
1704 list_del ( &usbio->dev.siblings );
1705 bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
1706 efi_image_handle, handle );
1707 free ( usbio );
1708 }
1709
1710 /** EFI USB I/O driver */
1711 struct efi_driver usbio_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
1712 .name = "USBIO",
1713 .supported = usbio_supported,
1714 .start = usbio_start,
1715 .stop = usbio_stop,
1716 };