idmouse.c 11 KB

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