Esempio n. 1
0
/* initialize driver -- returns 1 on success, 0 on error */
static int ati_init()
{
	struct usb_device *usb_dev;
	int pipe_fd[2] = { -1, -1 };

	LOGPRINTF(1, "initializing USB receiver");

	init_rec_buffer();

	/* A separate process will be forked to read data from the USB
	 * receiver and write it to a pipe. hw.fd is set to the readable
	 * end of this pipe. */
	if (pipe(pipe_fd) != 0) {
		logperror(LOG_ERR, "couldn't open pipe");
		return 0;
	}
	hw.fd = pipe_fd[0];

	usb_dev = find_usb_device();
	if (usb_dev == NULL) {
		logprintf(LOG_ERR, "couldn't find a compatible USB device");
		return 0;
	}

	if (!find_device_endpoints(usb_dev)) {
		logprintf(LOG_ERR, "couldn't find device endpoints");
		return 0;
	}

	dev_handle = usb_open(usb_dev);
	if (dev_handle == NULL) {
		logperror(LOG_ERR, "couldn't open USB receiver");
		goto fail;
	}

	if (usb_claim_interface(dev_handle, 0) != 0) {
		logperror(LOG_ERR, "couldn't claim USB interface");
		goto fail;
	}

	errno = 0;
	if ((usb_interrupt_write(dev_handle, dev_ep_out->bEndpointAddress, init1, sizeof(init1), 100) != sizeof(init1))
	    || (usb_interrupt_write(dev_handle, dev_ep_out->bEndpointAddress, init2, sizeof(init2), 100) !=
		sizeof(init2))) {
		logprintf(LOG_ERR, "couldn't initialize USB receiver: %s", errno ? strerror(errno) : "short write");
		goto fail;
	}

	child = fork();
	if (child == -1) {
		logperror(LOG_ERR, "couldn't fork child process");
		goto fail;
	} else if (child == 0) {
		usb_read_loop(pipe_fd[1]);
	}

	LOGPRINTF(1, "USB receiver initialized");
	return 1;

fail:
	if (dev_handle) {
		usb_close(dev_handle);
		dev_handle = NULL;
	}
	if (pipe_fd[0] >= 0)
		close(pipe_fd[0]);
	if (pipe_fd[1] >= 0)
		close(pipe_fd[1]);
	return 0;
}
Esempio n. 2
0
/* initialize driver -- returns 1 on success, 0 on error */
static int awlibusb_init()
{
	struct usb_device *usb_dev;
	int pipe_fd[2] = { -1, -1 };

	LOGPRINTF(1, "initializing USB receiver");

	init_rec_buffer();

	/* A separate process will be forked to read data from the USB
	 * receiver and write it to a pipe. drv.fd is set to the readable
	 * end of this pipe. */
	if (pipe(pipe_fd) != 0) {
		logperror(LIRC_ERROR, "couldn't open pipe");
		return 0;
	}
	drv.fd = pipe_fd[0];

	usb_dev = find_usb_device();
	if (usb_dev == NULL) {
		logprintf(LIRC_ERROR, "couldn't find a compatible USB device");
		goto fail;
	}

	if (!find_device_endpoints(usb_dev)) {
		logprintf(LIRC_ERROR, "couldn't find device endpoints");
		goto fail;
	}

	dev_handle = usb_open(usb_dev);
	if (dev_handle == NULL) {
		logperror(LIRC_ERROR, "couldn't open USB receiver");
		goto fail;
	}

	if (usb_claim_interface(dev_handle, 0) != 0) {
		logperror(LIRC_ERROR, "couldn't claim USB interface");
		goto fail;
	}

	child = fork();
	if (child == -1) {
		logperror(LIRC_ERROR, "couldn't fork child process");
		goto fail;
	} else if (child == 0) {
		usb_read_loop(pipe_fd[1]);
	}

	LOGPRINTF(1, "USB receiver initialized");
	return 1;

fail:
	if (dev_handle) {
		usb_close(dev_handle);
		dev_handle = NULL;
	}
	if (pipe_fd[0] >= 0)
		close(pipe_fd[0]);
	if (pipe_fd[1] >= 0)
		close(pipe_fd[1]);
	return 0;
}