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