usb-skeleton.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:17k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * USB Skeleton driver - 0.7
  3.  *
  4.  * Copyright (c) 2001 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; either version 2 of
  9.  * the License, or (at your option) any later version.
  10.  *
  11.  *
  12.  * This driver is to be used as a skeleton driver to be able to create a
  13.  * USB driver quickly.  The design of it is based on the usb-serial and
  14.  * dc2xx drivers.
  15.  *
  16.  * Thanks to Oliver Neukum and David Brownell for their help in debugging
  17.  * this driver.
  18.  *
  19.  * TODO:
  20.  * - fix urb->status race condition in write sequence
  21.  * - move minor_table to a dynamic list.
  22.  *
  23.  * History:
  24.  *
  25.  * 2002_02_12 - 0.7 - zero out dev in probe function for devices that do
  26.  * not have both a bulk in and bulk out endpoint.
  27.  * Thanks to Holger Waechtler for the fix.
  28.  * 2001_11_05 - 0.6 - fix minor locking problem in skel_disconnect.
  29.  * Thanks to Pete Zaitcev for the fix.
  30.  * 2001_09_04 - 0.5 - fix devfs bug in skel_disconnect. Thanks to wim delvaux
  31.  * 2001_08_21 - 0.4 - more small bug fixes.
  32.  * 2001_05_29 - 0.3 - more bug fixes based on review from linux-usb-devel
  33.  * 2001_05_24 - 0.2 - bug fixes based on review from linux-usb-devel people
  34.  * 2001_05_01 - 0.1 - first version
  35.  * 
  36.  */
  37. #include <linux/config.h>
  38. #include <linux/kernel.h>
  39. #include <linux/sched.h>
  40. #include <linux/signal.h>
  41. #include <linux/errno.h>
  42. #include <linux/poll.h>
  43. #include <linux/init.h>
  44. #include <linux/slab.h>
  45. #include <linux/fcntl.h>
  46. #include <linux/module.h>
  47. #include <linux/spinlock.h>
  48. #include <linux/list.h>
  49. #include <linux/smp_lock.h>
  50. #include <linux/devfs_fs_kernel.h>
  51. #include <linux/usb.h>
  52. #ifdef CONFIG_USB_DEBUG
  53. static int debug = 1;
  54. #else
  55. static int debug;
  56. #endif
  57. /* Use our own dbg macro */
  58. #undef dbg
  59. #define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "n" , ## arg); } while (0)
  60. /* Version Information */
  61. #define DRIVER_VERSION "v0.4"
  62. #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com"
  63. #define DRIVER_DESC "USB Skeleton Driver"
  64. /* Module paramaters */
  65. MODULE_PARM(debug, "i");
  66. MODULE_PARM_DESC(debug, "Debug enabled or not");
  67. /* Define these values to match your device */
  68. #define USB_SKEL_VENDOR_ID 0xfff0
  69. #define USB_SKEL_PRODUCT_ID 0xfff0
  70. /* table of devices that work with this driver */
  71. static struct usb_device_id skel_table [] = {
  72. { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
  73. { } /* Terminating entry */
  74. };
  75. MODULE_DEVICE_TABLE (usb, skel_table);
  76. /* Get a minor range for your devices from the usb maintainer */
  77. #define USB_SKEL_MINOR_BASE 200
  78. /* we can have up to this number of device plugged in at once */
  79. #define MAX_DEVICES 16
  80. /* Structure to hold all of our device specific stuff */
  81. struct usb_skel {
  82. struct usb_device * udev; /* save off the usb device pointer */
  83. struct usb_interface * interface; /* the interface for this device */
  84. devfs_handle_t devfs; /* devfs device node */
  85. unsigned char minor; /* the starting minor number for this device */
  86. unsigned char num_ports; /* the number of ports this device has */
  87. char num_interrupt_in; /* number of interrupt in endpoints we have */
  88. char num_bulk_in; /* number of bulk in endpoints we have */
  89. char num_bulk_out; /* number of bulk out endpoints we have */
  90. unsigned char * bulk_in_buffer; /* the buffer to receive data */
  91. int bulk_in_size; /* the size of the receive buffer */
  92. __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
  93. unsigned char * bulk_out_buffer; /* the buffer to send data */
  94. int bulk_out_size; /* the size of the send buffer */
  95. struct urb * write_urb; /* the urb used to send data */
  96. __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
  97. struct tq_struct tqueue; /* task queue for line discipline waking up */
  98. int open_count; /* number of times this port has been opened */
  99. struct semaphore sem; /* locks this structure */
  100. };
  101. /* the global usb devfs handle */
  102. extern devfs_handle_t usb_devfs_handle;
  103. /* local function prototypes */
  104. static ssize_t skel_read (struct file *file, char *buffer, size_t count, loff_t *ppos);
  105. static ssize_t skel_write (struct file *file, const char *buffer, size_t count, loff_t *ppos);
  106. static int skel_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
  107. static int skel_open (struct inode *inode, struct file *file);
  108. static int skel_release (struct inode *inode, struct file *file);
  109. static void * skel_probe (struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *id);
  110. static void skel_disconnect (struct usb_device *dev, void *ptr);
  111. static void skel_write_bulk_callback (struct urb *urb);
  112. /* array of pointers to our devices that are currently connected */
  113. static struct usb_skel *minor_table[MAX_DEVICES];
  114. /* lock to protect the minor_table structure */
  115. static DECLARE_MUTEX (minor_table_mutex);
  116. /*
  117.  * File operations needed when we register this driver.
  118.  * This assumes that this driver NEEDS file operations,
  119.  * of course, which means that the driver is expected
  120.  * to have a node in the /dev directory. If the USB
  121.  * device were for a network interface then the driver
  122.  * would use "struct net_driver" instead, and a serial
  123.  * device would use "struct tty_driver". 
  124.  */
  125. static struct file_operations skel_fops = {
  126. /*
  127.  * The owner field is part of the module-locking
  128.  * mechanism. The idea is that the kernel knows
  129.  * which module to increment the use-counter of
  130.  * BEFORE it calls the device's open() function.
  131.  * This also means that the kernel can decrement
  132.  * the use-counter again before calling release()
  133.  * or should the open() function fail.
  134.  *
  135.  * Not all device structures have an "owner" field
  136.  * yet. "struct file_operations" and "struct net_device"
  137.  * do, while "struct tty_driver" does not. If the struct
  138.  * has an "owner" field, then initialize it to the value
  139.  * THIS_MODULE and the kernel will handle all module
  140.  * locking for you automatically. Otherwise, you must
  141.  * increment the use-counter in the open() function
  142.  * and decrement it again in the release() function
  143.  * yourself.
  144.  */
  145. owner: THIS_MODULE,
  146. read: skel_read,
  147. write: skel_write,
  148. ioctl: skel_ioctl,
  149. open: skel_open,
  150. release: skel_release,
  151. };      
  152. /* usb specific object needed to register this driver with the usb subsystem */
  153. static struct usb_driver skel_driver = {
  154. name: "skeleton",
  155. probe: skel_probe,
  156. disconnect: skel_disconnect,
  157. fops: &skel_fops,
  158. minor: USB_SKEL_MINOR_BASE,
  159. id_table: skel_table,
  160. };
  161. /**
  162.  * usb_skel_debug_data
  163.  */
  164. static inline void usb_skel_debug_data (const char *function, int size, const unsigned char *data)
  165. {
  166. int i;
  167. if (!debug)
  168. return;
  169. printk (KERN_DEBUG __FILE__": %s - length = %d, data = ", 
  170. function, size);
  171. for (i = 0; i < size; ++i) {
  172. printk ("%.2x ", data[i]);
  173. }
  174. printk ("n");
  175. }
  176. /**
  177.  * skel_delete
  178.  */
  179. static inline void skel_delete (struct usb_skel *dev)
  180. {
  181. minor_table[dev->minor] = NULL;
  182. if (dev->bulk_in_buffer != NULL)
  183. kfree (dev->bulk_in_buffer);
  184. if (dev->bulk_out_buffer != NULL)
  185. kfree (dev->bulk_out_buffer);
  186. if (dev->write_urb != NULL)
  187. usb_free_urb (dev->write_urb);
  188. kfree (dev);
  189. }
  190. /**
  191.  * skel_open
  192.  */
  193. static int skel_open (struct inode *inode, struct file *file)
  194. {
  195. struct usb_skel *dev = NULL;
  196. int subminor;
  197. int retval = 0;
  198. dbg(__FUNCTION__);
  199. subminor = MINOR (inode->i_rdev) - USB_SKEL_MINOR_BASE;
  200. if ((subminor < 0) ||
  201.     (subminor >= MAX_DEVICES)) {
  202. return -ENODEV;
  203. }
  204. /* Increment our usage count for the module.
  205.  * This is redundant here, because "struct file_operations"
  206.  * has an "owner" field. This line is included here soley as
  207.  * a reference for drivers using lesser structures... ;-)
  208.  */
  209. MOD_INC_USE_COUNT;
  210. /* lock our minor table and get our local data for this minor */
  211. down (&minor_table_mutex);
  212. dev = minor_table[subminor];
  213. if (dev == NULL) {
  214. up (&minor_table_mutex);
  215. MOD_DEC_USE_COUNT;
  216. return -ENODEV;
  217. }
  218. /* lock this device */
  219. down (&dev->sem);
  220. /* unlock the minor table */
  221. up (&minor_table_mutex);
  222. /* increment our usage count for the driver */
  223. ++dev->open_count;
  224. /* save our object in the file's private structure */
  225. file->private_data = dev;
  226. /* unlock this device */
  227. up (&dev->sem);
  228. return retval;
  229. }
  230. /**
  231.  * skel_release
  232.  */
  233. static int skel_release (struct inode *inode, struct file *file)
  234. {
  235. struct usb_skel *dev;
  236. int retval = 0;
  237. dev = (struct usb_skel *)file->private_data;
  238. if (dev == NULL) {
  239. dbg (__FUNCTION__ " - object is NULL");
  240. return -ENODEV;
  241. }
  242. dbg(__FUNCTION__ " - minor %d", dev->minor);
  243. /* lock our minor table */
  244. down (&minor_table_mutex);
  245. /* lock our device */
  246. down (&dev->sem);
  247. if (dev->open_count <= 0) {
  248. dbg (__FUNCTION__ " - device not opened");
  249. retval = -ENODEV;
  250. goto exit_not_opened;
  251. }
  252. if (dev->udev == NULL) {
  253. /* the device was unplugged before the file was released */
  254. up (&dev->sem);
  255. skel_delete (dev);
  256. up (&minor_table_mutex);
  257. MOD_DEC_USE_COUNT;
  258. return 0;
  259. }
  260. /* decrement our usage count for the device */
  261. --dev->open_count;
  262. if (dev->open_count <= 0) {
  263. /* shutdown any bulk writes that might be going on */
  264. usb_unlink_urb (dev->write_urb);
  265. dev->open_count = 0;
  266. }
  267. /* decrement our usage count for the module */
  268. MOD_DEC_USE_COUNT;
  269. exit_not_opened:
  270. up (&dev->sem);
  271. up (&minor_table_mutex);
  272. return retval;
  273. }
  274. /**
  275.  * skel_read
  276.  */
  277. static ssize_t skel_read (struct file *file, char *buffer, size_t count, loff_t *ppos)
  278. {
  279. struct usb_skel *dev;
  280. int retval = 0;
  281. dev = (struct usb_skel *)file->private_data;
  282. dbg(__FUNCTION__ " - minor %d, count = %d", dev->minor, count);
  283. /* lock this object */
  284. down (&dev->sem);
  285. /* verify that the device wasn't unplugged */
  286. if (dev->udev == NULL) {
  287. up (&dev->sem);
  288. return -ENODEV;
  289. }
  290. /* do an immediate bulk read to get data from the device */
  291. retval = usb_bulk_msg (dev->udev,
  292.        usb_rcvbulkpipe (dev->udev, 
  293. dev->bulk_in_endpointAddr),
  294.        dev->bulk_in_buffer, dev->bulk_in_size,
  295.        &count, HZ*10);
  296. /* if the read was successful, copy the data to userspace */
  297. if (!retval) {
  298. if (copy_to_user (buffer, dev->bulk_in_buffer, count))
  299. retval = -EFAULT;
  300. else
  301. retval = count;
  302. }
  303. /* unlock the device */
  304. up (&dev->sem);
  305. return retval;
  306. }
  307. /**
  308.  * skel_write
  309.  */
  310. static ssize_t skel_write (struct file *file, const char *buffer, size_t count, loff_t *ppos)
  311. {
  312. struct usb_skel *dev;
  313. ssize_t bytes_written = 0;
  314. int retval = 0;
  315. dev = (struct usb_skel *)file->private_data;
  316. dbg(__FUNCTION__ " - minor %d, count = %d", dev->minor, count);
  317. /* lock this object */
  318. down (&dev->sem);
  319. /* verify that the device wasn't unplugged */
  320. if (dev->udev == NULL) {
  321. retval = -ENODEV;
  322. goto exit;
  323. }
  324. /* verify that we actually have some data to write */
  325. if (count == 0) {
  326. dbg(__FUNCTION__ " - write request of 0 bytes");
  327. goto exit;
  328. }
  329. /* see if we are already in the middle of a write */
  330. if (dev->write_urb->status == -EINPROGRESS) {
  331. dbg (__FUNCTION__ " - already writing");
  332. goto exit;
  333. }
  334. /* we can only write as much as 1 urb will hold */
  335. bytes_written = (count > dev->bulk_out_size) ? 
  336. dev->bulk_out_size : count;
  337. /* copy the data from userspace into our urb */
  338. if (copy_from_user(dev->write_urb->transfer_buffer, buffer, 
  339.    bytes_written)) {
  340. retval = -EFAULT;
  341. goto exit;
  342. }
  343. usb_skel_debug_data (__FUNCTION__, bytes_written, 
  344.      dev->write_urb->transfer_buffer);
  345. /* set up our urb */
  346. FILL_BULK_URB(dev->write_urb, dev->udev, 
  347.       usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
  348.       dev->write_urb->transfer_buffer, bytes_written,
  349.       skel_write_bulk_callback, dev);
  350. /* send the data out the bulk port */
  351. retval = usb_submit_urb(dev->write_urb);
  352. if (retval) {
  353. err(__FUNCTION__ " - failed submitting write urb, error %d",
  354.     retval);
  355. } else {
  356. retval = bytes_written;
  357. }
  358. exit:
  359. /* unlock the device */
  360. up (&dev->sem);
  361. return retval;
  362. }
  363. /**
  364.  * skel_ioctl
  365.  */
  366. static int skel_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  367. {
  368. struct usb_skel *dev;
  369. dev = (struct usb_skel *)file->private_data;
  370. /* lock this object */
  371. down (&dev->sem);
  372. /* verify that the device wasn't unplugged */
  373. if (dev->udev == NULL) {
  374. up (&dev->sem);
  375. return -ENODEV;
  376. }
  377. dbg(__FUNCTION__ " - minor %d, cmd 0x%.4x, arg %ld", 
  378.     dev->minor, cmd, arg);
  379. /* fill in your device specific stuff here */
  380. /* unlock the device */
  381. up (&dev->sem);
  382. /* return that we did not understand this ioctl call */
  383. return -ENOTTY;
  384. }
  385. /**
  386.  * skel_write_bulk_callback
  387.  */
  388. static void skel_write_bulk_callback (struct urb *urb)
  389. {
  390. struct usb_skel *dev = (struct usb_skel *)urb->context;
  391. dbg(__FUNCTION__ " - minor %d", dev->minor);
  392. if ((urb->status != -ENOENT) && 
  393.     (urb->status != -ECONNRESET)) {
  394. dbg(__FUNCTION__ " - nonzero write bulk status received: %d",
  395.     urb->status);
  396. return;
  397. }
  398. return;
  399. }
  400. /**
  401.  * skel_probe
  402.  *
  403.  * Called by the usb core when a new device is connected that it thinks
  404.  * this driver might be interested in.
  405.  */
  406. static void * skel_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
  407. {
  408. struct usb_skel *dev = NULL;
  409. struct usb_interface *interface;
  410. struct usb_interface_descriptor *iface_desc;
  411. struct usb_endpoint_descriptor *endpoint;
  412. int minor;
  413. int buffer_size;
  414. int i;
  415. char name[10];
  416. /* See if the device offered us matches what we can accept */
  417. if ((udev->descriptor.idVendor != USB_SKEL_VENDOR_ID) ||
  418.     (udev->descriptor.idProduct != USB_SKEL_PRODUCT_ID)) {
  419. return NULL;
  420. }
  421. /* select a "subminor" number (part of a minor number) */
  422. down (&minor_table_mutex);
  423. for (minor = 0; minor < MAX_DEVICES; ++minor) {
  424. if (minor_table[minor] == NULL)
  425. break;
  426. }
  427. if (minor >= MAX_DEVICES) {
  428. info ("Too many devices plugged in, can not handle this device.");
  429. goto exit;
  430. }
  431. /* allocate memory for our device state and intialize it */
  432. dev = kmalloc (sizeof(struct usb_skel), GFP_KERNEL);
  433. if (dev == NULL) {
  434. err ("Out of memory");
  435. goto exit;
  436. }
  437. memset (dev, 0x00, sizeof (*dev));
  438. minor_table[minor] = dev;
  439. interface = &udev->actconfig->interface[ifnum];
  440. init_MUTEX (&dev->sem);
  441. dev->udev = udev;
  442. dev->interface = interface;
  443. dev->minor = minor;
  444. /* set up the endpoint information */
  445. /* check out the endpoints */
  446. iface_desc = &interface->altsetting[0];
  447. for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
  448. endpoint = &iface_desc->endpoint[i];
  449. if ((endpoint->bEndpointAddress & 0x80) &&
  450.     ((endpoint->bmAttributes & 3) == 0x02)) {
  451. /* we found a bulk in endpoint */
  452. buffer_size = endpoint->wMaxPacketSize;
  453. dev->bulk_in_size = buffer_size;
  454. dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
  455. dev->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
  456. if (!dev->bulk_in_buffer) {
  457. err("Couldn't allocate bulk_in_buffer");
  458. goto error;
  459. }
  460. }
  461. if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
  462.     ((endpoint->bmAttributes & 3) == 0x02)) {
  463. /* we found a bulk out endpoint */
  464. dev->write_urb = usb_alloc_urb(0);
  465. if (!dev->write_urb) {
  466. err("No free urbs available");
  467. goto error;
  468. }
  469. buffer_size = endpoint->wMaxPacketSize;
  470. dev->bulk_out_size = buffer_size;
  471. dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
  472. dev->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
  473. if (!dev->bulk_out_buffer) {
  474. err("Couldn't allocate bulk_out_buffer");
  475. goto error;
  476. }
  477. FILL_BULK_URB(dev->write_urb, udev, 
  478.       usb_sndbulkpipe(udev, 
  479.       endpoint->bEndpointAddress),
  480.       dev->bulk_out_buffer, buffer_size,
  481.       skel_write_bulk_callback, dev);
  482. }
  483. }
  484. /* initialize the devfs node for this device and register it */
  485. sprintf(name, "skel%d", dev->minor);
  486. dev->devfs = devfs_register (usb_devfs_handle, name,
  487.      DEVFS_FL_DEFAULT, USB_MAJOR,
  488.      USB_SKEL_MINOR_BASE + dev->minor,
  489.      S_IFCHR | S_IRUSR | S_IWUSR | 
  490.      S_IRGRP | S_IWGRP | S_IROTH, 
  491.      &skel_fops, NULL);
  492. /* let the user know what node this device is now attached to */
  493. info ("USB Skeleton device now attached to USBSkel%d", dev->minor);
  494. goto exit;
  495. error:
  496. skel_delete (dev);
  497. dev = NULL;
  498. exit:
  499. up (&minor_table_mutex);
  500. return dev;
  501. }
  502. /**
  503.  * skel_disconnect
  504.  *
  505.  * Called by the usb core when the device is removed from the system.
  506.  */
  507. static void skel_disconnect(struct usb_device *udev, void *ptr)
  508. {
  509. struct usb_skel *dev;
  510. int minor;
  511. dev = (struct usb_skel *)ptr;
  512. down (&minor_table_mutex);
  513. down (&dev->sem);
  514. minor = dev->minor;
  515. /* remove our devfs node */
  516. devfs_unregister(dev->devfs);
  517. /* if the device is not opened, then we clean up right now */
  518. if (!dev->open_count) {
  519. up (&dev->sem);
  520. skel_delete (dev);
  521. } else {
  522. dev->udev = NULL;
  523. up (&dev->sem);
  524. }
  525. info("USB Skeleton #%d now disconnected", minor);
  526. up (&minor_table_mutex);
  527. }
  528. /**
  529.  * usb_skel_init
  530.  */
  531. static int __init usb_skel_init(void)
  532. {
  533. int result;
  534. /* register this driver with the USB subsystem */
  535. result = usb_register(&skel_driver);
  536. if (result < 0) {
  537. err("usb_register failed for the "__FILE__" driver. Error number %d",
  538.     result);
  539. return -1;
  540. }
  541. info(DRIVER_DESC " " DRIVER_VERSION);
  542. return 0;
  543. }
  544. /**
  545.  * usb_skel_exit
  546.  */
  547. static void __exit usb_skel_exit(void)
  548. {
  549. /* deregister this driver with the USB subsystem */
  550. usb_deregister(&skel_driver);
  551. }
  552. module_init (usb_skel_init);
  553. module_exit (usb_skel_exit);
  554. MODULE_AUTHOR(DRIVER_AUTHOR);
  555. MODULE_DESCRIPTION(DRIVER_DESC);
  556. MODULE_LICENSE("GPL");