Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
c0704541 | 2 | * USB Skeleton driver - 2.2 |
1da177e4 LT |
3 | * |
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 | * | |
c0704541 | 10 | * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c |
121e287c | 11 | * but has been rewritten to be easier to read and use. |
1da177e4 LT |
12 | * |
13 | */ | |
14 | ||
1da177e4 LT |
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 <asm/uaccess.h> | |
22 | #include <linux/usb.h> | |
121e287c | 23 | #include <linux/mutex.h> |
1da177e4 LT |
24 | |
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 */ | |
31 | static struct usb_device_id skel_table [] = { | |
32 | { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, | |
33 | { } /* Terminating entry */ | |
34 | }; | |
c0704541 | 35 | MODULE_DEVICE_TABLE(usb, skel_table); |
1da177e4 LT |
36 | |
37 | ||
38 | /* Get a minor range for your devices from the usb maintainer */ | |
39 | #define USB_SKEL_MINOR_BASE 192 | |
40 | ||
ff906518 | 41 | /* our private defines. if this grows any larger, use your own .h file */ |
c0704541 | 42 | #define MAX_TRANSFER (PAGE_SIZE - 512) |
ff906518 ON |
43 | #define WRITES_IN_FLIGHT 8 |
44 | ||
1da177e4 LT |
45 | /* Structure to hold all of our device specific stuff */ |
46 | struct usb_skel { | |
c0704541 LFC |
47 | struct usb_device *dev; /* the usb device for this device */ |
48 | struct usb_interface *interface; /* the interface for this device */ | |
ff906518 | 49 | struct semaphore limit_sem; /* limiting the number of writes in progress */ |
c0704541 | 50 | unsigned char *bulk_in_buffer; /* the buffer to receive data */ |
1da177e4 LT |
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; | |
121e287c | 55 | struct mutex io_mutex; /* synchronize I/O with disconnect */ |
1da177e4 LT |
56 | }; |
57 | #define to_skel_dev(d) container_of(d, struct usb_skel, kref) | |
58 | ||
59 | static struct usb_driver skel_driver; | |
60 | ||
61 | static void skel_delete(struct kref *kref) | |
c0704541 | 62 | { |
1da177e4 LT |
63 | struct usb_skel *dev = to_skel_dev(kref); |
64 | ||
65 | usb_put_dev(dev->udev); | |
c0704541 LFC |
66 | kfree(dev->bulk_in_buffer); |
67 | kfree(dev); | |
1da177e4 LT |
68 | } |
69 | ||
70 | static 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 | ||
5b064708 ON |
93 | /* increment our usage count for the device */ |
94 | kref_get(&dev->kref); | |
95 | ||
01d883d4 AS |
96 | /* prevent the device from being autosuspended */ |
97 | retval = usb_autopm_get_interface(interface); | |
5b064708 ON |
98 | if (retval) { |
99 | kref_put(&dev->kref, skel_delete); | |
01d883d4 | 100 | goto exit; |
5b064708 | 101 | } |
1da177e4 LT |
102 | |
103 | /* save our object in the file's private structure */ | |
104 | file->private_data = dev; | |
105 | ||
106 | exit: | |
107 | return retval; | |
108 | } | |
109 | ||
110 | static int skel_release(struct inode *inode, struct file *file) | |
111 | { | |
112 | struct usb_skel *dev; | |
113 | ||
114 | dev = (struct usb_skel *)file->private_data; | |
115 | if (dev == NULL) | |
116 | return -ENODEV; | |
117 | ||
01d883d4 AS |
118 | /* allow the device to be autosuspended */ |
119 | mutex_lock(&dev->io_mutex); | |
120 | if (dev->interface) | |
121 | usb_autopm_put_interface(dev->interface); | |
122 | mutex_unlock(&dev->io_mutex); | |
123 | ||
1da177e4 LT |
124 | /* decrement the count on our device */ |
125 | kref_put(&dev->kref, skel_delete); | |
126 | return 0; | |
127 | } | |
128 | ||
129 | static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos) | |
130 | { | |
131 | struct usb_skel *dev; | |
c0704541 | 132 | int retval; |
1da177e4 LT |
133 | int bytes_read; |
134 | ||
135 | dev = (struct usb_skel *)file->private_data; | |
121e287c AS |
136 | |
137 | mutex_lock(&dev->io_mutex); | |
138 | if (!dev->interface) { /* disconnect() was called */ | |
139 | retval = -ENODEV; | |
140 | goto exit; | |
141 | } | |
142 | ||
1da177e4 LT |
143 | /* do a blocking bulk read to get data from the device */ |
144 | retval = usb_bulk_msg(dev->udev, | |
145 | usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr), | |
146 | dev->bulk_in_buffer, | |
147 | min(dev->bulk_in_size, count), | |
148 | &bytes_read, 10000); | |
149 | ||
150 | /* if the read was successful, copy the data to userspace */ | |
151 | if (!retval) { | |
152 | if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read)) | |
153 | retval = -EFAULT; | |
154 | else | |
155 | retval = bytes_read; | |
156 | } | |
157 | ||
121e287c AS |
158 | exit: |
159 | mutex_unlock(&dev->io_mutex); | |
1da177e4 LT |
160 | return retval; |
161 | } | |
162 | ||
7d12e780 | 163 | static void skel_write_bulk_callback(struct urb *urb) |
1da177e4 LT |
164 | { |
165 | struct usb_skel *dev; | |
166 | ||
167 | dev = (struct usb_skel *)urb->context; | |
168 | ||
169 | /* sync/async unlink faults aren't errors */ | |
c0704541 LFC |
170 | if (urb->status && |
171 | !(urb->status == -ENOENT || | |
1da177e4 LT |
172 | urb->status == -ECONNRESET || |
173 | urb->status == -ESHUTDOWN)) { | |
c0704541 | 174 | err("%s - nonzero write bulk status received: %d", |
1da177e4 LT |
175 | __FUNCTION__, urb->status); |
176 | } | |
177 | ||
178 | /* free up our allocated buffer */ | |
c0704541 | 179 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, |
1da177e4 | 180 | urb->transfer_buffer, urb->transfer_dma); |
ff906518 | 181 | up(&dev->limit_sem); |
1da177e4 LT |
182 | } |
183 | ||
184 | static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos) | |
185 | { | |
186 | struct usb_skel *dev; | |
187 | int retval = 0; | |
188 | struct urb *urb = NULL; | |
189 | char *buf = NULL; | |
c8dd7709 | 190 | size_t writesize = min(count, (size_t)MAX_TRANSFER); |
1da177e4 LT |
191 | |
192 | dev = (struct usb_skel *)file->private_data; | |
193 | ||
194 | /* verify that we actually have some data to write */ | |
195 | if (count == 0) | |
196 | goto exit; | |
197 | ||
ff906518 | 198 | /* limit the number of URBs in flight to stop a user from using up all RAM */ |
c8dd7709 SB |
199 | if (down_interruptible(&dev->limit_sem)) { |
200 | retval = -ERESTARTSYS; | |
201 | goto exit; | |
202 | } | |
ff906518 | 203 | |
121e287c AS |
204 | mutex_lock(&dev->io_mutex); |
205 | if (!dev->interface) { /* disconnect() was called */ | |
206 | retval = -ENODEV; | |
207 | goto error; | |
208 | } | |
209 | ||
1da177e4 LT |
210 | /* create a urb, and a buffer for it, and copy the data to the urb */ |
211 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
212 | if (!urb) { | |
213 | retval = -ENOMEM; | |
214 | goto error; | |
215 | } | |
216 | ||
ff906518 | 217 | buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma); |
1da177e4 LT |
218 | if (!buf) { |
219 | retval = -ENOMEM; | |
220 | goto error; | |
221 | } | |
222 | ||
ff906518 | 223 | if (copy_from_user(buf, user_buffer, writesize)) { |
1da177e4 LT |
224 | retval = -EFAULT; |
225 | goto error; | |
226 | } | |
227 | ||
228 | /* initialize the urb properly */ | |
229 | usb_fill_bulk_urb(urb, dev->udev, | |
230 | usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), | |
ff906518 | 231 | buf, writesize, skel_write_bulk_callback, dev); |
1da177e4 LT |
232 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
233 | ||
234 | /* send the data out the bulk port */ | |
235 | retval = usb_submit_urb(urb, GFP_KERNEL); | |
236 | if (retval) { | |
237 | err("%s - failed submitting write urb, error %d", __FUNCTION__, retval); | |
238 | goto error; | |
239 | } | |
240 | ||
241 | /* release our reference to this urb, the USB core will eventually free it entirely */ | |
242 | usb_free_urb(urb); | |
243 | ||
121e287c | 244 | mutex_unlock(&dev->io_mutex); |
ff906518 | 245 | return writesize; |
1da177e4 LT |
246 | |
247 | error: | |
121e287c AS |
248 | if (urb) { |
249 | usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma); | |
250 | usb_free_urb(urb); | |
251 | } | |
252 | mutex_unlock(&dev->io_mutex); | |
ff906518 | 253 | up(&dev->limit_sem); |
121e287c AS |
254 | |
255 | exit: | |
1da177e4 LT |
256 | return retval; |
257 | } | |
258 | ||
066202dd | 259 | static const struct file_operations skel_fops = { |
1da177e4 LT |
260 | .owner = THIS_MODULE, |
261 | .read = skel_read, | |
262 | .write = skel_write, | |
263 | .open = skel_open, | |
264 | .release = skel_release, | |
265 | }; | |
266 | ||
c0704541 | 267 | /* |
1da177e4 | 268 | * usb class driver info in order to get a minor number from the usb core, |
595b14cb | 269 | * and to have the device registered with the driver core |
1da177e4 LT |
270 | */ |
271 | static struct usb_class_driver skel_class = { | |
d6e5bcf4 | 272 | .name = "skel%d", |
1da177e4 | 273 | .fops = &skel_fops, |
1da177e4 LT |
274 | .minor_base = USB_SKEL_MINOR_BASE, |
275 | }; | |
276 | ||
277 | static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id) | |
278 | { | |
c0704541 | 279 | struct usb_skel *dev; |
1da177e4 LT |
280 | struct usb_host_interface *iface_desc; |
281 | struct usb_endpoint_descriptor *endpoint; | |
282 | size_t buffer_size; | |
283 | int i; | |
284 | int retval = -ENOMEM; | |
285 | ||
286 | /* allocate memory for our device state and initialize it */ | |
ff906518 | 287 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
c0704541 | 288 | if (!dev) { |
1da177e4 LT |
289 | err("Out of memory"); |
290 | goto error; | |
291 | } | |
1da177e4 | 292 | kref_init(&dev->kref); |
ff906518 | 293 | sema_init(&dev->limit_sem, WRITES_IN_FLIGHT); |
121e287c | 294 | mutex_init(&dev->io_mutex); |
1da177e4 LT |
295 | |
296 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); | |
297 | dev->interface = interface; | |
298 | ||
299 | /* set up the endpoint information */ | |
300 | /* use only the first bulk-in and bulk-out endpoints */ | |
301 | iface_desc = interface->cur_altsetting; | |
302 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | |
303 | endpoint = &iface_desc->endpoint[i].desc; | |
304 | ||
305 | if (!dev->bulk_in_endpointAddr && | |
c0704541 | 306 | usb_endpoint_is_bulk_in(endpoint)) { |
1da177e4 LT |
307 | /* we found a bulk in endpoint */ |
308 | buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); | |
309 | dev->bulk_in_size = buffer_size; | |
310 | dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; | |
311 | dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); | |
312 | if (!dev->bulk_in_buffer) { | |
313 | err("Could not allocate bulk_in_buffer"); | |
314 | goto error; | |
315 | } | |
316 | } | |
317 | ||
318 | if (!dev->bulk_out_endpointAddr && | |
c0704541 | 319 | usb_endpoint_is_bulk_out(endpoint)) { |
1da177e4 LT |
320 | /* we found a bulk out endpoint */ |
321 | dev->bulk_out_endpointAddr = endpoint->bEndpointAddress; | |
322 | } | |
323 | } | |
324 | if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) { | |
325 | err("Could not find both bulk-in and bulk-out endpoints"); | |
326 | goto error; | |
327 | } | |
328 | ||
329 | /* save our data pointer in this interface device */ | |
330 | usb_set_intfdata(interface, dev); | |
331 | ||
332 | /* we can register the device now, as it is ready */ | |
333 | retval = usb_register_dev(interface, &skel_class); | |
334 | if (retval) { | |
335 | /* something prevented us from registering this driver */ | |
336 | err("Not able to get a minor for this device."); | |
337 | usb_set_intfdata(interface, NULL); | |
338 | goto error; | |
339 | } | |
340 | ||
341 | /* let the user know what node this device is now attached to */ | |
342 | info("USB Skeleton device now attached to USBSkel-%d", interface->minor); | |
343 | return 0; | |
344 | ||
345 | error: | |
346 | if (dev) | |
347 | kref_put(&dev->kref, skel_delete); | |
348 | return retval; | |
349 | } | |
350 | ||
351 | static void skel_disconnect(struct usb_interface *interface) | |
352 | { | |
353 | struct usb_skel *dev; | |
354 | int minor = interface->minor; | |
355 | ||
356 | /* prevent skel_open() from racing skel_disconnect() */ | |
357 | lock_kernel(); | |
358 | ||
359 | dev = usb_get_intfdata(interface); | |
360 | usb_set_intfdata(interface, NULL); | |
361 | ||
362 | /* give back our minor */ | |
363 | usb_deregister_dev(interface, &skel_class); | |
364 | ||
121e287c AS |
365 | /* prevent more I/O from starting */ |
366 | mutex_lock(&dev->io_mutex); | |
367 | dev->interface = NULL; | |
368 | mutex_unlock(&dev->io_mutex); | |
369 | ||
1da177e4 LT |
370 | unlock_kernel(); |
371 | ||
372 | /* decrement our usage count */ | |
373 | kref_put(&dev->kref, skel_delete); | |
374 | ||
375 | info("USB Skeleton #%d now disconnected", minor); | |
376 | } | |
377 | ||
378 | static struct usb_driver skel_driver = { | |
1da177e4 LT |
379 | .name = "skeleton", |
380 | .probe = skel_probe, | |
381 | .disconnect = skel_disconnect, | |
382 | .id_table = skel_table, | |
383 | }; | |
384 | ||
385 | static int __init usb_skel_init(void) | |
386 | { | |
387 | int result; | |
388 | ||
389 | /* register this driver with the USB subsystem */ | |
390 | result = usb_register(&skel_driver); | |
391 | if (result) | |
392 | err("usb_register failed. Error number %d", result); | |
393 | ||
394 | return result; | |
395 | } | |
396 | ||
397 | static void __exit usb_skel_exit(void) | |
398 | { | |
399 | /* deregister this driver with the USB subsystem */ | |
400 | usb_deregister(&skel_driver); | |
401 | } | |
402 | ||
c0704541 LFC |
403 | module_init(usb_skel_init); |
404 | module_exit(usb_skel_exit); | |
1da177e4 LT |
405 | |
406 | MODULE_LICENSE("GPL"); |