blob: 33f0e81c58d322491e5a878bc055d17102640127 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alan Stern121e2872006-07-01 22:06:36 -04002 * USB Skeleton driver - 2.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2001-2004 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, version 2.
9 *
10 * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
Alan Stern121e2872006-07-01 22:06:36 -040011 * but has been rewritten to be easier to read and use.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Alan Stern121e2872006-07-01 22:06:36 -040023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25
26/* Define these values to match your devices */
27#define USB_SKEL_VENDOR_ID 0xfff0
28#define USB_SKEL_PRODUCT_ID 0xfff0
29
30/* table of devices that work with this driver */
31static struct usb_device_id skel_table [] = {
32 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
33 { } /* Terminating entry */
34};
35MODULE_DEVICE_TABLE (usb, skel_table);
36
37
38/* Get a minor range for your devices from the usb maintainer */
39#define USB_SKEL_MINOR_BASE 192
40
Oliver Neukumff906512005-12-21 19:27:29 +010041/* our private defines. if this grows any larger, use your own .h file */
42#define MAX_TRANSFER ( PAGE_SIZE - 512 )
43#define WRITES_IN_FLIGHT 8
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* Structure to hold all of our device specific stuff */
46struct usb_skel {
47 struct usb_device * udev; /* the usb device for this device */
48 struct usb_interface * interface; /* the interface for this device */
Oliver Neukumff906512005-12-21 19:27:29 +010049 struct semaphore limit_sem; /* limiting the number of writes in progress */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned char * bulk_in_buffer; /* the buffer to receive data */
51 size_t bulk_in_size; /* the size of the receive buffer */
52 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
53 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
54 struct kref kref;
Alan Stern121e2872006-07-01 22:06:36 -040055 struct mutex io_mutex; /* synchronize I/O with disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57#define to_skel_dev(d) container_of(d, struct usb_skel, kref)
58
59static struct usb_driver skel_driver;
60
61static void skel_delete(struct kref *kref)
62{
63 struct usb_skel *dev = to_skel_dev(kref);
64
65 usb_put_dev(dev->udev);
66 kfree (dev->bulk_in_buffer);
67 kfree (dev);
68}
69
70static int skel_open(struct inode *inode, struct file *file)
71{
72 struct usb_skel *dev;
73 struct usb_interface *interface;
74 int subminor;
75 int retval = 0;
76
77 subminor = iminor(inode);
78
79 interface = usb_find_interface(&skel_driver, subminor);
80 if (!interface) {
81 err ("%s - error, can't find device for minor %d",
82 __FUNCTION__, subminor);
83 retval = -ENODEV;
84 goto exit;
85 }
86
87 dev = usb_get_intfdata(interface);
88 if (!dev) {
89 retval = -ENODEV;
90 goto exit;
91 }
92
93 /* increment our usage count for the device */
94 kref_get(&dev->kref);
95
96 /* save our object in the file's private structure */
97 file->private_data = dev;
98
99exit:
100 return retval;
101}
102
103static int skel_release(struct inode *inode, struct file *file)
104{
105 struct usb_skel *dev;
106
107 dev = (struct usb_skel *)file->private_data;
108 if (dev == NULL)
109 return -ENODEV;
110
111 /* decrement the count on our device */
112 kref_put(&dev->kref, skel_delete);
113 return 0;
114}
115
116static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
117{
118 struct usb_skel *dev;
119 int retval = 0;
120 int bytes_read;
121
122 dev = (struct usb_skel *)file->private_data;
Alan Stern121e2872006-07-01 22:06:36 -0400123
124 mutex_lock(&dev->io_mutex);
125 if (!dev->interface) { /* disconnect() was called */
126 retval = -ENODEV;
127 goto exit;
128 }
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* do a blocking bulk read to get data from the device */
131 retval = usb_bulk_msg(dev->udev,
132 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
133 dev->bulk_in_buffer,
134 min(dev->bulk_in_size, count),
135 &bytes_read, 10000);
136
137 /* if the read was successful, copy the data to userspace */
138 if (!retval) {
139 if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
140 retval = -EFAULT;
141 else
142 retval = bytes_read;
143 }
144
Alan Stern121e2872006-07-01 22:06:36 -0400145exit:
146 mutex_unlock(&dev->io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return retval;
148}
149
150static void skel_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
151{
152 struct usb_skel *dev;
153
154 dev = (struct usb_skel *)urb->context;
155
156 /* sync/async unlink faults aren't errors */
157 if (urb->status &&
158 !(urb->status == -ENOENT ||
159 urb->status == -ECONNRESET ||
160 urb->status == -ESHUTDOWN)) {
161 dbg("%s - nonzero write bulk status received: %d",
162 __FUNCTION__, urb->status);
163 }
164
165 /* free up our allocated buffer */
166 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
167 urb->transfer_buffer, urb->transfer_dma);
Oliver Neukumff906512005-12-21 19:27:29 +0100168 up(&dev->limit_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
172{
173 struct usb_skel *dev;
174 int retval = 0;
175 struct urb *urb = NULL;
176 char *buf = NULL;
Sam Bishopc8dd7702005-12-22 17:11:02 -0700177 size_t writesize = min(count, (size_t)MAX_TRANSFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 dev = (struct usb_skel *)file->private_data;
180
181 /* verify that we actually have some data to write */
182 if (count == 0)
183 goto exit;
184
Oliver Neukumff906512005-12-21 19:27:29 +0100185 /* limit the number of URBs in flight to stop a user from using up all RAM */
Sam Bishopc8dd7702005-12-22 17:11:02 -0700186 if (down_interruptible(&dev->limit_sem)) {
187 retval = -ERESTARTSYS;
188 goto exit;
189 }
Oliver Neukumff906512005-12-21 19:27:29 +0100190
Alan Stern121e2872006-07-01 22:06:36 -0400191 mutex_lock(&dev->io_mutex);
192 if (!dev->interface) { /* disconnect() was called */
193 retval = -ENODEV;
194 goto error;
195 }
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* create a urb, and a buffer for it, and copy the data to the urb */
198 urb = usb_alloc_urb(0, GFP_KERNEL);
199 if (!urb) {
200 retval = -ENOMEM;
201 goto error;
202 }
203
Oliver Neukumff906512005-12-21 19:27:29 +0100204 buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!buf) {
206 retval = -ENOMEM;
207 goto error;
208 }
209
Oliver Neukumff906512005-12-21 19:27:29 +0100210 if (copy_from_user(buf, user_buffer, writesize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 retval = -EFAULT;
212 goto error;
213 }
214
215 /* initialize the urb properly */
216 usb_fill_bulk_urb(urb, dev->udev,
217 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
Oliver Neukumff906512005-12-21 19:27:29 +0100218 buf, writesize, skel_write_bulk_callback, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
220
221 /* send the data out the bulk port */
222 retval = usb_submit_urb(urb, GFP_KERNEL);
223 if (retval) {
224 err("%s - failed submitting write urb, error %d", __FUNCTION__, retval);
225 goto error;
226 }
227
228 /* release our reference to this urb, the USB core will eventually free it entirely */
229 usb_free_urb(urb);
230
Alan Stern121e2872006-07-01 22:06:36 -0400231 mutex_unlock(&dev->io_mutex);
Oliver Neukumff906512005-12-21 19:27:29 +0100232 return writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234error:
Alan Stern121e2872006-07-01 22:06:36 -0400235 if (urb) {
236 usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma);
237 usb_free_urb(urb);
238 }
239 mutex_unlock(&dev->io_mutex);
Oliver Neukumff906512005-12-21 19:27:29 +0100240 up(&dev->limit_sem);
Alan Stern121e2872006-07-01 22:06:36 -0400241
242exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return retval;
244}
245
246static struct file_operations skel_fops = {
247 .owner = THIS_MODULE,
248 .read = skel_read,
249 .write = skel_write,
250 .open = skel_open,
251 .release = skel_release,
252};
253
254/*
255 * usb class driver info in order to get a minor number from the usb core,
Greg Kroah-Hartman595b14c2006-01-18 17:36:58 -0500256 * and to have the device registered with the driver core
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258static struct usb_class_driver skel_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700259 .name = "skel%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .fops = &skel_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .minor_base = USB_SKEL_MINOR_BASE,
262};
263
264static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id)
265{
266 struct usb_skel *dev = NULL;
267 struct usb_host_interface *iface_desc;
268 struct usb_endpoint_descriptor *endpoint;
269 size_t buffer_size;
270 int i;
271 int retval = -ENOMEM;
272
273 /* allocate memory for our device state and initialize it */
Oliver Neukumff906512005-12-21 19:27:29 +0100274 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (dev == NULL) {
276 err("Out of memory");
277 goto error;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 kref_init(&dev->kref);
Oliver Neukumff906512005-12-21 19:27:29 +0100280 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
Alan Stern121e2872006-07-01 22:06:36 -0400281 mutex_init(&dev->io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 dev->udev = usb_get_dev(interface_to_usbdev(interface));
284 dev->interface = interface;
285
286 /* set up the endpoint information */
287 /* use only the first bulk-in and bulk-out endpoints */
288 iface_desc = interface->cur_altsetting;
289 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
290 endpoint = &iface_desc->endpoint[i].desc;
291
292 if (!dev->bulk_in_endpointAddr &&
Conger, Chris A6b216df82005-07-29 12:18:23 -0700293 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
294 == USB_DIR_IN) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
296 == USB_ENDPOINT_XFER_BULK)) {
297 /* we found a bulk in endpoint */
298 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
299 dev->bulk_in_size = buffer_size;
300 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
301 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
302 if (!dev->bulk_in_buffer) {
303 err("Could not allocate bulk_in_buffer");
304 goto error;
305 }
306 }
307
308 if (!dev->bulk_out_endpointAddr &&
Conger, Chris A6b216df82005-07-29 12:18:23 -0700309 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
310 == USB_DIR_OUT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
312 == USB_ENDPOINT_XFER_BULK)) {
313 /* we found a bulk out endpoint */
314 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
315 }
316 }
317 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
318 err("Could not find both bulk-in and bulk-out endpoints");
319 goto error;
320 }
321
322 /* save our data pointer in this interface device */
323 usb_set_intfdata(interface, dev);
324
325 /* we can register the device now, as it is ready */
326 retval = usb_register_dev(interface, &skel_class);
327 if (retval) {
328 /* something prevented us from registering this driver */
329 err("Not able to get a minor for this device.");
330 usb_set_intfdata(interface, NULL);
331 goto error;
332 }
333
334 /* let the user know what node this device is now attached to */
335 info("USB Skeleton device now attached to USBSkel-%d", interface->minor);
336 return 0;
337
338error:
339 if (dev)
340 kref_put(&dev->kref, skel_delete);
341 return retval;
342}
343
344static void skel_disconnect(struct usb_interface *interface)
345{
346 struct usb_skel *dev;
347 int minor = interface->minor;
348
349 /* prevent skel_open() from racing skel_disconnect() */
350 lock_kernel();
351
352 dev = usb_get_intfdata(interface);
353 usb_set_intfdata(interface, NULL);
354
355 /* give back our minor */
356 usb_deregister_dev(interface, &skel_class);
357
Alan Stern121e2872006-07-01 22:06:36 -0400358 /* prevent more I/O from starting */
359 mutex_lock(&dev->io_mutex);
360 dev->interface = NULL;
361 mutex_unlock(&dev->io_mutex);
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 unlock_kernel();
364
365 /* decrement our usage count */
366 kref_put(&dev->kref, skel_delete);
367
368 info("USB Skeleton #%d now disconnected", minor);
369}
370
371static struct usb_driver skel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 .name = "skeleton",
373 .probe = skel_probe,
374 .disconnect = skel_disconnect,
375 .id_table = skel_table,
376};
377
378static int __init usb_skel_init(void)
379{
380 int result;
381
382 /* register this driver with the USB subsystem */
383 result = usb_register(&skel_driver);
384 if (result)
385 err("usb_register failed. Error number %d", result);
386
387 return result;
388}
389
390static void __exit usb_skel_exit(void)
391{
392 /* deregister this driver with the USB subsystem */
393 usb_deregister(&skel_driver);
394}
395
396module_init (usb_skel_init);
397module_exit (usb_skel_exit);
398
399MODULE_LICENSE("GPL");