1 /* 2 * usblp.c Version 0.13 3 * 4 * Copyright (c) 1999 Michael Gee <michael@linuxspecific.com> 5 * Copyright (c) 1999 Pavel Machek <pavel@suse.cz> 6 * Copyright (c) 2000 Randy Dunlap <rdunlap@xenotime.net> 7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz> 8 # Copyright (c) 2001 Pete Zaitcev <zaitcev@redhat.com> 9 # Copyright (c) 2001 David Paschal <paschal@rcsis.com> 10 * 11 * USB Printer Device Class driver for USB printers and printer cables 12 * 13 * Sponsored by SuSE 14 * 15 * ChangeLog: 16 * v0.1 - thorough cleaning, URBification, almost a rewrite 17 * v0.2 - some more cleanups 18 * v0.3 - cleaner again, waitqueue fixes 19 * v0.4 - fixes in unidirectional mode 20 * v0.5 - add DEVICE_ID string support 21 * v0.6 - never time out 22 * v0.7 - fixed bulk-IN read and poll (David Paschal) 23 * v0.8 - add devfs support 24 * v0.9 - fix unplug-while-open paths 25 * v0.10- remove sleep_on, fix error on oom (oliver@neukum.org) 26 * v0.11 - add proto_bias option (Pete Zaitcev) 27 * v0.12 - add hpoj.sourceforge.net ioctls (David Paschal) 28 * v0.13 - alloc space for statusbuf (<status> not on stack); 29 * use usb_buffer_alloc() for read buf & write buf; 30 */ 31 32 /* 33 * This program is free software; you can redistribute it and/or modify 34 * it under the terms of the GNU General Public License as published by 35 * the Free Software Foundation; either version 2 of the License, or 36 * (at your option) any later version. 37 * 38 * This program is distributed in the hope that it will be useful, 39 * but WITHOUT ANY WARRANTY; without even the implied warranty of 40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 41 * GNU General Public License for more details. 42 * 43 * You should have received a copy of the GNU General Public License 44 * along with this program; if not, write to the Free Software 45 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 46 */ 47 48 #include <linux/module.h> 49 #include <linux/kernel.h> 50 #include <linux/sched.h> 51 #include <linux/smp_lock.h> 52 #include <linux/signal.h> 53 #include <linux/poll.h> 54 #include <linux/init.h> 55 #include <linux/slab.h> 56 #include <linux/lp.h> 57 #undef DEBUG 58 #include <linux/usb.h> 59 60 /* 61 * Version Information 62 */ 63 #define DRIVER_VERSION "v0.13" 64 #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal" 65 #define DRIVER_DESC "USB Printer Device Class driver" 66 67 #define USBLP_BUF_SIZE 8192 68 #define USBLP_DEVICE_ID_SIZE 1024 69 70 /* ioctls: */ 71 #define LPGETSTATUS 0x060b /* same as in drivers/char/lp.c */ 72 #define IOCNR_GET_DEVICE_ID 1 73 #define IOCNR_GET_PROTOCOLS 2 74 #define IOCNR_SET_PROTOCOL 3 75 #define IOCNR_HP_SET_CHANNEL 4 76 #define IOCNR_GET_BUS_ADDRESS 5 77 #define IOCNR_GET_VID_PID 6 78 #define IOCNR_SOFT_RESET 7 79 /* Get device_id string: */ 80 #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len) 81 /* The following ioctls were added for http://hpoj.sourceforge.net: */ 82 /* Get two-int array: 83 * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3), 84 * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */ 85 #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len) 86 /* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */ 87 #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0) 88 /* Set channel number (HP Vendor-specific command): */ 89 #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0) 90 /* Get two-int array: [0]=bus number, [1]=device address: */ 91 #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len) 92 /* Get two-int array: [0]=vendor ID, [1]=product ID: */ 93 #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len) 94 /* Perform class specific soft reset */ 95 #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0); 96 97 /* 98 * A DEVICE_ID string may include the printer's serial number. 99 * It should end with a semi-colon (';'). 100 * An example from an HP 970C DeskJet printer is (this is one long string, 101 * with the serial number changed): 102 MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:Hewlett-Packard DeskJet 970C;SERN:US970CSEPROF;VSTATUS:$HB0$NC0,ff,DN,IDLE,CUT,K1,C0,DP,NR,KP000,CP027;VP:0800,FL,B0;VJ: ; 103 */ 104 105 /* 106 * USB Printer Requests 107 */ 108 109 #define USBLP_REQ_GET_ID 0x00 110 #define USBLP_REQ_GET_STATUS 0x01 111 #define USBLP_REQ_RESET 0x02 112 #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */ 113 114 #define USBLP_MINORS 16 115 #define USBLP_MINOR_BASE 0 116 117 #define USBLP_WRITE_TIMEOUT (5000) /* 5 seconds */ 118 119 #define USBLP_FIRST_PROTOCOL 1 120 #define USBLP_LAST_PROTOCOL 3 121 #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1) 122 123 /* 124 * some arbitrary status buffer size; 125 * need a status buffer that is allocated via kmalloc(), not on stack 126 */ 127 #define STATUS_BUF_SIZE 8 128 129 struct usblp { 130 struct usb_device *dev; /* USB device */ 131 struct semaphore sem; /* locks this struct, especially "dev" */ 132 char *writebuf; /* write transfer_buffer */ 133 char *readbuf; /* read transfer_buffer */ 134 char *statusbuf; /* status transfer_buffer */ 135 struct urb *readurb, *writeurb; /* The urbs */ 136 wait_queue_head_t wait; /* Zzzzz ... */ 137 int readcount; /* Counter for reads */ 138 int ifnum; /* Interface number */ 139 struct usb_interface *intf; /* The interface */ 140 /* Alternate-setting numbers and endpoints for each protocol 141 * (7/1/{index=1,2,3}) that the device supports: */ 142 struct { 143 int alt_setting; 144 struct usb_endpoint_descriptor *epwrite; 145 struct usb_endpoint_descriptor *epread; 146 } protocol[USBLP_MAX_PROTOCOLS]; 147 int current_protocol; 148 int minor; /* minor number of device */ 149 int wcomplete; /* writing is completed */ 150 int rcomplete; /* reading is completed */ 151 unsigned int quirks; /* quirks flags */ 152 unsigned char used; /* True if open */ 153 unsigned char present; /* True if not disconnected */ 154 unsigned char bidir; /* interface is bidirectional */ 155 unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */ 156 /* first 2 bytes are (big-endian) length */ 157 }; 158 159 #ifdef DEBUG 160 static void usblp_dump(struct usblp *usblp) { 161 int p; 162 163 dbg("usblp=0x%p", usblp); 164 dbg("dev=0x%p", usblp->dev); 165 dbg("present=%d", usblp->present); 166 dbg("readbuf=0x%p", usblp->readbuf); 167 dbg("writebuf=0x%p", usblp->writebuf); 168 dbg("readurb=0x%p", usblp->readurb); 169 dbg("writeurb=0x%p", usblp->writeurb); 170 dbg("readcount=%d", usblp->readcount); 171 dbg("ifnum=%d", usblp->ifnum); 172 for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) { 173 dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting); 174 dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite); 175 dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread); 176 } 177 dbg("current_protocol=%d", usblp->current_protocol); 178 dbg("minor=%d", usblp->minor); 179 dbg("wcomplete=%d", usblp->wcomplete); 180 dbg("rcomplete=%d", usblp->rcomplete); 181 dbg("quirks=%d", usblp->quirks); 182 dbg("used=%d", usblp->used); 183 dbg("bidir=%d", usblp->bidir); 184 dbg("device_id_string=\"%s\"", 185 usblp->device_id_string ? 186 usblp->device_id_string + 2 : 187 (unsigned char *)"(null)"); 188 } 189 #endif 190 191 /* Quirks: various printer quirks are handled by this table & its flags. */ 192 193 struct quirk_printer_struct { 194 __u16 vendorId; 195 __u16 productId; 196 unsigned int quirks; 197 }; 198 199 #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */ 200 #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */ 201 202 static const struct quirk_printer_struct quirk_printers[] = { 203 { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */ 204 { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */ 205 { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */ 206 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */ 207 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */ 208 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */ 209 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */ 210 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */ 211 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */ 212 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */ 213 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */ 214 { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */ 215 { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */ 216 { 0, 0 } 217 }; 218 219 static int usblp_select_alts(struct usblp *usblp); 220 static int usblp_set_protocol(struct usblp *usblp, int protocol); 221 static int usblp_cache_device_id_string(struct usblp *usblp); 222 223 /* forward reference to make our lives easier */ 224 static struct usb_driver usblp_driver; 225 static DECLARE_MUTEX(usblp_sem); /* locks the existence of usblp's */ 226 227 /* 228 * Functions for usblp control messages. 229 */ 230 231 static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len) 232 { 233 int retval; 234 int index = usblp->ifnum; 235 236 /* High byte has the interface index. 237 Low byte has the alternate setting. 238 */ 239 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) { 240 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting; 241 } 242 243 retval = usb_control_msg(usblp->dev, 244 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0), 245 request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT); 246 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d", 247 request, !!dir, recip, value, index, len, retval); 248 return retval < 0 ? retval : 0; 249 } 250 251 #define usblp_read_status(usblp, status)\ 252 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1) 253 #define usblp_get_id(usblp, config, id, maxlen)\ 254 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen) 255 #define usblp_reset(usblp)\ 256 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0) 257 258 #define usblp_hp_channel_change_request(usblp, channel, buffer) \ 259 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1) 260 261 /* 262 * See the description for usblp_select_alts() below for the usage 263 * explanation. Look into your /proc/bus/usb/devices and dmesg in 264 * case of any trouble. 265 */ 266 static int proto_bias = -1; 267 268 /* 269 * URB callback. 270 */ 271 272 static void usblp_bulk_read(struct urb *urb, struct pt_regs *regs) 273 { 274 struct usblp *usblp = urb->context; 275 276 if (!usblp || !usblp->dev || !usblp->used || !usblp->present) 277 return; 278 279 if (unlikely(urb->status)) 280 warn("usblp%d: nonzero read/write bulk status received: %d", 281 usblp->minor, urb->status); 282 usblp->rcomplete = 1; 283 wake_up_interruptible(&usblp->wait); 284 } 285 286 static void usblp_bulk_write(struct urb *urb, struct pt_regs *regs) 287 { 288 struct usblp *usblp = urb->context; 289 290 if (!usblp || !usblp->dev || !usblp->used || !usblp->present) 291 return; 292 293 if (unlikely(urb->status)) 294 warn("usblp%d: nonzero read/write bulk status received: %d", 295 usblp->minor, urb->status); 296 usblp->wcomplete = 1; 297 wake_up_interruptible(&usblp->wait); 298 } 299 300 /* 301 * Get and print printer errors. 302 */ 303 304 static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" }; 305 306 static int usblp_check_status(struct usblp *usblp, int err) 307 { 308 unsigned char status, newerr = 0; 309 int error; 310 311 error = usblp_read_status (usblp, usblp->statusbuf); 312 if (error < 0) { 313 if (printk_ratelimit()) 314 err("usblp%d: error %d reading printer status", 315 usblp->minor, error); 316 return 0; 317 } 318 319 status = *usblp->statusbuf; 320 321 if (~status & LP_PERRORP) 322 newerr = 3; 323 if (status & LP_POUTPA) 324 newerr = 1; 325 if (~status & LP_PSELECD) 326 newerr = 2; 327 328 if (newerr != err) 329 info("usblp%d: %s", usblp->minor, usblp_messages[newerr]); 330 331 return newerr; 332 } 333 334 /* 335 * File op functions. 336 */ 337 338 static int usblp_open(struct inode *inode, struct file *file) 339 { 340 int minor = iminor(inode); 341 struct usblp *usblp; 342 struct usb_interface *intf; 343 int retval; 344 345 if (minor < 0) 346 return -ENODEV; 347 348 down (&usblp_sem); 349 350 retval = -ENODEV; 351 intf = usb_find_interface(&usblp_driver, minor); 352 if (!intf) { 353 goto out; 354 } 355 usblp = usb_get_intfdata (intf); 356 if (!usblp || !usblp->dev || !usblp->present) 357 goto out; 358 359 retval = -EBUSY; 360 if (usblp->used) 361 goto out; 362 363 /* 364 * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ??? 365 * This is #if 0-ed because we *don't* want to fail an open 366 * just because the printer is off-line. 367 */ 368 #if 0 369 if ((retval = usblp_check_status(usblp, 0))) { 370 retval = retval > 1 ? -EIO : -ENOSPC; 371 goto out; 372 } 373 #else 374 retval = 0; 375 #endif 376 377 usblp->used = 1; 378 file->private_data = usblp; 379 380 usblp->writeurb->transfer_buffer_length = 0; 381 usblp->wcomplete = 1; /* we begin writeable */ 382 usblp->rcomplete = 0; 383 usblp->writeurb->status = 0; 384 usblp->readurb->status = 0; 385 386 if (usblp->bidir) { 387 usblp->readcount = 0; 388 usblp->readurb->dev = usblp->dev; 389 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) { 390 retval = -EIO; 391 usblp->used = 0; 392 file->private_data = NULL; 393 } 394 } 395 out: 396 up (&usblp_sem); 397 return retval; 398 } 399 400 static void usblp_cleanup (struct usblp *usblp) 401 { 402 info("usblp%d: removed", usblp->minor); 403 404 kfree (usblp->device_id_string); 405 kfree (usblp->statusbuf); 406 usb_free_urb(usblp->writeurb); 407 usb_free_urb(usblp->readurb); 408 kfree (usblp); 409 } 410 411 static void usblp_unlink_urbs(struct usblp *usblp) 412 { 413 usb_kill_urb(usblp->writeurb); 414 if (usblp->bidir) 415 usb_kill_urb(usblp->readurb); 416 } 417 418 static int usblp_release(struct inode *inode, struct file *file) 419 { 420 struct usblp *usblp = file->private_data; 421 422 down (&usblp_sem); 423 usblp->used = 0; 424 if (usblp->present) { 425 usblp_unlink_urbs(usblp); 426 } else /* finish cleanup from disconnect */ 427 usblp_cleanup (usblp); 428 up (&usblp_sem); 429 return 0; 430 } 431 432 /* No kernel lock - fine */ 433 static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait) 434 { 435 struct usblp *usblp = file->private_data; 436 poll_wait(file, &usblp->wait, wait); 437 return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM) 438 | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM); 439 } 440 441 static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 442 { 443 struct usblp *usblp = file->private_data; 444 int length, err, i; 445 unsigned char newChannel; 446 int status; 447 int twoints[2]; 448 int retval = 0; 449 450 down (&usblp->sem); 451 if (!usblp->present) { 452 retval = -ENODEV; 453 goto done; 454 } 455 456 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd), 457 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) ); 458 459 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */ 460 461 switch (_IOC_NR(cmd)) { 462 463 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */ 464 if (_IOC_DIR(cmd) != _IOC_READ) { 465 retval = -EINVAL; 466 goto done; 467 } 468 469 length = usblp_cache_device_id_string(usblp); 470 if (length < 0) { 471 retval = length; 472 goto done; 473 } 474 if (length > _IOC_SIZE(cmd)) 475 length = _IOC_SIZE(cmd); /* truncate */ 476 477 if (copy_to_user((void __user *) arg, 478 usblp->device_id_string, 479 (unsigned long) length)) { 480 retval = -EFAULT; 481 goto done; 482 } 483 484 break; 485 486 case IOCNR_GET_PROTOCOLS: 487 if (_IOC_DIR(cmd) != _IOC_READ || 488 _IOC_SIZE(cmd) < sizeof(twoints)) { 489 retval = -EINVAL; 490 goto done; 491 } 492 493 twoints[0] = usblp->current_protocol; 494 twoints[1] = 0; 495 for (i = USBLP_FIRST_PROTOCOL; 496 i <= USBLP_LAST_PROTOCOL; i++) { 497 if (usblp->protocol[i].alt_setting >= 0) 498 twoints[1] |= (1<<i); 499 } 500 501 if (copy_to_user((void __user *)arg, 502 (unsigned char *)twoints, 503 sizeof(twoints))) { 504 retval = -EFAULT; 505 goto done; 506 } 507 508 break; 509 510 case IOCNR_SET_PROTOCOL: 511 if (_IOC_DIR(cmd) != _IOC_WRITE) { 512 retval = -EINVAL; 513 goto done; 514 } 515 516 #ifdef DEBUG 517 if (arg == -10) { 518 usblp_dump(usblp); 519 break; 520 } 521 #endif 522 523 usblp_unlink_urbs(usblp); 524 retval = usblp_set_protocol(usblp, arg); 525 if (retval < 0) { 526 usblp_set_protocol(usblp, 527 usblp->current_protocol); 528 } 529 break; 530 531 case IOCNR_HP_SET_CHANNEL: 532 if (_IOC_DIR(cmd) != _IOC_WRITE || 533 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 || 534 usblp->quirks & USBLP_QUIRK_BIDIR) { 535 retval = -EINVAL; 536 goto done; 537 } 538 539 err = usblp_hp_channel_change_request(usblp, 540 arg, &newChannel); 541 if (err < 0) { 542 err("usblp%d: error = %d setting " 543 "HP channel", 544 usblp->minor, err); 545 retval = -EIO; 546 goto done; 547 } 548 549 dbg("usblp%d requested/got HP channel %ld/%d", 550 usblp->minor, arg, newChannel); 551 break; 552 553 case IOCNR_GET_BUS_ADDRESS: 554 if (_IOC_DIR(cmd) != _IOC_READ || 555 _IOC_SIZE(cmd) < sizeof(twoints)) { 556 retval = -EINVAL; 557 goto done; 558 } 559 560 twoints[0] = usblp->dev->bus->busnum; 561 twoints[1] = usblp->dev->devnum; 562 if (copy_to_user((void __user *)arg, 563 (unsigned char *)twoints, 564 sizeof(twoints))) { 565 retval = -EFAULT; 566 goto done; 567 } 568 569 dbg("usblp%d is bus=%d, device=%d", 570 usblp->minor, twoints[0], twoints[1]); 571 break; 572 573 case IOCNR_GET_VID_PID: 574 if (_IOC_DIR(cmd) != _IOC_READ || 575 _IOC_SIZE(cmd) < sizeof(twoints)) { 576 retval = -EINVAL; 577 goto done; 578 } 579 580 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor); 581 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct); 582 if (copy_to_user((void __user *)arg, 583 (unsigned char *)twoints, 584 sizeof(twoints))) { 585 retval = -EFAULT; 586 goto done; 587 } 588 589 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X", 590 usblp->minor, twoints[0], twoints[1]); 591 break; 592 593 case IOCNR_SOFT_RESET: 594 if (_IOC_DIR(cmd) != _IOC_NONE) { 595 retval = -EINVAL; 596 goto done; 597 } 598 retval = usblp_reset(usblp); 599 break; 600 default: 601 retval = -ENOTTY; 602 } 603 else /* old-style ioctl value */ 604 switch (cmd) { 605 606 case LPGETSTATUS: 607 if (usblp_read_status(usblp, usblp->statusbuf)) { 608 if (printk_ratelimit()) 609 err("usblp%d: failed reading printer status", 610 usblp->minor); 611 retval = -EIO; 612 goto done; 613 } 614 status = *usblp->statusbuf; 615 if (copy_to_user ((void __user *)arg, &status, sizeof(int))) 616 retval = -EFAULT; 617 break; 618 619 default: 620 retval = -ENOTTY; 621 } 622 623 done: 624 up (&usblp->sem); 625 return retval; 626 } 627 628 static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 629 { 630 DECLARE_WAITQUEUE(wait, current); 631 struct usblp *usblp = file->private_data; 632 int timeout, err = 0, transfer_length = 0; 633 size_t writecount = 0; 634 635 while (writecount < count) { 636 if (!usblp->wcomplete) { 637 barrier(); 638 if (file->f_flags & O_NONBLOCK) { 639 writecount += transfer_length; 640 return writecount ? writecount : -EAGAIN; 641 } 642 643 timeout = USBLP_WRITE_TIMEOUT; 644 add_wait_queue(&usblp->wait, &wait); 645 while ( 1==1 ) { 646 647 if (signal_pending(current)) { 648 remove_wait_queue(&usblp->wait, &wait); 649 return writecount ? writecount : -EINTR; 650 } 651 set_current_state(TASK_INTERRUPTIBLE); 652 if (timeout && !usblp->wcomplete) { 653 timeout = schedule_timeout(timeout); 654 } else { 655 set_current_state(TASK_RUNNING); 656 break; 657 } 658 } 659 remove_wait_queue(&usblp->wait, &wait); 660 } 661 662 down (&usblp->sem); 663 if (!usblp->present) { 664 up (&usblp->sem); 665 return -ENODEV; 666 } 667 668 if (usblp->writeurb->status != 0) { 669 if (usblp->quirks & USBLP_QUIRK_BIDIR) { 670 if (!usblp->wcomplete) 671 err("usblp%d: error %d writing to printer", 672 usblp->minor, usblp->writeurb->status); 673 err = usblp->writeurb->status; 674 } else 675 err = usblp_check_status(usblp, err); 676 up (&usblp->sem); 677 678 /* if the fault was due to disconnect, let khubd's 679 * call to usblp_disconnect() grab usblp->sem ... 680 */ 681 schedule (); 682 continue; 683 } 684 685 /* We must increment writecount here, and not at the 686 * end of the loop. Otherwise, the final loop iteration may 687 * be skipped, leading to incomplete printer output. 688 */ 689 writecount += transfer_length; 690 if (writecount == count) { 691 up(&usblp->sem); 692 break; 693 } 694 695 transfer_length=(count - writecount); 696 if (transfer_length > USBLP_BUF_SIZE) 697 transfer_length = USBLP_BUF_SIZE; 698 699 usblp->writeurb->transfer_buffer_length = transfer_length; 700 701 if (copy_from_user(usblp->writeurb->transfer_buffer, 702 buffer + writecount, transfer_length)) { 703 up(&usblp->sem); 704 return writecount ? writecount : -EFAULT; 705 } 706 707 usblp->writeurb->dev = usblp->dev; 708 usblp->wcomplete = 0; 709 err = usb_submit_urb(usblp->writeurb, GFP_KERNEL); 710 if (err) { 711 if (err != -ENOMEM) 712 count = -EIO; 713 else 714 count = writecount ? writecount : -ENOMEM; 715 up (&usblp->sem); 716 break; 717 } 718 up (&usblp->sem); 719 } 720 721 return count; 722 } 723 724 static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) 725 { 726 struct usblp *usblp = file->private_data; 727 DECLARE_WAITQUEUE(wait, current); 728 729 if (!usblp->bidir) 730 return -EINVAL; 731 732 down (&usblp->sem); 733 if (!usblp->present) { 734 count = -ENODEV; 735 goto done; 736 } 737 738 if (!usblp->rcomplete) { 739 barrier(); 740 741 if (file->f_flags & O_NONBLOCK) { 742 count = -EAGAIN; 743 goto done; 744 } 745 746 add_wait_queue(&usblp->wait, &wait); 747 while (1==1) { 748 if (signal_pending(current)) { 749 count = -EINTR; 750 remove_wait_queue(&usblp->wait, &wait); 751 goto done; 752 } 753 up (&usblp->sem); 754 set_current_state(TASK_INTERRUPTIBLE); 755 if (!usblp->rcomplete) { 756 schedule(); 757 } else { 758 set_current_state(TASK_RUNNING); 759 down(&usblp->sem); 760 break; 761 } 762 down (&usblp->sem); 763 } 764 remove_wait_queue(&usblp->wait, &wait); 765 } 766 767 if (!usblp->present) { 768 count = -ENODEV; 769 goto done; 770 } 771 772 if (usblp->readurb->status) { 773 err("usblp%d: error %d reading from printer", 774 usblp->minor, usblp->readurb->status); 775 usblp->readurb->dev = usblp->dev; 776 usblp->readcount = 0; 777 usblp->rcomplete = 0; 778 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) 779 dbg("error submitting urb"); 780 count = -EIO; 781 goto done; 782 } 783 784 count = count < usblp->readurb->actual_length - usblp->readcount ? 785 count : usblp->readurb->actual_length - usblp->readcount; 786 787 if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) { 788 count = -EFAULT; 789 goto done; 790 } 791 792 if ((usblp->readcount += count) == usblp->readurb->actual_length) { 793 usblp->readcount = 0; 794 usblp->readurb->dev = usblp->dev; 795 usblp->rcomplete = 0; 796 if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) { 797 count = -EIO; 798 goto done; 799 } 800 } 801 802 done: 803 up (&usblp->sem); 804 return count; 805 } 806 807 /* 808 * Checks for printers that have quirks, such as requiring unidirectional 809 * communication but reporting bidirectional; currently some HP printers 810 * have this flaw (HP 810, 880, 895, etc.), or needing an init string 811 * sent at each open (like some Epsons). 812 * Returns 1 if found, 0 if not found. 813 * 814 * HP recommended that we use the bidirectional interface but 815 * don't attempt any bulk IN transfers from the IN endpoint. 816 * Here's some more detail on the problem: 817 * The problem is not that it isn't bidirectional though. The problem 818 * is that if you request a device ID, or status information, while 819 * the buffers are full, the return data will end up in the print data 820 * buffer. For example if you make sure you never request the device ID 821 * while you are sending print data, and you don't try to query the 822 * printer status every couple of milliseconds, you will probably be OK. 823 */ 824 static unsigned int usblp_quirks (__u16 vendor, __u16 product) 825 { 826 int i; 827 828 for (i = 0; quirk_printers[i].vendorId; i++) { 829 if (vendor == quirk_printers[i].vendorId && 830 product == quirk_printers[i].productId) 831 return quirk_printers[i].quirks; 832 } 833 return 0; 834 } 835 836 static struct file_operations usblp_fops = { 837 .owner = THIS_MODULE, 838 .read = usblp_read, 839 .write = usblp_write, 840 .poll = usblp_poll, 841 .unlocked_ioctl = usblp_ioctl, 842 .compat_ioctl = usblp_ioctl, 843 .open = usblp_open, 844 .release = usblp_release, 845 }; 846 847 static struct usb_class_driver usblp_class = { 848 .name = "lp%d", 849 .fops = &usblp_fops, 850 .minor_base = USBLP_MINOR_BASE, 851 }; 852 853 static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf) 854 { 855 struct usb_interface *intf = to_usb_interface(dev); 856 struct usblp *usblp = usb_get_intfdata (intf); 857 858 if (usblp->device_id_string[0] == 0 && 859 usblp->device_id_string[1] == 0) 860 return 0; 861 862 return sprintf(buf, "%s", usblp->device_id_string+2); 863 } 864 865 static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL); 866 867 static int usblp_probe(struct usb_interface *intf, 868 const struct usb_device_id *id) 869 { 870 struct usb_device *dev = interface_to_usbdev (intf); 871 struct usblp *usblp = NULL; 872 int protocol; 873 int retval; 874 875 /* Malloc and start initializing usblp structure so we can use it 876 * directly. */ 877 if (!(usblp = kmalloc(sizeof(struct usblp), GFP_KERNEL))) { 878 err("out of memory for usblp"); 879 goto abort; 880 } 881 memset(usblp, 0, sizeof(struct usblp)); 882 usblp->dev = dev; 883 init_MUTEX (&usblp->sem); 884 init_waitqueue_head(&usblp->wait); 885 usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber; 886 usblp->intf = intf; 887 888 usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL); 889 if (!usblp->writeurb) { 890 err("out of memory"); 891 goto abort; 892 } 893 usblp->readurb = usb_alloc_urb(0, GFP_KERNEL); 894 if (!usblp->readurb) { 895 err("out of memory"); 896 goto abort; 897 } 898 899 /* Malloc device ID string buffer to the largest expected length, 900 * since we can re-query it on an ioctl and a dynamic string 901 * could change in length. */ 902 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) { 903 err("out of memory for device_id_string"); 904 goto abort; 905 } 906 907 usblp->writebuf = usblp->readbuf = NULL; 908 usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 909 usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 910 /* Malloc write & read buffers. We somewhat wastefully 911 * malloc both regardless of bidirectionality, because the 912 * alternate setting can be changed later via an ioctl. */ 913 if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE, 914 GFP_KERNEL, &usblp->writeurb->transfer_dma))) { 915 err("out of memory for write buf"); 916 goto abort; 917 } 918 if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE, 919 GFP_KERNEL, &usblp->readurb->transfer_dma))) { 920 err("out of memory for read buf"); 921 goto abort; 922 } 923 924 /* Allocate buffer for printer status */ 925 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL); 926 if (!usblp->statusbuf) { 927 err("out of memory for statusbuf"); 928 goto abort; 929 } 930 931 /* Lookup quirks for this printer. */ 932 usblp->quirks = usblp_quirks( 933 le16_to_cpu(dev->descriptor.idVendor), 934 le16_to_cpu(dev->descriptor.idProduct)); 935 936 /* Analyze and pick initial alternate settings and endpoints. */ 937 protocol = usblp_select_alts(usblp); 938 if (protocol < 0) { 939 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X", 940 le16_to_cpu(dev->descriptor.idVendor), 941 le16_to_cpu(dev->descriptor.idProduct)); 942 goto abort; 943 } 944 945 /* Setup the selected alternate setting and endpoints. */ 946 if (usblp_set_protocol(usblp, protocol) < 0) 947 goto abort; 948 949 /* Retrieve and store the device ID string. */ 950 usblp_cache_device_id_string(usblp); 951 device_create_file(&intf->dev, &dev_attr_ieee1284_id); 952 953 #ifdef DEBUG 954 usblp_check_status(usblp, 0); 955 #endif 956 957 usb_set_intfdata (intf, usblp); 958 959 usblp->present = 1; 960 961 retval = usb_register_dev(intf, &usblp_class); 962 if (retval) { 963 err("Not able to get a minor for this device."); 964 goto abort_intfdata; 965 } 966 usblp->minor = intf->minor; 967 info("usblp%d: USB %sdirectional printer dev %d " 968 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X", 969 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum, 970 usblp->ifnum, 971 usblp->protocol[usblp->current_protocol].alt_setting, 972 usblp->current_protocol, 973 le16_to_cpu(usblp->dev->descriptor.idVendor), 974 le16_to_cpu(usblp->dev->descriptor.idProduct)); 975 976 return 0; 977 978 abort_intfdata: 979 usb_set_intfdata (intf, NULL); 980 device_remove_file(&intf->dev, &dev_attr_ieee1284_id); 981 abort: 982 if (usblp) { 983 if (usblp->writebuf) 984 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE, 985 usblp->writebuf, usblp->writeurb->transfer_dma); 986 if (usblp->readbuf) 987 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE, 988 usblp->readbuf, usblp->writeurb->transfer_dma); 989 kfree(usblp->statusbuf); 990 kfree(usblp->device_id_string); 991 usb_free_urb(usblp->writeurb); 992 usb_free_urb(usblp->readurb); 993 kfree(usblp); 994 } 995 return -EIO; 996 } 997 998 /* 999 * We are a "new" style driver with usb_device_id table, 1000 * but our requirements are too intricate for simple match to handle. 1001 * 1002 * The "proto_bias" option may be used to specify the preferred protocol 1003 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device 1004 * supports the preferred protocol, then we bind to it. 1005 * 1006 * The best interface for us is 7/1/2, because it is compatible 1007 * with a stream of characters. If we find it, we bind to it. 1008 * 1009 * Note that the people from hpoj.sourceforge.net need to be able to 1010 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose. 1011 * 1012 * Failing 7/1/2, we look for 7/1/3, even though it's probably not 1013 * stream-compatible, because this matches the behaviour of the old code. 1014 * 1015 * If nothing else, we bind to 7/1/1 - the unidirectional interface. 1016 */ 1017 static int usblp_select_alts(struct usblp *usblp) 1018 { 1019 struct usb_interface *if_alt; 1020 struct usb_host_interface *ifd; 1021 struct usb_endpoint_descriptor *epd, *epwrite, *epread; 1022 int p, i, e; 1023 1024 if_alt = usblp->intf; 1025 1026 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++) 1027 usblp->protocol[p].alt_setting = -1; 1028 1029 /* Find out what we have. */ 1030 for (i = 0; i < if_alt->num_altsetting; i++) { 1031 ifd = &if_alt->altsetting[i]; 1032 1033 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1) 1034 continue; 1035 1036 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL || 1037 ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL) 1038 continue; 1039 1040 /* Look for bulk OUT and IN endpoints. */ 1041 epwrite = epread = NULL; 1042 for (e = 0; e < ifd->desc.bNumEndpoints; e++) { 1043 epd = &ifd->endpoint[e].desc; 1044 1045 if ((epd->bmAttributes&USB_ENDPOINT_XFERTYPE_MASK)!= 1046 USB_ENDPOINT_XFER_BULK) 1047 continue; 1048 1049 if (!(epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK)) { 1050 if (!epwrite) 1051 epwrite = epd; 1052 1053 } else { 1054 if (!epread) 1055 epread = epd; 1056 } 1057 } 1058 1059 /* Ignore buggy hardware without the right endpoints. */ 1060 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread)) 1061 continue; 1062 1063 /* Turn off reads for 7/1/1 (unidirectional) interfaces 1064 * and buggy bidirectional printers. */ 1065 if (ifd->desc.bInterfaceProtocol == 1) { 1066 epread = NULL; 1067 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) { 1068 info("Disabling reads from problem bidirectional " 1069 "printer on usblp%d", usblp->minor); 1070 epread = NULL; 1071 } 1072 1073 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting = 1074 ifd->desc.bAlternateSetting; 1075 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite; 1076 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread; 1077 } 1078 1079 /* If our requested protocol is supported, then use it. */ 1080 if (proto_bias >= USBLP_FIRST_PROTOCOL && 1081 proto_bias <= USBLP_LAST_PROTOCOL && 1082 usblp->protocol[proto_bias].alt_setting != -1) 1083 return proto_bias; 1084 1085 /* Ordering is important here. */ 1086 if (usblp->protocol[2].alt_setting != -1) 1087 return 2; 1088 if (usblp->protocol[1].alt_setting != -1) 1089 return 1; 1090 if (usblp->protocol[3].alt_setting != -1) 1091 return 3; 1092 1093 /* If nothing is available, then don't bind to this device. */ 1094 return -1; 1095 } 1096 1097 static int usblp_set_protocol(struct usblp *usblp, int protocol) 1098 { 1099 int r, alts; 1100 1101 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL) 1102 return -EINVAL; 1103 1104 alts = usblp->protocol[protocol].alt_setting; 1105 if (alts < 0) 1106 return -EINVAL; 1107 r = usb_set_interface(usblp->dev, usblp->ifnum, alts); 1108 if (r < 0) { 1109 err("can't set desired altsetting %d on interface %d", 1110 alts, usblp->ifnum); 1111 return r; 1112 } 1113 1114 usb_fill_bulk_urb(usblp->writeurb, usblp->dev, 1115 usb_sndbulkpipe(usblp->dev, 1116 usblp->protocol[protocol].epwrite->bEndpointAddress), 1117 usblp->writebuf, 0, 1118 usblp_bulk_write, usblp); 1119 1120 usblp->bidir = (usblp->protocol[protocol].epread != NULL); 1121 if (usblp->bidir) 1122 usb_fill_bulk_urb(usblp->readurb, usblp->dev, 1123 usb_rcvbulkpipe(usblp->dev, 1124 usblp->protocol[protocol].epread->bEndpointAddress), 1125 usblp->readbuf, USBLP_BUF_SIZE, 1126 usblp_bulk_read, usblp); 1127 1128 usblp->current_protocol = protocol; 1129 dbg("usblp%d set protocol %d", usblp->minor, protocol); 1130 return 0; 1131 } 1132 1133 /* Retrieves and caches device ID string. 1134 * Returns length, including length bytes but not null terminator. 1135 * On error, returns a negative errno value. */ 1136 static int usblp_cache_device_id_string(struct usblp *usblp) 1137 { 1138 int err, length; 1139 1140 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1); 1141 if (err < 0) { 1142 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string", 1143 usblp->minor, err); 1144 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0'; 1145 return -EIO; 1146 } 1147 1148 /* First two bytes are length in big-endian. 1149 * They count themselves, and we copy them into 1150 * the user's buffer. */ 1151 length = be16_to_cpu(*((__be16 *)usblp->device_id_string)); 1152 if (length < 2) 1153 length = 2; 1154 else if (length >= USBLP_DEVICE_ID_SIZE) 1155 length = USBLP_DEVICE_ID_SIZE - 1; 1156 usblp->device_id_string[length] = '\0'; 1157 1158 dbg("usblp%d Device ID string [len=%d]=\"%s\"", 1159 usblp->minor, length, &usblp->device_id_string[2]); 1160 1161 return length; 1162 } 1163 1164 static void usblp_disconnect(struct usb_interface *intf) 1165 { 1166 struct usblp *usblp = usb_get_intfdata (intf); 1167 1168 usb_deregister_dev(intf, &usblp_class); 1169 1170 if (!usblp || !usblp->dev) { 1171 err("bogus disconnect"); 1172 BUG (); 1173 } 1174 1175 device_remove_file(&intf->dev, &dev_attr_ieee1284_id); 1176 1177 down (&usblp_sem); 1178 down (&usblp->sem); 1179 usblp->present = 0; 1180 usb_set_intfdata (intf, NULL); 1181 1182 usblp_unlink_urbs(usblp); 1183 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE, 1184 usblp->writebuf, usblp->writeurb->transfer_dma); 1185 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE, 1186 usblp->readbuf, usblp->readurb->transfer_dma); 1187 up (&usblp->sem); 1188 1189 if (!usblp->used) 1190 usblp_cleanup (usblp); 1191 up (&usblp_sem); 1192 } 1193 1194 static struct usb_device_id usblp_ids [] = { 1195 { USB_DEVICE_INFO(7, 1, 1) }, 1196 { USB_DEVICE_INFO(7, 1, 2) }, 1197 { USB_DEVICE_INFO(7, 1, 3) }, 1198 { USB_INTERFACE_INFO(7, 1, 1) }, 1199 { USB_INTERFACE_INFO(7, 1, 2) }, 1200 { USB_INTERFACE_INFO(7, 1, 3) }, 1201 { } /* Terminating entry */ 1202 }; 1203 1204 MODULE_DEVICE_TABLE (usb, usblp_ids); 1205 1206 static struct usb_driver usblp_driver = { 1207 .name = "usblp", 1208 .probe = usblp_probe, 1209 .disconnect = usblp_disconnect, 1210 .id_table = usblp_ids, 1211 }; 1212 1213 static int __init usblp_init(void) 1214 { 1215 int retval; 1216 retval = usb_register(&usblp_driver); 1217 if (retval) 1218 goto out; 1219 info(DRIVER_VERSION ": " DRIVER_DESC); 1220 out: 1221 return retval; 1222 } 1223 1224 static void __exit usblp_exit(void) 1225 { 1226 usb_deregister(&usblp_driver); 1227 } 1228 1229 module_init(usblp_init); 1230 module_exit(usblp_exit); 1231 1232 MODULE_AUTHOR( DRIVER_AUTHOR ); 1233 MODULE_DESCRIPTION( DRIVER_DESC ); 1234 module_param(proto_bias, int, S_IRUGO | S_IWUSR); 1235 MODULE_PARM_DESC(proto_bias, "Favourite protocol number"); 1236 MODULE_LICENSE("GPL"); 1237