aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/usb-skeleton.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/usb/usb-skeleton.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/usb/usb-skeleton.c')
-rw-r--r--drivers/usb/usb-skeleton.c360
1 files changed, 360 insertions, 0 deletions
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c
new file mode 100644
index 000000000000..6051a646fe69
--- /dev/null
+++ b/drivers/usb/usb-skeleton.c
@@ -0,0 +1,360 @@
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
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>
25
26
27/* Define these values to match your devices */
28#define USB_SKEL_VENDOR_ID 0xfff0
29#define USB_SKEL_PRODUCT_ID 0xfff0
30
31/* table of devices that work with this driver */
32static struct usb_device_id skel_table [] = {
33 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
34 { } /* Terminating entry */
35};
36MODULE_DEVICE_TABLE (usb, skel_table);
37
38
39/* Get a minor range for your devices from the usb maintainer */
40#define USB_SKEL_MINOR_BASE 192
41
42/* Structure to hold all of our device specific stuff */
43struct 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;
51};
52#define to_skel_dev(d) container_of(d, struct usb_skel, kref)
53
54static struct usb_driver skel_driver;
55
56static void skel_delete(struct kref *kref)
57{
58 struct usb_skel *dev = to_skel_dev(kref);
59
60 usb_put_dev(dev->udev);
61 kfree (dev->bulk_in_buffer);
62 kfree (dev);
63}
64
65static int skel_open(struct inode *inode, struct file *file)
66{
67 struct usb_skel *dev;
68 struct usb_interface *interface;
69 int subminor;
70 int retval = 0;
71
72 subminor = iminor(inode);
73
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;
80 }
81
82 dev = usb_get_intfdata(interface);
83 if (!dev) {
84 retval = -ENODEV;
85 goto exit;
86 }
87
88 /* increment our usage count for the device */
89 kref_get(&dev->kref);
90
91 /* save our object in the file's private structure */
92 file->private_data = dev;
93
94exit:
95 return retval;
96}
97
98static int skel_release(struct inode *inode, struct file *file)
99{
100 struct usb_skel *dev;
101
102 dev = (struct usb_skel *)file->private_data;
103 if (dev == NULL)
104 return -ENODEV;
105
106 /* decrement the count on our device */
107 kref_put(&dev->kref, skel_delete);
108 return 0;
109}
110
111static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
112{
113 struct usb_skel *dev;
114 int retval = 0;
115 int bytes_read;
116
117 dev = (struct usb_skel *)file->private_data;
118
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);
125
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;
132 }
133
134 return retval;
135}
136
137static void skel_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
138{
139 struct usb_skel *dev;
140
141 dev = (struct usb_skel *)urb->context;
142
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);
150 }
151
152 /* free up our allocated buffer */
153 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
154 urb->transfer_buffer, urb->transfer_dma);
155}
156
157static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
158{
159 struct usb_skel *dev;
160 int retval = 0;
161 struct urb *urb = NULL;
162 char *buf = NULL;
163
164 dev = (struct usb_skel *)file->private_data;
165
166 /* verify that we actually have some data to write */
167 if (count == 0)
168 goto exit;
169
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;
175 }
176
177 buf = usb_buffer_alloc(dev->udev, count, GFP_KERNEL, &urb->transfer_dma);
178 if (!buf) {
179 retval = -ENOMEM;
180 goto error;
181 }
182
183 if (copy_from_user(buf, user_buffer, count)) {
184 retval = -EFAULT;
185 goto error;
186 }
187
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;
193
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;
199 }
200
201 /* release our reference to this urb, the USB core will eventually free it entirely */
202 usb_free_urb(urb);
203
204exit:
205 return count;
206
207error:
208 usb_buffer_free(dev->udev, count, buf, urb->transfer_dma);
209 usb_free_urb(urb);
210 return retval;
211}
212
213static 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,
219};
220
221/*
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
224 */
225static 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,
230};
231
232static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id)
233{
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;
240
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;
246 }
247 memset(dev, 0x00, sizeof(*dev));
248 kref_init(&dev->kref);
249
250 dev->udev = usb_get_dev(interface_to_usbdev(interface));
251 dev->interface = interface;
252
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;
258
259 if (!dev->bulk_in_endpointAddr &&
260 (endpoint->bEndpointAddress & USB_DIR_IN) &&
261 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
262 == USB_ENDPOINT_XFER_BULK)) {
263 /* we found a bulk in endpoint */
264 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
265 dev->bulk_in_size = buffer_size;
266 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
267 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
268 if (!dev->bulk_in_buffer) {
269 err("Could not allocate bulk_in_buffer");
270 goto error;
271 }
272 }
273
274 if (!dev->bulk_out_endpointAddr &&
275 !(endpoint->bEndpointAddress & USB_DIR_OUT) &&
276 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
277 == USB_ENDPOINT_XFER_BULK)) {
278 /* we found a bulk out endpoint */
279 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
280 }
281 }
282 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
283 err("Could not find both bulk-in and bulk-out endpoints");
284 goto error;
285 }
286
287 /* save our data pointer in this interface device */
288 usb_set_intfdata(interface, dev);
289
290 /* we can register the device now, as it is ready */
291 retval = usb_register_dev(interface, &skel_class);
292 if (retval) {
293 /* something prevented us from registering this driver */
294 err("Not able to get a minor for this device.");
295 usb_set_intfdata(interface, NULL);
296 goto error;
297 }
298
299 /* let the user know what node this device is now attached to */
300 info("USB Skeleton device now attached to USBSkel-%d", interface->minor);
301 return 0;
302
303error:
304 if (dev)
305 kref_put(&dev->kref, skel_delete);
306 return retval;
307}
308
309static void skel_disconnect(struct usb_interface *interface)
310{
311 struct usb_skel *dev;
312 int minor = interface->minor;
313
314 /* prevent skel_open() from racing skel_disconnect() */
315 lock_kernel();
316
317 dev = usb_get_intfdata(interface);
318 usb_set_intfdata(interface, NULL);
319
320 /* give back our minor */
321 usb_deregister_dev(interface, &skel_class);
322
323 unlock_kernel();
324
325 /* decrement our usage count */
326 kref_put(&dev->kref, skel_delete);
327
328 info("USB Skeleton #%d now disconnected", minor);
329}
330
331static struct usb_driver skel_driver = {
332 .owner = THIS_MODULE,
333 .name = "skeleton",
334 .probe = skel_probe,
335 .disconnect = skel_disconnect,
336 .id_table = skel_table,
337};
338
339static int __init usb_skel_init(void)
340{
341 int result;
342
343 /* register this driver with the USB subsystem */
344 result = usb_register(&skel_driver);
345 if (result)
346 err("usb_register failed. Error number %d", result);
347
348 return result;
349}
350
351static void __exit usb_skel_exit(void)
352{
353 /* deregister this driver with the USB subsystem */
354 usb_deregister(&skel_driver);
355}
356
357module_init (usb_skel_init);
358module_exit (usb_skel_exit);
359
360MODULE_LICENSE("GPL");