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