idmouse.c 11 KB

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