[PATCH] inotify delete race fix
[linux-2.6/mini2440.git] / drivers / usb / usb-skeleton.c
blob353f24d45bc18849a121abd09ffc945330a0113a
1 /*
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
12 * needed anymore.
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 */
43 struct usb_skel {
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 */
50 struct kref kref;
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);
62 kfree (dev);
65 static int skel_open(struct inode *inode, struct file *file)
67 struct usb_skel *dev;
68 struct usb_interface *interface;
69 int subminor;
70 int retval = 0;
72 subminor = iminor(inode);
74 interface = usb_find_interface(&skel_driver, subminor);
75 if (!interface) {
76 err ("%s - error, can't find device for minor %d",
77 __FUNCTION__, subminor);
78 retval = -ENODEV;
79 goto exit;
82 dev = usb_get_intfdata(interface);
83 if (!dev) {
84 retval = -ENODEV;
85 goto exit;
88 /* increment our usage count for the device */
89 kref_get(&dev->kref);
91 /* save our object in the file's private structure */
92 file->private_data = dev;
94 exit:
95 return retval;
98 static int skel_release(struct inode *inode, struct file *file)
100 struct usb_skel *dev;
102 dev = (struct usb_skel *)file->private_data;
103 if (dev == NULL)
104 return -ENODEV;
106 /* decrement the count on our device */
107 kref_put(&dev->kref, skel_delete);
108 return 0;
111 static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
113 struct usb_skel *dev;
114 int retval = 0;
115 int bytes_read;
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),
122 dev->bulk_in_buffer,
123 min(dev->bulk_in_size, count),
124 &bytes_read, 10000);
126 /* if the read was successful, copy the data to userspace */
127 if (!retval) {
128 if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
129 retval = -EFAULT;
130 else
131 retval = bytes_read;
134 return retval;
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 */
144 if (urb->status &&
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;
160 int retval = 0;
161 struct urb *urb = NULL;
162 char *buf = NULL;
164 dev = (struct usb_skel *)file->private_data;
166 /* verify that we actually have some data to write */
167 if (count == 0)
168 goto exit;
170 /* create a urb, and a buffer for it, and copy the data to the urb */
171 urb = usb_alloc_urb(0, GFP_KERNEL);
172 if (!urb) {
173 retval = -ENOMEM;
174 goto error;
177 buf = usb_buffer_alloc(dev->udev, count, GFP_KERNEL, &urb->transfer_dma);
178 if (!buf) {
179 retval = -ENOMEM;
180 goto error;
183 if (copy_from_user(buf, user_buffer, count)) {
184 retval = -EFAULT;
185 goto error;
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);
196 if (retval) {
197 err("%s - failed submitting write urb, error %d", __FUNCTION__, retval);
198 goto error;
201 /* release our reference to this urb, the USB core will eventually free it entirely */
202 usb_free_urb(urb);
204 exit:
205 return count;
207 error:
208 usb_buffer_free(dev->udev, count, buf, urb->transfer_dma);
209 usb_free_urb(urb);
210 return retval;
213 static struct file_operations skel_fops = {
214 .owner = THIS_MODULE,
215 .read = skel_read,
216 .write = skel_write,
217 .open = skel_open,
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",
227 .fops = &skel_fops,
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;
237 size_t buffer_size;
238 int i;
239 int retval = -ENOMEM;
241 /* allocate memory for our device state and initialize it */
242 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
243 if (dev == NULL) {
244 err("Out of memory");
245 goto error;
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)
261 == USB_DIR_IN) &&
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");
271 goto error;
275 if (!dev->bulk_out_endpointAddr &&
276 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
277 == USB_DIR_OUT) &&
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");
286 goto error;
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);
294 if (retval) {
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);
298 goto error;
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);
303 return 0;
305 error:
306 if (dev)
307 kref_put(&dev->kref, skel_delete);
308 return retval;
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() */
317 lock_kernel();
319 dev = usb_get_intfdata(interface);
320 usb_set_intfdata(interface, NULL);
322 /* give back our minor */
323 usb_deregister_dev(interface, &skel_class);
325 unlock_kernel();
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,
335 .name = "skeleton",
336 .probe = skel_probe,
337 .disconnect = skel_disconnect,
338 .id_table = skel_table,
341 static int __init usb_skel_init(void)
343 int result;
345 /* register this driver with the USB subsystem */
346 result = usb_register(&skel_driver);
347 if (result)
348 err("usb_register failed. Error number %d", result);
350 return 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");