Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.11
      1 /*	$NetBSD: ulpt.c,v 1.11 1999/01/10 11:13:36 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/malloc.h>
     48 #include <sys/kernel.h>
     49 #if defined(__NetBSD__)
     50 #include <sys/device.h>
     51 #include <sys/ioctl.h>
     52 #elif defined(__FreeBSD__)
     53 #include <sys/ioccom.h>
     54 #include <sys/module.h>
     55 #include <sys/bus.h>
     56 #endif
     57 #include <sys/uio.h>
     58 #include <sys/conf.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) printf x
     75 #define DPRINTFN(n,x)	if (ulptdebug>(n)) printf 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 
    109 int ulptopen __P((dev_t, int, int, struct proc *));
    110 int ulptclose __P((dev_t, int, int, struct proc *p));
    111 int ulptwrite __P((dev_t, struct uio *uio, int));
    112 int ulptioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    113 void ulpt_disco __P((void *));
    114 
    115 int ulpt_status __P((struct ulpt_softc *));
    116 void ulpt_reset __P((struct ulpt_softc *));
    117 int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
    118 
    119 #define	ULPTUNIT(s)	(minor(s) & 0x1f)
    120 #define	ULPTFLAGS(s)	(minor(s) & 0xe0)
    121 
    122 USB_DECLARE_DRIVER(ulpt);
    123 
    124 USB_MATCH(ulpt)
    125 {
    126 	USB_MATCH_START(ulpt, uaa);
    127 	usb_interface_descriptor_t *id;
    128 
    129 	DPRINTFN(10,("ulpt_match\n"));
    130 	if (!uaa->iface)
    131 		return (UMATCH_NONE);
    132 	id = usbd_get_interface_descriptor(uaa->iface);
    133 	if (id &&
    134 	    id->bInterfaceClass == UCLASS_PRINTER &&
    135 	    id->bInterfaceSubClass == USUBCLASS_PRINTER &&
    136 	    (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
    137 	     id->bInterfaceProtocol == UPROTO_PRINTER_BI))
    138 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    139 	return (UMATCH_NONE);
    140 }
    141 
    142 USB_ATTACH(ulpt)
    143 {
    144 	USB_ATTACH_START(ulpt, sc, uaa);
    145 	usbd_device_handle dev = uaa->device;
    146 	usbd_interface_handle iface = uaa->iface;
    147 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    148 #if 0
    149 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    150 	usb_device_request_t req;
    151 #endif
    152 	char devinfo[1024];
    153 	usb_endpoint_descriptor_t *ed;
    154 	usbd_status r;
    155 
    156 	DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
    157 	usbd_devinfo(dev, 0, devinfo);
    158 	USB_ATTACH_SETUP;
    159 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    160 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    161 
    162 	/* Figure out which endpoint is the bulk out endpoint. */
    163 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    164 	if (!ed)
    165 		goto nobulk;
    166 	if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
    167 	    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
    168 		/* In case we are using a bidir protocol... */
    169 		ed = usbd_interface2endpoint_descriptor(iface, 1);
    170 		if (!ed)
    171 			goto nobulk;
    172 		if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
    173 		    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
    174 			goto nobulk;
    175 	}
    176 	sc->sc_bulk = ed->bEndpointAddress;
    177 	DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
    178 
    179 	sc->sc_iface = iface;
    180 	r = usbd_interface2device_handle(iface, &sc->sc_udev);
    181 	if (r != USBD_NORMAL_COMPLETION)
    182 		USB_ATTACH_ERROR_RETURN;
    183 	sc->sc_ifaceno = id->bInterfaceNumber;
    184 
    185 #if 0
    186 XXX needs a different way to read the id string since the length
    187 is unknown.  usbd_do_request() returns error on a short transfer.
    188 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    189 	req.bRequest = UR_GET_DEVICE_ID;
    190 	USETW(req.wValue, cd->bConfigurationValue);
    191 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    192 	USETW(req.wLength, sizeof devinfo - 1);
    193 	r = usbd_do_request(dev, &req, devinfo);
    194 	if (r == USBD_NORMAL_COMPLETION) {
    195 		int len;
    196 		char *idstr;
    197 		len = (devinfo[0] << 8) | (devinfo[1] & 0xff);
    198 		/* devinfo now contains an IEEE-1284 device ID */
    199 		idstr = devinfo+2;
    200 		idstr[len] = 0;
    201 		printf("%s: device id <%s>\n", USBDEVNAME(sc->sc_dev), idstr);
    202 	} else {
    203 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
    204 	}
    205 #endif
    206 
    207 	USB_ATTACH_SUCCESS_RETURN;
    208 
    209  nobulk:
    210 	printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
    211 	USB_ATTACH_ERROR_RETURN;
    212 }
    213 
    214 int
    215 ulpt_status(sc)
    216 	struct ulpt_softc *sc;
    217 {
    218 	usb_device_request_t req;
    219 	usbd_status r;
    220 	u_char status;
    221 
    222 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    223 	req.bRequest = UR_GET_PORT_STATUS;
    224 	USETW(req.wValue, 0);
    225 	USETW(req.wIndex, sc->sc_ifaceno);
    226 	USETW(req.wLength, 1);
    227 	r = usbd_do_request(sc->sc_udev, &req, &status);
    228 	DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
    229 	if (r == USBD_NORMAL_COMPLETION)
    230 		return (status);
    231 	else
    232 		return (0);
    233 }
    234 
    235 void
    236 ulpt_reset(sc)
    237 	struct ulpt_softc *sc;
    238 {
    239 	usb_device_request_t req;
    240 
    241 	DPRINTFN(1, ("ulpt_reset\n"));
    242 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    243 	req.bRequest = UR_SOFT_RESET;
    244 	USETW(req.wValue, 0);
    245 	USETW(req.wIndex, sc->sc_ifaceno);
    246 	USETW(req.wLength, 0);
    247 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    248 }
    249 
    250 /*
    251  * Reset the printer, then wait until it's selected and not busy.
    252  */
    253 int
    254 ulptopen(dev, flag, mode, p)
    255 	dev_t dev;
    256 	int flag;
    257 	int mode;
    258 	struct proc *p;
    259 {
    260 	u_char flags = ULPTFLAGS(dev);
    261 	usbd_status r;
    262 	int spin, error;
    263 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    264 
    265 	if (!sc || !sc->sc_iface)
    266 		return ENXIO;
    267 
    268 	if (sc->sc_state)
    269 		return EBUSY;
    270 
    271 	sc->sc_state = ULPT_INIT;
    272 	sc->sc_flags = flags;
    273 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    274 
    275 	if ((flags & ULPT_NOPRIME) == 0)
    276 		ulpt_reset(sc);
    277 
    278 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    279 		if (spin >= TIMEOUT) {
    280 			sc->sc_state = 0;
    281 			return EBUSY;
    282 		}
    283 
    284 		/* wait 1/4 second, give up if we get a signal */
    285 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    286 		if (error != EWOULDBLOCK) {
    287 			sc->sc_state = 0;
    288 			return error;
    289 		}
    290 	}
    291 
    292 	r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
    293 	if (r != USBD_NORMAL_COMPLETION) {
    294 		sc->sc_state = 0;
    295 		return (EIO);
    296 	}
    297 
    298 	sc->sc_state = ULPT_OPEN;
    299 
    300 	DPRINTF(("ulptopen: done\n"));
    301 	return (0);
    302 }
    303 
    304 int
    305 ulpt_statusmsg(status, sc)
    306 	u_char status;
    307 	struct ulpt_softc *sc;
    308 {
    309 	u_char new;
    310 
    311 	status = (status ^ LPS_INVERT) & LPS_MASK;
    312 	new = status & ~sc->sc_laststatus;
    313 	sc->sc_laststatus = status;
    314 
    315 	if (new & LPS_SELECT)
    316 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    317 	else if (new & LPS_NOPAPER)
    318 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    319 	else if (new & LPS_NERR)
    320 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    321 
    322 	return status;
    323 }
    324 
    325 int
    326 ulptclose(dev, flag, mode, p)
    327 	dev_t dev;
    328 	int flag;
    329 	int mode;
    330 	struct proc *p;
    331 {
    332 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    333 
    334 	usbd_close_pipe(sc->sc_bulkpipe);
    335 
    336 	sc->sc_state = 0;
    337 
    338 	DPRINTF(("ulptclose: closed\n"));
    339 	return (0);
    340 }
    341 
    342 int
    343 ulptwrite(dev, uio, flags)
    344 	dev_t dev;
    345 	struct uio *uio;
    346 	int flags;
    347 {
    348 	size_t n;
    349 	int error = 0;
    350 	char buf[ULPT_BSIZE];
    351 	usbd_request_handle reqh;
    352 	usbd_status r;
    353 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    354 
    355 	DPRINTF(("ulptwrite\n"));
    356 	reqh = usbd_alloc_request();
    357 	if (reqh == 0)
    358 		return (ENOMEM);
    359 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    360 		ulpt_statusmsg(ulpt_status(sc), sc);
    361 		error = uiomove(buf, n, uio);
    362 		if (error)
    363 			break;
    364 		/* XXX use callback to enable interrupt? */
    365 		r = usbd_setup_request(reqh, sc->sc_bulkpipe, 0, buf, n,
    366 				       0, USBD_NO_TIMEOUT, 0);
    367 		if (r != USBD_NORMAL_COMPLETION) {
    368 			error = EIO;
    369 			break;
    370 		}
    371 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    372 		r = usbd_sync_transfer(reqh);
    373 		if (r != USBD_NORMAL_COMPLETION) {
    374 			DPRINTF(("ulptwrite: error=%d\n", r));
    375 			usbd_clear_endpoint_stall(sc->sc_bulkpipe);
    376 			error = EIO;
    377 			break;
    378 		}
    379 	}
    380 	usbd_free_request(reqh);
    381 	return (error);
    382 }
    383 
    384 int
    385 ulptioctl(dev, cmd, data, flag, p)
    386 	dev_t dev;
    387 	u_long cmd;
    388 	caddr_t data;
    389 	int flag;
    390 	struct proc *p;
    391 {
    392 	int error = 0;
    393 
    394 	switch (cmd) {
    395 	default:
    396 		error = ENODEV;
    397 	}
    398 
    399 	return error;
    400 }
    401 
    402 #if defined(__FreeBSD__)
    403 static int
    404 ulpt_detach(device_t self)
    405 {
    406         struct ulpt_softc *sc = device_get_softc(self);
    407 	char *devinfo = (char *) device_get_desc(self);
    408 
    409 	if (devinfo) {
    410 		device_set_desc(self, NULL);
    411 		free(devinfo, M_USB);
    412 	}
    413 	return 0;
    414 }
    415 
    416 DRIVER_MODULE(ulpt, usb, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    417 #endif
    418