1 /* $NetBSD: ulpt.c,v 1.11 1999/01/10 11:13:36 augustss Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss@carlstedt.se) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * Printer Class spec: http://www.usb.org/developers/data/usbprn10.pdf
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #if defined(__NetBSD__)
51 #include <sys/device.h>
52 #include <sys/ioctl.h>
53 #elif defined(__FreeBSD__)
54 #include <sys/ioccom.h>
55 #include <sys/module.h>
60 #include <sys/syslog.h>
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdevs.h>
66 #include <dev/usb/usb_quirks.h>
68 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
71 #define LPTPRI (PZERO+8)
72 #define ULPT_BSIZE 1024
75 #define DPRINTF(x) if (ulptdebug) logprintf x
76 #define DPRINTFN(n,x) if (ulptdebug>(n)) logprintf x
83 #define UR_GET_DEVICE_ID 0
84 #define UR_GET_PORT_STATUS 1
85 #define UR_SOFT_RESET 2
87 #define LPS_NERR 0x08 /* printer no error */
88 #define LPS_SELECT 0x10 /* printer selected */
89 #define LPS_NOPAPER 0x20 /* printer out of paper */
90 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
91 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
95 usbd_device_handle sc_udev; /* device */
96 usbd_interface_handle sc_iface; /* interface */
98 usbd_pipe_handle sc_bulkpipe; /* bulk pipe */
102 #define ULPT_OPEN 0x01 /* device is open */
103 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
104 #define ULPT_INIT 0x04 /* waiting to initialize for open */
106 #if defined(__NetBSD__)
107 #define ULPT_NOPRIME 0x40 /* don't prime on open */
108 #elif defined(__FreeBSD__)
109 /* values taken from i386/isa/lpt.c */
110 #define ULPT_POS_INIT 0x04 /* if we are a postive init signal */
111 #define ULPT_POS_ACK 0x08 /* if we are a positive going ack */
112 #define ULPT_NOPRIME 0x10 /* don't prime the printer at all */
113 #define ULPT_PRIMEOPEN 0x20 /* prime on every open */
114 #define ULPT_AUTOLF 0x40 /* tell printer to do an automatic lf */
115 #define ULPT_BYPASS 0x80 /* bypass printer ready checks */
117 u_char sc_laststatus;
120 #if defined(__NetBSD__)
121 int ulptopen __P((dev_t, int, int, struct proc *));
122 int ulptclose __P((dev_t, int, int, struct proc *p));
123 int ulptwrite __P((dev_t, struct uio *uio, int));
124 int ulptioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
125 #elif defined(__FreeBSD__)
126 static d_open_t ulptopen;
127 static d_close_t ulptclose;
128 static d_write_t ulptwrite;
129 static d_ioctl_t ulptioctl;
131 #define ULPT_CDEV_MAJOR 113
133 static struct cdevsw ulpt_cdevsw = {
135 /* close */ ulptclose,
137 /* write */ ulptwrite,
138 /* ioctl */ ulptioctl,
141 /* devtotty */ nodevtotty,
144 /* strategy */ nostrategy,
147 /* maj */ ULPT_CDEV_MAJOR,
156 int ulpt_status __P((struct ulpt_softc *));
157 void ulpt_reset __P((struct ulpt_softc *));
158 int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
160 #if defined(__NetBSD__)
161 #define ULPTUNIT(s) (minor(s) & 0x1f)
162 #define ULPTFLAGS(s) (minor(s) & 0xe0)
163 #elif defined(__FreeBSD__)
164 /* defines taken from i386/isa/lpt.c */
165 #define ULPTUNIT(s) (minor(s) & 0x03)
166 #define ULPTFLAGS(s) (minor(s) & 0xfc)
169 USB_DECLARE_DRIVER(ulpt);
173 USB_MATCH_START(ulpt, uaa);
174 usb_interface_descriptor_t *id;
176 DPRINTFN(10,("ulpt_match\n"));
178 return (UMATCH_NONE);
179 id = usbd_get_interface_descriptor(uaa->iface);
181 id->bInterfaceClass == UCLASS_PRINTER &&
182 id->bInterfaceSubClass == USUBCLASS_PRINTER &&
183 (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
184 id->bInterfaceProtocol == UPROTO_PRINTER_BI))
185 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
186 return (UMATCH_NONE);
191 USB_ATTACH_START(ulpt, sc, uaa);
192 usbd_device_handle dev = uaa->device;
193 usbd_interface_handle iface = uaa->iface;
194 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
195 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
196 usb_device_request_t req;
198 usb_endpoint_descriptor_t *ed;
201 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
202 usbd_devinfo(dev, 0, devinfo);
204 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
205 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
207 /* Figure out which endpoint is the bulk out endpoint. */
208 ed = usbd_interface2endpoint_descriptor(iface, 0);
211 if ((ed->bEndpointAddress & UE_DIR) != UE_OUT ||
212 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
213 /* In case we are using a bidir protocol... */
214 ed = usbd_interface2endpoint_descriptor(iface, 1);
217 if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
218 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
221 sc->sc_bulk = ed->bEndpointAddress;
222 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
224 sc->sc_iface = iface;
225 r = usbd_interface2device_handle(iface, &sc->sc_udev);
226 if (r != USBD_NORMAL_COMPLETION)
227 USB_ATTACH_ERROR_RETURN;
228 sc->sc_ifaceno = id->bInterfaceNumber;
230 req.bmRequestType = UT_READ_CLASS_INTERFACE;
231 req.bRequest = UR_GET_DEVICE_ID;
232 USETW(req.wValue, cd->bConfigurationValue);
233 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
234 USETW(req.wLength, sizeof devinfo - 1);
235 r = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK, NULL);
236 if (r == USBD_NORMAL_COMPLETION) {
239 len = (devinfo[0] << 8) | (devinfo[1] & 0xff);
240 /* devinfo now contains an IEEE-1284 device ID */
243 printf("%s: device id <%s>\n", USBDEVNAME(sc->sc_dev), idstr);
245 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
248 USB_ATTACH_SUCCESS_RETURN;
251 printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
252 USB_ATTACH_ERROR_RETURN;
257 struct ulpt_softc *sc;
259 usb_device_request_t req;
263 req.bmRequestType = UT_READ_CLASS_INTERFACE;
264 req.bRequest = UR_GET_PORT_STATUS;
265 USETW(req.wValue, 0);
266 USETW(req.wIndex, sc->sc_ifaceno);
267 USETW(req.wLength, 1);
268 r = usbd_do_request(sc->sc_udev, &req, &status);
269 DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
270 if (r == USBD_NORMAL_COMPLETION)
278 struct ulpt_softc *sc;
280 usb_device_request_t req;
282 DPRINTFN(1, ("ulpt_reset\n"));
283 req.bmRequestType = UT_WRITE_CLASS_OTHER;
284 req.bRequest = UR_SOFT_RESET;
285 USETW(req.wValue, 0);
286 USETW(req.wIndex, sc->sc_ifaceno);
287 USETW(req.wLength, 0);
288 (void)usbd_do_request(sc->sc_udev, &req, 0);
292 * Reset the printer, then wait until it's selected and not busy.
295 ulptopen(dev, flag, mode, p)
301 u_char flags = ULPTFLAGS(dev);
304 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
306 if (!sc || !sc->sc_iface)
312 sc->sc_state = ULPT_INIT;
313 sc->sc_flags = flags;
314 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
316 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
317 /* Ignoring these flags might not be a good idea */
318 if ((flags & ~ULPT_NOPRIME) != 0)
319 printf("ulptopen: flags ignored: %b\n", flags,
320 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
322 if ((flags & ULPT_NOPRIME) == 0)
325 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
326 if (spin >= TIMEOUT) {
331 /* wait 1/4 second, give up if we get a signal */
332 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
333 if (error != EWOULDBLOCK) {
339 r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
340 if (r != USBD_NORMAL_COMPLETION) {
345 sc->sc_state = ULPT_OPEN;
347 DPRINTF(("ulptopen: done\n"));
352 ulpt_statusmsg(status, sc)
354 struct ulpt_softc *sc;
358 status = (status ^ LPS_INVERT) & LPS_MASK;
359 new = status & ~sc->sc_laststatus;
360 sc->sc_laststatus = status;
362 if (new & LPS_SELECT)
363 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
364 else if (new & LPS_NOPAPER)
365 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
366 else if (new & LPS_NERR)
367 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
373 ulptclose(dev, flag, mode, p)
379 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
381 usbd_close_pipe(sc->sc_bulkpipe);
389 ulptwrite(dev, uio, flags)
396 char buf[ULPT_BSIZE];
397 usbd_request_handle reqh;
399 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
401 reqh = usbd_alloc_request();
404 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
405 ulpt_statusmsg(ulpt_status(sc), sc);
406 error = uiomove(buf, n, uio);
409 /* XXX use callback to enable interrupt? */
410 r = usbd_setup_request(reqh, sc->sc_bulkpipe, 0, buf, n,
411 0, USBD_NO_TIMEOUT, 0);
412 if (r != USBD_NORMAL_COMPLETION) {
416 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
417 r = usbd_sync_transfer(reqh);
418 if (r != USBD_NORMAL_COMPLETION) {
419 DPRINTFN(1, ("ulptwrite: error=%d\n", r));
420 usbd_clear_endpoint_stall(sc->sc_bulkpipe);
425 usbd_free_request(reqh);
430 ulptioctl(dev, cmd, data, flag, p)
447 #if defined(__FreeBSD__)
449 ulpt_detach(device_t self)
451 DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
452 device_set_desc(self, NULL);
456 DEV_DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, ulpt_cdevsw, usbd_driver_load, 0);