]>
Commit | Line | Data |
---|---|---|
37fb59d3 GH |
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 | ||
132a3f55 GH |
154 | /* ------------------------------------------------------------------ */ |
155 | ||
156 | void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str) | |
157 | { | |
158 | USBDescString *s; | |
159 | ||
160 | QLIST_FOREACH(s, &dev->strings, next) { | |
161 | if (s->index == index) { | |
162 | break; | |
163 | } | |
164 | } | |
165 | if (s == NULL) { | |
166 | s = qemu_mallocz(sizeof(*s)); | |
167 | s->index = index; | |
168 | QLIST_INSERT_HEAD(&dev->strings, s, next); | |
169 | } | |
170 | qemu_free(s->str); | |
171 | s->str = qemu_strdup(str); | |
172 | } | |
173 | ||
174 | const char *usb_desc_get_string(USBDevice *dev, uint8_t index) | |
175 | { | |
176 | USBDescString *s; | |
177 | ||
178 | QLIST_FOREACH(s, &dev->strings, next) { | |
179 | if (s->index == index) { | |
180 | return s->str; | |
181 | } | |
182 | } | |
183 | return NULL; | |
184 | } | |
185 | ||
186 | int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len) | |
37fb59d3 GH |
187 | { |
188 | uint8_t bLength, pos, i; | |
132a3f55 | 189 | const char *str; |
37fb59d3 GH |
190 | |
191 | if (len < 4) { | |
192 | return -1; | |
193 | } | |
194 | ||
195 | if (index == 0) { | |
196 | /* language ids */ | |
197 | dest[0] = 4; | |
198 | dest[1] = USB_DT_STRING; | |
199 | dest[2] = 0x09; | |
200 | dest[3] = 0x04; | |
201 | return 4; | |
202 | } | |
203 | ||
132a3f55 GH |
204 | str = usb_desc_get_string(dev, index); |
205 | if (str == NULL) { | |
206 | str = dev->info->usb_desc->str[index]; | |
207 | if (str == NULL) { | |
208 | return 0; | |
209 | } | |
37fb59d3 | 210 | } |
132a3f55 GH |
211 | |
212 | bLength = strlen(str) * 2 + 2; | |
37fb59d3 GH |
213 | dest[0] = bLength; |
214 | dest[1] = USB_DT_STRING; | |
215 | i = 0; pos = 2; | |
216 | while (pos+1 < bLength && pos+1 < len) { | |
132a3f55 | 217 | dest[pos++] = str[i++]; |
37fb59d3 GH |
218 | dest[pos++] = 0; |
219 | } | |
220 | return pos; | |
221 | } | |
222 | ||
37fb59d3 GH |
223 | int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len) |
224 | { | |
225 | const USBDesc *desc = dev->info->usb_desc; | |
226 | uint8_t buf[256]; | |
227 | uint8_t type = value >> 8; | |
228 | uint8_t index = value & 0xff; | |
229 | int ret = -1; | |
230 | ||
231 | switch(type) { | |
232 | case USB_DT_DEVICE: | |
233 | ret = usb_desc_device(&desc->id, desc->full, buf, sizeof(buf)); | |
234 | trace_usb_desc_device(dev->addr, len, ret); | |
235 | break; | |
236 | case USB_DT_CONFIG: | |
237 | if (index < desc->full->bNumConfigurations) { | |
238 | ret = usb_desc_config(desc->full->confs + index, buf, sizeof(buf)); | |
239 | } | |
240 | trace_usb_desc_config(dev->addr, index, len, ret); | |
241 | break; | |
242 | case USB_DT_STRING: | |
132a3f55 | 243 | ret = usb_desc_string(dev, index, buf, sizeof(buf)); |
37fb59d3 GH |
244 | trace_usb_desc_string(dev->addr, index, len, ret); |
245 | break; | |
246 | default: | |
247 | fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__, | |
248 | dev->addr, type, len); | |
249 | break; | |
250 | } | |
251 | ||
252 | if (ret > 0) { | |
253 | if (ret > len) { | |
254 | ret = len; | |
255 | } | |
256 | memcpy(dest, buf, ret); | |
257 | } | |
258 | return ret; | |
259 | } | |
260 | ||
261 | int usb_desc_handle_control(USBDevice *dev, int request, int value, | |
262 | int index, int length, uint8_t *data) | |
263 | { | |
264 | const USBDesc *desc = dev->info->usb_desc; | |
265 | int ret = -1; | |
266 | ||
267 | assert(desc != NULL); | |
268 | switch(request) { | |
41c6abbd GH |
269 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: |
270 | dev->addr = value; | |
271 | trace_usb_set_addr(dev->addr); | |
272 | ret = 0; | |
273 | break; | |
274 | ||
37fb59d3 GH |
275 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
276 | ret = usb_desc_get_descriptor(dev, value, data, length); | |
277 | break; | |
278 | } | |
279 | return ret; | |
280 | } |