]> Git Repo - qemu.git/blob - hw/usb-desc.c
usb: add usb_desc_attach
[qemu.git] / hw / usb-desc.c
1 #include "usb.h"
2 #include "usb-desc.h"
3 #include "trace.h"
4
5 /* ------------------------------------------------------------------ */
6
7 static uint8_t usb_lo(uint16_t val)
8 {
9     return val & 0xff;
10 }
11
12 static uint8_t usb_hi(uint16_t val)
13 {
14     return (val >> 8) & 0xff;
15 }
16
17 int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
18                     uint8_t *dest, size_t len)
19 {
20     uint8_t bLength = 0x12;
21
22     if (len < bLength) {
23         return -1;
24     }
25
26     dest[0x00] = bLength;
27     dest[0x01] = USB_DT_DEVICE;
28
29     dest[0x02] = usb_lo(dev->bcdUSB);
30     dest[0x03] = usb_hi(dev->bcdUSB);
31     dest[0x04] = dev->bDeviceClass;
32     dest[0x05] = dev->bDeviceSubClass;
33     dest[0x06] = dev->bDeviceProtocol;
34     dest[0x07] = dev->bMaxPacketSize0;
35
36     dest[0x08] = usb_lo(id->idVendor);
37     dest[0x09] = usb_hi(id->idVendor);
38     dest[0x0a] = usb_lo(id->idProduct);
39     dest[0x0b] = usb_hi(id->idProduct);
40     dest[0x0c] = usb_lo(id->bcdDevice);
41     dest[0x0d] = usb_hi(id->bcdDevice);
42     dest[0x0e] = id->iManufacturer;
43     dest[0x0f] = id->iProduct;
44     dest[0x10] = id->iSerialNumber;
45
46     dest[0x11] = dev->bNumConfigurations;
47
48     return bLength;
49 }
50
51 int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
52 {
53     uint8_t  bLength = 0x09;
54     uint16_t wTotalLength = 0;
55     int i, rc, count;
56
57     if (len < bLength) {
58         return -1;
59     }
60
61     dest[0x00] = bLength;
62     dest[0x01] = USB_DT_CONFIG;
63     dest[0x04] = conf->bNumInterfaces;
64     dest[0x05] = conf->bConfigurationValue;
65     dest[0x06] = conf->iConfiguration;
66     dest[0x07] = conf->bmAttributes;
67     dest[0x08] = conf->bMaxPower;
68     wTotalLength += bLength;
69
70     count = conf->nif ? conf->nif : conf->bNumInterfaces;
71     for (i = 0; i < count; i++) {
72         rc = usb_desc_iface(conf->ifs + i, dest + wTotalLength, len - wTotalLength);
73         if (rc < 0) {
74             return rc;
75         }
76         wTotalLength += rc;
77     }
78
79     dest[0x02] = usb_lo(wTotalLength);
80     dest[0x03] = usb_hi(wTotalLength);
81     return wTotalLength;
82 }
83
84 int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
85 {
86     uint8_t bLength = 0x09;
87     int i, rc, pos = 0;
88
89     if (len < bLength) {
90         return -1;
91     }
92
93     dest[0x00] = bLength;
94     dest[0x01] = USB_DT_INTERFACE;
95     dest[0x02] = iface->bInterfaceNumber;
96     dest[0x03] = iface->bAlternateSetting;
97     dest[0x04] = iface->bNumEndpoints;
98     dest[0x05] = iface->bInterfaceClass;
99     dest[0x06] = iface->bInterfaceSubClass;
100     dest[0x07] = iface->bInterfaceProtocol;
101     dest[0x08] = iface->iInterface;
102     pos += bLength;
103
104     for (i = 0; i < iface->ndesc; i++) {
105         rc = usb_desc_other(iface->descs + i, dest + pos, len - pos);
106         if (rc < 0) {
107             return rc;
108         }
109         pos += rc;
110     }
111
112     for (i = 0; i < iface->bNumEndpoints; i++) {
113         rc = usb_desc_endpoint(iface->eps + i, dest + pos, len - pos);
114         if (rc < 0) {
115             return rc;
116         }
117         pos += rc;
118     }
119
120     return pos;
121 }
122
123 int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
124 {
125     uint8_t bLength = 0x07;
126
127     if (len < bLength) {
128         return -1;
129     }
130
131     dest[0x00] = bLength;
132     dest[0x01] = USB_DT_ENDPOINT;
133     dest[0x02] = ep->bEndpointAddress;
134     dest[0x03] = ep->bmAttributes;
135     dest[0x04] = usb_lo(ep->wMaxPacketSize);
136     dest[0x05] = usb_hi(ep->wMaxPacketSize);
137     dest[0x06] = ep->bInterval;
138
139     return bLength;
140 }
141
142 int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
143 {
144     int bLength = desc->length ? desc->length : desc->data[0];
145
146     if (len < bLength) {
147         return -1;
148     }
149
150     memcpy(dest, desc->data, bLength);
151     return bLength;
152 }
153
154 /* ------------------------------------------------------------------ */
155
156 static void usb_desc_setdefaults(USBDevice *dev)
157 {
158     const USBDesc *desc = dev->info->usb_desc;
159
160     assert(desc != NULL);
161     switch (dev->speed) {
162     case USB_SPEED_LOW:
163     case USB_SPEED_FULL:
164         dev->device = desc->full;
165         break;
166     case USB_SPEED_HIGH:
167         dev->device = desc->high;
168         break;
169     }
170     dev->config = dev->device->confs;
171 }
172
173 void usb_desc_init(USBDevice *dev)
174 {
175     dev->speed = USB_SPEED_FULL;
176     usb_desc_setdefaults(dev);
177 }
178
179 void usb_desc_attach(USBDevice *dev)
180 {
181     const USBDesc *desc = dev->info->usb_desc;
182
183     assert(desc != NULL);
184     if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
185         dev->speed = USB_SPEED_HIGH;
186     } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
187         dev->speed = USB_SPEED_FULL;
188     } else {
189         fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
190                 dev->info->product_desc);
191         return;
192     }
193     usb_desc_setdefaults(dev);
194 }
195
196 void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
197 {
198     USBDescString *s;
199
200     QLIST_FOREACH(s, &dev->strings, next) {
201         if (s->index == index) {
202             break;
203         }
204     }
205     if (s == NULL) {
206         s = qemu_mallocz(sizeof(*s));
207         s->index = index;
208         QLIST_INSERT_HEAD(&dev->strings, s, next);
209     }
210     qemu_free(s->str);
211     s->str = qemu_strdup(str);
212 }
213
214 const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
215 {
216     USBDescString *s;
217
218     QLIST_FOREACH(s, &dev->strings, next) {
219         if (s->index == index) {
220             return s->str;
221         }
222     }
223     return NULL;
224 }
225
226 int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
227 {
228     uint8_t bLength, pos, i;
229     const char *str;
230
231     if (len < 4) {
232         return -1;
233     }
234
235     if (index == 0) {
236         /* language ids */
237         dest[0] = 4;
238         dest[1] = USB_DT_STRING;
239         dest[2] = 0x09;
240         dest[3] = 0x04;
241         return 4;
242     }
243
244     str = usb_desc_get_string(dev, index);
245     if (str == NULL) {
246         str = dev->info->usb_desc->str[index];
247         if (str == NULL) {
248             return 0;
249         }
250     }
251
252     bLength = strlen(str) * 2 + 2;
253     dest[0] = bLength;
254     dest[1] = USB_DT_STRING;
255     i = 0; pos = 2;
256     while (pos+1 < bLength && pos+1 < len) {
257         dest[pos++] = str[i++];
258         dest[pos++] = 0;
259     }
260     return pos;
261 }
262
263 int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
264 {
265     const USBDesc *desc = dev->info->usb_desc;
266     uint8_t buf[256];
267     uint8_t type = value >> 8;
268     uint8_t index = value & 0xff;
269     int ret = -1;
270
271     switch(type) {
272     case USB_DT_DEVICE:
273         ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
274         trace_usb_desc_device(dev->addr, len, ret);
275         break;
276     case USB_DT_CONFIG:
277         if (index < dev->device->bNumConfigurations) {
278             ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
279         }
280         trace_usb_desc_config(dev->addr, index, len, ret);
281         break;
282     case USB_DT_STRING:
283         ret = usb_desc_string(dev, index, buf, sizeof(buf));
284         trace_usb_desc_string(dev->addr, index, len, ret);
285         break;
286     default:
287         fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
288                 dev->addr, type, len);
289         break;
290     }
291
292     if (ret > 0) {
293         if (ret > len) {
294             ret = len;
295         }
296         memcpy(dest, buf, ret);
297     }
298     return ret;
299 }
300
301 int usb_desc_handle_control(USBDevice *dev, int request, int value,
302                             int index, int length, uint8_t *data)
303 {
304     const USBDesc *desc = dev->info->usb_desc;
305     int i, ret = -1;
306
307     assert(desc != NULL);
308     switch(request) {
309     case DeviceOutRequest | USB_REQ_SET_ADDRESS:
310         dev->addr = value;
311         trace_usb_set_addr(dev->addr);
312         ret = 0;
313         break;
314
315     case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
316         ret = usb_desc_get_descriptor(dev, value, data, length);
317         break;
318
319     case DeviceRequest | USB_REQ_GET_CONFIGURATION:
320         data[0] = dev->config->bConfigurationValue;
321         ret = 1;
322         break;
323     case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
324         for (i = 0; i < dev->device->bNumConfigurations; i++) {
325             if (dev->device->confs[i].bConfigurationValue == value) {
326                 dev->config = dev->device->confs + i;
327                 ret = 0;
328             }
329         }
330         trace_usb_set_config(dev->addr, value, ret);
331         break;
332
333     case DeviceRequest | USB_REQ_GET_STATUS:
334         data[0] = 0;
335         if (dev->config->bmAttributes & 0x40) {
336             data[0] |= 1 << USB_DEVICE_SELF_POWERED;
337         }
338         if (dev->remote_wakeup) {
339             data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
340         }
341         data[1] = 0x00;
342         ret = 2;
343         break;
344     case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
345         if (value == USB_DEVICE_REMOTE_WAKEUP) {
346             dev->remote_wakeup = 0;
347             ret = 0;
348         }
349         trace_usb_clear_device_feature(dev->addr, value, ret);
350         break;
351     case DeviceOutRequest | USB_REQ_SET_FEATURE:
352         if (value == USB_DEVICE_REMOTE_WAKEUP) {
353             dev->remote_wakeup = 1;
354             ret = 0;
355         }
356         trace_usb_set_device_feature(dev->addr, value, ret);
357         break;
358     }
359     return ret;
360 }
This page took 0.046233 seconds and 4 git commands to generate.