| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ | 
|  | 32 | static struct usb_device_id skel_table [] = { | 
|  | 33 | { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, | 
|  | 34 | { }					/* Terminating entry */ | 
|  | 35 | }; | 
|  | 36 | MODULE_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 */ | 
|  | 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; | 
|  | 51 | }; | 
|  | 52 | #define to_skel_dev(d) container_of(d, struct usb_skel, kref) | 
|  | 53 |  | 
|  | 54 | static struct usb_driver skel_driver; | 
|  | 55 |  | 
|  | 56 | static 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 |  | 
|  | 65 | static 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 |  | 
|  | 94 | exit: | 
|  | 95 | return retval; | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | static 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 |  | 
|  | 111 | static 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 |  | 
|  | 137 | static 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 |  | 
|  | 157 | static 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 |  | 
|  | 204 | exit: | 
|  | 205 | return count; | 
|  | 206 |  | 
|  | 207 | error: | 
|  | 208 | usb_buffer_free(dev->udev, count, buf, urb->transfer_dma); | 
|  | 209 | usb_free_urb(urb); | 
|  | 210 | return retval; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 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, | 
|  | 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 | */ | 
|  | 225 | static struct usb_class_driver skel_class = { | 
| Greg Kroah-Hartman | d6e5bcf | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 226 | .name =		"skel%d", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | .fops =		&skel_fops, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | .minor_base =	USB_SKEL_MINOR_BASE, | 
|  | 229 | }; | 
|  | 230 |  | 
|  | 231 | static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id) | 
|  | 232 | { | 
|  | 233 | struct usb_skel *dev = NULL; | 
|  | 234 | struct usb_host_interface *iface_desc; | 
|  | 235 | struct usb_endpoint_descriptor *endpoint; | 
|  | 236 | size_t buffer_size; | 
|  | 237 | int i; | 
|  | 238 | int retval = -ENOMEM; | 
|  | 239 |  | 
|  | 240 | /* allocate memory for our device state and initialize it */ | 
|  | 241 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); | 
|  | 242 | if (dev == NULL) { | 
|  | 243 | err("Out of memory"); | 
|  | 244 | goto error; | 
|  | 245 | } | 
|  | 246 | memset(dev, 0x00, sizeof(*dev)); | 
|  | 247 | kref_init(&dev->kref); | 
|  | 248 |  | 
|  | 249 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); | 
|  | 250 | dev->interface = interface; | 
|  | 251 |  | 
|  | 252 | /* set up the endpoint information */ | 
|  | 253 | /* use only the first bulk-in and bulk-out endpoints */ | 
|  | 254 | iface_desc = interface->cur_altsetting; | 
|  | 255 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | 
|  | 256 | endpoint = &iface_desc->endpoint[i].desc; | 
|  | 257 |  | 
|  | 258 | if (!dev->bulk_in_endpointAddr && | 
| Conger, Chris A | 6b216df | 2005-07-29 12:18:23 -0700 | [diff] [blame] | 259 | ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) | 
|  | 260 | == USB_DIR_IN) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 && | 
| Conger, Chris A | 6b216df | 2005-07-29 12:18:23 -0700 | [diff] [blame] | 275 | ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) | 
|  | 276 | == USB_DIR_OUT) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | 
|  | 278 | == USB_ENDPOINT_XFER_BULK)) { | 
|  | 279 | /* we found a bulk out endpoint */ | 
|  | 280 | dev->bulk_out_endpointAddr = endpoint->bEndpointAddress; | 
|  | 281 | } | 
|  | 282 | } | 
|  | 283 | if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) { | 
|  | 284 | err("Could not find both bulk-in and bulk-out endpoints"); | 
|  | 285 | goto error; | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | /* save our data pointer in this interface device */ | 
|  | 289 | usb_set_intfdata(interface, dev); | 
|  | 290 |  | 
|  | 291 | /* we can register the device now, as it is ready */ | 
|  | 292 | retval = usb_register_dev(interface, &skel_class); | 
|  | 293 | if (retval) { | 
|  | 294 | /* something prevented us from registering this driver */ | 
|  | 295 | err("Not able to get a minor for this device."); | 
|  | 296 | usb_set_intfdata(interface, NULL); | 
|  | 297 | goto error; | 
|  | 298 | } | 
|  | 299 |  | 
|  | 300 | /* let the user know what node this device is now attached to */ | 
|  | 301 | info("USB Skeleton device now attached to USBSkel-%d", interface->minor); | 
|  | 302 | return 0; | 
|  | 303 |  | 
|  | 304 | error: | 
|  | 305 | if (dev) | 
|  | 306 | kref_put(&dev->kref, skel_delete); | 
|  | 307 | return retval; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | static void skel_disconnect(struct usb_interface *interface) | 
|  | 311 | { | 
|  | 312 | struct usb_skel *dev; | 
|  | 313 | int minor = interface->minor; | 
|  | 314 |  | 
|  | 315 | /* prevent skel_open() from racing skel_disconnect() */ | 
|  | 316 | lock_kernel(); | 
|  | 317 |  | 
|  | 318 | dev = usb_get_intfdata(interface); | 
|  | 319 | usb_set_intfdata(interface, NULL); | 
|  | 320 |  | 
|  | 321 | /* give back our minor */ | 
|  | 322 | usb_deregister_dev(interface, &skel_class); | 
|  | 323 |  | 
|  | 324 | unlock_kernel(); | 
|  | 325 |  | 
|  | 326 | /* decrement our usage count */ | 
|  | 327 | kref_put(&dev->kref, skel_delete); | 
|  | 328 |  | 
|  | 329 | info("USB Skeleton #%d now disconnected", minor); | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | static struct usb_driver skel_driver = { | 
|  | 333 | .owner =	THIS_MODULE, | 
|  | 334 | .name =		"skeleton", | 
|  | 335 | .probe =	skel_probe, | 
|  | 336 | .disconnect =	skel_disconnect, | 
|  | 337 | .id_table =	skel_table, | 
|  | 338 | }; | 
|  | 339 |  | 
|  | 340 | static int __init usb_skel_init(void) | 
|  | 341 | { | 
|  | 342 | int result; | 
|  | 343 |  | 
|  | 344 | /* register this driver with the USB subsystem */ | 
|  | 345 | result = usb_register(&skel_driver); | 
|  | 346 | if (result) | 
|  | 347 | err("usb_register failed. Error number %d", result); | 
|  | 348 |  | 
|  | 349 | return result; | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | static void __exit usb_skel_exit(void) | 
|  | 353 | { | 
|  | 354 | /* deregister this driver with the USB subsystem */ | 
|  | 355 | usb_deregister(&skel_driver); | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | module_init (usb_skel_init); | 
|  | 359 | module_exit (usb_skel_exit); | 
|  | 360 |  | 
|  | 361 | MODULE_LICENSE("GPL"); |