idmouse.c 12 KB

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