blob: 01ef2551be46c951800d24c97dfe30b292e29636 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* Siemens ID Mouse driver v0.6
2
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.
7
8 Copyright (C) 2004-5 by Florian 'Floe' Echtler <echtler@fs.tum.de>
9 and Andreas 'ad' Deresch <aderesch@fs.tum.de>
10
11 Derived from the USB Skeleton driver 1.1,
12 Copyright (C) 2003 Greg Kroah-Hartman (greg@kroah.com)
13
14 Additional information provided by Martin Reising
15 <Martin.Reising@natural-computing.de>
16
17*/
18
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>
29
30/* image constants */
31#define WIDTH 225
32#define HEIGHT 289
33#define HEADER "P5 225 289 255 "
34#define IMGSIZE ((WIDTH * HEIGHT) + sizeof(HEADER)-1)
35
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"
39
40/* minor number for misc USB devices */
41#define USB_IDMOUSE_MINOR_BASE 132
42
43/* vendor and device IDs */
44#define ID_SIEMENS 0x0681
45#define ID_IDMOUSE 0x0005
46#define ID_CHERRY 0x0010
47
48/* device ID table */
49static 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 */
53};
54
55/* sensor commands */
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
61
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)
65
66MODULE_DEVICE_TABLE(usb, idmouse_table);
67static DEFINE_MUTEX(open_disc_mutex);
68
69/* structure to hold all of our device specific stuff */
70struct usb_idmouse {
71
72 struct usb_device *udev; /* save off the usb device pointer */
73 struct usb_interface *interface; /* the interface for this device */
74
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 */
79
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 */
83
84};
85
86/* local function prototypes */
87static ssize_t idmouse_read(struct file *file, char __user *buffer,
88 size_t count, loff_t * ppos);
89
90static int idmouse_open(struct inode *inode, struct file *file);
91static int idmouse_release(struct inode *inode, struct file *file);
92
93static int idmouse_probe(struct usb_interface *interface,
94 const struct usb_device_id *id);
95
96static void idmouse_disconnect(struct usb_interface *interface);
97static int idmouse_suspend(struct usb_interface *intf, pm_message_t message);
98static int idmouse_resume(struct usb_interface *intf);
99
100/* file operation pointers */
101static 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,
107};
108
109/* class driver information */
110static struct usb_class_driver idmouse_class = {
111 .name = "idmouse%d",
112 .fops = &idmouse_fops,
113 .minor_base = USB_IDMOUSE_MINOR_BASE,
114};
115
116/* usb specific object needed to register this driver with the usb subsystem */
117static 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,
126};
127
128static int idmouse_create_image(struct usb_idmouse *dev)
129{
130 int bytes_read;
131 int bulk_read;
132 int result;
133
134 memcpy(dev->bulk_in_buffer, HEADER, sizeof(HEADER)-1);
135 bytes_read = sizeof(HEADER)-1;
136
137 /* reset the device and set a fast blink rate */
138 result = ftip_command(dev, FTIP_RELEASE, 0, 0);
139 if (result < 0)
140 goto reset;
141 result = ftip_command(dev, FTIP_BLINK, 1, 0);
142 if (result < 0)
143 goto reset;
144
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);
148 if (result < 0)
149 goto reset;
150 result = ftip_command(dev, FTIP_ACQUIRE, 0, 0);
151 if (result < 0)
152 goto reset;
153
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);
157 if (result < 0)
158 goto reset;
159 result = ftip_command(dev, FTIP_RESET, 0, 0);
160 if (result < 0)
161 goto reset;
162
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);
169 if (result < 0) {
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;
174 result = -EAGAIN;
175 }
176 break;
177 }
178 if (signal_pending(current)) {
179 result = -EINTR;
180 break;
181 }
182 bytes_read += bulk_read;
183 }
184
185 /* reset the device */
186reset:
187 ftip_command(dev, FTIP_RELEASE, 0, 0);
188
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)
193 return -EAGAIN;
194
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)
198 return -EAGAIN;
199
200 /* should be IMGSIZE == 65040 */
201 dev_dbg(&dev->interface->dev, "read %d bytes fingerprint data\n",
202 bytes_read);
203 return result;
204}
205
206/* PM operations are nops as this driver does IO only during open() */
207static int idmouse_suspend(struct usb_interface *intf, pm_message_t message)
208{
209 return 0;
210}
211
212static int idmouse_resume(struct usb_interface *intf)
213{
214 return 0;
215}
216
217static inline void idmouse_delete(struct usb_idmouse *dev)
218{
219 kfree(dev->bulk_in_buffer);
220 kfree(dev);
221}
222
223static int idmouse_open(struct inode *inode, struct file *file)
224{
225 struct usb_idmouse *dev;
226 struct usb_interface *interface;
227 int result;
228
229 /* get the interface from minor number and driver information */
230 interface = usb_find_interface (&idmouse_driver, iminor (inode));
231 if (!interface)
232 return -ENODEV;
233
234 mutex_lock(&open_disc_mutex);
235 /* get the device information block from the interface */
236 dev = usb_get_intfdata(interface);
237 if (!dev) {
238 mutex_unlock(&open_disc_mutex);
239 return -ENODEV;
240 }
241
242 /* lock this device */
243 mutex_lock(&dev->lock);
244 mutex_unlock(&open_disc_mutex);
245
246 /* check if already open */
247 if (dev->open) {
248
249 /* already open, so fail */
250 result = -EBUSY;
251
252 } else {
253
254 /* create a new image and check for success */
255 result = usb_autopm_get_interface(interface);
256 if (result)
257 goto error;
258 result = idmouse_create_image (dev);
259 usb_autopm_put_interface(interface);
260 if (result)
261 goto error;
262
263 /* increment our usage count for the driver */
264 ++dev->open;
265
266 /* save our object in the file's private structure */
267 file->private_data = dev;
268
269 }
270
271error:
272
273 /* unlock this device */
274 mutex_unlock(&dev->lock);
275 return result;
276}
277
278static int idmouse_release(struct inode *inode, struct file *file)
279{
280 struct usb_idmouse *dev;
281
282 dev = file->private_data;
283
284 if (dev == NULL)
285 return -ENODEV;
286
287 mutex_lock(&open_disc_mutex);
288 /* lock our device */
289 mutex_lock(&dev->lock);
290
291 /* are we really open? */
292 if (dev->open <= 0) {
293 mutex_unlock(&dev->lock);
294 mutex_unlock(&open_disc_mutex);
295 return -ENODEV;
296 }
297
298 --dev->open;
299
300 if (!dev->present) {
301 /* the device was unplugged before the file was released */
302 mutex_unlock(&dev->lock);
303 mutex_unlock(&open_disc_mutex);
304 idmouse_delete(dev);
305 } else {
306 mutex_unlock(&dev->lock);
307 mutex_unlock(&open_disc_mutex);
308 }
309 return 0;
310}
311
312static ssize_t idmouse_read(struct file *file, char __user *buffer, size_t count,
313 loff_t * ppos)
314{
315 struct usb_idmouse *dev = file->private_data;
316 int result;
317
318 /* lock this object */
319 mutex_lock(&dev->lock);
320
321 /* verify that the device wasn't unplugged */
322 if (!dev->present) {
323 mutex_unlock(&dev->lock);
324 return -ENODEV;
325 }
326
327 result = simple_read_from_buffer(buffer, count, ppos,
328 dev->bulk_in_buffer, IMGSIZE);
329 /* unlock the device */
330 mutex_unlock(&dev->lock);
331 return result;
332}
333
334static int idmouse_probe(struct usb_interface *interface,
335 const struct usb_device_id *id)
336{
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;
341 int result;
342
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)
346 return -ENODEV;
347
348 if (iface_desc->desc.bNumEndpoints < 1)
349 return -ENODEV;
350
351 /* allocate memory for our device state and initialize it */
352 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
353 if (dev == NULL)
354 return -ENOMEM;
355
356 mutex_init(&dev->lock);
357 dev->udev = udev;
358 dev->interface = interface;
359
360 /* set up the endpoint information - use only the first bulk-in endpoint */
361 result = usb_find_bulk_in_endpoint(iface_desc, &endpoint);
362 if (result) {
363 dev_err(&interface->dev, "Unable to find bulk-in endpoint.\n");
364 idmouse_delete(dev);
365 return result;
366 }
367
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) {
373 idmouse_delete(dev);
374 return -ENOMEM;
375 }
376
377 /* allow device read, write and ioctl */
378 dev->present = 1;
379
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);
383 if (result) {
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);
387 idmouse_delete(dev);
388 return result;
389 }
390
391 /* be noisy */
392 dev_info(&interface->dev,"%s now attached\n",DRIVER_DESC);
393
394 return 0;
395}
396
397static void idmouse_disconnect(struct usb_interface *interface)
398{
399 struct usb_idmouse *dev;
400
401 /* get device structure */
402 dev = usb_get_intfdata(interface);
403
404 /* give back our minor */
405 usb_deregister_dev(interface, &idmouse_class);
406
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);
412
413 /* prevent device read, write and ioctl */
414 dev->present = 0;
415
416 /* if the device is opened, idmouse_release will clean this up */
417 if (!dev->open) {
418 mutex_unlock(&dev->lock);
419 idmouse_delete(dev);
420 } else {
421 /* unlock */
422 mutex_unlock(&dev->lock);
423 }
424
425 dev_info(&interface->dev, "disconnected\n");
426}
427
428module_usb_driver(idmouse_driver);
429
430MODULE_AUTHOR(DRIVER_AUTHOR);
431MODULE_DESCRIPTION(DRIVER_DESC);
432MODULE_LICENSE("GPL");
433