Home | History | Annotate | Line # | Download | only in usb
ulpt.c revision 1.11.4.2
      1  1.11.4.2   thorpej /*	$NetBSD: ulpt.c,v 1.11.4.2 1999/08/02 22:08:59 thorpej Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*
      4       1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5       1.1  augustss  * All rights reserved.
      6       1.1  augustss  *
      7       1.3  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.3  augustss  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9       1.3  augustss  * Carlstedt Research & Technology.
     10       1.1  augustss  *
     11       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12       1.1  augustss  * modification, are permitted provided that the following conditions
     13       1.1  augustss  * are met:
     14       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19       1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     20       1.1  augustss  *    must display the following acknowledgement:
     21       1.1  augustss  *        This product includes software developed by the NetBSD
     22       1.1  augustss  *        Foundation, Inc. and its contributors.
     23       1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1  augustss  *    contributors may be used to endorse or promote products derived
     25       1.1  augustss  *    from this software without specific prior written permission.
     26       1.1  augustss  *
     27       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38      1.11  augustss  */
     39      1.11  augustss 
     40      1.11  augustss /*
     41      1.11  augustss  * Printer Class spec: http://www.usb.org/developers/data/usbprn10.pdf
     42       1.1  augustss  */
     43       1.1  augustss 
     44       1.1  augustss #include <sys/param.h>
     45       1.1  augustss #include <sys/systm.h>
     46       1.1  augustss #include <sys/proc.h>
     47       1.1  augustss #include <sys/kernel.h>
     48       1.8  augustss #if defined(__NetBSD__)
     49       1.8  augustss #include <sys/device.h>
     50       1.1  augustss #include <sys/ioctl.h>
     51       1.8  augustss #elif defined(__FreeBSD__)
     52       1.8  augustss #include <sys/ioccom.h>
     53       1.8  augustss #include <sys/module.h>
     54       1.8  augustss #include <sys/bus.h>
     55       1.8  augustss #endif
     56       1.1  augustss #include <sys/uio.h>
     57       1.1  augustss #include <sys/conf.h>
     58  1.11.4.1   thorpej #include <sys/vnode.h>
     59       1.1  augustss #include <sys/syslog.h>
     60       1.1  augustss 
     61       1.1  augustss #include <dev/usb/usb.h>
     62       1.1  augustss #include <dev/usb/usbdi.h>
     63       1.1  augustss #include <dev/usb/usbdi_util.h>
     64       1.1  augustss #include <dev/usb/usbdevs.h>
     65       1.1  augustss #include <dev/usb/usb_quirks.h>
     66       1.1  augustss 
     67       1.1  augustss #define	TIMEOUT		hz*16	/* wait up to 16 seconds for a ready */
     68       1.1  augustss #define	STEP		hz/4
     69       1.1  augustss 
     70       1.1  augustss #define	LPTPRI		(PZERO+8)
     71       1.1  augustss #define	ULPT_BSIZE	1024
     72       1.1  augustss 
     73       1.1  augustss #ifdef USB_DEBUG
     74       1.1  augustss #define DPRINTF(x)	if (ulptdebug) printf x
     75       1.1  augustss #define DPRINTFN(n,x)	if (ulptdebug>(n)) printf x
     76       1.1  augustss int	ulptdebug = 0;
     77       1.1  augustss #else
     78       1.1  augustss #define DPRINTF(x)
     79       1.1  augustss #define DPRINTFN(n,x)
     80       1.1  augustss #endif
     81       1.1  augustss 
     82       1.1  augustss #define UR_GET_DEVICE_ID 0
     83       1.1  augustss #define UR_GET_PORT_STATUS 1
     84       1.1  augustss #define UR_SOFT_RESET 2
     85       1.1  augustss 
     86       1.1  augustss #define	LPS_NERR		0x08	/* printer no error */
     87       1.1  augustss #define	LPS_SELECT		0x10	/* printer selected */
     88       1.1  augustss #define	LPS_NOPAPER		0x20	/* printer out of paper */
     89       1.1  augustss #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
     90       1.1  augustss #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
     91       1.1  augustss 
     92       1.1  augustss struct ulpt_softc {
     93       1.8  augustss 	bdevice sc_dev;
     94       1.1  augustss 	usbd_device_handle sc_udev;	/* device */
     95       1.1  augustss 	usbd_interface_handle sc_iface;	/* interface */
     96       1.1  augustss 	int sc_ifaceno;
     97       1.1  augustss 	usbd_pipe_handle sc_bulkpipe;	/* bulk pipe */
     98       1.1  augustss 	int sc_bulk;
     99       1.1  augustss 
    100       1.1  augustss 	u_char sc_state;
    101       1.1  augustss #define	ULPT_OPEN	0x01	/* device is open */
    102       1.1  augustss #define	ULPT_OBUSY	0x02	/* printer is busy doing output */
    103       1.1  augustss #define	ULPT_INIT	0x04	/* waiting to initialize for open */
    104       1.1  augustss 	u_char sc_flags;
    105       1.1  augustss #define	ULPT_NOPRIME	0x40	/* don't prime on open */
    106       1.1  augustss 	u_char sc_laststatus;
    107  1.11.4.1   thorpej 
    108  1.11.4.1   thorpej 	int sc_refcnt;
    109  1.11.4.1   thorpej 	u_char sc_dying;
    110       1.1  augustss };
    111       1.1  augustss 
    112       1.1  augustss int ulptopen __P((dev_t, int, int, struct proc *));
    113       1.1  augustss int ulptclose __P((dev_t, int, int, struct proc *p));
    114       1.1  augustss int ulptwrite __P((dev_t, struct uio *uio, int));
    115       1.1  augustss int ulptioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    116       1.1  augustss void ulpt_disco __P((void *));
    117       1.1  augustss 
    118  1.11.4.1   thorpej int ulpt_do_write __P((struct ulpt_softc *, struct uio *uio, int));
    119       1.1  augustss int ulpt_status __P((struct ulpt_softc *));
    120       1.1  augustss void ulpt_reset __P((struct ulpt_softc *));
    121       1.1  augustss int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
    122       1.1  augustss 
    123       1.1  augustss #define	ULPTUNIT(s)	(minor(s) & 0x1f)
    124       1.1  augustss #define	ULPTFLAGS(s)	(minor(s) & 0xe0)
    125       1.1  augustss 
    126       1.8  augustss USB_DECLARE_DRIVER(ulpt);
    127       1.1  augustss 
    128       1.8  augustss USB_MATCH(ulpt)
    129       1.1  augustss {
    130       1.8  augustss 	USB_MATCH_START(ulpt, uaa);
    131       1.1  augustss 	usb_interface_descriptor_t *id;
    132       1.1  augustss 
    133       1.1  augustss 	DPRINTFN(10,("ulpt_match\n"));
    134       1.1  augustss 	if (!uaa->iface)
    135       1.1  augustss 		return (UMATCH_NONE);
    136       1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    137       1.7  augustss 	if (id &&
    138       1.7  augustss 	    id->bInterfaceClass == UCLASS_PRINTER &&
    139       1.1  augustss 	    id->bInterfaceSubClass == USUBCLASS_PRINTER &&
    140       1.1  augustss 	    (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
    141       1.1  augustss 	     id->bInterfaceProtocol == UPROTO_PRINTER_BI))
    142       1.1  augustss 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    143       1.1  augustss 	return (UMATCH_NONE);
    144       1.1  augustss }
    145       1.1  augustss 
    146       1.8  augustss USB_ATTACH(ulpt)
    147       1.1  augustss {
    148       1.8  augustss 	USB_ATTACH_START(ulpt, sc, uaa);
    149       1.1  augustss 	usbd_device_handle dev = uaa->device;
    150       1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    151       1.1  augustss 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    152       1.1  augustss #if 0
    153       1.1  augustss 	usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    154       1.1  augustss 	usb_device_request_t req;
    155       1.1  augustss #endif
    156       1.1  augustss 	char devinfo[1024];
    157       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    158       1.1  augustss 	usbd_status r;
    159       1.1  augustss 
    160       1.8  augustss 	DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
    161       1.2  augustss 	usbd_devinfo(dev, 0, devinfo);
    162       1.8  augustss 	USB_ATTACH_SETUP;
    163       1.8  augustss 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    164       1.4  augustss 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    165       1.1  augustss 
    166       1.1  augustss 	/* Figure out which endpoint is the bulk out endpoint. */
    167       1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    168       1.1  augustss 	if (!ed)
    169       1.1  augustss 		goto nobulk;
    170       1.1  augustss 	if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
    171       1.1  augustss 	    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
    172       1.1  augustss 		/* In case we are using a bidir protocol... */
    173       1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface, 1);
    174       1.1  augustss 		if (!ed)
    175       1.1  augustss 			goto nobulk;
    176       1.1  augustss 		if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
    177       1.1  augustss 		    (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
    178       1.1  augustss 			goto nobulk;
    179       1.1  augustss 	}
    180       1.1  augustss 	sc->sc_bulk = ed->bEndpointAddress;
    181       1.8  augustss 	DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
    182       1.1  augustss 
    183       1.1  augustss 	sc->sc_iface = iface;
    184       1.1  augustss 	r = usbd_interface2device_handle(iface, &sc->sc_udev);
    185  1.11.4.1   thorpej 	if (r != USBD_NORMAL_COMPLETION) {
    186  1.11.4.1   thorpej 		sc->sc_dying = 1;
    187       1.8  augustss 		USB_ATTACH_ERROR_RETURN;
    188  1.11.4.1   thorpej 	}
    189       1.1  augustss 	sc->sc_ifaceno = id->bInterfaceNumber;
    190       1.1  augustss 
    191       1.1  augustss #if 0
    192       1.1  augustss XXX needs a different way to read the id string since the length
    193       1.1  augustss is unknown.  usbd_do_request() returns error on a short transfer.
    194       1.1  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    195       1.1  augustss 	req.bRequest = UR_GET_DEVICE_ID;
    196       1.1  augustss 	USETW(req.wValue, cd->bConfigurationValue);
    197       1.1  augustss 	USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
    198       1.1  augustss 	USETW(req.wLength, sizeof devinfo - 1);
    199       1.1  augustss 	r = usbd_do_request(dev, &req, devinfo);
    200       1.1  augustss 	if (r == USBD_NORMAL_COMPLETION) {
    201       1.1  augustss 		int len;
    202       1.1  augustss 		char *idstr;
    203       1.1  augustss 		len = (devinfo[0] << 8) | (devinfo[1] & 0xff);
    204       1.1  augustss 		/* devinfo now contains an IEEE-1284 device ID */
    205       1.1  augustss 		idstr = devinfo+2;
    206       1.1  augustss 		idstr[len] = 0;
    207       1.8  augustss 		printf("%s: device id <%s>\n", USBDEVNAME(sc->sc_dev), idstr);
    208       1.1  augustss 	} else {
    209       1.8  augustss 		printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
    210       1.1  augustss 	}
    211       1.1  augustss #endif
    212       1.1  augustss 
    213       1.8  augustss 	USB_ATTACH_SUCCESS_RETURN;
    214       1.1  augustss 
    215       1.1  augustss  nobulk:
    216       1.8  augustss 	printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
    217  1.11.4.1   thorpej 	sc->sc_dying = 1;
    218       1.8  augustss 	USB_ATTACH_ERROR_RETURN;
    219       1.1  augustss }
    220       1.1  augustss 
    221       1.1  augustss int
    222  1.11.4.1   thorpej ulpt_activate(self, act)
    223  1.11.4.1   thorpej 	struct device *self;
    224  1.11.4.1   thorpej 	enum devact act;
    225  1.11.4.1   thorpej {
    226  1.11.4.1   thorpej 	return (0);
    227  1.11.4.1   thorpej }
    228  1.11.4.1   thorpej 
    229  1.11.4.1   thorpej int
    230  1.11.4.1   thorpej ulpt_detach(self, flags)
    231  1.11.4.1   thorpej 	struct device  *self;
    232  1.11.4.1   thorpej 	int flags;
    233  1.11.4.1   thorpej {
    234  1.11.4.1   thorpej 	struct ulpt_softc *sc = (struct ulpt_softc *)self;
    235  1.11.4.1   thorpej 	int maj, mn;
    236  1.11.4.1   thorpej 	int s;
    237  1.11.4.1   thorpej 
    238  1.11.4.1   thorpej 	DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
    239  1.11.4.1   thorpej 
    240  1.11.4.1   thorpej 	sc->sc_dying = 1;
    241  1.11.4.1   thorpej 	if (sc->sc_bulkpipe)
    242  1.11.4.1   thorpej 		usbd_abort_pipe(sc->sc_bulkpipe);
    243  1.11.4.1   thorpej 
    244  1.11.4.1   thorpej 	s = splusb();
    245  1.11.4.1   thorpej 	if (--sc->sc_refcnt >= 0) {
    246  1.11.4.1   thorpej 		/* There is noone to wake, aborting the pipe is enough */
    247  1.11.4.1   thorpej 		/* Wait for processes to go away. */
    248  1.11.4.1   thorpej 		usb_detach_wait(&sc->sc_dev);
    249  1.11.4.1   thorpej 	}
    250  1.11.4.1   thorpej 	splx(s);
    251  1.11.4.1   thorpej 
    252  1.11.4.1   thorpej 	/* locate the major number */
    253  1.11.4.1   thorpej 	for (maj = 0; maj < nchrdev; maj++)
    254  1.11.4.1   thorpej 		if (cdevsw[maj].d_open == ulptopen)
    255  1.11.4.1   thorpej 			break;
    256  1.11.4.1   thorpej 
    257  1.11.4.1   thorpej 	/* Nuke the vnodes for any open instances (calls close). */
    258  1.11.4.1   thorpej 	mn = self->dv_unit;
    259  1.11.4.1   thorpej 	vdevgone(maj, mn, mn, VCHR);
    260  1.11.4.1   thorpej 
    261  1.11.4.1   thorpej 	return (0);
    262  1.11.4.1   thorpej }
    263  1.11.4.1   thorpej 
    264  1.11.4.1   thorpej int
    265       1.1  augustss ulpt_status(sc)
    266       1.1  augustss 	struct ulpt_softc *sc;
    267       1.1  augustss {
    268       1.1  augustss 	usb_device_request_t req;
    269       1.1  augustss 	usbd_status r;
    270       1.1  augustss 	u_char status;
    271       1.1  augustss 
    272       1.1  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    273       1.1  augustss 	req.bRequest = UR_GET_PORT_STATUS;
    274       1.1  augustss 	USETW(req.wValue, 0);
    275       1.1  augustss 	USETW(req.wIndex, sc->sc_ifaceno);
    276       1.1  augustss 	USETW(req.wLength, 1);
    277       1.1  augustss 	r = usbd_do_request(sc->sc_udev, &req, &status);
    278       1.1  augustss 	DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
    279       1.1  augustss 	if (r == USBD_NORMAL_COMPLETION)
    280       1.1  augustss 		return (status);
    281       1.1  augustss 	else
    282       1.1  augustss 		return (0);
    283       1.1  augustss }
    284       1.1  augustss 
    285       1.1  augustss void
    286       1.1  augustss ulpt_reset(sc)
    287       1.1  augustss 	struct ulpt_softc *sc;
    288       1.1  augustss {
    289       1.1  augustss 	usb_device_request_t req;
    290       1.1  augustss 
    291       1.1  augustss 	DPRINTFN(1, ("ulpt_reset\n"));
    292       1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    293       1.1  augustss 	req.bRequest = UR_SOFT_RESET;
    294       1.1  augustss 	USETW(req.wValue, 0);
    295       1.1  augustss 	USETW(req.wIndex, sc->sc_ifaceno);
    296       1.1  augustss 	USETW(req.wLength, 0);
    297       1.1  augustss 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    298       1.1  augustss }
    299       1.1  augustss 
    300       1.1  augustss /*
    301       1.1  augustss  * Reset the printer, then wait until it's selected and not busy.
    302       1.1  augustss  */
    303       1.1  augustss int
    304       1.1  augustss ulptopen(dev, flag, mode, p)
    305       1.1  augustss 	dev_t dev;
    306       1.1  augustss 	int flag;
    307       1.1  augustss 	int mode;
    308       1.1  augustss 	struct proc *p;
    309       1.1  augustss {
    310       1.1  augustss 	u_char flags = ULPTFLAGS(dev);
    311       1.1  augustss 	usbd_status r;
    312       1.1  augustss 	int spin, error;
    313       1.8  augustss 	USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
    314       1.1  augustss 
    315  1.11.4.1   thorpej 	if (!sc || !sc->sc_iface || sc->sc_dying)
    316  1.11.4.2   thorpej 		return (ENXIO);
    317       1.1  augustss 
    318       1.1  augustss 	if (sc->sc_state)
    319  1.11.4.2   thorpej 		return (EBUSY);
    320       1.1  augustss 
    321       1.1  augustss 	sc->sc_state = ULPT_INIT;
    322       1.1  augustss 	sc->sc_flags = flags;
    323       1.1  augustss 	DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
    324       1.1  augustss 
    325       1.1  augustss 	if ((flags & ULPT_NOPRIME) == 0)
    326       1.1  augustss 		ulpt_reset(sc);
    327       1.1  augustss 
    328       1.1  augustss 	for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
    329       1.1  augustss 		if (spin >= TIMEOUT) {
    330       1.1  augustss 			sc->sc_state = 0;
    331  1.11.4.2   thorpej 			return (EBUSY);
    332       1.1  augustss 		}
    333       1.1  augustss 
    334       1.1  augustss 		/* wait 1/4 second, give up if we get a signal */
    335       1.1  augustss 		error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
    336       1.1  augustss 		if (error != EWOULDBLOCK) {
    337       1.1  augustss 			sc->sc_state = 0;
    338  1.11.4.2   thorpej 			return (error);
    339       1.1  augustss 		}
    340       1.1  augustss 	}
    341       1.1  augustss 
    342       1.1  augustss 	r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
    343       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    344       1.1  augustss 		sc->sc_state = 0;
    345       1.1  augustss 		return (EIO);
    346       1.1  augustss 	}
    347       1.1  augustss 
    348       1.1  augustss 	sc->sc_state = ULPT_OPEN;
    349       1.1  augustss 
    350       1.1  augustss 	DPRINTF(("ulptopen: done\n"));
    351       1.1  augustss 	return (0);
    352       1.1  augustss }
    353       1.1  augustss 
    354       1.1  augustss int
    355       1.1  augustss ulpt_statusmsg(status, sc)
    356       1.1  augustss 	u_char status;
    357       1.1  augustss 	struct ulpt_softc *sc;
    358       1.1  augustss {
    359       1.1  augustss 	u_char new;
    360       1.1  augustss 
    361       1.1  augustss 	status = (status ^ LPS_INVERT) & LPS_MASK;
    362       1.1  augustss 	new = status & ~sc->sc_laststatus;
    363       1.1  augustss 	sc->sc_laststatus = status;
    364       1.1  augustss 
    365       1.1  augustss 	if (new & LPS_SELECT)
    366       1.8  augustss 		log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
    367       1.1  augustss 	else if (new & LPS_NOPAPER)
    368       1.8  augustss 		log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
    369       1.1  augustss 	else if (new & LPS_NERR)
    370       1.8  augustss 		log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
    371       1.1  augustss 
    372  1.11.4.2   thorpej 	return (status);
    373       1.1  augustss }
    374       1.1  augustss 
    375       1.1  augustss int
    376       1.1  augustss ulptclose(dev, flag, mode, p)
    377       1.1  augustss 	dev_t dev;
    378       1.1  augustss 	int flag;
    379       1.1  augustss 	int mode;
    380       1.1  augustss 	struct proc *p;
    381       1.1  augustss {
    382       1.8  augustss 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    383  1.11.4.2   thorpej 
    384  1.11.4.2   thorpej 	if (sc->sc_state != ULPT_OPEN)
    385  1.11.4.2   thorpej 		/* We are being forced to close before the open completed. */
    386  1.11.4.2   thorpej 		return (0);
    387       1.1  augustss 
    388       1.1  augustss 	usbd_close_pipe(sc->sc_bulkpipe);
    389  1.11.4.1   thorpej 	sc->sc_bulkpipe = 0;
    390       1.1  augustss 
    391       1.1  augustss 	sc->sc_state = 0;
    392       1.1  augustss 
    393       1.1  augustss 	DPRINTF(("ulptclose: closed\n"));
    394       1.1  augustss 	return (0);
    395       1.1  augustss }
    396       1.1  augustss 
    397       1.1  augustss int
    398  1.11.4.1   thorpej ulpt_do_write(sc, uio, flags)
    399  1.11.4.1   thorpej 	struct ulpt_softc *sc;
    400       1.1  augustss 	struct uio *uio;
    401       1.1  augustss 	int flags;
    402       1.1  augustss {
    403       1.1  augustss 	size_t n;
    404       1.1  augustss 	int error = 0;
    405       1.1  augustss 	char buf[ULPT_BSIZE];
    406       1.1  augustss 	usbd_request_handle reqh;
    407       1.1  augustss 	usbd_status r;
    408       1.1  augustss 
    409       1.1  augustss 	DPRINTF(("ulptwrite\n"));
    410       1.1  augustss 	reqh = usbd_alloc_request();
    411       1.1  augustss 	if (reqh == 0)
    412       1.6  augustss 		return (ENOMEM);
    413       1.1  augustss 	while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
    414       1.1  augustss 		ulpt_statusmsg(ulpt_status(sc), sc);
    415       1.6  augustss 		error = uiomove(buf, n, uio);
    416       1.6  augustss 		if (error)
    417       1.6  augustss 			break;
    418       1.1  augustss 		/* XXX use callback to enable interrupt? */
    419       1.1  augustss 		r = usbd_setup_request(reqh, sc->sc_bulkpipe, 0, buf, n,
    420       1.1  augustss 				       0, USBD_NO_TIMEOUT, 0);
    421       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION) {
    422       1.1  augustss 			error = EIO;
    423       1.1  augustss 			break;
    424       1.1  augustss 		}
    425       1.1  augustss 		DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
    426       1.1  augustss 		r = usbd_sync_transfer(reqh);
    427       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION) {
    428       1.1  augustss 			DPRINTF(("ulptwrite: error=%d\n", r));
    429       1.1  augustss 			usbd_clear_endpoint_stall(sc->sc_bulkpipe);
    430       1.1  augustss 			error = EIO;
    431       1.1  augustss 			break;
    432       1.1  augustss 		}
    433       1.1  augustss 	}
    434       1.1  augustss 	usbd_free_request(reqh);
    435  1.11.4.1   thorpej 
    436  1.11.4.1   thorpej 	return (error);
    437  1.11.4.1   thorpej }
    438  1.11.4.1   thorpej 
    439  1.11.4.1   thorpej int
    440  1.11.4.1   thorpej ulptwrite(dev, uio, flags)
    441  1.11.4.1   thorpej 	dev_t dev;
    442  1.11.4.1   thorpej 	struct uio *uio;
    443  1.11.4.1   thorpej 	int flags;
    444  1.11.4.1   thorpej {
    445  1.11.4.1   thorpej 	USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
    446  1.11.4.1   thorpej 	int error;
    447  1.11.4.1   thorpej 
    448  1.11.4.1   thorpej 	sc->sc_refcnt++;
    449  1.11.4.1   thorpej 	error = ulpt_do_write(sc, uio, flags);
    450  1.11.4.1   thorpej 	if (--sc->sc_refcnt < 0)
    451  1.11.4.1   thorpej 		usb_detach_wakeup(&sc->sc_dev);
    452       1.1  augustss 	return (error);
    453       1.1  augustss }
    454       1.1  augustss 
    455       1.1  augustss int
    456       1.1  augustss ulptioctl(dev, cmd, data, flag, p)
    457       1.1  augustss 	dev_t dev;
    458       1.1  augustss 	u_long cmd;
    459       1.1  augustss 	caddr_t data;
    460       1.1  augustss 	int flag;
    461       1.1  augustss 	struct proc *p;
    462       1.1  augustss {
    463       1.1  augustss 	int error = 0;
    464       1.1  augustss 
    465       1.1  augustss 	switch (cmd) {
    466       1.1  augustss 	default:
    467       1.1  augustss 		error = ENODEV;
    468       1.1  augustss 	}
    469       1.1  augustss 
    470       1.1  augustss 	return error;
    471       1.1  augustss }
    472       1.8  augustss 
    473       1.8  augustss #if defined(__FreeBSD__)
    474      1.10  augustss DRIVER_MODULE(ulpt, usb, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
    475       1.8  augustss #endif
    476