File: /usr/src/linux/drivers/usb/usb-skeleton.c

1     /*
2      * USB Skeleton driver - 0.4
3      *
4      * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
5      *
6      *	This program is free software; you can redistribute it and/or
7      *	modify it under the terms of the GNU General Public License as
8      *	published by the Free Software Foundation; either version 2 of
9      *	the License, or (at your option) any later version.
10      *
11      *
12      * This driver is to be used as a skeleton driver to be able to create a
13      * USB driver quickly.  The design of it is based on the usb-serial and
14      * dc2xx drivers.
15      *
16      * Thanks to Oliver Neukum and David Brownell for their help in debugging
17      * this driver.
18      *
19      * TODO:
20      *	- fix urb->status race condition in write sequence
21      *	- move minor_table to a dynamic list.
22      *
23      * History:
24      *
25      * 2001_08_21 - 0.4 - more small bug fixes.
26      * 2001_05_29 - 0.3 - more bug fixes based on review from linux-usb-devel
27      * 2001_05_24 - 0.2 - bug fixes based on review from linux-usb-devel people
28      * 2001_05_01 - 0.1 - first version
29      * 
30      */
31     
32     #include <linux/config.h>
33     #include <linux/kernel.h>
34     #include <linux/sched.h>
35     #include <linux/signal.h>
36     #include <linux/errno.h>
37     #include <linux/poll.h>
38     #include <linux/init.h>
39     #include <linux/slab.h>
40     #include <linux/fcntl.h>
41     #include <linux/module.h>
42     #include <linux/spinlock.h>
43     #include <linux/list.h>
44     #include <linux/smp_lock.h>
45     #include <linux/devfs_fs_kernel.h>
46     #include <linux/usb.h>
47     
48     #ifdef CONFIG_USB_DEBUG
49     	static int debug = 1;
50     #else
51     	static int debug;
52     #endif
53     
54     /* Use our own dbg macro */
55     #undef dbg
56     #define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg); } while (0)
57     
58     
59     /* Version Information */
60     #define DRIVER_VERSION "v0.4"
61     #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com"
62     #define DRIVER_DESC "USB Skeleton Driver"
63     
64     /* Module paramaters */
65     MODULE_PARM(debug, "i");
66     MODULE_PARM_DESC(debug, "Debug enabled or not");
67     
68     
69     /* Define these values to match your device */
70     #define USB_SKEL_VENDOR_ID	0xfff0
71     #define USB_SKEL_PRODUCT_ID	0xfff0
72     
73     /* table of devices that work with this driver */
74     static struct usb_device_id skel_table [] = {
75     	{ USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
76     	{ }					/* Terminating entry */
77     };
78     
79     MODULE_DEVICE_TABLE (usb, skel_table);
80     
81     
82     
83     /* Get a minor range for your devices from the usb maintainer */
84     #define USB_SKEL_MINOR_BASE	200	
85     
86     /* we can have up to this number of device plugged in at once */
87     #define MAX_DEVICES		16
88     
89     /* Structure to hold all of our device specific stuff */
90     struct usb_skel {
91     	struct usb_device *	udev;			/* save off the usb device pointer */
92     	struct usb_interface *	interface;		/* the interface for this device */
93     	devfs_handle_t		devfs;			/* devfs device node */
94     	unsigned char		minor;			/* the starting minor number for this device */
95     	unsigned char		num_ports;		/* the number of ports this device has */
96     	char			num_interrupt_in;	/* number of interrupt in endpoints we have */
97     	char			num_bulk_in;		/* number of bulk in endpoints we have */
98     	char			num_bulk_out;		/* number of bulk out endpoints we have */
99     
100     	unsigned char *		bulk_in_buffer;		/* the buffer to receive data */
101     	int			bulk_in_size;		/* the size of the receive buffer */
102     	__u8			bulk_in_endpointAddr;	/* the address of the bulk in endpoint */
103     
104     	unsigned char *		bulk_out_buffer;	/* the buffer to send data */
105     	int			bulk_out_size;		/* the size of the send buffer */
106     	struct urb *		write_urb;		/* the urb used to send data */
107     	__u8			bulk_out_endpointAddr;	/* the address of the bulk out endpoint */
108     
109     	struct tq_struct	tqueue;			/* task queue for line discipline waking up */
110     	int			open_count;		/* number of times this port has been opened */
111     	struct semaphore	sem;			/* locks this structure */
112     };
113     
114     
115     /* the global usb devfs handle */
116     extern devfs_handle_t usb_devfs_handle;
117     
118     
119     /* local function prototypes */
120     static ssize_t skel_read	(struct file *file, char *buffer, size_t count, loff_t *ppos);
121     static ssize_t skel_write	(struct file *file, const char *buffer, size_t count, loff_t *ppos);
122     static int skel_ioctl		(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
123     static int skel_open		(struct inode *inode, struct file *file);
124     static int skel_release		(struct inode *inode, struct file *file);
125     	
126     static void * skel_probe	(struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *id);
127     static void skel_disconnect	(struct usb_device *dev, void *ptr);
128     
129     static void skel_write_bulk_callback	(struct urb *urb);
130     
131     
132     /* array of pointers to our devices that are currently connected */
133     static struct usb_skel		*minor_table[MAX_DEVICES];
134     
135     /* lock to protect the minor_table structure */
136     static DECLARE_MUTEX (minor_table_mutex);
137     
138     /* file operations needed when we register this driver */
139     static struct file_operations skel_fops = {
140     	owner:		THIS_MODULE,
141     	read:		skel_read,
142     	write:		skel_write,
143     	ioctl:		skel_ioctl,
144     	open:		skel_open,
145     	release:	skel_release,
146     };      
147     
148     
149     /* usb specific object needed to register this driver with the usb subsystem */
150     static struct usb_driver skel_driver = {
151     	name:		"skeleton",
152     	probe:		skel_probe,
153     	disconnect:	skel_disconnect,
154     	fops:		&skel_fops,
155     	minor:		USB_SKEL_MINOR_BASE,
156     	id_table:	skel_table,
157     };
158     
159     
160     
161     
162     
163     /**
164      *	usb_skel_debug_data
165      */
166     static inline void usb_skel_debug_data (const char *function, int size, const unsigned char *data)
167     {
168     	int i;
169     
170     	if (!debug)
171     		return;
172     	
173     	printk (KERN_DEBUG __FILE__": %s - length = %d, data = ", 
174     		function, size);
175     	for (i = 0; i < size; ++i) {
176     		printk ("%.2x ", data[i]);
177     	}
178     	printk ("\n");
179     }
180     
181     
182     /**
183      *	skel_delete
184      */
185     static inline void skel_delete (struct usb_skel *dev)
186     {
187     	minor_table[dev->minor] = NULL;
188     	if (dev->bulk_in_buffer != NULL)
189     		kfree (dev->bulk_in_buffer);
190     	if (dev->bulk_out_buffer != NULL)
191     		kfree (dev->bulk_out_buffer);
192     	if (dev->write_urb != NULL)
193     		usb_free_urb (dev->write_urb);
194     	kfree (dev);
195     }
196     
197     
198     /**
199      *	skel_open
200      */
201     static int skel_open (struct inode *inode, struct file *file)
202     {
203     	struct usb_skel *dev = NULL;
204     	int subminor;
205     	int retval = 0;
206     	
207     	dbg(__FUNCTION__);
208     
209     	subminor = MINOR (inode->i_rdev) - USB_SKEL_MINOR_BASE;
210     	if ((subminor < 0) ||
211     	    (subminor >= MAX_DEVICES)) {
212     		return -ENODEV;
213     	}
214     
215     	/* increment our usage count for the module */
216     	MOD_INC_USE_COUNT;
217     
218     	/* lock our minor table and get our local data for this minor */
219     	down (&minor_table_mutex);
220     	dev = minor_table[subminor];
221     	if (dev == NULL) {
222     		up (&minor_table_mutex);
223     		MOD_DEC_USE_COUNT;
224     		return -ENODEV;
225     	}
226     
227     	/* lock this device */
228     	down (&dev->sem);
229     
230     	/* unlock the minor table */
231     	up (&minor_table_mutex);
232     
233     	/* increment our usage count for the driver */
234     	++dev->open_count;
235     
236     	/* save our object in the file's private structure */
237     	file->private_data = dev;
238     
239     	/* unlock this device */
240     	up (&dev->sem);
241     
242     	return retval;
243     }
244     
245     
246     /**
247      *	skel_release
248      */
249     static int skel_release (struct inode *inode, struct file *file)
250     {
251     	struct usb_skel *dev;
252     	int retval = 0;
253     
254     	dev = (struct usb_skel *)file->private_data;
255     	if (dev == NULL) {
256     		dbg (__FUNCTION__ " - object is NULL");
257     		return -ENODEV;
258     	}
259     
260     	dbg(__FUNCTION__ " - minor %d", dev->minor);
261     
262     	/* lock our minor table */
263     	down (&minor_table_mutex);
264     
265     	/* lock our device */
266     	down (&dev->sem);
267     
268     	if (dev->open_count <= 0) {
269     		dbg (__FUNCTION__ " - device not opened");
270     		retval = -ENODEV;
271     		goto exit_not_opened;
272     	}
273     
274     	if (dev->udev == NULL) {
275     		/* the device was unplugged before the file was released */
276     		up (&dev->sem);
277     		skel_delete (dev);
278     		MOD_DEC_USE_COUNT;
279     		up (&minor_table_mutex);
280     		return 0;
281     	}
282     
283     	/* decrement our usage count for the device */
284     	--dev->open_count;
285     	if (dev->open_count <= 0) {
286     		/* shutdown any bulk writes that might be going on */
287     		usb_unlink_urb (dev->write_urb);
288     		dev->open_count = 0;
289     	}
290     
291     	/* decrement our usage count for the module */
292     	MOD_DEC_USE_COUNT;
293     
294     exit_not_opened:
295     	up (&dev->sem);
296     	up (&minor_table_mutex);
297     
298     	return retval;
299     }
300     
301     
302     /**
303      *	skel_read
304      */
305     static ssize_t skel_read (struct file *file, char *buffer, size_t count, loff_t *ppos)
306     {
307     	struct usb_skel *dev;
308     	int retval = 0;
309     
310     	dev = (struct usb_skel *)file->private_data;
311     	
312     	dbg(__FUNCTION__ " - minor %d, count = %d", dev->minor, count);
313     
314     	/* lock this object */
315     	down (&dev->sem);
316     
317     	/* verify that the device wasn't unplugged */
318     	if (dev->udev == NULL) {
319     		up (&dev->sem);
320     		return -ENODEV;
321     	}
322     	
323     	/* do an immediate bulk read to get data from the device */
324     	retval = usb_bulk_msg (dev->udev,
325     			       usb_rcvbulkpipe (dev->udev, 
326     						dev->bulk_in_endpointAddr),
327     			       dev->bulk_in_buffer, dev->bulk_in_size,
328     			       &count, HZ*10);
329     
330     	/* if the read was successful, copy the data to userspace */
331     	if (!retval) {
332     		if (copy_to_user (buffer, dev->bulk_in_buffer, count))
333     			retval = -EFAULT;
334     		else
335     			retval = count;
336     	}
337     	
338     	/* unlock the device */
339     	up (&dev->sem);
340     	return retval;
341     }
342     
343     
344     /**
345      *	skel_write
346      */
347     static ssize_t skel_write (struct file *file, const char *buffer, size_t count, loff_t *ppos)
348     {
349     	struct usb_skel *dev;
350     	ssize_t bytes_written = 0;
351     	int retval = 0;
352     
353     	dev = (struct usb_skel *)file->private_data;
354     
355     	dbg(__FUNCTION__ " - minor %d, count = %d", dev->minor, count);
356     
357     	/* lock this object */
358     	down (&dev->sem);
359     
360     	/* verify that the device wasn't unplugged */
361     	if (dev->udev == NULL) {
362     		retval = -ENODEV;
363     		goto exit;
364     	}
365     
366     	/* verify that we actually have some data to write */
367     	if (count == 0) {
368     		dbg(__FUNCTION__ " - write request of 0 bytes");
369     		goto exit;
370     	}
371     
372     	/* see if we are already in the middle of a write */
373     	if (dev->write_urb->status == -EINPROGRESS) {
374     		dbg (__FUNCTION__ " - already writing");
375     		goto exit;
376     	}
377     
378     	/* we can only write as much as 1 urb will hold */
379     	bytes_written = (count > dev->bulk_out_size) ? 
380     				dev->bulk_out_size : count;
381     
382     	/* copy the data from userspace into our urb */
383     	if (copy_from_user(dev->write_urb->transfer_buffer, buffer, 
384     			   bytes_written)) {
385     		retval = -EFAULT;
386     		goto exit;
387     	}
388     
389     	usb_skel_debug_data (__FUNCTION__, bytes_written, 
390     			     dev->write_urb->transfer_buffer);
391     
392     	/* set up our urb */
393     	FILL_BULK_URB(dev->write_urb, dev->udev, 
394     		      usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
395     		      dev->write_urb->transfer_buffer, bytes_written,
396     		      skel_write_bulk_callback, dev);
397     
398     	/* send the data out the bulk port */
399     	retval = usb_submit_urb(dev->write_urb);
400     	if (retval) {
401     		err(__FUNCTION__ " - failed submitting write urb, error %d",
402     		    retval);
403     	} else {
404     		retval = bytes_written;
405     	}
406     
407     exit:
408     	/* unlock the device */
409     	up (&dev->sem);
410     
411     	return retval;
412     }
413     
414     
415     /**
416      *	skel_ioctl
417      */
418     static int skel_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
419     {
420     	struct usb_skel *dev;
421     
422     	dev = (struct usb_skel *)file->private_data;
423     
424     	/* lock this object */
425     	down (&dev->sem);
426     
427     	/* verify that the device wasn't unplugged */
428     	if (dev->udev == NULL) {
429     		up (&dev->sem);
430     		return -ENODEV;
431     	}
432     
433     	dbg(__FUNCTION__ " - minor %d, cmd 0x%.4x, arg %ld", 
434     	    dev->minor, cmd, arg);
435     
436     
437     	/* fill in your device specific stuff here */
438     	
439     	/* unlock the device */
440     	up (&dev->sem);
441     	
442     	/* return that we did not understand this ioctl call */
443     	return -ENOTTY;
444     }
445     
446     
447     /**
448      *	skel_write_bulk_callback
449      */
450     static void skel_write_bulk_callback (struct urb *urb)
451     {
452     	struct usb_skel *dev = (struct usb_skel *)urb->context;
453     
454     	dbg(__FUNCTION__ " - minor %d", dev->minor);
455     
456     	if ((urb->status != -ENOENT) && 
457     	    (urb->status != -ECONNRESET)) {
458     		dbg(__FUNCTION__ " - nonzero write bulk status received: %d",
459     		    urb->status);
460     		return;
461     	}
462     
463     	return;
464     }
465     
466     
467     /**
468      *	skel_probe
469      *
470      *	Called by the usb core when a new device is connected that it thinks
471      *	this driver might be interested in.
472      */
473     static void * skel_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
474     {
475     	struct usb_skel *dev = NULL;
476     	struct usb_interface *interface;
477     	struct usb_interface_descriptor *iface_desc;
478     	struct usb_endpoint_descriptor *endpoint;
479     	int minor;
480     	int buffer_size;
481     	int i;
482     	char name[10];
483     
484     	
485     	/* See if the device offered us matches what we can accept */
486     	if ((udev->descriptor.idVendor != USB_SKEL_VENDOR_ID) ||
487     	    (udev->descriptor.idProduct != USB_SKEL_PRODUCT_ID)) {
488     		return NULL;
489     	}
490     
491     	/* select a "subminor" number (part of a minor number) */
492     	down (&minor_table_mutex);
493     	for (minor = 0; minor < MAX_DEVICES; ++minor) {
494     		if (minor_table[minor] == NULL)
495     			break;
496     	}
497     	if (minor >= MAX_DEVICES) {
498     		info ("Too many devices plugged in, can not handle this device.");
499     		goto exit;
500     	}
501     
502     	/* allocate memory for our device state and intialize it */
503     	dev = kmalloc (sizeof(struct usb_skel), GFP_KERNEL);
504     	if (dev == NULL) {
505     		err ("Out of memory");
506     		goto exit;
507     	}
508     	minor_table[minor] = dev;
509     
510     	interface = &udev->actconfig->interface[ifnum];
511     
512     	init_MUTEX (&dev->sem);
513     	dev->udev = udev;
514     	dev->interface = interface;
515     	dev->minor = minor;
516     
517     	/* set up the endpoint information */
518     	/* check out the endpoints */
519     	iface_desc = &interface->altsetting[0];
520     	for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
521     		endpoint = &iface_desc->endpoint[i];
522     
523     		if ((endpoint->bEndpointAddress & 0x80) &&
524     		    ((endpoint->bmAttributes & 3) == 0x02)) {
525     			/* we found a bulk in endpoint */
526     			buffer_size = endpoint->wMaxPacketSize;
527     			dev->bulk_in_size = buffer_size;
528     			dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
529     			dev->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
530     			if (!dev->bulk_in_buffer) {
531     				err("Couldn't allocate bulk_in_buffer");
532     				goto error;
533     			}
534     		}
535     		
536     		if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
537     		    ((endpoint->bmAttributes & 3) == 0x02)) {
538     			/* we found a bulk out endpoint */
539     			dev->write_urb = usb_alloc_urb(0);
540     			if (!dev->write_urb) {
541     				err("No free urbs available");
542     				goto error;
543     			}
544     			buffer_size = endpoint->wMaxPacketSize;
545     			dev->bulk_out_size = buffer_size;
546     			dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
547     			dev->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
548     			if (!dev->bulk_out_buffer) {
549     				err("Couldn't allocate bulk_out_buffer");
550     				goto error;
551     			}
552     			FILL_BULK_URB(dev->write_urb, udev, 
553     				      usb_sndbulkpipe(udev, 
554     						      endpoint->bEndpointAddress),
555     				      dev->bulk_out_buffer, buffer_size,
556     				      skel_write_bulk_callback, dev);
557     		}
558     	}
559     
560     	/* initialize the devfs node for this device and register it */
561     	sprintf(name, "skel%d", dev->minor);
562     	
563     	dev->devfs = devfs_register (usb_devfs_handle, name,
564     				     DEVFS_FL_DEFAULT, USB_MAJOR,
565     				     USB_SKEL_MINOR_BASE + dev->minor,
566     				     S_IFCHR | S_IRUSR | S_IWUSR | 
567     				     S_IRGRP | S_IWGRP | S_IROTH, 
568     				     &skel_fops, NULL);
569     
570     	/* let the user know what node this device is now attached to */
571     	info ("USB Skeleton device now attached to USBSkel%d", dev->minor);
572     	goto exit;
573     	
574     error:
575     	skel_delete (dev);
576     	dev = NULL;
577     
578     exit:
579     	up (&minor_table_mutex);
580     	return dev;
581     }
582     
583     
584     /**
585      *	skel_disconnect
586      *
587      *	Called by the usb core when the device is removed from the system.
588      */
589     static void skel_disconnect(struct usb_device *udev, void *ptr)
590     {
591     	struct usb_skel *dev;
592     	int minor;
593     
594     	dev = (struct usb_skel *)ptr;
595     	
596     	down (&minor_table_mutex);
597     	down (&dev->sem);
598     		
599     	minor = dev->minor;
600     
601     	/* if the device is not opened, then we clean up right now */
602     	if (!dev->open_count) {
603     		skel_delete (dev);
604     	} else {
605     		dev->udev = NULL;
606     		up (&dev->sem);
607     	}
608     
609     	/* remove our devfs node */
610     	devfs_unregister(dev->devfs);
611     
612     	info("USB Skeleton #%d now disconnected", minor);
613     	up (&minor_table_mutex);
614     }
615     
616     
617     
618     /**
619      *	usb_skel_init
620      */
621     static int __init usb_skel_init(void)
622     {
623     	int result;
624     
625     	/* register this driver with the USB subsystem */
626     	result = usb_register(&skel_driver);
627     	if (result < 0) {
628     		err("usb_register failed for the "__FILE__" driver. Error number %d",
629     		    result);
630     		return -1;
631     	}
632     
633     	info(DRIVER_DESC " " DRIVER_VERSION);
634     	return 0;
635     }
636     
637     
638     /**
639      *	usb_skel_exit
640      */
641     static void __exit usb_skel_exit(void)
642     {
643     	/* deregister this driver with the USB subsystem */
644     	usb_deregister(&skel_driver);
645     }
646     
647     
648     module_init (usb_skel_init);
649     module_exit (usb_skel_exit);
650     
651     MODULE_AUTHOR(DRIVER_AUTHOR);
652     MODULE_DESCRIPTION(DRIVER_DESC);
653     MODULE_LICENSE("GPL");
654     
655