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