Changeset 660
- Timestamp:
- 05/29/05 12:26:36 (7 years ago)
- Location:
- trunk/src/ifd
- Files:
-
- 3 edited
-
usb-descriptors.c (modified) (1 diff)
-
usb-descriptors.h (modified) (5 diffs)
-
usb.c (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/ifd/usb-descriptors.c
r638 r660 15 15 #include "usb-descriptors.h" 16 16 17 static int 18 ifd_usb_parse_endpoint(struct ifd_usb_endpoint_descriptor *endpoint, 19 unsigned char *buffer, int size) 20 { 21 struct ifd_usb_descriptor_header *header; 22 unsigned char *begin; 23 int parsed = 0, len, numskipped; 24 25 header = (struct ifd_usb_descriptor_header *)buffer; 26 27 /* Everything should be fine being passed into here, but we sanity */ 28 /* check JIC */ 29 if (header->bLength > size) { 30 ct_debug("ran out of descriptors parsing"); 31 return -1; 32 } 33 34 if (header->bDescriptorType != IFD_USB_DT_ENDPOINT) { 35 ct_debug("unexpected descriptor 0x%X, expecting endpoint descriptor, type 0x%X", 36 endpoint->bDescriptorType, IFD_USB_DT_ENDPOINT); 37 return parsed; 38 } 39 if (header->bLength == IFD_USB_DT_ENDPOINT_AUDIO_SIZE) 40 memcpy(endpoint, buffer, IFD_USB_DT_ENDPOINT_AUDIO_SIZE); 41 else 42 memcpy(endpoint, buffer, IFD_USB_DT_ENDPOINT_SIZE); 43 44 IFD_USB_LE16_TO_CPU(endpoint->wMaxPacketSize); 45 46 buffer += header->bLength; 47 size -= header->bLength; 48 parsed += header->bLength; 49 50 /* Skip over the rest of the Class Specific or Vendor Specific */ 51 /* descriptors */ 52 begin = buffer; 53 numskipped = 0; 54 while (size >= sizeof(struct ifd_usb_descriptor_header)) { 55 header = (struct ifd_usb_descriptor_header *)buffer; 56 57 if (header->bLength < 2) { 58 ct_debug("invalid descriptor length of %d", header->bLength); 59 return -1; 60 } 61 62 /* If we find another "proper" descriptor then we're done */ 63 if ((header->bDescriptorType == IFD_USB_DT_ENDPOINT) || 64 (header->bDescriptorType == IFD_USB_DT_INTERFACE) || 65 (header->bDescriptorType == IFD_USB_DT_CONFIG) || 66 (header->bDescriptorType == IFD_USB_DT_DEVICE)) 67 break; 68 69 ct_debug("skipping descriptor 0x%X", header->bDescriptorType); 70 numskipped++; 71 72 buffer += header->bLength; 73 size -= header->bLength; 74 parsed += header->bLength; 75 } 76 77 if (numskipped) 78 ct_debug("skipped %d class/vendor specific endpoint descriptors", numskipped); 79 80 /* Copy any unknown descriptors into a storage area for drivers */ 81 /* to later parse */ 82 len = (int)(buffer - begin); 83 if (!len) { 84 endpoint->extra = NULL; 85 endpoint->extralen = 0; 86 return parsed; 87 } 88 89 endpoint->extra = (unsigned char *) malloc(len); 90 if (!endpoint->extra) { 91 ct_debug("couldn't allocate memory for endpoint extra descriptors"); 92 endpoint->extralen = 0; 93 return parsed; 94 } 95 96 memcpy(endpoint->extra, begin, len); 97 endpoint->extralen = len; 98 99 return parsed; 100 } 101 102 static int ifd_usb_parse_interface(struct ifd_usb_interface *interface, unsigned char *buffer, int size) 103 { 104 int i, len, numskipped, retval, parsed = 0; 105 struct ifd_usb_descriptor_header *header; 106 struct ifd_usb_interface_descriptor *ifp; 107 unsigned char *begin; 108 109 interface->num_altsetting = 0; 110 111 while (size > 0) { 112 interface->altsetting = (struct ifd_usb_interface_descriptor *) realloc(interface->altsetting, 113 sizeof(struct ifd_usb_interface_descriptor) * (interface->num_altsetting + 1)); 114 if (!interface->altsetting) { 115 ct_debug("couldn't malloc interface->altsetting"); 116 return -1; 117 } 118 119 ifp = interface->altsetting + interface->num_altsetting; 120 interface->num_altsetting++; 121 122 memcpy(ifp, buffer, IFD_USB_DT_INTERFACE_SIZE); 123 124 /* Skip over the interface */ 125 buffer += ifp->bLength; 126 parsed += ifp->bLength; 127 size -= ifp->bLength; 128 129 begin = buffer; 130 numskipped = 0; 131 132 /* Skip over any interface, class or vendor descriptors */ 133 while (size >= sizeof(struct ifd_usb_descriptor_header)) { 134 header = (struct ifd_usb_descriptor_header *)buffer; 135 136 if (header->bLength < 2) { 137 ct_debug("invalid descriptor length of %d", header->bLength); return -1; 138 } 139 140 /* If we find another "proper" descriptor then we're done */ 141 if ((header->bDescriptorType == IFD_USB_DT_INTERFACE) || 142 (header->bDescriptorType == IFD_USB_DT_ENDPOINT) || 143 (header->bDescriptorType == IFD_USB_DT_CONFIG) || 144 (header->bDescriptorType == IFD_USB_DT_DEVICE)) 145 break; 146 147 numskipped++; 148 149 buffer += header->bLength; 150 parsed += header->bLength; 151 size -= header->bLength; 152 } 153 154 if (numskipped) 155 ct_debug("skipped %d class/vendor specific interface descriptors", numskipped); 156 157 /* Copy any unknown descriptors into a storage area for */ 158 /* drivers to later parse */ 159 len = (int)(buffer - begin); 160 if (!len) { 161 ifp->extra = NULL; 162 ifp->extralen = 0; 163 } else { 164 ifp->extra = (unsigned char *) malloc(len); 165 if (!ifp->extra) { 166 ct_debug("couldn't allocate memory for interface extra descriptors"); 167 ifp->extralen = 0; 168 return -1; 169 } 170 memcpy(ifp->extra, begin, len); 171 ifp->extralen = len; 172 } 173 174 /* Did we hit an unexpected descriptor? */ 175 header = (struct ifd_usb_descriptor_header *)buffer; 176 if ((size >= sizeof(struct ifd_usb_descriptor_header)) && 177 ((header->bDescriptorType == IFD_USB_DT_CONFIG) || 178 (header->bDescriptorType == IFD_USB_DT_DEVICE))) 179 return parsed; 180 181 if (ifp->bNumEndpoints > IFD_USB_MAXENDPOINTS) { 182 ct_debug("too many endpoints"); 183 return -1; 184 } 185 186 ifp->endpoint = (struct ifd_usb_endpoint_descriptor *) 187 malloc(ifp->bNumEndpoints * 188 sizeof(struct ifd_usb_endpoint_descriptor)); 189 if (!ifp->endpoint) { 190 ct_debug("couldn't allocate memory for ifp->endpoint"); 191 return -1; 192 } 193 194 memset(ifp->endpoint, 0, ifp->bNumEndpoints * 195 sizeof(struct ifd_usb_endpoint_descriptor)); 196 197 for (i = 0; i < ifp->bNumEndpoints; i++) { 198 header = (struct ifd_usb_descriptor_header *)buffer; 199 200 if (header->bLength > size) { 201 ct_debug("ran out of descriptors parsing"); 202 return -1; 203 } 204 205 retval = ifd_usb_parse_endpoint(ifp->endpoint + i, buffer, size); 206 if (retval < 0) 207 return retval; 208 209 buffer += retval; 210 parsed += retval; 211 size -= retval; 212 } 213 214 /* We check to see if it's an alternate to this one */ 215 ifp = (struct ifd_usb_interface_descriptor *)buffer; 216 if (size < IFD_USB_DT_INTERFACE_SIZE || 217 ifp->bDescriptorType != IFD_USB_DT_INTERFACE || 218 !ifp->bAlternateSetting) 219 return parsed; 220 } 221 222 return parsed; 223 } 224 225 int ifd_usb_parse_configuration(struct ifd_usb_config_descriptor *config, unsigned char *buffer) 226 { 227 int i, retval, size; 228 struct ifd_usb_descriptor_header *header; 229 230 memcpy(config, buffer, IFD_USB_DT_CONFIG_SIZE); 231 IFD_USB_LE16_TO_CPU(config->wTotalLength); 232 size = config->wTotalLength; 233 234 if (config->bNumInterfaces > IFD_USB_MAXINTERFACES) { 235 ct_debug("too many interfaces"); 236 return -1; 237 } 238 239 config->interface = (struct ifd_usb_interface *) 240 malloc(config->bNumInterfaces * 241 sizeof(struct ifd_usb_interface)); 242 if (!config->interface) { 243 ct_debug("out of memory"); 244 return -1; 245 } 246 247 memset(config->interface, 0, config->bNumInterfaces * sizeof(struct ifd_usb_interface)); 248 249 buffer += config->bLength; 250 size -= config->bLength; 251 252 config->extra = NULL; 253 config->extralen = 0; 254 255 for (i = 0; i < config->bNumInterfaces; i++) { 256 int numskipped, len; 257 unsigned char *begin; 258 259 /* Skip over the rest of the Class Specific or Vendor */ 260 /* Specific descriptors */ 261 begin = buffer; 262 numskipped = 0; 263 while (size >= sizeof(struct ifd_usb_descriptor_header)) { 264 header = (struct ifd_usb_descriptor_header *)buffer; 265 266 if ((header->bLength > size) || (header->bLength < 2)) { 267 ct_debug("invalid descriptor length of %d", header->bLength); 268 return -1; 269 } 270 271 /* If we find another "proper" descriptor then we're done */ 272 if ((header->bDescriptorType == IFD_USB_DT_ENDPOINT) || 273 (header->bDescriptorType == IFD_USB_DT_INTERFACE) || 274 (header->bDescriptorType == IFD_USB_DT_CONFIG) || 275 (header->bDescriptorType == IFD_USB_DT_DEVICE)) 276 break; 277 278 ct_debug("skipping descriptor 0x%X", header->bDescriptorType); 279 numskipped++; 280 281 buffer += header->bLength; 282 size -= header->bLength; 283 } 284 285 if (numskipped) 286 ct_debug("skipped %d class/vendor specific endpoint descriptors", numskipped); 287 288 /* Copy any unknown descriptors into a storage area for */ 289 /* drivers to later parse */ 290 len = (int)(buffer - begin); 291 if (len) { 292 /* FIXME: We should realloc and append here */ 293 if (!config->extralen) { 294 config->extra = (unsigned char *) malloc(len); 295 if (!config->extra) { 296 ct_debug("couldn't allocate memory for config extra descriptors"); 297 config->extralen = 0; 298 return -1; 299 } 300 301 memcpy(config->extra, begin, len); 302 config->extralen = len; 303 } 304 } 305 306 retval = ifd_usb_parse_interface(config->interface + i, buffer, size); 307 if (retval < 0) 308 return retval; 309 310 buffer += retval; 311 size -= retval; 312 } 313 314 return size; 315 } 316 317 int ifd_usb_get_device(ifd_device_t *dev, struct ifd_usb_device_descriptor *d) { 318 unsigned char devd[18]; 319 int r; 320 321 /* 0x6 == USB_REQ_GET_DESCRIPTOR 322 * 0x1 == USB_DT_DEVICE 323 */ 324 r=ifd_usb_control(dev, 0x80, 0x6, 0x100, 0, devd, 18, 10000); 325 if (r <= 0) { 326 ct_error("cannot get descriptors"); 327 return 1; 328 } 329 memcpy(d, devd, sizeof(devd)); 330 d->bcdUSB=devd[3] <<8 | devd[2]; 331 d->idVendor=devd[9] <<8 | devd[8]; 332 d->idProduct=devd[11] <<8 | devd[10]; 333 d->bcdDevice=devd[13] <<8 | devd[12]; 334 335 return 0; 336 } 337 338 int 339 ifd_usb_get_config(ifd_device_t *dev, int n, 340 struct ifd_usb_config_descriptor *ret) { 341 unsigned char *b; 342 unsigned short len; 343 int r; 344 memset(ret,0,sizeof(struct ifd_usb_config_descriptor)); 345 346 /* 0x6 == USB_REQ_GET_DESCRIPTOR 347 * 0x2 == USB_DT_CONFIG 348 */ 349 r=ifd_usb_control(dev, 0x80, 0x6, 0x200 | n, 0, ret, 8, 1000); 350 if (r <= 0) { 351 ct_error("cannot get descriptors"); 352 return 1; 353 } 354 355 IFD_USB_LE16_TO_CPU(ret->wTotalLength); 356 len=ret->wTotalLength; 357 b=(unsigned char *) malloc(len); 358 if (!b) { 359 ct_error("cannot malloc descriptor buffer"); 360 return 1; 361 } 362 363 r=ifd_usb_control(dev, 0x80, 0x6, 0x200 | n, 0, b, len, 1000); 364 if (r < len) { 365 ct_error("cannot get descriptors"); 366 free(b); 367 return 1; 368 } 369 r=ifd_usb_parse_configuration(ret, b); 370 free(b); 371 if (r < 0) { 372 return 1; 373 } 374 return 0; 375 } 376 377 void ifd_usb_free_configuration(struct ifd_usb_config_descriptor *cf) { 378 int i, j, k; 379 if (!cf->interface) 380 return; 381 382 for (i = 0; i < cf->bNumInterfaces; i++) { 383 struct ifd_usb_interface *ifp = &cf->interface[i]; 384 385 if (!ifp->altsetting) 386 break; 387 388 for (j = 0; j < ifp->num_altsetting; j++) { 389 struct ifd_usb_interface_descriptor *as = &ifp->altsetting[j]; 390 391 if (as->extra) 392 free(as->extra); 393 394 if (!as->endpoint) 395 break; 396 397 for (k = 0; k < as->bNumEndpoints; k++) { 398 if (as->endpoint[k].extra) 399 free(as->endpoint[k].extra); 400 } 401 free(as->endpoint); 402 } 403 404 free(ifp->altsetting); 405 } 406 407 free(cf->interface); 408 } 409 410 17 static int ifd_usb_parse_endpoint(struct ifd_usb_endpoint_descriptor *endpoint, 18 unsigned char *buffer, int size) 19 { 20 struct ifd_usb_descriptor_header *header; 21 unsigned char *begin; 22 int parsed = 0, len, numskipped; 23 24 header = (struct ifd_usb_descriptor_header *)buffer; 25 26 /* Everything should be fine being passed into here, but we sanity */ 27 /* check JIC */ 28 if (header->bLength > size) { 29 ct_debug("ran out of descriptors parsing"); 30 return -1; 31 } 32 33 if (header->bDescriptorType != IFD_USB_DT_ENDPOINT) { 34 ct_debug 35 ("unexpected descriptor 0x%X, expecting endpoint descriptor, type 0x%X", 36 endpoint->bDescriptorType, IFD_USB_DT_ENDPOINT); 37 return parsed; 38 } 39 if (header->bLength == IFD_USB_DT_ENDPOINT_AUDIO_SIZE) 40 memcpy(endpoint, buffer, IFD_USB_DT_ENDPOINT_AUDIO_SIZE); 41 else 42 memcpy(endpoint, buffer, IFD_USB_DT_ENDPOINT_SIZE); 43 44 IFD_USB_LE16_TO_CPU(endpoint->wMaxPacketSize); 45 46 buffer += header->bLength; 47 size -= header->bLength; 48 parsed += header->bLength; 49 50 /* Skip over the rest of the Class Specific or Vendor Specific */ 51 /* descriptors */ 52 begin = buffer; 53 numskipped = 0; 54 while (size >= sizeof(struct ifd_usb_descriptor_header)) { 55 header = (struct ifd_usb_descriptor_header *)buffer; 56 57 if (header->bLength < 2) { 58 ct_debug("invalid descriptor length of %d", 59 header->bLength); 60 return -1; 61 } 62 63 /* If we find another "proper" descriptor then we're done */ 64 if ((header->bDescriptorType == IFD_USB_DT_ENDPOINT) || 65 (header->bDescriptorType == IFD_USB_DT_INTERFACE) || 66 (header->bDescriptorType == IFD_USB_DT_CONFIG) || 67 (header->bDescriptorType == IFD_USB_DT_DEVICE)) 68 break; 69 70 ct_debug("skipping descriptor 0x%X", header->bDescriptorType); 71 numskipped++; 72 73 buffer += header->bLength; 74 size -= header->bLength; 75 parsed += header->bLength; 76 } 77 78 if (numskipped) 79 ct_debug 80 ("skipped %d class/vendor specific endpoint descriptors", 81 numskipped); 82 83 /* Copy any unknown descriptors into a storage area for drivers */ 84 /* to later parse */ 85 len = (int)(buffer - begin); 86 if (!len) { 87 endpoint->extra = NULL; 88 endpoint->extralen = 0; 89 return parsed; 90 } 91 92 endpoint->extra = (unsigned char *)malloc(len); 93 if (!endpoint->extra) { 94 ct_debug 95 ("couldn't allocate memory for endpoint extra descriptors"); 96 endpoint->extralen = 0; 97 return parsed; 98 } 99 100 memcpy(endpoint->extra, begin, len); 101 endpoint->extralen = len; 102 103 return parsed; 104 } 105 106 static int ifd_usb_parse_interface(struct ifd_usb_interface *interface, 107 unsigned char *buffer, int size) 108 { 109 int i, len, numskipped, retval, parsed = 0; 110 struct ifd_usb_descriptor_header *header; 111 struct ifd_usb_interface_descriptor *ifp; 112 unsigned char *begin; 113 114 interface->num_altsetting = 0; 115 116 while (size > 0) { 117 interface->altsetting = (struct ifd_usb_interface_descriptor *) 118 realloc(interface->altsetting, 119 sizeof(struct ifd_usb_interface_descriptor) 120 * (interface-> num_altsetting + 1) 121 ); 122 if (!interface->altsetting) { 123 ct_debug("couldn't malloc interface->altsetting"); 124 return -1; 125 } 126 127 ifp = interface->altsetting + interface->num_altsetting; 128 interface->num_altsetting++; 129 130 memcpy(ifp, buffer, IFD_USB_DT_INTERFACE_SIZE); 131 132 /* Skip over the interface */ 133 buffer += ifp->bLength; 134 parsed += ifp->bLength; 135 size -= ifp->bLength; 136 137 begin = buffer; 138 numskipped = 0; 139 140 /* Skip over any interface, class or vendor descriptors */ 141 while (size >= sizeof(struct ifd_usb_descriptor_header)) { 142 header = (struct ifd_usb_descriptor_header *)buffer; 143 144 if (header->bLength < 2) { 145 ct_debug("invalid descriptor length of %d", 146 header->bLength); 147 return -1; 148 } 149 150 /* If we find another "proper" descriptor then we're done */ 151 if ((header->bDescriptorType == IFD_USB_DT_INTERFACE) || 152 (header->bDescriptorType == IFD_USB_DT_ENDPOINT) || 153 (header->bDescriptorType == IFD_USB_DT_CONFIG) || 154 (header->bDescriptorType == IFD_USB_DT_DEVICE)) 155 break; 156 157 numskipped++; 158 159 buffer += header->bLength; 160 parsed += header->bLength; 161 size -= header->bLength; 162 } 163 164 if (numskipped) 165 ct_debug 166 ("skipped %d class/vendor specific interface descriptors", 167 numskipped); 168 169 /* Copy any unknown descriptors into a storage area for */ 170 /* drivers to later parse */ 171 len = (int)(buffer - begin); 172 if (!len) { 173 ifp->extra = NULL; 174 ifp->extralen = 0; 175 } else { 176 ifp->extra = (unsigned char *)malloc(len); 177 if (!ifp->extra) { 178 ct_debug 179 ("couldn't allocate memory for interface extra descriptors"); 180 ifp->extralen = 0; 181 return -1; 182 } 183 memcpy(ifp->extra, begin, len); 184 ifp->extralen = len; 185 } 186 187 /* Did we hit an unexpected descriptor? */ 188 header = (struct ifd_usb_descriptor_header *)buffer; 189 if ((size >= sizeof(struct ifd_usb_descriptor_header)) && 190 ((header->bDescriptorType == IFD_USB_DT_CONFIG) || 191 (header->bDescriptorType == IFD_USB_DT_DEVICE))) 192 return parsed; 193 194 if (ifp->bNumEndpoints > IFD_USB_MAXENDPOINTS) { 195 ct_debug("too many endpoints"); 196 return -1; 197 } 198 199 ifp->endpoint = (struct ifd_usb_endpoint_descriptor *) 200 malloc(ifp->bNumEndpoints * 201 sizeof(struct ifd_usb_endpoint_descriptor)); 202 if (!ifp->endpoint) { 203 ct_debug("couldn't allocate memory for ifp->endpoint"); 204 return -1; 205 } 206 207 memset(ifp->endpoint, 0, ifp->bNumEndpoints * 208 sizeof(struct ifd_usb_endpoint_descriptor)); 209 210 for (i = 0; i < ifp->bNumEndpoints; i++) { 211 header = (struct ifd_usb_descriptor_header *)buffer; 212 213 if (header->bLength > size) { 214 ct_debug("ran out of descriptors parsing"); 215 return -1; 216 } 217 218 retval = 219 ifd_usb_parse_endpoint(ifp->endpoint + i, buffer, 220 size); 221 if (retval < 0) 222 return retval; 223 224 buffer += retval; 225 parsed += retval; 226 size -= retval; 227 } 228 229 /* We check to see if it's an alternate to this one */ 230 ifp = (struct ifd_usb_interface_descriptor *)buffer; 231 if (size < IFD_USB_DT_INTERFACE_SIZE || 232 ifp->bDescriptorType != IFD_USB_DT_INTERFACE || 233 !ifp->bAlternateSetting) 234 return parsed; 235 } 236 237 return parsed; 238 } 239 240 int ifd_usb_parse_configuration(struct ifd_usb_config_descriptor *config, 241 unsigned char *buffer) 242 { 243 int i, retval, size; 244 struct ifd_usb_descriptor_header *header; 245 246 memcpy(config, buffer, IFD_USB_DT_CONFIG_SIZE); 247 IFD_USB_LE16_TO_CPU(config->wTotalLength); 248 size = config->wTotalLength; 249 250 if (config->bNumInterfaces > IFD_USB_MAXINTERFACES) { 251 ct_debug("too many interfaces"); 252 return -1; 253 } 254 255 config->interface = (struct ifd_usb_interface *) 256 malloc(config->bNumInterfaces * sizeof(struct ifd_usb_interface)); 257 if (!config->interface) { 258 ct_debug("out of memory"); 259 return -1; 260 } 261 262 memset(config->interface, 0, 263 config->bNumInterfaces * sizeof(struct ifd_usb_interface)); 264 265 buffer += config->bLength; 266 size -= config->bLength; 267 268 config->extra = NULL; 269 config->extralen = 0; 270 271 for (i = 0; i < config->bNumInterfaces; i++) { 272 int numskipped, len; 273 unsigned char *begin; 274 275 /* Skip over the rest of the Class Specific or Vendor */ 276 /* Specific descriptors */ 277 begin = buffer; 278 numskipped = 0; 279 while (size >= sizeof(struct ifd_usb_descriptor_header)) { 280 header = (struct ifd_usb_descriptor_header *)buffer; 281 282 if ((header->bLength > size) || (header->bLength < 2)) { 283 ct_debug("invalid descriptor length of %d", 284 header->bLength); 285 return -1; 286 } 287 288 /* If we find another "proper" descriptor then we're done */ 289 if ((header->bDescriptorType == IFD_USB_DT_ENDPOINT) || 290 (header->bDescriptorType == IFD_USB_DT_INTERFACE) || 291 (header->bDescriptorType == IFD_USB_DT_CONFIG) || 292 (header->bDescriptorType == IFD_USB_DT_DEVICE)) 293 break; 294 295 ct_debug("skipping descriptor 0x%X", 296 header->bDescriptorType); 297 numskipped++; 298 299 buffer += header->bLength; 300 size -= header->bLength; 301 } 302 303 if (numskipped) 304 ct_debug 305 ("skipped %d class/vendor specific endpoint descriptors", 306 numskipped); 307 308 /* Copy any unknown descriptors into a storage area for */ 309 /* drivers to later parse */ 310 len = (int)(buffer - begin); 311 if (len) { 312 /* FIXME: We should realloc and append here */ 313 if (!config->extralen) { 314 config->extra = (unsigned char *)malloc(len); 315 if (!config->extra) { 316 ct_debug 317 ("couldn't allocate memory for config extra descriptors"); 318 config->extralen = 0; 319 return -1; 320 } 321 322 memcpy(config->extra, begin, len); 323 config->extralen = len; 324 } 325 } 326 327 retval = 328 ifd_usb_parse_interface(config->interface + i, buffer, 329 size); 330 if (retval < 0) 331 return retval; 332 333 buffer += retval; 334 size -= retval; 335 } 336 337 return size; 338 } 339 340 int ifd_usb_get_device(ifd_device_t * dev, struct ifd_usb_device_descriptor *d) 341 { 342 unsigned char devd[18]; 343 int r; 344 345 /* 0x6 == USB_REQ_GET_DESCRIPTOR 346 * 0x1 == USB_DT_DEVICE 347 */ 348 r = ifd_usb_control(dev, 0x80, 0x6, 0x100, 0, devd, 18, 10000); 349 if (r <= 0) { 350 ct_error("cannot get descriptors"); 351 return 1; 352 } 353 memcpy(d, devd, sizeof(devd)); 354 d->bcdUSB = devd[3] << 8 | devd[2]; 355 d->idVendor = devd[9] << 8 | devd[8]; 356 d->idProduct = devd[11] << 8 | devd[10]; 357 d->bcdDevice = devd[13] << 8 | devd[12]; 358 359 return 0; 360 } 361 362 int ifd_usb_get_config(ifd_device_t * dev, int n, 363 struct ifd_usb_config_descriptor *ret) 364 { 365 unsigned char *b; 366 unsigned short len; 367 int r; 368 memset(ret, 0, sizeof(struct ifd_usb_config_descriptor)); 369 370 /* 0x6 == USB_REQ_GET_DESCRIPTOR 371 * 0x2 == USB_DT_CONFIG 372 */ 373 r = ifd_usb_control(dev, 0x80, 0x6, 0x200 | n, 0, ret, 8, 1000); 374 if (r <= 0) { 375 ct_error("cannot get descriptors"); 376 return 1; 377 } 378 379 IFD_USB_LE16_TO_CPU(ret->wTotalLength); 380 len = ret->wTotalLength; 381 b = (unsigned char *)malloc(len); 382 if (!b) { 383 ct_error("cannot malloc descriptor buffer"); 384 return 1; 385 } 386 387 r = ifd_usb_control(dev, 0x80, 0x6, 0x200 | n, 0, b, len, 1000); 388 if (r < len) { 389 ct_error("cannot get descriptors"); 390 free(b); 391 return 1; 392 } 393 r = ifd_usb_parse_configuration(ret, b); 394 free(b); 395 if (r < 0) { 396 return 1; 397 } 398 return 0; 399 } 400 401 void ifd_usb_free_configuration(struct ifd_usb_config_descriptor *cf) 402 { 403 int i, j, k; 404 if (!cf->interface) 405 return; 406 407 for (i = 0; i < cf->bNumInterfaces; i++) { 408 struct ifd_usb_interface *ifp = &cf->interface[i]; 409 410 if (!ifp->altsetting) 411 break; 412 413 for (j = 0; j < ifp->num_altsetting; j++) { 414 struct ifd_usb_interface_descriptor *as = 415 &ifp->altsetting[j]; 416 417 if (as->extra) 418 free(as->extra); 419 420 if (!as->endpoint) 421 break; 422 423 for (k = 0; k < as->bNumEndpoints; k++) { 424 if (as->endpoint[k].extra) 425 free(as->endpoint[k].extra); 426 } 427 free(as->endpoint); 428 } 429 430 free(ifp->altsetting); 431 } 432 433 free(cf->interface); 434 } -
trunk/src/ifd/usb-descriptors.h
r506 r660 28 28 #define IFD_USB_DT_INTERFACE_SIZE 9 29 29 #define IFD_USB_DT_ENDPOINT_SIZE 7 30 #define IFD_USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */30 #define IFD_USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ 31 31 #define IFD_USB_DT_HUB_NONVAR_SIZE 7 32 32 33 33 struct ifd_usb_descriptor_header { 34 uint8_tbLength;35 uint8_tbDescriptorType;34 uint8_t bLength; 35 uint8_t bDescriptorType; 36 36 }; 37 37 38 38 /* String descriptor */ 39 39 struct ifd_usb_string_descriptor { 40 uint8_t bLength;41 uint8_t bDescriptorType;42 uint16_t wData[1];40 uint8_t bLength; 41 uint8_t bDescriptorType; 42 uint16_t wData[1]; 43 43 }; 44 44 … … 46 46 #define IFD_USB_MAXENDPOINTS 32 47 47 struct ifd_usb_endpoint_descriptor { 48 uint8_tbLength;49 uint8_tbDescriptorType;50 uint8_tbEndpointAddress;51 uint8_tbmAttributes;52 uint16_t wMaxPacketSize;53 uint8_tbInterval;54 uint8_tbRefresh;55 uint8_tbSynchAddress;48 uint8_t bLength; 49 uint8_t bDescriptorType; 50 uint8_t bEndpointAddress; 51 uint8_t bmAttributes; 52 uint16_t wMaxPacketSize; 53 uint8_t bInterval; 54 uint8_t bRefresh; 55 uint8_t bSynchAddress; 56 56 57 unsigned char *extra;/* Extra descriptors */58 int extralen;57 unsigned char *extra; /* Extra descriptors */ 58 int extralen; 59 59 }; 60 #define IFD_USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */60 #define IFD_USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ 61 61 #define IFD_USB_ENDPOINT_DIR_MASK 0x80 62 62 63 #define IFD_USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */63 #define IFD_USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ 64 64 #define IFD_USB_ENDPOINT_TYPE_CONTROL 0 65 65 #define IFD_USB_ENDPOINT_TYPE_ISOCHRONOUS 1 … … 70 70 #define IFD_USB_MAXINTERFACES 32 71 71 struct ifd_usb_interface_descriptor { 72 uint8_tbLength;73 uint8_tbDescriptorType;74 uint8_tbInterfaceNumber;75 uint8_tbAlternateSetting;76 uint8_tbNumEndpoints;77 uint8_tbInterfaceClass;78 uint8_tbInterfaceSubClass;79 uint8_tbInterfaceProtocol;80 uint8_tiInterface;72 uint8_t bLength; 73 uint8_t bDescriptorType; 74 uint8_t bInterfaceNumber; 75 uint8_t bAlternateSetting; 76 uint8_t bNumEndpoints; 77 uint8_t bInterfaceClass; 78 uint8_t bInterfaceSubClass; 79 uint8_t bInterfaceProtocol; 80 uint8_t iInterface; 81 81 82 struct ifd_usb_endpoint_descriptor *endpoint;82 struct ifd_usb_endpoint_descriptor *endpoint; 83 83 84 unsigned char *extra;/* Extra descriptors */85 int extralen;84 unsigned char *extra; /* Extra descriptors */ 85 int extralen; 86 86 }; 87 87 88 #define IFD_USB_MAXALTSETTING 128 /* Hard limit */88 #define IFD_USB_MAXALTSETTING 128 /* Hard limit */ 89 89 struct ifd_usb_interface { 90 struct ifd_usb_interface_descriptor *altsetting;90 struct ifd_usb_interface_descriptor *altsetting; 91 91 92 int num_altsetting;92 int num_altsetting; 93 93 }; 94 94 … … 96 96 #define IFD_USB_MAXCONFIG 8 97 97 struct ifd_usb_config_descriptor { 98 uint8_tbLength;99 uint8_tbDescriptorType;100 uint16_t wTotalLength;101 uint8_tbNumInterfaces;102 uint8_tbConfigurationValue;103 uint8_tiConfiguration;104 uint8_tbmAttributes;105 uint8_tMaxPower;98 uint8_t bLength; 99 uint8_t bDescriptorType; 100 uint16_t wTotalLength; 101 uint8_t bNumInterfaces; 102 uint8_t bConfigurationValue; 103 uint8_t iConfiguration; 104 uint8_t bmAttributes; 105 uint8_t MaxPower; 106 106 107 struct ifd_usb_interface *interface;107 struct ifd_usb_interface *interface; 108 108 109 unsigned char *extra;/* Extra descriptors */110 int extralen;109 unsigned char *extra; /* Extra descriptors */ 110 int extralen; 111 111 }; 112 112 113 113 /* Device descriptor */ 114 114 struct ifd_usb_device_descriptor { 115 uint8_tbLength;116 uint8_tbDescriptorType;117 uint16_t bcdUSB;118 uint8_tbDeviceClass;119 uint8_tbDeviceSubClass;120 uint8_tbDeviceProtocol;121 uint8_tbMaxPacketSize0;122 uint16_t idVendor;123 uint16_t idProduct;124 uint16_t bcdDevice;125 uint8_tiManufacturer;126 uint8_tiProduct;127 uint8_tiSerialNumber;128 uint8_tbNumConfigurations;115 uint8_t bLength; 116 uint8_t bDescriptorType; 117 uint16_t bcdUSB; 118 uint8_t bDeviceClass; 119 uint8_t bDeviceSubClass; 120 uint8_t bDeviceProtocol; 121 uint8_t bMaxPacketSize0; 122 uint16_t idVendor; 123 uint16_t idProduct; 124 uint16_t bcdDevice; 125 uint8_t iManufacturer; 126 uint8_t iProduct; 127 uint8_t iSerialNumber; 128 uint8_t bNumConfigurations; 129 129 }; 130 130 #define IFD_USB_REQ_GET_DESCRIPTOR 0x06 … … 143 143 #define IFD_USB_ENDPOINT_OUT 0x00 144 144 145 #define IFD_USB_LE16_TO_CPU(x) do { unsigned char *y=(unsigned char *)(&x); x = (y[1] << 8) | y[0] ; } while(0) 145 #define IFD_USB_LE16_TO_CPU(x) do { unsigned char *y=(unsigned char *)(&x); x = (y[1] << 8) | y[0] ; } while(0) 146 146 147 extern int ifd_usb_get_device(ifd_device_t * dev, 148 struct ifd_usb_device_descriptor *d); 147 149 148 extern int ifd_usb_get_device(ifd_device_t *dev, 149 struct ifd_usb_device_descriptor *d); 150 151 extern int ifd_usb_get_config(ifd_device_t *dev, 152 int n, struct ifd_usb_config_descriptor *c); 150 extern int ifd_usb_get_config(ifd_device_t * dev, 151 int n, struct ifd_usb_config_descriptor *c); 153 152 extern void ifd_usb_free_configuration(struct ifd_usb_config_descriptor *c); 154 153 -
trunk/src/ifd/usb.c
r642 r660 14 14 * Send/receive USB control block 15 15 */ 16 int 17 ifd_usb_control(ifd_device_t *dev, 18 unsigned int requesttype, 19 unsigned int request, 20 unsigned int value, 21 unsigned int index, 22 void *buffer, size_t len, 23 long timeout) 24 { 25 int n; 16 int ifd_usb_control(ifd_device_t * dev, unsigned int requesttype, 17 unsigned int request, unsigned int value, 18 unsigned int index, void *buffer, size_t len, long timeout) 19 { 20 int n; 26 21 27 22 if (dev->type != IFD_DEVICE_TYPE_USB) … … 31 26 32 27 if ((ct_config.debug >= 3) && !(requesttype & 0x80)) { 33 ifd_debug(4, "usb req type=x%02x req=x%02x val=x%04x ind=x%04x len=%u", 34 requesttype, 35 request, 36 value, 37 index, 38 len); 28 ifd_debug(4, 29 "usb req type=x%02x req=x%02x val=x%04x ind=x%04x len=%u", 30 requesttype, request, value, index, len); 39 31 if (len) 40 32 ifd_debug(4, "send %s", ct_hexdump(buffer, len)); … … 42 34 43 35 n = ifd_sysdep_usb_control(dev, requesttype, request, value, index, 44 buffer, len, timeout);36 buffer, len, timeout); 45 37 46 38 if ((ct_config.debug >= 3) && (requesttype & 0x80)) { 47 ifd_debug(4, "usb req type=x%02x req=x%02x val=x%04x ind=x%04x len=%d", 48 requesttype, 49 request, 50 value, 51 index, 52 n); 39 ifd_debug(4, 40 "usb req type=x%02x req=x%02x val=x%04x ind=x%04x len=%d", 41 requesttype, request, value, index, n); 53 42 if (n > 0) 54 43 ifd_debug(4, "recv %s", ct_hexdump(buffer, n)); … … 61 50 * USB frame capture 62 51 */ 63 int 64 ifd_usb_begin_capture(ifd_device_t *dev, int type, int endpoint, 65 size_t maxpacket, ifd_usb_capture_t **capret) 52 int ifd_usb_begin_capture(ifd_device_t * dev, int type, int endpoint, 53 size_t maxpacket, ifd_usb_capture_t ** capret) 66 54 { 67 55 if (dev->type != IFD_DEVICE_TYPE_USB) … … 70 58 if (ct_config.debug >= 5) 71 59 ifd_debug(5, "usb capture type=%d ep=x%x maxpacket=%u", 72 type, endpoint, maxpacket);73 return ifd_sysdep_usb_begin_capture(dev, type, endpoint, maxpacket, capret);74 } 75 76 int 77 i fd_usb_capture(ifd_device_t *dev, ifd_usb_capture_t *cap,78 void *buffer,size_t len, long timeout)79 { 80 int rc;60 type, endpoint, maxpacket); 61 return ifd_sysdep_usb_begin_capture(dev, type, endpoint, maxpacket, 62 capret); 63 } 64 65 int ifd_usb_capture(ifd_device_t * dev, ifd_usb_capture_t * cap, void *buffer, 66 size_t len, long timeout) 67 { 68 int rc; 81 69 82 70 if (dev->type != IFD_DEVICE_TYPE_USB) … … 89 77 ifd_debug(1, "usb capture: %s", ct_strerror(rc)); 90 78 if (rc > 0) 91 ifd_debug(5, "usb capture: recv %s", ct_hexdump(buffer, rc)); 79 ifd_debug(5, "usb capture: recv %s", 80 ct_hexdump(buffer, rc)); 92 81 if (rc == 0) 93 82 ifd_debug(5, "usb capture: rc=%d (timeout?)", rc); … … 96 85 } 97 86 98 int 99 ifd_usb_end_capture(ifd_device_t *dev, ifd_usb_capture_t *cap) 87 int ifd_usb_end_capture(ifd_device_t * dev, ifd_usb_capture_t * cap) 100 88 { 101 89 if (dev->type != IFD_DEVICE_TYPE_USB) … … 107 95 * Set usb params (for now, endpoint for transceive) 108 96 */ 109 static int 110 usb_set_params(ifd_device_t *dev, const ifd_device_params_t *params)97 static int usb_set_params(ifd_device_t * dev, 98 const ifd_device_params_t * params) 111 99 { 112 100 113 101 ifd_debug(1, "called. config x%02x ifc x%02x eps x%02x/x%02x", 114 params->usb.configuration, params->usb.interface,115 params->usb.ep_o, params->usb.ep_i);116 if (params->usb.interface != -1 && params->usb.interface > 255)102 params->usb.configuration, params->usb.interface, 103 params->usb.ep_o, params->usb.ep_i); 104 if (params->usb.interface != -1 && params->usb.interface > 255) 117 105 return IFD_ERROR_INVALID_ARG; 118 if (params->usb.ep_o != -1 && (params->usb.ep_o & ~0x7F))106 if (params->usb.ep_o != -1 && (params->usb.ep_o & ~0x7F)) 119 107 return IFD_ERROR_INVALID_ARG; 120 if ((params->usb.ep_i != -1 && (params->usb.ep_i & ~0xFF))121 || !(params->usb.ep_i & 0x80))108 if ((params->usb.ep_i != -1 && (params->usb.ep_i & ~0xFF)) 109 || !(params->usb.ep_i & 0x80)) 122 110 return IFD_ERROR_INVALID_ARG; 123 111 124 112 if (dev->settings.usb.interface != -1) 125 ifd_sysdep_usb_release_interface(dev, 126 dev->settings.usb.interface);113 ifd_sysdep_usb_release_interface(dev, 114 dev->settings.usb.interface); 127 115 128 116 if (params->usb.configuration != -1 129 && ifd_sysdep_usb_set_configuration(dev, params->usb.configuration))117 && ifd_sysdep_usb_set_configuration(dev, params->usb.configuration)) 130 118 return -1; 131 119 … … 134 122 return -1; 135 123 if (params->usb.altsetting != -1 136 && ifd_sysdep_usb_set_interface(dev,137 params->usb.interface,138 params->usb.altsetting))124 && ifd_sysdep_usb_set_interface(dev, 125 params->usb.interface, 126 params->usb.altsetting)) 139 127 return -1; 140 128 } 141 129 142 dev->settings = *params;143 return 0;144 } 145 146 static int 147 usb_send(ifd_device_t *dev, const unsigned char *send,size_t sendlen)130 dev->settings = *params; 131 return 0; 132 } 133 134 static int usb_send(ifd_device_t * dev, const unsigned char *send, 135 size_t sendlen) 148 136 { 149 137 if (dev->settings.usb.ep_o == -1) … … 156 144 157 145 return ifd_sysdep_usb_bulk(dev, 158 dev->settings.usb.ep_o, 159 (unsigned char *) send, sendlen, 10000); 160 } 161 162 163 static int 164 usb_recv(ifd_device_t *dev, unsigned char *recv, size_t recvlen, long timeout) 165 { 166 int rc; 146 dev->settings.usb.ep_o, 147 (unsigned char *)send, sendlen, 10000); 148 } 149 150 static int usb_recv(ifd_device_t * dev, unsigned char *recv, size_t recvlen, 151 long timeout) 152 { 153 int rc; 167 154 168 155 if (dev->settings.usb.ep_i == -1) 169 156 return IFD_ERROR_NOT_SUPPORTED; 170 157 171 rc = ifd_sysdep_usb_bulk(dev, dev->settings.usb.ep_i, 172 recv, recvlen, timeout);158 rc = ifd_sysdep_usb_bulk(dev, dev->settings.usb.ep_i, 159 recv, recvlen, timeout); 173 160 if (rc >= 0 && ct_config.debug >= 4) { 174 161 ifd_debug(4, "usb recv from=x%02x", dev->settings.usb.ep_i); … … 180 167 } 181 168 182 static struct ifd_device_ops ifd_usb_ops;169 static struct ifd_device_ops ifd_usb_ops; 183 170 184 171 /* 185 172 * Open USB device 186 173 */ 187 ifd_device_t * 188 ifd_open_usb(const char *device) 189 { 190 ifd_device_t *dev; 191 int fd; 174 ifd_device_t *ifd_open_usb(const char *device) 175 { 176 ifd_device_t *dev; 177 int fd; 192 178 193 179 if ((fd = ifd_sysdep_usb_open(device, O_EXCL | O_RDWR)) < 0) {
Note: See TracChangeset
for help on using the changeset viewer.
