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