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 <linux/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 static DEFINE_MUTEX(skel_mutex
);
32 /* table of devices that work with this driver */
33 static const struct usb_device_id skel_table
[] = {
34 { USB_DEVICE(USB_SKEL_VENDOR_ID
, USB_SKEL_PRODUCT_ID
) },
35 { } /* Terminating entry */
37 MODULE_DEVICE_TABLE(usb
, skel_table
);
40 /* Get a minor range for your devices from the usb maintainer */
41 #define USB_SKEL_MINOR_BASE 192
43 /* our private defines. if this grows any larger, use your own .h file */
44 #define MAX_TRANSFER (PAGE_SIZE - 512)
45 /* MAX_TRANSFER is chosen so that the VM is not stressed by
46 allocations > PAGE_SIZE and the number of packets in a page
47 is an integer 512 is the largest possible packet on EHCI */
48 #define WRITES_IN_FLIGHT 8
49 /* arbitrarily chosen */
51 /* Structure to hold all of our device specific stuff */
53 struct usb_device
*udev
; /* the usb device for this device */
54 struct usb_interface
*interface
; /* the interface for this device */
55 struct semaphore limit_sem
; /* limiting the number of writes in progress */
56 struct usb_anchor submitted
; /* in case we need to retract our submissions */
57 struct urb
*bulk_in_urb
; /* the urb to read data with */
58 unsigned char *bulk_in_buffer
; /* the buffer to receive data */
59 size_t bulk_in_size
; /* the size of the receive buffer */
60 size_t bulk_in_filled
; /* number of bytes in the buffer */
61 size_t bulk_in_copied
; /* already copied to user space */
62 __u8 bulk_in_endpointAddr
; /* the address of the bulk in endpoint */
63 __u8 bulk_out_endpointAddr
; /* the address of the bulk out endpoint */
64 int errors
; /* the last request tanked */
65 bool ongoing_read
; /* a read is going on */
66 bool processed_urb
; /* indicates we haven't processed the urb */
67 spinlock_t err_lock
; /* lock for errors */
69 struct mutex io_mutex
; /* synchronize I/O with disconnect */
70 struct completion bulk_in_completion
; /* to wait for an ongoing read */
72 #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
74 static struct usb_driver skel_driver
;
75 static void skel_draw_down(struct usb_skel
*dev
);
77 static void skel_delete(struct kref
*kref
)
79 struct usb_skel
*dev
= to_skel_dev(kref
);
81 usb_free_urb(dev
->bulk_in_urb
);
82 usb_put_dev(dev
->udev
);
83 kfree(dev
->bulk_in_buffer
);
87 static int skel_open(struct inode
*inode
, struct file
*file
)
90 struct usb_interface
*interface
;
94 subminor
= iminor(inode
);
96 interface
= usb_find_interface(&skel_driver
, subminor
);
98 err("%s - error, can't find device for minor %d",
104 mutex_lock(&skel_mutex
);
105 dev
= usb_get_intfdata(interface
);
107 mutex_unlock(&skel_mutex
);
112 /* increment our usage count for the device */
113 kref_get(&dev
->kref
);
114 mutex_unlock(&skel_mutex
);
116 /* lock the device to allow correctly handling errors
118 mutex_lock(&dev
->io_mutex
);
119 if (!dev
->interface
) {
124 retval
= usb_autopm_get_interface(interface
);
128 /* save our object in the file's private structure */
129 file
->private_data
= dev
;
132 mutex_unlock(&dev
->io_mutex
);
134 kref_put(&dev
->kref
, skel_delete
);
140 static int skel_release(struct inode
*inode
, struct file
*file
)
142 struct usb_skel
*dev
;
144 dev
= file
->private_data
;
148 /* allow the device to be autosuspended */
149 mutex_lock(&dev
->io_mutex
);
151 usb_autopm_put_interface(dev
->interface
);
152 mutex_unlock(&dev
->io_mutex
);
154 /* decrement the count on our device */
155 kref_put(&dev
->kref
, skel_delete
);
159 static int skel_flush(struct file
*file
, fl_owner_t id
)
161 struct usb_skel
*dev
;
164 dev
= file
->private_data
;
168 /* wait for io to stop */
169 mutex_lock(&dev
->io_mutex
);
172 /* read out errors, leave subsequent opens a clean slate */
173 spin_lock_irq(&dev
->err_lock
);
174 res
= dev
->errors
? (dev
->errors
== -EPIPE
? -EPIPE
: -EIO
) : 0;
176 spin_unlock_irq(&dev
->err_lock
);
178 mutex_unlock(&dev
->io_mutex
);
183 static void skel_read_bulk_callback(struct urb
*urb
)
185 struct usb_skel
*dev
;
189 spin_lock(&dev
->err_lock
);
190 /* sync/async unlink faults aren't errors */
192 if (!(urb
->status
== -ENOENT
||
193 urb
->status
== -ECONNRESET
||
194 urb
->status
== -ESHUTDOWN
))
195 err("%s - nonzero write bulk status received: %d",
196 __func__
, urb
->status
);
198 dev
->errors
= urb
->status
;
200 dev
->bulk_in_filled
= urb
->actual_length
;
202 dev
->ongoing_read
= 0;
203 spin_unlock(&dev
->err_lock
);
205 complete(&dev
->bulk_in_completion
);
208 static int skel_do_read_io(struct usb_skel
*dev
, size_t count
)
213 usb_fill_bulk_urb(dev
->bulk_in_urb
,
215 usb_rcvbulkpipe(dev
->udev
,
216 dev
->bulk_in_endpointAddr
),
218 min(dev
->bulk_in_size
, count
),
219 skel_read_bulk_callback
,
221 /* tell everybody to leave the URB alone */
222 spin_lock_irq(&dev
->err_lock
);
223 dev
->ongoing_read
= 1;
224 spin_unlock_irq(&dev
->err_lock
);
227 rv
= usb_submit_urb(dev
->bulk_in_urb
, GFP_KERNEL
);
229 err("%s - failed submitting read urb, error %d",
231 dev
->bulk_in_filled
= 0;
232 rv
= (rv
== -ENOMEM
) ? rv
: -EIO
;
233 spin_lock_irq(&dev
->err_lock
);
234 dev
->ongoing_read
= 0;
235 spin_unlock_irq(&dev
->err_lock
);
241 static ssize_t
skel_read(struct file
*file
, char *buffer
, size_t count
,
244 struct usb_skel
*dev
;
248 dev
= file
->private_data
;
250 /* if we cannot read at all, return EOF */
251 if (!dev
->bulk_in_urb
|| !count
)
254 /* no concurrent readers */
255 rv
= mutex_lock_interruptible(&dev
->io_mutex
);
259 if (!dev
->interface
) { /* disconnect() was called */
264 /* if IO is under way, we must not touch things */
266 spin_lock_irq(&dev
->err_lock
);
267 ongoing_io
= dev
->ongoing_read
;
268 spin_unlock_irq(&dev
->err_lock
);
271 /* nonblocking IO shall not wait */
272 if (file
->f_flags
& O_NONBLOCK
) {
277 * IO may take forever
278 * hence wait in an interruptible state
280 rv
= wait_for_completion_interruptible(&dev
->bulk_in_completion
);
284 * by waiting we also semiprocessed the urb
287 dev
->bulk_in_copied
= 0;
288 dev
->processed_urb
= 1;
291 if (!dev
->processed_urb
) {
293 * the URB hasn't been processed
296 wait_for_completion(&dev
->bulk_in_completion
);
297 dev
->bulk_in_copied
= 0;
298 dev
->processed_urb
= 1;
301 /* errors must be reported */
304 /* any error is reported once */
306 /* to preserve notifications about reset */
307 rv
= (rv
== -EPIPE
) ? rv
: -EIO
;
308 /* no data to deliver */
309 dev
->bulk_in_filled
= 0;
315 * if the buffer is filled we may satisfy the read
316 * else we need to start IO
319 if (dev
->bulk_in_filled
) {
320 /* we had read data */
321 size_t available
= dev
->bulk_in_filled
- dev
->bulk_in_copied
;
322 size_t chunk
= min(available
, count
);
326 * all data has been used
327 * actual IO needs to be done
329 rv
= skel_do_read_io(dev
, count
);
337 * chunk tells us how much shall be copied
340 if (copy_to_user(buffer
,
341 dev
->bulk_in_buffer
+ dev
->bulk_in_copied
,
347 dev
->bulk_in_copied
+= chunk
;
350 * if we are asked for more than we have,
351 * we start IO but don't wait
353 if (available
< count
)
354 skel_do_read_io(dev
, count
- chunk
);
356 /* no data in the buffer */
357 rv
= skel_do_read_io(dev
, count
);
360 else if (!(file
->f_flags
& O_NONBLOCK
))
365 mutex_unlock(&dev
->io_mutex
);
369 static void skel_write_bulk_callback(struct urb
*urb
)
371 struct usb_skel
*dev
;
375 /* sync/async unlink faults aren't errors */
377 if (!(urb
->status
== -ENOENT
||
378 urb
->status
== -ECONNRESET
||
379 urb
->status
== -ESHUTDOWN
))
380 err("%s - nonzero write bulk status received: %d",
381 __func__
, urb
->status
);
383 spin_lock(&dev
->err_lock
);
384 dev
->errors
= urb
->status
;
385 spin_unlock(&dev
->err_lock
);
388 /* free up our allocated buffer */
389 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
390 urb
->transfer_buffer
, urb
->transfer_dma
);
394 static ssize_t
skel_write(struct file
*file
, const char *user_buffer
,
395 size_t count
, loff_t
*ppos
)
397 struct usb_skel
*dev
;
399 struct urb
*urb
= NULL
;
401 size_t writesize
= min(count
, (size_t)MAX_TRANSFER
);
403 dev
= file
->private_data
;
405 /* verify that we actually have some data to write */
410 * limit the number of URBs in flight to stop a user from using up all
413 if (!(file
->f_flags
& O_NONBLOCK
)) {
414 if (down_interruptible(&dev
->limit_sem
)) {
415 retval
= -ERESTARTSYS
;
419 if (down_trylock(&dev
->limit_sem
)) {
425 spin_lock_irq(&dev
->err_lock
);
426 retval
= dev
->errors
;
428 /* any error is reported once */
430 /* to preserve notifications about reset */
431 retval
= (retval
== -EPIPE
) ? retval
: -EIO
;
433 spin_unlock_irq(&dev
->err_lock
);
437 /* create a urb, and a buffer for it, and copy the data to the urb */
438 urb
= usb_alloc_urb(0, GFP_KERNEL
);
444 buf
= usb_alloc_coherent(dev
->udev
, writesize
, GFP_KERNEL
,
451 if (copy_from_user(buf
, user_buffer
, writesize
)) {
456 /* this lock makes sure we don't submit URBs to gone devices */
457 mutex_lock(&dev
->io_mutex
);
458 if (!dev
->interface
) { /* disconnect() was called */
459 mutex_unlock(&dev
->io_mutex
);
464 /* initialize the urb properly */
465 usb_fill_bulk_urb(urb
, dev
->udev
,
466 usb_sndbulkpipe(dev
->udev
, dev
->bulk_out_endpointAddr
),
467 buf
, writesize
, skel_write_bulk_callback
, dev
);
468 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
469 usb_anchor_urb(urb
, &dev
->submitted
);
471 /* send the data out the bulk port */
472 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
473 mutex_unlock(&dev
->io_mutex
);
475 err("%s - failed submitting write urb, error %d", __func__
,
481 * release our reference to this urb, the USB core will eventually free
490 usb_unanchor_urb(urb
);
493 usb_free_coherent(dev
->udev
, writesize
, buf
, urb
->transfer_dma
);
502 static const struct file_operations skel_fops
= {
503 .owner
= THIS_MODULE
,
507 .release
= skel_release
,
509 .llseek
= noop_llseek
,
513 * usb class driver info in order to get a minor number from the usb core,
514 * and to have the device registered with the driver core
516 static struct usb_class_driver skel_class
= {
519 .minor_base
= USB_SKEL_MINOR_BASE
,
522 static int skel_probe(struct usb_interface
*interface
,
523 const struct usb_device_id
*id
)
525 struct usb_skel
*dev
;
526 struct usb_host_interface
*iface_desc
;
527 struct usb_endpoint_descriptor
*endpoint
;
530 int retval
= -ENOMEM
;
532 /* allocate memory for our device state and initialize it */
533 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
535 err("Out of memory");
538 kref_init(&dev
->kref
);
539 sema_init(&dev
->limit_sem
, WRITES_IN_FLIGHT
);
540 mutex_init(&dev
->io_mutex
);
541 spin_lock_init(&dev
->err_lock
);
542 init_usb_anchor(&dev
->submitted
);
543 init_completion(&dev
->bulk_in_completion
);
545 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
546 dev
->interface
= interface
;
548 /* set up the endpoint information */
549 /* use only the first bulk-in and bulk-out endpoints */
550 iface_desc
= interface
->cur_altsetting
;
551 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
552 endpoint
= &iface_desc
->endpoint
[i
].desc
;
554 if (!dev
->bulk_in_endpointAddr
&&
555 usb_endpoint_is_bulk_in(endpoint
)) {
556 /* we found a bulk in endpoint */
557 buffer_size
= usb_endpoint_maxp(endpoint
);
558 dev
->bulk_in_size
= buffer_size
;
559 dev
->bulk_in_endpointAddr
= endpoint
->bEndpointAddress
;
560 dev
->bulk_in_buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
561 if (!dev
->bulk_in_buffer
) {
562 err("Could not allocate bulk_in_buffer");
565 dev
->bulk_in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
566 if (!dev
->bulk_in_urb
) {
567 err("Could not allocate bulk_in_urb");
572 if (!dev
->bulk_out_endpointAddr
&&
573 usb_endpoint_is_bulk_out(endpoint
)) {
574 /* we found a bulk out endpoint */
575 dev
->bulk_out_endpointAddr
= endpoint
->bEndpointAddress
;
578 if (!(dev
->bulk_in_endpointAddr
&& dev
->bulk_out_endpointAddr
)) {
579 err("Could not find both bulk-in and bulk-out endpoints");
583 /* save our data pointer in this interface device */
584 usb_set_intfdata(interface
, dev
);
586 /* we can register the device now, as it is ready */
587 retval
= usb_register_dev(interface
, &skel_class
);
589 /* something prevented us from registering this driver */
590 err("Not able to get a minor for this device.");
591 usb_set_intfdata(interface
, NULL
);
595 /* let the user know what node this device is now attached to */
596 dev_info(&interface
->dev
,
597 "USB Skeleton device now attached to USBSkel-%d",
603 /* this frees allocated memory */
604 kref_put(&dev
->kref
, skel_delete
);
608 static void skel_disconnect(struct usb_interface
*interface
)
610 struct usb_skel
*dev
;
611 int minor
= interface
->minor
;
613 dev
= usb_get_intfdata(interface
);
615 /* give back our minor */
616 usb_deregister_dev(interface
, &skel_class
);
618 /* prevent more I/O from starting */
619 mutex_lock(&dev
->io_mutex
);
620 dev
->interface
= NULL
;
621 mutex_unlock(&dev
->io_mutex
);
623 usb_kill_anchored_urbs(&dev
->submitted
);
625 mutex_lock(&skel_mutex
);
626 usb_set_intfdata(interface
, NULL
);
628 /* decrement our usage count */
629 kref_put(&dev
->kref
, skel_delete
);
630 mutex_unlock(&skel_mutex
);
632 dev_info(&interface
->dev
, "USB Skeleton #%d now disconnected", minor
);
635 static void skel_draw_down(struct usb_skel
*dev
)
639 time
= usb_wait_anchor_empty_timeout(&dev
->submitted
, 1000);
641 usb_kill_anchored_urbs(&dev
->submitted
);
642 usb_kill_urb(dev
->bulk_in_urb
);
645 static int skel_suspend(struct usb_interface
*intf
, pm_message_t message
)
647 struct usb_skel
*dev
= usb_get_intfdata(intf
);
655 static int skel_resume(struct usb_interface
*intf
)
660 static int skel_pre_reset(struct usb_interface
*intf
)
662 struct usb_skel
*dev
= usb_get_intfdata(intf
);
664 mutex_lock(&dev
->io_mutex
);
670 static int skel_post_reset(struct usb_interface
*intf
)
672 struct usb_skel
*dev
= usb_get_intfdata(intf
);
674 /* we are sure no URBs are active - no locking needed */
675 dev
->errors
= -EPIPE
;
676 mutex_unlock(&dev
->io_mutex
);
681 static struct usb_driver skel_driver
= {
684 .disconnect
= skel_disconnect
,
685 .suspend
= skel_suspend
,
686 .resume
= skel_resume
,
687 .pre_reset
= skel_pre_reset
,
688 .post_reset
= skel_post_reset
,
689 .id_table
= skel_table
,
690 .supports_autosuspend
= 1,
693 module_usb_driver(skel_driver
);
695 MODULE_LICENSE("GPL");