1 /* Siemens ID Mouse driver v0.6
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License as
5 published by the Free Software Foundation; either version 2 of
6 the License, or (at your option) any later version.
8 Copyright (C) 2004-5 by Florian 'Floe' Echtler <echtler@fs.tum.de>
9 and Andreas 'ad' Deresch <aderesch@fs.tum.de>
11 Derived from the USB Skeleton driver 1.1,
12 Copyright (C) 2003 Greg Kroah-Hartman (greg@kroah.com)
14 Additional information provided by Martin Reising
15 <Martin.Reising@natural-computing.de>
19 #include <linux/kernel.h>
20 #include <linux/sched/signal.h>
21 #include <linux/errno.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/completion.h>
26 #include <linux/mutex.h>
27 #include <linux/uaccess.h>
28 #include <linux/usb.h>
33 #define HEADER "P5 225 289 255 "
34 #define IMGSIZE ((WIDTH * HEIGHT) + sizeof(HEADER)-1)
36 #define DRIVER_SHORT "idmouse"
37 #define DRIVER_AUTHOR "Florian 'Floe' Echtler <echtler@fs.tum.de>"
38 #define DRIVER_DESC "Siemens ID Mouse FingerTIP Sensor Driver"
40 /* minor number for misc USB devices */
41 #define USB_IDMOUSE_MINOR_BASE 132
43 /* vendor and device IDs */
44 #define ID_SIEMENS 0x0681
45 #define ID_IDMOUSE 0x0005
46 #define ID_CHERRY 0x0010
49 static const struct usb_device_id idmouse_table[] = {
50 {USB_DEVICE(ID_SIEMENS, ID_IDMOUSE)}, /* Siemens ID Mouse (Professional) */
51 {USB_DEVICE(ID_SIEMENS, ID_CHERRY )}, /* Cherry FingerTIP ID Board */
52 {} /* terminating null entry */
56 #define FTIP_RESET 0x20
57 #define FTIP_ACQUIRE 0x21
58 #define FTIP_RELEASE 0x22
59 #define FTIP_BLINK 0x23 /* LSB of value = blink pulse width */
60 #define FTIP_SCROLL 0x24
62 #define ftip_command(dev, command, value, index) \
63 usb_control_msg (dev->udev, usb_sndctrlpipe (dev->udev, 0), command, \
64 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT, value, index, NULL, 0, 1000)
66 MODULE_DEVICE_TABLE(usb, idmouse_table);
67 static DEFINE_MUTEX(open_disc_mutex);
69 /* structure to hold all of our device specific stuff */
72 struct usb_device *udev; /* save off the usb device pointer */
73 struct usb_interface *interface; /* the interface for this device */
75 unsigned char *bulk_in_buffer; /* the buffer to receive data */
76 size_t bulk_in_size; /* the maximum bulk packet size */
77 size_t orig_bi_size; /* same as above, but reported by the device */
78 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
80 int open; /* if the port is open or not */
81 int present; /* if the device is not disconnected */
82 struct mutex lock; /* locks this structure */
86 /* local function prototypes */
87 static ssize_t idmouse_read(struct file *file, char __user *buffer,
88 size_t count, loff_t * ppos);
90 static int idmouse_open(struct inode *inode, struct file *file);
91 static int idmouse_release(struct inode *inode, struct file *file);
93 static int idmouse_probe(struct usb_interface *interface,
94 const struct usb_device_id *id);
96 static void idmouse_disconnect(struct usb_interface *interface);
97 static int idmouse_suspend(struct usb_interface *intf, pm_message_t message);
98 static int idmouse_resume(struct usb_interface *intf);
100 /* file operation pointers */
101 static const struct file_operations idmouse_fops = {
102 .owner = THIS_MODULE,
103 .read = idmouse_read,
104 .open = idmouse_open,
105 .release = idmouse_release,
106 .llseek = default_llseek,
109 /* class driver information */
110 static struct usb_class_driver idmouse_class = {
112 .fops = &idmouse_fops,
113 .minor_base = USB_IDMOUSE_MINOR_BASE,
116 /* usb specific object needed to register this driver with the usb subsystem */
117 static struct usb_driver idmouse_driver = {
118 .name = DRIVER_SHORT,
119 .probe = idmouse_probe,
120 .disconnect = idmouse_disconnect,
121 .suspend = idmouse_suspend,
122 .resume = idmouse_resume,
123 .reset_resume = idmouse_resume,
124 .id_table = idmouse_table,
125 .supports_autosuspend = 1,
128 static int idmouse_create_image(struct usb_idmouse *dev)
134 memcpy(dev->bulk_in_buffer, HEADER, sizeof(HEADER)-1);
135 bytes_read = sizeof(HEADER)-1;
137 /* reset the device and set a fast blink rate */
138 result = ftip_command(dev, FTIP_RELEASE, 0, 0);
141 result = ftip_command(dev, FTIP_BLINK, 1, 0);
145 /* initialize the sensor - sending this command twice */
146 /* significantly reduces the rate of failed reads */
147 result = ftip_command(dev, FTIP_ACQUIRE, 0, 0);
150 result = ftip_command(dev, FTIP_ACQUIRE, 0, 0);
154 /* start the readout - sending this command twice */
155 /* presumably enables the high dynamic range mode */
156 result = ftip_command(dev, FTIP_RESET, 0, 0);
159 result = ftip_command(dev, FTIP_RESET, 0, 0);
163 /* loop over a blocking bulk read to get data from the device */
164 while (bytes_read < IMGSIZE) {
165 result = usb_bulk_msg (dev->udev,
166 usb_rcvbulkpipe (dev->udev, dev->bulk_in_endpointAddr),
167 dev->bulk_in_buffer + bytes_read,
168 dev->bulk_in_size, &bulk_read, 5000);
170 /* Maybe this error was caused by the increased packet size? */
171 /* Reset to the original value and tell userspace to retry. */
172 if (dev->bulk_in_size != dev->orig_bi_size) {
173 dev->bulk_in_size = dev->orig_bi_size;
178 if (signal_pending(current)) {
182 bytes_read += bulk_read;
185 /* reset the device */
187 ftip_command(dev, FTIP_RELEASE, 0, 0);
189 /* check for valid image */
190 /* right border should be black (0x00) */
191 for (bytes_read = sizeof(HEADER)-1 + WIDTH-1; bytes_read < IMGSIZE; bytes_read += WIDTH)
192 if (dev->bulk_in_buffer[bytes_read] != 0x00)
195 /* lower border should be white (0xFF) */
196 for (bytes_read = IMGSIZE-WIDTH; bytes_read < IMGSIZE-1; bytes_read++)
197 if (dev->bulk_in_buffer[bytes_read] != 0xFF)
200 /* should be IMGSIZE == 65040 */
201 dev_dbg(&dev->interface->dev, "read %d bytes fingerprint data\n",
206 /* PM operations are nops as this driver does IO only during open() */
207 static int idmouse_suspend(struct usb_interface *intf, pm_message_t message)
212 static int idmouse_resume(struct usb_interface *intf)
217 static inline void idmouse_delete(struct usb_idmouse *dev)
219 kfree(dev->bulk_in_buffer);
223 static int idmouse_open(struct inode *inode, struct file *file)
225 struct usb_idmouse *dev;
226 struct usb_interface *interface;
229 /* get the interface from minor number and driver information */
230 interface = usb_find_interface (&idmouse_driver, iminor (inode));
234 mutex_lock(&open_disc_mutex);
235 /* get the device information block from the interface */
236 dev = usb_get_intfdata(interface);
238 mutex_unlock(&open_disc_mutex);
242 /* lock this device */
243 mutex_lock(&dev->lock);
244 mutex_unlock(&open_disc_mutex);
246 /* check if already open */
249 /* already open, so fail */
254 /* create a new image and check for success */
255 result = usb_autopm_get_interface(interface);
258 result = idmouse_create_image (dev);
259 usb_autopm_put_interface(interface);
263 /* increment our usage count for the driver */
266 /* save our object in the file's private structure */
267 file->private_data = dev;
273 /* unlock this device */
274 mutex_unlock(&dev->lock);
278 static int idmouse_release(struct inode *inode, struct file *file)
280 struct usb_idmouse *dev;
282 dev = file->private_data;
287 mutex_lock(&open_disc_mutex);
288 /* lock our device */
289 mutex_lock(&dev->lock);
291 /* are we really open? */
292 if (dev->open <= 0) {
293 mutex_unlock(&dev->lock);
294 mutex_unlock(&open_disc_mutex);
301 /* the device was unplugged before the file was released */
302 mutex_unlock(&dev->lock);
303 mutex_unlock(&open_disc_mutex);
306 mutex_unlock(&dev->lock);
307 mutex_unlock(&open_disc_mutex);
312 static ssize_t idmouse_read(struct file *file, char __user *buffer, size_t count,
315 struct usb_idmouse *dev = file->private_data;
318 /* lock this object */
319 mutex_lock(&dev->lock);
321 /* verify that the device wasn't unplugged */
323 mutex_unlock(&dev->lock);
327 result = simple_read_from_buffer(buffer, count, ppos,
328 dev->bulk_in_buffer, IMGSIZE);
329 /* unlock the device */
330 mutex_unlock(&dev->lock);
334 static int idmouse_probe(struct usb_interface *interface,
335 const struct usb_device_id *id)
337 struct usb_device *udev = interface_to_usbdev(interface);
338 struct usb_idmouse *dev;
339 struct usb_host_interface *iface_desc;
340 struct usb_endpoint_descriptor *endpoint;
343 /* check if we have gotten the data or the hid interface */
344 iface_desc = interface->cur_altsetting;
345 if (iface_desc->desc.bInterfaceClass != 0x0A)
348 if (iface_desc->desc.bNumEndpoints < 1)
351 /* allocate memory for our device state and initialize it */
352 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
356 mutex_init(&dev->lock);
358 dev->interface = interface;
360 /* set up the endpoint information - use only the first bulk-in endpoint */
361 result = usb_find_bulk_in_endpoint(iface_desc, &endpoint);
363 dev_err(&interface->dev, "Unable to find bulk-in endpoint.\n");
368 dev->orig_bi_size = usb_endpoint_maxp(endpoint);
369 dev->bulk_in_size = 0x200; /* works _much_ faster */
370 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
371 dev->bulk_in_buffer = kmalloc(IMGSIZE + dev->bulk_in_size, GFP_KERNEL);
372 if (!dev->bulk_in_buffer) {
377 /* allow device read, write and ioctl */
380 /* we can register the device now, as it is ready */
381 usb_set_intfdata(interface, dev);
382 result = usb_register_dev(interface, &idmouse_class);
384 /* something prevented us from registering this device */
385 dev_err(&interface->dev, "Unable to allocate minor number.\n");
386 usb_set_intfdata(interface, NULL);
392 dev_info(&interface->dev,"%s now attached\n",DRIVER_DESC);
397 static void idmouse_disconnect(struct usb_interface *interface)
399 struct usb_idmouse *dev;
401 /* get device structure */
402 dev = usb_get_intfdata(interface);
404 /* give back our minor */
405 usb_deregister_dev(interface, &idmouse_class);
407 mutex_lock(&open_disc_mutex);
408 usb_set_intfdata(interface, NULL);
409 /* lock the device */
410 mutex_lock(&dev->lock);
411 mutex_unlock(&open_disc_mutex);
413 /* prevent device read, write and ioctl */
416 /* if the device is opened, idmouse_release will clean this up */
418 mutex_unlock(&dev->lock);
422 mutex_unlock(&dev->lock);
425 dev_info(&interface->dev, "disconnected\n");
428 module_usb_driver(idmouse_driver);
430 MODULE_AUTHOR(DRIVER_AUTHOR);
431 MODULE_DESCRIPTION(DRIVER_DESC);
432 MODULE_LICENSE("GPL");