⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printer.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	int timeout, err = 0, writecount = 0;	while (writecount < count) {		if (usblp->writeurb.status == -EINPROGRESS) {			if (file->f_flags & O_NONBLOCK)				return -EAGAIN;			timeout = USBLP_WRITE_TIMEOUT;			while (timeout && usblp->writeurb.status == -EINPROGRESS) {				if (signal_pending(current))					return writecount ? writecount : -EINTR;				timeout = interruptible_sleep_on_timeout(&usblp->wait, timeout);			}		}		if (!usblp->dev)			return -ENODEV;		if (usblp->writeurb.status) {			if (usblp->quirks & USBLP_QUIRK_BIDIR) {				if (usblp->writeurb.status != -EINPROGRESS)					err("usblp%d: error %d writing to printer",						usblp->minor, usblp->writeurb.status);				err = usblp->writeurb.status;				continue;			}			else {				err = usblp_check_status(usblp, err);				continue;			}		}		writecount += usblp->writeurb.transfer_buffer_length;		usblp->writeurb.transfer_buffer_length = 0;		if (writecount == count)			continue;		usblp->writeurb.transfer_buffer_length = (count - writecount) < USBLP_BUF_SIZE ?							 (count - writecount) : USBLP_BUF_SIZE;		if (copy_from_user(usblp->writeurb.transfer_buffer, buffer + writecount,				usblp->writeurb.transfer_buffer_length)) return -EFAULT;		usblp->writeurb.dev = usblp->dev;		usb_submit_urb(&usblp->writeurb);	}	return count;}static ssize_t usblp_read(struct file *file, char *buffer, size_t count, loff_t *ppos){	struct usblp *usblp = file->private_data;	if (!usblp->bidir)		return -EINVAL;	if (usblp->readurb.status == -EINPROGRESS) {		if (file->f_flags & O_NONBLOCK)			return -EAGAIN;		while (usblp->readurb.status == -EINPROGRESS) {			if (signal_pending(current))				return -EINTR;			interruptible_sleep_on(&usblp->wait);		}	}	if (!usblp->dev)		return -ENODEV;	if (usblp->readurb.status) {		err("usblp%d: error %d reading from printer",			usblp->minor, usblp->readurb.status);		usblp->readurb.dev = usblp->dev;		usb_submit_urb(&usblp->readurb);		return -EIO;	}	count = count < usblp->readurb.actual_length - usblp->readcount ?		count :	usblp->readurb.actual_length - usblp->readcount;	if (copy_to_user(buffer, usblp->readurb.transfer_buffer + usblp->readcount, count))		return -EFAULT;	if ((usblp->readcount += count) == usblp->readurb.actual_length) {		usblp->readcount = 0;		usblp->readurb.dev = usblp->dev;		usb_submit_urb(&usblp->readurb);	}	return count;}/* * Checks for printers that have quirks, such as requiring unidirectional * communication but reporting bidirectional; currently some HP printers * have this flaw (HP 810, 880, 895, etc.), or needing an init string * sent at each open (like some Epsons). * Returns 1 if found, 0 if not found. * * HP recommended that we use the bidirectional interface but * don't attempt any bulk IN transfers from the IN endpoint. * Here's some more detail on the problem: * The problem is not that it isn't bidirectional though. The problem * is that if you request a device ID, or status information, while * the buffers are full, the return data will end up in the print data * buffer. For example if you make sure you never request the device ID * while you are sending print data, and you don't try to query the * printer status every couple of milliseconds, you will probably be OK. */static unsigned int usblp_quirks (__u16 vendor, __u16 product){	int i;	for (i = 0; quirk_printers[i].vendorId; i++) {		if (vendor == quirk_printers[i].vendorId &&		    product == quirk_printers[i].productId)			return quirk_printers[i].quirks; 	}	return 0;}static void *usblp_probe(struct usb_device *dev, unsigned int ifnum,			 const struct usb_device_id *id){	struct usb_interface_descriptor *interface;	struct usb_endpoint_descriptor *epread, *epwrite;	struct usblp *usblp;	int minor, i, bidir = 0, quirks;	int alts = dev->actconfig->interface[ifnum].act_altsetting;	int length, err;	char *buf;	/* If a bidirectional interface exists, use it. */	for (i = 0; i < dev->actconfig->interface[ifnum].num_altsetting; i++) {		interface = &dev->actconfig->interface[ifnum].altsetting[i];		if (interface->bInterfaceClass != 7 || interface->bInterfaceSubClass != 1 ||		    interface->bInterfaceProtocol < 1 || interface->bInterfaceProtocol > 3 ||		   (interface->bInterfaceProtocol > 1 && interface->bNumEndpoints < 2))			continue;		if (interface->bInterfaceProtocol > 1) {			bidir = 1;			alts = i;			break;		}	}	interface = &dev->actconfig->interface[ifnum].altsetting[alts];	if (usb_set_interface(dev, ifnum, alts))		err("can't set desired altsetting %d on interface %d", alts, ifnum);	epwrite = interface->endpoint + 0;	epread = bidir ? interface->endpoint + 1 : NULL;	if ((epwrite->bEndpointAddress & 0x80) == 0x80) {		if (interface->bNumEndpoints == 1)			return NULL;		epwrite = interface->endpoint + 1;		epread = bidir ? interface->endpoint + 0 : NULL;	}	if ((epwrite->bEndpointAddress & 0x80) == 0x80)		return NULL;	if (bidir && (epread->bEndpointAddress & 0x80) != 0x80)		return NULL;	for (minor = 0; minor < USBLP_MINORS && usblp_table[minor]; minor++);	if (usblp_table[minor]) {		err("no more free usblp devices");		return NULL;	}	if (!(usblp = kmalloc(sizeof(struct usblp), GFP_KERNEL))) {		err("out of memory");		return NULL;	}	memset(usblp, 0, sizeof(struct usblp));	/* lookup quirks for this printer */	quirks = usblp_quirks(dev->descriptor.idVendor, dev->descriptor.idProduct);	if (bidir && (quirks & USBLP_QUIRK_BIDIR)) {		bidir = 0;		epread = NULL;		info ("Disabling reads from problem bidirectional printer on usblp%d",			minor);	}	usblp->dev = dev;	usblp->ifnum = ifnum;	usblp->minor = minor;	usblp->bidir = bidir;	usblp->quirks = quirks;	init_waitqueue_head(&usblp->wait);	if (!(buf = kmalloc(USBLP_BUF_SIZE * (bidir ? 2 : 1), GFP_KERNEL))) {		err("out of memory");		kfree(usblp);		return NULL;	}	if (!(usblp->device_id_string = kmalloc(DEVICE_ID_SIZE, GFP_KERNEL))) {		err("out of memory");		kfree(usblp);		kfree(buf);		return NULL;	}	FILL_BULK_URB(&usblp->writeurb, dev, usb_sndbulkpipe(dev, epwrite->bEndpointAddress),		buf, 0, usblp_bulk, usblp);	if (bidir)		FILL_BULK_URB(&usblp->readurb, dev, usb_rcvbulkpipe(dev, epread->bEndpointAddress),			buf + USBLP_BUF_SIZE, USBLP_BUF_SIZE, usblp_bulk, usblp);	/* Get the device_id string if possible. FIXME: Could make this kmalloc(length). */	err = usblp_get_id(usblp, 0, usblp->device_id_string, DEVICE_ID_SIZE - 1);	if (err >= 0) {		length = (usblp->device_id_string[0] << 8) + usblp->device_id_string[1]; /* big-endian */		if (length < DEVICE_ID_SIZE)			usblp->device_id_string[length] = '\0';		else			usblp->device_id_string[DEVICE_ID_SIZE - 1] = '\0';		dbg ("usblp%d Device ID string [%d]=%s",			minor, length, &usblp->device_id_string[2]);	}	else {		err ("usblp%d: error = %d reading IEEE-1284 Device ID string",			minor, err);		usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';	}#ifdef DEBUG	usblp_check_status(usblp, 0);#endif	info("usblp%d: USB %sdirectional printer dev %d if %d alt %d",		minor, bidir ? "Bi" : "Uni", dev->devnum, ifnum, alts);	return usblp_table[minor] = usblp;}static void usblp_disconnect(struct usb_device *dev, void *ptr){	struct usblp *usblp = ptr;	if (!usblp || !usblp->dev) {		err("disconnect on nonexisting interface");		return;	}	usblp->dev = NULL;	usb_unlink_urb(&usblp->writeurb);	if (usblp->bidir)		usb_unlink_urb(&usblp->readurb);	kfree(usblp->writeurb.transfer_buffer);	if (usblp->used) return;	kfree(usblp->device_id_string);	usblp_table[usblp->minor] = NULL;	kfree(usblp);}static struct file_operations usblp_fops = {	owner:		THIS_MODULE,	read:		usblp_read,	write:		usblp_write,	poll:		usblp_poll,	ioctl:		usblp_ioctl,	open:		usblp_open,	release:	usblp_release,};static struct usb_device_id usblp_ids [] = {	{ USB_INTERFACE_INFO(7, 1, 1) },	{ USB_INTERFACE_INFO(7, 1, 2) },	{ USB_INTERFACE_INFO(7, 1, 3) },	{ }						/* Terminating entry */};MODULE_DEVICE_TABLE (usb, usblp_ids);static struct usb_driver usblp_driver = {	name:		"usblp",	probe:		usblp_probe,	disconnect:	usblp_disconnect,	fops:		&usblp_fops,	minor:		USBLP_MINOR_BASE,	id_table:	usblp_ids,};static int __init usblp_init(void){	if (usb_register(&usblp_driver))		return -1;	return 0;}static void __exit usblp_exit(void){	usb_deregister(&usblp_driver);}module_init(usblp_init);module_exit(usblp_exit);MODULE_AUTHOR("Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap");MODULE_DESCRIPTION("USB Printer Device Class driver");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -