Changeset 661 for trunk/src/ifd/sys-bsd.c
- Timestamp:
- 05/29/05 12:48:54 (7 years ago)
- File:
-
- 1 edited
-
trunk/src/ifd/sys-bsd.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/ifd/sys-bsd.c
r638 r661 28 28 #include "usb-descriptors.h" 29 29 30 int 31 ifd_sysdep_device_type(const char *name) 30 int ifd_sysdep_device_type(const char *name) 32 31 { 33 32 struct stat stb; … … 50 49 * Poll for presence of USB device 51 50 */ 52 int 53 ifd_sysdep_usb_poll_presence(ifd_device_t *dev, struct pollfd *pfd) 51 int ifd_sysdep_usb_poll_presence(ifd_device_t * dev, struct pollfd *pfd) 54 52 { 55 53 if (pfd->revents & POLLHUP) … … 61 59 62 60 typedef struct ep { 63 int ep_fd;61 int ep_fd; 64 62 } ep_t; 65 63 … … 73 71 * Open interface endpoint 74 72 */ 75 int 76 open_ep(char *name, int interface, int endpoint, int flags) 77 { 78 char filename[256]; 79 80 if(interfaces[interface][endpoint].ep_fd) { 81 ifd_debug(6, "open_ep: endpoint already opened"); 82 return 0; 83 } 84 85 sprintf((char *)&filename,"%s.%d", name, endpoint); 86 87 if((interfaces[interface][endpoint].ep_fd=open( 88 filename, flags)) < 0) { 89 ifd_debug(6, "open_ep: error opening \"%s\": %s", filename, strerror(errno)); 90 interfaces[interface][endpoint].ep_fd=0; 91 return -1; 92 } 93 return 0; 73 int open_ep(char *name, int interface, int endpoint, int flags) 74 { 75 char filename[256]; 76 77 if (interfaces[interface][endpoint].ep_fd) { 78 ifd_debug(6, "open_ep: endpoint already opened"); 79 return 0; 80 } 81 82 sprintf((char *)&filename, "%s.%d", name, endpoint); 83 84 if ((interfaces[interface][endpoint].ep_fd = open(filename, flags)) < 0) { 85 ifd_debug(6, "open_ep: error opening \"%s\": %s", filename, 86 strerror(errno)); 87 interfaces[interface][endpoint].ep_fd = 0; 88 return -1; 89 } 90 return 0; 94 91 } 95 92 96 93 close_ep(int interface, int endpoint) 97 94 { 98 if(interfaces[interface][endpoint].ep_fd) { 99 close(interfaces[interface][endpoint].ep_fd); 100 interfaces[interface][endpoint].ep_fd=0; 101 } 102 } 103 104 int 105 ifd_sysdep_usb_bulk(ifd_device_t *dev, int ep, void *buffer, size_t len, long timeout) 106 { 107 int bytes_to_process; 108 int bytes_processed; 109 int direction = (ep & IFD_USB_ENDPOINT_DIR_MASK) == IFD_USB_ENDPOINT_IN ? 1 : 0; 110 int endpoint = (ep & ~IFD_USB_ENDPOINT_DIR_MASK); 111 112 ct_debug("ifd_sysdep_usb_bulk: endpoint=%d direction=%d", endpoint, direction); 113 if(open_ep(dev->name,0,endpoint,O_RDWR|O_NONBLOCK)) { 114 ct_debug("ifd_sysdep_usb_bulk: opening endpoint failed"); 115 return -1; 116 } 117 if(direction) { 118 if((bytes_to_process=read(interfaces[0][endpoint].ep_fd,buffer,len))<0) { 119 ifd_debug(6, "ifd_sysdep_usb_bulk: read failed: %s", strerror(errno)); 120 ct_error("usb_bulk read failed: %s", strerror(errno)); 121 return IFD_ERROR_COMM_ERROR; 122 } 123 ct_debug("ifd_sysdep_usb_bulk: read %d bytes", bytes_to_process); 95 if (interfaces[interface][endpoint].ep_fd) { 96 close(interfaces[interface][endpoint].ep_fd); 97 interfaces[interface][endpoint].ep_fd = 0; 98 } 99 } 100 101 int ifd_sysdep_usb_bulk(ifd_device_t * dev, int ep, void *buffer, size_t len, 102 long timeout) 103 { 104 int bytes_to_process; 105 int bytes_processed; 106 int direction = 107 (ep & IFD_USB_ENDPOINT_DIR_MASK) == IFD_USB_ENDPOINT_IN ? 1 : 0; 108 int endpoint = (ep & ~IFD_USB_ENDPOINT_DIR_MASK); 109 110 ct_debug("ifd_sysdep_usb_bulk: endpoint=%d direction=%d", endpoint, 111 direction); 112 if (open_ep(dev->name, 0, endpoint, O_RDWR | O_NONBLOCK)) { 113 ct_debug("ifd_sysdep_usb_bulk: opening endpoint failed"); 114 return -1; 115 } 116 if (direction) { 117 if ((bytes_to_process = 118 read(interfaces[0][endpoint].ep_fd, buffer, len)) < 0) { 119 ifd_debug(6, "ifd_sysdep_usb_bulk: read failed: %s", 120 strerror(errno)); 121 ct_error("usb_bulk read failed: %s", strerror(errno)); 122 return IFD_ERROR_COMM_ERROR; 123 } 124 ct_debug("ifd_sysdep_usb_bulk: read %d bytes", 125 bytes_to_process); 126 return bytes_to_process; 127 } else { 128 bytes_to_process = len; 129 if ((bytes_processed = 130 write(interfaces[0][endpoint].ep_fd, buffer, 131 bytes_to_process)) != bytes_to_process) { 132 ifd_debug(6, "ifd_sysdep_usb_bulk: write failed: %s", 133 strerror(errno)); 134 ct_error("usb_bulk write failed: %s", strerror(errno)); 135 return IFD_ERROR_COMM_ERROR; 136 } 137 ct_debug("ifd_sysdep_usb_bulk: wrote buffer[%d]=%s", 138 bytes_processed, ct_hexdump(buffer, len)); 139 return bytes_processed; 140 } 141 } 142 143 /* 144 * USB URB capture 145 */ 146 struct ifd_usb_capture { 147 int type; 148 int endpoint; 149 size_t maxpacket; 150 unsigned int interface; 151 }; 152 153 int ifd_sysdep_usb_begin_capture(ifd_device_t * dev, int type, int ep, 154 size_t maxpacket, ifd_usb_capture_t ** capret) 155 { 156 ifd_usb_capture_t *cap; 157 int direction = 158 (ep & IFD_USB_ENDPOINT_DIR_MASK) == IFD_USB_ENDPOINT_IN ? 1 : 0; 159 int endpoint = (ep & ~IFD_USB_ENDPOINT_DIR_MASK); 160 161 if (!(cap = (ifd_usb_capture_t *) calloc(1, sizeof(*cap) + maxpacket))) { 162 ct_debug("ifd_sysdep_usb_begin_capture: calloc failed"); 163 return -1; 164 } 165 cap->type = type; 166 cap->endpoint = ep; 167 cap->maxpacket = maxpacket; 168 169 if (!interfaces[0][endpoint].ep_fd) { 170 if (open_ep(dev->name, 0, endpoint, O_RDONLY | O_NONBLOCK)) { 171 ct_debug 172 ("ifd_sysdep_usb_begin_capture: opening endpoint failed"); 173 return -1; 174 } 175 } 176 *capret = cap; 177 return 0; 178 } 179 180 int ifd_sysdep_usb_capture(ifd_device_t * dev, ifd_usb_capture_t * cap, 181 void *buffer, size_t len, long timeout) 182 { 183 struct timeval begin; 184 int bytes_to_process = 0; 185 int direction = 186 (cap->endpoint & IFD_USB_ENDPOINT_DIR_MASK) == 187 IFD_USB_ENDPOINT_IN ? 1 : 0; 188 int endpoint = (cap->endpoint & ~IFD_USB_ENDPOINT_DIR_MASK); 189 190 gettimeofday(&begin, NULL); 191 do { 192 struct pollfd pfd; 193 long wait; 194 195 if ((wait = (timeout - ifd_time_elapsed(&begin))) <= 0) 196 return IFD_ERROR_TIMEOUT; 197 198 pfd.fd = interfaces[0][endpoint].ep_fd; 199 pfd.events = POLLIN; 200 if (poll(&pfd, 1, wait) != 1) 201 continue; 202 203 if ((bytes_to_process = 204 read(interfaces[0][endpoint].ep_fd, buffer, len)) < 0) { 205 ifd_debug(6, "ifd_sysdep_usb_bulk: read failed: %s", 206 strerror(errno)); 207 ct_error("usb_bulk read failed: %s", strerror(errno)); 208 return IFD_ERROR_COMM_ERROR; 209 } 210 } while (!bytes_to_process); 211 ct_debug("ifd_sysdep_usb_capture: read buffer[%d]=%s", bytes_to_process, 212 ct_hexdump(buffer, bytes_to_process)); 124 213 return bytes_to_process; 125 } else { 126 bytes_to_process=len; 127 if((bytes_processed=write(interfaces[0][endpoint].ep_fd,buffer,bytes_to_process))!=bytes_to_process) { 128 ifd_debug(6, "ifd_sysdep_usb_bulk: write failed: %s", strerror(errno)); 129 ct_error("usb_bulk write failed: %s", strerror(errno)); 130 return IFD_ERROR_COMM_ERROR; 131 } 132 ct_debug("ifd_sysdep_usb_bulk: wrote buffer[%d]=%s", bytes_processed, ct_hexdump(buffer,len)); 133 return bytes_processed; 134 } 135 } 136 137 /* 138 * USB URB capture 139 */ 140 struct ifd_usb_capture { 141 int type; 142 int endpoint; 143 size_t maxpacket; 144 unsigned int interface; 145 }; 146 147 int 148 ifd_sysdep_usb_begin_capture(ifd_device_t *dev, 149 int type, int ep, size_t maxpacket, 150 ifd_usb_capture_t **capret) 151 { 152 ifd_usb_capture_t *cap; 153 int direction = (ep & IFD_USB_ENDPOINT_DIR_MASK) == IFD_USB_ENDPOINT_IN ? 1 : 0; 154 int endpoint = (ep & ~IFD_USB_ENDPOINT_DIR_MASK); 155 156 if(!(cap = (ifd_usb_capture_t *) calloc(1, sizeof(*cap) + maxpacket))) { 157 ct_debug("ifd_sysdep_usb_begin_capture: calloc failed"); 158 return -1; 159 } 160 cap->type = type; 161 cap->endpoint = ep; 162 cap->maxpacket = maxpacket; 163 164 if(!interfaces[0][endpoint].ep_fd) { 165 if(open_ep(dev->name,0,endpoint,O_RDONLY|O_NONBLOCK)) { 166 ct_debug("ifd_sysdep_usb_begin_capture: opening endpoint failed"); 167 return -1; 168 } 169 } 170 *capret = cap; 171 return 0; 172 } 173 174 int 175 ifd_sysdep_usb_capture(ifd_device_t *dev, 176 ifd_usb_capture_t *cap, 177 void *buffer, size_t len, 178 long timeout) 179 { 180 struct timeval begin; 181 int bytes_to_process=0; 182 int direction = (cap->endpoint & IFD_USB_ENDPOINT_DIR_MASK) == IFD_USB_ENDPOINT_IN ? 1 : 0; 183 int endpoint = (cap->endpoint & ~IFD_USB_ENDPOINT_DIR_MASK); 184 185 gettimeofday(&begin,NULL); 186 do { 187 struct pollfd pfd; 188 long wait; 189 190 if ((wait = (timeout - ifd_time_elapsed(&begin))) <= 0) 191 return IFD_ERROR_TIMEOUT; 192 193 pfd.fd = interfaces[0][endpoint].ep_fd; 194 pfd.events = POLLIN; 195 if(poll(&pfd,1,wait)!=1) 196 continue; 197 198 if((bytes_to_process=read(interfaces[0][endpoint].ep_fd,buffer,len))<0) { 199 ifd_debug(6, "ifd_sysdep_usb_bulk: read failed: %s", strerror(errno)); 200 ct_error("usb_bulk read failed: %s", strerror(errno)); 201 return IFD_ERROR_COMM_ERROR; 202 } 203 } while (!bytes_to_process); 204 ct_debug("ifd_sysdep_usb_capture: read buffer[%d]=%s", bytes_to_process, ct_hexdump(buffer,bytes_to_process)); 205 return bytes_to_process; 206 } 207 208 int 209 ifd_sysdep_usb_end_capture(ifd_device_t *dev, ifd_usb_capture_t *cap) 210 { 211 int direction = (cap->endpoint & IFD_USB_ENDPOINT_DIR_MASK) == IFD_USB_ENDPOINT_IN ? 1 : 0; 212 int endpoint = (cap->endpoint & ~IFD_USB_ENDPOINT_DIR_MASK); 213 close_ep(0,endpoint); 214 if(cap) free(cap); 215 return 0; 214 } 215 216 int ifd_sysdep_usb_end_capture(ifd_device_t * dev, ifd_usb_capture_t * cap) 217 { 218 int direction = 219 (cap->endpoint & IFD_USB_ENDPOINT_DIR_MASK) == 220 IFD_USB_ENDPOINT_IN ? 1 : 0; 221 int endpoint = (cap->endpoint & ~IFD_USB_ENDPOINT_DIR_MASK); 222 close_ep(0, endpoint); 223 if (cap) 224 free(cap); 225 return 0; 216 226 } 217 227 … … 219 229 * USB control command 220 230 */ 221 int 222 ifd_sysdep_usb_control(ifd_device_t *dev, 223 unsigned int requesttype, 224 unsigned int request, 225 unsigned int value, 226 unsigned int index, 227 void *data, size_t len, long timeout) 231 int ifd_sysdep_usb_control(ifd_device_t * dev, unsigned int requesttype, 232 unsigned int request, unsigned int value, 233 unsigned int index, void *data, size_t len, 234 long timeout) 228 235 { 229 236 struct usb_ctl_request ctrl; 230 int rc,val;237 int rc, val; 231 238 232 239 ifd_debug(1, "BSD: ifd_sysdep_usb_control(0x%x)", request); 233 240 memset(&ctrl, 0, sizeof(ctrl)); 234 241 235 242 ctrl.ucr_request.bmRequestType = requesttype; 236 243 ctrl.ucr_request.bRequest = request; … … 243 250 244 251 ifd_debug(1, "BSD: CTRL bmRequestType 0x%x bRequest 0x%x " 245 "wValue 0x%x wIndex 0x%x wLength 0x%x",246 requesttype, request, value, index, len);247 if (len)248 ifd_debug(5, "BSD: CTRL SEND data %s", ct_hexdump(data, len));252 "wValue 0x%x wIndex 0x%x wLength 0x%x", 253 requesttype, request, value, index, len); 254 if (len) 255 ifd_debug(5, "BSD: CTRL SEND data %s", ct_hexdump(data, len)); 249 256 250 257 val = timeout; 251 258 if ((rc = ioctl(dev->fd, USB_SET_TIMEOUT, &val)) < 0) { 252 ifd_debug(1, "USB_SET_TIMEOUT failed: %d", rc);259 ifd_debug(1, "USB_SET_TIMEOUT failed: %d", rc); 253 260 ct_error("usb_set_timeout failed: %s(%d)", 254 strerror(errno), errno);255 return IFD_ERROR_COMM_ERROR;256 } 257 261 strerror(errno), errno); 262 return IFD_ERROR_COMM_ERROR; 263 } 264 258 265 if ((rc = ioctl(dev->fd, USB_DO_REQUEST, &ctrl)) < 0) { 259 266 ifd_debug(1, "USB_DO_REQUEST failed: %d", rc); 260 267 ct_error("usb_do_request failed: %s (%d)", 261 strerror(errno), errno);268 strerror(errno), errno); 262 269 return IFD_ERROR_COMM_ERROR; 263 }264 265 if (ctrl.ucr_data==NULL)270 } 271 272 if (ctrl.ucr_data == NULL) 266 273 ifd_debug(1, "BSD: ctrl.ucr_data == NULL "); 267 if (ctrl.ucr_data && ctrl.ucr_actlen)274 if (ctrl.ucr_data && ctrl.ucr_actlen) 268 275 ifd_debug(1, "BSD: CTRL RECV data %s", 269 ct_hexdump(ctrl.ucr_data,ctrl.ucr_actlen));276 ct_hexdump(ctrl.ucr_data, ctrl.ucr_actlen)); 270 277 return ctrl.ucr_actlen; 271 278 } 272 279 273 int 274 ifd_sysdep_usb_set_configuration(ifd_device_t *dev, int config) 275 { 276 int value, rc; 277 value = config; 278 if ((rc = ioctl(dev->fd, USB_SET_CONFIG, &value)) < 0) { 279 ifd_debug(1,"USB_SET_CONFIG failed: %d", rc); 280 ct_error("usb_set_configuration failed: %s(%d)", 281 strerror(errno), errno); 282 return IFD_ERROR_COMM_ERROR; 283 } 284 return 0; 285 } 286 287 288 int 289 ifd_sysdep_usb_set_interface(ifd_device_t *dev, int ifc, int alt) 290 { 291 int rc; 292 struct usb_alt_interface { 293 int uai_config_index; 294 int uai_interface_index; 295 int uai_alt_no; 296 } value; 297 298 value.uai_config_index=ifc; 299 value.uai_interface_index=0; 300 value.uai_alt_no=alt; 301 if ((rc = ioctl(dev->fd, USB_SET_ALTINTERFACE, &value)) < 0) { 302 ifd_debug(1,"USB_SET_ALTINTERFACE failed: %d", rc); 303 ct_error("usb_set_interface failed: %s(%d)", 304 strerror(errno), errno); 305 return IFD_ERROR_COMM_ERROR; 306 } 307 return 0; 308 } 309 310 int 311 ifd_sysdep_usb_claim_interface(ifd_device_t *dev, int interface) 312 { 313 ct_debug("ifd_sysdep_usb_claim_interface: interface=%d (not yet implemented)", interface); 314 return 0; 315 } 316 317 int 318 ifd_sysdep_usb_release_interface(ifd_device_t *dev, int interface) 319 { 320 ct_debug("ifd_sysdep_usb_release_interface: interface=%d (not yet implemented)", interface); 321 return 0; 322 } 323 324 int 325 ifd_sysdep_usb_open(char *device, int flags) 326 { 327 return open(device, O_EXCL | O_RDWR); 280 int ifd_sysdep_usb_set_configuration(ifd_device_t * dev, int config) 281 { 282 int value, rc; 283 value = config; 284 if ((rc = ioctl(dev->fd, USB_SET_CONFIG, &value)) < 0) { 285 ifd_debug(1, "USB_SET_CONFIG failed: %d", rc); 286 ct_error("usb_set_configuration failed: %s(%d)", 287 strerror(errno), errno); 288 return IFD_ERROR_COMM_ERROR; 289 } 290 return 0; 291 } 292 293 int ifd_sysdep_usb_set_interface(ifd_device_t * dev, int ifc, int alt) 294 { 295 int rc; 296 struct usb_alt_interface { 297 int uai_config_index; 298 int uai_interface_index; 299 int uai_alt_no; 300 } value; 301 302 value.uai_config_index = ifc; 303 value.uai_interface_index = 0; 304 value.uai_alt_no = alt; 305 if ((rc = ioctl(dev->fd, USB_SET_ALTINTERFACE, &value)) < 0) { 306 ifd_debug(1, "USB_SET_ALTINTERFACE failed: %d", rc); 307 ct_error("usb_set_interface failed: %s(%d)", 308 strerror(errno), errno); 309 return IFD_ERROR_COMM_ERROR; 310 } 311 return 0; 312 } 313 314 int ifd_sysdep_usb_claim_interface(ifd_device_t * dev, int interface) 315 { 316 ct_debug 317 ("ifd_sysdep_usb_claim_interface: interface=%d (not yet implemented)", 318 interface); 319 return 0; 320 } 321 322 int ifd_sysdep_usb_release_interface(ifd_device_t * dev, int interface) 323 { 324 ct_debug 325 ("ifd_sysdep_usb_release_interface: interface=%d (not yet implemented)", 326 interface); 327 return 0; 328 } 329 330 int ifd_sysdep_usb_open(char *device, int flags) 331 { 332 return open(device, O_EXCL | O_RDWR); 328 333 } 329 334 … … 331 336 * Scan all usb devices to see if there is one we support 332 337 */ 333 int 334 ifd_scan_usb(void) 335 { 336 int i, controller_fd; 337 char controller_devname[10]; 338 339 ifd_debug(1, "BSD: ifd_scan_usb"); 340 for (i = 0; i < 10; i++) { 341 snprintf(controller_devname, 10, "/dev/usb%d", i); 342 if((controller_fd = open(controller_devname, O_RDONLY))<0) 343 continue; 344 345 if (controller_fd >= 0) { 346 int address; 347 for (address = 1; address < USB_MAX_DEVICES; address++) { 348 struct usb_device_info device_info; 349 ifd_devid_t id; 350 const char *driver; 351 char device[256]; 352 353 device_info.udi_addr = address; 354 355 if(ioctl(controller_fd, USB_DEVICEINFO, &device_info)) { 356 if (errno != ENXIO) 357 fprintf(stderr, "addr %d: I/O error\n", address); 358 continue; 359 } 360 361 if(strncmp(device_info.udi_devnames[0],"ugen",4)!=0) 362 continue; 363 364 id.type = IFD_DEVICE_TYPE_USB; 365 id.num = 2; 366 367 id.val[0] = device_info.udi_vendorNo; 368 id.val[1] = device_info.udi_productNo; 369 370 ifd_debug(1, "BSD: ifd_scan_usb: " 371 "ifd_driver_for(%s[0x%04x].%s[0x%04x)", 372 device_info.udi_vendor, 373 device_info.udi_vendorNo, 374 device_info.udi_product, 375 device_info.udi_productNo); 376 377 if (!(driver = ifd_driver_for_id(&id))) 378 continue; 379 380 snprintf(device, sizeof(device), 381 "/dev/%s", device_info.udi_devnames[0]); 382 383 ifd_spawn_handler(driver, device, -1); 384 } 385 close(controller_fd); 386 } else { 387 if (errno == ENOENT || errno == ENXIO) 388 continue; 389 /* a more suitable error recovery should be done here */ 390 } 391 } 392 return 0; 393 } 394 #endif /* __Net/Free/OpenBSD__ */ 338 int ifd_scan_usb(void) 339 { 340 int i, controller_fd; 341 char controller_devname[10]; 342 343 ifd_debug(1, "BSD: ifd_scan_usb"); 344 for (i = 0; i < 10; i++) { 345 int address; 346 347 snprintf(controller_devname, 10, "/dev/usb%d", i); 348 if ((controller_fd = open(controller_devname, O_RDONLY)) < 0) 349 continue; 350 351 if (controller_fd < 0) { 352 if (errno == ENOENT || errno == ENXIO) 353 continue; 354 /* a more suitable error recovery should be done here */ 355 continue; 356 } 357 for (address = 1; address < USB_MAX_DEVICES; address++) { 358 struct usb_device_info device_info; 359 ifd_devid_t id; 360 const char *driver; 361 char device[256]; 362 363 device_info.udi_addr = address; 364 365 if (ioctl(controller_fd, USB_DEVICEINFO, &device_info)) { 366 if (errno != ENXIO) 367 fprintf(stderr, 368 "addr %d: I/O error\n", 369 address); 370 continue; 371 } 372 373 if (strncmp 374 (device_info.udi_devnames[0], "ugen", 4) != 0) 375 continue; 376 377 id.type = IFD_DEVICE_TYPE_USB; 378 id.num = 2; 379 380 id.val[0] = device_info.udi_vendorNo; 381 id.val[1] = device_info.udi_productNo; 382 383 ifd_debug(1, "BSD: ifd_scan_usb: " 384 "ifd_driver_for(%s[0x%04x].%s[0x%04x)", 385 device_info.udi_vendor, 386 device_info.udi_vendorNo, 387 device_info.udi_product, 388 device_info.udi_productNo); 389 390 if (!(driver = ifd_driver_for_id(&id))) 391 continue; 392 393 snprintf(device, sizeof(device), 394 "/dev/%s", device_info.udi_devnames[0]); 395 396 ifd_spawn_handler(driver, device, -1); 397 } 398 close(controller_fd); 399 } 400 return 0; 401 } 402 #endif /* __Net/Free/OpenBSD__ */
Note: See TracChangeset
for help on using the changeset viewer.
