| Commit | Line | Data |
|---|---|---|
| 62ade751 | 1 | /* |
| 1550dfd9 MD |
2 | * $NetBSD: ulpt.c,v 1.55 2002/10/23 09:14:01 jdolecek Exp $ |
| 3 | * $FreeBSD: src/sys/dev/usb/ulpt.c,v 1.59 2003/09/28 20:48:13 phk Exp $ | |
| b508545d | 4 | * $DragonFly: src/sys/dev/usbmisc/ulpt/ulpt.c,v 1.25 2008/02/11 16:56:53 dillon Exp $ |
| 62ade751 | 5 | */ |
| 984263bc MD |
6 | |
| 7 | /* | |
| 8 | * Copyright (c) 1998 The NetBSD Foundation, Inc. | |
| 9 | * All rights reserved. | |
| 10 | * | |
| 11 | * This code is derived from software contributed to The NetBSD Foundation | |
| 12 | * by Lennart Augustsson (lennart@augustsson.net) at | |
| 13 | * Carlstedt Research & Technology. | |
| 14 | * | |
| 15 | * Redistribution and use in source and binary forms, with or without | |
| 16 | * modification, are permitted provided that the following conditions | |
| 17 | * are met: | |
| 18 | * 1. Redistributions of source code must retain the above copyright | |
| 19 | * notice, this list of conditions and the following disclaimer. | |
| 20 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 21 | * notice, this list of conditions and the following disclaimer in the | |
| 22 | * documentation and/or other materials provided with the distribution. | |
| 23 | * 3. All advertising materials mentioning features or use of this software | |
| 24 | * must display the following acknowledgement: | |
| 25 | * This product includes software developed by the NetBSD | |
| 26 | * Foundation, Inc. and its contributors. | |
| 27 | * 4. Neither the name of The NetBSD Foundation nor the names of its | |
| 28 | * contributors may be used to endorse or promote products derived | |
| 29 | * from this software without specific prior written permission. | |
| 30 | * | |
| 31 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
| 32 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
| 33 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 34 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
| 35 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 36 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 37 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 38 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 39 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 40 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 41 | * POSSIBILITY OF SUCH DAMAGE. | |
| 42 | */ | |
| 43 | ||
| 44 | /* | |
| 1550dfd9 | 45 | * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF |
| 984263bc MD |
46 | */ |
| 47 | ||
| 984263bc MD |
48 | #include <sys/param.h> |
| 49 | #include <sys/systm.h> | |
| 50 | #include <sys/proc.h> | |
| 51 | #include <sys/kernel.h> | |
| 984263bc MD |
52 | #include <sys/module.h> |
| 53 | #include <sys/bus.h> | |
| 984263bc MD |
54 | #include <sys/uio.h> |
| 55 | #include <sys/conf.h> | |
| fef8985e | 56 | #include <sys/device.h> |
| 984263bc MD |
57 | #include <sys/syslog.h> |
| 58 | #include <sys/sysctl.h> | |
| 4e01b467 | 59 | #include <sys/thread2.h> |
| 984263bc | 60 | |
| 1f2de5d4 MD |
61 | #include <bus/usb/usb.h> |
| 62 | #include <bus/usb/usbdi.h> | |
| 63 | #include <bus/usb/usbdi_util.h> | |
| 1550dfd9 | 64 | #include <bus/usb/usb_quirks.h> |
| 984263bc MD |
65 | |
| 66 | #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */ | |
| 67 | #define STEP hz/4 | |
| 68 | ||
| 984263bc MD |
69 | #define ULPT_BSIZE 16384 |
| 70 | ||
| 71 | #ifdef USB_DEBUG | |
| fc1ef497 HT |
72 | #define DPRINTF(x) if (ulptdebug) kprintf x |
| 73 | #define DPRINTFN(n,x) if (ulptdebug>(n)) kprintf x | |
| 984263bc MD |
74 | int ulptdebug = 0; |
| 75 | SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt"); | |
| 76 | SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW, | |
| 77 | &ulptdebug, 0, "ulpt debug level"); | |
| 78 | #else | |
| 79 | #define DPRINTF(x) | |
| 80 | #define DPRINTFN(n,x) | |
| 81 | #endif | |
| 82 | ||
| 83 | #define UR_GET_DEVICE_ID 0 | |
| 84 | #define UR_GET_PORT_STATUS 1 | |
| 85 | #define UR_SOFT_RESET 2 | |
| 86 | ||
| 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) | |
| 92 | ||
| 93 | struct ulpt_softc { | |
| 6ed427ca | 94 | device_t sc_dev; |
| 984263bc MD |
95 | usbd_device_handle sc_udev; /* device */ |
| 96 | usbd_interface_handle sc_iface; /* interface */ | |
| 97 | int sc_ifaceno; | |
| 1550dfd9 MD |
98 | |
| 99 | int sc_out; | |
| 100 | usbd_pipe_handle sc_out_pipe; /* bulk out pipe */ | |
| 101 | ||
| 102 | int sc_in; | |
| 103 | usbd_pipe_handle sc_in_pipe; /* bulk in pipe */ | |
| 104 | usbd_xfer_handle sc_in_xfer1; | |
| 105 | usbd_xfer_handle sc_in_xfer2; | |
| 106 | u_char sc_junk[64]; /* somewhere to dump input */ | |
| 984263bc MD |
107 | |
| 108 | u_char sc_state; | |
| 109 | #define ULPT_OPEN 0x01 /* device is open */ | |
| 110 | #define ULPT_OBUSY 0x02 /* printer is busy doing output */ | |
| 111 | #define ULPT_INIT 0x04 /* waiting to initialize for open */ | |
| 112 | u_char sc_flags; | |
| 113 | #define ULPT_NOPRIME 0x40 /* don't prime on open */ | |
| 114 | u_char sc_laststatus; | |
| 115 | ||
| 116 | int sc_refcnt; | |
| 117 | u_char sc_dying; | |
| b508545d MD |
118 | int vendor; |
| 119 | int product; | |
| 984263bc MD |
120 | }; |
| 121 | ||
| 6ed427ca HT |
122 | static d_open_t ulptopen; |
| 123 | static d_close_t ulptclose; | |
| 124 | static d_write_t ulptwrite; | |
| 125 | static d_ioctl_t ulptioctl; | |
| 984263bc MD |
126 | |
| 127 | #define ULPT_CDEV_MAJOR 113 | |
| 128 | ||
| 6ed427ca | 129 | static struct dev_ops ulpt_ops = { |
| fef8985e MD |
130 | { "ulpt", ULPT_CDEV_MAJOR, 0 }, |
| 131 | .d_open = ulptopen, | |
| 132 | .d_close = ulptclose, | |
| 133 | .d_write = ulptwrite, | |
| 134 | .d_ioctl = ulptioctl, | |
| 984263bc | 135 | }; |
| 984263bc MD |
136 | |
| 137 | void ulpt_disco(void *); | |
| 138 | ||
| 139 | int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int); | |
| 140 | int ulpt_status(struct ulpt_softc *); | |
| 141 | void ulpt_reset(struct ulpt_softc *); | |
| 142 | int ulpt_statusmsg(u_char, struct ulpt_softc *); | |
| 143 | ||
| 1550dfd9 | 144 | #if 0 |
| 984263bc | 145 | void ieee1284_print_id(char *); |
| 1550dfd9 | 146 | #endif |
| 984263bc MD |
147 | |
| 148 | #define ULPTUNIT(s) (minor(s) & 0x1f) | |
| 149 | #define ULPTFLAGS(s) (minor(s) & 0xe0) | |
| 150 | ||
| 151 | ||
| 61b69189 HT |
152 | static device_probe_t ulpt_match; |
| 153 | static device_attach_t ulpt_attach; | |
| 154 | static device_detach_t ulpt_detach; | |
| 155 | ||
| 156 | static devclass_t ulpt_devclass; | |
| 157 | ||
| 158 | static kobj_method_t ulpt_methods[] = { | |
| 159 | DEVMETHOD(device_probe, ulpt_match), | |
| 160 | DEVMETHOD(device_attach, ulpt_attach), | |
| 161 | DEVMETHOD(device_detach, ulpt_detach), | |
| 162 | {0,0} | |
| 163 | }; | |
| 164 | ||
| 165 | static driver_t ulpt_driver = { | |
| 166 | "ulpt", | |
| 167 | ulpt_methods, | |
| 168 | sizeof(struct ulpt_softc) | |
| 169 | }; | |
| 170 | ||
| 171 | MODULE_DEPEND(ulpt, usb, 1, 1, 1); | |
| 984263bc | 172 | |
| e785a5d9 HT |
173 | static int |
| 174 | ulpt_match(device_t self) | |
| 984263bc | 175 | { |
| e785a5d9 | 176 | struct usb_attach_arg *uaa = device_get_ivars(self); |
| 984263bc | 177 | usb_interface_descriptor_t *id; |
| 1550dfd9 | 178 | |
| 984263bc MD |
179 | DPRINTFN(10,("ulpt_match\n")); |
| 180 | if (uaa->iface == NULL) | |
| 181 | return (UMATCH_NONE); | |
| 182 | id = usbd_get_interface_descriptor(uaa->iface); | |
| 183 | if (id != NULL && | |
| 184 | id->bInterfaceClass == UICLASS_PRINTER && | |
| 185 | id->bInterfaceSubClass == UISUBCLASS_PRINTER && | |
| 186 | (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI || | |
| 1550dfd9 MD |
187 | id->bInterfaceProtocol == UIPROTO_PRINTER_BI || |
| 188 | id->bInterfaceProtocol == UIPROTO_PRINTER_1284)) | |
| 984263bc MD |
189 | return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO); |
| 190 | return (UMATCH_NONE); | |
| 191 | } | |
| 192 | ||
| e785a5d9 HT |
193 | static int |
| 194 | ulpt_attach(device_t self) | |
| 984263bc | 195 | { |
| e785a5d9 HT |
196 | struct ulpt_softc *sc = device_get_softc(self); |
| 197 | struct usb_attach_arg *uaa = device_get_ivars(self); | |
| 984263bc MD |
198 | usbd_device_handle dev = uaa->device; |
| 199 | usbd_interface_handle iface = uaa->iface; | |
| 1550dfd9 MD |
200 | usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface); |
| 201 | usb_interface_descriptor_t *id, *iend; | |
| 202 | usb_config_descriptor_t *cdesc; | |
| 203 | usbd_status err; | |
| 984263bc | 204 | usb_endpoint_descriptor_t *ed; |
| 1550dfd9 MD |
205 | u_int8_t epcount; |
| 206 | int i, altno; | |
| 207 | ||
| 984263bc | 208 | DPRINTFN(10,("ulpt_attach: sc=%p\n", sc)); |
| e785a5d9 | 209 | sc->sc_dev = self; |
| 1550dfd9 MD |
210 | |
| 211 | /* XXX | |
| 212 | * Stepping through the alternate settings needs to be abstracted out. | |
| 213 | */ | |
| 214 | cdesc = usbd_get_config_descriptor(dev); | |
| 215 | if (cdesc == NULL) { | |
| e3869ec7 | 216 | kprintf("%s: failed to get configuration descriptor\n", |
| 6ed427ca | 217 | device_get_nameunit(sc->sc_dev)); |
| e785a5d9 | 218 | return ENXIO; |
| 1550dfd9 MD |
219 | } |
| 220 | iend = (usb_interface_descriptor_t *) | |
| 221 | ((char *)cdesc + UGETW(cdesc->wTotalLength)); | |
| 222 | #ifdef DIAGNOSTIC | |
| 223 | if (ifcd < (usb_interface_descriptor_t *)cdesc || | |
| 224 | ifcd >= iend) | |
| 225 | panic("ulpt: iface desc out of range"); | |
| 226 | #endif | |
| 227 | /* Step through all the descriptors looking for bidir mode */ | |
| 228 | for (id = ifcd, altno = 0; | |
| 229 | id < iend; | |
| 230 | id = (void *)((char *)id + id->bLength)) { | |
| 231 | if (id->bDescriptorType == UDESC_INTERFACE && | |
| 232 | id->bInterfaceNumber == ifcd->bInterfaceNumber) { | |
| 233 | if (id->bInterfaceClass == UICLASS_PRINTER && | |
| 234 | id->bInterfaceSubClass == UISUBCLASS_PRINTER && | |
| 235 | (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* || | |
| 236 | id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */)) | |
| 237 | goto found; | |
| 238 | altno++; | |
| 239 | } | |
| 240 | } | |
| 241 | id = ifcd; /* not found, use original */ | |
| 242 | found: | |
| 243 | if (id != ifcd) { | |
| 244 | /* Found a new bidir setting */ | |
| 245 | DPRINTF(("ulpt_attach: set altno = %d\n", altno)); | |
| 246 | err = usbd_set_interface(iface, altno); | |
| 247 | if (err) { | |
| e3869ec7 | 248 | kprintf("%s: setting alternate interface failed\n", |
| 6ed427ca | 249 | device_get_nameunit(sc->sc_dev)); |
| 1550dfd9 | 250 | sc->sc_dying = 1; |
| e785a5d9 | 251 | return ENXIO; |
| 1550dfd9 | 252 | } |
| 984263bc | 253 | } |
| 984263bc | 254 | |
| 1550dfd9 MD |
255 | epcount = 0; |
| 256 | (void)usbd_endpoint_count(iface, &epcount); | |
| 257 | ||
| 258 | sc->sc_in = -1; | |
| 259 | sc->sc_out = -1; | |
| b508545d MD |
260 | sc->vendor = uaa->vendor; |
| 261 | sc->product = uaa->product; | |
| 1550dfd9 MD |
262 | for (i = 0; i < epcount; i++) { |
| 263 | ed = usbd_interface2endpoint_descriptor(iface, i); | |
| 264 | if (ed == NULL) { | |
| e3869ec7 | 265 | kprintf("%s: couldn't get ep %d\n", |
| 6ed427ca | 266 | device_get_nameunit(sc->sc_dev), i); |
| e785a5d9 | 267 | return ENXIO; |
| 1550dfd9 MD |
268 | } |
| 269 | if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && | |
| 270 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { | |
| 271 | sc->sc_in = ed->bEndpointAddress; | |
| 272 | } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && | |
| 273 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { | |
| 274 | sc->sc_out = ed->bEndpointAddress; | |
| 275 | } | |
| 276 | } | |
| 277 | if (sc->sc_out == -1) { | |
| e3869ec7 | 278 | kprintf("%s: could not find bulk out endpoint\n", |
| 6ed427ca | 279 | device_get_nameunit(sc->sc_dev)); |
| 984263bc | 280 | sc->sc_dying = 1; |
| e785a5d9 | 281 | return ENXIO; |
| 984263bc | 282 | } |
| 1550dfd9 MD |
283 | |
| 284 | if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) { | |
| 285 | /* This device doesn't handle reading properly. */ | |
| 286 | sc->sc_in = -1; | |
| 287 | } | |
| 288 | ||
| 6ed427ca | 289 | kprintf("%s: using %s-directional mode\n", device_get_nameunit(sc->sc_dev), |
| 1550dfd9 MD |
290 | sc->sc_in >= 0 ? "bi" : "uni"); |
| 291 | ||
| 292 | DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out)); | |
| 293 | ||
| 294 | sc->sc_iface = iface; | |
| 984263bc | 295 | sc->sc_ifaceno = id->bInterfaceNumber; |
| 1550dfd9 | 296 | sc->sc_udev = dev; |
| 984263bc | 297 | |
| fef8985e | 298 | make_dev(&ulpt_ops, device_get_unit(self), |
| 3e82b46c MD |
299 | UID_ROOT, GID_OPERATOR, 0644, |
| 300 | "ulpt%d", device_get_unit(self)); | |
| fef8985e | 301 | make_dev(&ulpt_ops, |
| 3e82b46c MD |
302 | device_get_unit(self)|ULPT_NOPRIME, |
| 303 | UID_ROOT, GID_OPERATOR, 0644, | |
| 304 | "unlpt%d", device_get_unit(self)); | |
| 984263bc | 305 | |
| 1550dfd9 | 306 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, |
| 2939c544 | 307 | sc->sc_dev); |
| 984263bc | 308 | |
| e785a5d9 | 309 | return 0; |
| 984263bc MD |
310 | } |
| 311 | ||
| e785a5d9 HT |
312 | static int |
| 313 | ulpt_detach(device_t self) | |
| 984263bc | 314 | { |
| e785a5d9 | 315 | struct ulpt_softc *sc = device_get_softc(self); |
| 984263bc | 316 | |
| 984263bc | 317 | DPRINTF(("ulpt_detach: sc=%p\n", sc)); |
| 984263bc MD |
318 | |
| 319 | sc->sc_dying = 1; | |
| 1550dfd9 MD |
320 | if (sc->sc_out_pipe != NULL) |
| 321 | usbd_abort_pipe(sc->sc_out_pipe); | |
| 322 | if (sc->sc_in_pipe != NULL) | |
| 323 | usbd_abort_pipe(sc->sc_in_pipe); | |
| 984263bc | 324 | |
| 473ed6e2 MD |
325 | /* |
| 326 | * Wait for any ongoing operations to complete before we actually | |
| 327 | * close things down. | |
| 328 | */ | |
| 329 | ||
| 4e01b467 | 330 | crit_enter(); |
| 473ed6e2 MD |
331 | --sc->sc_refcnt; |
| 332 | if (sc->sc_refcnt >= 0) { | |
| 6ed427ca | 333 | kprintf("%s: waiting for idle\n", device_get_nameunit(sc->sc_dev)); |
| 473ed6e2 | 334 | while (sc->sc_refcnt >= 0) |
| 2939c544 | 335 | usb_detach_wait(sc->sc_dev); |
| 6ed427ca | 336 | kprintf("%s: idle wait done\n", device_get_nameunit(sc->sc_dev)); |
| 984263bc | 337 | } |
| 4e01b467 | 338 | crit_exit(); |
| 984263bc | 339 | |
| cd29885a | 340 | dev_ops_remove_minor(&ulpt_ops, /*-1, */device_get_unit(self)); |
| 984263bc | 341 | |
| 1550dfd9 | 342 | usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, |
| 2939c544 | 343 | sc->sc_dev); |
| 1550dfd9 | 344 | |
| 984263bc MD |
345 | return (0); |
| 346 | } | |
| 347 | ||
| 348 | int | |
| 349 | ulpt_status(struct ulpt_softc *sc) | |
| 350 | { | |
| 351 | usb_device_request_t req; | |
| 352 | usbd_status err; | |
| 353 | u_char status; | |
| 354 | ||
| 355 | req.bmRequestType = UT_READ_CLASS_INTERFACE; | |
| 356 | req.bRequest = UR_GET_PORT_STATUS; | |
| 357 | USETW(req.wValue, 0); | |
| 358 | USETW(req.wIndex, sc->sc_ifaceno); | |
| 359 | USETW(req.wLength, 1); | |
| 360 | err = usbd_do_request(sc->sc_udev, &req, &status); | |
| 361 | DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err)); | |
| 362 | if (!err) | |
| 363 | return (status); | |
| 364 | else | |
| 365 | return (0); | |
| 366 | } | |
| 367 | ||
| 368 | void | |
| 369 | ulpt_reset(struct ulpt_softc *sc) | |
| 370 | { | |
| 371 | usb_device_request_t req; | |
| b508545d MD |
372 | int other_fails; |
| 373 | ||
| 374 | /* The Brother HL1240 doesn't handle UT_WRITE_CLASS_OTHER */ | |
| 375 | other_fails = (sc->vendor == 0x04f9 && sc->product == 0x0006); | |
| 984263bc MD |
376 | |
| 377 | DPRINTFN(1, ("ulpt_reset\n")); | |
| 984263bc MD |
378 | req.bRequest = UR_SOFT_RESET; |
| 379 | USETW(req.wValue, 0); | |
| 380 | USETW(req.wIndex, sc->sc_ifaceno); | |
| 381 | USETW(req.wLength, 0); | |
| 1550dfd9 MD |
382 | |
| 383 | /* | |
| 384 | * There was a mistake in the USB printer 1.0 spec that gave the | |
| 385 | * request type as UT_WRITE_CLASS_OTHER; it should have been | |
| 386 | * UT_WRITE_CLASS_INTERFACE. Many printers use the old one, | |
| 387 | * so we try both. | |
| 388 | */ | |
| 389 | req.bmRequestType = UT_WRITE_CLASS_OTHER; | |
| b508545d MD |
390 | /* Some printers don't handle UT_WRITE_CLASS_OTHER */ |
| 391 | if (other_fails || usbd_do_request(sc->sc_udev, &req, 0)) {/* 1.0 */ | |
| 1550dfd9 MD |
392 | req.bmRequestType = UT_WRITE_CLASS_INTERFACE; |
| 393 | (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */ | |
| 394 | } | |
| 984263bc MD |
395 | } |
| 396 | ||
| 1550dfd9 MD |
397 | static void |
| 398 | ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) | |
| 399 | { | |
| 400 | struct ulpt_softc *sc = priv; | |
| 401 | u_int32_t count; | |
| 402 | ||
| 403 | /* Don't loop on errors or 0-length input. */ | |
| 404 | usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL); | |
| 405 | if (status != USBD_NORMAL_COMPLETION || count == 0) | |
| 406 | return; | |
| 407 | ||
| 408 | DPRINTFN(2,("ulpt_input: got some data\n")); | |
| 409 | /* Do it again. */ | |
| 410 | if (xfer == sc->sc_in_xfer1) | |
| 411 | usbd_transfer(sc->sc_in_xfer2); | |
| 412 | else | |
| 413 | usbd_transfer(sc->sc_in_xfer1); | |
| 414 | } | |
| 415 | ||
| 416 | int ulptusein = 1; | |
| 417 | ||
| 984263bc MD |
418 | /* |
| 419 | * Reset the printer, then wait until it's selected and not busy. | |
| 420 | */ | |
| 421 | int | |
| fef8985e | 422 | ulptopen(struct dev_open_args *ap) |
| 984263bc | 423 | { |
| b13267a5 | 424 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
425 | u_char flags = ULPTFLAGS(dev); |
| 426 | struct ulpt_softc *sc; | |
| 427 | usbd_status err; | |
| 428 | int spin, error; | |
| 429 | ||
| 1e089e7e HT |
430 | sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev)); |
| 431 | if (sc == NULL) | |
| 432 | return (ENXIO); | |
| 984263bc MD |
433 | |
| 434 | if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying) | |
| 435 | return (ENXIO); | |
| 436 | ||
| 437 | if (sc->sc_state) | |
| 438 | return (EBUSY); | |
| 439 | ||
| 440 | sc->sc_state = ULPT_INIT; | |
| 441 | sc->sc_flags = flags; | |
| 442 | DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags)); | |
| 443 | ||
| 7c070ee7 | 444 | #if defined(USB_DEBUG) |
| 984263bc MD |
445 | /* Ignoring these flags might not be a good idea */ |
| 446 | if ((flags & ~ULPT_NOPRIME) != 0) | |
| e3869ec7 | 447 | kprintf("ulptopen: flags ignored: %b\n", flags, |
| 984263bc MD |
448 | "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS"); |
| 449 | #endif | |
| 450 | ||
| 1550dfd9 MD |
451 | error = 0; |
| 452 | sc->sc_refcnt++; | |
| 984263bc | 453 | |
| 62ade751 | 454 | if ((flags & ULPT_NOPRIME) == 0) { |
| 984263bc | 455 | ulpt_reset(sc); |
| 62ade751 | 456 | if (sc->sc_dying) { |
| 1550dfd9 | 457 | error = ENXIO; |
| 62ade751 | 458 | sc->sc_state = 0; |
| 1550dfd9 | 459 | goto done; |
| 62ade751 MD |
460 | } |
| 461 | } | |
| 984263bc MD |
462 | |
| 463 | for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) { | |
| 1550dfd9 | 464 | DPRINTF(("ulpt_open: waiting a while\n")); |
| 984263bc | 465 | if (spin >= TIMEOUT) { |
| 1550dfd9 | 466 | error = EBUSY; |
| 984263bc | 467 | sc->sc_state = 0; |
| 1550dfd9 | 468 | goto done; |
| 984263bc MD |
469 | } |
| 470 | ||
| 471 | /* wait 1/4 second, give up if we get a signal */ | |
| 377d4740 | 472 | error = tsleep((caddr_t)sc, PCATCH, "ulptop", STEP); |
| 984263bc MD |
473 | if (error != EWOULDBLOCK) { |
| 474 | sc->sc_state = 0; | |
| 1550dfd9 | 475 | goto done; |
| 984263bc MD |
476 | } |
| 477 | ||
| 478 | if (sc->sc_dying) { | |
| 1550dfd9 | 479 | error = ENXIO; |
| 984263bc | 480 | sc->sc_state = 0; |
| 1550dfd9 | 481 | goto done; |
| 984263bc MD |
482 | } |
| 483 | } | |
| 484 | ||
| 1550dfd9 | 485 | err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe); |
| 984263bc MD |
486 | if (err) { |
| 487 | sc->sc_state = 0; | |
| 1550dfd9 MD |
488 | error = EIO; |
| 489 | goto done; | |
| 490 | } | |
| 491 | ||
| 492 | if (ulptusein && sc->sc_in != -1) { | |
| 493 | DPRINTF(("ulpt_open: open input pipe\n")); | |
| 494 | err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe); | |
| 495 | if (err) { | |
| 496 | error = EIO; | |
| 497 | usbd_close_pipe(sc->sc_out_pipe); | |
| 498 | sc->sc_out_pipe = NULL; | |
| 499 | sc->sc_state = 0; | |
| 500 | goto done; | |
| 501 | } | |
| 502 | sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev); | |
| 503 | sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev); | |
| 504 | if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) { | |
| 505 | error = ENOMEM; | |
| 506 | if (sc->sc_in_xfer1 != NULL) { | |
| 507 | usbd_free_xfer(sc->sc_in_xfer1); | |
| 508 | sc->sc_in_xfer1 = NULL; | |
| 509 | } | |
| 510 | if (sc->sc_in_xfer2 != NULL) { | |
| 511 | usbd_free_xfer(sc->sc_in_xfer2); | |
| 512 | sc->sc_in_xfer2 = NULL; | |
| 513 | } | |
| 514 | usbd_close_pipe(sc->sc_out_pipe); | |
| 515 | sc->sc_out_pipe = NULL; | |
| 516 | usbd_close_pipe(sc->sc_in_pipe); | |
| 517 | sc->sc_in_pipe = NULL; | |
| 518 | sc->sc_state = 0; | |
| 519 | goto done; | |
| 520 | } | |
| 521 | usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc, | |
| 522 | sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK, | |
| 523 | USBD_NO_TIMEOUT, ulpt_input); | |
| 524 | usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc, | |
| 525 | sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK, | |
| 526 | USBD_NO_TIMEOUT, ulpt_input); | |
| 527 | usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */ | |
| 984263bc MD |
528 | } |
| 529 | ||
| 530 | sc->sc_state = ULPT_OPEN; | |
| 531 | ||
| 1550dfd9 MD |
532 | done: |
| 533 | if (--sc->sc_refcnt < 0) | |
| 2939c544 | 534 | usb_detach_wakeup(sc->sc_dev); |
| 1550dfd9 MD |
535 | |
| 536 | DPRINTF(("ulptopen: done, error=%d\n", error)); | |
| 537 | return (error); | |
| 984263bc MD |
538 | } |
| 539 | ||
| 540 | int | |
| 541 | ulpt_statusmsg(u_char status, struct ulpt_softc *sc) | |
| 542 | { | |
| 543 | u_char new; | |
| 544 | ||
| 545 | status = (status ^ LPS_INVERT) & LPS_MASK; | |
| 546 | new = status & ~sc->sc_laststatus; | |
| 547 | sc->sc_laststatus = status; | |
| 548 | ||
| 549 | if (new & LPS_SELECT) | |
| 6ed427ca | 550 | log(LOG_NOTICE, "%s: offline\n", device_get_nameunit(sc->sc_dev)); |
| 984263bc | 551 | else if (new & LPS_NOPAPER) |
| 6ed427ca | 552 | log(LOG_NOTICE, "%s: out of paper\n", device_get_nameunit(sc->sc_dev)); |
| 984263bc | 553 | else if (new & LPS_NERR) |
| 6ed427ca | 554 | log(LOG_NOTICE, "%s: output error\n", device_get_nameunit(sc->sc_dev)); |
| 984263bc MD |
555 | |
| 556 | return (status); | |
| 557 | } | |
| 558 | ||
| 559 | int | |
| fef8985e | 560 | ulptclose(struct dev_close_args *ap) |
| 984263bc | 561 | { |
| b13267a5 | 562 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
563 | struct ulpt_softc *sc; |
| 564 | ||
| 1e089e7e | 565 | sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev)); |
| 984263bc MD |
566 | |
| 567 | if (sc->sc_state != ULPT_OPEN) | |
| 568 | /* We are being forced to close before the open completed. */ | |
| 569 | return (0); | |
| 570 | ||
| 1550dfd9 MD |
571 | if (sc->sc_out_pipe != NULL) { |
| 572 | usbd_close_pipe(sc->sc_out_pipe); | |
| 573 | sc->sc_out_pipe = NULL; | |
| 574 | } | |
| 575 | if (sc->sc_in_pipe != NULL) { | |
| 576 | usbd_abort_pipe(sc->sc_in_pipe); | |
| 577 | usbd_close_pipe(sc->sc_in_pipe); | |
| 578 | sc->sc_in_pipe = NULL; | |
| 579 | if (sc->sc_in_xfer1 != NULL) { | |
| 580 | usbd_free_xfer(sc->sc_in_xfer1); | |
| 581 | sc->sc_in_xfer1 = NULL; | |
| 582 | } | |
| 583 | if (sc->sc_in_xfer2 != NULL) { | |
| 584 | usbd_free_xfer(sc->sc_in_xfer2); | |
| 585 | sc->sc_in_xfer2 = NULL; | |
| 586 | } | |
| 587 | } | |
| 984263bc MD |
588 | |
| 589 | sc->sc_state = 0; | |
| 590 | ||
| 591 | DPRINTF(("ulptclose: closed\n")); | |
| 592 | return (0); | |
| 593 | } | |
| 594 | ||
| 595 | int | |
| 596 | ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags) | |
| 597 | { | |
| 598 | u_int32_t n; | |
| 599 | int error = 0; | |
| 600 | void *bufp; | |
| 601 | usbd_xfer_handle xfer; | |
| 602 | usbd_status err; | |
| 603 | ||
| 604 | DPRINTF(("ulptwrite\n")); | |
| 605 | xfer = usbd_alloc_xfer(sc->sc_udev); | |
| 606 | if (xfer == NULL) | |
| 607 | return (ENOMEM); | |
| 608 | bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE); | |
| 609 | if (bufp == NULL) { | |
| 610 | usbd_free_xfer(xfer); | |
| 611 | return (ENOMEM); | |
| 612 | } | |
| e54488bb | 613 | while ((n = szmin(ULPT_BSIZE, uio->uio_resid)) != 0) { |
| 984263bc MD |
614 | ulpt_statusmsg(ulpt_status(sc), sc); |
| 615 | error = uiomove(bufp, n, uio); | |
| 616 | if (error) | |
| 617 | break; | |
| 618 | DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n)); | |
| 1550dfd9 | 619 | err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY, |
| 984263bc MD |
620 | USBD_NO_TIMEOUT, bufp, &n, "ulptwr"); |
| 621 | if (err) { | |
| 622 | DPRINTF(("ulptwrite: error=%d\n", err)); | |
| 623 | error = EIO; | |
| 624 | break; | |
| 625 | } | |
| 626 | } | |
| 627 | usbd_free_xfer(xfer); | |
| 628 | ||
| 629 | return (error); | |
| 630 | } | |
| 631 | ||
| 632 | int | |
| fef8985e | 633 | ulptwrite(struct dev_write_args *ap) |
| 984263bc | 634 | { |
| b13267a5 | 635 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
636 | struct ulpt_softc *sc; |
| 637 | int error; | |
| 638 | ||
| 1e089e7e | 639 | sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev)); |
| 984263bc MD |
640 | |
| 641 | if (sc->sc_dying) | |
| 642 | return (EIO); | |
| 643 | ||
| 644 | sc->sc_refcnt++; | |
| fef8985e | 645 | error = ulpt_do_write(sc, ap->a_uio, ap->a_ioflag); |
| 984263bc | 646 | if (--sc->sc_refcnt < 0) |
| 2939c544 | 647 | usb_detach_wakeup(sc->sc_dev); |
| 984263bc MD |
648 | return (error); |
| 649 | } | |
| 650 | ||
| 651 | int | |
| fef8985e | 652 | ulptioctl(struct dev_ioctl_args *ap) |
| 984263bc MD |
653 | { |
| 654 | int error = 0; | |
| 655 | ||
| fef8985e | 656 | switch (ap->a_cmd) { |
| 984263bc MD |
657 | default: |
| 658 | error = ENODEV; | |
| 659 | } | |
| 660 | ||
| 661 | return (error); | |
| 662 | } | |
| 663 | ||
| 664 | #if 0 | |
| 665 | /* XXX This does not belong here. */ | |
| 666 | /* | |
| 1550dfd9 | 667 | * Print select parts of an IEEE 1284 device ID. |
| 984263bc MD |
668 | */ |
| 669 | void | |
| 670 | ieee1284_print_id(char *str) | |
| 671 | { | |
| 672 | char *p, *q; | |
| 673 | ||
| 674 | for (p = str-1; p; p = strchr(p, ';')) { | |
| 675 | p++; /* skip ';' */ | |
| 676 | if (strncmp(p, "MFG:", 4) == 0 || | |
| 677 | strncmp(p, "MANUFACTURER:", 14) == 0 || | |
| 678 | strncmp(p, "MDL:", 4) == 0 || | |
| 679 | strncmp(p, "MODEL:", 6) == 0) { | |
| 680 | q = strchr(p, ';'); | |
| 681 | if (q) | |
| e3869ec7 | 682 | kprintf("%.*s", (int)(q - p + 1), p); |
| 984263bc MD |
683 | } |
| 684 | } | |
| 685 | } | |
| 686 | #endif | |
| 687 | ||
| 984263bc | 688 | DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0); |
| 7c070ee7 | 689 |