[pci] Add support for PCI MSI-X interrupts
[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 memset ( &desc, 0, sizeof ( desc ) );
1320 desc.vendor = device.IdVendor;
1321 desc.product = device.IdProduct;
1322
1323 /* Get interface descriptor */
1324 if ( ( efirc = usb.io->UsbGetInterfaceDescriptor ( usb.io,
1325 &interface ) ) !=0){
1326 rc = -EEFI ( efirc );
1327 DBGC ( handle, "USB %s could not get interface descriptor: "
1328 "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
1329 goto err_get_interface_descriptor;
1330 }
1331 desc.class.class.class = interface.InterfaceClass;
1332 desc.class.class.subclass = interface.InterfaceSubClass;
1333 desc.class.class.protocol = interface.InterfaceProtocol;
1334
1335 /* Look for a driver for this interface */
1336 driver = usb_find_driver ( &desc, &id );
1337 if ( ! driver ) {
1338 rc = -ENOTSUP;
1339 goto err_unsupported;
1340 }
1341
1342 /* Success */
1343 rc = 0;
1344
1345 err_unsupported:
1346 err_get_interface_descriptor:
1347 err_get_device_descriptor:
1348 bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
1349 efi_image_handle, handle );
1350 err_open_protocol:
1351 return rc;
1352 }
1353
1354 /**
1355 * Fetch configuration descriptor
1356 *
1357 * @v usbio USB I/O device
1358 * @ret rc Return status code
1359 */
1360 static int usbio_config ( struct usbio_device *usbio ) {
1361 EFI_HANDLE handle = usbio->handle;
1362 EFI_USB_IO_PROTOCOL *io = usbio->io;
1363 EFI_USB_DEVICE_DESCRIPTOR device;
1364 EFI_USB_CONFIG_DESCRIPTOR partial;
1365 union {
1366 struct usb_setup_packet setup;
1367 EFI_USB_DEVICE_REQUEST efi;
1368 } msg;
1369 UINT32 status;
1370 size_t len;
1371 unsigned int count;
1372 unsigned int value;
1373 unsigned int i;
1374 EFI_STATUS efirc;
1375 int rc;
1376
1377 /* Get device descriptor */
1378 if ( ( efirc = io->UsbGetDeviceDescriptor ( io, &device ) ) != 0 ) {
1379 rc = -EEFI ( efirc );
1380 DBGC ( usbio, "USB %s could not get device descriptor: "
1381 "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
1382 goto err_get_device_descriptor;
1383 }
1384 count = device.NumConfigurations;
1385
1386 /* Get current partial configuration descriptor */
1387 if ( ( efirc = io->UsbGetConfigDescriptor ( io, &partial ) ) != 0 ) {
1388 rc = -EEFI ( efirc );
1389 DBGC ( usbio, "USB %s could not get partial configuration "
1390 "descriptor: %s\n", efi_handle_name ( handle ),
1391 strerror ( rc ) );
1392 goto err_get_configuration_descriptor;
1393 }
1394 len = le16_to_cpu ( partial.TotalLength );
1395
1396 /* Allocate configuration descriptor */
1397 usbio->config = malloc ( len );
1398 if ( ! usbio->config ) {
1399 rc = -ENOMEM;
1400 goto err_alloc;
1401 }
1402
1403 /* There is, naturally, no way to retrieve the entire device
1404 * configuration descriptor via EFI_USB_IO_PROTOCOL. Worse,
1405 * there is no way to even retrieve the index of the current
1406 * configuration descriptor. We have to iterate over all
1407 * possible configuration descriptors looking for the
1408 * descriptor that matches the current configuration value.
1409 */
1410 for ( i = 0 ; i < count ; i++ ) {
1411
1412 /* Construct request */
1413 msg.setup.request = cpu_to_le16 ( USB_GET_DESCRIPTOR );
1414 value = ( ( USB_CONFIGURATION_DESCRIPTOR << 8 ) | i );
1415 msg.setup.value = cpu_to_le16 ( value );
1416 msg.setup.index = 0;
1417 msg.setup.len = cpu_to_le16 ( len );
1418
1419 /* Get full configuration descriptor */
1420 if ( ( efirc = io->UsbControlTransfer ( io, &msg.efi,
1421 EfiUsbDataIn, 0,
1422 usbio->config, len,
1423 &status ) ) != 0 ) {
1424 rc = -EEFI ( efirc );
1425 DBGC ( usbio, "USB %s could not get configuration %d "
1426 "descriptor: %s\n", efi_handle_name ( handle ),
1427 i, strerror ( rc ) );
1428 goto err_control_transfer;
1429 }
1430
1431 /* Ignore unless this is the current configuration */
1432 if ( usbio->config->config != partial.ConfigurationValue )
1433 continue;
1434
1435 /* Check length */
1436 if ( le16_to_cpu ( usbio->config->len ) != len ) {
1437 DBGC ( usbio, "USB %s configuration descriptor length "
1438 "mismatch\n", efi_handle_name ( handle ) );
1439 rc = -EINVAL;
1440 goto err_len;
1441 }
1442
1443 return 0;
1444 }
1445
1446 /* No match found */
1447 DBGC ( usbio, "USB %s could not find current configuration "
1448 "descriptor\n", efi_handle_name ( handle ) );
1449 rc = -ENOENT;
1450
1451 err_len:
1452 err_control_transfer:
1453 free ( usbio->config );
1454 err_alloc:
1455 err_get_configuration_descriptor:
1456 err_get_device_descriptor:
1457 return rc;
1458 }
1459
1460 /**
1461 * Construct device path for opening other interfaces
1462 *
1463 * @v usbio USB I/O device
1464 * @ret rc Return status code
1465 */
1466 static int usbio_path ( struct usbio_device *usbio ) {
1467 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1468 EFI_HANDLE handle = usbio->handle;
1469 EFI_DEVICE_PATH_PROTOCOL *path;
1470 EFI_DEVICE_PATH_PROTOCOL *end;
1471 USB_DEVICE_PATH *usbpath;
1472 union {
1473 void *interface;
1474 EFI_DEVICE_PATH_PROTOCOL *path;
1475 } u;
1476 size_t len;
1477 EFI_STATUS efirc;
1478 int rc;
1479
1480 /* Open device path protocol */
1481 if ( ( efirc = bs->OpenProtocol ( handle,
1482 &efi_device_path_protocol_guid,
1483 &u.interface, efi_image_handle,
1484 handle,
1485 EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
1486 rc = -EEFI ( efirc );
1487 DBGC ( usbio, "USBIO %s cannot open device path protocol: "
1488 "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
1489 goto err_open_protocol;
1490 }
1491 path = u.interface;
1492
1493 /* Locate end of device path and sanity check */
1494 len = efi_devpath_len ( path );
1495 if ( len < sizeof ( *usbpath ) ) {
1496 DBGC ( usbio, "USBIO %s underlength device path\n",
1497 efi_handle_name ( handle ) );
1498 rc = -EINVAL;
1499 goto err_underlength;
1500 }
1501 usbpath = ( ( ( void * ) path ) + len - sizeof ( *usbpath ) );
1502 if ( ! ( ( usbpath->Header.Type == MESSAGING_DEVICE_PATH ) &&
1503 ( usbpath->Header.SubType == MSG_USB_DP ) ) ) {
1504 DBGC ( usbio, "USBIO %s not a USB device path: ",
1505 efi_handle_name ( handle ) );
1506 DBGC ( usbio, "%s\n", efi_devpath_text ( path ) );
1507 rc = -EINVAL;
1508 goto err_non_usb;
1509 }
1510
1511 /* Allocate copy of device path */
1512 usbio->path = malloc ( len + sizeof ( *end ) );
1513 if ( ! usbio->path ) {
1514 rc = -ENOMEM;
1515 goto err_alloc;
1516 }
1517 memcpy ( usbio->path, path, ( len + sizeof ( *end ) ) );
1518 usbio->usbpath = ( ( ( void * ) usbio->path ) + len -
1519 sizeof ( *usbpath ) );
1520
1521 /* Close protocol */
1522 bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
1523 efi_image_handle, handle );
1524
1525 return 0;
1526
1527 free ( usbio->path );
1528 err_alloc:
1529 err_non_usb:
1530 err_underlength:
1531 bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
1532 efi_image_handle, handle );
1533 err_open_protocol:
1534 return rc;
1535 }
1536
1537 /**
1538 * Construct interface list
1539 *
1540 * @v usbio USB I/O device
1541 * @ret rc Return status code
1542 */
1543 static int usbio_interfaces ( struct usbio_device *usbio ) {
1544 EFI_HANDLE handle = usbio->handle;
1545 EFI_USB_IO_PROTOCOL *io = usbio->io;
1546 EFI_USB_INTERFACE_DESCRIPTOR interface;
1547 unsigned int first;
1548 unsigned int count;
1549 EFI_STATUS efirc;
1550 int rc;
1551
1552 /* Get interface descriptor */
1553 if ( ( efirc = io->UsbGetInterfaceDescriptor ( io, &interface ) ) != 0){
1554 rc = -EEFI ( efirc );
1555 DBGC ( usbio, "USB %s could not get interface descriptor: "
1556 "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
1557 goto err_get_interface_descriptor;
1558 }
1559
1560 /* Record first interface number */
1561 first = interface.InterfaceNumber;
1562 count = usbio->config->interfaces;
1563 assert ( first < count );
1564 usbio->first = first;
1565
1566 /* Allocate interface list */
1567 usbio->interface = zalloc ( count * sizeof ( usbio->interface[0] ) );
1568 if ( ! usbio->interface ) {
1569 rc = -ENOMEM;
1570 goto err_alloc;
1571 }
1572
1573 /* Use already-opened protocol for control transfers and for
1574 * the first interface.
1575 */
1576 usbio->interface[0].handle = handle;
1577 usbio->interface[0].io = io;
1578 usbio->interface[0].count = 1;
1579 usbio->interface[first].handle = handle;
1580 usbio->interface[first].io = io;
1581 usbio->interface[first].count = 1;
1582
1583 return 0;
1584
1585 free ( usbio->interface );
1586 err_alloc:
1587 err_get_interface_descriptor:
1588 return rc;
1589 }
1590
1591 /**
1592 * Attach driver to device
1593 *
1594 * @v efidev EFI device
1595 * @ret rc Return status code
1596 */
1597 static int usbio_start ( struct efi_device *efidev ) {
1598 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1599 EFI_HANDLE handle = efidev->device;
1600 struct usbio_device *usbio;
1601 struct usb_port *port;
1602 union {
1603 void *interface;
1604 EFI_USB_IO_PROTOCOL *io;
1605 } u;
1606 EFI_STATUS efirc;
1607 int rc;
1608
1609 /* Allocate and initialise structure */
1610 usbio = zalloc ( sizeof ( *usbio ) );
1611 if ( ! usbio ) {
1612 rc = -ENOMEM;
1613 goto err_alloc;
1614 }
1615 efidev_set_drvdata ( efidev, usbio );
1616 usbio->handle = handle;
1617 INIT_LIST_HEAD ( &usbio->endpoints );
1618
1619 /* Open USB I/O protocol */
1620 if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
1621 &u.interface, efi_image_handle,
1622 handle,
1623 ( EFI_OPEN_PROTOCOL_BY_DRIVER |
1624 EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
1625 rc = -EEFI ( efirc );
1626 DBGC ( usbio, "USBIO %s cannot open USB I/O protocol: %s\n",
1627 efi_handle_name ( handle ), strerror ( rc ) );
1628 DBGC_EFI_OPENERS ( usbio, handle, &efi_usb_io_protocol_guid );
1629 goto err_open_usbio;
1630 }
1631 usbio->io = u.io;
1632
1633 /* Describe generic device */
1634 efi_device_info ( handle, "USB", &usbio->dev );
1635 usbio->dev.parent = &efidev->dev;
1636 list_add ( &usbio->dev.siblings, &efidev->dev.children );
1637 INIT_LIST_HEAD ( &usbio->dev.children );
1638
1639 /* Fetch configuration descriptor */
1640 if ( ( rc = usbio_config ( usbio ) ) != 0 )
1641 goto err_config;
1642
1643 /* Construct device path */
1644 if ( ( rc = usbio_path ( usbio ) ) != 0 )
1645 goto err_path;
1646
1647 /* Construct interface list */
1648 if ( ( rc = usbio_interfaces ( usbio ) ) != 0 )
1649 goto err_interfaces;
1650
1651 /* Allocate USB bus */
1652 usbio->bus = alloc_usb_bus ( &usbio->dev, 1 /* single "port" */,
1653 USBIO_MTU, &usbio_operations );
1654 if ( ! usbio->bus ) {
1655 rc = -ENOMEM;
1656 goto err_alloc_bus;
1657 }
1658 usb_bus_set_hostdata ( usbio->bus, usbio );
1659 usb_hub_set_drvdata ( usbio->bus->hub, usbio );
1660
1661 /* Set port protocol */
1662 port = usb_port ( usbio->bus->hub, 1 );
1663 port->protocol = USB_PROTO_2_0;
1664
1665 /* Register USB bus */
1666 if ( ( rc = register_usb_bus ( usbio->bus ) ) != 0 )
1667 goto err_register;
1668
1669 return 0;
1670
1671 unregister_usb_bus ( usbio->bus );
1672 err_register:
1673 free_usb_bus ( usbio->bus );
1674 err_alloc_bus:
1675 free ( usbio->interface );
1676 err_interfaces:
1677 free ( usbio->path );
1678 err_path:
1679 free ( usbio->config );
1680 err_config:
1681 list_del ( &usbio->dev.siblings );
1682 bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
1683 efi_image_handle, handle );
1684 err_open_usbio:
1685 free ( usbio );
1686 err_alloc:
1687 return rc;
1688 }
1689
1690 /**
1691 * Detach driver from device
1692 *
1693 * @v efidev EFI device
1694 */
1695 static void usbio_stop ( struct efi_device *efidev ) {
1696 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1697 EFI_HANDLE handle = efidev->device;
1698 struct usbio_device *usbio = efidev_get_drvdata ( efidev );
1699
1700 unregister_usb_bus ( usbio->bus );
1701 free_usb_bus ( usbio->bus );
1702 free ( usbio->interface );
1703 free ( usbio->path );
1704 free ( usbio->config );
1705 list_del ( &usbio->dev.siblings );
1706 bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
1707 efi_image_handle, handle );
1708 free ( usbio );
1709 }
1710
1711 /** EFI USB I/O driver */
1712 struct efi_driver usbio_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
1713 .name = "USBIO",
1714 .supported = usbio_supported,
1715 .start = usbio_start,
1716 .stop = usbio_stop,
1717 };