usblp.c

来自「linux 内核源代码」· C语言 代码 · 共 1,436 行 · 第 1/3 页

C
1,436
字号
	spin_lock_irqsave(&usblp->lock, flags);	if (usblp->rcomplete) {		spin_unlock_irqrestore(&usblp->lock, flags);		return 0;	}	spin_unlock_irqrestore(&usblp->lock, flags);	if (usblp->sleeping)		return -ENODEV;	if (nonblock)		return -EAGAIN;	return 1;}/* * Please check ->bidir and other such things outside for now. */static int usblp_submit_read(struct usblp *usblp){	struct urb *urb;	unsigned long flags;	int rc;	rc = -ENOMEM;	if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL)		goto raise_urb;	usb_fill_bulk_urb(urb, usblp->dev,		usb_rcvbulkpipe(usblp->dev,		  usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),		usblp->readbuf, USBLP_BUF_SIZE_IN,		usblp_bulk_read, usblp);	usb_anchor_urb(urb, &usblp->urbs);	spin_lock_irqsave(&usblp->lock, flags);	usblp->readcount = 0; /* XXX Why here? */	usblp->rcomplete = 0;	spin_unlock_irqrestore(&usblp->lock, flags);	if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) {		dbg("error submitting urb (%d)", rc);		spin_lock_irqsave(&usblp->lock, flags);		usblp->rstatus = rc;		usblp->rcomplete = 1;		spin_unlock_irqrestore(&usblp->lock, flags);		goto raise_submit;	}	return 0;raise_submit:	usb_unanchor_urb(urb);	usb_free_urb(urb);raise_urb:	return rc;}/* * 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 const struct file_operations usblp_fops = {	.owner =	THIS_MODULE,	.read =		usblp_read,	.write =	usblp_write,	.poll =		usblp_poll,	.unlocked_ioctl =	usblp_ioctl,	.compat_ioctl =		usblp_ioctl,	.open =		usblp_open,	.release =	usblp_release,};static struct usb_class_driver usblp_class = {	.name =		"lp%d",	.fops =		&usblp_fops,	.minor_base =	USBLP_MINOR_BASE,};static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf){	struct usb_interface *intf = to_usb_interface(dev);	struct usblp *usblp = usb_get_intfdata (intf);	if (usblp->device_id_string[0] == 0 &&	    usblp->device_id_string[1] == 0)		return 0;	return sprintf(buf, "%s", usblp->device_id_string+2);}static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);static int usblp_probe(struct usb_interface *intf,		       const struct usb_device_id *id){	struct usb_device *dev = interface_to_usbdev (intf);	struct usblp *usblp = NULL;	int protocol;	int retval;	/* Malloc and start initializing usblp structure so we can use it	 * directly. */	if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {		retval = -ENOMEM;		goto abort;	}	usblp->dev = dev;	mutex_init(&usblp->wmut);	mutex_init (&usblp->mut);	spin_lock_init(&usblp->lock);	init_waitqueue_head(&usblp->rwait);	init_waitqueue_head(&usblp->wwait);	init_usb_anchor(&usblp->urbs);	usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;	usblp->intf = intf;	/* Malloc device ID string buffer to the largest expected length,	 * since we can re-query it on an ioctl and a dynamic string	 * could change in length. */	if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {		retval = -ENOMEM;		goto abort;	}	/*	 * Allocate read buffer. We somewhat wastefully	 * malloc both regardless of bidirectionality, because the	 * alternate setting can be changed later via an ioctl.	 */	if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) {		retval = -ENOMEM;		goto abort;	}	/* Allocate buffer for printer status */	usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);	if (!usblp->statusbuf) {		retval = -ENOMEM;		goto abort;	}	/* Lookup quirks for this printer. */	usblp->quirks = usblp_quirks(		le16_to_cpu(dev->descriptor.idVendor),		le16_to_cpu(dev->descriptor.idProduct));	/* Analyze and pick initial alternate settings and endpoints. */	protocol = usblp_select_alts(usblp);	if (protocol < 0) {		dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",			le16_to_cpu(dev->descriptor.idVendor),			le16_to_cpu(dev->descriptor.idProduct));		retval = -ENODEV;		goto abort;	}	/* Setup the selected alternate setting and endpoints. */	if (usblp_set_protocol(usblp, protocol) < 0) {		retval = -ENODEV;	/* ->probe isn't ->ioctl */		goto abort;	}	/* Retrieve and store the device ID string. */	usblp_cache_device_id_string(usblp);	retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);	if (retval)		goto abort_intfdata;#ifdef DEBUG	usblp_check_status(usblp, 0);#endif	usb_set_intfdata (intf, usblp);	usblp->present = 1;	retval = usb_register_dev(intf, &usblp_class);	if (retval) {		printk(KERN_ERR "usblp: Not able to get a minor"		    " (base %u, slice default): %d\n",		    USBLP_MINOR_BASE, retval);		goto abort_intfdata;	}	usblp->minor = intf->minor;	printk(KERN_INFO "usblp%d: USB %sdirectional printer dev %d "		"if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",		usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,		usblp->ifnum,		usblp->protocol[usblp->current_protocol].alt_setting,		usblp->current_protocol,		le16_to_cpu(usblp->dev->descriptor.idVendor),		le16_to_cpu(usblp->dev->descriptor.idProduct));	return 0;abort_intfdata:	usb_set_intfdata (intf, NULL);	device_remove_file(&intf->dev, &dev_attr_ieee1284_id);abort:	if (usblp) {		kfree(usblp->readbuf);		kfree(usblp->statusbuf);		kfree(usblp->device_id_string);		kfree(usblp);	}	return retval;}/* * We are a "new" style driver with usb_device_id table, * but our requirements are too intricate for simple match to handle. * * The "proto_bias" option may be used to specify the preferred protocol * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3).  If the device * supports the preferred protocol, then we bind to it. * * The best interface for us is 7/1/2, because it is compatible * with a stream of characters. If we find it, we bind to it. * * Note that the people from hpoj.sourceforge.net need to be able to * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose. * * Failing 7/1/2, we look for 7/1/3, even though it's probably not * stream-compatible, because this matches the behaviour of the old code. * * If nothing else, we bind to 7/1/1 - the unidirectional interface. */static int usblp_select_alts(struct usblp *usblp){	struct usb_interface *if_alt;	struct usb_host_interface *ifd;	struct usb_endpoint_descriptor *epd, *epwrite, *epread;	int p, i, e;	if_alt = usblp->intf;	for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)		usblp->protocol[p].alt_setting = -1;	/* Find out what we have. */	for (i = 0; i < if_alt->num_altsetting; i++) {		ifd = &if_alt->altsetting[i];		if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)			if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))				continue;		if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||		    ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)			continue;		/* Look for bulk OUT and IN endpoints. */		epwrite = epread = NULL;		for (e = 0; e < ifd->desc.bNumEndpoints; e++) {			epd = &ifd->endpoint[e].desc;			if (usb_endpoint_is_bulk_out(epd))				if (!epwrite)					epwrite = epd;			if (usb_endpoint_is_bulk_in(epd))				if (!epread)					epread = epd;		}		/* Ignore buggy hardware without the right endpoints. */		if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))			continue;		/* Turn off reads for 7/1/1 (unidirectional) interfaces		 * and buggy bidirectional printers. */		if (ifd->desc.bInterfaceProtocol == 1) {			epread = NULL;		} else if (usblp->quirks & USBLP_QUIRK_BIDIR) {			printk(KERN_INFO "usblp%d: Disabling reads from "			    "problematic bidirectional printer\n",			    usblp->minor);			epread = NULL;		}		usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =				ifd->desc.bAlternateSetting;		usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;		usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;	}	/* If our requested protocol is supported, then use it. */	if (proto_bias >= USBLP_FIRST_PROTOCOL &&	    proto_bias <= USBLP_LAST_PROTOCOL &&	    usblp->protocol[proto_bias].alt_setting != -1)		return proto_bias;	/* Ordering is important here. */	if (usblp->protocol[2].alt_setting != -1)		return 2;	if (usblp->protocol[1].alt_setting != -1)		return 1;	if (usblp->protocol[3].alt_setting != -1)		return 3;	/* If nothing is available, then don't bind to this device. */	return -1;}static int usblp_set_protocol(struct usblp *usblp, int protocol){	int r, alts;	if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)		return -EINVAL;	alts = usblp->protocol[protocol].alt_setting;	if (alts < 0)		return -EINVAL;	r = usb_set_interface(usblp->dev, usblp->ifnum, alts);	if (r < 0) {		printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",			alts, usblp->ifnum);		return r;	}	usblp->bidir = (usblp->protocol[protocol].epread != NULL);	usblp->current_protocol = protocol;	dbg("usblp%d set protocol %d", usblp->minor, protocol);	return 0;}/* Retrieves and caches device ID string. * Returns length, including length bytes but not null terminator. * On error, returns a negative errno value. */static int usblp_cache_device_id_string(struct usblp *usblp){	int err, length;	err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);	if (err < 0) {		dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",			usblp->minor, err);		usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';		return -EIO;	}	/* First two bytes are length in big-endian.	 * They count themselves, and we copy them into	 * the user's buffer. */	length = be16_to_cpu(*((__be16 *)usblp->device_id_string));	if (length < 2)		length = 2;	else if (length >= USBLP_DEVICE_ID_SIZE)		length = USBLP_DEVICE_ID_SIZE - 1;	usblp->device_id_string[length] = '\0';	dbg("usblp%d Device ID string [len=%d]=\"%s\"",		usblp->minor, length, &usblp->device_id_string[2]);	return length;}static void usblp_disconnect(struct usb_interface *intf){	struct usblp *usblp = usb_get_intfdata (intf);	usb_deregister_dev(intf, &usblp_class);	if (!usblp || !usblp->dev) {		err("bogus disconnect");		BUG ();	}	device_remove_file(&intf->dev, &dev_attr_ieee1284_id);	mutex_lock (&usblp_mutex);	mutex_lock (&usblp->mut);	usblp->present = 0;	wake_up(&usblp->wwait);	wake_up(&usblp->rwait);	usb_set_intfdata (intf, NULL);	usblp_unlink_urbs(usblp);	mutex_unlock (&usblp->mut);	if (!usblp->used)		usblp_cleanup (usblp);	mutex_unlock (&usblp_mutex);}static int usblp_suspend (struct usb_interface *intf, pm_message_t message){	struct usblp *usblp = usb_get_intfdata (intf);	/* we take no more IO */	usblp->sleeping = 1;	usblp_unlink_urbs(usblp);#if 0 /* XXX Do we want this? What if someone is reading, should we fail? */	/* not strictly necessary, but just in case */	wake_up(&usblp->wwait);	wake_up(&usblp->rwait);#endif	return 0;}static int usblp_resume (struct usb_interface *intf){	struct usblp *usblp = usb_get_intfdata (intf);	int r;	usblp->sleeping = 0;	r = handle_bidir (usblp);	return r;}static struct usb_device_id usblp_ids [] = {	{ USB_DEVICE_INFO(7, 1, 1) },	{ USB_DEVICE_INFO(7, 1, 2) },	{ USB_DEVICE_INFO(7, 1, 3) },	{ USB_INTERFACE_INFO(7, 1, 1) },	{ USB_INTERFACE_INFO(7, 1, 2) },	{ USB_INTERFACE_INFO(7, 1, 3) },	{ USB_DEVICE(0x04b8, 0x0202) },	/* Seiko Epson Receipt Printer M129C */	{ }						/* Terminating entry */};MODULE_DEVICE_TABLE (usb, usblp_ids);static struct usb_driver usblp_driver = {	.name =		"usblp",	.probe =	usblp_probe,	.disconnect =	usblp_disconnect,	.suspend =	usblp_suspend,	.resume =	usblp_resume,	.id_table =	usblp_ids,	.supports_autosuspend =	1,};static int __init usblp_init(void){	return usb_register(&usblp_driver);}static void __exit usblp_exit(void){	usb_deregister(&usblp_driver);}module_init(usblp_init);module_exit(usblp_exit);MODULE_AUTHOR( DRIVER_AUTHOR );MODULE_DESCRIPTION( DRIVER_DESC );module_param(proto_bias, int, S_IRUGO | S_IWUSR);MODULE_PARM_DESC(proto_bias, "Favourite protocol number");MODULE_LICENSE("GPL");

⌨️ 快捷键说明

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