idmouse.c 12 KB

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