2 * USB Skeleton driver - 1.1
4 * Copyright (c) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
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, version 2.
11 * This driver is to be used as a skeleton driver to be able to create a
12 * USB driver quickly. The design of it is based on the usb-serial and
15 * Thanks to Oliver Neukum, David Brownell, and Alan Stern for their help
16 * in debugging this driver.
21 * 2003-05-06 - 1.1 - changes due to usb core changes with usb_register_dev()
22 * 2003-02-25 - 1.0 - fix races involving urb->status, unlink_urb(), and
23 * disconnect. Fix transfer amount in read(). Use
24 * macros instead of magic numbers in probe(). Change
25 * size variables to size_t. Show how to eliminate
27 * 2002_12_12 - 0.9 - compile fixes and got rid of fixed minor array.
28 * 2002_09_26 - 0.8 - changes due to USB core conversion to struct device
30 * 2002_02_12 - 0.7 - zero out dev in probe function for devices that do
31 * not have both a bulk in and bulk out endpoint.
32 * Thanks to Holger Waechtler for the fix.
33 * 2001_11_05 - 0.6 - fix minor locking problem in skel_disconnect.
34 * Thanks to Pete Zaitcev for the fix.
35 * 2001_09_04 - 0.5 - fix devfs bug in skel_disconnect. Thanks to wim delvaux
36 * 2001_08_21 - 0.4 - more small bug fixes.
37 * 2001_05_29 - 0.3 - more bug fixes based on review from linux-usb-devel
38 * 2001_05_24 - 0.2 - bug fixes based on review from linux-usb-devel people
39 * 2001_05_01 - 0.1 - first version
43 #include <linux/config.h>
44 #include <linux/kernel.h>
45 #include <linux/errno.h>
46 #include <linux/init.h>
47 #include <linux/slab.h>
48 #include <linux/module.h>
49 #include <linux/smp_lock.h>
50 #include <linux/completion.h>
51 #include <asm/uaccess.h>
52 #include <linux/usb.h>
54 #ifdef CONFIG_USB_DEBUG
60 /* Use our own dbg macro */
62 #define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg); } while (0)
65 /* Version Information */
66 #define DRIVER_VERSION "v1.0"
67 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com"
68 #define DRIVER_DESC "USB Skeleton Driver"
70 /* Module parameters */
71 MODULE_PARM(debug
, "i");
72 MODULE_PARM_DESC(debug
, "Debug enabled or not");
75 /* Define these values to match your devices */
76 #define USB_SKEL_VENDOR_ID 0xfff0
77 #define USB_SKEL_PRODUCT_ID 0xfff0
79 /* table of devices that work with this driver */
80 static struct usb_device_id skel_table
[] = {
81 { USB_DEVICE(USB_SKEL_VENDOR_ID
, USB_SKEL_PRODUCT_ID
) },
82 /* "Gadget Zero" firmware runs under Linux */
83 { USB_DEVICE(0x0525, 0xa4a0) },
84 { } /* Terminating entry */
87 MODULE_DEVICE_TABLE (usb
, skel_table
);
90 /* Get a minor range for your devices from the usb maintainer */
91 #define USB_SKEL_MINOR_BASE 192
93 /* Structure to hold all of our device specific stuff */
95 struct usb_device
* udev
; /* save off the usb device pointer */
96 struct usb_interface
* interface
; /* the interface for this device */
97 unsigned char minor
; /* the starting minor number for this device */
98 unsigned char num_ports
; /* the number of ports this device has */
99 char num_interrupt_in
; /* number of interrupt in endpoints we have */
100 char num_bulk_in
; /* number of bulk in endpoints we have */
101 char num_bulk_out
; /* number of bulk out endpoints we have */
103 unsigned char * bulk_in_buffer
; /* the buffer to receive data */
104 size_t bulk_in_size
; /* the size of the receive buffer */
105 __u8 bulk_in_endpointAddr
; /* the address of the bulk in endpoint */
107 unsigned char * bulk_out_buffer
; /* the buffer to send data */
108 size_t bulk_out_size
; /* the size of the send buffer */
109 struct urb
* write_urb
; /* the urb used to send data */
110 __u8 bulk_out_endpointAddr
; /* the address of the bulk out endpoint */
111 atomic_t write_busy
; /* true iff write urb is busy */
112 struct completion write_finished
; /* wait for the write to finish */
114 int open
; /* if the port is open or not */
115 struct semaphore sem
; /* locks this structure */
119 /* prevent races between open() and disconnect() */
120 static DECLARE_MUTEX (disconnect_sem
);
122 /* local function prototypes */
123 static ssize_t
skel_read (struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
);
124 static ssize_t
skel_write (struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
);
125 static int skel_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
);
126 static int skel_open (struct inode
*inode
, struct file
*file
);
127 static int skel_release (struct inode
*inode
, struct file
*file
);
129 static int skel_probe (struct usb_interface
*interface
, const struct usb_device_id
*id
);
130 static void skel_disconnect (struct usb_interface
*interface
);
132 static void skel_write_bulk_callback (struct urb
*urb
, struct pt_regs
*regs
);
135 * File operations needed when we register this driver.
136 * This assumes that this driver NEEDS file operations,
137 * of course, which means that the driver is expected
138 * to have a node in the /dev directory. If the USB
139 * device were for a network interface then the driver
140 * would use "struct net_driver" instead, and a serial
141 * device would use "struct tty_driver".
143 static struct file_operations skel_fops
= {
145 * The owner field is part of the module-locking
146 * mechanism. The idea is that the kernel knows
147 * which module to increment the use-counter of
148 * BEFORE it calls the device's open() function.
149 * This also means that the kernel can decrement
150 * the use-counter again before calling release()
151 * or should the open() function fail.
153 .owner
= THIS_MODULE
,
159 .release
= skel_release
,
163 * usb class driver info in order to get a minor number from the usb core,
164 * and to have the device registered with devfs and the driver core
166 static struct usb_class_driver skel_class
= {
167 .name
= "usb/skel%d",
169 .mode
= S_IFCHR
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
,
170 .minor_base
= USB_SKEL_MINOR_BASE
,
173 /* usb specific object needed to register this driver with the usb subsystem */
174 static struct usb_driver skel_driver
= {
175 .owner
= THIS_MODULE
,
178 .disconnect
= skel_disconnect
,
179 .id_table
= skel_table
,
184 * usb_skel_debug_data
186 static inline void usb_skel_debug_data (const char *function
, int size
, const unsigned char *data
)
193 printk (KERN_DEBUG __FILE__
": %s - length = %d, data = ",
195 for (i
= 0; i
< size
; ++i
) {
196 printk ("%.2x ", data
[i
]);
205 static inline void skel_delete (struct usb_skel
*dev
)
207 if (dev
->bulk_in_buffer
!= NULL
)
208 kfree (dev
->bulk_in_buffer
);
209 if (dev
->bulk_out_buffer
!= NULL
)
210 usb_buffer_free (dev
->udev
, dev
->bulk_out_size
,
211 dev
->bulk_out_buffer
,
212 dev
->write_urb
->transfer_dma
);
213 if (dev
->write_urb
!= NULL
)
214 usb_free_urb (dev
->write_urb
);
222 static int skel_open (struct inode
*inode
, struct file
*file
)
224 struct usb_skel
*dev
= NULL
;
225 struct usb_interface
*interface
;
229 dbg("%s", __FUNCTION__
);
231 subminor
= minor (inode
->i_rdev
);
233 /* prevent disconnects */
234 down (&disconnect_sem
);
236 interface
= usb_find_interface (&skel_driver
, subminor
);
238 err ("%s - error, can't find device for minor %d",
239 __FUNCTION__
, subminor
);
244 dev
= usb_get_intfdata(interface
);
250 /* lock this device */
253 /* increment our usage count for the driver */
256 /* save our object in the file's private structure */
257 file
->private_data
= dev
;
259 /* unlock this device */
263 up (&disconnect_sem
);
271 static int skel_release (struct inode
*inode
, struct file
*file
)
273 struct usb_skel
*dev
;
276 dev
= (struct usb_skel
*)file
->private_data
;
278 dbg ("%s - object is NULL", __FUNCTION__
);
282 dbg("%s - minor %d", __FUNCTION__
, dev
->minor
);
284 /* lock our device */
287 if (dev
->open
<= 0) {
288 dbg ("%s - device not opened", __FUNCTION__
);
290 goto exit_not_opened
;
293 /* wait for any bulk writes that might be going on to finish up */
294 if (atomic_read (&dev
->write_busy
))
295 wait_for_completion (&dev
->write_finished
);
299 if (dev
->udev
== NULL
) {
300 /* the device was unplugged before the file was released */
316 static ssize_t
skel_read (struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
318 struct usb_skel
*dev
;
321 dev
= (struct usb_skel
*)file
->private_data
;
323 dbg("%s - minor %d, count = %d", __FUNCTION__
, dev
->minor
, count
);
325 /* lock this object */
328 /* verify that the device wasn't unplugged */
329 if (dev
->udev
== NULL
) {
334 /* do a blocking bulk read to get data from the device */
335 retval
= usb_bulk_msg (dev
->udev
,
336 usb_rcvbulkpipe (dev
->udev
,
337 dev
->bulk_in_endpointAddr
),
339 min (dev
->bulk_in_size
, count
),
342 /* if the read was successful, copy the data to userspace */
344 if (copy_to_user (buffer
, dev
->bulk_in_buffer
, count
))
350 /* unlock the device */
359 * A device driver has to decide how to report I/O errors back to the
360 * user. The safest course is to wait for the transfer to finish before
361 * returning so that any errors will be reported reliably. skel_read()
362 * works like this. But waiting for I/O is slow, so many drivers only
363 * check for errors during I/O initiation and do not report problems
364 * that occur during the actual transfer. That's what we will do here.
366 * A driver concerned with maximum I/O throughput would use double-
367 * buffering: Two urbs would be devoted to write transfers, so that
368 * one urb could always be active while the other was waiting for the
369 * user to send more data.
371 static ssize_t
skel_write (struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
)
373 struct usb_skel
*dev
;
374 ssize_t bytes_written
= 0;
377 dev
= (struct usb_skel
*)file
->private_data
;
379 dbg("%s - minor %d, count = %d", __FUNCTION__
, dev
->minor
, count
);
381 /* lock this object */
384 /* verify that the device wasn't unplugged */
385 if (dev
->udev
== NULL
) {
390 /* verify that we actually have some data to write */
392 dbg("%s - write request of 0 bytes", __FUNCTION__
);
396 /* wait for a previous write to finish up; we don't use a timeout
397 * and so a nonresponsive device can delay us indefinitely.
399 if (atomic_read (&dev
->write_busy
))
400 wait_for_completion (&dev
->write_finished
);
402 /* we can only write as much as our buffer will hold */
403 bytes_written
= min (dev
->bulk_out_size
, count
);
405 /* copy the data from userspace into our transfer buffer;
406 * this is the only copy required.
408 if (copy_from_user(dev
->write_urb
->transfer_buffer
, buffer
,
414 usb_skel_debug_data (__FUNCTION__
, bytes_written
,
415 dev
->write_urb
->transfer_buffer
);
417 /* this urb was already set up, except for this write size */
418 dev
->write_urb
->transfer_buffer_length
= bytes_written
;
420 /* send the data out the bulk port */
421 /* a character device write uses GFP_KERNEL,
422 unless a spinlock is held */
423 init_completion (&dev
->write_finished
);
424 atomic_set (&dev
->write_busy
, 1);
425 retval
= usb_submit_urb(dev
->write_urb
, GFP_KERNEL
);
427 atomic_set (&dev
->write_busy
, 0);
428 err("%s - failed submitting write urb, error %d",
429 __FUNCTION__
, retval
);
431 retval
= bytes_written
;
435 /* unlock the device */
445 static int skel_ioctl (struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
447 struct usb_skel
*dev
;
449 dev
= (struct usb_skel
*)file
->private_data
;
451 /* lock this object */
454 /* verify that the device wasn't unplugged */
455 if (dev
->udev
== NULL
) {
460 dbg("%s - minor %d, cmd 0x%.4x, arg %ld", __FUNCTION__
,
461 dev
->minor
, cmd
, arg
);
463 /* fill in your device specific stuff here */
465 /* unlock the device */
468 /* return that we did not understand this ioctl call */
474 * skel_write_bulk_callback
476 static void skel_write_bulk_callback (struct urb
*urb
, struct pt_regs
*regs
)
478 struct usb_skel
*dev
= (struct usb_skel
*)urb
->context
;
480 dbg("%s - minor %d", __FUNCTION__
, dev
->minor
);
482 /* sync/async unlink faults aren't errors */
483 if (urb
->status
&& !(urb
->status
== -ENOENT
||
484 urb
->status
== -ECONNRESET
)) {
485 dbg("%s - nonzero write bulk status received: %d",
486 __FUNCTION__
, urb
->status
);
489 /* notify anyone waiting that the write has finished */
490 atomic_set (&dev
->write_busy
, 0);
491 complete (&dev
->write_finished
);
498 * Called by the usb core when a new device is connected that it thinks
499 * this driver might be interested in.
501 static int skel_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
503 struct usb_device
*udev
= interface_to_usbdev(interface
);
504 struct usb_skel
*dev
= NULL
;
505 struct usb_host_interface
*iface_desc
;
506 struct usb_endpoint_descriptor
*endpoint
;
513 /* See if the device offered us matches what we can accept */
514 if ((udev
->descriptor
.idVendor
!= USB_SKEL_VENDOR_ID
) ||
515 (udev
->descriptor
.idProduct
!= USB_SKEL_PRODUCT_ID
)) {
519 retval
= usb_register_dev (interface
, &skel_class
);
521 /* something prevented us from registering this driver */
522 err ("Not able to get a minor for this device.");
526 /* allocate memory for our device state and initialize it */
527 dev
= kmalloc (sizeof(struct usb_skel
), GFP_KERNEL
);
529 err ("Out of memory");
532 memset (dev
, 0x00, sizeof (*dev
));
534 init_MUTEX (&dev
->sem
);
536 dev
->interface
= interface
;
537 dev
->minor
= interface
->minor
;
539 /* set up the endpoint information */
540 /* check out the endpoints */
541 /* use only the first bulk-in and bulk-out endpoints */
542 iface_desc
= &interface
->altsetting
[0];
543 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
544 endpoint
= &iface_desc
->endpoint
[i
].desc
;
546 if (!dev
->bulk_in_endpointAddr
&&
547 (endpoint
->bEndpointAddress
& USB_DIR_IN
) &&
548 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
549 == USB_ENDPOINT_XFER_BULK
)) {
550 /* we found a bulk in endpoint */
551 buffer_size
= endpoint
->wMaxPacketSize
;
552 dev
->bulk_in_size
= buffer_size
;
553 dev
->bulk_in_endpointAddr
= endpoint
->bEndpointAddress
;
554 dev
->bulk_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
555 if (!dev
->bulk_in_buffer
) {
556 err("Couldn't allocate bulk_in_buffer");
561 if (!dev
->bulk_out_endpointAddr
&&
562 !(endpoint
->bEndpointAddress
& USB_DIR_IN
) &&
563 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
564 == USB_ENDPOINT_XFER_BULK
)) {
565 /* we found a bulk out endpoint */
566 /* a probe() may sleep and has no restrictions on memory allocations */
567 dev
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
568 if (!dev
->write_urb
) {
569 err("No free urbs available");
572 dev
->bulk_out_endpointAddr
= endpoint
->bEndpointAddress
;
574 /* on some platforms using this kind of buffer alloc
575 * call eliminates a dma "bounce buffer".
577 * NOTE: you'd normally want i/o buffers that hold
578 * more than one packet, so that i/o delays between
579 * packets don't hurt throughput.
581 buffer_size
= endpoint
->wMaxPacketSize
;
582 dev
->bulk_out_size
= buffer_size
;
583 dev
->write_urb
->transfer_flags
= (URB_NO_DMA_MAP
|
585 dev
->bulk_out_buffer
= usb_buffer_alloc (udev
,
586 buffer_size
, GFP_KERNEL
,
587 &dev
->write_urb
->transfer_dma
);
588 if (!dev
->bulk_out_buffer
) {
589 err("Couldn't allocate bulk_out_buffer");
592 usb_fill_bulk_urb(dev
->write_urb
, udev
,
593 usb_sndbulkpipe(udev
,
594 endpoint
->bEndpointAddress
),
595 dev
->bulk_out_buffer
, buffer_size
,
596 skel_write_bulk_callback
, dev
);
599 if (!(dev
->bulk_in_endpointAddr
&& dev
->bulk_out_endpointAddr
)) {
600 err("Couldn't find both bulk-in and bulk-out endpoints");
604 /* let the user know what node this device is now attached to */
605 info ("USB Skeleton device now attached to USBSkel-%d", dev
->minor
);
614 usb_deregister_dev (interface
, &skel_class
);
618 usb_set_intfdata (interface
, dev
);
628 * Called by the usb core when the device is removed from the system.
630 * This routine guarantees that the driver will not submit any more urbs
631 * by clearing dev->udev. It is also supposed to terminate any currently
632 * active urbs. Unfortunately, usb_bulk_msg(), used in skel_read(), does
633 * not provide any way to do this. But at least we can cancel an active
636 static void skel_disconnect(struct usb_interface
*interface
)
638 struct usb_skel
*dev
;
641 /* prevent races with open() */
642 down (&disconnect_sem
);
644 dev
= usb_get_intfdata (interface
);
645 usb_set_intfdata (interface
, NULL
);
653 interface
->minor
= -1;
657 /* give back our minor */
658 usb_deregister_dev (interface
, &skel_class
);
660 /* terminate an ongoing write */
661 if (atomic_read (&dev
->write_busy
)) {
662 usb_unlink_urb (dev
->write_urb
);
663 wait_for_completion (&dev
->write_finished
);
669 /* if the device is not opened, then we clean up right now */
673 up (&disconnect_sem
);
675 info("USB Skeleton #%d now disconnected", minor
);
683 static int __init
usb_skel_init(void)
687 /* register this driver with the USB subsystem */
688 result
= usb_register(&skel_driver
);
690 err("usb_register failed. Error number %d",
695 info(DRIVER_DESC
" " DRIVER_VERSION
);
703 static void __exit
usb_skel_exit(void)
705 /* deregister this driver with the USB subsystem */
706 usb_deregister(&skel_driver
);
710 module_init (usb_skel_init
);
711 module_exit (usb_skel_exit
);
713 MODULE_AUTHOR(DRIVER_AUTHOR
);
714 MODULE_DESCRIPTION(DRIVER_DESC
);
715 MODULE_LICENSE("GPL");