ss.c revision 1.81
1/* $NetBSD: ss.c,v 1.81 2012/02/28 11:41:00 mbalmer Exp $ */ 2 3/* 4 * Copyright (c) 1995 Kenneth Stailey. All rights reserved. 5 * modified for configurable scanner support by Joachim Koenig 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Kenneth Stailey. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__KERNEL_RCSID(0, "$NetBSD: ss.c,v 1.81 2012/02/28 11:41:00 mbalmer Exp $"); 35 36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/fcntl.h> 39#include <sys/errno.h> 40#include <sys/ioctl.h> 41#include <sys/malloc.h> 42#include <sys/buf.h> 43#include <sys/bufq.h> 44#include <sys/proc.h> 45#include <sys/device.h> 46#include <sys/conf.h> 47#include <sys/vnode.h> 48#include <sys/scanio.h> 49 50#include <dev/scsipi/scsi_all.h> 51#include <dev/scsipi/scsipi_all.h> 52#include <dev/scsipi/scsi_scanner.h> 53#include <dev/scsipi/scsiconf.h> 54#include <dev/scsipi/ssvar.h> 55 56#include <dev/scsipi/ss_mustek.h> 57 58#define SSMODE(z) ( minor(z) & 0x03) 59#define SSUNIT(z) ((minor(z) >> 4) ) 60#define SSNMINOR 16 61 62/* 63 * If the mode is 3 (e.g. minor = 3,7,11,15) 64 * then the device has been openned to set defaults 65 * This mode does NOT ALLOW I/O, only ioctls 66 */ 67#define MODE_REWIND 0 68#define MODE_NONREWIND 1 69#define MODE_CONTROL 3 70 71static int ssmatch(device_t, cfdata_t, void *); 72static void ssattach(device_t, device_t, void *); 73static int ssdetach(device_t self, int flags); 74 75CFATTACH_DECL_NEW( 76 ss, 77 sizeof(struct ss_softc), 78 ssmatch, 79 ssattach, 80 ssdetach, 81 NULL 82); 83 84extern struct cfdriver ss_cd; 85 86static dev_type_open(ssopen); 87static dev_type_close(ssclose); 88static dev_type_read(ssread); 89static dev_type_ioctl(ssioctl); 90 91const struct cdevsw ss_cdevsw = { 92 ssopen, ssclose, ssread, nowrite, ssioctl, 93 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER, 94}; 95 96static void ssstrategy(struct buf *); 97static void ssstart(struct scsipi_periph *); 98static void ssdone(struct scsipi_xfer *, int); 99static void ssminphys(struct buf *); 100 101static const struct scsipi_periphsw ss_switch = { 102 NULL, 103 ssstart, 104 NULL, 105 ssdone, 106}; 107 108static const struct scsipi_inquiry_pattern ss_patterns[] = { 109 {T_SCANNER, T_FIXED, 110 "", "", ""}, 111 {T_SCANNER, T_REMOV, 112 "", "", ""}, 113 {T_PROCESSOR, T_FIXED, 114 "HP ", "C1130A ", ""}, 115 {T_PROCESSOR, T_FIXED, 116 "HP ", "C1750A ", ""}, 117 {T_PROCESSOR, T_FIXED, 118 "HP ", "C2500A ", ""}, 119 {T_PROCESSOR, T_FIXED, 120 "HP ", "C2520A ", ""}, 121 {T_PROCESSOR, T_FIXED, 122 "HP ", "C5110A ", ""}, 123 {T_PROCESSOR, T_FIXED, 124 "HP ", "C7670A ", ""}, 125 {T_PROCESSOR, T_FIXED, 126 "HP ", "", ""}, 127}; 128 129static int 130ssmatch(device_t parent, cfdata_t match, void *aux) 131{ 132 struct scsipibus_attach_args *sa = aux; 133 int priority; 134 135 (void)scsipi_inqmatch(&sa->sa_inqbuf, 136 ss_patterns, sizeof(ss_patterns) / sizeof(ss_patterns[0]), 137 sizeof(ss_patterns[0]), &priority); 138 return priority; 139} 140 141/* 142 * The routine called by the low level scsi routine when it discovers 143 * A device suitable for this driver 144 * If it is a know special, call special attach routine to install 145 * special handlers into the ss_softc structure 146 */ 147static void 148ssattach(device_t parent, device_t self, void *aux) 149{ 150 struct ss_softc *ss = device_private(self); 151 struct scsipibus_attach_args *sa = aux; 152 struct scsipi_periph *periph = sa->sa_periph; 153 154 SC_DEBUG(periph, SCSIPI_DB2, ("ssattach: ")); 155 ss->sc_dev = self; 156 157 ss->flags |= SSF_AUTOCONF; 158 159 /* Store information needed to contact our base driver */ 160 ss->sc_periph = periph; 161 periph->periph_dev = ss->sc_dev; 162 periph->periph_switch = &ss_switch; 163 164 printf("\n"); 165 166 /* Set up the buf queue for this device */ 167 bufq_alloc(&ss->buf_queue, "fcfs", 0); 168 169 callout_init(&ss->sc_callout, 0); 170 171 /* 172 * look for non-standard scanners with help of the quirk table 173 * and install functions for special handling 174 */ 175 SC_DEBUG(periph, SCSIPI_DB2, ("ssattach:\n")); 176 if (memcmp(sa->sa_inqbuf.vendor, "MUSTEK", 6) == 0) 177 mustek_attach(ss, sa); 178 if (memcmp(sa->sa_inqbuf.vendor, "HP ", 8) == 0 && 179 memcmp(sa->sa_inqbuf.product, "ScanJet 5300C", 13) != 0) 180 scanjet_attach(ss, sa); 181#if 0 182 if (ss->special == NULL) { 183 /* XXX add code to restart a SCSI2 scanner, if any */ 184 } 185#endif 186 ss->flags &= ~SSF_AUTOCONF; 187} 188 189static int 190ssdetach(device_t self, int flags) 191{ 192 struct ss_softc *ss = device_private(self); 193 int s, cmaj, mn; 194 195 /* locate the major number */ 196 cmaj = cdevsw_lookup_major(&ss_cdevsw); 197 198 /* kill any pending restart */ 199 callout_stop(&ss->sc_callout); 200 201 s = splbio(); 202 203 /* Kill off any queued buffers. */ 204 bufq_drain(ss->buf_queue); 205 206 bufq_free(ss->buf_queue); 207 208 /* Kill off any pending commands. */ 209 scsipi_kill_pending(ss->sc_periph); 210 211 splx(s); 212 213 /* Nuke the vnodes for any open instances */ 214 mn = SSUNIT(device_unit(self)); 215 vdevgone(cmaj, mn, mn+SSNMINOR-1, VCHR); 216 217 return 0; 218} 219 220/* 221 * open the device. 222 */ 223static int 224ssopen(dev_t dev, int flag, int mode, struct lwp *l) 225{ 226 int unit; 227 u_int ssmode; 228 int error; 229 struct ss_softc *ss; 230 struct scsipi_periph *periph; 231 struct scsipi_adapter *adapt; 232 233 unit = SSUNIT(dev); 234 ss = device_lookup_private(&ss_cd, unit); 235 if (ss == NULL) 236 return ENXIO; 237 238 if (!device_is_active(ss->sc_dev)) 239 return ENODEV; 240 241 ssmode = SSMODE(dev); 242 243 periph = ss->sc_periph; 244 adapt = periph->periph_channel->chan_adapter; 245 246 SC_DEBUG(periph, SCSIPI_DB1, 247 ("open: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit, 248 ss_cd.cd_ndevs)); 249 250 if (periph->periph_flags & PERIPH_OPEN) { 251 aprint_error_dev(ss->sc_dev, "already open\n"); 252 return EBUSY; 253 } 254 255 if ((error = scsipi_adapter_addref(adapt)) != 0) 256 return error; 257 258 /* 259 * Catch any unit attention errors. 260 * 261 * XS_CTL_IGNORE_MEDIA_CHANGE: when you have an ADF, some scanners 262 * consider paper to be a changeable media 263 * 264 */ 265 error = scsipi_test_unit_ready(periph, 266 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_IGNORE_ILLEGAL_REQUEST | 267 (ssmode == MODE_CONTROL ? XS_CTL_IGNORE_NOT_READY : 0)); 268 if (error) 269 goto bad; 270 271 periph->periph_flags |= PERIPH_OPEN; /* unit attn now errors */ 272 273 /* 274 * If the mode is 3 (e.g. minor = 3,7,11,15) 275 * then the device has been opened to set defaults 276 * This mode does NOT ALLOW I/O, only ioctls 277 */ 278 if (ssmode == MODE_CONTROL) 279 return 0; 280 281 SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n")); 282 return 0; 283 284bad: 285 scsipi_adapter_delref(adapt); 286 periph->periph_flags &= ~PERIPH_OPEN; 287 return error; 288} 289 290/* 291 * close the device.. only called if we are the LAST 292 * occurence of an open device 293 */ 294static int 295ssclose(dev_t dev, int flag, int mode, struct lwp *l) 296{ 297 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev)); 298 struct scsipi_periph *periph = ss->sc_periph; 299 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 300 int error; 301 302 SC_DEBUG(ss->sc_periph, SCSIPI_DB1, ("closing\n")); 303 304 if (SSMODE(dev) == MODE_REWIND) { 305 if (ss->special && ss->special->rewind_scanner) { 306 /* call special handler to rewind/abort scan */ 307 error = (ss->special->rewind_scanner)(ss); 308 if (error) 309 return error; 310 } 311#if 0 312 else { 313 /* XXX add code to restart a SCSI2 scanner, if any */ 314 } 315#endif 316 ss->sio.scan_window_size = 0; 317 ss->flags &= ~SSF_TRIGGERED; 318 } 319 320 scsipi_wait_drain(periph); 321 322 scsipi_adapter_delref(adapt); 323 periph->periph_flags &= ~PERIPH_OPEN; 324 325 return 0; 326} 327 328/* 329 * trim the size of the transfer if needed, 330 * called by physio 331 * basically the smaller of our min and the scsi driver's 332 * minphys 333 */ 334static void 335ssminphys(struct buf *bp) 336{ 337 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(bp->b_dev)); 338 struct scsipi_periph *periph = ss->sc_periph; 339 340 scsipi_adapter_minphys(periph->periph_channel, bp); 341 342 /* 343 * trim the transfer further for special devices this is 344 * because some scanners only read multiples of a line at a 345 * time, also some cannot disconnect, so the read must be 346 * short enough to happen quickly 347 */ 348 if (ss->special && ss->special->minphys) 349 (ss->special->minphys)(ss, bp); 350} 351 352/* 353 * Do a read on a device for a user process. 354 * Prime scanner at start of read, check uio values, call ssstrategy 355 * via physio for the actual transfer. 356 */ 357static int 358ssread(dev_t dev, struct uio *uio, int flag) 359{ 360 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev)); 361 int error; 362 363 if (!device_is_active(ss->sc_dev)) 364 return ENODEV; 365 366 /* if the scanner has not yet been started, do it now */ 367 if (!(ss->flags & SSF_TRIGGERED)) { 368 if (ss->special && ss->special->trigger_scanner) { 369 error = (ss->special->trigger_scanner)(ss); 370 if (error) 371 return (error); 372 } 373 ss->flags |= SSF_TRIGGERED; 374 } 375 376 return physio(ssstrategy, NULL, dev, B_READ, ssminphys, uio); 377} 378 379/* 380 * Actually translate the requested transfer into one the physical 381 * driver can understand The transfer is described by a buf and will 382 * include only one physical transfer. 383 */ 384static void 385ssstrategy(struct buf *bp) 386{ 387 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(bp->b_dev)); 388 struct scsipi_periph *periph = ss->sc_periph; 389 int s; 390 391 SC_DEBUG(ss->sc_periph, SCSIPI_DB1, 392 ("ssstrategy %d bytes @ blk %" PRId64 "\n", bp->b_bcount, 393 bp->b_blkno)); 394 395 /* If the device has been made invalid, error out */ 396 if (!device_is_active(ss->sc_dev)) { 397 if (periph->periph_flags & PERIPH_OPEN) 398 bp->b_error = EIO; 399 else 400 bp->b_error = ENODEV; 401 goto done; 402 } 403 404 /* If negative offset, error */ 405 if (bp->b_blkno < 0) { 406 bp->b_error = EINVAL; 407 goto done; 408 } 409 410 if (bp->b_bcount > ss->sio.scan_window_size) 411 bp->b_bcount = ss->sio.scan_window_size; 412 413 /* If it's a null transfer, return immediatly */ 414 if (bp->b_bcount == 0) 415 goto done; 416 417 s = splbio(); 418 419 /* 420 * Place it in the queue of activities for this scanner 421 * at the end (a bit silly because we only have on user.. 422 * (but it could fork())) 423 */ 424 bufq_put(ss->buf_queue, bp); 425 426 /* 427 * Tell the device to get going on the transfer if it's 428 * not doing anything, otherwise just wait for completion 429 * (All a bit silly if we're only allowing 1 open but..) 430 */ 431 ssstart(ss->sc_periph); 432 433 splx(s); 434 return; 435done: 436 /* Correctly set the buf to indicate a completed xfer */ 437 bp->b_resid = bp->b_bcount; 438 biodone(bp); 439} 440 441/* 442 * ssstart looks to see if there is a buf waiting for the device 443 * and that the device is not already busy. If both are true, 444 * It dequeues the buf and creates a scsi command to perform the 445 * transfer required. The transfer request will call scsipi_done 446 * on completion, which will in turn call this routine again 447 * so that the next queued transfer is performed. 448 * The bufs are queued by the strategy routine (ssstrategy) 449 * 450 * This routine is also called after other non-queued requests 451 * have been made of the scsi driver, to ensure that the queue 452 * continues to be drained. 453 * ssstart() is called at splbio 454 */ 455static void 456ssstart(struct scsipi_periph *periph) 457{ 458 struct ss_softc *ss = device_private(periph->periph_dev); 459 struct buf *bp; 460 461 SC_DEBUG(periph, SCSIPI_DB2, ("ssstart ")); 462 /* 463 * See if there is a buf to do and we are not already 464 * doing one 465 */ 466 while (periph->periph_active < periph->periph_openings) { 467 /* if a special awaits, let it proceed first */ 468 if (periph->periph_flags & PERIPH_WAITING) { 469 periph->periph_flags &= ~PERIPH_WAITING; 470 wakeup((void *)periph); 471 return; 472 } 473 474 /* 475 * See if there is a buf with work for us to do.. 476 */ 477 if ((bp = bufq_peek(ss->buf_queue)) == NULL) 478 return; 479 480 if (ss->special && ss->special->read) 481 (ss->special->read)(ss, bp); 482#if 0 483 } else { 484 /* generic scsi2 scanner read */ 485 /* XXX add code for SCSI2 scanner read */ 486 } 487#endif 488 } 489} 490 491void 492ssrestart(void *v) 493{ 494 int s = splbio(); 495 ssstart((struct scsipi_periph *)v); 496 splx(s); 497} 498 499static void 500ssdone(struct scsipi_xfer *xs, int error) 501{ 502 struct buf *bp = xs->bp; 503 504 if (bp) { 505 bp->b_error = error; 506 bp->b_resid = xs->resid; 507 biodone(bp); 508 } 509} 510 511 512/* 513 * Perform special action on behalf of the user; 514 * knows about the internals of this device 515 */ 516int 517ssioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 518{ 519 struct ss_softc *ss = device_lookup_private(&ss_cd, SSUNIT(dev)); 520 int error = 0; 521 struct scan_io *sio; 522 523 if (!device_is_active(ss->sc_dev)) 524 return ENODEV; 525 526 switch (cmd) { 527 case SCIOCGET: 528 if (ss->special && ss->special->get_params) { 529 /* call special handler */ 530 error = (ss->special->get_params)(ss); 531 if (error) 532 return error; 533 } else 534 /* XXX add code for SCSI2 scanner, if any */ 535 return EOPNOTSUPP; 536 memcpy(addr, &ss->sio, sizeof(struct scan_io)); 537 break; 538 case SCIOCSET: 539 sio = (struct scan_io *)addr; 540 541 if (ss->special && ss->special->set_params) { 542 /* call special handler */ 543 error = (ss->special->set_params)(ss, sio); 544 if (error) 545 return error; 546 } else 547 /* XXX add code for SCSI2 scanner, if any */ 548 return EOPNOTSUPP; 549 break; 550 case SCIOCRESTART: 551 if (ss->special && ss->special->rewind_scanner ) { 552 /* call special handler */ 553 error = (ss->special->rewind_scanner)(ss); 554 if (error) 555 return error; 556 } else 557 /* XXX add code for SCSI2 scanner, if any */ 558 return EOPNOTSUPP; 559 ss->flags &= ~SSF_TRIGGERED; 560 break; 561#ifdef NOTYET 562 case SCAN_USE_ADF: 563 break; 564#endif 565 default: 566 return scsipi_do_ioctl(ss->sc_periph, dev, cmd, addr, flag, l); 567 } 568 return error; 569} 570