Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.59.2.2
      1  1.59.2.2     skrll /*	$NetBSD: ulpt.c,v 1.59.2.2 2004/08/03 10:51:36 skrll Exp $	*/
      2      1.30  augustss /*	$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.59.2.2     skrll  * Copyright (c) 1998, 2003 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.37  augustss  * 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.40  augustss  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
     43       1.1  augustss  */
     44      1.44     lukem 
     45      1.44     lukem #include <sys/cdefs.h>
     46  1.59.2.2     skrll __KERNEL_RCSID(0, "$NetBSD: ulpt.c,v 1.59.2.2 2004/08/03 10:51:36 skrll Exp $");
     47      1.27  augustss 
     48       1.1  augustss #include <sys/param.h>
     49       1.1  augustss #include <sys/systm.h>
     50       1.1  augustss #include <sys/proc.h>
     51       1.1  augustss #include <sys/kernel.h>
     52  1.59.2.2     skrll #include <sys/fcntl.h>
     53      1.35  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
     54       1.8  augustss #include <sys/device.h>
     55       1.1  augustss #include <sys/ioctl.h>
     56       1.8  augustss #elif defined(__FreeBSD__)
     57       1.8  augustss #include <sys/ioccom.h>
     58       1.8  augustss #include <sys/module.h>
     59       1.8  augustss #include <sys/bus.h>
     60       1.8  augustss #endif
     61       1.1  augustss #include <sys/uio.h>
     62       1.1  augustss #include <sys/conf.h>
     63      1.12  augustss #include <sys/vnode.h>
     64       1.1  augustss #include <sys/syslog.h>
     65       1.1  augustss 
     66  1.59.2.2     skrll #include <machine/vmparam.h>	/* PAGE_SIZE */
     67  1.59.2.2     skrll 
     68       1.1  augustss #include <dev/usb/usb.h>
     69       1.1  augustss #include <dev/usb/usbdi.h>
     70       1.1  augustss #include <dev/usb/usbdi_util.h>
     71       1.1  augustss #include <dev/usb/usbdevs.h>
     72       1.1  augustss #include <dev/usb/usb_quirks.h>
     73       1.1  augustss 
     74       1.1  augustss #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     75       1.1  augustss #define	STEP		hz/4
     76       1.1  augustss 
     77       1.1  augustss #define	LPTPRI		(PZERO+8)
     78  1.59.2.2     skrll #define	ULPT_BSIZE	PAGE_SIZE
     79  1.59.2.2     skrll 
     80  1.59.2.2     skrll #define ULPT_READS_PER_SEC 5
     81  1.59.2.2     skrll #define ULPT_READ_TIMO 10
     82       1.1  augustss 
     83      1.27  augustss #ifdef ULPT_DEBUG
     84      1.14  augustss #define DPRINTF(x)	if (ulptdebug) logprintf x
     85      1.14  augustss #define DPRINTFN(n,x)	if (ulptdebug>(n)) logprintf x
     86       1.1  augustss int	ulptdebug = 0;
     87       1.1  augustss #else
     88       1.1  augustss #define DPRINTF(x)
     89       1.1  augustss #define DPRINTFN(n,x)
     90       1.1  augustss #endif
     91       1.1  augustss 
     92       1.1  augustss #define UR_GET_DEVICE_ID 0
     93       1.1  augustss #define UR_GET_PORT_STATUS 1
     94       1.1  augustss #define UR_SOFT_RESET 2
     95       1.1  augustss 
     96       1.1  augustss #define	LPS_NERR		0x08	/* printer no error */
     97       1.1  augustss #define	LPS_SELECT		0x10	/* printer selected */
     98       1.1  augustss #define	LPS_NOPAPER		0x20	/* printer out of paper */
     99       1.1  augustss #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
    100       1.1  augustss #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
    101       1.1  augustss 
    102       1.1  augustss struct ulpt_softc {
    103      1.20  augustss 	USBBASEDEVICE sc_dev;
    104       1.1  augustss 	usbd_device_handle sc_udev;	/* device */
    105       1.1  augustss 	usbd_interface_handle sc_iface;	/* interface */
    106       1.1  augustss 	int sc_ifaceno;
    107      1.42  augustss 
    108      1.42  augustss 	int sc_out;
    109      1.42  augustss 	usbd_pipe_handle sc_out_pipe;	/* bulk out pipe */
    110  1.59.2.2     skrll 	usbd_xfer_handle sc_out_xfer;
    111  1.59.2.2     skrll 	void *sc_out_buf;
    112      1.42  augustss 
    113      1.42  augustss 	int sc_in;
    114      1.42  augustss 	usbd_pipe_handle sc_in_pipe;	/* bulk in pipe */
    115  1.59.2.2     skrll 	usbd_xfer_handle sc_in_xfer;
    116  1.59.2.2     skrll 	void *sc_in_buf;
    117  1.59.2.2     skrll 
    118  1.59.2.2     skrll 	usb_callout_t sc_read_callout;
    119  1.59.2.2     skrll 	int sc_has_callout;
    120       1.1  augustss 
    121       1.1  augustss 	u_char sc_state;
    122       1.1  augustss #define	ULPT_OPEN	0x01	/* device is open */
    123       1.1  augustss #define	ULPT_OBUSY	0x02	/* printer is busy doing output */
    124       1.1  augustss #define	ULPT_INIT	0x04	/* waiting to initialize for open */
    125       1.1  augustss 	u_char sc_flags;
    126       1.1  augustss #define	ULPT_NOPRIME	0x40	/* don't prime on open */
    127       1.1  augustss 	u_char sc_laststatus;
    128      1.12  augustss 
    129      1.12  augustss 	int sc_refcnt;
    130      1.12  augustss 	u_char sc_dying;
    131      1.27  augustss 
    132      1.27  augustss #if defined(__FreeBSD__)
    133      1.27  augustss 	dev_t dev;
    134      1.27  augustss 	dev_t dev_noprime;
    135      1.27  augustss #endif
    136       1.1  augustss };
    137       1.1  augustss 
    138      1.52   gehenna #if defined(__NetBSD__)
    139      1.52   gehenna dev_type_open(ulptopen);
    140      1.52   gehenna dev_type_close(ulptclose);
    141      1.52   gehenna dev_type_write(ulptwrite);
    142  1.59.2.2     skrll dev_type_read(ulptread);
    143      1.52   gehenna dev_type_ioctl(ulptioctl);
    144      1.52   gehenna 
    145      1.52   gehenna const struct cdevsw ulpt_cdevsw = {
    146  1.59.2.2     skrll 	ulptopen, ulptclose, ulptread, ulptwrite, ulptioctl,
    147      1.55  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
    148      1.52   gehenna };
    149      1.52   gehenna #elif defined(__OpenBSD__)
    150      1.28  augustss cdev_decl(ulpt);
    151      1.27  augustss #elif defined(__FreeBSD__)
    152      1.34  augustss Static d_open_t ulptopen;
    153      1.34  augustss Static d_close_t ulptclose;
    154      1.34  augustss Static d_write_t ulptwrite;
    155      1.34  augustss Static d_ioctl_t ulptioctl;
    156      1.27  augustss 
    157      1.27  augustss #define ULPT_CDEV_MAJOR 113
    158      1.27  augustss 
    159      1.34  augustss Static struct cdevsw ulpt_cdevsw = {
    160      1.27  augustss 	/* open */	ulptopen,
    161      1.27  augustss 	/* close */	ulptclose,
    162      1.27  augustss 	/* read */	noread,
    163      1.27  augustss 	/* write */	ulptwrite,
    164      1.27  augustss 	/* ioctl */	ulptioctl,
    165      1.27  augustss 	/* poll */	nopoll,
    166      1.27  augustss 	/* mmap */	nommap,
    167      1.27  augustss 	/* strategy */	nostrategy,
    168      1.27  augustss 	/* name */	"ulpt",
    169      1.27  augustss 	/* maj */	ULPT_CDEV_MAJOR,
    170      1.27  augustss 	/* dump */	nodump,
    171      1.27  augustss 	/* psize */	nopsize,
    172      1.27  augustss 	/* flags */	0,
    173      1.47  augustss #if !defined(__FreeBSD__) || (__FreeBSD__ < 5)
    174      1.27  augustss 	/* bmaj */	-1
    175      1.47  augustss #endif
    176      1.27  augustss };
    177      1.27  augustss #endif
    178      1.27  augustss 
    179      1.38  augustss void ulpt_disco(void *);
    180       1.1  augustss 
    181      1.38  augustss int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
    182  1.59.2.2     skrll int ulpt_do_read(struct ulpt_softc *, struct uio *uio, int);
    183      1.38  augustss int ulpt_status(struct ulpt_softc *);
    184      1.38  augustss void ulpt_reset(struct ulpt_softc *);
    185      1.38  augustss int ulpt_statusmsg(u_char, struct ulpt_softc *);
    186  1.59.2.2     skrll void ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    187  1.59.2.2     skrll 		  usbd_status status);
    188  1.59.2.2     skrll void ulpt_tick(void *xsc);
    189       1.1  augustss 
    190      1.42  augustss #if 0
    191      1.38  augustss void ieee1284_print_id(char *);
    192      1.42  augustss #endif
    193      1.16  augustss 
    194       1.1  augustss #define	ULPTUNIT(s)	(minor(s) & 0x1f)
    195       1.1  augustss #define	ULPTFLAGS(s)	(minor(s) & 0xe0)
    196       1.1  augustss 
    197      1.27  augustss 
    198       1.8  augustss USB_DECLARE_DRIVER(ulpt);
    199       1.1  augustss 
    200       1.8  augustss USB_MATCH(ulpt)
    201       1.1  augustss {
    202       1.8  augustss 	USB_MATCH_START(ulpt, uaa);
    203       1.1  augustss 	usb_interface_descriptor_t *id;
    204      1.50  augustss 
    205       1.1  augustss 	DPRINTFN(10,("ulpt_match\n"));
    206      1.28  augustss 	if (uaa->iface == NULL)
    207       1.1  augustss 		return (UMATCH_NONE);
    208       1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    209      1.28  augustss 	if (id != NULL &&
    210      1.32  augustss 	    id->bInterfaceClass == UICLASS_PRINTER &&
    211      1.32  augustss 	    id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
    212      1.32  augustss 	    (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
    213      1.43   nathanw 	     id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
    214      1.43   nathanw 	     id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
    215       1.1  augustss 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    216       1.1  augustss 	return (UMATCH_NONE);
    217       1.1  augustss }
    218       1.1  augustss 
    219       1.8  augustss USB_ATTACH(ulpt)
    220       1.1  augustss {
    221       1.8  augustss 	USB_ATTACH_START(ulpt, sc, uaa);
    222       1.1  augustss 	usbd_device_handle dev = uaa->device;
    223       1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    224      1.42  augustss 	usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
    225      1.42  augustss 	usb_interface_descriptor_t *id, *iend;
    226      1.42  augustss 	usb_config_descriptor_t *cdesc;
    227      1.42  augustss 	usbd_status err;
    228       1.1  augustss 	char devinfo[1024];
    229       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    230      1.42  augustss 	u_int8_t epcount;
    231      1.42  augustss 	int i, altno;
    232      1.50  augustss 
    233       1.8  augustss 	DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
    234  1.59.2.2     skrll 	usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
    235       1.8  augustss 	USB_ATTACH_SETUP;
    236       1.8  augustss 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    237      1.42  augustss 	       devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
    238       1.1  augustss 
    239      1.50  augustss 	/* XXX
    240      1.42  augustss 	 * Stepping through the alternate settings needs to be abstracted out.
    241      1.42  augustss 	 */
    242      1.42  augustss 	cdesc = usbd_get_config_descriptor(dev);
    243      1.42  augustss 	if (cdesc == NULL) {
    244      1.42  augustss 		printf("%s: failed to get configuration descriptor\n",
    245      1.42  augustss 		       USBDEVNAME(sc->sc_dev));
    246      1.42  augustss 		USB_ATTACH_ERROR_RETURN;
    247      1.42  augustss 	}
    248      1.42  augustss 	iend = (usb_interface_descriptor_t *)
    249      1.42  augustss 		   ((char *)cdesc + UGETW(cdesc->wTotalLength));
    250      1.42  augustss #ifdef DIAGNOSTIC
    251      1.42  augustss 	if (ifcd < (usb_interface_descriptor_t *)cdesc ||
    252      1.42  augustss 	    ifcd >= iend)
    253      1.54    provos 		panic("ulpt: iface desc out of range");
    254      1.42  augustss #endif
    255  1.59.2.2     skrll 
    256      1.42  augustss 	/* Step through all the descriptors looking for bidir mode */
    257      1.42  augustss 	for (id = ifcd, altno = 0;
    258      1.42  augustss 	     id < iend;
    259      1.42  augustss 	     id = (void *)((char *)id + id->bLength)) {
    260      1.42  augustss 		if (id->bDescriptorType == UDESC_INTERFACE &&
    261      1.42  augustss 		    id->bInterfaceNumber == ifcd->bInterfaceNumber) {
    262      1.42  augustss 			if (id->bInterfaceClass == UICLASS_PRINTER &&
    263      1.42  augustss 			    id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
    264      1.56  augustss 			    (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*||
    265      1.56  augustss 			     id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/))
    266      1.42  augustss 				goto found;
    267      1.42  augustss 			altno++;
    268      1.42  augustss 		}
    269      1.42  augustss 	}
    270      1.42  augustss 	id = ifcd;		/* not found, use original */
    271      1.42  augustss  found:
    272      1.42  augustss 	if (id != ifcd) {
    273      1.42  augustss 		/* Found a new bidir setting */
    274      1.42  augustss 		DPRINTF(("ulpt_attach: set altno = %d\n", altno));
    275      1.42  augustss 		err = usbd_set_interface(iface, altno);
    276      1.42  augustss 		if (err) {
    277      1.42  augustss 			printf("%s: setting alternate interface failed\n",
    278      1.42  augustss 			       USBDEVNAME(sc->sc_dev));
    279      1.42  augustss 			sc->sc_dying = 1;
    280      1.42  augustss 			USB_ATTACH_ERROR_RETURN;
    281      1.42  augustss 		}
    282       1.1  augustss 	}
    283       1.1  augustss 
    284      1.42  augustss 	epcount = 0;
    285      1.42  augustss 	(void)usbd_endpoint_count(iface, &epcount);
    286      1.42  augustss 
    287      1.42  augustss 	sc->sc_in = -1;
    288      1.42  augustss 	sc->sc_out = -1;
    289      1.42  augustss 	for (i = 0; i < epcount; i++) {
    290      1.42  augustss 		ed = usbd_interface2endpoint_descriptor(iface, i);
    291      1.42  augustss 		if (ed == NULL) {
    292      1.42  augustss 			printf("%s: couldn't get ep %d\n",
    293      1.42  augustss 			    USBDEVNAME(sc->sc_dev), i);
    294      1.42  augustss 			USB_ATTACH_ERROR_RETURN;
    295      1.42  augustss 		}
    296      1.42  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    297      1.42  augustss 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    298      1.42  augustss 			sc->sc_in = ed->bEndpointAddress;
    299      1.42  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    300      1.42  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    301      1.42  augustss 			sc->sc_out = ed->bEndpointAddress;
    302      1.42  augustss 		}
    303      1.42  augustss 	}
    304      1.42  augustss 	if (sc->sc_out == -1) {
    305      1.51  augustss 		printf("%s: could not find bulk out endpoint\n",
    306      1.42  augustss 		    USBDEVNAME(sc->sc_dev));
    307      1.12  augustss 		sc->sc_dying = 1;
    308       1.8  augustss 		USB_ATTACH_ERROR_RETURN;
    309      1.12  augustss 	}
    310      1.42  augustss 
    311      1.42  augustss 	if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
    312      1.42  augustss 		/* This device doesn't handle reading properly. */
    313      1.42  augustss 		sc->sc_in = -1;
    314      1.42  augustss 	}
    315      1.51  augustss 
    316      1.51  augustss 	printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
    317      1.51  augustss 	       sc->sc_in >= 0 ? "bi" : "uni");
    318      1.42  augustss 
    319      1.42  augustss 	DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
    320      1.42  augustss 
    321      1.42  augustss 	sc->sc_iface = iface;
    322       1.1  augustss 	sc->sc_ifaceno = id->bInterfaceNumber;
    323      1.42  augustss 	sc->sc_udev = dev;
    324       1.1  augustss 
    325      1.22  augustss #if 0
    326      1.22  augustss /*
    327      1.36  augustss  * This code is disabled because for some mysterious reason it causes
    328      1.22  augustss  * printing not to work.  But only sometimes, and mostly with
    329      1.22  augustss  * UHCI and less often with OHCI.  *sigh*
    330      1.22  augustss  */
    331      1.22  augustss 	{
    332      1.22  augustss 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    333      1.22  augustss 	usb_device_request_t req;
    334      1.22  augustss 	int len, alen;
    335      1.22  augustss 
    336       1.1  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    337       1.1  augustss 	req.bRequest = UR_GET_DEVICE_ID;
    338       1.1  augustss 	USETW(req.wValue, cd->bConfigurationValue);
    339       1.1  augustss 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    340       1.1  augustss 	USETW(req.wLength, sizeof devinfo - 1);
    341      1.47  augustss 	err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
    342      1.48  augustss 		  &alen, USBD_DEFAULT_TIMEOUT);
    343      1.28  augustss 	if (err) {
    344      1.16  augustss 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
    345      1.16  augustss 	} else if (alen <= 2) {
    346      1.16  augustss 		printf("%s: empty device id, no printer connected?\n",
    347      1.16  augustss 		       USBDEVNAME(sc->sc_dev));
    348      1.16  augustss 	} else {
    349       1.1  augustss 		/* devinfo now contains an IEEE-1284 device ID */
    350      1.16  augustss 		len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
    351      1.16  augustss 		if (len > sizeof devinfo - 3)
    352      1.16  augustss 			len = sizeof devinfo - 3;
    353      1.16  augustss 		devinfo[len] = 0;
    354      1.16  augustss 		printf("%s: device id <", USBDEVNAME(sc->sc_dev));
    355      1.16  augustss 		ieee1284_print_id(devinfo+2);
    356      1.16  augustss 		printf(">\n");
    357       1.1  augustss 	}
    358      1.22  augustss 	}
    359      1.22  augustss #endif
    360       1.1  augustss 
    361      1.27  augustss #if defined(__FreeBSD__)
    362      1.27  augustss 	sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
    363      1.27  augustss 		UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
    364      1.27  augustss 	sc->dev_noprime = make_dev(&ulpt_cdevsw,
    365      1.27  augustss 		device_get_unit(self)|ULPT_NOPRIME,
    366      1.27  augustss 		UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
    367      1.27  augustss #endif
    368      1.27  augustss 
    369      1.31  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    370      1.31  augustss 			   USBDEV(sc->sc_dev));
    371      1.31  augustss 
    372       1.8  augustss 	USB_ATTACH_SUCCESS_RETURN;
    373       1.1  augustss }
    374       1.1  augustss 
    375      1.27  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    376       1.1  augustss int
    377      1.38  augustss ulpt_activate(device_ptr_t self, enum devact act)
    378      1.12  augustss {
    379      1.17  augustss 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    380      1.17  augustss 
    381      1.17  augustss 	switch (act) {
    382      1.17  augustss 	case DVACT_ACTIVATE:
    383      1.17  augustss 		return (EOPNOTSUPP);
    384      1.17  augustss 
    385      1.17  augustss 	case DVACT_DEACTIVATE:
    386      1.17  augustss 		sc->sc_dying = 1;
    387      1.17  augustss 		break;
    388      1.17  augustss 	}
    389      1.12  augustss 	return (0);
    390      1.12  augustss }
    391      1.27  augustss #endif
    392      1.12  augustss 
    393      1.27  augustss USB_DETACH(ulpt)
    394      1.12  augustss {
    395      1.27  augustss 	USB_DETACH_START(ulpt, sc);
    396      1.27  augustss 	int s;
    397      1.27  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    398      1.12  augustss 	int maj, mn;
    399      1.47  augustss #elif defined(__FreeBSD__)
    400      1.47  augustss 	struct vnode *vp;
    401      1.47  augustss #endif
    402      1.12  augustss 
    403      1.27  augustss 	DPRINTF(("ulpt_detach: sc=%p\n", sc));
    404      1.12  augustss 
    405      1.12  augustss 	sc->sc_dying = 1;
    406      1.42  augustss 	if (sc->sc_out_pipe != NULL)
    407      1.42  augustss 		usbd_abort_pipe(sc->sc_out_pipe);
    408      1.42  augustss 	if (sc->sc_in_pipe != NULL)
    409      1.42  augustss 		usbd_abort_pipe(sc->sc_in_pipe);
    410      1.12  augustss 
    411      1.12  augustss 	s = splusb();
    412      1.12  augustss 	if (--sc->sc_refcnt >= 0) {
    413      1.12  augustss 		/* There is noone to wake, aborting the pipe is enough */
    414      1.12  augustss 		/* Wait for processes to go away. */
    415      1.20  augustss 		usb_detach_wait(USBDEV(sc->sc_dev));
    416      1.12  augustss 	}
    417      1.12  augustss 	splx(s);
    418      1.12  augustss 
    419      1.27  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    420      1.12  augustss 	/* locate the major number */
    421      1.52   gehenna #if defined(__NetBSD__)
    422      1.52   gehenna 	maj = cdevsw_lookup_major(&ulpt_cdevsw);
    423      1.52   gehenna #elif defined(__OpenBSD__)
    424      1.12  augustss 	for (maj = 0; maj < nchrdev; maj++)
    425      1.12  augustss 		if (cdevsw[maj].d_open == ulptopen)
    426      1.12  augustss 			break;
    427      1.52   gehenna #endif
    428      1.12  augustss 
    429      1.12  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    430      1.12  augustss 	mn = self->dv_unit;
    431      1.12  augustss 	vdevgone(maj, mn, mn, VCHR);
    432      1.57       scw 	vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
    433      1.27  augustss #elif defined(__FreeBSD__)
    434      1.47  augustss 	vp = SLIST_FIRST(&sc->dev->si_hlist);
    435      1.47  augustss 	if (vp)
    436      1.47  augustss 		VOP_REVOKE(vp, REVOKEALL);
    437      1.47  augustss 	vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
    438      1.47  augustss 	if (vp)
    439      1.47  augustss 		VOP_REVOKE(vp, REVOKEALL);
    440      1.27  augustss 
    441      1.30  augustss 	destroy_dev(sc->dev);
    442      1.30  augustss 	destroy_dev(sc->dev_noprime);
    443      1.27  augustss #endif
    444      1.31  augustss 
    445      1.31  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    446      1.31  augustss 			   USBDEV(sc->sc_dev));
    447      1.12  augustss 
    448      1.12  augustss 	return (0);
    449      1.12  augustss }
    450      1.12  augustss 
    451      1.12  augustss int
    452      1.38  augustss ulpt_status(struct ulpt_softc *sc)
    453       1.1  augustss {
    454       1.1  augustss 	usb_device_request_t req;
    455      1.28  augustss 	usbd_status err;
    456       1.1  augustss 	u_char status;
    457       1.1  augustss 
    458       1.1  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    459       1.1  augustss 	req.bRequest = UR_GET_PORT_STATUS;
    460       1.1  augustss 	USETW(req.wValue, 0);
    461       1.1  augustss 	USETW(req.wIndex, sc->sc_ifaceno);
    462       1.1  augustss 	USETW(req.wLength, 1);
    463      1.28  augustss 	err = usbd_do_request(sc->sc_udev, &req, &status);
    464      1.30  augustss 	DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
    465      1.28  augustss 	if (!err)
    466       1.1  augustss 		return (status);
    467       1.1  augustss 	else
    468       1.1  augustss 		return (0);
    469       1.1  augustss }
    470       1.1  augustss 
    471       1.1  augustss void
    472      1.38  augustss ulpt_reset(struct ulpt_softc *sc)
    473       1.1  augustss {
    474       1.1  augustss 	usb_device_request_t req;
    475       1.1  augustss 
    476       1.1  augustss 	DPRINTFN(1, ("ulpt_reset\n"));
    477       1.1  augustss 	req.bRequest = UR_SOFT_RESET;
    478       1.1  augustss 	USETW(req.wValue, 0);
    479       1.1  augustss 	USETW(req.wIndex, sc->sc_ifaceno);
    480       1.1  augustss 	USETW(req.wLength, 0);
    481      1.41  augustss 
    482      1.41  augustss 	/*
    483      1.41  augustss 	 * There was a mistake in the USB printer 1.0 spec that gave the
    484      1.42  augustss 	 * request type as UT_WRITE_CLASS_OTHER; it should have been
    485      1.41  augustss 	 * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
    486      1.41  augustss 	 * so we try both.
    487      1.41  augustss 	 */
    488      1.49  augustss 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    489      1.42  augustss 	if (usbd_do_request(sc->sc_udev, &req, 0)) {	/* 1.0 */
    490      1.41  augustss 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    491      1.42  augustss 		(void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
    492      1.41  augustss 	}
    493       1.1  augustss }
    494       1.1  augustss 
    495  1.59.2.2     skrll #if 0
    496      1.42  augustss static void
    497      1.42  augustss ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    498      1.42  augustss {
    499      1.42  augustss 	struct ulpt_softc *sc = priv;
    500      1.42  augustss 
    501      1.42  augustss 	DPRINTFN(2,("ulpt_input: got some data\n"));
    502      1.42  augustss 	/* Do it again. */
    503      1.42  augustss 	if (xfer == sc->sc_in_xfer1)
    504      1.42  augustss 		usbd_transfer(sc->sc_in_xfer2);
    505      1.42  augustss 	else
    506      1.42  augustss 		usbd_transfer(sc->sc_in_xfer1);
    507      1.42  augustss }
    508  1.59.2.2     skrll #endif
    509      1.42  augustss 
    510      1.42  augustss int ulptusein = 1;
    511      1.42  augustss 
    512       1.1  augustss /*
    513       1.1  augustss  * Reset the printer, then wait until it's selected and not busy.
    514       1.1  augustss  */
    515       1.1  augustss int
    516  1.59.2.1   darrenr ulptopen(dev_t dev, int flag, int mode, struct lwp *l)
    517       1.1  augustss {
    518       1.1  augustss 	u_char flags = ULPTFLAGS(dev);
    519      1.26  augustss 	struct ulpt_softc *sc;
    520      1.28  augustss 	usbd_status err;
    521       1.1  augustss 	int spin, error;
    522      1.26  augustss 
    523       1.8  augustss 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    524       1.1  augustss 
    525      1.30  augustss 	if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
    526      1.13  augustss 		return (ENXIO);
    527       1.1  augustss 
    528       1.1  augustss 	if (sc->sc_state)
    529      1.13  augustss 		return (EBUSY);
    530       1.1  augustss 
    531       1.1  augustss 	sc->sc_state = ULPT_INIT;
    532       1.1  augustss 	sc->sc_flags = flags;
    533       1.1  augustss 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    534       1.1  augustss 
    535      1.27  augustss #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
    536      1.27  augustss 	/* Ignoring these flags might not be a good idea */
    537      1.27  augustss 	if ((flags & ~ULPT_NOPRIME) != 0)
    538      1.27  augustss 		printf("ulptopen: flags ignored: %b\n", flags,
    539      1.27  augustss 			"\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
    540      1.27  augustss #endif
    541      1.27  augustss 
    542      1.27  augustss 
    543      1.39  augustss 	error = 0;
    544      1.39  augustss 	sc->sc_refcnt++;
    545      1.39  augustss 
    546  1.59.2.2     skrll #if 0 /* XXX causes some printers to disconnect */
    547       1.1  augustss 	if ((flags & ULPT_NOPRIME) == 0)
    548       1.1  augustss 		ulpt_reset(sc);
    549  1.59.2.2     skrll #endif
    550       1.1  augustss 
    551       1.1  augustss 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    552      1.42  augustss 		DPRINTF(("ulpt_open: waiting a while\n"));
    553       1.1  augustss 		if (spin >= TIMEOUT) {
    554      1.39  augustss 			error = EBUSY;
    555       1.1  augustss 			sc->sc_state = 0;
    556      1.39  augustss 			goto done;
    557       1.1  augustss 		}
    558       1.1  augustss 
    559       1.1  augustss 		/* wait 1/4 second, give up if we get a signal */
    560       1.1  augustss 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    561       1.1  augustss 		if (error != EWOULDBLOCK) {
    562       1.1  augustss 			sc->sc_state = 0;
    563      1.39  augustss 			goto done;
    564      1.39  augustss 		}
    565      1.39  augustss 
    566      1.39  augustss 		if (sc->sc_dying) {
    567      1.39  augustss 			error = ENXIO;
    568      1.39  augustss 			sc->sc_state = 0;
    569      1.39  augustss 			goto done;
    570       1.1  augustss 		}
    571       1.1  augustss 	}
    572       1.1  augustss 
    573      1.42  augustss 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
    574      1.28  augustss 	if (err) {
    575      1.39  augustss 		error = EIO;
    576  1.59.2.2     skrll 		goto err0;
    577       1.1  augustss 	}
    578  1.59.2.2     skrll 	sc->sc_out_xfer = usbd_alloc_xfer(sc->sc_udev);
    579  1.59.2.2     skrll 	if (sc->sc_out_xfer == NULL) {
    580  1.59.2.2     skrll 		error = ENOMEM;
    581  1.59.2.2     skrll 		goto err1;
    582  1.59.2.2     skrll 	}
    583  1.59.2.2     skrll 	sc->sc_out_buf = usbd_alloc_buffer(sc->sc_out_xfer, ULPT_BSIZE);
    584  1.59.2.2     skrll 	if (sc->sc_out_buf == NULL) {
    585  1.59.2.2     skrll 		error = ENOMEM;
    586  1.59.2.2     skrll 		goto err2;
    587  1.59.2.2     skrll 	}
    588  1.59.2.2     skrll 
    589      1.42  augustss 	if (ulptusein && sc->sc_in != -1) {
    590      1.42  augustss 		DPRINTF(("ulpt_open: open input pipe\n"));
    591      1.42  augustss 		err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
    592      1.42  augustss 		if (err) {
    593      1.42  augustss 			error = EIO;
    594  1.59.2.2     skrll 			goto err2;
    595      1.42  augustss 		}
    596  1.59.2.2     skrll 		sc->sc_in_xfer = usbd_alloc_xfer(sc->sc_udev);
    597  1.59.2.2     skrll 		if (sc->sc_in_xfer == NULL) {
    598      1.42  augustss 			error = ENOMEM;
    599  1.59.2.2     skrll 			goto err3;
    600  1.59.2.2     skrll 		}
    601  1.59.2.2     skrll 		sc->sc_in_buf = usbd_alloc_buffer(sc->sc_in_xfer, ULPT_BSIZE);
    602  1.59.2.2     skrll 		if (sc->sc_in_buf == NULL) {
    603  1.59.2.2     skrll 			error = ENOMEM;
    604  1.59.2.2     skrll 			goto err4;
    605  1.59.2.2     skrll 		}
    606  1.59.2.2     skrll 
    607  1.59.2.2     skrll 		/* If it's not opened for read the set up a reader. */
    608  1.59.2.2     skrll 		if (!(flags & FREAD)) {
    609  1.59.2.2     skrll 			DPRINTF(("ulpt_open: start read callout\n"));
    610  1.59.2.2     skrll 			usb_callout_init(sc->sc_read_callout);
    611  1.59.2.2     skrll 			usb_callout(sc->sc_read_callout, hz/5, ulpt_tick, sc);
    612  1.59.2.2     skrll 			sc->sc_has_callout = 1;
    613      1.42  augustss 		}
    614      1.42  augustss 	}
    615       1.1  augustss 
    616       1.1  augustss 	sc->sc_state = ULPT_OPEN;
    617  1.59.2.2     skrll 	goto done;
    618  1.59.2.2     skrll 
    619  1.59.2.2     skrll  err4:
    620  1.59.2.2     skrll 	usbd_free_xfer(sc->sc_in_xfer);
    621  1.59.2.2     skrll 	sc->sc_in_xfer = NULL;
    622  1.59.2.2     skrll  err3:
    623  1.59.2.2     skrll 	usbd_close_pipe(sc->sc_in_pipe);
    624  1.59.2.2     skrll 	sc->sc_in_pipe = NULL;
    625  1.59.2.2     skrll  err2:
    626  1.59.2.2     skrll 	usbd_free_xfer(sc->sc_out_xfer);
    627  1.59.2.2     skrll 	sc->sc_out_xfer = NULL;
    628  1.59.2.2     skrll  err1:
    629  1.59.2.2     skrll 	usbd_close_pipe(sc->sc_out_pipe);
    630  1.59.2.2     skrll 	sc->sc_out_pipe = NULL;
    631  1.59.2.2     skrll  err0:
    632  1.59.2.2     skrll 	sc->sc_state = 0;
    633       1.1  augustss 
    634      1.39  augustss  done:
    635      1.39  augustss 	if (--sc->sc_refcnt < 0)
    636      1.39  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    637      1.39  augustss 
    638      1.39  augustss 	DPRINTF(("ulptopen: done, error=%d\n", error));
    639      1.39  augustss 	return (error);
    640       1.1  augustss }
    641       1.1  augustss 
    642       1.1  augustss int
    643      1.38  augustss ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
    644       1.1  augustss {
    645       1.1  augustss 	u_char new;
    646       1.1  augustss 
    647       1.1  augustss 	status = (status ^ LPS_INVERT) & LPS_MASK;
    648       1.1  augustss 	new = status & ~sc->sc_laststatus;
    649       1.1  augustss 	sc->sc_laststatus = status;
    650       1.1  augustss 
    651       1.1  augustss 	if (new & LPS_SELECT)
    652       1.8  augustss 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    653       1.1  augustss 	else if (new & LPS_NOPAPER)
    654       1.8  augustss 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    655       1.1  augustss 	else if (new & LPS_NERR)
    656       1.8  augustss 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    657       1.1  augustss 
    658      1.13  augustss 	return (status);
    659       1.1  augustss }
    660       1.1  augustss 
    661       1.1  augustss int
    662  1.59.2.1   darrenr ulptclose(dev_t dev, int flag, int mode, struct lwp *l)
    663       1.1  augustss {
    664      1.26  augustss 	struct ulpt_softc *sc;
    665      1.26  augustss 
    666       1.8  augustss 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    667      1.13  augustss 
    668      1.13  augustss 	if (sc->sc_state != ULPT_OPEN)
    669      1.13  augustss 		/* We are being forced to close before the open completed. */
    670      1.13  augustss 		return (0);
    671       1.1  augustss 
    672  1.59.2.2     skrll 	if (sc->sc_has_callout) {
    673  1.59.2.2     skrll 		usb_uncallout(sc->sc_read_callout, ulpt_tick, sc);
    674  1.59.2.2     skrll 		sc->sc_has_callout = 0;
    675  1.59.2.2     skrll 	}
    676  1.59.2.2     skrll 
    677      1.42  augustss 	if (sc->sc_out_pipe != NULL) {
    678  1.59.2.2     skrll 		usbd_abort_pipe(sc->sc_out_pipe);
    679      1.42  augustss 		usbd_close_pipe(sc->sc_out_pipe);
    680      1.42  augustss 		sc->sc_out_pipe = NULL;
    681      1.42  augustss 	}
    682  1.59.2.2     skrll 	if (sc->sc_out_xfer != NULL) {
    683  1.59.2.2     skrll 		usbd_free_xfer(sc->sc_out_xfer);
    684  1.59.2.2     skrll 		sc->sc_out_xfer = NULL;
    685  1.59.2.2     skrll 	}
    686  1.59.2.2     skrll 
    687      1.42  augustss 	if (sc->sc_in_pipe != NULL) {
    688      1.42  augustss 		usbd_abort_pipe(sc->sc_in_pipe);
    689      1.42  augustss 		usbd_close_pipe(sc->sc_in_pipe);
    690      1.42  augustss 		sc->sc_in_pipe = NULL;
    691  1.59.2.2     skrll 	}
    692  1.59.2.2     skrll 	if (sc->sc_in_xfer != NULL) {
    693  1.59.2.2     skrll 		usbd_free_xfer(sc->sc_in_xfer);
    694  1.59.2.2     skrll 		sc->sc_in_xfer = NULL;
    695      1.42  augustss 	}
    696       1.1  augustss 
    697       1.1  augustss 	sc->sc_state = 0;
    698       1.1  augustss 
    699       1.1  augustss 	DPRINTF(("ulptclose: closed\n"));
    700       1.1  augustss 	return (0);
    701       1.1  augustss }
    702       1.1  augustss 
    703       1.1  augustss int
    704      1.38  augustss ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
    705       1.1  augustss {
    706      1.15  augustss 	u_int32_t n;
    707       1.1  augustss 	int error = 0;
    708      1.23  augustss 	void *bufp;
    709      1.28  augustss 	usbd_xfer_handle xfer;
    710      1.28  augustss 	usbd_status err;
    711       1.1  augustss 
    712       1.1  augustss 	DPRINTF(("ulptwrite\n"));
    713  1.59.2.2     skrll 	xfer = sc->sc_out_xfer;
    714  1.59.2.2     skrll 	bufp = sc->sc_out_buf;
    715       1.1  augustss 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    716       1.1  augustss 		ulpt_statusmsg(ulpt_status(sc), sc);
    717      1.23  augustss 		error = uiomove(bufp, n, uio);
    718       1.6  augustss 		if (error)
    719       1.6  augustss 			break;
    720       1.1  augustss 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    721      1.50  augustss 		err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
    722      1.28  augustss 			  USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
    723      1.28  augustss 		if (err) {
    724      1.28  augustss 			DPRINTF(("ulptwrite: error=%d\n", err));
    725       1.1  augustss 			error = EIO;
    726       1.1  augustss 			break;
    727       1.1  augustss 		}
    728       1.1  augustss 	}
    729      1.12  augustss 
    730      1.12  augustss 	return (error);
    731      1.12  augustss }
    732      1.12  augustss 
    733      1.12  augustss int
    734      1.38  augustss ulptwrite(dev_t dev, struct uio *uio, int flags)
    735      1.12  augustss {
    736      1.26  augustss 	struct ulpt_softc *sc;
    737      1.26  augustss 	int error;
    738      1.26  augustss 
    739      1.12  augustss 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    740      1.21  augustss 
    741      1.21  augustss 	if (sc->sc_dying)
    742      1.21  augustss 		return (EIO);
    743      1.12  augustss 
    744      1.12  augustss 	sc->sc_refcnt++;
    745      1.12  augustss 	error = ulpt_do_write(sc, uio, flags);
    746      1.12  augustss 	if (--sc->sc_refcnt < 0)
    747      1.20  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    748       1.1  augustss 	return (error);
    749       1.1  augustss }
    750       1.1  augustss 
    751       1.1  augustss int
    752  1.59.2.2     skrll ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags)
    753  1.59.2.2     skrll {
    754  1.59.2.2     skrll 	u_int32_t n, on;
    755  1.59.2.2     skrll 	int error = 0;
    756  1.59.2.2     skrll 	void *bufp;
    757  1.59.2.2     skrll 	usbd_xfer_handle xfer;
    758  1.59.2.2     skrll 	usbd_status err;
    759  1.59.2.2     skrll 
    760  1.59.2.2     skrll 	DPRINTF(("ulptread\n"));
    761  1.59.2.2     skrll 
    762  1.59.2.2     skrll 	if (sc->sc_in_pipe == NULL)
    763  1.59.2.2     skrll 		return 0;
    764  1.59.2.2     skrll 
    765  1.59.2.2     skrll 	xfer = sc->sc_in_xfer;
    766  1.59.2.2     skrll 	bufp = sc->sc_in_buf;
    767  1.59.2.2     skrll 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    768  1.59.2.2     skrll 		DPRINTFN(1, ("ulptread: transfer %d bytes\n", n));
    769  1.59.2.2     skrll 		on = n;
    770  1.59.2.2     skrll 		err = usbd_bulk_transfer(xfer, sc->sc_in_pipe,
    771  1.59.2.2     skrll 			  USBD_NO_COPY | USBD_SHORT_XFER_OK,
    772  1.59.2.2     skrll 			  USBD_NO_TIMEOUT, bufp, &n, "ulptrd");
    773  1.59.2.2     skrll 		if (err) {
    774  1.59.2.2     skrll 			DPRINTF(("ulptread: error=%d\n", err));
    775  1.59.2.2     skrll 			error = EIO;
    776  1.59.2.2     skrll 			break;
    777  1.59.2.2     skrll 		}
    778  1.59.2.2     skrll 		error = uiomove(bufp, n, uio);
    779  1.59.2.2     skrll 		if (error)
    780  1.59.2.2     skrll 			break;
    781  1.59.2.2     skrll 		if (on != n)
    782  1.59.2.2     skrll 			break;
    783  1.59.2.2     skrll 	}
    784  1.59.2.2     skrll 
    785  1.59.2.2     skrll 	return (error);
    786  1.59.2.2     skrll }
    787  1.59.2.2     skrll 
    788  1.59.2.2     skrll int
    789  1.59.2.2     skrll ulptread(dev_t dev, struct uio *uio, int flags)
    790  1.59.2.2     skrll {
    791  1.59.2.2     skrll 	struct ulpt_softc *sc;
    792  1.59.2.2     skrll 	int error;
    793  1.59.2.2     skrll 
    794  1.59.2.2     skrll 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    795  1.59.2.2     skrll 
    796  1.59.2.2     skrll 	if (sc->sc_dying)
    797  1.59.2.2     skrll 		return (EIO);
    798  1.59.2.2     skrll 
    799  1.59.2.2     skrll 	sc->sc_refcnt++;
    800  1.59.2.2     skrll 	error = ulpt_do_read(sc, uio, flags);
    801  1.59.2.2     skrll 	if (--sc->sc_refcnt < 0)
    802  1.59.2.2     skrll 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    803  1.59.2.2     skrll 	return (error);
    804  1.59.2.2     skrll }
    805  1.59.2.2     skrll 
    806  1.59.2.2     skrll void
    807  1.59.2.2     skrll ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    808  1.59.2.2     skrll 	     usbd_status status)
    809  1.59.2.2     skrll {
    810  1.59.2.2     skrll 	usbd_status err;
    811  1.59.2.2     skrll 	u_int32_t n;
    812  1.59.2.2     skrll 	usbd_private_handle xsc;
    813  1.59.2.2     skrll 	struct ulpt_softc *sc;
    814  1.59.2.2     skrll 
    815  1.59.2.2     skrll 	usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err);
    816  1.59.2.2     skrll 	sc = xsc;
    817  1.59.2.2     skrll 
    818  1.59.2.2     skrll 	DPRINTFN(1,("ulpt_read_cb: start sc=%p, err=%d n=%d\n", sc, err, n));
    819  1.59.2.2     skrll 
    820  1.59.2.2     skrll #ifdef ULPT_DEBUG
    821  1.59.2.2     skrll 	if (!err && n > 0)
    822  1.59.2.2     skrll 		DPRINTF(("ulpt_tick: discarding %d bytes\n", n));
    823  1.59.2.2     skrll #endif
    824  1.59.2.2     skrll 	if (!err || err == USBD_TIMEOUT)
    825  1.59.2.2     skrll 		usb_callout(sc->sc_read_callout, hz / ULPT_READS_PER_SEC,
    826  1.59.2.2     skrll 			    ulpt_tick, sc);
    827  1.59.2.2     skrll }
    828  1.59.2.2     skrll 
    829  1.59.2.2     skrll void
    830  1.59.2.2     skrll ulpt_tick(void *xsc)
    831  1.59.2.2     skrll {
    832  1.59.2.2     skrll 	struct ulpt_softc *sc = xsc;
    833  1.59.2.2     skrll 	usbd_status err;
    834  1.59.2.2     skrll 
    835  1.59.2.2     skrll 	if (sc == NULL || sc->sc_dying)
    836  1.59.2.2     skrll 		return;
    837  1.59.2.2     skrll 
    838  1.59.2.2     skrll 	DPRINTFN(1,("ulpt_tick: start sc=%p\n", sc));
    839  1.59.2.2     skrll 
    840  1.59.2.2     skrll 	usbd_setup_xfer(sc->sc_in_xfer, sc->sc_in_pipe, sc, sc->sc_in_buf,
    841  1.59.2.2     skrll 			ULPT_BSIZE, USBD_NO_COPY | USBD_SHORT_XFER_OK,
    842  1.59.2.2     skrll 			ULPT_READ_TIMO, ulpt_read_cb);
    843  1.59.2.2     skrll 	err = usbd_transfer(sc->sc_in_xfer);
    844  1.59.2.2     skrll 	DPRINTFN(1,("ulpt_tick: err=%d\n", err));
    845  1.59.2.2     skrll }
    846  1.59.2.2     skrll 
    847  1.59.2.2     skrll int
    848  1.59.2.1   darrenr ulptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    849       1.1  augustss {
    850       1.1  augustss 	int error = 0;
    851       1.1  augustss 
    852       1.1  augustss 	switch (cmd) {
    853       1.1  augustss 	default:
    854       1.1  augustss 		error = ENODEV;
    855       1.1  augustss 	}
    856       1.1  augustss 
    857      1.17  augustss 	return (error);
    858      1.16  augustss }
    859      1.16  augustss 
    860      1.22  augustss #if 0
    861      1.16  augustss /* XXX This does not belong here. */
    862      1.16  augustss /*
    863      1.17  augustss  * Print select parts of a IEEE 1284 device ID.
    864      1.16  augustss  */
    865      1.16  augustss void
    866      1.38  augustss ieee1284_print_id(char *str)
    867      1.16  augustss {
    868      1.16  augustss 	char *p, *q;
    869      1.16  augustss 
    870      1.16  augustss 	for (p = str-1; p; p = strchr(p, ';')) {
    871      1.16  augustss 		p++;		/* skip ';' */
    872      1.16  augustss 		if (strncmp(p, "MFG:", 4) == 0 ||
    873      1.16  augustss 		    strncmp(p, "MANUFACTURER:", 14) == 0 ||
    874      1.16  augustss 		    strncmp(p, "MDL:", 4) == 0 ||
    875      1.16  augustss 		    strncmp(p, "MODEL:", 6) == 0) {
    876      1.16  augustss 			q = strchr(p, ';');
    877      1.16  augustss 			if (q)
    878      1.16  augustss 				printf("%.*s", (int)(q - p + 1), p);
    879      1.16  augustss 		}
    880      1.16  augustss 	}
    881       1.1  augustss }
    882      1.22  augustss #endif
    883       1.8  augustss 
    884       1.8  augustss #if defined(__FreeBSD__)
    885      1.30  augustss DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    886       1.8  augustss #endif
    887  1.59.2.2     skrll 
    888  1.59.2.2     skrll 
    889  1.59.2.2     skrll 
    890  1.59.2.2     skrll 
    891  1.59.2.2     skrll 
    892  1.59.2.2     skrll #if 0
    893  1.59.2.2     skrll 		usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
    894  1.59.2.2     skrll 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    895  1.59.2.2     skrll 		    USBD_NO_TIMEOUT, ulpt_input);
    896  1.59.2.2     skrll 		usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
    897  1.59.2.2     skrll 		    sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
    898  1.59.2.2     skrll 		    USBD_NO_TIMEOUT, ulpt_input);
    899  1.59.2.2     skrll 		usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
    900  1.59.2.2     skrll #endif
    901