Hi,

This is my first patch; I hope I did it right.
Sorry if it's a bit large, but it's a replacement of the current driver...

This patch is against a vanilla 2.6.11.4 kernel.


Please CC me, as I'm not subscribed to the list.

>>I got some usb-lcd's from ww.usblcd.de.
>>It always annoyed me that with the current kernel-driver (usblcd.c), you
>>were limited to only have one device attached.
>>So I finally hacked a completely new driver, based on the usb-skeleton.c
>>file.
>>
>>It nearly is an exact copy of the skel, except the MINOR, vendor-id, a
>>hardware-version-check and an ioctl function.

> Looks sane, care to send it to me as a patch, as per
> Documentation/SubmittingPatches and CC: the linux-usb-devel mailing list
> with it?
>
> thanks,
> greg k-h


The contribution was created in whole or in part by me and I have the right to 
submit it under the open source license indicated in the file.
Signed-off-by: Georges Toth <[EMAIL PROTECTED]>


-- 
Kind regards,
Georges Toth
--- linux-2.6.11.4/drivers/usb/misc/usblcd.c.orig	2005-03-18 03:07:22.471662672 +0100
+++ linux-2.6.11.4/drivers/usb/misc/usblcd.c	2005-03-18 03:09:57.985021048 +0100
@@ -1,14 +1,16 @@
 /*****************************************************************************
  *                          USBLCD Kernel Driver                             *
- *        See http://www.usblcd.de for Hardware and Documentation.           *
- *                            Version 1.03                                   *
- *             (C) 2002 Adams IT Services <[EMAIL PROTECTED]>                   *
+ *                            Version 1.05                                   *
+ *             (C) 2005 Georges Toth <[EMAIL PROTECTED]>                       *
  *                                                                           *
  *     This file is licensed under the GPL. See COPYING in the package.      *
- * Based on rio500.c by Cesar Miquel ([EMAIL PROTECTED]) which is based on    *
- * hp_scanner.c by David E. Nelson ([EMAIL PROTECTED])                        *
+ * Based on usb-skeleton.c 2.0 by Greg Kroah-Hartman ([EMAIL PROTECTED])        *
  *                                                                           *
- * 23.7.02 RA changed minor device number to the official assigned one       *
+ *                                                                           *
+ * 28.02.05 Complete rewrite of the original usblcd.c driver,                *
+ *          based on usb_skeleton.c.                                         *
+ *          This new driver allows more than one USB-LCD to be connected     *
+ *          and controlled, at once                                          *
  *****************************************************************************/
 #include <linux/module.h>
 #include <linux/kernel.h>
@@ -18,74 +20,130 @@
 #include <asm/uaccess.h>
 #include <linux/usb.h>
 
-#define DRIVER_VERSION "USBLCD Driver Version 1.04"
+#define DRIVER_VERSION "USBLCD Driver Version 1.05"
 
 #define USBLCD_MINOR		144
 
 #define IOCTL_GET_HARD_VERSION	1
 #define IOCTL_GET_DRV_VERSION	2
 
-/* stall/wait timeout for USBLCD */
-#define NAK_TIMEOUT	(10*HZ)
 
-#define IBUF_SIZE	0x1000
-#define OBUF_SIZE	0x10000
+static struct usb_device_id id_table [] = {
+	{ .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
+	{ },
+};
+MODULE_DEVICE_TABLE (usb, id_table);
+
 
-struct lcd_usb_data {
-	struct usb_device *lcd_dev;	/* init: probe_lcd */
-	unsigned int ifnum;		/* Interface number of the USB device */
-	int isopen;			/* nz if open */
-	int present;			/* Device is present on the bus */
-	char *obuf, *ibuf;		/* transfer buffers */
-	char bulk_in_ep, bulk_out_ep;	/* Endpoint assignments */
-	wait_queue_head_t wait_q;	/* for timeouts */
+struct usb_lcd {
+	struct usb_device *	udev;			/* init: probe_lcd */
+	struct usb_interface *  interface;		/* the interface for this device */
+	unsigned char *         bulk_in_buffer;		/* the buffer to receive data */
+	size_t			bulk_in_size;		/* the size of the receive buffer */
+	__u8			bulk_in_endpointAddr;	/* the address of the bulk in endpoint */
+	__u8			bulk_out_endpointAddr;	/* the address of the bulk out endpoint */
+	struct kref             kref;
 };
+#define to_lcd_dev(d) container_of(d, struct usb_lcd, kref)
+
+static struct usb_driver lcd_driver;
+
+
+static void lcd_delete(struct kref *kref)
+{
+	struct usb_lcd *dev = to_lcd_dev(kref);
+
+	usb_put_dev(dev->udev);
+	kfree (dev->bulk_in_buffer);
+	kfree (dev);
+}
 
-static struct lcd_usb_data lcd_instance;
 
-static int open_lcd(struct inode *inode, struct file *file)
+static int lcd_open(struct inode *inode, struct file *file)
 {
-	struct lcd_usb_data *lcd = &lcd_instance;
+	struct usb_lcd *dev;
+	struct usb_interface *interface;
+	int subminor;
+	int retval = 0;
+
+	subminor = iminor(inode);
+
+	interface = usb_find_interface(&lcd_driver, subminor);
+	if (!interface) {
+		err ("USBLCD: %s - error, can't find device for minor %d",
+		     __FUNCTION__, subminor);
+		retval = -ENODEV;
+		goto exit;
+	}
 
-	if (lcd->isopen || !lcd->present) {
-		return -EBUSY;
+	dev = usb_get_intfdata(interface);
+	if (!dev) {
+		retval = -ENODEV;
+		goto exit;
 	}
-	lcd->isopen = 1;
 
-	init_waitqueue_head(&lcd->wait_q);
+	/* increment our usage count for the device */
+	kref_get(&dev->kref);
 
-	info("USBLCD opened.");
+	/* save our object in the file's private structure */
+	file->private_data = dev;
 
-	return 0;
+exit:
+	return retval;
 }
 
-static int close_lcd(struct inode *inode, struct file *file)
+static int lcd_release(struct inode *inode, struct file *file)
 {
-	struct lcd_usb_data *lcd = &lcd_instance;
+	struct usb_lcd *dev;
 
-	lcd->isopen = 0;
+	dev = (struct usb_lcd *)file->private_data;
+	if (dev == NULL)
+		return -ENODEV;
 
-	info("USBLCD closed.");
+	/* decrement the count on our device */
+	kref_put(&dev->kref, lcd_delete);
 	return 0;
 }
 
-static int
-ioctl_lcd(struct inode *inode, struct file *file, unsigned int cmd,
-	  unsigned long arg)
+static ssize_t lcd_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+{
+	struct usb_lcd *dev;
+	int retval = 0;
+
+	dev = (struct usb_lcd *)file->private_data;
+
+	/* do a blocking bulk read to get data from the device */
+	retval = usb_bulk_msg(dev->udev, 
+			      usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
+			      dev->bulk_in_buffer,
+			      min(dev->bulk_in_size, count),
+			      &count, HZ*10);
+
+	/* if the read was successful, copy the data to userspace */
+	if (!retval) {
+		if (copy_to_user(buffer, dev->bulk_in_buffer, count))
+			retval = -EFAULT;
+		else
+			retval = count;
+	}
+
+	return retval;
+}
+
+static int lcd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
 {
-	struct lcd_usb_data *lcd = &lcd_instance;
+	struct usb_lcd *dev;
+
+	dev = (struct usb_lcd *)file->private_data;
+	if (dev == NULL)
+		return -ENODEV;
+	
 	u16 bcdDevice;
 	char buf[30];
 
-	/* Sanity check to make sure lcd is connected, powered, etc */
-	if (lcd == NULL ||
-	    lcd->present == 0 ||
-	    lcd->lcd_dev == NULL)
-		return -1;
-
 	switch (cmd) {
 	case IOCTL_GET_HARD_VERSION:
-		bcdDevice = le16_to_cpu((lcd->lcd_dev)->descriptor.bcdDevice);
+		bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice);
 		sprintf(buf,"%1d%1d.%1d%1d",
 			(bcdDevice & 0xF000)>>12,
 			(bcdDevice & 0xF00)>>8,
@@ -107,269 +165,240 @@ ioctl_lcd(struct inode *inode, struct fi
 	return 0;
 }
 
-static ssize_t
-write_lcd(struct file *file, const char __user *buffer,
-	  size_t count, loff_t * ppos)
-{
-	struct lcd_usb_data *lcd = &lcd_instance;
-
-	unsigned long copy_size;
-	unsigned long bytes_written = 0;
-	unsigned int partial;
-
-	int result = 0;
-	int maxretry;
-
-	/* Sanity check to make sure lcd is connected, powered, etc */
-	if (lcd == NULL ||
-	    lcd->present == 0 ||
-	    lcd->lcd_dev == NULL)
-		return -1;
-
-	do {
-		unsigned long thistime;
-		char *obuf = lcd->obuf;
-
-		thistime = copy_size =
-		    (count >= OBUF_SIZE) ? OBUF_SIZE : count;
-		if (copy_from_user(lcd->obuf, buffer, copy_size))
-			return -EFAULT;
-		maxretry = 5;
-		while (thistime) {
-			if (!lcd->lcd_dev)
-				return -ENODEV;
-			if (signal_pending(current)) {
-				return bytes_written ? bytes_written : -EINTR;
-			}
+static void lcd_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
+{
+	struct usb_lcd *dev;
 
-			result = usb_bulk_msg(lcd->lcd_dev,
-					 usb_sndbulkpipe(lcd->lcd_dev, 1),
-					 obuf, thistime, &partial, 10 * HZ);
-
-			dbg("write stats: result:%d thistime:%lu partial:%u",
-			     result, thistime, partial);
-
-			if (result == -ETIMEDOUT) {	/* NAK - so hold for a while */
-				if (!maxretry--) {
-					return -ETIME;
-				}
-				interruptible_sleep_on_timeout(&lcd-> wait_q, NAK_TIMEOUT);
-				continue;
-			} else if (!result && partial) {
-				obuf += partial;
-				thistime -= partial;
-			} else
-				break;
-		};
-		if (result) {
-			err("Write Whoops - %x", result);
-			return -EIO;
-		}
-		bytes_written += copy_size;
-		count -= copy_size;
-		buffer += copy_size;
-	} while (count > 0);
+	dev = (struct usb_lcd *)urb->context;
+
+	/* sync/async unlink faults aren't errors */
+	if (urb->status &&
+	    !(urb->status == -ENOENT ||
+	      urb->status == -ECONNRESET ||
+              urb->status == -ESHUTDOWN)) {
+		dbg("USBLCD: %s - nonzero write bulk status received: %d",
+		    __FUNCTION__, urb->status);
+	}
 
-	return bytes_written ? bytes_written : -EIO;
+	/* free up our allocated buffer */
+	usb_buffer_free(urb->dev, urb->transfer_buffer_length,
+			urb->transfer_buffer, urb->transfer_dma);
 }
 
-static ssize_t
-read_lcd(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
+static ssize_t lcd_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
 {
-	struct lcd_usb_data *lcd = &lcd_instance;
-	ssize_t read_count;
-	unsigned int partial;
-	int this_read;
-	int result;
-	int maxretry = 10;
-	char *ibuf = lcd->ibuf;
-
-	/* Sanity check to make sure lcd is connected, powered, etc */
-	if (lcd == NULL ||
-	    lcd->present == 0 ||
-	    lcd->lcd_dev == NULL)
-		return -1;
-
-	read_count = 0;
-
-	while (count > 0) {
-		if (signal_pending(current)) {
-			return read_count ? read_count : -EINTR;
-		}
-		if (!lcd->lcd_dev)
-			return -ENODEV;
-		this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
-
-		result = usb_bulk_msg(lcd->lcd_dev,
-				      usb_rcvbulkpipe(lcd->lcd_dev, 0),
-				      ibuf, this_read, &partial,
-				      (int) (HZ * 8));
-
-		dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
-		       result, this_read, partial);
-
-		if (partial) {
-			count = this_read = partial;
-		} else if (result == -ETIMEDOUT || result == 15) {	/* FIXME: 15 ??? */
-			if (!maxretry--) {
-				err("read_lcd: maxretry timeout");
-				return -ETIME;
-			}
-			interruptible_sleep_on_timeout(&lcd->wait_q,
-						       NAK_TIMEOUT);
-			continue;
-		} else if (result != -EREMOTEIO) {
-			err("Read Whoops - result:%u partial:%u this_read:%u",
-			     result, partial, this_read);
-			return -EIO;
-		} else {
-			return (0);
-		}
-
-		if (this_read) {
-			if (copy_to_user(buffer, ibuf, this_read))
-				return -EFAULT;
-			count -= this_read;
-			read_count += this_read;
-			buffer += this_read;
-		}
+	struct usb_lcd *dev;
+        int retval = 0;
+	struct urb *urb = NULL;
+	char *buf = NULL;
+	
+	dev = (struct usb_lcd *)file->private_data;
+	
+	/* verify that we actually have some data to write */
+	if (count == 0)
+		goto exit;
+
+	/* create a urb, and a buffer for it, and copy the data to the urb */
+	urb = usb_alloc_urb(0, GFP_KERNEL);
+	if (!urb) {
+		retval = -ENOMEM;
+		goto error;
 	}
-	return read_count;
+	
+	buf = usb_buffer_alloc(dev->udev, count, GFP_KERNEL, &urb->transfer_dma);
+	if (!buf) {
+		retval = -ENOMEM;
+		goto error;
+	}
+	
+	if (copy_from_user(buf, user_buffer, count)) {
+		retval = -EFAULT;
+		goto error;
+	}
+	
+	/* initialize the urb properly */
+	usb_fill_bulk_urb(urb, dev->udev,
+			  usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
+			  buf, count, lcd_write_bulk_callback, dev);
+	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+	
+	/* send the data out the bulk port */
+	retval = usb_submit_urb(urb, GFP_KERNEL);
+	if (retval) {
+		err("USBLCD: %s - failed submitting write urb, error %d", __FUNCTION__, retval);
+		goto error;
+	}
+	
+	/* release our reference to this urb, the USB core will eventually free it entirely */
+	usb_free_urb(urb);
+
+exit:
+	return count;
+
+error:
+	usb_buffer_free(dev->udev, count, buf, urb->transfer_dma);
+	usb_free_urb(urb);
+	return retval;
 }
 
-static struct
-file_operations usb_lcd_fops = {
-	.owner =	THIS_MODULE,
-	.read =		read_lcd,
-	.write =	write_lcd,
-	.ioctl =	ioctl_lcd,
-	.open =		open_lcd,
-	.release =	close_lcd,
+static struct file_operations lcd_fops = {
+        .owner =        THIS_MODULE,
+        .read =         lcd_read,
+        .write =        lcd_write,
+        .open =         lcd_open,
+	.ioctl =        lcd_ioctl,
+        .release =      lcd_release,
 };
 
-static struct usb_class_driver usb_lcd_class = {
-	.name =		"usb/lcd%d",
-	.fops =		&usb_lcd_fops,
-	.mode =		S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
-	.minor_base =	USBLCD_MINOR,
+/*
+ *  * usb class driver info in order to get a minor number from the usb core,
+ *   * and to have the device registered with devfs and the driver core
+ *    */
+static struct usb_class_driver lcd_class = {
+        .name =         "usb/lcd%d",
+        .fops =         &lcd_fops,
+        .mode =         S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
+        .minor_base =   USBLCD_MINOR,
 };
 
-static int probe_lcd(struct usb_interface *intf, const struct usb_device_id *id)
+static int lcd_probe(struct usb_interface *interface, const struct usb_device_id *id)
 {
-	struct usb_device *dev = interface_to_usbdev(intf);
-	struct lcd_usb_data *lcd = &lcd_instance;
+	struct usb_lcd *dev = NULL;
+	struct usb_host_interface *iface_desc;
+	struct usb_endpoint_descriptor *endpoint;
+	size_t buffer_size;
 	int i;
-	int retval;
+	int retval = -ENOMEM;
 
-	if (le16_to_cpu(dev->descriptor.idProduct) != 0x0001) {
-		warn(KERN_INFO "USBLCD model not supported.");
-		return -ENODEV;
+	/* allocate memory for our device state and initialize it */
+	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
+	if (dev == NULL) {
+		err("Out of memory");
+		goto error;
 	}
+	memset(dev, 0x00, sizeof(*dev));
+	kref_init(&dev->kref);
 
-	if (lcd->present == 1) {
-		warn(KERN_INFO "Multiple USBLCDs are not supported!");
+	dev->udev = usb_get_dev(interface_to_usbdev(interface));
+	dev->interface = interface;
+
+	if (le16_to_cpu(dev->udev->descriptor.idProduct) != 0x0001) {
+		warn(KERN_INFO "USBLCD model not supported.");
 		return -ENODEV;
 	}
+	
+	/* set up the endpoint information */
+	/* use only the first bulk-in and bulk-out endpoints */
+	iface_desc = interface->cur_altsetting;
+	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
+		endpoint = &iface_desc->endpoint[i].desc;
+
+		if (!dev->bulk_in_endpointAddr &&
+		    (endpoint->bEndpointAddress & USB_DIR_IN) &&
+		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
+					== USB_ENDPOINT_XFER_BULK)) {
+			/* we found a bulk in endpoint */
+			buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+			dev->bulk_in_size = buffer_size;
+			dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
+			dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
+			if (!dev->bulk_in_buffer) {
+				err("Could not allocate bulk_in_buffer");
+				goto error;
+			}
+		}
 
-	i = le16_to_cpu(dev->descriptor.bcdDevice);
-
-	info("USBLCD Version %1d%1d.%1d%1d found at address %d",
-		(i & 0xF000)>>12,(i & 0xF00)>>8,(i & 0xF0)>>4,(i & 0xF),
-		dev->devnum);
-
-
-
-	lcd->present = 1;
-	lcd->lcd_dev = dev;
-
-	if (!(lcd->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
-		err("probe_lcd: Not enough memory for the output buffer");
-		return -ENOMEM;
+		if (!dev->bulk_out_endpointAddr &&
+		    !(endpoint->bEndpointAddress & USB_DIR_IN) &&
+		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
+					== USB_ENDPOINT_XFER_BULK)) {
+			/* we found a bulk out endpoint */
+			dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
+		}
 	}
-	dbg("probe_lcd: obuf address:%p", lcd->obuf);
-
-	if (!(lcd->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
-		err("probe_lcd: Not enough memory for the input buffer");
-		kfree(lcd->obuf);
-		return -ENOMEM;
+	if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
+		err("Could not find both bulk-in and bulk-out endpoints");
+		goto error;
 	}
-	dbg("probe_lcd: ibuf address:%p", lcd->ibuf);
 
-	retval = usb_register_dev(intf, &usb_lcd_class);
+	/* save our data pointer in this interface device */
+	usb_set_intfdata(interface, dev);
+
+	/* we can register the device now, as it is ready */
+	retval = usb_register_dev(interface, &lcd_class);
 	if (retval) {
+		/* something prevented us from registering this driver */
 		err("Not able to get a minor for this device.");
-		kfree(lcd->obuf);
-		kfree(lcd->ibuf);
-		return -ENOMEM;
+		usb_set_intfdata(interface, NULL);
+		goto error;
 	}
 
-	usb_set_intfdata (intf, lcd);
+	i = le16_to_cpu(dev->udev->descriptor.bcdDevice);
+
+	info("USBLCD Version %1d%1d.%1d%1d found at address %d",
+		(i & 0xF000)>>12,(i & 0xF00)>>8,(i & 0xF0)>>4,(i & 0xF),
+		dev->udev->devnum);
+
+	/* let the user know what node this device is now attached to */
+	info("USB LCD device now attached to USBLCD-%d", interface->minor);
 	return 0;
+
+error:
+	if (dev)
+		kref_put(&dev->kref, lcd_delete);
+	return retval;
 }
 
-static void disconnect_lcd(struct usb_interface *intf)
+static void lcd_disconnect(struct usb_interface *interface)
 {
-	struct lcd_usb_data *lcd = usb_get_intfdata (intf);
+	struct usb_lcd *dev;
+        int minor = interface->minor;
 
-	usb_set_intfdata (intf, NULL);
-	if (lcd) {
-		usb_deregister_dev(intf, &usb_lcd_class);
-
-		if (lcd->isopen) {
-			lcd->isopen = 0;
-			/* better let it finish - the release will do whats needed */
-			lcd->lcd_dev = NULL;
-			return;
-		}
-		kfree(lcd->ibuf);
-		kfree(lcd->obuf);
+        /* prevent skel_open() from racing skel_disconnect() */
+        lock_kernel();
 
-		info("USBLCD disconnected.");
+        dev = usb_get_intfdata(interface);
+        usb_set_intfdata(interface, NULL);
 
-		lcd->present = 0;
-	}
-}
+        /* give back our minor */
+        usb_deregister_dev(interface, &lcd_class);
+ 
+	unlock_kernel();
 
-static struct usb_device_id id_table [] = {
-	{ .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
-	{},
-};
+	/* decrement our usage count */
+	kref_put(&dev->kref, lcd_delete);
 
-MODULE_DEVICE_TABLE (usb, id_table);
+	info("USB LCD #%d now disconnected", minor);
+}
 
 static struct usb_driver lcd_driver = {
 	.owner =	THIS_MODULE,
 	.name =		"usblcd",
-	.probe =	(void *)probe_lcd,
-	.disconnect =	disconnect_lcd,
+	.probe =	lcd_probe,
+	.disconnect =	lcd_disconnect,
 	.id_table =	id_table,
 };
 
 static int __init usb_lcd_init(void)
 {
-	int retval;
-	retval = usb_register(&lcd_driver);
-	if (retval)
-		goto out;
-
-	info("%s (C) Adams IT Services http://www.usblcd.de";, DRIVER_VERSION);
-	info("USBLCD support registered.");
-out:
-	return retval;
+	int result;
+	
+	result = usb_register(&lcd_driver);
+	if (result)
+		err("usb_register failed. Error number %d", result);
+
+	return result;
 }
 
 
-static void __exit usb_lcd_cleanup(void)
+static void __exit usb_lcd_exit(void)
 {
-	struct lcd_usb_data *lcd = &lcd_instance;
-
-	lcd->present = 0;
 	usb_deregister(&lcd_driver);
 }
 
 module_init(usb_lcd_init);
-module_exit(usb_lcd_cleanup);
+module_exit(usb_lcd_exit);
 
-MODULE_AUTHOR("Adams IT Services <[EMAIL PROTECTED]>");
+MODULE_AUTHOR("Georges Toth <[EMAIL PROTECTED]>");
 MODULE_DESCRIPTION(DRIVER_VERSION);
 MODULE_LICENSE("GPL");

Reply via email to