1 #include <sys/cdefs.h> 2 __FBSDID("$FreeBSD$"); 3 4 /* $NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $ */ 5 6 /*- 7 * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc. 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to The NetBSD Foundation 11 * by Lennart Augustsson (lennart@augustsson.net) at 12 * Carlstedt Research & Technology. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /* 37 * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF 38 * Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf 39 */ 40 41 #include <sys/stdint.h> 42 #include <sys/stddef.h> 43 #include <sys/param.h> 44 #include <sys/queue.h> 45 #include <sys/types.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/bus.h> 49 #include <sys/module.h> 50 #include <sys/lock.h> 51 #include <sys/mutex.h> 52 #include <sys/condvar.h> 53 #include <sys/sysctl.h> 54 #include <sys/sx.h> 55 #include <sys/unistd.h> 56 #include <sys/callout.h> 57 #include <sys/malloc.h> 58 #include <sys/priv.h> 59 #include <sys/syslog.h> 60 #include <sys/selinfo.h> 61 #include <sys/conf.h> 62 #include <sys/fcntl.h> 63 64 #include <dev/usb/usb.h> 65 #include <dev/usb/usbdi.h> 66 #include <dev/usb/usbdi_util.h> 67 #include <dev/usb/usbhid.h> 68 #include "usbdevs.h" 69 70 #define USB_DEBUG_VAR ulpt_debug 71 #include <dev/usb/usb_debug.h> 72 #include <dev/usb/usb_process.h> 73 74 #ifdef USB_DEBUG 75 static int ulpt_debug = 0; 76 77 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt"); 78 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW, 79 &ulpt_debug, 0, "Debug level"); 80 #endif 81 82 #define ULPT_BSIZE (1<<15) /* bytes */ 83 #define ULPT_IFQ_MAXLEN 2 /* units */ 84 85 #define UR_GET_DEVICE_ID 0x00 86 #define UR_GET_PORT_STATUS 0x01 87 #define UR_SOFT_RESET 0x02 88 89 #define LPS_NERR 0x08 /* printer no error */ 90 #define LPS_SELECT 0x10 /* printer selected */ 91 #define LPS_NOPAPER 0x20 /* printer out of paper */ 92 #define LPS_INVERT (LPS_SELECT|LPS_NERR) 93 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER) 94 95 enum { 96 ULPT_BULK_DT_WR, 97 ULPT_BULK_DT_RD, 98 ULPT_INTR_DT_RD, 99 ULPT_N_TRANSFER, 100 }; 101 102 struct ulpt_softc { 103 struct usb_fifo_sc sc_fifo; 104 struct usb_fifo_sc sc_fifo_noreset; 105 struct mtx sc_mtx; 106 struct usb_callout sc_watchdog; 107 108 device_t sc_dev; 109 struct usb_device *sc_udev; 110 struct usb_fifo *sc_fifo_open[2]; 111 struct usb_xfer *sc_xfer[ULPT_N_TRANSFER]; 112 113 int sc_fflags; /* current open flags, FREAD and 114 * FWRITE */ 115 uint8_t sc_iface_no; 116 uint8_t sc_last_status; 117 uint8_t sc_zlps; /* number of consequtive zero length 118 * packets received */ 119 }; 120 121 /* prototypes */ 122 123 static device_probe_t ulpt_probe; 124 static device_attach_t ulpt_attach; 125 static device_detach_t ulpt_detach; 126 127 static usb_callback_t ulpt_write_callback; 128 static usb_callback_t ulpt_read_callback; 129 static usb_callback_t ulpt_status_callback; 130 131 static void ulpt_reset(struct ulpt_softc *); 132 static void ulpt_watchdog(void *); 133 134 static usb_fifo_close_t ulpt_close; 135 static usb_fifo_cmd_t ulpt_start_read; 136 static usb_fifo_cmd_t ulpt_start_write; 137 static usb_fifo_cmd_t ulpt_stop_read; 138 static usb_fifo_cmd_t ulpt_stop_write; 139 static usb_fifo_ioctl_t ulpt_ioctl; 140 static usb_fifo_open_t ulpt_open; 141 static usb_fifo_open_t unlpt_open; 142 143 static struct usb_fifo_methods ulpt_fifo_methods = { 144 .f_close = &ulpt_close, 145 .f_ioctl = &ulpt_ioctl, 146 .f_open = &ulpt_open, 147 .f_start_read = &ulpt_start_read, 148 .f_start_write = &ulpt_start_write, 149 .f_stop_read = &ulpt_stop_read, 150 .f_stop_write = &ulpt_stop_write, 151 .basename[0] = "ulpt", 152 }; 153 154 static struct usb_fifo_methods unlpt_fifo_methods = { 155 .f_close = &ulpt_close, 156 .f_ioctl = &ulpt_ioctl, 157 .f_open = &unlpt_open, 158 .f_start_read = &ulpt_start_read, 159 .f_start_write = &ulpt_start_write, 160 .f_stop_read = &ulpt_stop_read, 161 .f_stop_write = &ulpt_stop_write, 162 .basename[0] = "unlpt", 163 }; 164 165 static void 166 ulpt_reset(struct ulpt_softc *sc) 167 { 168 struct usb_device_request req; 169 170 DPRINTFN(2, "\n"); 171 172 req.bRequest = UR_SOFT_RESET; 173 USETW(req.wValue, 0); 174 USETW(req.wIndex, sc->sc_iface_no); 175 USETW(req.wLength, 0); 176 177 /* 178 * There was a mistake in the USB printer 1.0 spec that gave the 179 * request type as UT_WRITE_CLASS_OTHER; it should have been 180 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one, 181 * so we try both. 182 */ 183 184 mtx_lock(&sc->sc_mtx); 185 req.bmRequestType = UT_WRITE_CLASS_OTHER; 186 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 187 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.0 */ 188 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 189 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 190 &req, NULL, 0, NULL, 2 * USB_MS_HZ)) { /* 1.1 */ 191 /* ignore error */ 192 } 193 } 194 mtx_unlock(&sc->sc_mtx); 195 } 196 197 static void 198 ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error) 199 { 200 struct ulpt_softc *sc = usbd_xfer_softc(xfer); 201 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX]; 202 struct usb_page_cache *pc; 203 int actlen, max; 204 205 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 206 207 if (f == NULL) { 208 /* should not happen */ 209 DPRINTF("no FIFO\n"); 210 return; 211 } 212 DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen); 213 214 switch (USB_GET_STATE(xfer)) { 215 case USB_ST_TRANSFERRED: 216 case USB_ST_SETUP: 217 tr_setup: 218 pc = usbd_xfer_get_frame(xfer, 0); 219 max = usbd_xfer_max_len(xfer); 220 if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) { 221 usbd_xfer_set_frame_len(xfer, 0, actlen); 222 usbd_transfer_submit(xfer); 223 } 224 break; 225 226 default: /* Error */ 227 if (error != USB_ERR_CANCELLED) { 228 /* try to clear stall first */ 229 usbd_xfer_set_stall(xfer); 230 goto tr_setup; 231 } 232 break; 233 } 234 } 235 236 static void 237 ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error) 238 { 239 struct ulpt_softc *sc = usbd_xfer_softc(xfer); 240 struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX]; 241 struct usb_page_cache *pc; 242 int actlen; 243 244 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 245 246 if (f == NULL) { 247 /* should not happen */ 248 DPRINTF("no FIFO\n"); 249 return; 250 } 251 DPRINTF("state=0x%x\n", USB_GET_STATE(xfer)); 252 253 switch (USB_GET_STATE(xfer)) { 254 case USB_ST_TRANSFERRED: 255 256 if (actlen == 0) { 257 258 if (sc->sc_zlps == 4) { 259 /* enable BULK throttle */ 260 usbd_xfer_set_interval(xfer, 500); /* ms */ 261 } else { 262 sc->sc_zlps++; 263 } 264 } else { 265 /* disable BULK throttle */ 266 267 usbd_xfer_set_interval(xfer, 0); 268 sc->sc_zlps = 0; 269 } 270 271 pc = usbd_xfer_get_frame(xfer, 0); 272 usb_fifo_put_data(f, pc, 0, actlen, 1); 273 274 case USB_ST_SETUP: 275 tr_setup: 276 if (usb_fifo_put_bytes_max(f) != 0) { 277 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 278 usbd_transfer_submit(xfer); 279 } 280 break; 281 282 default: /* Error */ 283 /* disable BULK throttle */ 284 usbd_xfer_set_interval(xfer, 0); 285 sc->sc_zlps = 0; 286 287 if (error != USB_ERR_CANCELLED) { 288 /* try to clear stall first */ 289 usbd_xfer_set_stall(xfer); 290 goto tr_setup; 291 } 292 break; 293 } 294 } 295 296 static void 297 ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error) 298 { 299 struct ulpt_softc *sc = usbd_xfer_softc(xfer); 300 struct usb_device_request req; 301 struct usb_page_cache *pc; 302 uint8_t cur_status; 303 uint8_t new_status; 304 305 switch (USB_GET_STATE(xfer)) { 306 case USB_ST_TRANSFERRED: 307 308 pc = usbd_xfer_get_frame(xfer, 1); 309 usbd_copy_out(pc, 0, &cur_status, 1); 310 311 cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK; 312 new_status = cur_status & ~sc->sc_last_status; 313 sc->sc_last_status = cur_status; 314 315 if (new_status & LPS_SELECT) 316 log(LOG_NOTICE, "%s: offline\n", 317 device_get_nameunit(sc->sc_dev)); 318 else if (new_status & LPS_NOPAPER) 319 log(LOG_NOTICE, "%s: out of paper\n", 320 device_get_nameunit(sc->sc_dev)); 321 else if (new_status & LPS_NERR) 322 log(LOG_NOTICE, "%s: output error\n", 323 device_get_nameunit(sc->sc_dev)); 324 break; 325 326 case USB_ST_SETUP: 327 req.bmRequestType = UT_READ_CLASS_INTERFACE; 328 req.bRequest = UR_GET_PORT_STATUS; 329 USETW(req.wValue, 0); 330 req.wIndex[0] = sc->sc_iface_no; 331 req.wIndex[1] = 0; 332 USETW(req.wLength, 1); 333 334 pc = usbd_xfer_get_frame(xfer, 0); 335 usbd_copy_in(pc, 0, &req, sizeof(req)); 336 337 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 338 usbd_xfer_set_frame_len(xfer, 1, 1); 339 usbd_xfer_set_frames(xfer, 2); 340 usbd_transfer_submit(xfer); 341 break; 342 343 default: /* Error */ 344 DPRINTF("error=%s\n", usbd_errstr(error)); 345 if (error != USB_ERR_CANCELLED) { 346 /* wait for next watchdog timeout */ 347 } 348 break; 349 } 350 } 351 352 static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = { 353 [ULPT_BULK_DT_WR] = { 354 .type = UE_BULK, 355 .endpoint = UE_ADDR_ANY, 356 .direction = UE_DIR_OUT, 357 .bufsize = ULPT_BSIZE, 358 .flags = {.pipe_bof = 1,.proxy_buffer = 1}, 359 .callback = &ulpt_write_callback, 360 }, 361 362 [ULPT_BULK_DT_RD] = { 363 .type = UE_BULK, 364 .endpoint = UE_ADDR_ANY, 365 .direction = UE_DIR_IN, 366 .bufsize = ULPT_BSIZE, 367 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1}, 368 .callback = &ulpt_read_callback, 369 }, 370 371 [ULPT_INTR_DT_RD] = { 372 .type = UE_CONTROL, 373 .endpoint = 0x00, /* Control pipe */ 374 .direction = UE_DIR_ANY, 375 .bufsize = sizeof(struct usb_device_request) + 1, 376 .callback = &ulpt_status_callback, 377 .timeout = 1000, /* 1 second */ 378 }, 379 }; 380 381 static void 382 ulpt_start_read(struct usb_fifo *fifo) 383 { 384 struct ulpt_softc *sc = usb_fifo_softc(fifo); 385 386 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]); 387 } 388 389 static void 390 ulpt_stop_read(struct usb_fifo *fifo) 391 { 392 struct ulpt_softc *sc = usb_fifo_softc(fifo); 393 394 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]); 395 } 396 397 static void 398 ulpt_start_write(struct usb_fifo *fifo) 399 { 400 struct ulpt_softc *sc = usb_fifo_softc(fifo); 401 402 usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]); 403 } 404 405 static void 406 ulpt_stop_write(struct usb_fifo *fifo) 407 { 408 struct ulpt_softc *sc = usb_fifo_softc(fifo); 409 410 usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]); 411 } 412 413 static int 414 ulpt_open(struct usb_fifo *fifo, int fflags) 415 { 416 struct ulpt_softc *sc = usb_fifo_softc(fifo); 417 418 /* we assume that open is a serial process */ 419 420 if (sc->sc_fflags == 0) { 421 422 /* reset USB paralell port */ 423 424 ulpt_reset(sc); 425 } 426 return (unlpt_open(fifo, fflags)); 427 } 428 429 static int 430 unlpt_open(struct usb_fifo *fifo, int fflags) 431 { 432 struct ulpt_softc *sc = usb_fifo_softc(fifo); 433 434 if (sc->sc_fflags & fflags) { 435 return (EBUSY); 436 } 437 if (fflags & FREAD) { 438 /* clear stall first */ 439 mtx_lock(&sc->sc_mtx); 440 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]); 441 mtx_unlock(&sc->sc_mtx); 442 if (usb_fifo_alloc_buffer(fifo, 443 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]), 444 ULPT_IFQ_MAXLEN)) { 445 return (ENOMEM); 446 } 447 /* set which FIFO is opened */ 448 sc->sc_fifo_open[USB_FIFO_RX] = fifo; 449 } 450 if (fflags & FWRITE) { 451 /* clear stall first */ 452 mtx_lock(&sc->sc_mtx); 453 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]); 454 mtx_unlock(&sc->sc_mtx); 455 if (usb_fifo_alloc_buffer(fifo, 456 usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]), 457 ULPT_IFQ_MAXLEN)) { 458 return (ENOMEM); 459 } 460 /* set which FIFO is opened */ 461 sc->sc_fifo_open[USB_FIFO_TX] = fifo; 462 } 463 sc->sc_fflags |= fflags & (FREAD | FWRITE); 464 return (0); 465 } 466 467 static void 468 ulpt_close(struct usb_fifo *fifo, int fflags) 469 { 470 struct ulpt_softc *sc = usb_fifo_softc(fifo); 471 472 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE)); 473 474 if (fflags & (FREAD | FWRITE)) { 475 usb_fifo_free_buffer(fifo); 476 } 477 } 478 479 static int 480 ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data, 481 int fflags) 482 { 483 return (ENODEV); 484 } 485 486 static int 487 ulpt_probe(device_t dev) 488 { 489 struct usb_attach_arg *uaa = device_get_ivars(dev); 490 491 DPRINTFN(11, "\n"); 492 493 if (uaa->usb_mode != USB_MODE_HOST) { 494 return (ENXIO); 495 } 496 if ((uaa->info.bInterfaceClass == UICLASS_PRINTER) && 497 (uaa->info.bInterfaceSubClass == UISUBCLASS_PRINTER) && 498 ((uaa->info.bInterfaceProtocol == UIPROTO_PRINTER_UNI) || 499 (uaa->info.bInterfaceProtocol == UIPROTO_PRINTER_BI) || 500 (uaa->info.bInterfaceProtocol == UIPROTO_PRINTER_1284))) { 501 return (0); 502 } 503 return (ENXIO); 504 } 505 506 static int 507 ulpt_attach(device_t dev) 508 { 509 struct usb_attach_arg *uaa = device_get_ivars(dev); 510 struct ulpt_softc *sc = device_get_softc(dev); 511 struct usb_interface_descriptor *id; 512 int unit = device_get_unit(dev); 513 int error; 514 uint8_t iface_index = uaa->info.bIfaceIndex; 515 uint8_t alt_index; 516 517 DPRINTFN(11, "sc=%p\n", sc); 518 519 sc->sc_dev = dev; 520 sc->sc_udev = uaa->device; 521 522 device_set_usb_desc(dev); 523 524 mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE); 525 526 usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 527 528 /* search through all the descriptors looking for bidir mode */ 529 530 id = usbd_get_interface_descriptor(uaa->iface); 531 alt_index = 0 - 1; 532 while (1) { 533 if (id == NULL) { 534 break; 535 } 536 if ((id->bDescriptorType == UDESC_INTERFACE) && 537 (id->bLength >= sizeof(*id))) { 538 if (id->bInterfaceNumber != uaa->info.bIfaceNum) { 539 break; 540 } else { 541 alt_index++; 542 if ((id->bInterfaceClass == UICLASS_PRINTER) && 543 (id->bInterfaceSubClass == UISUBCLASS_PRINTER) && 544 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) { 545 goto found; 546 } 547 } 548 } 549 id = (void *)usb_desc_foreach( 550 usbd_get_config_descriptor(uaa->device), (void *)id); 551 } 552 goto detach; 553 554 found: 555 556 DPRINTF("setting alternate " 557 "config number: %d\n", alt_index); 558 559 if (alt_index) { 560 561 error = usbd_set_alt_interface_index 562 (uaa->device, iface_index, alt_index); 563 564 if (error) { 565 DPRINTF("could not set alternate " 566 "config, error=%s\n", usbd_errstr(error)); 567 goto detach; 568 } 569 } 570 sc->sc_iface_no = id->bInterfaceNumber; 571 572 error = usbd_transfer_setup(uaa->device, &iface_index, 573 sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER, 574 sc, &sc->sc_mtx); 575 if (error) { 576 DPRINTF("error=%s\n", usbd_errstr(error)); 577 goto detach; 578 } 579 device_printf(sc->sc_dev, "using bi-directional mode\n"); 580 581 #if 0 582 /* 583 * This code is disabled because for some mysterious reason it causes 584 * printing not to work. But only sometimes, and mostly with 585 * UHCI and less often with OHCI. *sigh* 586 */ 587 { 588 struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev); 589 struct usb_device_request req; 590 int len, alen; 591 592 req.bmRequestType = UT_READ_CLASS_INTERFACE; 593 req.bRequest = UR_GET_DEVICE_ID; 594 USETW(req.wValue, cd->bConfigurationValue); 595 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting); 596 USETW(req.wLength, sizeof devinfo - 1); 597 error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK, 598 &alen, USB_DEFAULT_TIMEOUT); 599 if (error) { 600 device_printf(sc->sc_dev, "cannot get device id\n"); 601 } else if (alen <= 2) { 602 device_printf(sc->sc_dev, "empty device id, no " 603 "printer connected?\n"); 604 } else { 605 /* devinfo now contains an IEEE-1284 device ID */ 606 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff); 607 if (len > sizeof devinfo - 3) 608 len = sizeof devinfo - 3; 609 devinfo[len] = 0; 610 printf("%s: device id <", device_get_nameunit(sc->sc_dev)); 611 ieee1284_print_id(devinfo + 2); 612 printf(">\n"); 613 } 614 } 615 #endif 616 617 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 618 &ulpt_fifo_methods, &sc->sc_fifo, 619 unit, 0 - 1, uaa->info.bIfaceIndex, 620 UID_ROOT, GID_OPERATOR, 0644); 621 if (error) { 622 goto detach; 623 } 624 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 625 &unlpt_fifo_methods, &sc->sc_fifo_noreset, 626 unit, 0 - 1, uaa->info.bIfaceIndex, 627 UID_ROOT, GID_OPERATOR, 0644); 628 if (error) { 629 goto detach; 630 } 631 /* start reading of status */ 632 633 mtx_lock(&sc->sc_mtx); 634 ulpt_watchdog(sc); 635 mtx_unlock(&sc->sc_mtx); 636 return (0); 637 638 detach: 639 ulpt_detach(dev); 640 return (ENOMEM); 641 } 642 643 static int 644 ulpt_detach(device_t dev) 645 { 646 struct ulpt_softc *sc = device_get_softc(dev); 647 648 DPRINTF("sc=%p\n", sc); 649 650 usb_fifo_detach(&sc->sc_fifo); 651 usb_fifo_detach(&sc->sc_fifo_noreset); 652 653 mtx_lock(&sc->sc_mtx); 654 usb_callout_stop(&sc->sc_watchdog); 655 mtx_unlock(&sc->sc_mtx); 656 657 usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER); 658 usb_callout_drain(&sc->sc_watchdog); 659 mtx_destroy(&sc->sc_mtx); 660 661 return (0); 662 } 663 664 #if 0 665 /* XXX This does not belong here. */ 666 667 /* 668 * Compare two strings until the second ends. 669 */ 670 671 static uint8_t 672 ieee1284_compare(const char *a, const char *b) 673 { 674 while (1) { 675 676 if (*b == 0) { 677 break; 678 } 679 if (*a != *b) { 680 return 1; 681 } 682 b++; 683 a++; 684 } 685 return 0; 686 } 687 688 /* 689 * Print select parts of an IEEE 1284 device ID. 690 */ 691 void 692 ieee1284_print_id(char *str) 693 { 694 char *p, *q; 695 696 for (p = str - 1; p; p = strchr(p, ';')) { 697 p++; /* skip ';' */ 698 if (ieee1284_compare(p, "MFG:") == 0 || 699 ieee1284_compare(p, "MANUFACTURER:") == 0 || 700 ieee1284_compare(p, "MDL:") == 0 || 701 ieee1284_compare(p, "MODEL:") == 0) { 702 q = strchr(p, ';'); 703 if (q) 704 printf("%.*s", (int)(q - p + 1), p); 705 } 706 } 707 } 708 709 #endif 710 711 static void 712 ulpt_watchdog(void *arg) 713 { 714 struct ulpt_softc *sc = arg; 715 716 mtx_assert(&sc->sc_mtx, MA_OWNED); 717 718 /* 719 * Only read status while the device is not opened, due to 720 * possible hardware or firmware bug in some printers. 721 */ 722 if (sc->sc_fflags == 0) 723 usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]); 724 725 usb_callout_reset(&sc->sc_watchdog, 726 hz, &ulpt_watchdog, sc); 727 } 728 729 static devclass_t ulpt_devclass; 730 731 static device_method_t ulpt_methods[] = { 732 DEVMETHOD(device_probe, ulpt_probe), 733 DEVMETHOD(device_attach, ulpt_attach), 734 DEVMETHOD(device_detach, ulpt_detach), 735 {0, 0} 736 }; 737 738 static driver_t ulpt_driver = { 739 .name = "ulpt", 740 .methods = ulpt_methods, 741 .size = sizeof(struct ulpt_softc), 742 }; 743 744 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, NULL, 0); 745 MODULE_DEPEND(ulpt, usb, 1, 1, 1); 746 MODULE_VERSION(ulpt, 1); 747