void openusb(usbdevice* kb){ // The driver sometimes isn't completely ready yet, so give it a short delay sleep(1); // Handle 3 is the control handle kb->lastkeypress = -1; kb->handle = kb->handles[3]; // Set up the device int setup = setupusb(kb); if(setup == -1){ closehandle(kb); pthread_mutex_unlock(&kb->mutex); pthread_mutex_destroy(&kb->mutex); pthread_mutex_destroy(&kb->keymutex); return; } else if(setup){ // If the setup had a communication error, wait a bit and try again. sleep(1); resetusb(kb); } // Start handling HID reports for the Corsair input IOHIDDeviceRegisterInputReportCallback(kb->handles[2], kb->intinput, MSG_SIZE, reportcallback, kb); // Register for close notification IOHIDDeviceRegisterRemovalCallback(kb->handle, usbremove, kb); // Update connected updateconnected(); notifyconnect(kb, 1); int index = INDEX_OF(kb, keyboard); printf("Device ready at %s%d\n", devpath, index); pthread_mutex_unlock(&kb->mutex); }
static HANDLE audio_open(Pdev_t *pdev, int close) { HANDLE ph,th,hp=pdev->DeviceOutputHandle; DWORD err; WAVEFORMATEX *wp = (WAVEFORMATEX*)(pdev+1); if(!load_audio()) { logerr(0, "audio functions not supported"); return(0); } if(hp && P_CP->ntpid==pdev->NtPid) { if(!close) return(hp); (*pwaveOutReset)(hp); (*pwaveOutClose)(hp); } else if(pdev->NtPid && (ph=OpenProcess(PROCESS_ALL_ACCESS,FALSE,(DWORD)pdev->NtPid))) { /* remotely close the wave device */ if(th=CreateRemoteThread(ph,NULL,0,wave_close,(void*)hp,0,&err)) { if(WaitForSingleObject(th,1000)!=WAIT_OBJECT_0) logerr(LOG_PROC+2, "WaitForSingleObject"); closehandle(th,HT_THREAD); } else logerr(LOG_PROC+2, "CreateRemoteThread"); closehandle(ph,HT_PROC); } pdev->NtPid = 0; if(err=(*pwaveOutOpen)((HWAVEOUT*)&hp,WAVE_MAPPER,wp,0,0,WAVE_ALLOWSYNC)) { SetLastError(err); logerr(LOG_DEV+5, "waveOutOpen"); errno = maperr(err); return 0; } pdev->opencount = 1; pdev->NtPid = P_CP->ntpid; pdev->DeviceOutputHandle = hp; return(hp); }
void openusb(usbdevice* kb, short vendor, short product){ kb->lastkeypress = KEY_NONE; if(IS_RGB(vendor, product)) // Handle 3 is the control handle kb->handle = kb->handles[3]; else // Non RGB keyboards don't have one, so just use 0 kb->handle = kb->handles[0]; // Set up the device int setup = setupusb(kb, vendor, product); if(setup == -1 || (setup && usb_tryreset(kb))){ closehandle(kb); pthread_mutex_unlock(&kb->mutex); pthread_mutex_destroy(&kb->mutex); pthread_mutex_destroy(&kb->keymutex); return; } // Start handling HID reports for the input IOHIDDeviceRegisterInputReportCallback(kb->handles[0], kb->urbinput, 8, reportcallback, kb); if(IS_RGB(vendor, product)) IOHIDDeviceRegisterInputReportCallback(kb->handles[1], kb->urbinput + 8, 21, reportcallback, kb); else IOHIDDeviceRegisterInputReportCallback(kb->handles[1], kb->urbinput + 8, 4, reportcallback, kb); if(IS_RGB(vendor, product)) IOHIDDeviceRegisterInputReportCallback(kb->handles[2], kb->kbinput, MSG_SIZE, reportcallback, kb); else IOHIDDeviceRegisterInputReportCallback(kb->handles[2], kb->urbinput + 8 + 4, 15, reportcallback, kb); // Register for close notification IOHIDDeviceRegisterRemovalCallback(kb->handle, usbremove, kb); // Update connected updateconnected(); notifyconnect(kb, 1); int index = INDEX_OF(kb, keyboard); printf("Device ready at %s%d\n", devpath, index); pthread_mutex_unlock(&kb->mutex); }
~directory_iterator() { closehandle(); delete [] m_dir_path; }
int openusb(struct udev_device* dev, int model){ // Make sure it's not connected yet const char* path = udev_device_get_devnode(dev); for(int i = 1; i < DEV_MAX; i++){ if(keyboard[i].udev && !strcmp(path, udev_device_get_devnode(keyboard[i].udev))) return 0; } // Find a free USB slot for(int index = 1; index < DEV_MAX; index++){ usbdevice* kb = keyboard + index; if(!IS_ACTIVE(kb)){ // Open the sysfs device kb->udev = dev; kb->handle = open(path, O_RDWR); kb->model = model; if(kb->handle <= 0){ printf("Error: Failed to open USB device: %s\n", strerror(errno)); closehandle(kb); connectstatus |= 2; return -1; } // Copy device description and serial strncpy(kb->name, udev_device_get_sysattr_value(dev, "product"), NAME_LEN); strncpy(kb->setting.serial, udev_device_get_sysattr_value(dev, "serial"), SERIAL_LEN); printf("Connecting %s (S/N: %s)\n", kb->name, kb->setting.serial); // A USB reset is almost always required in order for it to work correctly printf("Resetting device\n"); if(ioctl(kb->handle, USBDEVFS_RESET, 0)){ printf("Reset failed (%s). Disconnecting.\n", strerror(errno)); closehandle(kb); connectstatus |= 2; return -1; } // Claim the USB interfaces if(usbclaim(kb)){ printf("Error: Failed to claim interface: %s\n", strerror(errno)); closehandle(kb); connectstatus |= 2; return -1; } // Put the M-keys (K95) as well as the Brightness/Lock keys into software-controlled mode. This packet disables their // hardware-based functions. unsigned char datapkt[MSG_SIZE] = { 0x07, 0x04, 0x02 }; struct usbdevfs_ctrltransfer transfer = { 0x21, 0x09, 0x0300, 0x03, MSG_SIZE, 500, datapkt }; // This packet doesn't always succeed, so reset the device if that happens if(ioctl(kb->handle, USBDEVFS_CONTROL, &transfer) != MSG_SIZE){ printf("Couldn't talk to device (%s), trying to reset again...\n", strerror(errno)); if(resetusb(kb) || ioctl(kb->handle, USBDEVFS_CONTROL, &transfer) != MSG_SIZE){ printf("Reset failed (%s). Disconnecting.\n", strerror(errno)); closehandle(kb); connectstatus |= 2; return -1; } } // Set up the device if(setupusb(index)){ closehandle(kb); connectstatus |= 2; return -1; } // Set up the interrupt transfers. kb->INPUT_TEST = 0; setint(kb); // We should receive an interrupt transfer shortly after setting them up. If it doesn't happen, the device // isn't working correctly and needs to be reset int received = 0; for(int wait = 0; wait < 10; wait++){ usleep(50000); if(kb->INPUT_TEST){ received = 1; break; } } if(!received){ printf("Didn't get input, trying to reset again...\n"); if(resetusb(kb)){ printf("Reset failed (%s). Disconnecting.\n", strerror(errno)); closehandle(kb); connectstatus |= 2; return -1; } } updateconnected(); printf("Device ready at %s%d\n", devpath, index); connectstatus |= 1; return 0; } } printf("Error: No free devices\n"); return -1; }