blob: 353f24d45bc18849a121abd09ffc945330a0113a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 &&
Conger, Chris A6b216df82005-07-29 12:18:23 -0700260 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
261 == USB_DIR_IN) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 ((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;
272 }
273 }
274
275 if (!dev->bulk_out_endpointAddr &&
Conger, Chris A6b216df82005-07-29 12:18:23 -0700276 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
277 == USB_DIR_OUT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 ((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;
282 }
283 }
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;
287 }
288
289 /* save our data pointer in this interface device */
290 usb_set_intfdata(interface, dev);
291
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;
299 }
300
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;
304
305error:
306 if (dev)
307 kref_put(&dev->kref, skel_delete);
308 return retval;
309}
310
311static void skel_disconnect(struct usb_interface *interface)
312{
313 struct usb_skel *dev;
314 int minor = interface->minor;
315
316 /* prevent skel_open() from racing skel_disconnect() */
317 lock_kernel();
318
319 dev = usb_get_intfdata(interface);
320 usb_set_intfdata(interface, NULL);
321
322 /* give back our minor */
323 usb_deregister_dev(interface, &skel_class);
324
325 unlock_kernel();
326
327 /* decrement our usage count */
328 kref_put(&dev->kref, skel_delete);
329
330 info("USB Skeleton #%d now disconnected", minor);
331}
332
333static 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,
339};
340
341static int __init usb_skel_init(void)
342{
343 int result;
344
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);
349
350 return result;
351}
352
353static void __exit usb_skel_exit(void)
354{
355 /* deregister this driver with the USB subsystem */
356 usb_deregister(&skel_driver);
357}
358
359module_init (usb_skel_init);
360module_exit (usb_skel_exit);
361
362MODULE_LICENSE("GPL");