2 * USB Skeleton driver - 2.0
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 easy to read and use, as no locks are now
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/kref.h>
23 #include <asm/uaccess.h>
24 #include <linux/usb.h>
27 /* Define these values to match your devices */
28 #define USB_SKEL_VENDOR_ID 0xfff0
29 #define USB_SKEL_PRODUCT_ID 0xfff0
31 /* table of devices that work with this driver */
32 static struct usb_device_id skel_table [] = {
33 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
34 { } /* Terminating entry */
36 MODULE_DEVICE_TABLE (usb, skel_table);
39 /* Get a minor range for your devices from the usb maintainer */
40 #define USB_SKEL_MINOR_BASE 192
42 /* Structure to hold all of our device specific stuff */
44 struct usb_device * udev; /* the usb device for this device */
45 struct usb_interface * interface; /* the interface for this device */
46 unsigned char * bulk_in_buffer; /* the buffer to receive data */
47 size_t bulk_in_size; /* the size of the receive buffer */
48 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
49 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
52 #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
54 static struct usb_driver skel_driver;
56 static void skel_delete(struct kref *kref)
58 struct usb_skel *dev = to_skel_dev(kref);
60 usb_put_dev(dev->udev);
61 kfree (dev->bulk_in_buffer);
65 static int skel_open(struct inode *inode, struct file *file)
68 struct usb_interface *interface;
72 subminor = iminor(inode);
74 interface = usb_find_interface(&skel_driver, subminor);
76 err ("%s - error, can't find device for minor %d",
77 __FUNCTION__, subminor);
82 dev = usb_get_intfdata(interface);
88 /* increment our usage count for the device */
91 /* save our object in the file's private structure */
92 file->private_data = dev;
98 static int skel_release(struct inode *inode, struct file *file)
100 struct usb_skel *dev;
102 dev = (struct usb_skel *)file->private_data;
106 /* decrement the count on our device */
107 kref_put(&dev->kref, skel_delete);
111 static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
113 struct usb_skel *dev;
117 dev = (struct usb_skel *)file->private_data;
119 /* do a blocking bulk read to get data from the device */
120 retval = usb_bulk_msg(dev->udev,
121 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
123 min(dev->bulk_in_size, count),
126 /* if the read was successful, copy the data to userspace */
128 if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
137 static void skel_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
139 struct usb_skel *dev;
141 dev = (struct usb_skel *)urb->context;
143 /* sync/async unlink faults aren't errors */
145 !(urb->status == -ENOENT ||
146 urb->status == -ECONNRESET ||
147 urb->status == -ESHUTDOWN)) {
148 dbg("%s - nonzero write bulk status received: %d",
149 __FUNCTION__, urb->status);
152 /* free up our allocated buffer */
153 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
154 urb->transfer_buffer, urb->transfer_dma);
157 static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
159 struct usb_skel *dev;
161 struct urb *urb = NULL;
164 dev = (struct usb_skel *)file->private_data;
166 /* verify that we actually have some data to write */
170 /* create a urb, and a buffer for it, and copy the data to the urb */
171 urb = usb_alloc_urb(0, GFP_KERNEL);
177 buf = usb_buffer_alloc(dev->udev, count, GFP_KERNEL, &urb->transfer_dma);
183 if (copy_from_user(buf, user_buffer, count)) {
188 /* initialize the urb properly */
189 usb_fill_bulk_urb(urb, dev->udev,
190 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
191 buf, count, skel_write_bulk_callback, dev);
192 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
194 /* send the data out the bulk port */
195 retval = usb_submit_urb(urb, GFP_KERNEL);
197 err("%s - failed submitting write urb, error %d", __FUNCTION__, retval);
201 /* release our reference to this urb, the USB core will eventually free it entirely */
208 usb_buffer_free(dev->udev, count, buf, urb->transfer_dma);
213 static struct file_operations skel_fops = {
214 .owner = THIS_MODULE,
218 .release = skel_release,
222 * usb class driver info in order to get a minor number from the usb core,
223 * and to have the device registered with devfs and the driver core
225 static struct usb_class_driver skel_class = {
226 .name = "usb/skel%d",
228 .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
229 .minor_base = USB_SKEL_MINOR_BASE,
232 static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id)
234 struct usb_skel *dev = NULL;
235 struct usb_host_interface *iface_desc;
236 struct usb_endpoint_descriptor *endpoint;
239 int retval = -ENOMEM;
241 /* allocate memory for our device state and initialize it */
242 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
244 err("Out of memory");
247 memset(dev, 0x00, sizeof(*dev));
248 kref_init(&dev->kref);
250 dev->udev = usb_get_dev(interface_to_usbdev(interface));
251 dev->interface = interface;
253 /* set up the endpoint information */
254 /* use only the first bulk-in and bulk-out endpoints */
255 iface_desc = interface->cur_altsetting;
256 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
257 endpoint = &iface_desc->endpoint[i].desc;
259 if (!dev->bulk_in_endpointAddr &&
260 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
262 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
263 == USB_ENDPOINT_XFER_BULK)) {
264 /* we found a bulk in endpoint */
265 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
266 dev->bulk_in_size = buffer_size;
267 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
268 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
269 if (!dev->bulk_in_buffer) {
270 err("Could not allocate bulk_in_buffer");
275 if (!dev->bulk_out_endpointAddr &&
276 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
278 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
279 == USB_ENDPOINT_XFER_BULK)) {
280 /* we found a bulk out endpoint */
281 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
284 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
285 err("Could not find both bulk-in and bulk-out endpoints");
289 /* save our data pointer in this interface device */
290 usb_set_intfdata(interface, dev);
292 /* we can register the device now, as it is ready */
293 retval = usb_register_dev(interface, &skel_class);
295 /* something prevented us from registering this driver */
296 err("Not able to get a minor for this device.");
297 usb_set_intfdata(interface, NULL);
301 /* let the user know what node this device is now attached to */
302 info("USB Skeleton device now attached to USBSkel-%d", interface->minor);
307 kref_put(&dev->kref, skel_delete);
311 static void skel_disconnect(struct usb_interface *interface)
313 struct usb_skel *dev;
314 int minor = interface->minor;
316 /* prevent skel_open() from racing skel_disconnect() */
319 dev = usb_get_intfdata(interface);
320 usb_set_intfdata(interface, NULL);
322 /* give back our minor */
323 usb_deregister_dev(interface, &skel_class);
327 /* decrement our usage count */
328 kref_put(&dev->kref, skel_delete);
330 info("USB Skeleton #%d now disconnected", minor);
333 static struct usb_driver skel_driver = {
334 .owner = THIS_MODULE,
337 .disconnect = skel_disconnect,
338 .id_table = skel_table,
341 static int __init usb_skel_init(void)
345 /* register this driver with the USB subsystem */
346 result = usb_register(&skel_driver);
348 err("usb_register failed. Error number %d", result);
353 static void __exit usb_skel_exit(void)
355 /* deregister this driver with the USB subsystem */
356 usb_deregister(&skel_driver);
359 module_init (usb_skel_init);
360 module_exit (usb_skel_exit);
362 MODULE_LICENSE("GPL");