1/* -*- linux-c -*- */ 2 3/* 4 * Driver for USB Scanners (linux-2.4) 5 * 6 * Copyright (C) 1999, 2000, 2001, 2002 David E. Nelson 7 * Copyright (C) 2002, 2003 Henning Meier-Geinitz 8 * 9 * Portions may be copyright Brad Keryan and Michael Gee. 10 * 11 * Previously maintained by Brian Beattie 12 * 13 * Current maintainer: Henning Meier-Geinitz <henning@meier-geinitz.de> 14 * 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. 19 * 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. 24 * 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. 28 * 29 * Originally based upon mouse.c (Brad Keryan) and printer.c (Michael Gee). 30 * 31 * History 32 * 33 * 0.1 8/31/1999 34 * 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. 40 * 41 * 42 * 0.2 10/16/1999 43 * 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 49 * 50 * 51 * 0.3 10/18/1999 52 * 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 59 * *and* configured. 60 * - Added user specified vendor:product USB ID's which can be passed 61 * as module parameters. 62 * 63 * 64 * 0.3.1 65 * 66 * - Applied patches for linux-2.3.25. 67 * - Error number reporting changed to reflect negative return codes. 68 * 69 * 70 * 0.3.2 71 * 72 * - Applied patches for linux-2.3.26 to scanner_init(). 73 * - Debug read/write stats now report values as signed decimal. 74 * 75 * 76 * 0.3.3 77 * 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 84 * all over the place. 85 * - kfree()'d the pointer after using usb_string() as documented in 86 * linux-usb-api.txt. 87 * - Added usb_set_configuration(). It got lost in version 0.3 -- ack! 88 * - Added the HP 5200C USB Vendor/Product ID's. 89 * 90 * 91 * 0.3.4 1/23/2000 92 * 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. 115 * 116 * 117 * 0.4 2/4/2000 118 * 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>. 135 * 136 * 137 * 0.4.1 2/15/2000 138 * 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 150 * in scanner.c. 151 * - Increased the timeout parameter in read_scanner() to 120 Secs. 152 * 153 * 154 * 0.4.2 3/23/2000 155 * 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 163 * from patch-2.3.48. 164 * - Replaced file_operations structure with new syntax. Updates 165 * from patch-2.3.49. 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 171 * -EINTR. 172 * 173 * 174 * 0.4.3 4/30/2000 175 * 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 182 * time. 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(). 198 * 199 * 200 * 0.4.3.1 201 * 202 * - Fixed HP S20 ID's...again..sigh. Thanks to Ruud 203 * Linders <rlinders@xs4all.nl>. 204 * 205 * 0.4.4 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>. 213 * 214 * 0.4.5 2/28/2001 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. 234 * 235 * 0.4.6 9/27/2001 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 247 * <hueb_s@gmx.de>. 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 276 * this out. 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. 283 * 284 * 0.4.7 11/28/2001 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. 305 * 306 * 0.4.8 5/30/2002 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>. 316 * 317 * 0.4.9 12/19/2002 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>. 335 * 336 * 0.4.10 01/07/2003 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. 354 * 355 * 0.4.11 2003-02-25 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. 359 * 360 * 0.4.12 2003-04-16 361 * - Fixed endpoint detection. The endpoints were numbered from 1 to n but 362 * that assumption is not correct in all cases. 363 * 364 * 365 * 0.4.13 2003-06-14 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. 369 * 370 * 0.4.14 2003-07-15 371 * - Added vendor/product ids for Avision, Canon, HP, Microtek and Relisys 372 * scanners. 373 * - When checking if all minors are used don't read beyond p_scn_table 374 * (Sergey Vlasov). 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 378 * Sergey Vlasov. 379 * - Clean up irq urb when not enough memory is available (Sergey Vlasov). 380 * 381 * 0.4.15 2003-10-03 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). 387 * 388 * 0.4.16 2003-11-04 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. 392 * 393 * TODO 394 * - Performance 395 * - Select/poll methods 396 * - More testing 397 * - More general usage ioctl's 398 * 399 * 400 * Thanks to: 401 * 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. 405 * - The GNU folks. 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. 415 * 416 * Performance: 417 * 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 */ 422 423/* 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. 428 */ 429 430 431/* 432 * Scanner definitions, macros, module info, 433 * debug/ioctl/data_dump enable, and other constants. 434 */ 435#include "scanner.h" 436 437static void purge_scanner(struct scn_usb_data *scn); 438 439static void 440irq_scanner(struct urb *urb) 441{ 442 443/* 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. 446 */ 447 448 struct scn_usb_data *scn; 449 unsigned char *data; 450 scn = urb->context; 451 452 data = &scn->button; 453 data += 0; /* Keep gcc from complaining about unused var */ 454 455 if (urb->status) { 456 return; 457 } 458 459 dbg("irq_scanner(%d): data:%x", scn->scn_minor, *data); 460 461 return; 462} 463 464static int 465open_scanner(struct inode * inode, struct file * file) 466{ 467 struct scn_usb_data *scn; 468 struct usb_device *dev; 469 470 kdev_t scn_minor; 471 472 int err=0; 473 474 down(&scn_mutex); 475 476 scn_minor = USB_SCN_MINOR(inode); 477 478 dbg("open_scanner: scn_minor:%d", scn_minor); 479 480 if (!p_scn_table[scn_minor]) { 481 up(&scn_mutex); 482 dbg("open_scanner(%d): Unable to access minor data", scn_minor); 483 return -ENODEV; 484 } 485 486 scn = p_scn_table[scn_minor]; 487 488 dev = scn->scn_dev; 489 490 down(&(scn->sem)); /* Now protect the scn_usb_data structure */ 491 492 up(&scn_mutex); /* Now handled by the above */ 493 494 if (!dev) { 495 err("open_scanner(%d): Scanner device not present", scn_minor); 496 err = -ENODEV; 497 goto out_error; 498 } 499 500 if (!scn->present) { 501 err("open_scanner(%d): Scanner is not present", scn_minor); 502 err = -ENODEV; 503 goto out_error; 504 } 505 506 if (scn->isopen) { 507 dbg("open_scanner(%d): Scanner device is already open", scn_minor); 508 err = -EBUSY; 509 goto out_error; 510 } 511 512 init_waitqueue_head(&scn->rd_wait_q); 513 514 scn->isopen = 1; 515 516 file->private_data = scn; /* Used by the read and write methods */ 517 518 519out_error: 520 521 up(&(scn->sem)); /* Wake up any possible contending processes */ 522 523 return err; 524} 525 526static int 527close_scanner(struct inode * inode, struct file * file) 528{ 529 struct scn_usb_data *scn = file->private_data; 530 531 down(&(scn->sem)); 532 scn->isopen = 0; 533 534 file->private_data = NULL; 535 536 if (!scn->present) { 537 /* The device was unplugged while open - need to clean up */ 538 up(&(scn->sem)); 539 purge_scanner(scn); 540 return 0; 541 } 542 543 up(&(scn->sem)); 544 545 return 0; 546} 547 548static ssize_t 549write_scanner(struct file * file, const char * buffer, 550 size_t count, loff_t *ppos) 551{ 552 struct scn_usb_data *scn; 553 struct usb_device *dev; 554 555 ssize_t bytes_written = 0; /* Overall count of bytes written */ 556 ssize_t ret = 0; 557 558 kdev_t scn_minor; 559 560 int this_write; /* Number of bytes to write */ 561 int partial; /* Number of bytes successfully written */ 562 int result = 0; 563 564 char *obuf; 565 566 scn = file->private_data; 567 568 down(&(scn->sem)); 569 570 if (!scn->present) { 571 /* The device was unplugged while open */ 572 up(&(scn->sem)); 573 return -ENODEV; 574 } 575 576 if (!scn->bulk_out_ep) { 577 /* This scanner does not have a bulk-out endpoint */ 578 up(&(scn->sem)); 579 return -EINVAL; 580 } 581 582 scn_minor = scn->scn_minor; 583 584 obuf = scn->obuf; 585 586 dev = scn->scn_dev; 587 588 file->f_dentry->d_inode->i_atime = CURRENT_TIME; 589 590 while (count > 0) { 591 592 if (signal_pending(current)) { 593 ret = -ERESTARTSYS; 594 break; 595 } 596 597 this_write = (count >= OBUF_SIZE) ? OBUF_SIZE : count; 598 599 if (copy_from_user(scn->obuf, buffer, this_write)) { 600 ret = -EFAULT; 601 break; 602 } 603 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); 606 607 if (result == -ETIMEDOUT) { /* NAK -- shouldn't happen */ 608 warn("write_scanner: NAK received."); 609 ret = result; 610 break; 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); 613 ret = -EIO; 614 break; 615 } 616 617#ifdef WR_DATA_DUMP 618 if (partial) { 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]); 624 } 625 printk("\n"); 626 } 627#endif 628 if (partial != this_write) { /* Unable to write all contents of obuf */ 629 ret = -EIO; 630 break; 631 } 632 633 if (partial) { /* Data written */ 634 buffer += partial; 635 count -= partial; 636 bytes_written += partial; 637 } else { /* No data written */ 638 ret = 0; 639 break; 640 } 641 } 642 up(&(scn->sem)); 643 mdelay(5); /* This seems to help with SANE queries */ 644 return ret ? ret : bytes_written; 645} 646 647static ssize_t 648read_scanner(struct file * file, char * buffer, 649 size_t count, loff_t *ppos) 650{ 651 struct scn_usb_data *scn; 652 struct usb_device *dev; 653 654 ssize_t bytes_read; /* Overall count of bytes_read */ 655 ssize_t ret; 656 657 kdev_t scn_minor; 658 659 int partial; /* Number of bytes successfully read */ 660 int this_read; /* Max number of bytes to read */ 661 int result; 662 int rd_expire = RD_EXPIRE; 663 664 char *ibuf; 665 666 scn = file->private_data; 667 668 down(&(scn->sem)); 669 670 if (!scn->present) { 671 /* The device was unplugged while open */ 672 up(&(scn->sem)); 673 return -ENODEV; 674 } 675 676 scn_minor = scn->scn_minor; 677 678 ibuf = scn->ibuf; 679 680 dev = scn->scn_dev; 681 682 bytes_read = 0; 683 ret = 0; 684 685 file->f_dentry->d_inode->i_atime = CURRENT_TIME; /* Update the 686 atime of 687 the device 688 node */ 689 while (count > 0) { 690 if (signal_pending(current)) { 691 ret = -ERESTARTSYS; 692 break; 693 } 694 695 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count; 696 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); 699 700/* 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. 712 */ 713 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); 718 ret = result; 719 break; 720 } else { /* Keep trying to read data */ 721 interruptible_sleep_on_timeout(&scn->rd_wait_q, scn->rd_nak_timeout); 722 continue; 723 } 724 } else { /* Timeout w/ some data */ 725 goto data_recvd; 726 } 727 } 728 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); 732 } 733 ret = result; 734 break; 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); 737 ret = -EIO; 738 break; 739 } 740 741 data_recvd: 742 743#ifdef RD_DATA_DUMP 744 if (partial) { 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]); 750 } 751 printk("\n"); 752 } 753#endif 754 755 if (partial) { /* Data returned */ 756 if (copy_to_user(buffer, ibuf, partial)) { 757 ret = -EFAULT; 758 break; 759 } 760 count -= this_read; /* Compensate for short reads */ 761 bytes_read += partial; /* Keep tally of what actually was read */ 762 buffer += partial; 763 } else { 764 ret = 0; 765 break; 766 } 767 } 768 up(&(scn->sem)); 769 return ret ? ret : bytes_read; 770} 771 772static int 773ioctl_scanner(struct inode *inode, struct file *file, 774 unsigned int cmd, unsigned long arg) 775{ 776 struct scn_usb_data *scn; 777 struct usb_device *dev; 778 int retval = -ENOTTY; 779 780 scn = file->private_data; 781 down(&(scn->sem)); 782 783 if (!scn->present) { 784 /* The device was unplugged while open */ 785 up(&(scn->sem)); 786 return -ENODEV; 787 } 788 789 dev = scn->scn_dev; 790 791 switch (cmd) 792 { 793 case SCANNER_IOCTL_VENDOR : 794 retval = (put_user(dev->descriptor.idVendor, (unsigned int *) arg)); 795 break; 796 case SCANNER_IOCTL_PRODUCT : 797 retval = (put_user(dev->descriptor.idProduct, (unsigned int *) arg)); 798 break; 799 case SCANNER_IOCTL_CTRLMSG: 800 { 801 struct ctrlmsg_ioctl { 802 struct usb_ctrlrequest req; 803 void *data; 804 } cmsg; 805 int pipe, nb, ret; 806 unsigned char buf[64]; 807 retval = 0; 808 809 if (copy_from_user(&cmsg, (void *)arg, sizeof(cmsg))) { 810 retval = -EFAULT; 811 break; 812 } 813 814 nb = cmsg.req.wLength; 815 816 if (nb > sizeof(buf)) { 817 retval = -EINVAL; 818 break; 819 } 820 821 if ((cmsg.req.bRequestType & 0x80) == 0) { 822 pipe = usb_sndctrlpipe(dev, 0); 823 if (nb > 0 && copy_from_user(buf, cmsg.data, nb)) { 824 retval = -EFAULT; 825 break; 826 } 827 } else { 828 pipe = usb_rcvctrlpipe(dev, 0); 829 } 830 831 ret = usb_control_msg(dev, pipe, cmsg.req.bRequest, 832 cmsg.req.bRequestType, 833 cmsg.req.wValue, 834 cmsg.req.wIndex, 835 buf, nb, HZ); 836 837 if (ret < 0) { 838 err("ioctl_scanner: control_msg returned %d\n", ret); 839 retval = -EIO; 840 break; 841 } 842 843 if (nb > 0 && (cmsg.req.bRequestType & 0x80) && copy_to_user(cmsg.data, buf, nb)) 844 retval = -EFAULT; 845 846 break; 847 } 848 default: 849 break; 850 } 851 up(&(scn->sem)); 852 return retval; 853} 854 855static struct 856file_operations usb_scanner_fops = { 857 owner: THIS_MODULE, 858 read: read_scanner, 859 write: write_scanner, 860 ioctl: ioctl_scanner, 861 open: open_scanner, 862 release: close_scanner, 863}; 864 865static void * 866probe_scanner(struct usb_device *dev, unsigned int ifnum, 867 const struct usb_device_id *id) 868{ 869 struct scn_usb_data *scn; 870 struct usb_interface_descriptor *interface; 871 struct usb_endpoint_descriptor *endpoint; 872 873 int ep_cnt; 874 int ix; 875 876 kdev_t scn_minor; 877 878 char valid_device = 0; 879 char have_bulk_in, have_bulk_out, have_intr; 880 char name[10]; 881 882 dbg("probe_scanner: USB dev address:%p", dev); 883 dbg("probe_scanner: ifnum:%u", ifnum); 884 885/* 886 * 1. Check Vendor/Product 887 * 2. Determine/Assign Bulk Endpoints 888 * 3. Determine/Assign Intr Endpoint 889 */ 890 891/* 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. 898 * 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. 903 * 904 * Until we detect a device which is pleasing, we silently punt. 905 */ 906 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)) { 910 valid_device = 1; 911 break; 912 } 913 } 914 if (dev->descriptor.idVendor == vendor && /* User specified */ 915 dev->descriptor.idProduct == product) { /* User specified */ 916 valid_device = 1; 917 } 918 919 if (!valid_device) 920 return NULL; /* We didn't find anything pleasing */ 921 922/* 923 * After this point we can be a little noisy about what we are trying to 924 * configure. 925 */ 926 927 if (dev->descriptor.bNumConfigurations != 1) { 928 info("probe_scanner: Only one device configuration is supported."); 929 return NULL; 930 } 931 932 interface = dev->config[0].interface[ifnum].altsetting; 933 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); 939 return NULL; 940 } 941 942 endpoint = interface[0].endpoint; 943 944/* 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... 948 */ 949 950 dbg("probe_scanner: Number of Endpoints:%d", (int) interface->bNumEndpoints); 951 952 ep_cnt = have_bulk_in = have_bulk_out = have_intr = 0; 953 954 while (ep_cnt < interface->bNumEndpoints) { 955 956 if (IS_EP_BULK_IN(endpoint[ep_cnt])) { 957 ep_cnt++; 958 if (have_bulk_in) { 959 info ("probe_scanner: ignoring additional bulk_in_ep:%d", ep_cnt); 960 continue; 961 } 962 have_bulk_in = endpoint[ep_cnt - 1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 963 dbg("probe_scanner: bulk_in_ep:%d", have_bulk_in); 964 continue; 965 } 966 967 if (IS_EP_BULK_OUT(endpoint[ep_cnt])) { 968 ep_cnt++; 969 if (have_bulk_out) { 970 info ("probe_scanner: ignoring additional bulk_out_ep:%d", ep_cnt); 971 continue; 972 } 973 have_bulk_out = endpoint[ep_cnt - 1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 974 dbg("probe_scanner: bulk_out_ep:%d", have_bulk_out); 975 continue; 976 } 977 978 if (IS_EP_INTR(endpoint[ep_cnt])) { 979 ep_cnt++; 980 if (have_intr) { 981 info ("probe_scanner: ignoring additional intr_ep:%d", ep_cnt); 982 continue; 983 } 984 have_intr = endpoint[ep_cnt - 1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 985 dbg("probe_scanner: intr_ep:%d", have_intr); 986 continue; 987 } 988 info("probe_scanner: Undetected endpoint -- consult Documentation/usb/scanner.txt."); 989 return NULL; /* Shouldn't ever get here unless we have something weird */ 990 } 991 992 993/* 994 * Perform a quick check to make sure that everything worked as it 995 * should have. 996 */ 997 if (!have_bulk_in) { 998 err("probe_scanner: One bulk-in endpoint required."); 999 return NULL; 1000 }
1001 1002 1003/* 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 1007 * devices. */ 1008 1009 down(&scn_mutex); 1010 1011 for (scn_minor = 0; scn_minor < SCN_MAX_MNR; scn_minor++) { 1012 if (!p_scn_table[scn_minor]) 1013 break; 1014 } 1015 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."); 1019 up(&scn_mutex); 1020 return NULL; 1021 } 1022 1023 dbg("probe_scanner: Allocated minor:%d", scn_minor); 1024 1025 if (!(scn = kmalloc (sizeof (struct scn_usb_data), GFP_KERNEL))) { 1026 err("probe_scanner: Out of memory."); 1027 up(&scn_mutex); 1028 return NULL; 1029 } 1030 memset (scn, 0, sizeof(struct scn_usb_data)); 1031 1032 init_MUTEX(&(scn->sem)); /* Initializes to unlocked */ 1033 1034 dbg ("probe_scanner(%d): Address of scn:%p", scn_minor, scn); 1035 1036/* Ok, if we detected an interrupt EP, setup a handler for it */ 1037 if (have_intr) { 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); 1043 250); 1044 1045 if (usb_submit_urb(&scn->scn_irq)) { 1046 err("probe_scanner(%d): Unable to allocate INT URB.", scn_minor); 1047 kfree(scn); 1048 up(&scn_mutex); 1049 return NULL; 1050 } 1051 } 1052 1053 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); 1057 if (have_intr) 1058 usb_unlink_urb(&scn->scn_irq); 1059 kfree(scn); 1060 up(&scn_mutex); 1061 return NULL; 1062 } 1063 dbg("probe_scanner(%d): obuf address:%p", scn_minor, scn->obuf); 1064 1065 if (!(scn->ibuf = (char *)kmalloc(IBUF_SIZE, GFP_KERNEL))) { 1066 err("probe_scanner(%d): Not enough memory for the input buffer.", scn_minor); 1067 if (have_intr) 1068 usb_unlink_urb(&scn->scn_irq); 1069 kfree(scn->obuf); 1070 kfree(scn); 1071 up(&scn_mutex); 1072 return NULL; 1073 } 1074 dbg("probe_scanner(%d): ibuf address:%p", scn_minor, scn->ibuf); 1075 1076 1077 switch (dev->descriptor.idVendor) { /* Scanner specific read timeout parameters */ 1078 case 0x04b8: /* Seiko/Epson */ 1079 scn->rd_nak_timeout = HZ * 60; 1080 break; 1081 case 0x055f: /* Mustek */ 1082 case 0x0400: /* Another Mustek */ 1083 scn->rd_nak_timeout = HZ * 1; 1084 default: 1085 scn->rd_nak_timeout = RD_NAK_TIMEOUT; 1086 } 1087 1088 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; 1092 } 1093 1094 1095 scn->bulk_in_ep = have_bulk_in; 1096 scn->bulk_out_ep = have_bulk_out; 1097 scn->intr_ep = have_intr; 1098 scn->present = 1; 1099 scn->scn_dev = dev; 1100 scn->scn_minor = scn_minor; 1101 scn->isopen = 0; 1102 1103 sprintf(name, "scanner%d", scn->scn_minor); 1104 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); 1112 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; 1116 1117 up(&scn_mutex); 1118 1119 return scn; 1120} 1121 1122static void 1123purge_scanner(struct scn_usb_data *scn) 1124{ 1125 kfree(scn->ibuf); 1126 kfree(scn->obuf); 1127 kfree(scn); 1128} 1129 1130static void 1131disconnect_scanner(struct usb_device *dev, void *ptr) 1132{ 1133 struct scn_usb_data *scn = (struct scn_usb_data *) ptr; 1134 1135 down (&scn_mutex); 1136 down (&(scn->sem)); 1137 1138 if(scn->intr_ep) { 1139 dbg("disconnect_scanner(%d): Unlinking IRQ URB", scn->scn_minor); 1140 usb_unlink_urb(&scn->scn_irq); 1141 } 1142 usb_driver_release_interface(&scanner_driver, 1143 &scn->scn_dev->actconfig->interface[scn->ifnum]); 1144 1145 dbg("disconnect_scanner: De-allocating minor:%d", scn->scn_minor); 1146 devfs_unregister(scn->devfs); 1147 p_scn_table[scn->scn_minor] = NULL; 1148 1149 if (scn->isopen) { 1150 /* The device is still open - cleanup must be delayed */ 1151 scn->present = 0; 1152 up(&(scn->sem)); 1153 up(&scn_mutex); 1154 return; 1155 } 1156 1157 up (&(scn->sem)); 1158 up (&scn_mutex); 1159 1160 purge_scanner(scn); 1161} 1162 1163static struct 1164usb_driver scanner_driver = { 1165 name: "usbscanner", 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. */ 1173}; 1174 1175static void __exit 1176usb_scanner_exit(void) 1177{ 1178 usb_deregister(&scanner_driver); 1179} 1180 1181static int __init 1182usb_scanner_init (void) 1183{ 1184 if (usb_register(&scanner_driver) < 0) 1185 return -1; 1186 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); 1190 return 0; 1191} 1192 1193module_init(usb_scanner_init); 1194module_exit(usb_scanner_exit); 1195

