1 /* $OpenBSD: ulpt.c,v 1.47 2014/07/12 20:26:33 mpi Exp $ */ 2 /* $NetBSD: ulpt.c,v 1.57 2003/01/05 10:19:42 scw Exp $ */ 3 /* $FreeBSD: src/sys/dev/usb/ulpt.c,v 1.24 1999/11/17 22:33:44 n_hibma Exp $ */ 4 5 /* 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Lennart Augustsson (lennart@augustsson.net) at 11 * Carlstedt Research & Technology. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Printer Class spec: 37 * http://www.usb.org/developers/devclass_docs/usbprint11.pdf 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/device.h> 44 #include <sys/uio.h> 45 #include <sys/conf.h> 46 #include <sys/vnode.h> 47 #include <sys/syslog.h> 48 #include <sys/types.h> 49 #include <sys/malloc.h> 50 51 #include <dev/usb/usb.h> 52 #include <dev/usb/usbdi.h> 53 #include <dev/usb/usbdi_util.h> 54 #include <dev/usb/usbdevs.h> 55 #include <dev/usb/usb_quirks.h> 56 57 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */ 58 #define STEP hz/4 59 60 #define LPTPRI (PZERO+8) 61 #define ULPT_BSIZE 16384 62 63 #ifdef ULPT_DEBUG 64 #define DPRINTF(x) do { if (ulptdebug) printf x; } while (0) 65 #define DPRINTFN(n,x) do { if (ulptdebug>(n)) printf x; } while (0) 66 int ulptdebug = 0; 67 #else 68 #define DPRINTF(x) 69 #define DPRINTFN(n,x) 70 #endif 71 72 #define UR_GET_DEVICE_ID 0 73 #define UR_GET_PORT_STATUS 1 74 #define UR_SOFT_RESET 2 75 76 #define LPS_NERR 0x08 /* printer no error */ 77 #define LPS_SELECT 0x10 /* printer selected */ 78 #define LPS_NOPAPER 0x20 /* printer out of paper */ 79 #define LPS_INVERT (LPS_SELECT|LPS_NERR) 80 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER) 81 82 struct ulpt_softc { 83 struct device sc_dev; 84 struct usbd_device *sc_udev; /* device */ 85 struct usbd_interface *sc_iface;/* interface */ 86 int sc_ifaceno; 87 88 int sc_out; 89 struct usbd_pipe *sc_out_pipe; /* bulk out pipe */ 90 91 int sc_in; 92 struct usbd_pipe *sc_in_pipe; /* bulk in pipe */ 93 struct usbd_xfer *sc_in_xfer1; 94 struct usbd_xfer *sc_in_xfer2; 95 u_char sc_junk[64]; /* somewhere to dump input */ 96 97 u_char sc_state; 98 #define ULPT_OPEN 0x01 /* device is open */ 99 #define ULPT_OBUSY 0x02 /* printer is busy doing output */ 100 #define ULPT_INIT 0x04 /* waiting to initialize for open */ 101 u_char sc_flags; 102 #define ULPT_NOPRIME 0x40 /* don't prime on open */ 103 u_char sc_laststatus; 104 105 int sc_refcnt; 106 107 struct ulpt_fwdev *sc_fwdev; 108 }; 109 110 void ulpt_disco(void *); 111 112 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int); 113 int ulpt_status(struct ulpt_softc *); 114 void ulpt_reset(struct ulpt_softc *); 115 int ulpt_statusmsg(u_char, struct ulpt_softc *); 116 117 /* 118 * Printers which need firmware uploads. 119 */ 120 void ulpt_load_firmware(void *); 121 usbd_status ulpt_ucode_loader_hp(struct ulpt_softc *); 122 struct ulpt_fwdev { 123 struct usb_devno uv_dev; 124 char *ucode_name; 125 usbd_status (*ucode_loader)(struct ulpt_softc *); 126 } ulpt_fwdevs[] = { 127 { 128 { USB_VENDOR_HP, USB_PRODUCT_HP_1000 }, 129 "ulpt-hp1000", 130 ulpt_ucode_loader_hp 131 }, 132 { 133 { USB_VENDOR_HP, USB_PRODUCT_HP_1005 }, 134 "ulpt-hp1005", 135 ulpt_ucode_loader_hp 136 }, 137 { 138 { USB_VENDOR_HP, USB_PRODUCT_HP_1018 }, 139 "ulpt-hp1018", 140 ulpt_ucode_loader_hp 141 }, 142 { 143 { USB_VENDOR_HP, USB_PRODUCT_HP_1020 }, 144 "ulpt-hp1020", 145 ulpt_ucode_loader_hp 146 }, 147 }; 148 149 #if 0 150 void ieee1284_print_id(char *); 151 #endif 152 153 #define ULPTUNIT(s) (minor(s) & 0x1f) 154 #define ULPTFLAGS(s) (minor(s) & 0xe0) 155 156 157 int ulpt_match(struct device *, void *, void *); 158 void ulpt_attach(struct device *, struct device *, void *); 159 int ulpt_detach(struct device *, int); 160 161 struct cfdriver ulpt_cd = { 162 NULL, "ulpt", DV_DULL 163 }; 164 165 const struct cfattach ulpt_ca = { 166 sizeof(struct ulpt_softc), ulpt_match, ulpt_attach, ulpt_detach 167 }; 168 169 int 170 ulpt_match(struct device *parent, void *match, void *aux) 171 { 172 struct usb_attach_arg *uaa = aux; 173 usb_interface_descriptor_t *id; 174 175 DPRINTFN(10,("ulpt_match\n")); 176 if (uaa->iface == NULL) 177 return (UMATCH_NONE); 178 id = usbd_get_interface_descriptor(uaa->iface); 179 if (id != NULL && 180 id->bInterfaceClass == UICLASS_PRINTER && 181 id->bInterfaceSubClass == UISUBCLASS_PRINTER && 182 ((id->bInterfaceProtocol == UIPROTO_PRINTER_UNI) || 183 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI) || 184 (id->bInterfaceProtocol == UIPROTO_PRINTER_1284))) 185 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO); 186 return (UMATCH_NONE); 187 } 188 189 void 190 ulpt_load_firmware(void *arg) 191 { 192 struct ulpt_softc *sc = (struct ulpt_softc *)arg; 193 usbd_status err; 194 195 err = (sc->sc_fwdev->ucode_loader)(sc); 196 if (err != USBD_NORMAL_COMPLETION) 197 printf("%s: could not load firmware '%s'\n", 198 sc->sc_dev.dv_xname, sc->sc_fwdev->ucode_name); 199 } 200 201 #define ulpt_lookup(v, p) \ 202 ((struct ulpt_fwdev *)usb_lookup(ulpt_fwdevs, v, p)) 203 204 void 205 ulpt_attach(struct device *parent, struct device *self, void *aux) 206 { 207 struct ulpt_softc *sc = (struct ulpt_softc *)self; 208 struct usb_attach_arg *uaa = aux; 209 struct usbd_device *dev = uaa->device; 210 struct usbd_interface *iface = uaa->iface; 211 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface); 212 usb_interface_descriptor_t *id, *iend; 213 usb_config_descriptor_t *cdesc; 214 usbd_status err; 215 usb_endpoint_descriptor_t *ed; 216 u_int8_t epcount; 217 int i, altno; 218 219 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc)); 220 221 //printf("%s: iclass %d/%d\n", sc->sc_dev.dv_xname, 222 // ifcd->bInterfaceClass, ifcd->bInterfaceSubClass); 223 224 /* XXX 225 * Stepping through the alternate settings needs to be abstracted out. 226 */ 227 cdesc = usbd_get_config_descriptor(dev); 228 if (cdesc == NULL) { 229 printf("%s: failed to get configuration descriptor\n", 230 sc->sc_dev.dv_xname); 231 return; 232 } 233 iend = (usb_interface_descriptor_t *) 234 ((char *)cdesc + UGETW(cdesc->wTotalLength)); 235 #ifdef DIAGNOSTIC 236 if (ifcd < (usb_interface_descriptor_t *)cdesc || 237 ifcd >= iend) 238 panic("ulpt: iface desc out of range"); 239 #endif 240 /* Step through all the descriptors looking for bidir mode */ 241 for (id = ifcd, altno = 0; 242 id < iend; 243 id = (void *)((char *)id + id->bLength)) { 244 if (id->bDescriptorType == UDESC_INTERFACE && 245 id->bInterfaceNumber == ifcd->bInterfaceNumber) { 246 if (id->bInterfaceClass == UICLASS_PRINTER && 247 id->bInterfaceSubClass == UISUBCLASS_PRINTER && 248 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*|| 249 id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/)) 250 goto found; 251 altno++; 252 } 253 } 254 id = ifcd; /* not found, use original */ 255 found: 256 if (id != ifcd) { 257 /* Found a new bidir setting */ 258 DPRINTF(("ulpt_attach: set altno = %d\n", altno)); 259 err = usbd_set_interface(iface, altno); 260 if (err) { 261 printf("%s: setting alternate interface failed\n", 262 sc->sc_dev.dv_xname); 263 usbd_deactivate(sc->sc_udev); 264 return; 265 } 266 } 267 268 epcount = 0; 269 (void)usbd_endpoint_count(iface, &epcount); 270 271 sc->sc_in = -1; 272 sc->sc_out = -1; 273 for (i = 0; i < epcount; i++) { 274 ed = usbd_interface2endpoint_descriptor(iface, i); 275 if (ed == NULL) { 276 printf("%s: couldn't get ep %d\n", 277 sc->sc_dev.dv_xname, i); 278 return; 279 } 280 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 281 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 282 sc->sc_in = ed->bEndpointAddress; 283 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 284 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 285 sc->sc_out = ed->bEndpointAddress; 286 } 287 } 288 if (sc->sc_out == -1) { 289 printf("%s: could not find bulk out endpoint\n", 290 sc->sc_dev.dv_xname); 291 usbd_deactivate(sc->sc_udev); 292 return; 293 } 294 295 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) { 296 /* This device doesn't handle reading properly. */ 297 sc->sc_in = -1; 298 } 299 300 printf("%s: using %s-directional mode\n", sc->sc_dev.dv_xname, 301 sc->sc_in >= 0 ? "bi" : "uni"); 302 303 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out)); 304 305 sc->sc_iface = iface; 306 sc->sc_ifaceno = id->bInterfaceNumber; 307 sc->sc_udev = dev; 308 309 /* maybe the device needs firmware */ 310 sc->sc_fwdev = ulpt_lookup(uaa->vendor, uaa->product); 311 if (sc->sc_fwdev) { 312 if (rootvp == NULL) 313 mountroothook_establish(ulpt_load_firmware, sc); 314 else 315 ulpt_load_firmware(sc); 316 } 317 318 #if 0 319 /* 320 * This code is disabled because for some mysterious reason it causes 321 * printing not to work. But only sometimes, and mostly with 322 * UHCI and less often with OHCI. *sigh* 323 */ 324 { 325 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev); 326 usb_device_request_t req; 327 int len, alen; 328 329 req.bmRequestType = UT_READ_CLASS_INTERFACE; 330 req.bRequest = UR_GET_DEVICE_ID; 331 USETW(req.wValue, cd->bConfigurationValue); 332 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting); 333 USETW(req.wLength, DEVINFOSIZE - 1); 334 err = usbd_do_request_flags(dev, &req, devinfop, USBD_SHORT_XFER_OK, 335 &alen, USBD_DEFAULT_TIMEOUT); 336 if (err) { 337 printf("%s: cannot get device id\n", sc->sc_dev.dv_xname); 338 } else if (alen <= 2) { 339 printf("%s: empty device id, no printer connected?\n", 340 sc->sc_dev.dv_xname); 341 } else { 342 /* devinfop now contains an IEEE-1284 device ID */ 343 len = ((devinfop[0] & 0xff) << 8) | (devinfop[1] & 0xff); 344 if (len > DEVINFOSIZE - 3) 345 len = DEVINFOSIZE - 3; 346 devinfo[len] = 0; 347 printf("%s: device id <", sc->sc_dev.dv_xname); 348 ieee1284_print_id(devinfop+2); 349 printf(">\n"); 350 } 351 } 352 #endif 353 } 354 355 int 356 ulpt_detach(struct device *self, int flags) 357 { 358 struct ulpt_softc *sc = (struct ulpt_softc *)self; 359 int s; 360 int maj, mn; 361 362 DPRINTF(("ulpt_detach: sc=%p\n", sc)); 363 364 if (sc->sc_out_pipe != NULL) 365 usbd_abort_pipe(sc->sc_out_pipe); 366 if (sc->sc_in_pipe != NULL) 367 usbd_abort_pipe(sc->sc_in_pipe); 368 369 s = splusb(); 370 if (--sc->sc_refcnt >= 0) { 371 /* There is noone to wake, aborting the pipe is enough */ 372 /* Wait for processes to go away. */ 373 usb_detach_wait(&sc->sc_dev); 374 } 375 splx(s); 376 377 /* locate the major number */ 378 for (maj = 0; maj < nchrdev; maj++) 379 if (cdevsw[maj].d_open == ulptopen) 380 break; 381 382 /* Nuke the vnodes for any open instances (calls close). */ 383 mn = self->dv_unit; 384 vdevgone(maj, mn, mn, VCHR); 385 vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR); 386 387 return (0); 388 } 389 390 int 391 ulpt_status(struct ulpt_softc *sc) 392 { 393 usb_device_request_t req; 394 usbd_status err; 395 u_char status; 396 397 req.bmRequestType = UT_READ_CLASS_INTERFACE; 398 req.bRequest = UR_GET_PORT_STATUS; 399 USETW(req.wValue, 0); 400 USETW(req.wIndex, sc->sc_ifaceno); 401 USETW(req.wLength, 1); 402 err = usbd_do_request(sc->sc_udev, &req, &status); 403 DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err)); 404 if (!err) 405 return (status); 406 else 407 return (0); 408 } 409 410 void 411 ulpt_reset(struct ulpt_softc *sc) 412 { 413 usb_device_request_t req; 414 415 DPRINTFN(1, ("ulpt_reset\n")); 416 req.bRequest = UR_SOFT_RESET; 417 USETW(req.wValue, 0); 418 USETW(req.wIndex, sc->sc_ifaceno); 419 USETW(req.wLength, 0); 420 421 /* 422 * There was a mistake in the USB printer 1.0 spec that gave the 423 * request type as UT_WRITE_CLASS_OTHER; it should have been 424 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one, 425 * so we try both. 426 */ 427 req.bmRequestType = UT_WRITE_CLASS_OTHER; 428 if (usbd_do_request(sc->sc_udev, &req, 0)) { /* 1.0 */ 429 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 430 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */ 431 } 432 } 433 434 static void 435 ulpt_input(struct usbd_xfer *xfer, void *priv, usbd_status status) 436 { 437 struct ulpt_softc *sc = priv; 438 439 DPRINTFN(2,("ulpt_input: got some data\n")); 440 /* Do it again. */ 441 if (xfer == sc->sc_in_xfer1) 442 usbd_transfer(sc->sc_in_xfer2); 443 else 444 usbd_transfer(sc->sc_in_xfer1); 445 } 446 447 int ulptusein = 1; 448 449 /* 450 * Reset the printer, then wait until it's selected and not busy. 451 */ 452 int 453 ulptopen(dev_t dev, int flag, int mode, struct proc *p) 454 { 455 u_char flags = ULPTFLAGS(dev); 456 struct ulpt_softc *sc; 457 usbd_status err; 458 int error; 459 460 if (ULPTUNIT(dev) >= ulpt_cd.cd_ndevs) 461 return (ENXIO); 462 sc = ulpt_cd.cd_devs[ULPTUNIT(dev)]; 463 if (sc == NULL) 464 return (ENXIO); 465 466 if (sc == NULL || sc->sc_iface == NULL || usbd_is_dying(sc->sc_udev)) 467 return (ENXIO); 468 469 if (sc->sc_state) 470 return (EBUSY); 471 472 sc->sc_state = ULPT_INIT; 473 sc->sc_flags = flags; 474 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags)); 475 476 error = 0; 477 sc->sc_refcnt++; 478 479 if ((flags & ULPT_NOPRIME) == 0) { 480 ulpt_reset(sc); 481 if (usbd_is_dying(sc->sc_udev)) { 482 error = ENXIO; 483 sc->sc_state = 0; 484 goto done; 485 } 486 } 487 488 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe); 489 if (err) { 490 sc->sc_state = 0; 491 error = EIO; 492 goto done; 493 } 494 if (ulptusein && sc->sc_in != -1) { 495 DPRINTF(("ulpt_open: open input pipe\n")); 496 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe); 497 if (err) { 498 error = EIO; 499 usbd_close_pipe(sc->sc_out_pipe); 500 sc->sc_out_pipe = NULL; 501 sc->sc_state = 0; 502 goto done; 503 } 504 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev); 505 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev); 506 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) { 507 error = ENOMEM; 508 if (sc->sc_in_xfer1 != NULL) { 509 usbd_free_xfer(sc->sc_in_xfer1); 510 sc->sc_in_xfer1 = NULL; 511 } 512 if (sc->sc_in_xfer2 != NULL) { 513 usbd_free_xfer(sc->sc_in_xfer2); 514 sc->sc_in_xfer2 = NULL; 515 } 516 usbd_close_pipe(sc->sc_out_pipe); 517 sc->sc_out_pipe = NULL; 518 usbd_close_pipe(sc->sc_in_pipe); 519 sc->sc_in_pipe = NULL; 520 sc->sc_state = 0; 521 goto done; 522 } 523 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc, 524 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK, 525 USBD_NO_TIMEOUT, ulpt_input); 526 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc, 527 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK, 528 USBD_NO_TIMEOUT, ulpt_input); 529 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */ 530 } 531 532 sc->sc_state = ULPT_OPEN; 533 534 done: 535 if (--sc->sc_refcnt < 0) 536 usb_detach_wakeup(&sc->sc_dev); 537 538 DPRINTF(("ulptopen: done, error=%d\n", error)); 539 return (error); 540 } 541 542 int 543 ulpt_statusmsg(u_char status, struct ulpt_softc *sc) 544 { 545 u_char new; 546 547 status = (status ^ LPS_INVERT) & LPS_MASK; 548 new = status & ~sc->sc_laststatus; 549 sc->sc_laststatus = status; 550 551 if (new & LPS_SELECT) 552 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname); 553 else if (new & LPS_NOPAPER) 554 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname); 555 else if (new & LPS_NERR) 556 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname); 557 558 return (status); 559 } 560 561 int 562 ulptclose(dev_t dev, int flag, int mode, struct proc *p) 563 { 564 struct ulpt_softc *sc; 565 566 sc = ulpt_cd.cd_devs[ULPTUNIT(dev)]; 567 568 if (sc->sc_state != ULPT_OPEN) 569 /* We are being forced to close before the open completed. */ 570 return (0); 571 572 if (sc->sc_out_pipe != NULL) { 573 usbd_close_pipe(sc->sc_out_pipe); 574 sc->sc_out_pipe = NULL; 575 } 576 if (sc->sc_in_pipe != NULL) { 577 usbd_abort_pipe(sc->sc_in_pipe); 578 usbd_close_pipe(sc->sc_in_pipe); 579 sc->sc_in_pipe = NULL; 580 if (sc->sc_in_xfer1 != NULL) { 581 usbd_free_xfer(sc->sc_in_xfer1); 582 sc->sc_in_xfer1 = NULL; 583 } 584 if (sc->sc_in_xfer2 != NULL) { 585 usbd_free_xfer(sc->sc_in_xfer2); 586 sc->sc_in_xfer2 = NULL; 587 } 588 } 589 590 sc->sc_state = 0; 591 592 DPRINTF(("ulptclose: closed\n")); 593 return (0); 594 } 595 596 int 597 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags) 598 { 599 u_int32_t n; 600 int error = 0; 601 void *bufp; 602 struct usbd_xfer *xfer; 603 usbd_status err; 604 605 DPRINTF(("ulptwrite\n")); 606 xfer = usbd_alloc_xfer(sc->sc_udev); 607 if (xfer == NULL) 608 return (ENOMEM); 609 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE); 610 if (bufp == NULL) { 611 usbd_free_xfer(xfer); 612 return (ENOMEM); 613 } 614 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) { 615 ulpt_statusmsg(ulpt_status(sc), sc); 616 error = uiomove(bufp, n, uio); 617 if (error) 618 break; 619 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n)); 620 usbd_setup_xfer(xfer, sc->sc_out_pipe, 0, bufp, n, 621 USBD_NO_COPY | USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL); 622 err = usbd_transfer(xfer); 623 if (err) { 624 usbd_clear_endpoint_stall(sc->sc_out_pipe); 625 DPRINTF(("ulptwrite: error=%d\n", err)); 626 error = EIO; 627 break; 628 } 629 } 630 usbd_free_xfer(xfer); 631 632 return (error); 633 } 634 635 int 636 ulptwrite(dev_t dev, struct uio *uio, int flags) 637 { 638 struct ulpt_softc *sc; 639 int error; 640 641 sc = ulpt_cd.cd_devs[ULPTUNIT(dev)]; 642 643 if (usbd_is_dying(sc->sc_udev)) 644 return (EIO); 645 646 sc->sc_refcnt++; 647 error = ulpt_do_write(sc, uio, flags); 648 if (--sc->sc_refcnt < 0) 649 usb_detach_wakeup(&sc->sc_dev); 650 return (error); 651 } 652 653 usbd_status 654 ulpt_ucode_loader_hp(struct ulpt_softc *sc) 655 { 656 usbd_status error; 657 int load_error; 658 uint8_t *ucode; 659 uint32_t len; 660 size_t ucode_size; 661 const char *ucode_name = sc->sc_fwdev->ucode_name; 662 int offset = 0, remain; 663 struct usbd_xfer *xfer; 664 void *bufp; 665 666 /* open microcode file */ 667 load_error = loadfirmware(ucode_name, &ucode, &ucode_size); 668 if (load_error != 0) { 669 printf("%s: failed loadfirmware of file %s (error %d)\n", 670 sc->sc_dev.dv_xname, ucode_name, load_error); 671 return (USBD_INVAL); 672 } 673 674 /* upload microcode */ 675 error = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe); 676 if (error) 677 goto free_ucode; 678 xfer = usbd_alloc_xfer(sc->sc_udev); 679 if (xfer == NULL) 680 goto close_pipe; 681 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE); 682 if (bufp == NULL) { 683 error = USBD_NOMEM; 684 goto free_xfer; 685 } 686 remain = ucode_size; 687 while (remain > 0) { 688 len = min(remain, ULPT_BSIZE); 689 memcpy(bufp, &ucode[offset], len); 690 usbd_setup_xfer(xfer, sc->sc_out_pipe, 0, bufp, len, 691 USBD_NO_COPY | USBD_SYNCHRONOUS, 0, NULL); 692 error = usbd_transfer(xfer); 693 if (error != USBD_NORMAL_COMPLETION) { 694 usbd_clear_endpoint_stall(sc->sc_out_pipe); 695 printf("%s: ucode upload error=%s!\n", 696 sc->sc_dev.dv_xname, usbd_errstr(error)); 697 break; 698 } 699 DPRINTF(("%s: uploaded %d bytes ucode\n", 700 sc->sc_dev.dv_xname, len)); 701 702 offset += len; 703 remain -= len; 704 } 705 free_xfer: 706 usbd_free_xfer(xfer); 707 close_pipe: 708 usbd_close_pipe(sc->sc_out_pipe); 709 sc->sc_out_pipe = NULL; 710 free_ucode: 711 free(ucode, M_DEVBUF, 0); 712 713 return (error); 714 } 715 716 #if 0 717 /* XXX This does not belong here. */ 718 /* 719 * Print select parts of a IEEE 1284 device ID. 720 */ 721 void 722 ieee1284_print_id(char *str) 723 { 724 char *p, *q; 725 726 for (p = str-1; p; p = strchr(p, ';')) { 727 p++; /* skip ';' */ 728 if (strncmp(p, "MFG:", 4) == 0 || 729 strncmp(p, "MANUFACTURER:", 14) == 0 || 730 strncmp(p, "MDL:", 4) == 0 || 731 strncmp(p, "MODEL:", 6) == 0) { 732 q = strchr(p, ';'); 733 if (q) 734 printf("%.*s", (int)(q - p + 1), p); 735 } 736 } 737 } 738 #endif 739