Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.26
      1 /*	$NetBSD: ulpt.c,v 1.26 1999/10/12 11:54:56 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Printer Class spec: http://www.usb.org/developers/data/usbprn10.pdf
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/proc.h>
     47 #include <sys/kernel.h>
     48 #if defined(__NetBSD__)
     49 #include <sys/device.h>
     50 #include <sys/ioctl.h>
     51 #elif defined(__FreeBSD__)
     52 #include <sys/ioccom.h>
     53 #include <sys/module.h>
     54 #include <sys/bus.h>
     55 #endif
     56 #include <sys/uio.h>
     57 #include <sys/conf.h>
     58 #include <sys/vnode.h>
     59 #include <sys/syslog.h>
     60 
     61 #include <dev/usb/usb.h>
     62 #include <dev/usb/usbdi.h>
     63 #include <dev/usb/usbdi_util.h>
     64 #include <dev/usb/usbdevs.h>
     65 #include <dev/usb/usb_quirks.h>
     66 
     67 #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     68 #define	STEP		hz/4
     69 
     70 #define	LPTPRI		(PZERO+8)
     71 #define	ULPT_BSIZE	16384
     72 
     73 #ifdef USB_DEBUG
     74 #define DPRINTF(x)	if (ulptdebug) logprintf x
     75 #define DPRINTFN(n,x)	if (ulptdebug>(n)) logprintf x
     76 int	ulptdebug = 0;
     77 #else
     78 #define DPRINTF(x)
     79 #define DPRINTFN(n,x)
     80 #endif
     81 
     82 #define UR_GET_DEVICE_ID 0
     83 #define UR_GET_PORT_STATUS 1
     84 #define UR_SOFT_RESET 2
     85 
     86 #define	LPS_NERR		0x08	/* printer no error */
     87 #define	LPS_SELECT		0x10	/* printer selected */
     88 #define	LPS_NOPAPER		0x20	/* printer out of paper */
     89 #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
     90 #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
     91 
     92 struct ulpt_softc {
     93 	USBBASEDEVICE sc_dev;
     94 	usbd_device_handle sc_udev;	/* device */
     95 	usbd_interface_handle sc_iface;	/* interface */
     96 	int sc_ifaceno;
     97 	usbd_pipe_handle sc_bulkpipe;	/* bulk pipe */
     98 	int sc_bulk;
     99 
    100 	u_char sc_state;
    101 #define	ULPT_OPEN	0x01	/* device is open */
    102 #define	ULPT_OBUSY	0x02	/* printer is busy doing output */
    103 #define	ULPT_INIT	0x04	/* waiting to initialize for open */
    104 	u_char sc_flags;
    105 #define	ULPT_NOPRIME	0x40	/* don't prime on open */
    106 	u_char sc_laststatus;
    107 
    108 	int sc_refcnt;
    109 	u_char sc_dying;
    110 };
    111 
    112 int ulptopen __P((dev_t, int, int, struct proc *));
    113 int ulptclose __P((dev_t, int, int, struct proc *p));
    114 int ulptwrite __P((dev_t, struct uio *uio, int));
    115 int ulptioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    116 void ulpt_disco __P((void *));
    117 
    118 int ulpt_do_write __P((struct ulpt_softc *, struct uio *uio, int));
    119 int ulpt_status __P((struct ulpt_softc *));
    120 void ulpt_reset __P((struct ulpt_softc *));
    121 int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
    122 
    123 void ieee1284_print_id __P((char *));
    124 
    125 #define	ULPTUNIT(s)	(minor(s) & 0x1f)
    126 #define	ULPTFLAGS(s)	(minor(s) & 0xe0)
    127 
    128 USB_DECLARE_DRIVER(ulpt);
    129 
    130 USB_MATCH(ulpt)
    131 {
    132 	USB_MATCH_START(ulpt, uaa);
    133 	usb_interface_descriptor_t *id;
    134 
    135 	DPRINTFN(10,("ulpt_match\n"));
    136 	if (!uaa->iface)
    137 		return (UMATCH_NONE);
    138 	id = usbd_get_interface_descriptor(uaa->iface);
    139 	if (id &&
    140 	    id->bInterfaceClass == UCLASS_PRINTER &&
    141 	    id->bInterfaceSubClass == USUBCLASS_PRINTER &&
    142 	    (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
    143 	     id->bInterfaceProtocol == UPROTO_PRINTER_BI))
    144 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    145 	return (UMATCH_NONE);
    146 }
    147 
    148 USB_ATTACH(ulpt)
    149 {
    150 	USB_ATTACH_START(ulpt, sc, uaa);
    151 	usbd_device_handle dev = uaa->device;
    152 	usbd_interface_handle iface = uaa->iface;
    153 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    154 	char devinfo[1024];
    155 	usb_endpoint_descriptor_t *ed;
    156 	usbd_status r;
    157 
    158 	DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
    159 	usbd_devinfo(dev, 0, devinfo);
    160 	USB_ATTACH_SETUP;
    161 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    162 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    163 
    164 	/* Figure out which endpoint is the bulk out endpoint. */
    165 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    166 	if (!ed)
    167 		goto nobulk;
    168 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
    169 	    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
    170 		/* In case we are using a bidir protocol... */
    171 		ed = usbd_interface2endpoint_descriptor(iface, 1);
    172 		if (!ed)
    173 			goto nobulk;
    174 		if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
    175 		    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
    176 			goto nobulk;
    177 	}
    178 	sc->sc_bulk = ed->bEndpointAddress;
    179 	DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
    180 
    181 	sc->sc_iface = iface;
    182 	r = usbd_interface2device_handle(iface, &sc->sc_udev);
    183 	if (r != USBD_NORMAL_COMPLETION) {
    184 		sc->sc_dying = 1;
    185 		USB_ATTACH_ERROR_RETURN;
    186 	}
    187 	sc->sc_ifaceno = id->bInterfaceNumber;
    188 
    189 #if 0
    190 /*
    191  * This code is disabled because for some mysterious it causes
    192  * printing not to work.  But only sometimes, and mostly with
    193  * UHCI and less often with OHCI.  *sigh*
    194  */
    195 	{
    196 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    197 	usb_device_request_t req;
    198 	int len, alen;
    199 
    200 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    201 	req.bRequest = UR_GET_DEVICE_ID;
    202 	USETW(req.wValue, cd->bConfigurationValue);
    203 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    204 	USETW(req.wLength, sizeof devinfo - 1);
    205 	r = usbd_do_request_flags(dev, &req, devinfo,USBD_SHORT_XFER_OK,&alen);
    206 	if (r != USBD_NORMAL_COMPLETION) {
    207 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
    208 	} else if (alen <= 2) {
    209 		printf("%s: empty device id, no printer connected?\n",
    210 		       USBDEVNAME(sc->sc_dev));
    211 	} else {
    212 		/* devinfo now contains an IEEE-1284 device ID */
    213 		len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
    214 		if (len > sizeof devinfo - 3)
    215 			len = sizeof devinfo - 3;
    216 		devinfo[len] = 0;
    217 		printf("%s: device id <", USBDEVNAME(sc->sc_dev));
    218 		ieee1284_print_id(devinfo+2);
    219 		printf(">\n");
    220 	}
    221 	}
    222 #endif
    223 
    224 	USB_ATTACH_SUCCESS_RETURN;
    225 
    226  nobulk:
    227 	printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
    228 	sc->sc_dying = 1;
    229 	USB_ATTACH_ERROR_RETURN;
    230 }
    231 
    232 int
    233 ulpt_activate(self, act)
    234 	device_ptr_t self;
    235 	enum devact act;
    236 {
    237 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    238 
    239 	switch (act) {
    240 	case DVACT_ACTIVATE:
    241 		return (EOPNOTSUPP);
    242 		break;
    243 
    244 	case DVACT_DEACTIVATE:
    245 		sc->sc_dying = 1;
    246 		break;
    247 	}
    248 	return (0);
    249 }
    250 
    251 int
    252 ulpt_detach(self, flags)
    253 	device_ptr_t self;
    254 	int flags;
    255 {
    256 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    257 	int maj, mn;
    258 	int s;
    259 
    260 	DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
    261 
    262 	sc->sc_dying = 1;
    263 	if (sc->sc_bulkpipe)
    264 		usbd_abort_pipe(sc->sc_bulkpipe);
    265 
    266 	s = splusb();
    267 	if (--sc->sc_refcnt >= 0) {
    268 		/* There is noone to wake, aborting the pipe is enough */
    269 		/* Wait for processes to go away. */
    270 		usb_detach_wait(USBDEV(sc->sc_dev));
    271 	}
    272 	splx(s);
    273 
    274 	/* locate the major number */
    275 	for (maj = 0; maj < nchrdev; maj++)
    276 		if (cdevsw[maj].d_open == ulptopen)
    277 			break;
    278 
    279 	/* Nuke the vnodes for any open instances (calls close). */
    280 	mn = self->dv_unit;
    281 	vdevgone(maj, mn, mn, VCHR);
    282 
    283 	return (0);
    284 }
    285 
    286 int
    287 ulpt_status(sc)
    288 	struct ulpt_softc *sc;
    289 {
    290 	usb_device_request_t req;
    291 	usbd_status r;
    292 	u_char status;
    293 
    294 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    295 	req.bRequest = UR_GET_PORT_STATUS;
    296 	USETW(req.wValue, 0);
    297 	USETW(req.wIndex, sc->sc_ifaceno);
    298 	USETW(req.wLength, 1);
    299 	r = usbd_do_request(sc->sc_udev, &req, &status);
    300 	DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
    301 	if (r == USBD_NORMAL_COMPLETION)
    302 		return (status);
    303 	else
    304 		return (0);
    305 }
    306 
    307 void
    308 ulpt_reset(sc)
    309 	struct ulpt_softc *sc;
    310 {
    311 	usb_device_request_t req;
    312 
    313 	DPRINTFN(1, ("ulpt_reset\n"));
    314 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    315 	req.bRequest = UR_SOFT_RESET;
    316 	USETW(req.wValue, 0);
    317 	USETW(req.wIndex, sc->sc_ifaceno);
    318 	USETW(req.wLength, 0);
    319 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    320 }
    321 
    322 /*
    323  * Reset the printer, then wait until it's selected and not busy.
    324  */
    325 int
    326 ulptopen(dev, flag, mode, p)
    327 	dev_t dev;
    328 	int flag;
    329 	int mode;
    330 	struct proc *p;
    331 {
    332 	u_char flags = ULPTFLAGS(dev);
    333 	struct ulpt_softc *sc;
    334 	usbd_status r;
    335 	int spin, error;
    336 
    337 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    338 
    339 	if (!sc->sc_iface || sc->sc_dying)
    340 		return (ENXIO);
    341 
    342 	if (sc->sc_state)
    343 		return (EBUSY);
    344 
    345 	sc->sc_state = ULPT_INIT;
    346 	sc->sc_flags = flags;
    347 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    348 
    349 	if ((flags & ULPT_NOPRIME) == 0)
    350 		ulpt_reset(sc);
    351 
    352 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    353 		if (spin >= TIMEOUT) {
    354 			sc->sc_state = 0;
    355 			return (EBUSY);
    356 		}
    357 
    358 		/* wait 1/4 second, give up if we get a signal */
    359 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    360 		if (error != EWOULDBLOCK) {
    361 			sc->sc_state = 0;
    362 			return (error);
    363 		}
    364 	}
    365 
    366 	r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
    367 	if (r != USBD_NORMAL_COMPLETION) {
    368 		sc->sc_state = 0;
    369 		return (EIO);
    370 	}
    371 
    372 	sc->sc_state = ULPT_OPEN;
    373 
    374 	DPRINTF(("ulptopen: done\n"));
    375 	return (0);
    376 }
    377 
    378 int
    379 ulpt_statusmsg(status, sc)
    380 	u_char status;
    381 	struct ulpt_softc *sc;
    382 {
    383 	u_char new;
    384 
    385 	status = (status ^ LPS_INVERT) & LPS_MASK;
    386 	new = status & ~sc->sc_laststatus;
    387 	sc->sc_laststatus = status;
    388 
    389 	if (new & LPS_SELECT)
    390 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    391 	else if (new & LPS_NOPAPER)
    392 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    393 	else if (new & LPS_NERR)
    394 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    395 
    396 	return (status);
    397 }
    398 
    399 int
    400 ulptclose(dev, flag, mode, p)
    401 	dev_t dev;
    402 	int flag;
    403 	int mode;
    404 	struct proc *p;
    405 {
    406 	struct ulpt_softc *sc;
    407 
    408 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    409 
    410 	if (sc->sc_state != ULPT_OPEN)
    411 		/* We are being forced to close before the open completed. */
    412 		return (0);
    413 
    414 	usbd_close_pipe(sc->sc_bulkpipe);
    415 	sc->sc_bulkpipe = 0;
    416 
    417 	sc->sc_state = 0;
    418 
    419 	DPRINTF(("ulptclose: closed\n"));
    420 	return (0);
    421 }
    422 
    423 int
    424 ulpt_do_write(sc, uio, flags)
    425 	struct ulpt_softc *sc;
    426 	struct uio *uio;
    427 	int flags;
    428 {
    429 	u_int32_t n;
    430 	int error = 0;
    431 	void *bufp;
    432 	usbd_request_handle reqh;
    433 	usbd_status r;
    434 
    435 	DPRINTF(("ulptwrite\n"));
    436 	reqh = usbd_alloc_request(sc->sc_udev);
    437 	if (reqh == 0)
    438 		return (ENOMEM);
    439 	bufp = usbd_alloc_buffer(reqh, ULPT_BSIZE);
    440 	if (bufp == 0) {
    441 		usbd_free_request(reqh);
    442 		return (ENOMEM);
    443 	}
    444 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    445 		ulpt_statusmsg(ulpt_status(sc), sc);
    446 		error = uiomove(bufp, n, uio);
    447 		if (error)
    448 			break;
    449 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    450 		r = usbd_bulk_transfer(reqh, sc->sc_bulkpipe, USBD_NO_COPY,
    451 				       USBD_NO_TIMEOUT, buf, &n, "ulptwr");
    452 		if (r != USBD_NORMAL_COMPLETION) {
    453 			DPRINTF(("ulptwrite: error=%d\n", r));
    454 			error = EIO;
    455 			break;
    456 		}
    457 	}
    458 	usbd_free_request(reqh);
    459 
    460 	return (error);
    461 }
    462 
    463 int
    464 ulptwrite(dev, uio, flags)
    465 	dev_t dev;
    466 	struct uio *uio;
    467 	int flags;
    468 {
    469 	struct ulpt_softc *sc;
    470 	int error;
    471 
    472 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    473 
    474 	if (sc->sc_dying)
    475 		return (EIO);
    476 
    477 	sc->sc_refcnt++;
    478 	error = ulpt_do_write(sc, uio, flags);
    479 	if (--sc->sc_refcnt < 0)
    480 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    481 	return (error);
    482 }
    483 
    484 int
    485 ulptioctl(dev, cmd, data, flag, p)
    486 	dev_t dev;
    487 	u_long cmd;
    488 	caddr_t data;
    489 	int flag;
    490 	struct proc *p;
    491 {
    492 	int error = 0;
    493 
    494 	switch (cmd) {
    495 	default:
    496 		error = ENODEV;
    497 	}
    498 
    499 	return (error);
    500 }
    501 
    502 #if 0
    503 /* XXX This does not belong here. */
    504 /*
    505  * Print select parts of a IEEE 1284 device ID.
    506  */
    507 void
    508 ieee1284_print_id(str)
    509 	char *str;
    510 {
    511 	char *p, *q;
    512 
    513 	for (p = str-1; p; p = strchr(p, ';')) {
    514 		p++;		/* skip ';' */
    515 		if (strncmp(p, "MFG:", 4) == 0 ||
    516 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
    517 		    strncmp(p, "MDL:", 4) == 0 ||
    518 		    strncmp(p, "MODEL:", 6) == 0) {
    519 			q = strchr(p, ';');
    520 			if (q)
    521 				printf("%.*s", (int)(q - p + 1), p);
    522 		}
    523 	}
    524 }
    525 #endif
    526 
    527 #if defined(__FreeBSD__)
    528 DRIVER_MODULE(ulpt, usb, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    529 #endif
    530