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