usb: move remote wakeup handling to common code
[qemu.git] / hw / usb-desc.c
blob56ef734bde1029ecac471086636b2bdca1e743cc
1 #include "usb.h"
2 #include "usb-desc.h"
3 #include "trace.h"
5 /* ------------------------------------------------------------------ */
7 static uint8_t usb_lo(uint16_t val)
9 return val & 0xff;
12 static uint8_t usb_hi(uint16_t val)
14 return (val >> 8) & 0xff;
17 int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
18 uint8_t *dest, size_t len)
20 uint8_t bLength = 0x12;
22 if (len < bLength) {
23 return -1;
26 dest[0x00] = bLength;
27 dest[0x01] = USB_DT_DEVICE;
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;
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;
46 dest[0x11] = dev->bNumConfigurations;
48 return bLength;
51 int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
53 uint8_t bLength = 0x09;
54 uint16_t wTotalLength = 0;
55 int i, rc, count;
57 if (len < bLength) {
58 return -1;
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;
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;
76 wTotalLength += rc;
79 dest[0x02] = usb_lo(wTotalLength);
80 dest[0x03] = usb_hi(wTotalLength);
81 return wTotalLength;
84 int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
86 uint8_t bLength = 0x09;
87 int i, rc, pos = 0;
89 if (len < bLength) {
90 return -1;
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;
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;
109 pos += rc;
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;
117 pos += rc;
120 return pos;
123 int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
125 uint8_t bLength = 0x07;
127 if (len < bLength) {
128 return -1;
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;
139 return bLength;
142 int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
144 int bLength = desc->length ? desc->length : desc->data[0];
146 if (len < bLength) {
147 return -1;
150 memcpy(dest, desc->data, bLength);
151 return bLength;
154 /* ------------------------------------------------------------------ */
156 void usb_desc_init(USBDevice *dev)
158 const USBDesc *desc = dev->info->usb_desc;
160 assert(desc != NULL);
161 dev->speed = USB_SPEED_FULL;
162 dev->device = desc->full;
163 dev->config = dev->device->confs;
166 void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
168 USBDescString *s;
170 QLIST_FOREACH(s, &dev->strings, next) {
171 if (s->index == index) {
172 break;
175 if (s == NULL) {
176 s = qemu_mallocz(sizeof(*s));
177 s->index = index;
178 QLIST_INSERT_HEAD(&dev->strings, s, next);
180 qemu_free(s->str);
181 s->str = qemu_strdup(str);
184 const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
186 USBDescString *s;
188 QLIST_FOREACH(s, &dev->strings, next) {
189 if (s->index == index) {
190 return s->str;
193 return NULL;
196 int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
198 uint8_t bLength, pos, i;
199 const char *str;
201 if (len < 4) {
202 return -1;
205 if (index == 0) {
206 /* language ids */
207 dest[0] = 4;
208 dest[1] = USB_DT_STRING;
209 dest[2] = 0x09;
210 dest[3] = 0x04;
211 return 4;
214 str = usb_desc_get_string(dev, index);
215 if (str == NULL) {
216 str = dev->info->usb_desc->str[index];
217 if (str == NULL) {
218 return 0;
222 bLength = strlen(str) * 2 + 2;
223 dest[0] = bLength;
224 dest[1] = USB_DT_STRING;
225 i = 0; pos = 2;
226 while (pos+1 < bLength && pos+1 < len) {
227 dest[pos++] = str[i++];
228 dest[pos++] = 0;
230 return pos;
233 int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
235 const USBDesc *desc = dev->info->usb_desc;
236 uint8_t buf[256];
237 uint8_t type = value >> 8;
238 uint8_t index = value & 0xff;
239 int ret = -1;
241 switch(type) {
242 case USB_DT_DEVICE:
243 ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
244 trace_usb_desc_device(dev->addr, len, ret);
245 break;
246 case USB_DT_CONFIG:
247 if (index < dev->device->bNumConfigurations) {
248 ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
250 trace_usb_desc_config(dev->addr, index, len, ret);
251 break;
252 case USB_DT_STRING:
253 ret = usb_desc_string(dev, index, buf, sizeof(buf));
254 trace_usb_desc_string(dev->addr, index, len, ret);
255 break;
256 default:
257 fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
258 dev->addr, type, len);
259 break;
262 if (ret > 0) {
263 if (ret > len) {
264 ret = len;
266 memcpy(dest, buf, ret);
268 return ret;
271 int usb_desc_handle_control(USBDevice *dev, int request, int value,
272 int index, int length, uint8_t *data)
274 const USBDesc *desc = dev->info->usb_desc;
275 int i, ret = -1;
277 assert(desc != NULL);
278 switch(request) {
279 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
280 dev->addr = value;
281 trace_usb_set_addr(dev->addr);
282 ret = 0;
283 break;
285 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
286 ret = usb_desc_get_descriptor(dev, value, data, length);
287 break;
289 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
290 data[0] = dev->config->bConfigurationValue;
291 ret = 1;
292 break;
293 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
294 for (i = 0; i < dev->device->bNumConfigurations; i++) {
295 if (dev->device->confs[i].bConfigurationValue == value) {
296 dev->config = dev->device->confs + i;
297 ret = 0;
300 trace_usb_set_config(dev->addr, value, ret);
301 break;
303 case DeviceRequest | USB_REQ_GET_STATUS:
304 data[0] = 0;
305 if (dev->config->bmAttributes & 0x40) {
306 data[0] |= 1 << USB_DEVICE_SELF_POWERED;
308 if (dev->remote_wakeup) {
309 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
311 data[1] = 0x00;
312 ret = 2;
313 break;
314 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
315 if (value == USB_DEVICE_REMOTE_WAKEUP) {
316 dev->remote_wakeup = 0;
317 ret = 0;
319 trace_usb_clear_device_feature(dev->addr, value, ret);
320 break;
321 case DeviceOutRequest | USB_REQ_SET_FEATURE:
322 if (value == USB_DEVICE_REMOTE_WAKEUP) {
323 dev->remote_wakeup = 1;
324 ret = 0;
326 trace_usb_set_device_feature(dev->addr, value, ret);
327 break;
329 return ret;