2 * USB Skeleton driver - 2.2
4 * Copyright (C) 2001-2004 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.
10 * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
11 * but has been rewritten to be easier to read and use.
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/kref.h>
21 #include <asm/uaccess.h>
22 #include <linux/usb.h>
23 #include <linux/mutex.h>
26 /* Define these values to match your devices */
27 #define USB_SKEL_VENDOR_ID 0xfff0
28 #define USB_SKEL_PRODUCT_ID 0xfff0
30 /* table of devices that work with this driver */
31 static struct usb_device_id skel_table
[] = {
32 { USB_DEVICE(USB_SKEL_VENDOR_ID
, USB_SKEL_PRODUCT_ID
) },
33 { } /* Terminating entry */
35 MODULE_DEVICE_TABLE(usb
, skel_table
);
38 /* Get a minor range for your devices from the usb maintainer */
39 #define USB_SKEL_MINOR_BASE 192
41 /* our private defines. if this grows any larger, use your own .h file */
42 #define MAX_TRANSFER (PAGE_SIZE - 512)
43 /* MAX_TRANSFER is chosen so that the VM is not stressed by
44 allocations > PAGE_SIZE and the number of packets in a page
45 is an integer 512 is the largest possible packet on EHCI */
46 #define WRITES_IN_FLIGHT 8
47 /* arbitrarily chosen */
49 /* Structure to hold all of our device specific stuff */
51 struct usb_device
*udev
; /* the usb device for this device */
52 struct usb_interface
*interface
; /* the interface for this device */
53 struct semaphore limit_sem
; /* limiting the number of writes in progress */
54 struct usb_anchor submitted
; /* in case we need to retract our submissions */
55 unsigned char *bulk_in_buffer
; /* the buffer to receive data */
56 size_t bulk_in_size
; /* the size of the receive buffer */
57 __u8 bulk_in_endpointAddr
; /* the address of the bulk in endpoint */
58 __u8 bulk_out_endpointAddr
; /* the address of the bulk out endpoint */
59 int errors
; /* the last request tanked */
60 int open_count
; /* count the number of openers */
61 spinlock_t err_lock
; /* lock for errors */
63 struct mutex io_mutex
; /* synchronize I/O with disconnect */
65 #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
67 static struct usb_driver skel_driver
;
68 static void skel_draw_down(struct usb_skel
*dev
);
70 static void skel_delete(struct kref
*kref
)
72 struct usb_skel
*dev
= to_skel_dev(kref
);
74 usb_put_dev(dev
->udev
);
75 kfree(dev
->bulk_in_buffer
);
79 static int skel_open(struct inode
*inode
, struct file
*file
)
82 struct usb_interface
*interface
;
86 subminor
= iminor(inode
);
88 interface
= usb_find_interface(&skel_driver
, subminor
);
90 err ("%s - error, can't find device for minor %d",
91 __FUNCTION__
, subminor
);
96 dev
= usb_get_intfdata(interface
);
102 /* increment our usage count for the device */
103 kref_get(&dev
->kref
);
105 /* lock the device to allow correctly handling errors
107 mutex_lock(&dev
->io_mutex
);
109 if (!dev
->open_count
++) {
110 retval
= usb_autopm_get_interface(interface
);
113 mutex_unlock(&dev
->io_mutex
);
114 kref_put(&dev
->kref
, skel_delete
);
117 } /* else { //uncomment this block if you want exclusive open
120 mutex_unlock(&dev->io_mutex);
121 kref_put(&dev->kref, skel_delete);
124 /* prevent the device from being autosuspended */
126 /* save our object in the file's private structure */
127 file
->private_data
= dev
;
133 static int skel_release(struct inode
*inode
, struct file
*file
)
135 struct usb_skel
*dev
;
137 dev
= (struct usb_skel
*)file
->private_data
;
141 /* allow the device to be autosuspended */
142 mutex_lock(&dev
->io_mutex
);
143 if (!--dev
->open_count
&& dev
->interface
)
144 usb_autopm_put_interface(dev
->interface
);
145 mutex_unlock(&dev
->io_mutex
);
147 /* decrement the count on our device */
148 kref_put(&dev
->kref
, skel_delete
);
152 static int skel_flush(struct file
*file
, fl_owner_t id
)
154 struct usb_skel
*dev
;
157 dev
= (struct usb_skel
*)file
->private_data
;
161 /* wait for io to stop */
162 mutex_lock(&dev
->io_mutex
);
165 /* read out errors, leave subsequent opens a clean slate */
166 spin_lock_irq(&dev
->err_lock
);
167 res
= dev
->errors
? (dev
->errors
== -EPIPE
? -EPIPE
: -EIO
) : 0;
169 spin_unlock_irq(&dev
->err_lock
);
171 mutex_unlock(&dev
->io_mutex
);
176 static ssize_t
skel_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
178 struct usb_skel
*dev
;
182 dev
= (struct usb_skel
*)file
->private_data
;
184 mutex_lock(&dev
->io_mutex
);
185 if (!dev
->interface
) { /* disconnect() was called */
190 /* do a blocking bulk read to get data from the device */
191 retval
= usb_bulk_msg(dev
->udev
,
192 usb_rcvbulkpipe(dev
->udev
, dev
->bulk_in_endpointAddr
),
194 min(dev
->bulk_in_size
, count
),
197 /* if the read was successful, copy the data to userspace */
199 if (copy_to_user(buffer
, dev
->bulk_in_buffer
, bytes_read
))
206 mutex_unlock(&dev
->io_mutex
);
210 static void skel_write_bulk_callback(struct urb
*urb
)
212 struct usb_skel
*dev
;
214 dev
= (struct usb_skel
*)urb
->context
;
216 /* sync/async unlink faults aren't errors */
218 if(!(urb
->status
== -ENOENT
||
219 urb
->status
== -ECONNRESET
||
220 urb
->status
== -ESHUTDOWN
))
221 err("%s - nonzero write bulk status received: %d",
222 __FUNCTION__
, urb
->status
);
224 spin_lock(&dev
->err_lock
);
225 dev
->errors
= urb
->status
;
226 spin_unlock(&dev
->err_lock
);
229 /* free up our allocated buffer */
230 usb_buffer_free(urb
->dev
, urb
->transfer_buffer_length
,
231 urb
->transfer_buffer
, urb
->transfer_dma
);
235 static ssize_t
skel_write(struct file
*file
, const char *user_buffer
, size_t count
, loff_t
*ppos
)
237 struct usb_skel
*dev
;
239 struct urb
*urb
= NULL
;
241 size_t writesize
= min(count
, (size_t)MAX_TRANSFER
);
243 dev
= (struct usb_skel
*)file
->private_data
;
245 /* verify that we actually have some data to write */
249 /* limit the number of URBs in flight to stop a user from using up all RAM */
250 if (down_interruptible(&dev
->limit_sem
)) {
251 retval
= -ERESTARTSYS
;
255 spin_lock_irq(&dev
->err_lock
);
256 if ((retval
= dev
->errors
) < 0) {
257 /* any error is reported once */
259 /* to preserve notifications about reset */
260 retval
= (retval
== -EPIPE
) ? retval
: -EIO
;
262 spin_unlock_irq(&dev
->err_lock
);
266 /* create a urb, and a buffer for it, and copy the data to the urb */
267 urb
= usb_alloc_urb(0, GFP_KERNEL
);
273 buf
= usb_buffer_alloc(dev
->udev
, writesize
, GFP_KERNEL
, &urb
->transfer_dma
);
279 if (copy_from_user(buf
, user_buffer
, writesize
)) {
284 /* this lock makes sure we don't submit URBs to gone devices */
285 mutex_lock(&dev
->io_mutex
);
286 if (!dev
->interface
) { /* disconnect() was called */
287 mutex_unlock(&dev
->io_mutex
);
292 /* initialize the urb properly */
293 usb_fill_bulk_urb(urb
, dev
->udev
,
294 usb_sndbulkpipe(dev
->udev
, dev
->bulk_out_endpointAddr
),
295 buf
, writesize
, skel_write_bulk_callback
, dev
);
296 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
297 usb_anchor_urb(urb
, &dev
->submitted
);
299 /* send the data out the bulk port */
300 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
301 mutex_unlock(&dev
->io_mutex
);
303 err("%s - failed submitting write urb, error %d", __FUNCTION__
, retval
);
307 /* release our reference to this urb, the USB core will eventually free it entirely */
314 usb_unanchor_urb(urb
);
317 usb_buffer_free(dev
->udev
, writesize
, buf
, urb
->transfer_dma
);
326 static const struct file_operations skel_fops
= {
327 .owner
= THIS_MODULE
,
331 .release
= skel_release
,
336 * usb class driver info in order to get a minor number from the usb core,
337 * and to have the device registered with the driver core
339 static struct usb_class_driver skel_class
= {
342 .minor_base
= USB_SKEL_MINOR_BASE
,
345 static int skel_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
347 struct usb_skel
*dev
;
348 struct usb_host_interface
*iface_desc
;
349 struct usb_endpoint_descriptor
*endpoint
;
352 int retval
= -ENOMEM
;
354 /* allocate memory for our device state and initialize it */
355 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
357 err("Out of memory");
360 kref_init(&dev
->kref
);
361 sema_init(&dev
->limit_sem
, WRITES_IN_FLIGHT
);
362 mutex_init(&dev
->io_mutex
);
363 spin_lock_init(&dev
->err_lock
);
364 init_usb_anchor(&dev
->submitted
);
366 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
367 dev
->interface
= interface
;
369 /* set up the endpoint information */
370 /* use only the first bulk-in and bulk-out endpoints */
371 iface_desc
= interface
->cur_altsetting
;
372 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
373 endpoint
= &iface_desc
->endpoint
[i
].desc
;
375 if (!dev
->bulk_in_endpointAddr
&&
376 usb_endpoint_is_bulk_in(endpoint
)) {
377 /* we found a bulk in endpoint */
378 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
379 dev
->bulk_in_size
= buffer_size
;
380 dev
->bulk_in_endpointAddr
= endpoint
->bEndpointAddress
;
381 dev
->bulk_in_buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
382 if (!dev
->bulk_in_buffer
) {
383 err("Could not allocate bulk_in_buffer");
388 if (!dev
->bulk_out_endpointAddr
&&
389 usb_endpoint_is_bulk_out(endpoint
)) {
390 /* we found a bulk out endpoint */
391 dev
->bulk_out_endpointAddr
= endpoint
->bEndpointAddress
;
394 if (!(dev
->bulk_in_endpointAddr
&& dev
->bulk_out_endpointAddr
)) {
395 err("Could not find both bulk-in and bulk-out endpoints");
399 /* save our data pointer in this interface device */
400 usb_set_intfdata(interface
, dev
);
402 /* we can register the device now, as it is ready */
403 retval
= usb_register_dev(interface
, &skel_class
);
405 /* something prevented us from registering this driver */
406 err("Not able to get a minor for this device.");
407 usb_set_intfdata(interface
, NULL
);
411 /* let the user know what node this device is now attached to */
412 info("USB Skeleton device now attached to USBSkel-%d", interface
->minor
);
417 /* this frees allocated memory */
418 kref_put(&dev
->kref
, skel_delete
);
422 static void skel_disconnect(struct usb_interface
*interface
)
424 struct usb_skel
*dev
;
425 int minor
= interface
->minor
;
427 dev
= usb_get_intfdata(interface
);
428 usb_set_intfdata(interface
, NULL
);
430 /* give back our minor */
431 usb_deregister_dev(interface
, &skel_class
);
433 /* prevent more I/O from starting */
434 mutex_lock(&dev
->io_mutex
);
435 dev
->interface
= NULL
;
436 mutex_unlock(&dev
->io_mutex
);
438 usb_kill_anchored_urbs(&dev
->submitted
);
440 /* decrement our usage count */
441 kref_put(&dev
->kref
, skel_delete
);
443 info("USB Skeleton #%d now disconnected", minor
);
446 static void skel_draw_down(struct usb_skel
*dev
)
450 time
= usb_wait_anchor_empty_timeout(&dev
->submitted
, 1000);
452 usb_kill_anchored_urbs(&dev
->submitted
);
455 static int skel_suspend(struct usb_interface
*intf
, pm_message_t message
)
457 struct usb_skel
*dev
= usb_get_intfdata(intf
);
465 static int skel_resume (struct usb_interface
*intf
)
470 static int skel_pre_reset(struct usb_interface
*intf
)
472 struct usb_skel
*dev
= usb_get_intfdata(intf
);
474 mutex_lock(&dev
->io_mutex
);
480 static int skel_post_reset(struct usb_interface
*intf
)
482 struct usb_skel
*dev
= usb_get_intfdata(intf
);
484 /* we are sure no URBs are active - no locking needed */
485 dev
->errors
= -EPIPE
;
486 mutex_unlock(&dev
->io_mutex
);
491 static struct usb_driver skel_driver
= {
494 .disconnect
= skel_disconnect
,
495 .suspend
= skel_suspend
,
496 .resume
= skel_resume
,
497 .pre_reset
= skel_pre_reset
,
498 .post_reset
= skel_post_reset
,
499 .id_table
= skel_table
,
500 .supports_autosuspend
= 1,
503 static int __init
usb_skel_init(void)
507 /* register this driver with the USB subsystem */
508 result
= usb_register(&skel_driver
);
510 err("usb_register failed. Error number %d", result
);
515 static void __exit
usb_skel_exit(void)
517 /* deregister this driver with the USB subsystem */
518 usb_deregister(&skel_driver
);
521 module_init(usb_skel_init
);
522 module_exit(usb_skel_exit
);
524 MODULE_LICENSE("GPL");