Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.18
      1 /*	$NetBSD: ulpt.c,v 1.18 1999/08/28 21:42:35 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	1024
     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 	bdevice 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 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    155 	usb_device_request_t req;
    156 	int len, alen;
    157 	char devinfo[1024];
    158 	usb_endpoint_descriptor_t *ed;
    159 	usbd_status r;
    160 
    161 	DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
    162 	usbd_devinfo(dev, 0, devinfo);
    163 	USB_ATTACH_SETUP;
    164 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    165 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    166 
    167 	/* Figure out which endpoint is the bulk out endpoint. */
    168 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    169 	if (!ed)
    170 		goto nobulk;
    171 	if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
    172 	    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
    173 		/* In case we are using a bidir protocol... */
    174 		ed = usbd_interface2endpoint_descriptor(iface, 1);
    175 		if (!ed)
    176 			goto nobulk;
    177 		if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
    178 		    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
    179 			goto nobulk;
    180 	}
    181 	sc->sc_bulk = ed->bEndpointAddress;
    182 	DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
    183 
    184 	sc->sc_iface = iface;
    185 	r = usbd_interface2device_handle(iface, &sc->sc_udev);
    186 	if (r != USBD_NORMAL_COMPLETION) {
    187 		sc->sc_dying = 1;
    188 		USB_ATTACH_ERROR_RETURN;
    189 	}
    190 	sc->sc_ifaceno = id->bInterfaceNumber;
    191 
    192 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    193 	req.bRequest = UR_GET_DEVICE_ID;
    194 	USETW(req.wValue, cd->bConfigurationValue);
    195 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    196 	USETW(req.wLength, sizeof devinfo - 1);
    197 	r = usbd_do_request_flags(dev, &req, devinfo,USBD_SHORT_XFER_OK,&alen);
    198 	if (r != USBD_NORMAL_COMPLETION) {
    199 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
    200 	} else if (alen <= 2) {
    201 		printf("%s: empty device id, no printer connected?\n",
    202 		       USBDEVNAME(sc->sc_dev));
    203 	} else {
    204 		/* devinfo now contains an IEEE-1284 device ID */
    205 		len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
    206 		if (len > sizeof devinfo - 3)
    207 			len = sizeof devinfo - 3;
    208 		devinfo[len] = 0;
    209 		printf("%s: device id <", USBDEVNAME(sc->sc_dev));
    210 		ieee1284_print_id(devinfo+2);
    211 		printf(">\n");
    212 	}
    213 
    214 	USB_ATTACH_SUCCESS_RETURN;
    215 
    216  nobulk:
    217 	printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
    218 	sc->sc_dying = 1;
    219 	USB_ATTACH_ERROR_RETURN;
    220 }
    221 
    222 int
    223 ulpt_activate(self, act)
    224 	bdevice *self;
    225 	enum devact act;
    226 {
    227 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    228 
    229 	switch (act) {
    230 	case DVACT_ACTIVATE:
    231 		return (EOPNOTSUPP);
    232 		break;
    233 
    234 	case DVACT_DEACTIVATE:
    235 		sc->sc_dying = 1;
    236 		break;
    237 	}
    238 	return (0);
    239 }
    240 
    241 int
    242 ulpt_detach(self, flags)
    243 	bdevice *self;
    244 	int flags;
    245 {
    246 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    247 	int maj, mn;
    248 	int s;
    249 
    250 	DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
    251 
    252 	sc->sc_dying = 1;
    253 	if (sc->sc_bulkpipe)
    254 		usbd_abort_pipe(sc->sc_bulkpipe);
    255 
    256 	s = splusb();
    257 	if (--sc->sc_refcnt >= 0) {
    258 		/* There is noone to wake, aborting the pipe is enough */
    259 		/* Wait for processes to go away. */
    260 		usb_detach_wait(&sc->sc_dev);
    261 	}
    262 	splx(s);
    263 
    264 	/* locate the major number */
    265 	for (maj = 0; maj < nchrdev; maj++)
    266 		if (cdevsw[maj].d_open == ulptopen)
    267 			break;
    268 
    269 	/* Nuke the vnodes for any open instances (calls close). */
    270 	mn = self->dv_unit;
    271 	vdevgone(maj, mn, mn, VCHR);
    272 
    273 	return (0);
    274 }
    275 
    276 int
    277 ulpt_status(sc)
    278 	struct ulpt_softc *sc;
    279 {
    280 	usb_device_request_t req;
    281 	usbd_status r;
    282 	u_char status;
    283 
    284 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    285 	req.bRequest = UR_GET_PORT_STATUS;
    286 	USETW(req.wValue, 0);
    287 	USETW(req.wIndex, sc->sc_ifaceno);
    288 	USETW(req.wLength, 1);
    289 	r = usbd_do_request(sc->sc_udev, &req, &status);
    290 	DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
    291 	if (r == USBD_NORMAL_COMPLETION)
    292 		return (status);
    293 	else
    294 		return (0);
    295 }
    296 
    297 void
    298 ulpt_reset(sc)
    299 	struct ulpt_softc *sc;
    300 {
    301 	usb_device_request_t req;
    302 
    303 	DPRINTFN(1, ("ulpt_reset\n"));
    304 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    305 	req.bRequest = UR_SOFT_RESET;
    306 	USETW(req.wValue, 0);
    307 	USETW(req.wIndex, sc->sc_ifaceno);
    308 	USETW(req.wLength, 0);
    309 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    310 }
    311 
    312 /*
    313  * Reset the printer, then wait until it's selected and not busy.
    314  */
    315 int
    316 ulptopen(dev, flag, mode, p)
    317 	dev_t dev;
    318 	int flag;
    319 	int mode;
    320 	struct proc *p;
    321 {
    322 	u_char flags = ULPTFLAGS(dev);
    323 	usbd_status r;
    324 	int spin, error;
    325 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    326 
    327 	if (!sc || !sc->sc_iface || sc->sc_dying)
    328 		return (ENXIO);
    329 
    330 	if (sc->sc_state)
    331 		return (EBUSY);
    332 
    333 	sc->sc_state = ULPT_INIT;
    334 	sc->sc_flags = flags;
    335 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    336 
    337 	if ((flags & ULPT_NOPRIME) == 0)
    338 		ulpt_reset(sc);
    339 
    340 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    341 		if (spin >= TIMEOUT) {
    342 			sc->sc_state = 0;
    343 			return (EBUSY);
    344 		}
    345 
    346 		/* wait 1/4 second, give up if we get a signal */
    347 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    348 		if (error != EWOULDBLOCK) {
    349 			sc->sc_state = 0;
    350 			return (error);
    351 		}
    352 	}
    353 
    354 	r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
    355 	if (r != USBD_NORMAL_COMPLETION) {
    356 		sc->sc_state = 0;
    357 		return (EIO);
    358 	}
    359 
    360 	sc->sc_state = ULPT_OPEN;
    361 
    362 	DPRINTF(("ulptopen: done\n"));
    363 	return (0);
    364 }
    365 
    366 int
    367 ulpt_statusmsg(status, sc)
    368 	u_char status;
    369 	struct ulpt_softc *sc;
    370 {
    371 	u_char new;
    372 
    373 	status = (status ^ LPS_INVERT) & LPS_MASK;
    374 	new = status & ~sc->sc_laststatus;
    375 	sc->sc_laststatus = status;
    376 
    377 	if (new & LPS_SELECT)
    378 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    379 	else if (new & LPS_NOPAPER)
    380 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    381 	else if (new & LPS_NERR)
    382 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    383 
    384 	return (status);
    385 }
    386 
    387 int
    388 ulptclose(dev, flag, mode, p)
    389 	dev_t dev;
    390 	int flag;
    391 	int mode;
    392 	struct proc *p;
    393 {
    394 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    395 
    396 	if (sc->sc_state != ULPT_OPEN)
    397 		/* We are being forced to close before the open completed. */
    398 		return (0);
    399 
    400 	usbd_close_pipe(sc->sc_bulkpipe);
    401 	sc->sc_bulkpipe = 0;
    402 
    403 	sc->sc_state = 0;
    404 
    405 	DPRINTF(("ulptclose: closed\n"));
    406 	return (0);
    407 }
    408 
    409 int
    410 ulpt_do_write(sc, uio, flags)
    411 	struct ulpt_softc *sc;
    412 	struct uio *uio;
    413 	int flags;
    414 {
    415 	u_int32_t n;
    416 	int error = 0;
    417 	char buf[ULPT_BSIZE];
    418 	usbd_request_handle reqh;
    419 	usbd_status r;
    420 
    421 	DPRINTF(("ulptwrite\n"));
    422 	reqh = usbd_alloc_request();
    423 	if (reqh == 0)
    424 		return (ENOMEM);
    425 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    426 		ulpt_statusmsg(ulpt_status(sc), sc);
    427 		error = uiomove(buf, n, uio);
    428 		if (error)
    429 			break;
    430 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    431 		r = usbd_bulk_transfer(reqh, sc->sc_bulkpipe, 0,
    432 				       USBD_NO_TIMEOUT, buf, &n, "ulptwr");
    433 		if (r != USBD_NORMAL_COMPLETION) {
    434 			DPRINTF(("ulptwrite: error=%d\n", r));
    435 			error = EIO;
    436 			break;
    437 		}
    438 	}
    439 	usbd_free_request(reqh);
    440 
    441 	return (error);
    442 }
    443 
    444 int
    445 ulptwrite(dev, uio, flags)
    446 	dev_t dev;
    447 	struct uio *uio;
    448 	int flags;
    449 {
    450 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    451 	int error;
    452 
    453 	sc->sc_refcnt++;
    454 	error = ulpt_do_write(sc, uio, flags);
    455 	if (--sc->sc_refcnt < 0)
    456 		usb_detach_wakeup(&sc->sc_dev);
    457 	return (error);
    458 }
    459 
    460 int
    461 ulptioctl(dev, cmd, data, flag, p)
    462 	dev_t dev;
    463 	u_long cmd;
    464 	caddr_t data;
    465 	int flag;
    466 	struct proc *p;
    467 {
    468 	int error = 0;
    469 
    470 	switch (cmd) {
    471 	default:
    472 		error = ENODEV;
    473 	}
    474 
    475 	return (error);
    476 }
    477 
    478 /* XXX This does not belong here. */
    479 /*
    480  * Print select parts of a IEEE 1284 device ID.
    481  */
    482 void
    483 ieee1284_print_id(str)
    484 	char *str;
    485 {
    486 	char *p, *q;
    487 
    488 	for (p = str-1; p; p = strchr(p, ';')) {
    489 		p++;		/* skip ';' */
    490 		if (strncmp(p, "MFG:", 4) == 0 ||
    491 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
    492 		    strncmp(p, "MDL:", 4) == 0 ||
    493 		    strncmp(p, "MODEL:", 6) == 0) {
    494 			q = strchr(p, ';');
    495 			if (q)
    496 				printf("%.*s", (int)(q - p + 1), p);
    497 		}
    498 	}
    499 }
    500 
    501 #if defined(__FreeBSD__)
    502 DRIVER_MODULE(ulpt, usb, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    503 #endif
    504