blob: 296b091cf168c7e460ec70db19ac7dcc18a777c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -03002 * USB Skeleton driver - 2.2
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 *
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030010 * 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};
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030035MODULE_DEVICE_TABLE(usb, skel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
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 */
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030042#define MAX_TRANSFER (PAGE_SIZE - 512)
Oliver Neukumff906512005-12-21 19:27:29 +010043#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 {
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030047 struct usb_device *dev; /* 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 */
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030050 unsigned char *bulk_in_buffer; /* the buffer to receive data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 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)
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030062{
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct usb_skel *dev = to_skel_dev(kref);
64
65 usb_put_dev(dev->udev);
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030066 kfree(dev->bulk_in_buffer);
67 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
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
Alan Stern01d883d2006-08-30 15:47:18 -040093 /* prevent the device from being autosuspended */
94 retval = usb_autopm_get_interface(interface);
95 if (retval)
96 goto exit;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* increment our usage count for the device */
99 kref_get(&dev->kref);
100
101 /* save our object in the file's private structure */
102 file->private_data = dev;
103
104exit:
105 return retval;
106}
107
108static int skel_release(struct inode *inode, struct file *file)
109{
110 struct usb_skel *dev;
111
112 dev = (struct usb_skel *)file->private_data;
113 if (dev == NULL)
114 return -ENODEV;
115
Alan Stern01d883d2006-08-30 15:47:18 -0400116 /* allow the device to be autosuspended */
117 mutex_lock(&dev->io_mutex);
118 if (dev->interface)
119 usb_autopm_put_interface(dev->interface);
120 mutex_unlock(&dev->io_mutex);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* decrement the count on our device */
123 kref_put(&dev->kref, skel_delete);
124 return 0;
125}
126
127static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
128{
129 struct usb_skel *dev;
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300130 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int bytes_read;
132
133 dev = (struct usb_skel *)file->private_data;
Alan Stern121e2872006-07-01 22:06:36 -0400134
135 mutex_lock(&dev->io_mutex);
136 if (!dev->interface) { /* disconnect() was called */
137 retval = -ENODEV;
138 goto exit;
139 }
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* do a blocking bulk read to get data from the device */
142 retval = usb_bulk_msg(dev->udev,
143 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
144 dev->bulk_in_buffer,
145 min(dev->bulk_in_size, count),
146 &bytes_read, 10000);
147
148 /* if the read was successful, copy the data to userspace */
149 if (!retval) {
150 if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
151 retval = -EFAULT;
152 else
153 retval = bytes_read;
154 }
155
Alan Stern121e2872006-07-01 22:06:36 -0400156exit:
157 mutex_unlock(&dev->io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return retval;
159}
160
David Howells7d12e782006-10-05 14:55:46 +0100161static void skel_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct usb_skel *dev;
164
165 dev = (struct usb_skel *)urb->context;
166
167 /* sync/async unlink faults aren't errors */
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300168 if (urb->status &&
169 !(urb->status == -ENOENT ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 urb->status == -ECONNRESET ||
171 urb->status == -ESHUTDOWN)) {
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300172 err("%s - nonzero write bulk status received: %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 __FUNCTION__, urb->status);
174 }
175
176 /* free up our allocated buffer */
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300177 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 urb->transfer_buffer, urb->transfer_dma);
Oliver Neukumff906512005-12-21 19:27:29 +0100179 up(&dev->limit_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
183{
184 struct usb_skel *dev;
185 int retval = 0;
186 struct urb *urb = NULL;
187 char *buf = NULL;
Sam Bishopc8dd7702005-12-22 17:11:02 -0700188 size_t writesize = min(count, (size_t)MAX_TRANSFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 dev = (struct usb_skel *)file->private_data;
191
192 /* verify that we actually have some data to write */
193 if (count == 0)
194 goto exit;
195
Oliver Neukumff906512005-12-21 19:27:29 +0100196 /* limit the number of URBs in flight to stop a user from using up all RAM */
Sam Bishopc8dd7702005-12-22 17:11:02 -0700197 if (down_interruptible(&dev->limit_sem)) {
198 retval = -ERESTARTSYS;
199 goto exit;
200 }
Oliver Neukumff906512005-12-21 19:27:29 +0100201
Alan Stern121e2872006-07-01 22:06:36 -0400202 mutex_lock(&dev->io_mutex);
203 if (!dev->interface) { /* disconnect() was called */
204 retval = -ENODEV;
205 goto error;
206 }
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* create a urb, and a buffer for it, and copy the data to the urb */
209 urb = usb_alloc_urb(0, GFP_KERNEL);
210 if (!urb) {
211 retval = -ENOMEM;
212 goto error;
213 }
214
Oliver Neukumff906512005-12-21 19:27:29 +0100215 buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!buf) {
217 retval = -ENOMEM;
218 goto error;
219 }
220
Oliver Neukumff906512005-12-21 19:27:29 +0100221 if (copy_from_user(buf, user_buffer, writesize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 retval = -EFAULT;
223 goto error;
224 }
225
226 /* initialize the urb properly */
227 usb_fill_bulk_urb(urb, dev->udev,
228 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
Oliver Neukumff906512005-12-21 19:27:29 +0100229 buf, writesize, skel_write_bulk_callback, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
231
232 /* send the data out the bulk port */
233 retval = usb_submit_urb(urb, GFP_KERNEL);
234 if (retval) {
235 err("%s - failed submitting write urb, error %d", __FUNCTION__, retval);
236 goto error;
237 }
238
239 /* release our reference to this urb, the USB core will eventually free it entirely */
240 usb_free_urb(urb);
241
Alan Stern121e2872006-07-01 22:06:36 -0400242 mutex_unlock(&dev->io_mutex);
Oliver Neukumff906512005-12-21 19:27:29 +0100243 return writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245error:
Alan Stern121e2872006-07-01 22:06:36 -0400246 if (urb) {
247 usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma);
248 usb_free_urb(urb);
249 }
250 mutex_unlock(&dev->io_mutex);
Oliver Neukumff906512005-12-21 19:27:29 +0100251 up(&dev->limit_sem);
Alan Stern121e2872006-07-01 22:06:36 -0400252
253exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return retval;
255}
256
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300257static const struct file_operations skel_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .owner = THIS_MODULE,
259 .read = skel_read,
260 .write = skel_write,
261 .open = skel_open,
262 .release = skel_release,
263};
264
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300265/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * usb class driver info in order to get a minor number from the usb core,
Greg Kroah-Hartman595b14c2006-01-18 17:36:58 -0500267 * and to have the device registered with the driver core
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269static struct usb_class_driver skel_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700270 .name = "skel%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 .fops = &skel_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .minor_base = USB_SKEL_MINOR_BASE,
273};
274
275static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id)
276{
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300277 struct usb_skel *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct usb_host_interface *iface_desc;
279 struct usb_endpoint_descriptor *endpoint;
280 size_t buffer_size;
281 int i;
282 int retval = -ENOMEM;
283
284 /* allocate memory for our device state and initialize it */
Oliver Neukumff906512005-12-21 19:27:29 +0100285 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300286 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 err("Out of memory");
288 goto error;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 kref_init(&dev->kref);
Oliver Neukumff906512005-12-21 19:27:29 +0100291 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
Alan Stern121e2872006-07-01 22:06:36 -0400292 mutex_init(&dev->io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 dev->udev = usb_get_dev(interface_to_usbdev(interface));
295 dev->interface = interface;
296
297 /* set up the endpoint information */
298 /* use only the first bulk-in and bulk-out endpoints */
299 iface_desc = interface->cur_altsetting;
300 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
301 endpoint = &iface_desc->endpoint[i].desc;
302
303 if (!dev->bulk_in_endpointAddr &&
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300304 usb_endpoint_is_bulk_in(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* we found a bulk in endpoint */
306 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
307 dev->bulk_in_size = buffer_size;
308 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
309 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
310 if (!dev->bulk_in_buffer) {
311 err("Could not allocate bulk_in_buffer");
312 goto error;
313 }
314 }
315
316 if (!dev->bulk_out_endpointAddr &&
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300317 usb_endpoint_is_bulk_out(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* we found a bulk out endpoint */
319 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
320 }
321 }
322 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
323 err("Could not find both bulk-in and bulk-out endpoints");
324 goto error;
325 }
326
327 /* save our data pointer in this interface device */
328 usb_set_intfdata(interface, dev);
329
330 /* we can register the device now, as it is ready */
331 retval = usb_register_dev(interface, &skel_class);
332 if (retval) {
333 /* something prevented us from registering this driver */
334 err("Not able to get a minor for this device.");
335 usb_set_intfdata(interface, NULL);
336 goto error;
337 }
338
339 /* let the user know what node this device is now attached to */
340 info("USB Skeleton device now attached to USBSkel-%d", interface->minor);
341 return 0;
342
343error:
344 if (dev)
345 kref_put(&dev->kref, skel_delete);
346 return retval;
347}
348
349static void skel_disconnect(struct usb_interface *interface)
350{
351 struct usb_skel *dev;
352 int minor = interface->minor;
353
354 /* prevent skel_open() from racing skel_disconnect() */
355 lock_kernel();
356
357 dev = usb_get_intfdata(interface);
358 usb_set_intfdata(interface, NULL);
359
360 /* give back our minor */
361 usb_deregister_dev(interface, &skel_class);
362
Alan Stern121e2872006-07-01 22:06:36 -0400363 /* prevent more I/O from starting */
364 mutex_lock(&dev->io_mutex);
365 dev->interface = NULL;
366 mutex_unlock(&dev->io_mutex);
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 unlock_kernel();
369
370 /* decrement our usage count */
371 kref_put(&dev->kref, skel_delete);
372
373 info("USB Skeleton #%d now disconnected", minor);
374}
375
376static struct usb_driver skel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 .name = "skeleton",
378 .probe = skel_probe,
379 .disconnect = skel_disconnect,
380 .id_table = skel_table,
381};
382
383static int __init usb_skel_init(void)
384{
385 int result;
386
387 /* register this driver with the USB subsystem */
388 result = usb_register(&skel_driver);
389 if (result)
390 err("usb_register failed. Error number %d", result);
391
392 return result;
393}
394
395static void __exit usb_skel_exit(void)
396{
397 /* deregister this driver with the USB subsystem */
398 usb_deregister(&skel_driver);
399}
400
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300401module_init(usb_skel_init);
402module_exit(usb_skel_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404MODULE_LICENSE("GPL");