36 # define sleep(x) Sleep((x)*1000) 50 for (i = 0; i < cnt; ++i)
71 if (cnt <= 0)
return NULL;
73 int cameraBusNo = libusb_get_bus_number(camera);
74 if (cameraBusNo < 0)
return NULL;
78 for (i = 0; i < cnt; i++)
92 int audioBusNo = libusb_get_bus_number(deviceList[i]);
93 if (audioBusNo == cameraBusNo)
97 libusb_device * audioParent = libusb_get_parent(deviceList[i]);
98 if (cameraParent == audioParent)
100 return deviceList[i];
114 *attribute_list = NULL;
129 for (i = 0; i < count; i++)
156 unsigned char serial[256];
165 const char*
const K4W_1473_SERIAL =
"0000000000000000";
166 if (strncmp((
const char*)serial, K4W_1473_SERIAL, 16) == 0)
170 if (audio_device != NULL)
201 memset(current_attr, 0,
sizeof(*current_attr));
204 *next_attr = current_attr;
205 next_attr = &(current_attr->
next);
274 int i = 0, nr_cam = 0, nr_mot = 0;
279 for (i = 0; i < cnt; i++)
295 if (res < 0 || !dev->usb_cam.dev)
297 FN_ERROR(
"Could not open camera: %d\n", res);
314 if (audioDevice != NULL)
321 FN_ERROR(
"Failed to set the LED of K4W or 1473 device: %d\n", res);
327 libusb_reset_device(audioHandle);
336 FN_ERROR(
"Unable to claim interface %d\n", res);
362 res = libusb_kernel_driver_active(dev->
usb_cam.
dev, 0);
365 res = libusb_detach_kernel_driver(dev->
usb_cam.
dev, 0);
368 FN_ERROR(
"Could not detach kernel driver for camera: %d\n", res);
378 FN_ERROR(
"Could not claim interface on camera: %d\n", res);
388 FN_ERROR(
"Failed to set alternate interface #1 for K4W: %d\n", res);
406 for (i = 0; i < cnt; i++)
420 if (res < 0 || !dev->usb_motor.dev)
422 FN_ERROR(
"Could not open motor: %d\n", res);
429 FN_ERROR(
"Could not claim interface on motor: %d\n", res);
445 if (nr_audio == index)
448 if (res < 0 || !dev->usb_audio.dev)
450 FN_ERROR(
"Could not open audio: %d\n", res);
457 FN_ERROR(
"Could not claim interface on audio: %d\n", res);
471 if (num_interfaces >= 2)
481 unsigned char string_desc[256];
485 FN_ERROR(
"Failed to retrieve serial number for audio device in bootloader state\n");
488 char* audio_serial = strdup((
char*)string_desc);
490 FN_SPEW(
"Uploading firmware to audio device in bootloader state.\n");
495 FN_SPEW(
"loading firmware from memory\n");
500 FN_SPEW(
"loading firmware from memory\n");
510 FN_ERROR(
"upload_firmware failed: %d\n", res);
517 for (loops = 0; loops < 10; loops++)
519 FN_SPEW(
"Try %d: Looking for new audio device matching serial %s\n", loops, audio_serial);
524 for (dev_index = 0; dev_index < num_new_devs; ++dev_index)
537 r =
libusb_open(new_dev_list[dev_index], &new_dev_handle);
544 FN_SPEW(
"Lost new audio device while fetching serial number.\n");
549 if (r == strlen(audio_serial) && strcmp((
char*)string_desc, audio_serial) == 0)
556 FN_SPEW(
"Device with serial %s reappeared but couldn't claim interface 0\n", audio_serial);
566 if (num_interfaces >= 2)
575 FN_SPEW(
"Opened audio with matching serial but too few interfaces.\n");
583 FN_SPEW(
"Got serial %s, expected serial %s\n", (
char*)string_desc, audio_serial);
623 FN_ERROR(
"Failed to open camera subdevice or it is not disabled.");
633 FN_ERROR(
"Failed to open motor subddevice or it is not disabled.");
643 FN_ERROR(
"Failed to open audio subdevice or it is not disabled.");
655 libusb_attach_kernel_driver(dev->
usb_cam.
dev, 0);
689 for (i=0; i<strm->
pkts; i++) {
696 FN_ERROR(
"iso_callback(): failed to resubmit transfer after successful completion: %d\n", res);
710 FN_ERROR(
"USB device disappeared, cancelling stream %02x :(\n", xfer->
endpoint);
727 FN_ERROR(
"Got cancelled transfer, but we didn't request it - device disconnected?\n");
744 FN_ERROR(
"Isochronous transfer resubmission failed after unknown error: %d\n", res);
772 for (i=0; i<xfers; i++) {
773 FN_SPEW(
"Creating EP %02x transfer #%d\n", ep, i);
782 FN_WARNING(
"Failed to submit isochronous transfer %d: %d\n", i, ret);
798 FN_FLOOD(
"fnusb_stop_iso() called\n");
804 FN_FLOOD(
"fnusb_stop_iso() cancelled all transfers\n");
813 FN_FLOOD(
"fnusb_stop_iso() freed all transfers\n");
818 FN_FLOOD(
"fnusb_stop_iso() freed buffers and stream\n");
819 FN_FLOOD(
"fnusb_stop_iso() done\n");
837 struct libusb_config_descriptor* config;
838 res = libusb_get_active_config_descriptor(d, &config);
841 retval = config->bNumInterfaces;
842 libusb_free_config_descriptor(config);
FN_INTERNAL int fnusb_bulk(fnusb_dev *dev, uint8_t endpoint, uint8_t *data, int len, int *transferred)
freenect_device_flags enabled_subdevices
int libusb_handle_events_timeout(libusb_context *ctx, struct timeval *timeout)
FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
int libusb_handle_events(libusb_context *ctx)
void libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, unsigned int length)
freenect_context * parent
unsigned char * fn_fw_k4w_ptr
int libusb_get_device_descriptor(libusb_device *dev, struct libusb_device_descriptor *desc)
FN_INTERNAL int fnusb_start_iso(fnusb_dev *dev, fnusb_isoc_stream *strm, fnusb_iso_cb cb, int ep, int xfers, int pkts, int len)
int device_does_motor_control_with_audio
enum libusb_transfer_status status
FN_INTERNAL libusb_device * fnusb_find_connected_audio_device(libusb_device *camera, libusb_device **deviceList, int cnt)
unsigned int fn_fw_k4w_size
void freenect_usb_context
const char * camera_serial
FN_INTERNAL int fnusb_stop_iso(fnusb_dev *dev, fnusb_isoc_stream *strm)
int libusb_init(libusb_context **context)
FN_INTERNAL int upload_firmware(fnusb_dev *dev, char *filename)
void libusb_free_device_list(libusb_device **list, int unref_devices)
FN_INTERNAL int fnusb_num_interfaces(fnusb_dev *dev)
FN_INTERNAL int fnusb_set_led_alt(libusb_device_handle *dev, freenect_context *ctx, freenect_led_options state)
int motor_control_with_audio_enabled
int libusb_open(libusb_device *dev, libusb_device_handle **handle)
libusb_device_handle * dev
unsigned int actual_length
struct libusb_transfer * libusb_alloc_transfer(int iso_packets)
FN_INTERNAL int fnusb_close_subdevices(freenect_device *dev)
void libusb_close(libusb_device_handle *dev_handle)
#define PID_K4W_AUDIO_ALT_2
int libusb_release_interface(libusb_device_handle *dev, int interface_number)
void(* fnusb_iso_cb)(freenect_device *dev, uint8_t *buf, int len)
unsigned char * fn_fw_nui_ptr
int libusb_control_transfer(libusb_device_handle *dev_handle, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength, unsigned int timeout)
FN_INTERNAL int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_device_attributes **attribute_list)
FN_INTERNAL int fnusb_control(fnusb_dev *dev, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t *data, uint16_t wLength)
void libusb_free_transfer(struct libusb_transfer *transfer)
FN_INTERNAL int upload_firmware_from_memory(fnusb_dev *dev, unsigned char *fw_from_mem, unsigned int fw_size_in_btyes)
struct libusb_iso_packet_descriptor * iso_packet_desc
struct freenect_device_attributes * next
ssize_t libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
FN_INTERNAL int fnusb_shutdown(fnusb_ctx *ctx)
void libusb_fill_iso_transfer(struct libusb_transfer *transfer, libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, int num_iso_packets, libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
int libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle, uint8_t desc_index, unsigned char *data, int length)
static freenect_context * ctx
int libusb_submit_transfer(struct libusb_transfer *transfer)
FN_INTERNAL int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval *timeout)
#define PID_K4W_AUDIO_ALT_1
int libusb_claim_interface(libusb_device_handle *dev, int interface_number)
unsigned int fn_fw_nui_size
int libusb_cancel_transfer(struct libusb_transfer *transfer)
FN_INTERNAL int fnusb_is_pid_k4w_audio(int pid)
FN_INTERNAL int fnusb_process_events(fnusb_ctx *ctx)
int libusb_bulk_transfer(libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *data, int length, int *actual_length, unsigned int timeout)
static void LIBUSB_CALL iso_callback(struct libusb_transfer *xfer)
FN_INTERNAL int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx)
FN_INTERNAL int fnusb_num_devices(fnusb_ctx *ctx)
int libusb_set_interface_alt_setting(libusb_device_handle *dev, int interface_number, int alternate_setting)
void libusb_exit(libusb_context *ctx)
struct libusb_transfer ** xfers