Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.1
      1 /*	$NetBSD: ulpt.c,v 1.1 1998/07/12 19:52:00 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Lennart Augustsson <augustss (at) carlstedt.se>
      8  *         Carlstedt Research & Technology
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/user.h>
     43 #include <sys/malloc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/uio.h>
     47 #include <sys/device.h>
     48 #include <sys/conf.h>
     49 #include <sys/syslog.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	1024
     62 
     63 #ifdef USB_DEBUG
     64 #define DPRINTF(x)	if (ulptdebug) printf x
     65 #define DPRINTFN(n,x)	if (ulptdebug>(n)) printf x
     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 	usbd_device_handle sc_udev;	/* device */
     85 	usbd_interface_handle sc_iface;	/* interface */
     86 	int sc_ifaceno;
     87 	usbd_pipe_handle sc_bulkpipe;	/* bulk pipe */
     88 	int sc_bulk;
     89 
     90 	u_char sc_state;
     91 #define	ULPT_OPEN	0x01	/* device is open */
     92 #define	ULPT_OBUSY	0x02	/* printer is busy doing output */
     93 #define	ULPT_INIT	0x04	/* waiting to initialize for open */
     94 	u_char sc_flags;
     95 #define	ULPT_NOPRIME	0x40	/* don't prime on open */
     96 	u_char sc_laststatus;
     97 };
     98 
     99 int ulpt_match __P((struct device *, struct cfdata *, void *));
    100 void ulpt_attach __P((struct device *, struct device *, void *));
    101 
    102 int ulptopen __P((dev_t, int, int, struct proc *));
    103 int ulptclose __P((dev_t, int, int, struct proc *p));
    104 int ulptwrite __P((dev_t, struct uio *uio, int));
    105 int ulptioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    106 void ulpt_disco __P((void *));
    107 
    108 int ulpt_status __P((struct ulpt_softc *));
    109 void ulpt_reset __P((struct ulpt_softc *));
    110 int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
    111 
    112 extern struct cfdriver ulpt_cd;
    113 
    114 #define	ULPTUNIT(s)	(minor(s) & 0x1f)
    115 #define	ULPTFLAGS(s)	(minor(s) & 0xe0)
    116 
    117 extern struct cfdriver ulpt_cd;
    118 
    119 struct cfattach ulpt_ca = {
    120 	sizeof(struct ulpt_softc), ulpt_match, ulpt_attach
    121 };
    122 
    123 int
    124 ulpt_match(parent, match, aux)
    125 	struct device *parent;
    126 	struct cfdata *match;
    127 	void *aux;
    128 {
    129 	struct usb_attach_arg *uaa = aux;
    130 	usb_interface_descriptor_t *id;
    131 
    132 	DPRINTFN(10,("ulpt_match\n"));
    133 	if (!uaa->iface)
    134 		return (UMATCH_NONE);
    135 	id = usbd_get_interface_descriptor(uaa->iface);
    136 	if (id->bInterfaceClass == UCLASS_PRINTER &&
    137 	    id->bInterfaceSubClass == USUBCLASS_PRINTER &&
    138 	    (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
    139 	     id->bInterfaceProtocol == UPROTO_PRINTER_BI))
    140 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    141 	return (UMATCH_NONE);
    142 }
    143 
    144 void
    145 ulpt_attach(parent, self, aux)
    146 	struct device *parent;
    147 	struct device *self;
    148 	void *aux;
    149 {
    150 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    151 	struct usb_attach_arg *uaa = aux;
    152 	usbd_device_handle dev = uaa->device;
    153 	usbd_interface_handle iface = uaa->iface;
    154 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    155 #if 0
    156 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    157 	usb_device_request_t req;
    158 #endif
    159 	char devinfo[1024];
    160 	usb_endpoint_descriptor_t *ed;
    161 	usbd_status r;
    162 
    163 	DPRINTFN(2,("ulpt_attach: sc=%p\n", sc));
    164 	usbd_devinfo(dev, 1, devinfo);
    165 	printf(": %s (interface class %d/%d)\n", devinfo,
    166 	       id->bInterfaceClass, id->bInterfaceSubClass);
    167 
    168 	/* Figure out which endpoint is the bulk out endpoint. */
    169 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    170 	if (!ed)
    171 		goto nobulk;
    172 	if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
    173 	    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
    174 		/* In case we are using a bidir protocol... */
    175 		ed = usbd_interface2endpoint_descriptor(iface, 1);
    176 		if (!ed)
    177 			goto nobulk;
    178 		if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
    179 		    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
    180 			goto nobulk;
    181 	}
    182 	sc->sc_bulk = ed->bEndpointAddress;
    183 	DPRINTFN(1, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
    184 
    185 	sc->sc_iface = iface;
    186 	r = usbd_interface2device_handle(iface, &sc->sc_udev);
    187 	if (r != USBD_NORMAL_COMPLETION)
    188 		return;
    189 	sc->sc_ifaceno = id->bInterfaceNumber;
    190 
    191 #if 0
    192 XXX needs a different way to read the id string since the length
    193 is unknown.  usbd_do_request() returns error on a short transfer.
    194 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    195 	req.bRequest = UR_GET_DEVICE_ID;
    196 	USETW(req.wValue, cd->bConfigurationValue);
    197 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    198 	USETW(req.wLength, sizeof devinfo - 1);
    199 	r = usbd_do_request(dev, &req, devinfo);
    200 	if (r == USBD_NORMAL_COMPLETION) {
    201 		int len;
    202 		char *idstr;
    203 		len = (devinfo[0] << 8) | (devinfo[1] & 0xff);
    204 		/* devinfo now contains an IEEE-1284 device ID */
    205 		idstr = devinfo+2;
    206 		idstr[len] = 0;
    207 		printf("%s: device id <%s>\n", sc->sc_dev.dv_xname, idstr);
    208 	} else {
    209 		printf("%s: cannot get device id\n", sc->sc_dev.dv_xname);
    210 	}
    211 #endif
    212 
    213 	return;
    214 
    215  nobulk:
    216 	printf("%s: could not find bulk endpoint\n", sc->sc_dev.dv_xname);
    217 	return;
    218 }
    219 
    220 int
    221 ulpt_status(sc)
    222 	struct ulpt_softc *sc;
    223 {
    224 	usb_device_request_t req;
    225 	usbd_status r;
    226 	u_char status;
    227 
    228 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    229 	req.bRequest = UR_GET_PORT_STATUS;
    230 	USETW(req.wValue, 0);
    231 	USETW(req.wIndex, sc->sc_ifaceno);
    232 	USETW(req.wLength, 1);
    233 	r = usbd_do_request(sc->sc_udev, &req, &status);
    234 	DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
    235 	if (r == USBD_NORMAL_COMPLETION)
    236 		return (status);
    237 	else
    238 		return (0);
    239 }
    240 
    241 void
    242 ulpt_reset(sc)
    243 	struct ulpt_softc *sc;
    244 {
    245 	usb_device_request_t req;
    246 
    247 	DPRINTFN(1, ("ulpt_reset\n"));
    248 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    249 	req.bRequest = UR_SOFT_RESET;
    250 	USETW(req.wValue, 0);
    251 	USETW(req.wIndex, sc->sc_ifaceno);
    252 	USETW(req.wLength, 0);
    253 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    254 }
    255 
    256 /*
    257  * Reset the printer, then wait until it's selected and not busy.
    258  */
    259 int
    260 ulptopen(dev, flag, mode, p)
    261 	dev_t dev;
    262 	int flag;
    263 	int mode;
    264 	struct proc *p;
    265 {
    266 	int unit = ULPTUNIT(dev);
    267 	u_char flags = ULPTFLAGS(dev);
    268 	struct ulpt_softc *sc;
    269 	usbd_status r;
    270 	int spin, error;
    271 
    272 	if (unit >= ulpt_cd.cd_ndevs)
    273 		return ENXIO;
    274 	sc = ulpt_cd.cd_devs[unit];
    275 	if (!sc || !sc->sc_iface)
    276 		return ENXIO;
    277 
    278 	if (sc->sc_state)
    279 		return EBUSY;
    280 
    281 	sc->sc_state = ULPT_INIT;
    282 	sc->sc_flags = flags;
    283 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    284 
    285 	if ((flags & ULPT_NOPRIME) == 0)
    286 		ulpt_reset(sc);
    287 
    288 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    289 		if (spin >= TIMEOUT) {
    290 			sc->sc_state = 0;
    291 			return EBUSY;
    292 		}
    293 
    294 		/* wait 1/4 second, give up if we get a signal */
    295 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    296 		if (error != EWOULDBLOCK) {
    297 			sc->sc_state = 0;
    298 			return error;
    299 		}
    300 	}
    301 
    302 	r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
    303 	if (r != USBD_NORMAL_COMPLETION) {
    304 		sc->sc_state = 0;
    305 		return (EIO);
    306 	}
    307 
    308 	sc->sc_state = ULPT_OPEN;
    309 
    310 	DPRINTF(("ulptopen: done\n"));
    311 	return (0);
    312 }
    313 
    314 int
    315 ulpt_statusmsg(status, sc)
    316 	u_char status;
    317 	struct ulpt_softc *sc;
    318 {
    319 	u_char new;
    320 
    321 	status = (status ^ LPS_INVERT) & LPS_MASK;
    322 	new = status & ~sc->sc_laststatus;
    323 	sc->sc_laststatus = status;
    324 
    325 	if (new & LPS_SELECT)
    326 		log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
    327 	else if (new & LPS_NOPAPER)
    328 		log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
    329 	else if (new & LPS_NERR)
    330 		log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
    331 
    332 	return status;
    333 }
    334 
    335 int
    336 ulptclose(dev, flag, mode, p)
    337 	dev_t dev;
    338 	int flag;
    339 	int mode;
    340 	struct proc *p;
    341 {
    342 	int unit = ULPTUNIT(dev);
    343 	struct ulpt_softc *sc = ulpt_cd.cd_devs[unit];
    344 
    345 	usbd_close_pipe(sc->sc_bulkpipe);
    346 
    347 	sc->sc_state = 0;
    348 
    349 	DPRINTF(("ulptclose: closed\n"));
    350 	return (0);
    351 }
    352 
    353 int
    354 ulptwrite(dev, uio, flags)
    355 	dev_t dev;
    356 	struct uio *uio;
    357 	int flags;
    358 {
    359 	struct ulpt_softc *sc = ulpt_cd.cd_devs[ULPTUNIT(dev)];
    360 	size_t n;
    361 	int error = 0;
    362 	char buf[ULPT_BSIZE];
    363 	usbd_request_handle reqh;
    364 	usbd_status r;
    365 
    366 	DPRINTF(("ulptwrite\n"));
    367 	reqh = usbd_alloc_request();
    368 	if (reqh == 0)
    369 		return (EIO);
    370 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    371 		ulpt_statusmsg(ulpt_status(sc), sc);
    372 		uiomove(buf, n, uio);
    373 		/* XXX use callback to enable interrupt? */
    374 		r = usbd_setup_request(reqh, sc->sc_bulkpipe, 0, buf, n,
    375 				       0, USBD_NO_TIMEOUT, 0);
    376 		if (r != USBD_NORMAL_COMPLETION) {
    377 			error = EIO;
    378 			break;
    379 		}
    380 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    381 		r = usbd_sync_transfer(reqh);
    382 		if (r != USBD_NORMAL_COMPLETION) {
    383 			DPRINTF(("ulptwrite: error=%d\n", r));
    384 			usbd_clear_endpoint_stall(sc->sc_bulkpipe);
    385 			error = EIO;
    386 			break;
    387 		}
    388 	}
    389 	usbd_free_request(reqh);
    390 	return (error);
    391 }
    392 
    393 int
    394 ulptioctl(dev, cmd, data, flag, p)
    395 	dev_t dev;
    396 	u_long cmd;
    397 	caddr_t data;
    398 	int flag;
    399 	struct proc *p;
    400 {
    401 	int error = 0;
    402 
    403 	switch (cmd) {
    404 	default:
    405 		error = ENODEV;
    406 	}
    407 
    408 	return error;
    409 }
    410