[USB] Assign proper names to devices. Fix pegasus driver to use it.
[people/balajirrao/gpxe.git] / src / drivers / bus / usb / usbcore.c
1 #include <errno.h>
2 #include <gpxe/tables.h>
3 #include <stdlib.h>
4 #include <gpxe/usb.h>
5 #include <little_bswap.h>
6 #include <gpxe/usb/ch9.h>
7
8 /* USB Device number. Starts from 2 */
9 static unsigned int usb_devnum = 2;
10
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);
15
16 /**
17  * Probe a USB device
18  *
19  * @v usb               USB device
20  * @ret rc              Return status code
21  *
22  * Searches for a driver for the USB device.  If a driver is found,
23  * its probe() routine is called.
24  */
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;
29         int rc;
30
31         vendor = udev->descriptor.idVendor;
32         device = udev->descriptor.idProduct;
33
34         DBG ("Adding,  USB device %04x:%04x\n", vendor, device);
35         
36         for (driver = usb_drivers ; driver < usb_drivers_end; driver++) {
37                 for (i = 0; i < driver->id_count; i++ ) {
38                         id = &driver->ids[i];
39                         if ((id->vendor != 0xffff) &&
40                              (id->vendor != vendor))
41                                 continue;
42                         if (( id->device != 0xffff ) &&
43                              ( id->device != device ))
44                                 continue;
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");
53                                 continue;
54                         }
55                         return 0;
56                 }
57         }
58
59         DBG ("...no driver found\n");
60         return -ENOTTY;
61 }
62
63 int usb_set_address(struct usb_device *udev, int devnum)
64 {
65         usb_control_msg(udev, &udev->ep_0_out,
66                 USB_REQ_SET_ADDRESS, 0, devnum, 0,
67                 NULL, 0);
68         udev->devnum = devnum;
69
70         return 0;
71 }
72
73 struct usb_device *usb_alloc_dev(void)
74 {
75         struct usb_device *udev;
76
77         udev = malloc(sizeof(*udev));
78         if (!udev) {
79                 DBG("Could not allocate memory for USB device\n");
80                 goto err_usb_device_malloc;
81         }
82
83         /* Default Address */
84         udev->devnum = 0;
85
86         /* Approximate a safe low value for endpoint zero's wMaxPacketSize and
87          * also encode its direction.
88          *
89          * This will be updated to the right value after GET DEVICE DESCRIPTOR
90          */
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;
95
96         udev->ep_0_out.desc.bLength = USB_DT_ENDPOINT_SIZE;
97         udev->ep_0_out.desc.bDescriptorType = USB_DT_ENDPOINT;
98         
99         return udev;
100
101         free(udev);
102 err_usb_device_malloc:
103
104         return NULL;
105 }
106
107 void usb_free_dev(struct usb_device *udev)
108 {
109         free(udev);
110 }
111
112 void usb_dev_init(struct usb_device *udev, int port)
113 {
114         struct usb_hcd *hcd = udev->hcd;
115         int ret;
116
117         /* Reset the port for a period of 50 msec. This will
118          * the device into proper speed.
119          */
120         if ((ret = hcd->driver->reset_port(hcd, port)) < 0)
121                 return;
122                 
123         if ((ret = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE)) < 0) {
124                 DBG("USB : Error getting device descriptor\n");
125                 return;
126         }
127         
128         if ((ret = usb_set_address(udev, usb_devnum++)) < 0) {
129                 DBG("USB : Error setting device address\n");
130                 usb_devnum--;
131                 return;
132         }
133         
134         if((ret = usb_get_configuration(udev)) < 0) {
135                 DBG("USB : Error getting configuration\n");
136                 return;
137         }
138         
139         if ((ret = usb_set_configuration(udev, 1)) < 0) {
140                 DBG("USB :Error setting configuration number to 1\n");
141                 return;
142         }
143         
144         usb_probe(udev);
145 }