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