Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.16
      1 /*	$NetBSD: ulpt.c,v 1.16 1999/08/23 22:35:19 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 	struct device *self;
    225 	enum devact act;
    226 {
    227 	return (0);
    228 }
    229 
    230 int
    231 ulpt_detach(self, flags)
    232 	struct device  *self;
    233 	int flags;
    234 {
    235 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    236 	int maj, mn;
    237 	int s;
    238 
    239 	DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
    240 
    241 	sc->sc_dying = 1;
    242 	if (sc->sc_bulkpipe)
    243 		usbd_abort_pipe(sc->sc_bulkpipe);
    244 
    245 	s = splusb();
    246 	if (--sc->sc_refcnt >= 0) {
    247 		/* There is noone to wake, aborting the pipe is enough */
    248 		/* Wait for processes to go away. */
    249 		usb_detach_wait(&sc->sc_dev);
    250 	}
    251 	splx(s);
    252 
    253 	/* locate the major number */
    254 	for (maj = 0; maj < nchrdev; maj++)
    255 		if (cdevsw[maj].d_open == ulptopen)
    256 			break;
    257 
    258 	/* Nuke the vnodes for any open instances (calls close). */
    259 	mn = self->dv_unit;
    260 	vdevgone(maj, mn, mn, VCHR);
    261 
    262 	return (0);
    263 }
    264 
    265 int
    266 ulpt_status(sc)
    267 	struct ulpt_softc *sc;
    268 {
    269 	usb_device_request_t req;
    270 	usbd_status r;
    271 	u_char status;
    272 
    273 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    274 	req.bRequest = UR_GET_PORT_STATUS;
    275 	USETW(req.wValue, 0);
    276 	USETW(req.wIndex, sc->sc_ifaceno);
    277 	USETW(req.wLength, 1);
    278 	r = usbd_do_request(sc->sc_udev, &req, &status);
    279 	DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
    280 	if (r == USBD_NORMAL_COMPLETION)
    281 		return (status);
    282 	else
    283 		return (0);
    284 }
    285 
    286 void
    287 ulpt_reset(sc)
    288 	struct ulpt_softc *sc;
    289 {
    290 	usb_device_request_t req;
    291 
    292 	DPRINTFN(1, ("ulpt_reset\n"));
    293 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    294 	req.bRequest = UR_SOFT_RESET;
    295 	USETW(req.wValue, 0);
    296 	USETW(req.wIndex, sc->sc_ifaceno);
    297 	USETW(req.wLength, 0);
    298 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    299 }
    300 
    301 /*
    302  * Reset the printer, then wait until it's selected and not busy.
    303  */
    304 int
    305 ulptopen(dev, flag, mode, p)
    306 	dev_t dev;
    307 	int flag;
    308 	int mode;
    309 	struct proc *p;
    310 {
    311 	u_char flags = ULPTFLAGS(dev);
    312 	usbd_status r;
    313 	int spin, error;
    314 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    315 
    316 	if (!sc || !sc->sc_iface || sc->sc_dying)
    317 		return (ENXIO);
    318 
    319 	if (sc->sc_state)
    320 		return (EBUSY);
    321 
    322 	sc->sc_state = ULPT_INIT;
    323 	sc->sc_flags = flags;
    324 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    325 
    326 	if ((flags & ULPT_NOPRIME) == 0)
    327 		ulpt_reset(sc);
    328 
    329 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    330 		if (spin >= TIMEOUT) {
    331 			sc->sc_state = 0;
    332 			return (EBUSY);
    333 		}
    334 
    335 		/* wait 1/4 second, give up if we get a signal */
    336 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    337 		if (error != EWOULDBLOCK) {
    338 			sc->sc_state = 0;
    339 			return (error);
    340 		}
    341 	}
    342 
    343 	r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
    344 	if (r != USBD_NORMAL_COMPLETION) {
    345 		sc->sc_state = 0;
    346 		return (EIO);
    347 	}
    348 
    349 	sc->sc_state = ULPT_OPEN;
    350 
    351 	DPRINTF(("ulptopen: done\n"));
    352 	return (0);
    353 }
    354 
    355 int
    356 ulpt_statusmsg(status, sc)
    357 	u_char status;
    358 	struct ulpt_softc *sc;
    359 {
    360 	u_char new;
    361 
    362 	status = (status ^ LPS_INVERT) & LPS_MASK;
    363 	new = status & ~sc->sc_laststatus;
    364 	sc->sc_laststatus = status;
    365 
    366 	if (new & LPS_SELECT)
    367 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    368 	else if (new & LPS_NOPAPER)
    369 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    370 	else if (new & LPS_NERR)
    371 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    372 
    373 	return (status);
    374 }
    375 
    376 int
    377 ulptclose(dev, flag, mode, p)
    378 	dev_t dev;
    379 	int flag;
    380 	int mode;
    381 	struct proc *p;
    382 {
    383 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    384 
    385 	if (sc->sc_state != ULPT_OPEN)
    386 		/* We are being forced to close before the open completed. */
    387 		return (0);
    388 
    389 	usbd_close_pipe(sc->sc_bulkpipe);
    390 	sc->sc_bulkpipe = 0;
    391 
    392 	sc->sc_state = 0;
    393 
    394 	DPRINTF(("ulptclose: closed\n"));
    395 	return (0);
    396 }
    397 
    398 int
    399 ulpt_do_write(sc, uio, flags)
    400 	struct ulpt_softc *sc;
    401 	struct uio *uio;
    402 	int flags;
    403 {
    404 	u_int32_t n;
    405 	int error = 0;
    406 	char buf[ULPT_BSIZE];
    407 	usbd_request_handle reqh;
    408 	usbd_status r;
    409 
    410 	DPRINTF(("ulptwrite\n"));
    411 	reqh = usbd_alloc_request();
    412 	if (reqh == 0)
    413 		return (ENOMEM);
    414 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    415 		ulpt_statusmsg(ulpt_status(sc), sc);
    416 		error = uiomove(buf, n, uio);
    417 		if (error)
    418 			break;
    419 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    420 		r = usbd_bulk_transfer(reqh, sc->sc_bulkpipe, 0,
    421 				       USBD_NO_TIMEOUT, buf, &n, "ulptwr");
    422 		if (r != USBD_NORMAL_COMPLETION) {
    423 			DPRINTF(("ulptwrite: error=%d\n", r));
    424 			error = EIO;
    425 			break;
    426 		}
    427 	}
    428 	usbd_free_request(reqh);
    429 
    430 	return (error);
    431 }
    432 
    433 int
    434 ulptwrite(dev, uio, flags)
    435 	dev_t dev;
    436 	struct uio *uio;
    437 	int flags;
    438 {
    439 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    440 	int error;
    441 
    442 	sc->sc_refcnt++;
    443 	error = ulpt_do_write(sc, uio, flags);
    444 	if (--sc->sc_refcnt < 0)
    445 		usb_detach_wakeup(&sc->sc_dev);
    446 	return (error);
    447 }
    448 
    449 int
    450 ulptioctl(dev, cmd, data, flag, p)
    451 	dev_t dev;
    452 	u_long cmd;
    453 	caddr_t data;
    454 	int flag;
    455 	struct proc *p;
    456 {
    457 	int error = 0;
    458 
    459 	switch (cmd) {
    460 	default:
    461 		error = ENODEV;
    462 	}
    463 
    464 	return error;
    465 }
    466 
    467 /* XXX This does not belong here. */
    468 /*
    469  * Print secelect parts of a IEEE 1284 device ID.
    470  */
    471 void
    472 ieee1284_print_id(str)
    473 	char *str;
    474 {
    475 	char *p, *q;
    476 
    477 	for (p = str-1; p; p = strchr(p, ';')) {
    478 		p++;		/* skip ';' */
    479 		if (strncmp(p, "MFG:", 4) == 0 ||
    480 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
    481 		    strncmp(p, "MDL:", 4) == 0 ||
    482 		    strncmp(p, "MODEL:", 6) == 0) {
    483 			q = strchr(p, ';');
    484 			if (q)
    485 				printf("%.*s", (int)(q - p + 1), p);
    486 		}
    487 	}
    488 }
    489 
    490 #if defined(__FreeBSD__)
    491 DRIVER_MODULE(ulpt, usb, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    492 #endif
    493