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