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