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