2 #include <gpxe/tables.h>
5 #include <little_bswap.h>
6 #include <gpxe/usb/ch9.h>
8 /* USB Device number. Starts from 2 */
9 static unsigned int usb_devnum = 2;
11 static struct usb_driver usb_drivers[0]
12 __table_start (struct usb_driver, usb_drivers);
13 static struct usb_driver usb_drivers_end[0]
14 __table_end (struct usb_driver, usb_drivers);
20 * @ret rc Return status code
22 * Searches for a driver for the USB device. If a driver is found,
23 * its probe() routine is called.
25 int usb_probe (struct usb_device *udev) {
26 struct usb_driver *driver;
27 struct usb_device_id *id;
28 unsigned int i, vendor, device;
31 vendor = udev->descriptor.idVendor;
32 device = udev->descriptor.idProduct;
34 DBG ("Adding, USB device %04x:%04x\n", vendor, device);
36 for (driver = usb_drivers ; driver < usb_drivers_end; driver++) {
37 for (i = 0; i < driver->id_count; i++ ) {
39 if ((id->vendor != 0xffff) &&
40 (id->vendor != vendor))
42 if (( id->device != 0xffff ) &&
43 ( id->device != device ))
45 udev->driver = driver;
46 udev->driver_name = id->name;
47 #define min(a, b) (((a) < (b)) ? (a) : (b))
48 strncpy(udev->dev.name, id->name,
49 min(strlen(id->name), sizeof(udev->dev.name)));
50 DBG ("...using driver %s\n", udev->driver_name);
51 if ((rc = driver->probe(udev, id )) != 0) {
52 DBG ("......probe failed\n");
59 DBG ("...no driver found\n");
63 int usb_set_address(struct usb_device *udev, int devnum)
65 usb_control_msg(udev, &udev->ep_0_out,
66 USB_REQ_SET_ADDRESS, 0, devnum, 0,
68 udev->devnum = devnum;
73 struct usb_device *usb_alloc_dev(void)
75 struct usb_device *udev;
77 udev = malloc(sizeof(*udev));
79 DBG("Could not allocate memory for USB device\n");
80 goto err_usb_device_malloc;
86 /* Approximate a safe low value for endpoint zero's wMaxPacketSize and
87 * also encode its direction.
89 * This will be updated to the right value after GET DEVICE DESCRIPTOR
91 udev->ep_0_in.desc.wMaxPacketSize = cpu_to_le32(8);
92 udev->ep_0_in.desc.bEndpointAddress = (uint8_t) (1 << 7);
93 udev->ep_0_in.desc.bLength = USB_DT_ENDPOINT_SIZE;
94 udev->ep_0_in.desc.bDescriptorType = USB_DT_ENDPOINT;
96 udev->ep_0_out.desc.bLength = USB_DT_ENDPOINT_SIZE;
97 udev->ep_0_out.desc.bDescriptorType = USB_DT_ENDPOINT;
102 err_usb_device_malloc:
107 void usb_free_dev(struct usb_device *udev)
112 void usb_dev_init(struct usb_device *udev, int port)
114 struct usb_hcd *hcd = udev->hcd;
117 /* Reset the port for a period of 50 msec. This will
118 * the device into proper speed.
120 if ((ret = hcd->driver->reset_port(hcd, port)) < 0)
123 if ((ret = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE)) < 0) {
124 DBG("USB : Error getting device descriptor\n");
128 if ((ret = usb_set_address(udev, usb_devnum++)) < 0) {
129 DBG("USB : Error setting device address\n");
134 if((ret = usb_get_configuration(udev)) < 0) {
135 DBG("USB : Error getting configuration\n");
139 if ((ret = usb_set_configuration(udev, 1)) < 0) {
140 DBG("USB :Error setting configuration number to 1\n");