/* TS thread function issues URB requests. */
static void tsthread(void* const param)
{
	struct tsthread_param* const ps = param;
	int ret, i;
	const unsigned  UrbSize = ROUNDUP( sizeof(struct usbdevfs_urb) + sizeof(struct usbdevfs_iso_packet_desc) * NUM_ISOC_PACKET ,0xF);
	void* ptrUrb;

	ptrUrb = uHeapAlloc(UrbSize * TS_MaxNumIO);
	if(! ptrUrb) {
		warn_info(errno,"failed to allocate");
		return;
	}

	ps->buff_push = 0;
	for(i = 0; i < TS_MaxNumIO; i++) {
		struct usbdevfs_urb* const  pUrb = ptrUrb + UrbSize * i;
		//# mark this URB unused
		pUrb->buffer = NULL;
	}

	for(;;) {
		if(!(ps->flags & 0x01)) {
			for(i = 0; i < TS_MaxNumIO; i++) {
				struct usbdevfs_urb* const  pUrb = ptrUrb + UrbSize * i;
				if(! pUrb->buffer) {
					//# continue to issue a new URB request
					submitNextURB(ps, pUrb);
				}
			}
		}
		if(ps->flags & 0x02) {
			//# canceled
			reapURB(ps);
			for(i = 0; i < TS_MaxNumIO; i++) {
				struct usbdevfs_urb* const  pUrb = ptrUrb + UrbSize * i;
				if(! pUrb->buffer) continue;
				if(ioctl(ps->pUSB->fd, USBDEVFS_DISCARDURB, pUrb) < 0) {
					warn_info(errno,"failed to discard URB");
				}
			}
			break; 
		}

		struct pollfd pfd;
		pfd.fd = ps->pUSB->fd;
		pfd.events = POLLOUT | POLLERR;
		ret = poll(&pfd, 1, TS_PollTimeout);
		if(0 > ret) {
			warn_info(errno,"poll failed");
			break;
		}else{
			if(0 == ret) {
				//# timeout
				if(!(ps->flags & 0x01))  dmsg("poll timeout");
			}
			if(reapURB(ps) < 0) break;
		}
	}
	uHeapFree(ptrUrb);
	ps->flags |= 0x01;    //# continue = F
}
Exemple #2
0
int main(void) {
	int i;
	int interfaceToClaim = 1;
	int deviceNumber = 0;
	char deviceToOpen[4096];
	int r = findDevice(deviceToOpen, &deviceNumber);
	if( r != 0 ) {
		printf("ERROR: Unable to locate LPCUSB device attached to system (or permissions to /dev/bus/usb are wrong)\n");
		exit(-1);
	} else {
		printf("Found LPCUSB device at %s with device number %d\n", deviceToOpen, deviceNumber);
	}
	
	int ret;
	char mybuffer[1024];
	mybuffer[0] = 'A';
	
	printf("Trying to open device %s\n", deviceToOpen);
	
    int fd = open(deviceToOpen, O_ASYNC | O_RDWR);
    if( fd == -1 ) {
    	printf("An error occured dirng open of device, errno=%d\n", errno);
    	printf("%s\n", strerror(errno));
    	exit(1);
    }
    
    //Claim interface 1 of the USB device
    printf("Claiming interface...\n");
    ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &interfaceToClaim);
    if( ret != 0 ) {
		printf("Error %d while claiming interface, string is: %s\n",errno, strerror(errno));
    }
    
    /*
    struct usbdevfs_connectinfo connInfo;
    connInfo.devnum = deviceNumber;
    connInfo.slow = 2;
    
    ret = ioctl(fd, USBDEVFS_CONNECTINFO, &connInfo);
	printf("ret from ioctl USBDEVFS_CONNECTINFO is %d\n", ret);
	if (ret == -1) {
		printf("Error %d string is: %s\n",errno, strerror(errno));
	} else {
		printf("Slow flag is %d\n", connInfo.slow);
	}
    */
	
    int requestUniqueID = 77;
    int requestUniqueIDOut = 78;
    int requestUniqueIDOut2 = 79;
    
    memset(infobuff3, 0, 4095);
    strncpy(infobuff3, "DAVE", 4095);
    
    struct usbdevfs_urb myOutURB, myOutURB2, myInURB;

	int q;
	for (q = 0; q < 6; q++) {
		printf("===============================================================================\n");
		writeURB(fd, &myOutURB, infobuff3, &requestUniqueIDOut);
		reapURB(fd);

		readURB(fd, &myInURB, &requestUniqueID);
		reapURB(fd);
	}
	
	//Release the interface
	printf("Releasing interface...\n");
    ret = ioctl(fd, USBDEVFS_RELEASEINTERFACE, &interfaceToClaim);
    if( ret != 0 ) {
		printf("Error %d string is: %s\n",errno, strerror(errno));
    }

	
	
	close(fd);
	
	return(0);
}