idmouse.c 11 KB

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