4 * Driver for USB Scanners (linux-2.4)
6 * Copyright (C) 1999, 2000, 2001, 2002 David E. Nelson
7 * Copyright (C) 2002, 2003 Henning Meier-Geinitz
9 * Portions may be copyright Brad Keryan and Michael Gee.
11 * Previously maintained by Brian Beattie
13 * Current maintainer: Henning Meier-Geinitz <henning@meier-geinitz.de>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of the
18 * License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Originally based upon mouse.c (Brad Keryan) and printer.c (Michael Gee).
35 * Developed/tested using linux-2.3.15 with minor ohci.c changes to
36 * support short packes during bulk xfer mode. Some testing was
37 * done with ohci-hcd but the performace was low. Very limited
38 * testing was performed with uhci but I was unable to get it to
39 * work. Initial relase to the linux-usb development effort.
44 * - Device can't be opened unless a scanner is plugged into the USB.
45 * - Finally settled on a reasonable value for the I/O buffer's.
46 * - Cleaned up write_scanner()
47 * - Disabled read/write stats
48 * - A little more code cleanup
53 * - Device registration changed to reflect new device
54 * allocation/registration for linux-2.3.22+.
55 * - Adopted David Brownell's <david-b@pacbell.net> technique for
56 * assigning bulk endpoints.
57 * - Removed unnessesary #include's
58 * - Scanner model now reported via syslog INFO after being detected
60 * - Added user specified vendor:product USB ID's which can be passed
61 * as module parameters.
66 * - Applied patches for linux-2.3.25.
67 * - Error number reporting changed to reflect negative return codes.
72 * - Applied patches for linux-2.3.26 to scanner_init().
73 * - Debug read/write stats now report values as signed decimal.
78 * - Updated the bulk_msg() calls to usb usb_bulk_msg().
79 * - Added a small delay in the write_scanner() method to aid in
80 * avoiding NULL data reads on HP scanners. We'll see how this works.
81 * - Return values from usb_bulk_msg() now ignore positive values for
82 * use with the ohci driver.
83 * - Added conditional debugging instead of commenting/uncommenting
85 * - kfree()'d the pointer after using usb_string() as documented in
87 * - Added usb_set_configuration(). It got lost in version 0.3 -- ack!
88 * - Added the HP 5200C USB Vendor/Product ID's.
93 * - Added Greg K-H's <greg@kroah.com> patch for better handling of
94 * Product/Vendor detection.
95 * - The driver now autoconfigures its endpoints including interrupt
96 * endpoints if one is detected. The concept was originally based
97 * upon David Brownell's method.
98 * - Added some Seiko/Epson ID's. Thanks to Karl Heinz
99 * Kremer <khk@khk.net>.
100 * - Added some preliminary ioctl() calls for the PV8630 which is used
101 * by the HP4200. The ioctl()'s still have to be registered. Thanks
102 * to Adrian Perez Jorge <adrianpj@easynews.com>.
103 * - Moved/migrated stuff to scanner.h
104 * - Removed the usb_set_configuration() since this is handled by
105 * the usb_new_device() routine in usb.c.
106 * - Added the HP 3300C. Thanks to Bruce Tenison.
107 * - Changed user specified vendor/product id so that root hub doesn't
108 * get falsely attached to. Thanks to Greg K-H.
109 * - Added some Mustek ID's. Thanks to Gernot Hoyler
110 * <Dr.Hoyler@t-online.de>.
111 * - Modified the usb_string() reporting. See kfree() comment above.
112 * - Added Umax Astra 2000U. Thanks to Doug Alcorn <doug@lathi.net>.
113 * - Updated the printk()'s to use the info/warn/dbg macros.
114 * - Updated usb_bulk_msg() argument types to fix gcc warnings.
119 * - Removed usb_string() from probe_scanner since the core now does a
120 * good job of reporting what was connnected.
121 * - Finally, simultaneous multiple device attachment!
122 * - Fixed some potential memory freeing issues should memory allocation
123 * fail in probe_scanner();
124 * - Some fixes to disconnect_scanner().
125 * - Added interrupt endpoint support.
126 * - Added Agfa SnapScan Touch. Thanks to Jan Van den Bergh
127 * <jan.vandenbergh@cs.kuleuven.ac.be>.
128 * - Added Umax 1220U ID's. Thanks to Maciek Klimkowski
129 * <mac@nexus.carleton.ca>.
130 * - Fixed bug in write_scanner(). The buffer was not being properly
131 * updated for writes larger than OBUF_SIZE. Thanks to Henrik
132 * Johansson <henrikjo@post.utfors.se> for identifying it.
133 * - Added Microtek X6 ID's. Thanks to Oliver Neukum
134 * <Oliver.Neukum@lrz.uni-muenchen.de>.
139 * - Fixed 'count' bug in read_scanner(). Thanks to Henrik
140 * Johansson <henrikjo@post.utfors.se> for identifying it. Amazing
141 * it has worked this long.
142 * - Fixed '>=' bug in both read/write_scanner methods.
143 * - Cleaned up both read/write_scanner() methods so that they are
144 * a little more readable.
145 * - Added a lot of Microtek ID's. Thanks to Adrian Perez Jorge.
146 * - Adopted the __initcall().
147 * - Added #include <linux/init.h> to scanner.h for __initcall().
148 * - Added one liner in irq_scanner() to keep gcc from complaining
149 * about an unused variable (data) if debugging was disabled
151 * - Increased the timeout parameter in read_scanner() to 120 Secs.
156 * - Added Umax 1236U ID. Thanks to Philipp Baer <ph_baer@npw.net>.
157 * - Added Primax, ReadyScan, Visioneer, Colorado, and Genius ID's.
158 * Thanks to Adrian Perez Jorge <adrianpj@easynews.com>.
159 * - Fixed error number reported for non-existant devices. Thanks to
160 * Spyridon Papadimitriou <Spyridon_Papadimitriou@gs91.sp.cs.cmu.edu>.
161 * - Added Acer Prisascan 620U ID's. Thanks to Joao <joey@knoware.nl>.
162 * - Replaced __initcall() with module_init()/module_exit(). Updates
164 * - Replaced file_operations structure with new syntax. Updates
166 * - Changed #include "usb.h" to #include <linux/usb.h>
167 * - Added #define SCN_IOCTL to exclude development areas
168 * since 2.4.x is about to be released. This mainly affects the
169 * ioctl() stuff. See scanner.h for more details.
170 * - Changed the return value for signal_pending() from -ERESTARTSYS to
176 * - Added Umax Astra 2200 ID. Thanks to Flynn Marquardt
177 * <flynn@isr.uni-stuttgart.de>.
178 * - Added iVina 1200U ID. Thanks to Dyson Lin <dyson@avision.com.tw>.
179 * - Added access time update for the device file courtesy of Paul
180 * Mackerras <paulus@samba.org>. This allows a user space daemon
181 * to turn the lamp off for a Umax 1220U scanner after a prescribed
183 * - Fixed HP S20 ID's. Thanks to Ruud Linders <rlinders@xs4all.nl>.
184 * - Added Acer ScanPrisa 620U ID. Thanks to Oliver
185 * Schwartz <Oliver.Schwartz@gmx.de> via sane-devel mail list.
186 * - Fixed bug in read_scanner for copy_to_user() function. The returned
187 * value should be 'partial' not 'this_read'.
188 * - Fixed bug in read_scanner. 'count' should be decremented
189 * by 'this_read' and not by 'partial'. This resulted in twice as many
190 * calls to read_scanner() for small amounts of data and possibly
191 * unexpected returns of '0'. Thanks to Karl Heinz
192 * Kremer <khk@khk.net> and Alain Knaff <Alain.Knaff@ltnb.lu>
193 * for discovering this.
194 * - Integrated Randy Dunlap's <randy.dunlap@intel.com> patch for a
195 * scanner lookup/ident table. Thanks Randy.
196 * - Documentation updates.
197 * - Added wait queues to read_scanner().
202 * - Fixed HP S20 ID's...again..sigh. Thanks to Ruud
203 * Linders <rlinders@xs4all.nl>.
206 * - Added addtional Mustek ID's (BearPaw 1200, 600 CU, 1200 USB,
207 * and 1200 UB. Thanks to Henning Meier-Geinitz <henningmg@gmx.de>.
208 * - Added the Vuego Scan Brisa 340U ID's. Apparently this scanner is
209 * marketed by Acer Peripherals as a cheap 300 dpi model. Thanks to
210 * David Gundersen <gundersd@paradise.net.nz>.
211 * - Added the Epson Expression1600 ID's. Thanks to Karl Heinz
212 * Kremer <khk@khk.net>.
215 * - Added Mustek ID's (BearPaw 2400, 1200 CU Plus, BearPaw 1200F).
216 * Thanks to Henning Meier-Geinitz <henningmg@gmx.de>.
217 * - Added read_timeout module parameter to override RD_NAK_TIMEOUT
218 * when read()'ing from devices.
219 * - Stalled pipes are now checked and cleared with
220 * usb_clear_halt() for the read_scanner() function. This should
221 * address the "funky result: -32" error messages.
222 * - Removed Microtek scanner ID's. Microtek scanners are now
223 * supported via the drivers/usb/microtek.c driver.
224 * - Added scanner specific read timeout's.
225 * - Return status errors are NEGATIVE!!! This should address the
226 * "funky result: -110" error messages.
227 * - Replaced USB_ST_TIMEOUT with ETIMEDOUT.
228 * - rd_nak was still defined in MODULE_PARM. It's been updated with
229 * read_timeout. Thanks to Mark W. Webb <markwebb@adelphia.net> for
230 * reporting this bug.
231 * - Added Epson Perfection 1640SU and 1640SU Photo. Thanks to
232 * Jean-Luc <f5ibh@db0bm.ampr.org> and Manuel
233 * Pelayo <Manuel.Pelayo@sesips.org>. Reported to work fine by Manuel.
236 * - Added IOCTL's to report back scanner USB ID's. Thanks to
237 * Karl Heinz <khk@lynx.phpwebhosting.com>
238 * - Added Umax Astra 2100U ID's. Thanks to Ron
239 * Wellsted <ron@wellsted.org.uk>.
240 * and Manuel Pelayo <Manuel.Pelayo@sesips.org>.
241 * - Added HP 3400 ID's. Thanks to Harald Hannelius <harald@iki.fi>
242 * and Bertrik Sikken <bertrik@zonnet.nl>. Reported to work at
243 * htpp://home.zonnet.nl/bertrik/hp3300c/hp3300c.htm.
244 * - Added Minolta Dimage Scan Dual II ID's. Thanks to Jose Paulo
245 * Moitinho de Almeida <moitinho@civil.ist.utl.pt>
246 * - Confirmed addition for SnapScan E20. Thanks to Steffen Hübner
248 * - Added Lifetec LT9385 ID's. Thanks to Van Bruwaene Kris
249 * <krvbr@yahoo.co.uk>
250 * - Added Agfa SnapScan e26 ID's. Reported to work with SANE
251 * 1.0.5. Thanks to Falk Sauer <falk@mgnkatze.franken.de>.
252 * - Added HP 4300 ID's. Thanks to Stefan Schlosser
253 * <castla@grmmbl.org>.
254 * - Added Relisis Episode ID's. Thanks to Manfred
255 * Morgner <odb-devel@gmx.net>.
256 * - Added many Acer ID's. Thanks to Oliver
257 * Schwartz <Oliver.Schwartz@gmx.de>.
258 * - Added Snapscan e40 ID's. Thanks to Oliver
259 * Schwartz <Oliver.Schwartz@gmx.de>.
260 * - Thanks to Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de>
261 * for helping with races.
262 * - Added Epson Perfection 1650 ID's. Thanks to Karl Heinz
263 * Kremer <khk@khk.net>.
264 * - Added Epson Perfection 2450 ID's (aka GT-9700 for the Japanese
265 * market). Thanks to Karl Heinz Kremer <khk@khk.net>.
266 * - Added Mustek 600 USB ID's. Thanks to Marcus
267 * Alanen <maalanen@ra.abo.fi>.
268 * - Added Acer ScanPrisa 1240UT ID's. Thanks to Morgan
269 * Collins <sirmorcant@morcant.org>.
270 * - Incorporated devfs patches!! Thanks to Tom Rini
271 * <trini@kernel.crashing.org>, Pavel Roskin <proski@gnu.org>,
272 * Greg KH <greg@kroah.com>, Yves Duret <yduret@mandrakesoft.com>,
273 * Flavio Stanchina <flavio.stanchina@tin.it>.
274 * - Removed Minolta ScanImage II. This scanner uses USB SCSI. Thanks
275 * to Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de> for pointing
277 * - Added additional SMP locking. Thanks to David Brownell and
278 * Oliver Neukum for their help.
279 * - Added version reporting - reports for both module load and modinfo
280 * - Started path to hopefully straighten/clean out ioctl()'s.
281 * - Users are now notified to consult the Documentation/usb/scanner.txt
282 * for common error messages rather than the maintainer.
285 * - Fixed typo in Documentation/scanner.txt. Thanks to
286 * Karel <karel.vervaeke@pandora.be> for pointing it out.
287 * - Added ID's for a Memorex 6136u. Thanks to Álvaro Gaspar de
288 * Valenzuela" <agaspard@utsi.edu>.
289 * - Added ID's for Agfa e25. Thanks to Heinrich
290 * Rust <Heinrich.Rust@gmx.de>. Also reported to work with
291 * Linux and SANE (?).
292 * - Added Canon FB620U, D646U, and 1220U ID's. Thanks to Paul
293 * Rensing <Paul_Rensing@StanfordAlumni.org>. For more info
294 * on Linux support for these models, contact
295 * salvestrini@users.sourceforge.net.
296 * - Added Plustek OpticPro UT12, OpticPro U24, KYE/Genius
297 * ColorPage-HR6 V2 ID's in addition to many "Unknown" models
298 * under those vendors. Thanks to
299 * Jaeger, Gerhard" <g.jaeger@earthling.net>. These scanner are
300 * apparently based upon the LM983x IC's.
301 * - Applied Frank's patch that addressed some locking and module
302 * referencing counts. Thanks to both
303 * Frank Zago <fzago@greshamstorage.com> and
304 * Oliver Neukum <520047054719-0001@t-online.de> for reviewing/testing.
307 * - Added Mustek BearPaw 2400 TA. Thanks to Sergey
308 * Vlasov <vsu@mivlgu.murom.ru>.
309 * - Added Mustek 1200UB Plus and Mustek BearPaw 1200 CU ID's. These use
310 * the Grandtech GT-6801 chip. Thanks to Henning
311 * Meier-Geinitz <henning@meier-geinitz.de>.
312 * - Increased Epson timeout to 60 secs as requested from
313 * Karl Heinz Kremer <khk@khk.net>.
314 * - Changed maintainership from David E. Nelson to Brian
315 * Beattie <beattie@beattie-home.net>.
318 * - Added vendor/product ids for Nikon, Mustek, Plustek, Genius, Epson,
319 * Canon, Umax, Hewlett-Packard, Benq, Agfa, Minolta scanners.
320 * Thanks to Dieter Faulbaum <faulbaum@mail.bessy.de>, Stian Jordet
321 * <liste@jordet.nu>, "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>,
322 * "Jaeger, Gerhard" <gerhard@gjaeger.de>, Ira Childress
323 * <ichildress@mn.rr.com>, Till Kamppeter <till.kamppeter@gmx.net>,
324 * Ed Hamrick <EdHamrick@aol.com>, Oliver Schwartz
325 * <Oliver.Schwartz@gmx.de> and everyone else who sent ids.
326 * - Some Benq, Genius and Plustek ids are identified now.
327 * - Don't clutter syslog with "Unable to access minor data" messages.
328 * - Accept scanners with only one bulk (in) endpoint (thanks to Sergey
329 * Vlasov <vsu@mivlgu.murom.ru>).
330 * - Accept devices with more than one interface. Only use interfaces that
331 * look like belonging to scanners.
332 * - Use altsetting[0], not altsetting[ifnum].
333 * - Add locking to ioctl_scanner(). Thanks to Oliver Neukum
334 * <oliver@neukum.name>.
337 * - Added vendor/product ids for Artec, Canon, Compaq, Epson, HP, Microtek
338 * and Visioneer scanners. Thanks to William Lam <wklam@triad.rr.com>,
339 * Till Kamppeter <till.kamppeter@gmx.net> and others for all the ids.
340 * - Cleaned up list of vendor/product ids.
341 * - Print ids and device number when a device was detected.
342 * - Don't print errors when the device is busy.
343 * - Added vendor/product ids for Visioneer scanners.
344 * - Print information about user-supplied ids only once at startup instead
345 * of everytime any USB device is plugged in.
346 * - Removed PV8630 ioctls. Use the standard ioctls instead.
347 * - Made endpoint detection more generic. Basically, only one bulk-in
348 * endpoint is required, everything else is optional.
349 * - Move the scanner ioctls to usb_scanner_ioctl.h to allow access by archs
350 * that need it (by Greg KH).
351 * - New maintainer: Henning Meier-Geinitz.
352 * - Print ids and device number when a device was detected.
353 * - Don't print errors when the device is busy.
356 * - Added vendor/product ids for Artec, Avision, Brother, Canon, Compaq,
357 * Fujitsu, Hewlett-Packard, Lexmark, LG Electronics, Medion, Microtek,
358 * Primax, Prolink, Plustek, SYSCAN, Trust and UMAX scanners.
361 * - Fixed endpoint detection. The endpoints were numbered from 1 to n but
362 * that assumption is not correct in all cases.
366 * - Added vendor/product ids for Genius, Hewlett-Packard, Microtek,
367 * Mustek, Pacific Image Electronics, Plustek, and Visioneer scanners.
368 * Fixed names of some other scanners.
371 * - Added vendor/product ids for Avision, Canon, HP, Microtek and Relisys
373 * - When checking if all minors are used don't read beyond p_scn_table
375 * - Kfree the scn structure only after disconnect AND close have occured and
376 * check for scn->present. This avoids crashing when someone writes (reads) to
377 * the device while it's already disconnected but still open. Patch from
379 * - Clean up irq urb when not enough memory is available (Sergey Vlasov).
382 * - Added vendor/product ids for Canon, HP, Microtek, Mustek, Siemens, UMAX, and
383 * Visioneer scanners.
384 * - Added test for USB_CLASS_CDC_DATA which is used by some fingerprint scanners
385 * - Use static declarations for usb_scanner_init/usb_scanner_exit
386 * (Daniele Bellucci).
389 * - Added vendor/product ids for Epson, Genius, Microtek, Plustek, Reflecta, and
390 * Visioneer scanners. Removed ids for HP PSC devices as these are supported by
391 * the hpoj userspace driver.
395 * - Select/poll methods
397 * - More general usage ioctl's
402 * - All the folks on the linux-usb list who put up with me. :) This
403 * has been a great learning experience for me.
404 * - To Linus Torvalds for this great OS.
406 * - The folks that forwarded Vendor:Product ID's to me.
407 * - Johannes Erdfelt for the loaning of a USB analyzer for tracking an
408 * issue with HP-4100 and uhci.
409 * - Adolfo Montero for his assistance.
410 * - All the folks who chimed in with reports and suggestions.
411 * - All the developers that are working on USB SANE backends or other
412 * applications to use USB scanners.
413 * - Thanks to Greg KH <greg@kroah.com> for setting up Brian Beattie
414 * and Henning Meier-Geinitz to be the new USB Scanner maintainer.
418 * System: Pentium 120, 80 MB RAM, OHCI, Linux 2.3.23, HP 4100C USB Scanner
419 * 300 dpi scan of the entire bed
420 * 24 Bit Color ~ 70 secs - 3.6 Mbit/sec
421 * 8 Bit Gray ~ 17 secs - 4.2 Mbit/sec */
424 * For documentation, see Documentation/usb/scanner.txt.
425 * Website: http://www.meier-geinitz.de/kernel/
426 * Please contact the maintainer if your scanner is not detected by this
427 * driver automatically.
432 * Scanner definitions, macros, module info,
433 * debug/ioctl/data_dump enable, and other constants.
437 static void purge_scanner(struct scn_usb_data *scn);
440 irq_scanner(struct urb *urb)
444 * For the meantime, this is just a placeholder until I figure out what
445 * all I want to do with it -- or somebody else for that matter.
448 struct scn_usb_data *scn;
453 data += 0; /* Keep gcc from complaining about unused var */
459 dbg("irq_scanner(%d): data:%x", scn->scn_minor, *data);
465 open_scanner(struct inode * inode, struct file * file)
467 struct scn_usb_data *scn;
468 struct usb_device *dev;
476 scn_minor = USB_SCN_MINOR(inode);
478 dbg("open_scanner: scn_minor:%d", scn_minor);
480 if (!p_scn_table[scn_minor]) {
482 dbg("open_scanner(%d): Unable to access minor data", scn_minor);
486 scn = p_scn_table[scn_minor];
490 down(&(scn->sem)); /* Now protect the scn_usb_data structure */
492 up(&scn_mutex); /* Now handled by the above */
495 err("open_scanner(%d): Scanner device not present", scn_minor);
501 err("open_scanner(%d): Scanner is not present", scn_minor);
507 dbg("open_scanner(%d): Scanner device is already open", scn_minor);
512 init_waitqueue_head(&scn->rd_wait_q);
516 file->private_data = scn; /* Used by the read and write methods */
521 up(&(scn->sem)); /* Wake up any possible contending processes */
527 close_scanner(struct inode * inode, struct file * file)
529 struct scn_usb_data *scn = file->private_data;
534 file->private_data = NULL;
537 /* The device was unplugged while open - need to clean up */
549 write_scanner(struct file * file, const char * buffer,
550 size_t count, loff_t *ppos)
552 struct scn_usb_data *scn;
553 struct usb_device *dev;
555 ssize_t bytes_written = 0; /* Overall count of bytes written */
560 int this_write; /* Number of bytes to write */
561 int partial; /* Number of bytes successfully written */
566 scn = file->private_data;
571 /* The device was unplugged while open */
576 if (!scn->bulk_out_ep) {
577 /* This scanner does not have a bulk-out endpoint */
582 scn_minor = scn->scn_minor;
588 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
592 if (signal_pending(current)) {
597 this_write = (count >= OBUF_SIZE) ? OBUF_SIZE : count;
599 if (copy_from_user(scn->obuf, buffer, this_write)) {
604 result = usb_bulk_msg(dev,usb_sndbulkpipe(dev, scn->bulk_out_ep), obuf, this_write, &partial, 60*HZ);
605 dbg("write stats(%d): result:%d this_write:%d partial:%d", scn_minor, result, this_write, partial);
607 if (result == -ETIMEDOUT) { /* NAK -- shouldn't happen */
608 warn("write_scanner: NAK received.");
611 } else if (result < 0) { /* We should not get any I/O errors */
612 warn("write_scanner(%d): funky result: %d. Consult Documentataion/usb/scanner.txt.", scn_minor, result);
619 unsigned char cnt, cnt_max;
620 cnt_max = (partial > 24) ? 24 : partial;
621 printk(KERN_DEBUG "dump(%d): ", scn_minor);
622 for (cnt=0; cnt < cnt_max; cnt++) {
623 printk("%X ", obuf[cnt]);
628 if (partial != this_write) { /* Unable to write all contents of obuf */
633 if (partial) { /* Data written */
636 bytes_written += partial;
637 } else { /* No data written */
643 mdelay(5); /* This seems to help with SANE queries */
644 return ret ? ret : bytes_written;
648 read_scanner(struct file * file, char * buffer,
649 size_t count, loff_t *ppos)
651 struct scn_usb_data *scn;
652 struct usb_device *dev;
654 ssize_t bytes_read; /* Overall count of bytes_read */
659 int partial; /* Number of bytes successfully read */
660 int this_read; /* Max number of bytes to read */
662 int rd_expire = RD_EXPIRE;
666 scn = file->private_data;
671 /* The device was unplugged while open */
676 scn_minor = scn->scn_minor;
685 file->f_dentry->d_inode->i_atime = CURRENT_TIME; /* Update the
690 if (signal_pending(current)) {
695 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
697 result = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, scn->bulk_in_ep), ibuf, this_read, &partial, scn->rd_nak_timeout);
698 dbg("read stats(%d): result:%d this_read:%d partial:%d count:%d", scn_minor, result, this_read, partial, count);
701 * Scanners are sometimes inheriently slow since they are mechanical
702 * in nature. USB bulk reads tend to timeout while the scanner is
703 * positioning, resetting, warming up the lamp, etc if the timeout is
704 * set too low. A very long timeout parameter for bulk reads was used
705 * to overcome this limitation, but this sometimes resulted in folks
706 * having to wait for the timeout to expire after pressing Ctrl-C from
707 * an application. The user was sometimes left with the impression
708 * that something had hung or crashed when in fact the USB read was
709 * just waiting on data. So, the below code retains the same long
710 * timeout period, but splits it up into smaller parts so that
711 * Ctrl-C's are acted upon in a reasonable amount of time.
714 if (result == -ETIMEDOUT) { /* NAK */
715 if (!partial) { /* No data */
716 if (--rd_expire <= 0) { /* Give it up */
717 warn("read_scanner(%d): excessive NAK's received", scn_minor);
720 } else { /* Keep trying to read data */
721 interruptible_sleep_on_timeout(&scn->rd_wait_q, scn->rd_nak_timeout);
724 } else { /* Timeout w/ some data */
729 if (result == -EPIPE) { /* No hope */
730 if(usb_clear_halt(dev, scn->bulk_in_ep)) {
731 err("read_scanner(%d): Failure to clear endpoint halt condition (%Zd).", scn_minor, ret);
735 } else if ((result < 0) && (result != USB_ST_DATAUNDERRUN)) {
736 warn("read_scanner(%d): funky result:%d. Consult Documentation/usb/scanner.txt.", scn_minor, (int)result);
745 unsigned char cnt, cnt_max;
746 cnt_max = (partial > 24) ? 24 : partial;
747 printk(KERN_DEBUG "dump(%d): ", scn_minor);
748 for (cnt=0; cnt < cnt_max; cnt++) {
749 printk("%X ", ibuf[cnt]);
755 if (partial) { /* Data returned */
756 if (copy_to_user(buffer, ibuf, partial)) {
760 count -= this_read; /* Compensate for short reads */
761 bytes_read += partial; /* Keep tally of what actually was read */
769 return ret ? ret : bytes_read;
773 ioctl_scanner(struct inode *inode, struct file *file,
774 unsigned int cmd, unsigned long arg)
776 struct scn_usb_data *scn;
777 struct usb_device *dev;
778 int retval = -ENOTTY;
780 scn = file->private_data;
784 /* The device was unplugged while open */
793 case SCANNER_IOCTL_VENDOR :
794 retval = (put_user(dev->descriptor.idVendor, (unsigned int *) arg));
796 case SCANNER_IOCTL_PRODUCT :
797 retval = (put_user(dev->descriptor.idProduct, (unsigned int *) arg));
799 case SCANNER_IOCTL_CTRLMSG:
801 struct ctrlmsg_ioctl {
802 struct usb_ctrlrequest req;
806 unsigned char buf[64];
809 if (copy_from_user(&cmsg, (void *)arg, sizeof(cmsg))) {
814 nb = cmsg.req.wLength;
816 if (nb > sizeof(buf)) {
821 if ((cmsg.req.bRequestType & 0x80) == 0) {
822 pipe = usb_sndctrlpipe(dev, 0);
823 if (nb > 0 && copy_from_user(buf, cmsg.data, nb)) {
828 pipe = usb_rcvctrlpipe(dev, 0);
831 ret = usb_control_msg(dev, pipe, cmsg.req.bRequest,
832 cmsg.req.bRequestType,
838 err("ioctl_scanner: control_msg returned %d\n", ret);
843 if (nb > 0 && (cmsg.req.bRequestType & 0x80) && copy_to_user(cmsg.data, buf, nb))
856 file_operations usb_scanner_fops = {
859 write: write_scanner,
860 ioctl: ioctl_scanner,
862 release: close_scanner,
866 probe_scanner(struct usb_device *dev, unsigned int ifnum,
867 const struct usb_device_id *id)
869 struct scn_usb_data *scn;
870 struct usb_interface_descriptor *interface;
871 struct usb_endpoint_descriptor *endpoint;
878 char valid_device = 0;
879 char have_bulk_in, have_bulk_out, have_intr;
882 dbg("probe_scanner: USB dev address:%p", dev);
883 dbg("probe_scanner: ifnum:%u", ifnum);
886 * 1. Check Vendor/Product
887 * 2. Determine/Assign Bulk Endpoints
888 * 3. Determine/Assign Intr Endpoint
892 * There doesn't seem to be an imaging class defined in the USB
893 * Spec. (yet). If there is, HP isn't following it and it doesn't
894 * look like anybody else is either. Therefore, we have to test the
895 * Vendor and Product ID's to see what we have. Also, other scanners
896 * may be able to use this driver by specifying both vendor and
897 * product ID's as options to the scanner module in conf.modules.
899 * NOTE: Just because a product is supported here does not mean that
900 * applications exist that support the product. It's in the hopes
901 * that this will allow developers a means to produce applications
902 * that will support USB products.
904 * Until we detect a device which is pleasing, we silently punt.
907 for (ix = 0; ix < sizeof (scanner_device_ids) / sizeof (struct usb_device_id); ix++) {
908 if ((dev->descriptor.idVendor == scanner_device_ids [ix].idVendor) &&
909 (dev->descriptor.idProduct == scanner_device_ids [ix].idProduct)) {
914 if (dev->descriptor.idVendor == vendor && /* User specified */
915 dev->descriptor.idProduct == product) { /* User specified */
920 return NULL; /* We didn't find anything pleasing */
923 * After this point we can be a little noisy about what we are trying to
927 if (dev->descriptor.bNumConfigurations != 1) {
928 info("probe_scanner: Only one device configuration is supported.");
932 interface = dev->config[0].interface[ifnum].altsetting;
934 if (interface[0].bInterfaceClass != USB_CLASS_VENDOR_SPEC &&
935 interface[0].bInterfaceClass != USB_CLASS_PER_INTERFACE &&
936 interface[0].bInterfaceClass != USB_CLASS_CDC_DATA &&
937 interface[0].bInterfaceClass != SCN_CLASS_SCANJET) {
938 dbg("probe_scanner: This interface doesn't look like a scanner (class=0x%x).", interface[0].bInterfaceClass);
942 endpoint = interface[0].endpoint;
945 * Start checking for bulk and interrupt endpoints. We are only using the first
946 * one of each type of endpoint. If we have an interrupt endpoint go ahead and
947 * setup the handler. FIXME: This is a future enhancement...
950 dbg("probe_scanner: Number of Endpoints:%d", (int) interface->bNumEndpoints);
952 ep_cnt = have_bulk_in = have_bulk_out = have_intr = 0;
954 while (ep_cnt < interface->bNumEndpoints) {
956 if (IS_EP_BULK_IN(endpoint[ep_cnt])) {
959 info ("probe_scanner: ignoring additional bulk_in_ep:%d", ep_cnt);
962 have_bulk_in = endpoint[ep_cnt - 1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
963 dbg("probe_scanner: bulk_in_ep:%d", have_bulk_in);
967 if (IS_EP_BULK_OUT(endpoint[ep_cnt])) {
970 info ("probe_scanner: ignoring additional bulk_out_ep:%d", ep_cnt);
973 have_bulk_out = endpoint[ep_cnt - 1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
974 dbg("probe_scanner: bulk_out_ep:%d", have_bulk_out);
978 if (IS_EP_INTR(endpoint[ep_cnt])) {
981 info ("probe_scanner: ignoring additional intr_ep:%d", ep_cnt);
984 have_intr = endpoint[ep_cnt - 1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
985 dbg("probe_scanner: intr_ep:%d", have_intr);
988 info("probe_scanner: Undetected endpoint -- consult Documentation/usb/scanner.txt.");
989 return NULL; /* Shouldn't ever get here unless we have something weird */
994 * Perform a quick check to make sure that everything worked as it
998 err("probe_scanner: One bulk-in endpoint required.");
1004 * Determine a minor number and initialize the structure associated
1005 * with it. The problem with this is that we are counting on the fact
1006 * that the user will sequentially add device nodes for the scanner
1011 for (scn_minor = 0; scn_minor < SCN_MAX_MNR; scn_minor++) {
1012 if (!p_scn_table[scn_minor])
1016 /* Check to make sure that the last slot isn't already taken */
1017 if (scn_minor >= SCN_MAX_MNR) {
1018 err("probe_scanner: No more minor devices remaining.");
1023 dbg("probe_scanner: Allocated minor:%d", scn_minor);
1025 if (!(scn = kmalloc (sizeof (struct scn_usb_data), GFP_KERNEL))) {
1026 err("probe_scanner: Out of memory.");
1030 memset (scn, 0, sizeof(struct scn_usb_data));
1032 init_MUTEX(&(scn->sem)); /* Initializes to unlocked */
1034 dbg ("probe_scanner(%d): Address of scn:%p", scn_minor, scn);
1036 /* Ok, if we detected an interrupt EP, setup a handler for it */
1038 dbg("probe_scanner(%d): Configuring IRQ handler for intr EP:%d", scn_minor, have_intr);
1039 FILL_INT_URB(&scn->scn_irq, dev,
1040 usb_rcvintpipe(dev, have_intr),
1041 &scn->button, 1, irq_scanner, scn,
1042 // endpoint[(int)have_intr].bInterval);
1045 if (usb_submit_urb(&scn->scn_irq)) {
1046 err("probe_scanner(%d): Unable to allocate INT URB.", scn_minor);
1054 /* Ok, now initialize all the relevant values */
1055 if (!(scn->obuf = (char *)kmalloc(OBUF_SIZE, GFP_KERNEL))) {
1056 err("probe_scanner(%d): Not enough memory for the output buffer.", scn_minor);
1058 usb_unlink_urb(&scn->scn_irq);
1063 dbg("probe_scanner(%d): obuf address:%p", scn_minor, scn->obuf);
1065 if (!(scn->ibuf = (char *)kmalloc(IBUF_SIZE, GFP_KERNEL))) {
1066 err("probe_scanner(%d): Not enough memory for the input buffer.", scn_minor);
1068 usb_unlink_urb(&scn->scn_irq);
1074 dbg("probe_scanner(%d): ibuf address:%p", scn_minor, scn->ibuf);
1077 switch (dev->descriptor.idVendor) { /* Scanner specific read timeout parameters */
1078 case 0x04b8: /* Seiko/Epson */
1079 scn->rd_nak_timeout = HZ * 60;
1081 case 0x055f: /* Mustek */
1082 case 0x0400: /* Another Mustek */
1083 scn->rd_nak_timeout = HZ * 1;
1085 scn->rd_nak_timeout = RD_NAK_TIMEOUT;
1089 if (read_timeout > 0) { /* User specified read timeout overrides everything */
1090 info("probe_scanner: User specified USB read timeout - %d", read_timeout);
1091 scn->rd_nak_timeout = read_timeout;
1095 scn->bulk_in_ep = have_bulk_in;
1096 scn->bulk_out_ep = have_bulk_out;
1097 scn->intr_ep = have_intr;
1100 scn->scn_minor = scn_minor;
1103 sprintf(name, "scanner%d", scn->scn_minor);
1105 scn->devfs = devfs_register(usb_devfs_handle, name,
1106 DEVFS_FL_DEFAULT, USB_MAJOR,
1107 SCN_BASE_MNR + scn->scn_minor,
1108 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
1109 S_IWGRP | S_IROTH | S_IWOTH, &usb_scanner_fops, NULL);
1110 if (scn->devfs == NULL)
1111 dbg("scanner%d: device node registration failed", scn_minor);
1113 info ("USB scanner device (0x%04x/0x%04x) now attached to %s",
1114 dev->descriptor.idVendor, dev->descriptor.idProduct, name);
1115 p_scn_table[scn_minor] = scn;
1123 purge_scanner(struct scn_usb_data *scn)
1131 disconnect_scanner(struct usb_device *dev, void *ptr)
1133 struct scn_usb_data *scn = (struct scn_usb_data *) ptr;
1139 dbg("disconnect_scanner(%d): Unlinking IRQ URB", scn->scn_minor);
1140 usb_unlink_urb(&scn->scn_irq);
1142 usb_driver_release_interface(&scanner_driver,
1143 &scn->scn_dev->actconfig->interface[scn->ifnum]);
1145 dbg("disconnect_scanner: De-allocating minor:%d", scn->scn_minor);
1146 devfs_unregister(scn->devfs);
1147 p_scn_table[scn->scn_minor] = NULL;
1150 /* The device is still open - cleanup must be delayed */
1164 usb_driver scanner_driver = {
1166 probe: probe_scanner,
1167 disconnect: disconnect_scanner,
1168 fops: &usb_scanner_fops,
1169 minor: SCN_BASE_MNR,
1170 id_table: NULL, /* This would be scanner_device_ids, but we
1171 need to check every USB device, in case
1172 we match a user defined vendor/product ID. */
1176 usb_scanner_exit(void)
1178 usb_deregister(&scanner_driver);
1182 usb_scanner_init (void)
1184 if (usb_register(&scanner_driver) < 0)
1187 info(DRIVER_VERSION ":" DRIVER_DESC);
1188 if (vendor != -1 && product != -1)
1189 info("probe_scanner: User specified USB scanner -- Vendor:Product - %x:%x", vendor, product);
1193 module_init(usb_scanner_init);
1194 module_exit(usb_scanner_exit);