int TCD2Usb::TCD2Usb_initialization(unsigned int vendor, unsigned int product) { // Initialize the USB library usb_init(); // printf("TCD2Usb::TCD2Usb_initialization: USB library initilaized\n"); // Find the buses if(usb_find_busses() < 0) { // printf("TCD2Usb::TCD2Usb_initialization: usb_find_busses failed \n"); return -TCD2USB_SYS_FIND_BUSSES_ERR; } // printf("TCD2Usb::TCD2Usb_initialization: USB system bus list updated\n"); // Find the devices if(usb_find_devices() < 0) { // printf("TCD2Usb::TCD2Usb_initialization: usb_find_devices failed \n"); return -TCD2USB_SYS_FIND_DEVICES_ERR; } // printf("TCD2Usb::TCD2Usb_initialization: USB system device list updated\n"); // Assign the device to usbdev if(!(usbdev=findDevice(vendor,product))) { // printf("TCD2Usb::TCD2Usb_initialization: No TCD2 USB device attached.\n"); return -TCD2USB_FIND_DEVICE_ERR; } // printf("TCD2Usb::TCD2Usb_initialization: TCD2 USB device found\n"); // printf("TCD2Usb::TCD2Usb_initialization: Using ID %04x:%04x on %s.%s.\n",usbdev->descriptor.idVendor,usbdev->descriptor.idProduct,usbdev->bus->dirname,usbdev->filename); // Open the USB device if( open() != 0 ) { // printf("TCD2Usb::TCD2Usb_initialization: Open failed.\n"); return -TCD2USB_OPEN_ERR; } // printf("TCD2Usb::TCD2Usb_initialization: TCD2 USB device opened\n"); // Claim the interface (This is the only class using it after all, so why not claim it now? if(claim_interface()) { // printf("TCD2Usb::TCD2Usb_initialization: Failed to claim interface 0: %s\n",usb_strerror()); return -TCD2USB_CLAIM_ERR; } // Set the alternate interface if(usb_set_altinterface(usbhdl,1)<0) { // printf("TCD2Usb::TCD2Usb_initialize: Failed to set altinterface 1: %s\n",usb_strerror()); return -TCD2USB_ALTINTERFACE_ERR; } // All's well! return 0; }
static int initialise_temper1(struct usb_dev_handle *handle) { int r = detach_driver(handle, INTERFACE0); if (r >= 0) r = detach_driver(handle, INTERFACE1); if (r >= 0) r = set_configuration(handle, 0x01); if (r >= 0) r = claim_interface(handle, INTERFACE0); if (r >= 0) r = claim_interface(handle, INTERFACE1); /* // In light of discovery that the HID implementation has a keyboard emulation // and a vendor specific mode, don't touch anything else. No init needed? if (r >= 0) r = control_message(handle, CTRL_REQ_TYPE, CTRL_REQ, CTRL_VALUE, 0, cq_initialise, sizeof(cq_initialise)); */ return r; }
bool USB_Device::set_default() { if (!_dev || !_dev->config || !_dev->config->interface || !_dev->config->interface->altsetting) { return false; } int config = _dev->config->bConfigurationValue; int interface = _dev->config->interface->altsetting->bInterfaceNumber; int alt = _dev->config->interface->altsetting->bAlternateSetting; return set_configuration(config) && claim_interface(interface) && set_altinterface(alt); }
UsbCamera::UsbCamera (std::shared_ptr<UsbSession> session, device_info dev, unsigned int _interface): usb_session(session), dev(dev), interface(_interface) { try { this->dev_handle = libusb_open_device_with_vid_pid(usb_session->get_session(), dev.idVendor, dev.idProduct); if (this->dev_handle == NULL) { throw std::runtime_error("Unable to attain device handle."); } claim_interface(); } catch (std::runtime_error& err) { throw; } }
/** * Submit a LinuxRequest. * @param env The JNIEnv. * @param fd The file descriptor. * @param linuxRequest The LinuxRequest. */ static void submitRequest( JNIEnv *env, int fd, jobject linuxRequest ) { int type, err, sync = 0; jclass LinuxRequest; jmethodID getType, setError, setCompleted; LinuxRequest = (*env)->GetObjectClass( env, linuxRequest ); getType = (*env)->GetMethodID( env, LinuxRequest, "getType", "()I" ); setCompleted = (*env)->GetMethodID( env, LinuxRequest, "setCompleted", "(Z)V" ); setError = (*env)->GetMethodID( env, LinuxRequest, "setError", "(I)V" ); (*env)->DeleteLocalRef( env, LinuxRequest ); type = (*env)->CallIntMethod( env, linuxRequest, getType ); dbg(MSG_DEBUG1, "submitRequest : Submitting Request.\n"); switch (type) { case LINUX_PIPE_REQUEST: dbg(MSG_DEBUG1, "submitRequest : Submitting Pipe Request.\n"); err = pipe_request( env, fd, linuxRequest ); break; case LINUX_DCP_REQUEST: dbg(MSG_DEBUG1, "submitRequest : Submitting Dcp Request.\n"); err = dcp_request( env, fd, linuxRequest ); break; case LINUX_SET_INTERFACE_REQUEST: dbg(MSG_DEBUG1, "submitRequest : Submitting SetInterface Request.\n"); err = set_interface( env, fd, linuxRequest ); sync = 1; break; case LINUX_SET_CONFIGURATION_REQUEST: dbg(MSG_DEBUG1, "submitRequest : Submitting SetConfiguration Request.\n"); err = set_configuration( env, fd, linuxRequest ); sync = 1; break; case LINUX_CLAIM_INTERFACE_REQUEST: dbg(MSG_DEBUG1, "submitRequest : Submitting ClaimInterface Request.\n"); err = claim_interface( env, fd, 1, linuxRequest ); sync = 1; break; case LINUX_RELEASE_INTERFACE_REQUEST: dbg(MSG_DEBUG1, "submitRequest : Submitting ReleaseInterface Request.\n"); err = claim_interface( env, fd, 0, linuxRequest ); sync = 1; break; case LINUX_IS_CLAIMED_INTERFACE_REQUEST: dbg(MSG_DEBUG1, "submitRequest : Submitting IsClaimed Request.\n"); err = is_claimed( env, fd, linuxRequest ); sync = 1; break; case LINUX_ISOCHRONOUS_REQUEST: dbg(MSG_DEBUG1, "submitRequest : Submitting Isochronous Request.\n"); err = isochronous_request( env, fd, linuxRequest ); break; default: /* ? */ dbg( MSG_ERROR, "submitRequest : Unknown Request type %d\n", type ); err = -EINVAL; break; } if (err) (*env)->CallVoidMethod( env, linuxRequest, setError, err ); if (sync || err) (*env)->CallVoidMethod( env, linuxRequest, setCompleted, JNI_TRUE ); }
void check_runtime_interface( struct dfu_if *_rt_dif, struct dfu_if *dif, struct dfu_status *status ) { if( !_rt_dif->flags & DFU_IFF_DFU ) { while(1) { /* In the 'first round' during runtime mode, there can only be one * DFU Interface descriptor according to the DFU Spec. */ /* FIXME: check if the selected device really has only one */ claim_interface(_rt_dif); printf( "Determining device status: " ); if( dfu_get_status ( _rt_dif->dev_handle, _rt_dif->interface, status ) < 0 ) { fprintf( stderr, "error get_status: %s\n", usb_strerror( ) ); exit( 1 ); } printf( "state = %s, status = %d\n", dfu_state_to_string( status->bState ), status->bStatus ); switch ( status->bState ) { case DFU_STATE_appIDLE: case DFU_STATE_appDETACH: printf( "Device really in Runtime Mode, send DFU " "detach request...\n" ); if( dfu_detach( _rt_dif->dev_handle, _rt_dif->interface, 1000 ) < 0 ) { fprintf( stderr, "error detaching: %s\n", usb_strerror( ) ); exit( 1 ); break; } printf( "Resetting USB...\n" ); int ret = usb_reset( _rt_dif->dev_handle ); if( ret < 0 && ret != -ENODEV ) fprintf( stderr, "error resetting after detach: %s\n", usb_strerror( ) ); sleep( 2 ); break; case DFU_STATE_dfuERROR: printf( "dfuERROR, clearing status\n" ); if( dfu_clear_status( _rt_dif->dev_handle, _rt_dif->interface ) < 0 ) { fprintf( stderr, "error clear_status: %s\n", usb_strerror( ) ); exit( 1 ); break; } break; default: fprintf( stderr, "WARNING: Runtime device already " "in DFU state ?!?\n" ); break; } if( in_array( status->bState, ( unsigned char[3] ) { DFU_STATE_appIDLE, DFU_STATE_appDETACH, DFU_STATE_dfuERROR}, 3 ) >= 0 ) { /* now we need to re-scan the bus and locate our device */ int ret = 0; if( ( ret = usb_find_devices( ) ) < 2 ) printf( "not at least 2 device changes found ?!?\n" ); locate_dfu_device(dif,"Lost device after RESET?"); open_usb_device(dif); } else break; sleep(1); }
int load_device(){ int ret; fprintf(stderr, "Starting\n"); struct usb_bus *busses; usb_init(); fprintf(stderr, "usb_init...\n"); usb_find_busses(); usb_find_devices(); fprintf(stderr, "Found busses, found devices...\n"); busses = usb_get_busses(); fprintf(stderr, "Got busses...\n"); struct usb_bus *bus; int c, i, a; /* ... */ for (bus = busses; bus; bus = bus->next) { struct usb_device *dev; for (dev = bus->devices; dev; dev = dev->next) { /* Check if this device is a printer */ if (dev->descriptor.idVendor == 4400) if(dev->descriptor.idProduct == 514) launcher = usb_open(dev); } } if(launcher == NULL){ printf("Didn't get launcher!\n"); return 1; } else { printf("Got launcher...\n"); } ret = claim_interface(0); printf("Interface 0 returned %d\n", ret); if(ret == 1){ printf("Returning 1 in claim_interface\n"); return 1; } ret = claim_interface(1); printf("Interface 1 returned %d\n", ret); if(ret == 1){ printf("Returning 1 in claim_interface\n"); return 1; } fprintf(stderr, "Claimed interfaces\n"); return 0; }