All pastes #43165 Raw Edit

kommerck

public c v1 · immutable
#43165 ·published 2006-02-25 23:11 UTC
rendered paste body
/* * modification of gladiac's g5.c * USE AT OWN RISK!!!!! :) * * This is a implementation of the Logitech G5 protocol. * Compile it with: * * gcc -o g5-test -lusb g5.c && ./g5-test * * The lomoco team. * */#include <stdio.h>#include <usb.h>#define TIMEOUT 0x1000#define VENDOR 0x046d#define MOUSE 0xc041unsigned char buf[1024]={0};usb_dev_handle *handle;int error_int;char kerneldriver[32];void delbuf(){	int d;	for(d=0;d<1024;++d)		buf[d]=0;}void printbuf(int cnt){	int d;	for(d=0;d<cnt;++d)		printf("%02x ",buf[d]);	printf("\n");	delbuf();}/*int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);*/void msg(int rtype, int request, int value, int index, char* bytes, int size){	printf("request type: %i\n",rtype);	if(usb_control_msg(handle,rtype,request,value,index,bytes,size,0x1000)<0)		perror("usb-msg-error:");	else		printf("success");}/*int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, unsigned char index, void *buf, int size);*/void desc(unsigned char type, unsigned char index, int size){	if(usb_get_descriptor(handle, type, index, buf, size)<0)		perror("usb-descriptor-error:");}void print_bytes(char *bytes, int len) {    int i;    if (len > 0) {	for (i=0; i<len; i++) {	    printf("%02x ", (int)((unsigned char)bytes[i]));	}	printf("\"");        for (i=0; i<len; i++) {	    printf("%c", isprint(bytes[i]) ? bytes[i] : '.');        }        printf("\"");    }}int main(){	struct usb_bus* bus;	struct usb_device *root_tree;	struct usb_device *device;	char string[255];/*	struct usb_config_descriptor config={0x09,0x02,0x003b,0x02,*/	usb_init();	usb_find_busses();	usb_find_devices();		for (bus = usb_busses; bus != NULL; bus = bus->next) 	{		root_tree = bus->devices;				for( device = root_tree; device; device = device->next ) 		{			if ((device->descriptor.idVendor == VENDOR) && (device->descriptor.idProduct == MOUSE)) 			{							handle = usb_open (device);				/* 				 * usbhid blocks the interface so we can't send a command				 * to the mouse. So remove it. It kills our mouse support				 * in X, but hey!				 */				/* get the kernel driver which blocks the interface */				usb_get_driver_np (handle, 0, kerneldriver, 32);				printf("Interface 0 is used by: %s\n", kerneldriver);								/* detach a kernel driver which blocks the interface */				error_int = usb_detach_kernel_driver_np (handle, 0);				printf("%i\n", error_int);								usb_get_driver_np (handle, 0, kerneldriver, 32);				printf("Interface 0 is now used by: %s\n", kerneldriver);								/* claim the interface */				error_int = usb_claim_interface (handle, 0);				printf("%i\n", error_int);								usb_get_string_simple (handle, device->descriptor.iProduct, string, sizeof(string));				printf ("%s\n",string);								/* start here */								/* URB 1 */				desc (0x01, 0x00, 0x12);				printf("urb 1:\n");				printbuf (0x12);				/* URB 2 */				desc (0x02, 0x00, 0xff);				printbuf (0x3b);				printf("urb 2:\n");				printbuf (0x3b);				/* URB 3 */				if (usb_set_configuration (handle, 0x01) < 0)					perror ("urb 3:");				else					printf("urb 3 succeeded\n");				/* URB 4 */				desc (0x01,0x00,0x12);				printf("urb 4:\n");				printbuf (0x12);				/* works till here */// 				if (usb_release_interface(handle, 0) < 0)// 					perror ("claim");									error_int = usb_claim_interface (handle, 0);				/* from usbsnoop2libusb generated file */				int ret = 0;				ret = usb_control_msg(handle, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 0xa, 0x000, 0x001, buf, 0x0000000, 1000);				printf("5 control msg returned %d\n", ret);				ret = usb_get_descriptor(handle, 0x0, 0x0, buf, 0x0000076);				printf("6 get descriptor returned %d, bytes: ", ret);				print_bytes(buf, ret);				printf("\n");				ret = usb_get_descriptor(handle, 0x1, 0x0, buf, 0x0000012);				printf("7 get descriptor returned %d, bytes: ", ret);				print_bytes(buf, ret);				printf("\n");				ret = usb_control_msg(handle, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 0xa, 0x000, 0x000, buf, 0x0000000, 1000);				printf("8 control msg returned %d", ret);				printf("\n");				ret = usb_get_descriptor(handle, 0x0, 0x0, buf, 0x000009e);				printf("9 get descriptor returned %d, bytes: ", ret);				print_bytes(buf, ret);				printf("\n");				/* URB 5? */				msg(0x21,0x0a,0x0000,0x0001,(char*)buf,0);				printbuf(0);			}		}	}		return 0;}