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