idmouse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* Siemens ID Mouse driver v0.6
  2. This program is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU General Public License as
  4. published by the Free Software Foundation; either version 2 of
  5. the License, or (at your option) any later version.
  6. Copyright (C) 2004-5 by Florian 'Floe' Echtler <echtler@fs.tum.de>
  7. and Andreas 'ad' Deresch <aderesch@fs.tum.de>
  8. Derived from the USB Skeleton driver 1.1,
  9. Copyright (C) 2003 Greg Kroah-Hartman (greg@kroah.com)
  10. Additional information provided by Martin Reising
  11. <Martin.Reising@natural-computing.de>
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/completion.h>
  19. #include <linux/mutex.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/usb.h>
  22. /* image constants */
  23. #define WIDTH 225
  24. #define HEIGHT 289
  25. #define HEADER "P5 225 289 255 "
  26. #define IMGSIZE ((WIDTH * HEIGHT) + sizeof(HEADER)-1)
  27. /* version information */
  28. #define DRIVER_VERSION "0.6"
  29. #define DRIVER_SHORT "idmouse"
  30. #define DRIVER_AUTHOR "Florian 'Floe' Echtler <echtler@fs.tum.de>"
  31. #define DRIVER_DESC "Siemens ID Mouse FingerTIP Sensor Driver"
  32. /* minor number for misc USB devices */
  33. #define USB_IDMOUSE_MINOR_BASE 132
  34. /* vendor and device IDs */
  35. #define ID_SIEMENS 0x0681
  36. #define ID_IDMOUSE 0x0005
  37. #define ID_CHERRY 0x0010
  38. /* device ID table */
  39. static const struct usb_device_id idmouse_table[] = {
  40. {USB_DEVICE(ID_SIEMENS, ID_IDMOUSE)}, /* Siemens ID Mouse (Professional) */
  41. {USB_DEVICE(ID_SIEMENS, ID_CHERRY )}, /* Cherry FingerTIP ID Board */
  42. {} /* terminating null entry */
  43. };
  44. /* sensor commands */
  45. #define FTIP_RESET 0x20
  46. #define FTIP_ACQUIRE 0x21
  47. #define FTIP_RELEASE 0x22
  48. #define FTIP_BLINK 0x23 /* LSB of value = blink pulse width */
  49. #define FTIP_SCROLL 0x24
  50. #define ftip_command(dev, command, value, index) \
  51. usb_control_msg (dev->udev, usb_sndctrlpipe (dev->udev, 0), command, \
  52. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT, value, index, NULL, 0, 1000)
  53. MODULE_DEVICE_TABLE(usb, idmouse_table);
  54. static DEFINE_MUTEX(open_disc_mutex);
  55. /* structure to hold all of our device specific stuff */
  56. struct usb_idmouse {
  57. struct usb_device *udev; /* save off the usb device pointer */
  58. struct usb_interface *interface; /* the interface for this device */
  59. unsigned char *bulk_in_buffer; /* the buffer to receive data */
  60. size_t bulk_in_size; /* the maximum bulk packet size */
  61. size_t orig_bi_size; /* same as above, but reported by the device */
  62. __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
  63. int open; /* if the port is open or not */
  64. int present; /* if the device is not disconnected */
  65. struct mutex lock; /* locks this structure */
  66. };
  67. /* local function prototypes */
  68. static ssize_t idmouse_read(struct file *file, char __user *buffer,
  69. size_t count, loff_t * ppos);
  70. static int idmouse_open(struct inode *inode, struct file *file);
  71. static int idmouse_release(struct inode *inode, struct file *file);
  72. static int idmouse_probe(struct usb_interface *interface,
  73. const struct usb_device_id *id);
  74. static void idmouse_disconnect(struct usb_interface *interface);
  75. static int idmouse_suspend(struct usb_interface *intf, pm_message_t message);
  76. static int idmouse_resume(struct usb_interface *intf);
  77. /* file operation pointers */
  78. static const struct file_operations idmouse_fops = {
  79. .owner = THIS_MODULE,
  80. .read = idmouse_read,
  81. .open = idmouse_open,
  82. .release = idmouse_release,
  83. .llseek = default_llseek,
  84. };
  85. /* class driver information */
  86. static struct usb_class_driver idmouse_class = {
  87. .name = "idmouse%d",
  88. .fops = &idmouse_fops,
  89. .minor_base = USB_IDMOUSE_MINOR_BASE,
  90. };
  91. /* usb specific object needed to register this driver with the usb subsystem */
  92. static struct usb_driver idmouse_driver = {
  93. .name = DRIVER_SHORT,
  94. .probe = idmouse_probe,
  95. .disconnect = idmouse_disconnect,
  96. .suspend = idmouse_suspend,
  97. .resume = idmouse_resume,
  98. .reset_resume = idmouse_resume,
  99. .id_table = idmouse_table,
  100. .supports_autosuspend = 1,
  101. };
  102. static int idmouse_create_image(struct usb_idmouse *dev)
  103. {
  104. int bytes_read;
  105. int bulk_read;
  106. int result;
  107. memcpy(dev->bulk_in_buffer, HEADER, sizeof(HEADER)-1);
  108. bytes_read = sizeof(HEADER)-1;
  109. /* reset the device and set a fast blink rate */
  110. result = ftip_command(dev, FTIP_RELEASE, 0, 0);
  111. if (result < 0)
  112. goto reset;
  113. result = ftip_command(dev, FTIP_BLINK, 1, 0);
  114. if (result < 0)
  115. goto reset;
  116. /* initialize the sensor - sending this command twice */
  117. /* significantly reduces the rate of failed reads */
  118. result = ftip_command(dev, FTIP_ACQUIRE, 0, 0);
  119. if (result < 0)
  120. goto reset;
  121. result = ftip_command(dev, FTIP_ACQUIRE, 0, 0);
  122. if (result < 0)
  123. goto reset;
  124. /* start the readout - sending this command twice */
  125. /* presumably enables the high dynamic range mode */
  126. result = ftip_command(dev, FTIP_RESET, 0, 0);
  127. if (result < 0)
  128. goto reset;
  129. result = ftip_command(dev, FTIP_RESET, 0, 0);
  130. if (result < 0)
  131. goto reset;
  132. /* loop over a blocking bulk read to get data from the device */
  133. while (bytes_read < IMGSIZE) {
  134. result = usb_bulk_msg (dev->udev,
  135. usb_rcvbulkpipe (dev->udev, dev->bulk_in_endpointAddr),
  136. dev->bulk_in_buffer + bytes_read,
  137. dev->bulk_in_size, &bulk_read, 5000);
  138. if (result < 0) {
  139. /* Maybe this error was caused by the increased packet size? */
  140. /* Reset to the original value and tell userspace to retry. */
  141. if (dev->bulk_in_size != dev->orig_bi_size) {
  142. dev->bulk_in_size = dev->orig_bi_size;
  143. result = -EAGAIN;
  144. }
  145. break;
  146. }
  147. if (signal_pending(current)) {
  148. result = -EINTR;
  149. break;
  150. }
  151. bytes_read += bulk_read;
  152. }
  153. /* reset the device */
  154. reset:
  155. ftip_command(dev, FTIP_RELEASE, 0, 0);
  156. /* check for valid image */
  157. /* right border should be black (0x00) */
  158. for (bytes_read = sizeof(HEADER)-1 + WIDTH-1; bytes_read < IMGSIZE; bytes_read += WIDTH)
  159. if (dev->bulk_in_buffer[bytes_read] != 0x00)
  160. return -EAGAIN;
  161. /* lower border should be white (0xFF) */
  162. for (bytes_read = IMGSIZE-WIDTH; bytes_read < IMGSIZE-1; bytes_read++)
  163. if (dev->bulk_in_buffer[bytes_read] != 0xFF)
  164. return -EAGAIN;
  165. /* should be IMGSIZE == 65040 */
  166. dev_dbg(&dev->interface->dev, "read %d bytes fingerprint data\n",
  167. bytes_read);
  168. return result;
  169. }
  170. /* PM operations are nops as this driver does IO only during open() */
  171. static int idmouse_suspend(struct usb_interface *intf, pm_message_t message)
  172. {
  173. return 0;
  174. }
  175. static int idmouse_resume(struct usb_interface *intf)
  176. {
  177. return 0;
  178. }
  179. static inline void idmouse_delete(struct usb_idmouse *dev)
  180. {
  181. kfree(dev->bulk_in_buffer);
  182. kfree(dev);
  183. }
  184. static int idmouse_open(struct inode *inode, struct file *file)
  185. {
  186. struct usb_idmouse *dev;
  187. struct usb_interface *interface;
  188. int result;
  189. /* get the interface from minor number and driver information */
  190. interface = usb_find_interface (&idmouse_driver, iminor (inode));
  191. if (!interface)
  192. return -ENODEV;
  193. mutex_lock(&open_disc_mutex);
  194. /* get the device information block from the interface */
  195. dev = usb_get_intfdata(interface);
  196. if (!dev) {
  197. mutex_unlock(&open_disc_mutex);
  198. return -ENODEV;
  199. }
  200. /* lock this device */
  201. mutex_lock(&dev->lock);
  202. mutex_unlock(&open_disc_mutex);
  203. /* check if already open */
  204. if (dev->open) {
  205. /* already open, so fail */
  206. result = -EBUSY;
  207. } else {
  208. /* create a new image and check for success */
  209. result = usb_autopm_get_interface(interface);
  210. if (result)
  211. goto error;
  212. result = idmouse_create_image (dev);
  213. if (result)
  214. goto error;
  215. usb_autopm_put_interface(interface);
  216. /* increment our usage count for the driver */
  217. ++dev->open;
  218. /* save our object in the file's private structure */
  219. file->private_data = dev;
  220. }
  221. error:
  222. /* unlock this device */
  223. mutex_unlock(&dev->lock);
  224. return result;
  225. }
  226. static int idmouse_release(struct inode *inode, struct file *file)
  227. {
  228. struct usb_idmouse *dev;
  229. dev = file->private_data;
  230. if (dev == NULL)
  231. return -ENODEV;
  232. mutex_lock(&open_disc_mutex);
  233. /* lock our device */
  234. mutex_lock(&dev->lock);
  235. /* are we really open? */
  236. if (dev->open <= 0) {
  237. mutex_unlock(&dev->lock);
  238. mutex_unlock(&open_disc_mutex);
  239. return -ENODEV;
  240. }
  241. --dev->open;
  242. if (!dev->present) {
  243. /* the device was unplugged before the file was released */
  244. mutex_unlock(&dev->lock);
  245. mutex_unlock(&open_disc_mutex);
  246. idmouse_delete(dev);
  247. } else {
  248. mutex_unlock(&dev->lock);
  249. mutex_unlock(&open_disc_mutex);
  250. }
  251. return 0;
  252. }
  253. static ssize_t idmouse_read(struct file *file, char __user *buffer, size_t count,
  254. loff_t * ppos)
  255. {
  256. struct usb_idmouse *dev = file->private_data;
  257. int result;
  258. /* lock this object */
  259. mutex_lock(&dev->lock);
  260. /* verify that the device wasn't unplugged */
  261. if (!dev->present) {
  262. mutex_unlock(&dev->lock);
  263. return -ENODEV;
  264. }
  265. result = simple_read_from_buffer(buffer, count, ppos,
  266. dev->bulk_in_buffer, IMGSIZE);
  267. /* unlock the device */
  268. mutex_unlock(&dev->lock);
  269. return result;
  270. }
  271. static int idmouse_probe(struct usb_interface *interface,
  272. const struct usb_device_id *id)
  273. {
  274. struct usb_device *udev = interface_to_usbdev(interface);
  275. struct usb_idmouse *dev;
  276. struct usb_host_interface *iface_desc;
  277. struct usb_endpoint_descriptor *endpoint;
  278. int result;
  279. /* check if we have gotten the data or the hid interface */
  280. iface_desc = &interface->altsetting[0];
  281. if (iface_desc->desc.bInterfaceClass != 0x0A)
  282. return -ENODEV;
  283. if (iface_desc->desc.bNumEndpoints < 1)
  284. return -ENODEV;
  285. /* allocate memory for our device state and initialize it */
  286. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  287. if (dev == NULL)
  288. return -ENOMEM;
  289. mutex_init(&dev->lock);
  290. dev->udev = udev;
  291. dev->interface = interface;
  292. /* set up the endpoint information - use only the first bulk-in endpoint */
  293. endpoint = &iface_desc->endpoint[0].desc;
  294. if (!dev->bulk_in_endpointAddr && usb_endpoint_is_bulk_in(endpoint)) {
  295. /* we found a bulk in endpoint */
  296. dev->orig_bi_size = usb_endpoint_maxp(endpoint);
  297. dev->bulk_in_size = 0x200; /* works _much_ faster */
  298. dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
  299. dev->bulk_in_buffer =
  300. kmalloc(IMGSIZE + dev->bulk_in_size, GFP_KERNEL);
  301. if (!dev->bulk_in_buffer) {
  302. dev_err(&interface->dev, "Unable to allocate input buffer.\n");
  303. idmouse_delete(dev);
  304. return -ENOMEM;
  305. }
  306. }
  307. if (!(dev->bulk_in_endpointAddr)) {
  308. dev_err(&interface->dev, "Unable to find bulk-in endpoint.\n");
  309. idmouse_delete(dev);
  310. return -ENODEV;
  311. }
  312. /* allow device read, write and ioctl */
  313. dev->present = 1;
  314. /* we can register the device now, as it is ready */
  315. usb_set_intfdata(interface, dev);
  316. result = usb_register_dev(interface, &idmouse_class);
  317. if (result) {
  318. /* something prevented us from registering this device */
  319. dev_err(&interface->dev, "Unable to allocate minor number.\n");
  320. usb_set_intfdata(interface, NULL);
  321. idmouse_delete(dev);
  322. return result;
  323. }
  324. /* be noisy */
  325. dev_info(&interface->dev,"%s now attached\n",DRIVER_DESC);
  326. return 0;
  327. }
  328. static void idmouse_disconnect(struct usb_interface *interface)
  329. {
  330. struct usb_idmouse *dev;
  331. /* get device structure */
  332. dev = usb_get_intfdata(interface);
  333. /* give back our minor */
  334. usb_deregister_dev(interface, &idmouse_class);
  335. mutex_lock(&open_disc_mutex);
  336. usb_set_intfdata(interface, NULL);
  337. /* lock the device */
  338. mutex_lock(&dev->lock);
  339. mutex_unlock(&open_disc_mutex);
  340. /* prevent device read, write and ioctl */
  341. dev->present = 0;
  342. /* if the device is opened, idmouse_release will clean this up */
  343. if (!dev->open) {
  344. mutex_unlock(&dev->lock);
  345. idmouse_delete(dev);
  346. } else {
  347. /* unlock */
  348. mutex_unlock(&dev->lock);
  349. }
  350. dev_info(&interface->dev, "disconnected\n");
  351. }
  352. module_usb_driver(idmouse_driver);
  353. MODULE_AUTHOR(DRIVER_AUTHOR);
  354. MODULE_DESCRIPTION(DRIVER_DESC);
  355. MODULE_LICENSE("GPL");