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