Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * USB Skeleton driver - 2.0 | |
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 | * | |
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. | |
13 | * | |
14 | */ | |
15 | ||
1da177e4 LT |
16 | #include <linux/kernel.h> |
17 | #include <linux/errno.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/kref.h> | |
22 | #include <asm/uaccess.h> | |
23 | #include <linux/usb.h> | |
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 | }; | |
35 | MODULE_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 | ||
ff906518 ON |
41 | /* 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 | ||
1da177e4 LT |
45 | /* Structure to hold all of our device specific stuff */ |
46 | struct usb_skel { | |
47 | struct usb_device * udev; /* 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 */ |
1da177e4 LT |
50 | 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; | |
55 | }; | |
56 | #define to_skel_dev(d) container_of(d, struct usb_skel, kref) | |
57 | ||
58 | static struct usb_driver skel_driver; | |
59 | ||
60 | static void skel_delete(struct kref *kref) | |
61 | { | |
62 | struct usb_skel *dev = to_skel_dev(kref); | |
63 | ||
64 | usb_put_dev(dev->udev); | |
65 | kfree (dev->bulk_in_buffer); | |
66 | kfree (dev); | |
67 | } | |
68 | ||
69 | static int skel_open(struct inode *inode, struct file *file) | |
70 | { | |
71 | struct usb_skel *dev; | |
72 | struct usb_interface *interface; | |
73 | int subminor; | |
74 | int retval = 0; | |
75 | ||
76 | subminor = iminor(inode); | |
77 | ||
78 | interface = usb_find_interface(&skel_driver, subminor); | |
79 | if (!interface) { | |
80 | err ("%s - error, can't find device for minor %d", | |
81 | __FUNCTION__, subminor); | |
82 | retval = -ENODEV; | |
83 | goto exit; | |
84 | } | |
85 | ||
86 | dev = usb_get_intfdata(interface); | |
87 | if (!dev) { | |
88 | retval = -ENODEV; | |
89 | goto exit; | |
90 | } | |
91 | ||
92 | /* increment our usage count for the device */ | |
93 | kref_get(&dev->kref); | |
94 | ||
95 | /* save our object in the file's private structure */ | |
96 | file->private_data = dev; | |
97 | ||
98 | exit: | |
99 | return retval; | |
100 | } | |
101 | ||
102 | static int skel_release(struct inode *inode, struct file *file) | |
103 | { | |
104 | struct usb_skel *dev; | |
105 | ||
106 | dev = (struct usb_skel *)file->private_data; | |
107 | if (dev == NULL) | |
108 | return -ENODEV; | |
109 | ||
110 | /* decrement the count on our device */ | |
111 | kref_put(&dev->kref, skel_delete); | |
112 | return 0; | |
113 | } | |
114 | ||
115 | static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos) | |
116 | { | |
117 | struct usb_skel *dev; | |
118 | int retval = 0; | |
119 | int bytes_read; | |
120 | ||
121 | dev = (struct usb_skel *)file->private_data; | |
122 | ||
123 | /* do a blocking bulk read to get data from the device */ | |
124 | retval = usb_bulk_msg(dev->udev, | |
125 | usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr), | |
126 | dev->bulk_in_buffer, | |
127 | min(dev->bulk_in_size, count), | |
128 | &bytes_read, 10000); | |
129 | ||
130 | /* if the read was successful, copy the data to userspace */ | |
131 | if (!retval) { | |
132 | if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read)) | |
133 | retval = -EFAULT; | |
134 | else | |
135 | retval = bytes_read; | |
136 | } | |
137 | ||
138 | return retval; | |
139 | } | |
140 | ||
141 | static void skel_write_bulk_callback(struct urb *urb, struct pt_regs *regs) | |
142 | { | |
143 | struct usb_skel *dev; | |
144 | ||
145 | dev = (struct usb_skel *)urb->context; | |
146 | ||
147 | /* sync/async unlink faults aren't errors */ | |
148 | if (urb->status && | |
149 | !(urb->status == -ENOENT || | |
150 | urb->status == -ECONNRESET || | |
151 | urb->status == -ESHUTDOWN)) { | |
152 | dbg("%s - nonzero write bulk status received: %d", | |
153 | __FUNCTION__, urb->status); | |
154 | } | |
155 | ||
156 | /* free up our allocated buffer */ | |
157 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, | |
158 | urb->transfer_buffer, urb->transfer_dma); | |
ff906518 | 159 | up(&dev->limit_sem); |
1da177e4 LT |
160 | } |
161 | ||
162 | static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos) | |
163 | { | |
164 | struct usb_skel *dev; | |
165 | int retval = 0; | |
166 | struct urb *urb = NULL; | |
167 | char *buf = NULL; | |
c8dd7709 | 168 | size_t writesize = min(count, (size_t)MAX_TRANSFER); |
1da177e4 LT |
169 | |
170 | dev = (struct usb_skel *)file->private_data; | |
171 | ||
172 | /* verify that we actually have some data to write */ | |
173 | if (count == 0) | |
174 | goto exit; | |
175 | ||
ff906518 | 176 | /* limit the number of URBs in flight to stop a user from using up all RAM */ |
c8dd7709 SB |
177 | if (down_interruptible(&dev->limit_sem)) { |
178 | retval = -ERESTARTSYS; | |
179 | goto exit; | |
180 | } | |
ff906518 | 181 | |
1da177e4 LT |
182 | /* create a urb, and a buffer for it, and copy the data to the urb */ |
183 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
184 | if (!urb) { | |
185 | retval = -ENOMEM; | |
186 | goto error; | |
187 | } | |
188 | ||
ff906518 | 189 | buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma); |
1da177e4 LT |
190 | if (!buf) { |
191 | retval = -ENOMEM; | |
192 | goto error; | |
193 | } | |
194 | ||
ff906518 | 195 | if (copy_from_user(buf, user_buffer, writesize)) { |
1da177e4 LT |
196 | retval = -EFAULT; |
197 | goto error; | |
198 | } | |
199 | ||
200 | /* initialize the urb properly */ | |
201 | usb_fill_bulk_urb(urb, dev->udev, | |
202 | usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), | |
ff906518 | 203 | buf, writesize, skel_write_bulk_callback, dev); |
1da177e4 LT |
204 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
205 | ||
206 | /* send the data out the bulk port */ | |
207 | retval = usb_submit_urb(urb, GFP_KERNEL); | |
208 | if (retval) { | |
209 | err("%s - failed submitting write urb, error %d", __FUNCTION__, retval); | |
210 | goto error; | |
211 | } | |
212 | ||
213 | /* release our reference to this urb, the USB core will eventually free it entirely */ | |
214 | usb_free_urb(urb); | |
215 | ||
216 | exit: | |
ff906518 | 217 | return writesize; |
1da177e4 LT |
218 | |
219 | error: | |
ff906518 | 220 | usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma); |
1da177e4 | 221 | usb_free_urb(urb); |
ff906518 | 222 | up(&dev->limit_sem); |
1da177e4 LT |
223 | return retval; |
224 | } | |
225 | ||
226 | static struct file_operations skel_fops = { | |
227 | .owner = THIS_MODULE, | |
228 | .read = skel_read, | |
229 | .write = skel_write, | |
230 | .open = skel_open, | |
231 | .release = skel_release, | |
232 | }; | |
233 | ||
234 | /* | |
235 | * usb class driver info in order to get a minor number from the usb core, | |
595b14cb | 236 | * and to have the device registered with the driver core |
1da177e4 LT |
237 | */ |
238 | static struct usb_class_driver skel_class = { | |
d6e5bcf4 | 239 | .name = "skel%d", |
1da177e4 | 240 | .fops = &skel_fops, |
1da177e4 LT |
241 | .minor_base = USB_SKEL_MINOR_BASE, |
242 | }; | |
243 | ||
244 | static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id) | |
245 | { | |
246 | struct usb_skel *dev = NULL; | |
247 | struct usb_host_interface *iface_desc; | |
248 | struct usb_endpoint_descriptor *endpoint; | |
249 | size_t buffer_size; | |
250 | int i; | |
251 | int retval = -ENOMEM; | |
252 | ||
253 | /* allocate memory for our device state and initialize it */ | |
ff906518 | 254 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
1da177e4 LT |
255 | if (dev == NULL) { |
256 | err("Out of memory"); | |
257 | goto error; | |
258 | } | |
1da177e4 | 259 | kref_init(&dev->kref); |
ff906518 | 260 | sema_init(&dev->limit_sem, WRITES_IN_FLIGHT); |
1da177e4 LT |
261 | |
262 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); | |
263 | dev->interface = interface; | |
264 | ||
265 | /* set up the endpoint information */ | |
266 | /* use only the first bulk-in and bulk-out endpoints */ | |
267 | iface_desc = interface->cur_altsetting; | |
268 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | |
269 | endpoint = &iface_desc->endpoint[i].desc; | |
270 | ||
271 | if (!dev->bulk_in_endpointAddr && | |
6b216df8 CC |
272 | ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
273 | == USB_DIR_IN) && | |
1da177e4 LT |
274 | ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
275 | == USB_ENDPOINT_XFER_BULK)) { | |
276 | /* we found a bulk in endpoint */ | |
277 | buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); | |
278 | dev->bulk_in_size = buffer_size; | |
279 | dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; | |
280 | dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); | |
281 | if (!dev->bulk_in_buffer) { | |
282 | err("Could not allocate bulk_in_buffer"); | |
283 | goto error; | |
284 | } | |
285 | } | |
286 | ||
287 | if (!dev->bulk_out_endpointAddr && | |
6b216df8 CC |
288 | ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
289 | == USB_DIR_OUT) && | |
1da177e4 LT |
290 | ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
291 | == USB_ENDPOINT_XFER_BULK)) { | |
292 | /* we found a bulk out endpoint */ | |
293 | dev->bulk_out_endpointAddr = endpoint->bEndpointAddress; | |
294 | } | |
295 | } | |
296 | if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) { | |
297 | err("Could not find both bulk-in and bulk-out endpoints"); | |
298 | goto error; | |
299 | } | |
300 | ||
301 | /* save our data pointer in this interface device */ | |
302 | usb_set_intfdata(interface, dev); | |
303 | ||
304 | /* we can register the device now, as it is ready */ | |
305 | retval = usb_register_dev(interface, &skel_class); | |
306 | if (retval) { | |
307 | /* something prevented us from registering this driver */ | |
308 | err("Not able to get a minor for this device."); | |
309 | usb_set_intfdata(interface, NULL); | |
310 | goto error; | |
311 | } | |
312 | ||
313 | /* let the user know what node this device is now attached to */ | |
314 | info("USB Skeleton device now attached to USBSkel-%d", interface->minor); | |
315 | return 0; | |
316 | ||
317 | error: | |
318 | if (dev) | |
319 | kref_put(&dev->kref, skel_delete); | |
320 | return retval; | |
321 | } | |
322 | ||
323 | static void skel_disconnect(struct usb_interface *interface) | |
324 | { | |
325 | struct usb_skel *dev; | |
326 | int minor = interface->minor; | |
327 | ||
328 | /* prevent skel_open() from racing skel_disconnect() */ | |
329 | lock_kernel(); | |
330 | ||
331 | dev = usb_get_intfdata(interface); | |
332 | usb_set_intfdata(interface, NULL); | |
333 | ||
334 | /* give back our minor */ | |
335 | usb_deregister_dev(interface, &skel_class); | |
336 | ||
337 | unlock_kernel(); | |
338 | ||
339 | /* decrement our usage count */ | |
340 | kref_put(&dev->kref, skel_delete); | |
341 | ||
342 | info("USB Skeleton #%d now disconnected", minor); | |
343 | } | |
344 | ||
345 | static struct usb_driver skel_driver = { | |
1da177e4 LT |
346 | .name = "skeleton", |
347 | .probe = skel_probe, | |
348 | .disconnect = skel_disconnect, | |
349 | .id_table = skel_table, | |
350 | }; | |
351 | ||
352 | static int __init usb_skel_init(void) | |
353 | { | |
354 | int result; | |
355 | ||
356 | /* register this driver with the USB subsystem */ | |
357 | result = usb_register(&skel_driver); | |
358 | if (result) | |
359 | err("usb_register failed. Error number %d", result); | |
360 | ||
361 | return result; | |
362 | } | |
363 | ||
364 | static void __exit usb_skel_exit(void) | |
365 | { | |
366 | /* deregister this driver with the USB subsystem */ | |
367 | usb_deregister(&skel_driver); | |
368 | } | |
369 | ||
370 | module_init (usb_skel_init); | |
371 | module_exit (usb_skel_exit); | |
372 | ||
373 | MODULE_LICENSE("GPL"); |