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