]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/usb/serial/ulpt.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / usb / serial / ulpt.c
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3
4 /*      $NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $       */
5
6 /*-
7  * Copyright (c) 1998, 2003 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  * Printer Class spec: http://www.usb.org/developers/devclass_docs/usbprint11.pdf
46  */
47
48 #include <sys/stdint.h>
49 #include <sys/stddef.h>
50 #include <sys/param.h>
51 #include <sys/queue.h>
52 #include <sys/types.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/bus.h>
56 #include <sys/linker_set.h>
57 #include <sys/module.h>
58 #include <sys/lock.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/sx.h>
63 #include <sys/unistd.h>
64 #include <sys/callout.h>
65 #include <sys/malloc.h>
66 #include <sys/priv.h>
67 #include <sys/syslog.h>
68 #include <sys/selinfo.h>
69 #include <sys/conf.h>
70 #include <sys/fcntl.h>
71
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/usbhid.h>
76 #include "usbdevs.h"
77
78 #define USB_DEBUG_VAR ulpt_debug
79 #include <dev/usb/usb_debug.h>
80 #include <dev/usb/usb_process.h>
81
82 #ifdef USB_DEBUG
83 static int ulpt_debug = 0;
84
85 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
86 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
87     &ulpt_debug, 0, "Debug level");
88 #endif
89
90 #define ULPT_BSIZE              (1<<15) /* bytes */
91 #define ULPT_IFQ_MAXLEN         2       /* units */
92
93 #define UR_GET_DEVICE_ID        0x00
94 #define UR_GET_PORT_STATUS      0x01
95 #define UR_SOFT_RESET           0x02
96
97 #define LPS_NERR                0x08    /* printer no error */
98 #define LPS_SELECT              0x10    /* printer selected */
99 #define LPS_NOPAPER             0x20    /* printer out of paper */
100 #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
101 #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
102
103 enum {
104         ULPT_BULK_DT_WR,
105         ULPT_BULK_DT_RD,
106         ULPT_INTR_DT_RD,
107         ULPT_N_TRANSFER,
108 };
109
110 struct ulpt_softc {
111         struct usb_fifo_sc sc_fifo;
112         struct usb_fifo_sc sc_fifo_noreset;
113         struct mtx sc_mtx;
114         struct usb_callout sc_watchdog;
115
116         device_t sc_dev;
117         struct usb_device *sc_udev;
118         struct usb_fifo *sc_fifo_open[2];
119         struct usb_xfer *sc_xfer[ULPT_N_TRANSFER];
120
121         int     sc_fflags;              /* current open flags, FREAD and
122                                          * FWRITE */
123         uint8_t sc_iface_no;
124         uint8_t sc_last_status;
125         uint8_t sc_zlps;                /* number of consequtive zero length
126                                          * packets received */
127 };
128
129 /* prototypes */
130
131 static device_probe_t ulpt_probe;
132 static device_attach_t ulpt_attach;
133 static device_detach_t ulpt_detach;
134
135 static usb_callback_t ulpt_write_callback;
136 static usb_callback_t ulpt_read_callback;
137 static usb_callback_t ulpt_status_callback;
138
139 static void     ulpt_reset(struct ulpt_softc *);
140 static void     ulpt_watchdog(void *);
141
142 static usb_fifo_close_t ulpt_close;
143 static usb_fifo_cmd_t ulpt_start_read;
144 static usb_fifo_cmd_t ulpt_start_write;
145 static usb_fifo_cmd_t ulpt_stop_read;
146 static usb_fifo_cmd_t ulpt_stop_write;
147 static usb_fifo_ioctl_t ulpt_ioctl;
148 static usb_fifo_open_t ulpt_open;
149 static usb_fifo_open_t unlpt_open;
150
151 static struct usb_fifo_methods ulpt_fifo_methods = {
152         .f_close = &ulpt_close,
153         .f_ioctl = &ulpt_ioctl,
154         .f_open = &ulpt_open,
155         .f_start_read = &ulpt_start_read,
156         .f_start_write = &ulpt_start_write,
157         .f_stop_read = &ulpt_stop_read,
158         .f_stop_write = &ulpt_stop_write,
159         .basename[0] = "ulpt",
160 };
161
162 static struct usb_fifo_methods unlpt_fifo_methods = {
163         .f_close = &ulpt_close,
164         .f_ioctl = &ulpt_ioctl,
165         .f_open = &unlpt_open,
166         .f_start_read = &ulpt_start_read,
167         .f_start_write = &ulpt_start_write,
168         .f_stop_read = &ulpt_stop_read,
169         .f_stop_write = &ulpt_stop_write,
170         .basename[0] = "unlpt",
171 };
172
173 static void
174 ulpt_reset(struct ulpt_softc *sc)
175 {
176         struct usb_device_request req;
177
178         DPRINTFN(2, "\n");
179
180         req.bRequest = UR_SOFT_RESET;
181         USETW(req.wValue, 0);
182         USETW(req.wIndex, sc->sc_iface_no);
183         USETW(req.wLength, 0);
184
185         /*
186          * There was a mistake in the USB printer 1.0 spec that gave the
187          * request type as UT_WRITE_CLASS_OTHER; it should have been
188          * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
189          * so we try both.
190          */
191
192         mtx_lock(&sc->sc_mtx);
193         req.bmRequestType = UT_WRITE_CLASS_OTHER;
194         if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
195             &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {      /* 1.0 */
196                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
197                 if (usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
198                     &req, NULL, 0, NULL, 2 * USB_MS_HZ)) {      /* 1.1 */
199                         /* ignore error */
200                 }
201         }
202         mtx_unlock(&sc->sc_mtx);
203 }
204
205 static void
206 ulpt_write_callback(struct usb_xfer *xfer, usb_error_t error)
207 {
208         struct ulpt_softc *sc = usbd_xfer_softc(xfer);
209         struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_TX];
210         struct usb_page_cache *pc;
211         int actlen, max;
212
213         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
214
215         if (f == NULL) {
216                 /* should not happen */
217                 DPRINTF("no FIFO\n");
218                 return;
219         }
220         DPRINTF("state=0x%x actlen=%d\n", USB_GET_STATE(xfer), actlen);
221
222         switch (USB_GET_STATE(xfer)) {
223         case USB_ST_TRANSFERRED:
224         case USB_ST_SETUP:
225 tr_setup:
226                 pc = usbd_xfer_get_frame(xfer, 0);
227                 max = usbd_xfer_max_len(xfer);
228                 if (usb_fifo_get_data(f, pc, 0, max, &actlen, 0)) {
229                         usbd_xfer_set_frame_len(xfer, 0, actlen);
230                         usbd_transfer_submit(xfer);
231                 }
232                 break;
233
234         default:                        /* Error */
235                 if (error != USB_ERR_CANCELLED) {
236                         /* try to clear stall first */
237                         usbd_xfer_set_stall(xfer);
238                         goto tr_setup;
239                 }
240                 break;
241         }
242 }
243
244 static void
245 ulpt_read_callback(struct usb_xfer *xfer, usb_error_t error)
246 {
247         struct ulpt_softc *sc = usbd_xfer_softc(xfer);
248         struct usb_fifo *f = sc->sc_fifo_open[USB_FIFO_RX];
249         struct usb_page_cache *pc;
250         int actlen;
251
252         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
253
254         if (f == NULL) {
255                 /* should not happen */
256                 DPRINTF("no FIFO\n");
257                 return;
258         }
259         DPRINTF("state=0x%x\n", USB_GET_STATE(xfer));
260
261         switch (USB_GET_STATE(xfer)) {
262         case USB_ST_TRANSFERRED:
263
264                 if (actlen == 0) {
265
266                         if (sc->sc_zlps == 4) {
267                                 /* enable BULK throttle */
268                                 usbd_xfer_set_interval(xfer, 500); /* ms */
269                         } else {
270                                 sc->sc_zlps++;
271                         }
272                 } else {
273                         /* disable BULK throttle */
274
275                         usbd_xfer_set_interval(xfer, 0);
276                         sc->sc_zlps = 0;
277                 }
278
279                 pc = usbd_xfer_get_frame(xfer, 0);
280                 usb_fifo_put_data(f, pc, 0, actlen, 1);
281
282         case USB_ST_SETUP:
283 tr_setup:
284                 if (usb_fifo_put_bytes_max(f) != 0) {
285                         usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
286                         usbd_transfer_submit(xfer);
287                 }
288                 break;
289
290         default:                        /* Error */
291                 /* disable BULK throttle */
292                 usbd_xfer_set_interval(xfer, 0);
293                 sc->sc_zlps = 0;
294
295                 if (error != USB_ERR_CANCELLED) {
296                         /* try to clear stall first */
297                         usbd_xfer_set_stall(xfer);
298                         goto tr_setup;
299                 }
300                 break;
301         }
302 }
303
304 static void
305 ulpt_status_callback(struct usb_xfer *xfer, usb_error_t error)
306 {
307         struct ulpt_softc *sc = usbd_xfer_softc(xfer);
308         struct usb_device_request req;
309         struct usb_page_cache *pc;
310         uint8_t cur_status;
311         uint8_t new_status;
312
313         switch (USB_GET_STATE(xfer)) {
314         case USB_ST_TRANSFERRED:
315
316                 pc = usbd_xfer_get_frame(xfer, 1);
317                 usbd_copy_out(pc, 0, &cur_status, 1);
318
319                 cur_status = (cur_status ^ LPS_INVERT) & LPS_MASK;
320                 new_status = cur_status & ~sc->sc_last_status;
321                 sc->sc_last_status = cur_status;
322
323                 if (new_status & LPS_SELECT)
324                         log(LOG_NOTICE, "%s: offline\n",
325                             device_get_nameunit(sc->sc_dev));
326                 else if (new_status & LPS_NOPAPER)
327                         log(LOG_NOTICE, "%s: out of paper\n",
328                             device_get_nameunit(sc->sc_dev));
329                 else if (new_status & LPS_NERR)
330                         log(LOG_NOTICE, "%s: output error\n",
331                             device_get_nameunit(sc->sc_dev));
332                 break;
333
334         case USB_ST_SETUP:
335                 req.bmRequestType = UT_READ_CLASS_INTERFACE;
336                 req.bRequest = UR_GET_PORT_STATUS;
337                 USETW(req.wValue, 0);
338                 req.wIndex[0] = sc->sc_iface_no;
339                 req.wIndex[1] = 0;
340                 USETW(req.wLength, 1);
341
342                 pc = usbd_xfer_get_frame(xfer, 0);
343                 usbd_copy_in(pc, 0, &req, sizeof(req));
344
345                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
346                 usbd_xfer_set_frame_len(xfer, 1, 1);
347                 usbd_xfer_set_frames(xfer, 2);
348                 usbd_transfer_submit(xfer);
349                 break;
350
351         default:                        /* Error */
352                 DPRINTF("error=%s\n", usbd_errstr(error));
353                 if (error != USB_ERR_CANCELLED) {
354                         /* wait for next watchdog timeout */
355                 }
356                 break;
357         }
358 }
359
360 static const struct usb_config ulpt_config[ULPT_N_TRANSFER] = {
361         [ULPT_BULK_DT_WR] = {
362                 .type = UE_BULK,
363                 .endpoint = UE_ADDR_ANY,
364                 .direction = UE_DIR_OUT,
365                 .bufsize = ULPT_BSIZE,
366                 .flags = {.pipe_bof = 1,.proxy_buffer = 1},
367                 .callback = &ulpt_write_callback,
368         },
369
370         [ULPT_BULK_DT_RD] = {
371                 .type = UE_BULK,
372                 .endpoint = UE_ADDR_ANY,
373                 .direction = UE_DIR_IN,
374                 .bufsize = ULPT_BSIZE,
375                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.proxy_buffer = 1},
376                 .callback = &ulpt_read_callback,
377         },
378
379         [ULPT_INTR_DT_RD] = {
380                 .type = UE_CONTROL,
381                 .endpoint = 0x00,       /* Control pipe */
382                 .direction = UE_DIR_ANY,
383                 .bufsize = sizeof(struct usb_device_request) + 1,
384                 .callback = &ulpt_status_callback,
385                 .timeout = 1000,        /* 1 second */
386         },
387 };
388
389 static void
390 ulpt_start_read(struct usb_fifo *fifo)
391 {
392         struct ulpt_softc *sc = usb_fifo_softc(fifo);
393
394         usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_RD]);
395 }
396
397 static void
398 ulpt_stop_read(struct usb_fifo *fifo)
399 {
400         struct ulpt_softc *sc = usb_fifo_softc(fifo);
401
402         usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_RD]);
403 }
404
405 static void
406 ulpt_start_write(struct usb_fifo *fifo)
407 {
408         struct ulpt_softc *sc = usb_fifo_softc(fifo);
409
410         usbd_transfer_start(sc->sc_xfer[ULPT_BULK_DT_WR]);
411 }
412
413 static void
414 ulpt_stop_write(struct usb_fifo *fifo)
415 {
416         struct ulpt_softc *sc = usb_fifo_softc(fifo);
417
418         usbd_transfer_stop(sc->sc_xfer[ULPT_BULK_DT_WR]);
419 }
420
421 static int
422 ulpt_open(struct usb_fifo *fifo, int fflags)
423 {
424         struct ulpt_softc *sc = usb_fifo_softc(fifo);
425
426         /* we assume that open is a serial process */
427
428         if (sc->sc_fflags == 0) {
429
430                 /* reset USB paralell port */
431
432                 ulpt_reset(sc);
433         }
434         return (unlpt_open(fifo, fflags));
435 }
436
437 static int
438 unlpt_open(struct usb_fifo *fifo, int fflags)
439 {
440         struct ulpt_softc *sc = usb_fifo_softc(fifo);
441
442         if (sc->sc_fflags & fflags) {
443                 return (EBUSY);
444         }
445         if (fflags & FREAD) {
446                 /* clear stall first */
447                 mtx_lock(&sc->sc_mtx);
448                 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_RD]);
449                 mtx_unlock(&sc->sc_mtx);
450                 if (usb_fifo_alloc_buffer(fifo,
451                     usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_RD]),
452                     ULPT_IFQ_MAXLEN)) {
453                         return (ENOMEM);
454                 }
455                 /* set which FIFO is opened */
456                 sc->sc_fifo_open[USB_FIFO_RX] = fifo;
457         }
458         if (fflags & FWRITE) {
459                 /* clear stall first */
460                 mtx_lock(&sc->sc_mtx);
461                 usbd_xfer_set_stall(sc->sc_xfer[ULPT_BULK_DT_WR]);
462                 mtx_unlock(&sc->sc_mtx);
463                 if (usb_fifo_alloc_buffer(fifo,
464                     usbd_xfer_max_len(sc->sc_xfer[ULPT_BULK_DT_WR]),
465                     ULPT_IFQ_MAXLEN)) {
466                         return (ENOMEM);
467                 }
468                 /* set which FIFO is opened */
469                 sc->sc_fifo_open[USB_FIFO_TX] = fifo;
470         }
471         sc->sc_fflags |= fflags & (FREAD | FWRITE);
472         return (0);
473 }
474
475 static void
476 ulpt_close(struct usb_fifo *fifo, int fflags)
477 {
478         struct ulpt_softc *sc = usb_fifo_softc(fifo);
479
480         sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
481
482         if (fflags & (FREAD | FWRITE)) {
483                 usb_fifo_free_buffer(fifo);
484         }
485 }
486
487 static int
488 ulpt_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
489     int fflags)
490 {
491         return (ENODEV);
492 }
493
494 static int
495 ulpt_probe(device_t dev)
496 {
497         struct usb_attach_arg *uaa = device_get_ivars(dev);
498
499         DPRINTFN(11, "\n");
500
501         if (uaa->usb_mode != USB_MODE_HOST) {
502                 return (ENXIO);
503         }
504         if ((uaa->info.bInterfaceClass == UICLASS_PRINTER) &&
505             (uaa->info.bInterfaceSubClass == UISUBCLASS_PRINTER) &&
506             ((uaa->info.bInterfaceProtocol == UIPROTO_PRINTER_UNI) ||
507             (uaa->info.bInterfaceProtocol == UIPROTO_PRINTER_BI) ||
508             (uaa->info.bInterfaceProtocol == UIPROTO_PRINTER_1284))) {
509                 return (0);
510         }
511         return (ENXIO);
512 }
513
514 static int
515 ulpt_attach(device_t dev)
516 {
517         struct usb_attach_arg *uaa = device_get_ivars(dev);
518         struct ulpt_softc *sc = device_get_softc(dev);
519         struct usb_interface_descriptor *id;
520         int unit = device_get_unit(dev);
521         int error;
522         uint8_t iface_index = uaa->info.bIfaceIndex;
523         uint8_t alt_index;
524
525         DPRINTFN(11, "sc=%p\n", sc);
526
527         sc->sc_dev = dev;
528         sc->sc_udev = uaa->device;
529
530         device_set_usb_desc(dev);
531
532         mtx_init(&sc->sc_mtx, "ulpt lock", NULL, MTX_DEF | MTX_RECURSE);
533
534         usb_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
535
536         /* search through all the descriptors looking for bidir mode */
537
538         id = usbd_get_interface_descriptor(uaa->iface);
539         alt_index = 0 - 1;
540         while (1) {
541                 if (id == NULL) {
542                         break;
543                 }
544                 if ((id->bDescriptorType == UDESC_INTERFACE) &&
545                     (id->bLength >= sizeof(*id))) {
546                         if (id->bInterfaceNumber != uaa->info.bIfaceNum) {
547                                 break;
548                         } else {
549                                 alt_index++;
550                                 if ((id->bInterfaceClass == UICLASS_PRINTER) &&
551                                     (id->bInterfaceSubClass == UISUBCLASS_PRINTER) &&
552                                     (id->bInterfaceProtocol == UIPROTO_PRINTER_BI)) {
553                                         goto found;
554                                 }
555                         }
556                 }
557                 id = (void *)usb_desc_foreach(
558                     usbd_get_config_descriptor(uaa->device), (void *)id);
559         }
560         goto detach;
561
562 found:
563
564         DPRINTF("setting alternate "
565             "config number: %d\n", alt_index);
566
567         if (alt_index) {
568
569                 error = usbd_set_alt_interface_index
570                     (uaa->device, iface_index, alt_index);
571
572                 if (error) {
573                         DPRINTF("could not set alternate "
574                             "config, error=%s\n", usbd_errstr(error));
575                         goto detach;
576                 }
577         }
578         sc->sc_iface_no = id->bInterfaceNumber;
579
580         error = usbd_transfer_setup(uaa->device, &iface_index,
581             sc->sc_xfer, ulpt_config, ULPT_N_TRANSFER,
582             sc, &sc->sc_mtx);
583         if (error) {
584                 DPRINTF("error=%s\n", usbd_errstr(error));
585                 goto detach;
586         }
587         device_printf(sc->sc_dev, "using bi-directional mode\n");
588
589 #if 0
590 /*
591  * This code is disabled because for some mysterious reason it causes
592  * printing not to work.  But only sometimes, and mostly with
593  * UHCI and less often with OHCI.  *sigh*
594  */
595         {
596                 struct usb_config_descriptor *cd = usbd_get_config_descriptor(dev);
597                 struct usb_device_request req;
598                 int len, alen;
599
600                 req.bmRequestType = UT_READ_CLASS_INTERFACE;
601                 req.bRequest = UR_GET_DEVICE_ID;
602                 USETW(req.wValue, cd->bConfigurationValue);
603                 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
604                 USETW(req.wLength, sizeof devinfo - 1);
605                 error = usbd_do_request_flags(dev, &req, devinfo, USB_SHORT_XFER_OK,
606                     &alen, USB_DEFAULT_TIMEOUT);
607                 if (error) {
608                         device_printf(sc->sc_dev, "cannot get device id\n");
609                 } else if (alen <= 2) {
610                         device_printf(sc->sc_dev, "empty device id, no "
611                             "printer connected?\n");
612                 } else {
613                         /* devinfo now contains an IEEE-1284 device ID */
614                         len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
615                         if (len > sizeof devinfo - 3)
616                                 len = sizeof devinfo - 3;
617                         devinfo[len] = 0;
618                         printf("%s: device id <", device_get_nameunit(sc->sc_dev));
619                         ieee1284_print_id(devinfo + 2);
620                         printf(">\n");
621                 }
622         }
623 #endif
624
625         error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
626             &ulpt_fifo_methods, &sc->sc_fifo,
627             unit, 0 - 1, uaa->info.bIfaceIndex,
628             UID_ROOT, GID_OPERATOR, 0644);
629         if (error) {
630                 goto detach;
631         }
632         error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
633             &unlpt_fifo_methods, &sc->sc_fifo_noreset,
634             unit, 0 - 1, uaa->info.bIfaceIndex,
635             UID_ROOT, GID_OPERATOR, 0644);
636         if (error) {
637                 goto detach;
638         }
639         /* start reading of status */
640
641         mtx_lock(&sc->sc_mtx);
642         ulpt_watchdog(sc);
643         mtx_unlock(&sc->sc_mtx);
644         return (0);
645
646 detach:
647         ulpt_detach(dev);
648         return (ENOMEM);
649 }
650
651 static int
652 ulpt_detach(device_t dev)
653 {
654         struct ulpt_softc *sc = device_get_softc(dev);
655
656         DPRINTF("sc=%p\n", sc);
657
658         usb_fifo_detach(&sc->sc_fifo);
659         usb_fifo_detach(&sc->sc_fifo_noreset);
660
661         mtx_lock(&sc->sc_mtx);
662         usb_callout_stop(&sc->sc_watchdog);
663         mtx_unlock(&sc->sc_mtx);
664
665         usbd_transfer_unsetup(sc->sc_xfer, ULPT_N_TRANSFER);
666         usb_callout_drain(&sc->sc_watchdog);
667         mtx_destroy(&sc->sc_mtx);
668
669         return (0);
670 }
671
672 #if 0
673 /* XXX This does not belong here. */
674
675 /*
676  * Compare two strings until the second ends.
677  */
678
679 static uint8_t
680 ieee1284_compare(const char *a, const char *b)
681 {
682         while (1) {
683
684                 if (*b == 0) {
685                         break;
686                 }
687                 if (*a != *b) {
688                         return 1;
689                 }
690                 b++;
691                 a++;
692         }
693         return 0;
694 }
695
696 /*
697  * Print select parts of an IEEE 1284 device ID.
698  */
699 void
700 ieee1284_print_id(char *str)
701 {
702         char *p, *q;
703
704         for (p = str - 1; p; p = strchr(p, ';')) {
705                 p++;                    /* skip ';' */
706                 if (ieee1284_compare(p, "MFG:") == 0 ||
707                     ieee1284_compare(p, "MANUFACTURER:") == 0 ||
708                     ieee1284_compare(p, "MDL:") == 0 ||
709                     ieee1284_compare(p, "MODEL:") == 0) {
710                         q = strchr(p, ';');
711                         if (q)
712                                 printf("%.*s", (int)(q - p + 1), p);
713                 }
714         }
715 }
716
717 #endif
718
719 static void
720 ulpt_watchdog(void *arg)
721 {
722         struct ulpt_softc *sc = arg;
723
724         mtx_assert(&sc->sc_mtx, MA_OWNED);
725
726         /* 
727          * Only read status while the device is not opened, due to
728          * possible hardware or firmware bug in some printers.
729          */
730         if (sc->sc_fflags == 0)
731                 usbd_transfer_start(sc->sc_xfer[ULPT_INTR_DT_RD]);
732
733         usb_callout_reset(&sc->sc_watchdog,
734             hz, &ulpt_watchdog, sc);
735 }
736
737 static devclass_t ulpt_devclass;
738
739 static device_method_t ulpt_methods[] = {
740         DEVMETHOD(device_probe, ulpt_probe),
741         DEVMETHOD(device_attach, ulpt_attach),
742         DEVMETHOD(device_detach, ulpt_detach),
743         {0, 0}
744 };
745
746 static driver_t ulpt_driver = {
747         .name = "ulpt",
748         .methods = ulpt_methods,
749         .size = sizeof(struct ulpt_softc),
750 };
751
752 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, NULL, 0);
753 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
754 MODULE_DEPEND(ulpt, ucom, 1, 1, 1);