ulpt.c revision 1.105 1 /* $NetBSD: ulpt.c,v 1.105 2020/03/13 18:17:41 christos Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ulpt.c,v 1.105 2020/03/13 18:17:41 christos Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_usb.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/kernel.h>
48 #include <sys/fcntl.h>
49 #include <sys/device.h>
50 #include <sys/ioctl.h>
51 #include <sys/uio.h>
52 #include <sys/conf.h>
53 #include <sys/vnode.h>
54 #include <sys/syslog.h>
55
56 #include <machine/vmparam.h> /* PAGE_SIZE */
57
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
63
64 #include "ioconf.h"
65
66 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
67 #define STEP hz/4
68
69 #define LPTPRI (PZERO+8)
70 #define ULPT_BSIZE PAGE_SIZE
71
72 #define ULPT_READS_PER_SEC 5
73 /* XXX Why is 10 us a reasonable value? */
74 #define ULPT_READ_TIMO 10
75
76 #ifdef ULPT_DEBUG
77 #define DPRINTFN(n,x) if (ulptdebug>=(n)) printf x
78 int ulptdebug = 0;
79 /*
80 * The strategy for debug levels is:
81 * 1: attach-time operations
82 * 2: open/close/status/reset
83 * 3: read/write basic
84 * 4: read/write details
85 * 10: left over from previous debug code
86 */
87 #else
88 #define DPRINTFN(n,x)
89 #endif
90
91 #define UR_GET_DEVICE_ID 0
92 #define UR_GET_PORT_STATUS 1
93 #define UR_SOFT_RESET 2
94
95 #define LPS_NERR 0x08 /* printer no error */
96 #define LPS_SELECT 0x10 /* printer selected */
97 #define LPS_NOPAPER 0x20 /* printer out of paper */
98 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
99 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
100
101 struct ulpt_softc {
102 device_t sc_dev;
103 struct usbd_device *sc_udev; /* device */
104 struct usbd_interface *sc_iface; /* interface */
105 int sc_ifaceno;
106
107 int sc_out;
108 struct usbd_pipe *sc_out_pipe; /* bulk out pipe */
109 struct usbd_xfer *sc_out_xfer;
110 void *sc_out_buf;
111
112 int sc_in;
113 struct usbd_pipe *sc_in_pipe; /* bulk in pipe */
114 struct usbd_xfer *sc_in_xfer;
115 void *sc_in_buf;
116
117 struct callout sc_read_callout; /* to drain input on write-only opens */
118 int sc_has_callout;
119
120 u_char sc_state;
121 #define ULPT_OPEN 0x01 /* device is open */
122 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
123 #define ULPT_INIT 0x04 /* waiting to initialize for open */
124 u_char sc_flags;
125 #define ULPT_NOPRIME 0x40 /* don't prime on open */
126 u_char sc_laststatus;
127
128 int sc_refcnt;
129 u_char sc_dying;
130 };
131
132 static dev_type_open(ulptopen);
133 static dev_type_close(ulptclose);
134 static dev_type_write(ulptwrite);
135 static dev_type_read(ulptread);
136 static dev_type_ioctl(ulptioctl);
137
138 const struct cdevsw ulpt_cdevsw = {
139 .d_open = ulptopen,
140 .d_close = ulptclose,
141 .d_read = ulptread,
142 .d_write = ulptwrite,
143 .d_ioctl = ulptioctl,
144 .d_stop = nostop,
145 .d_tty = notty,
146 .d_poll = nopoll,
147 .d_mmap = nommap,
148 .d_kqfilter = nokqfilter,
149 .d_discard = nodiscard,
150 .d_flag = D_OTHER
151 };
152
153 static int ulpt_do_write(struct ulpt_softc *, struct uio *, int);
154 static int ulpt_do_read(struct ulpt_softc *, struct uio *, int);
155 static int ulpt_status(struct ulpt_softc *);
156 static void ulpt_reset(struct ulpt_softc *);
157 static int ulpt_statusmsg(u_char, struct ulpt_softc *);
158 static void ulpt_read_cb(struct usbd_xfer *, void *, usbd_status);
159 static void ulpt_tick(void *xsc);
160
161 #if 0
162 void ieee1284_print_id(char *);
163 #endif
164
165 #define ULPTUNIT(s) (minor(s) & 0x1f)
166 #define ULPTFLAGS(s) (minor(s) & 0xe0)
167
168
169 static int ulpt_match(device_t, cfdata_t, void *);
170 static void ulpt_attach(device_t, device_t, void *);
171 static int ulpt_detach(device_t, int);
172 static int ulpt_activate(device_t, enum devact);
173
174
175
176 CFATTACH_DECL_NEW(ulpt, sizeof(struct ulpt_softc), ulpt_match, ulpt_attach,
177 ulpt_detach, ulpt_activate);
178
179 static int
180 ulpt_match(device_t parent, cfdata_t match, void *aux)
181 {
182 struct usbif_attach_arg *uiaa = aux;
183 /* XXX Print something useful, or don't. */
184 DPRINTFN(10,("ulpt_match\n"));
185
186 if (uiaa->uiaa_class == UICLASS_PRINTER &&
187 uiaa->uiaa_subclass == UISUBCLASS_PRINTER &&
188 (uiaa->uiaa_proto == UIPROTO_PRINTER_UNI ||
189 uiaa->uiaa_proto == UIPROTO_PRINTER_BI ||
190 uiaa->uiaa_proto == UIPROTO_PRINTER_1284))
191 return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
192 return UMATCH_NONE;
193 }
194
195 static void
196 ulpt_attach(device_t parent, device_t self, void *aux)
197 {
198 struct ulpt_softc *sc = device_private(self);
199 struct usbif_attach_arg *uiaa = aux;
200 struct usbd_device *dev = uiaa->uiaa_device;
201 struct usbd_interface *iface = uiaa->uiaa_iface;
202 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
203 const usb_interface_descriptor_t *id;
204 usbd_status err;
205 char *devinfop;
206 usb_endpoint_descriptor_t *ed;
207 uint8_t epcount;
208 int i, altno;
209 usbd_desc_iter_t iter;
210
211 sc->sc_dev = self;
212
213 aprint_naive("\n");
214 aprint_normal("\n");
215
216 devinfop = usbd_devinfo_alloc(dev, 0);
217 aprint_normal_dev(self, "%s, iclass %d/%d\n",
218 devinfop, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
219 usbd_devinfo_free(devinfop);
220
221 /* Loop through descriptors looking for a bidir mode. */
222 usb_desc_iter_init(dev, &iter);
223 for (altno = 0;;) {
224 id = (const usb_interface_descriptor_t *)usb_desc_iter_next(&iter);
225 if (!id)
226 break;
227 if (id->bDescriptorType == UDESC_INTERFACE &&
228 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
229 if (id->bInterfaceClass == UICLASS_PRINTER &&
230 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
231 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*||
232 id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/))
233 goto found;
234 altno++;
235 }
236 }
237 id = ifcd; /* not found, use original */
238 found:
239 if (id != ifcd) {
240 /* Found a new bidir setting */
241 DPRINTFN(1, ("ulpt_attach: set altno = %d\n", altno));
242 err = usbd_set_interface(iface, altno);
243 if (err) {
244 aprint_error_dev(self,
245 "setting alternate interface failed\n");
246 sc->sc_dying = 1;
247 return;
248 }
249 }
250
251 epcount = 0;
252 (void)usbd_endpoint_count(iface, &epcount);
253
254 sc->sc_in = -1;
255 sc->sc_out = -1;
256 for (i = 0; i < epcount; i++) {
257 ed = usbd_interface2endpoint_descriptor(iface, i);
258 if (ed == NULL) {
259 aprint_error_dev(self, "couldn't get ep %d\n", i);
260 return;
261 }
262 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
263 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
264 sc->sc_in = ed->bEndpointAddress;
265 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
266 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
267 sc->sc_out = ed->bEndpointAddress;
268 }
269 }
270 if (sc->sc_out == -1) {
271 aprint_error_dev(self, "could not find bulk out endpoint\n");
272 sc->sc_dying = 1;
273 return;
274 }
275
276 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
277 /* This device doesn't handle reading properly. */
278 sc->sc_in = -1;
279 }
280
281 aprint_normal_dev(self, "using %s-directional mode\n",
282 sc->sc_in >= 0 ? "bi" : "uni");
283
284 sc->sc_iface = iface;
285 sc->sc_ifaceno = id->bInterfaceNumber;
286 sc->sc_udev = dev;
287
288 #if 0
289 /*
290 * This code is disabled because for some mysterious reason it causes
291 * printing not to work. But only sometimes, and mostly with
292 * UHCI and less often with OHCI. *sigh*
293 */
294 {
295 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
296 usb_device_request_t req;
297 int len, alen;
298
299 req.bmRequestType = UT_READ_CLASS_INTERFACE;
300 req.bRequest = UR_GET_DEVICE_ID;
301 USETW(req.wValue, cd->bConfigurationValue);
302 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
303 USETW(req.wLength, DEVINFOSIZE - 1);
304 err = usbd_do_request_flags(dev, &req, devinfop, USBD_SHORT_XFER_OK,
305 &alen, USBD_DEFAULT_TIMEOUT);
306 if (err) {
307 printf("%s: cannot get device id\n", device_xname(sc->sc_dev));
308 } else if (alen <= 2) {
309 printf("%s: empty device id, no printer connected?\n",
310 device_xname(sc->sc_dev));
311 } else {
312 /* devinfop now contains an IEEE-1284 device ID */
313 len = ((devinfop[0] & 0xff) << 8) | (devinfop[1] & 0xff);
314 if (len > DEVINFOSIZE - 3)
315 len = DEVINFOSIZE - 3;
316 devinfop[len] = 0;
317 printf("%s: device id <", device_xname(sc->sc_dev));
318 ieee1284_print_id(devinfop+2);
319 printf(">\n");
320 }
321 }
322 #endif
323
324 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
325
326 DPRINTFN(1, ("ulpt_attach: sc=%p in=%d out=%d\n",
327 sc, sc->sc_out, sc->sc_in));
328
329 return;
330 }
331
332 static int
333 ulpt_activate(device_t self, enum devact act)
334 {
335 struct ulpt_softc *sc = device_private(self);
336
337 switch (act) {
338 case DVACT_DEACTIVATE:
339 sc->sc_dying = 1;
340 return 0;
341 default:
342 return EOPNOTSUPP;
343 }
344 }
345
346 static int
347 ulpt_detach(device_t self, int flags)
348 {
349 struct ulpt_softc *sc = device_private(self);
350 int s;
351 int maj, mn;
352
353 DPRINTFN(1, ("ulpt_detach: sc=%p\n", sc));
354
355 sc->sc_dying = 1;
356 if (sc->sc_out_pipe != NULL)
357 usbd_abort_pipe(sc->sc_out_pipe);
358 if (sc->sc_in_pipe != NULL)
359 usbd_abort_pipe(sc->sc_in_pipe);
360
361 s = splusb();
362 if (--sc->sc_refcnt >= 0) {
363 /* There is noone to wake, aborting the pipe is enough */
364 /* Wait for processes to go away. */
365 usb_detach_waitold(sc->sc_dev);
366 }
367 splx(s);
368
369 /* locate the major number */
370 maj = cdevsw_lookup_major(&ulpt_cdevsw);
371
372 /* Nuke the vnodes for any open instances (calls close). */
373 mn = device_unit(self);
374 vdevgone(maj, mn, mn, VCHR);
375 vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
376
377 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
378
379 return 0;
380 }
381
382 static int
383 ulpt_status(struct ulpt_softc *sc)
384 {
385 usb_device_request_t req;
386 usbd_status err;
387 u_char status;
388
389 req.bmRequestType = UT_READ_CLASS_INTERFACE;
390 req.bRequest = UR_GET_PORT_STATUS;
391 USETW(req.wValue, 0);
392 USETW(req.wIndex, sc->sc_ifaceno);
393 USETW(req.wLength, 1);
394 err = usbd_do_request(sc->sc_udev, &req, &status);
395 DPRINTFN(2, ("ulpt_status: status=%#02x err=%d\n", status, err));
396 if (!err)
397 return status;
398 else
399 return 0;
400 }
401
402 static void
403 ulpt_reset(struct ulpt_softc *sc)
404 {
405 usb_device_request_t req;
406
407 DPRINTFN(2, ("ulpt_reset\n"));
408 req.bRequest = UR_SOFT_RESET;
409 USETW(req.wValue, 0);
410 USETW(req.wIndex, sc->sc_ifaceno);
411 USETW(req.wLength, 0);
412
413 /*
414 * There was a mistake in the USB printer 1.0 spec that gave the
415 * request type as UT_WRITE_CLASS_OTHER; it should have been
416 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
417 * so we try both.
418 */
419 req.bmRequestType = UT_WRITE_CLASS_OTHER;
420 if (usbd_do_request(sc->sc_udev, &req, 0)) { /* 1.0 */
421 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
422 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
423 }
424 }
425
426 int ulptusein = 1;
427
428 /*
429 * Reset the printer, then wait until it's selected and not busy.
430 */
431 static int
432 ulptopen(dev_t dev, int flag, int mode, struct lwp *l)
433 {
434 u_char flags = ULPTFLAGS(dev);
435 struct ulpt_softc *sc;
436 usbd_status err;
437 int spin, error;
438
439 sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev));
440 if (sc == NULL)
441 return ENXIO;
442
443 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
444 return ENXIO;
445
446 if (sc->sc_state)
447 return EBUSY;
448
449 sc->sc_state = ULPT_INIT;
450 sc->sc_flags = flags;
451 DPRINTFN(2, ("ulptopen: flags=%#x\n", (unsigned)flags));
452
453 error = 0;
454 sc->sc_refcnt++;
455
456 if ((flags & ULPT_NOPRIME) == 0)
457 ulpt_reset(sc);
458
459 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
460 DPRINTFN(2, ("ulpt_open: waiting a while\n"));
461 if (spin >= TIMEOUT) {
462 error = EBUSY;
463 sc->sc_state = 0;
464 goto done;
465 }
466
467 /* wait 1/4 second, give up if we get a signal */
468 error = kpause("ulptop", true, STEP, NULL);
469 if (error != EWOULDBLOCK) {
470 sc->sc_state = 0;
471 goto done;
472 }
473
474 if (sc->sc_dying) {
475 error = ENXIO;
476 sc->sc_state = 0;
477 goto done;
478 }
479 }
480
481 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
482 if (err) {
483 error = EIO;
484 goto err0;
485 }
486 error = usbd_create_xfer(sc->sc_out_pipe, ULPT_BSIZE, 0, 0,
487 &sc->sc_out_xfer);
488 if (error)
489 goto err2;
490 sc->sc_out_buf = usbd_get_buffer(sc->sc_out_xfer);
491
492 if (ulptusein && sc->sc_in != -1) {
493 DPRINTFN(2, ("ulpt_open: opening input pipe %d\n", sc->sc_in));
494 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
495 if (err) {
496 error = EIO;
497 goto err2;
498 }
499 error = usbd_create_xfer(sc->sc_in_pipe, ULPT_BSIZE,
500 0, 0, &sc->sc_in_xfer);
501 if (error)
502 goto err3;
503 sc->sc_in_buf = usbd_get_buffer(sc->sc_in_xfer);
504 /* If it's not opened for read then set up a reader. */
505 if (!(flag & FREAD)) {
506 DPRINTFN(2, ("ulpt_open: start read callout\n"));
507 callout_init(&sc->sc_read_callout, 0);
508 callout_reset(&sc->sc_read_callout, hz/5, ulpt_tick, sc);
509 sc->sc_has_callout = 1;
510 }
511 }
512
513 sc->sc_state = ULPT_OPEN;
514 goto done;
515
516 err3:
517 usbd_close_pipe(sc->sc_in_pipe);
518 sc->sc_in_pipe = NULL;
519 err2:
520 usbd_destroy_xfer(sc->sc_out_xfer);
521 sc->sc_out_xfer = NULL;
522
523 usbd_close_pipe(sc->sc_out_pipe);
524 sc->sc_out_pipe = NULL;
525 err0:
526 sc->sc_state = 0;
527
528 done:
529 if (--sc->sc_refcnt < 0)
530 usb_detach_wakeupold(sc->sc_dev);
531
532 DPRINTFN(2, ("ulptopen: done, error=%d\n", error));
533 return error;
534 }
535
536 /*
537 * XXX Document return value semantics.
538 */
539 static int
540 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
541 {
542 u_char new;
543
544 status = (status ^ LPS_INVERT) & LPS_MASK;
545 new = status & ~sc->sc_laststatus;
546 sc->sc_laststatus = status;
547
548 if (new & LPS_SELECT)
549 log(LOG_NOTICE, "%s: offline\n", device_xname(sc->sc_dev));
550 if (new & LPS_NOPAPER)
551 log(LOG_NOTICE, "%s: out of paper\n", device_xname(sc->sc_dev));
552 if (new & LPS_NERR)
553 log(LOG_NOTICE, "%s: output error\n", device_xname(sc->sc_dev));
554
555 return status;
556 }
557
558 static int
559 ulptclose(dev_t dev, int flag, int mode,
560 struct lwp *l)
561 {
562 struct ulpt_softc *sc;
563
564 sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev));
565
566 if (sc->sc_state != ULPT_OPEN)
567 /* We are being forced to close before the open completed. */
568 return 0;
569
570 if (sc->sc_has_callout) {
571 DPRINTFN(2, ("ulptclose: stopping read callout\n"));
572 callout_halt(&sc->sc_read_callout, NULL);
573 callout_destroy(&sc->sc_read_callout);
574 sc->sc_has_callout = 0;
575 }
576
577 if (sc->sc_out_pipe != NULL) {
578 usbd_abort_pipe(sc->sc_out_pipe);
579 }
580 if (sc->sc_out_xfer != NULL) {
581 usbd_destroy_xfer(sc->sc_out_xfer);
582 sc->sc_out_xfer = NULL;
583 }
584 if (sc->sc_out_pipe != NULL) {
585 usbd_close_pipe(sc->sc_out_pipe);
586 sc->sc_out_pipe = NULL;
587 }
588 if (sc->sc_in_pipe != NULL) {
589 usbd_abort_pipe(sc->sc_in_pipe);
590 }
591 if (sc->sc_in_xfer != NULL) {
592 usbd_destroy_xfer(sc->sc_in_xfer);
593 sc->sc_in_xfer = NULL;
594 }
595 if (sc->sc_in_pipe != NULL) {
596 usbd_close_pipe(sc->sc_in_pipe);
597 sc->sc_in_pipe = NULL;
598 }
599
600 sc->sc_state = 0;
601
602 DPRINTFN(2, ("ulptclose: closed\n"));
603 return 0;
604 }
605
606 static int
607 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
608 {
609 uint32_t n;
610 int error = 0;
611 void *bufp;
612 struct usbd_xfer *xfer;
613 usbd_status err;
614
615 DPRINTFN(3, ("ulptwrite\n"));
616 xfer = sc->sc_out_xfer;
617 bufp = sc->sc_out_buf;
618 while ((n = uimin(ULPT_BSIZE, uio->uio_resid)) != 0) {
619 ulpt_statusmsg(ulpt_status(sc), sc);
620 error = uiomove(bufp, n, uio);
621 if (error)
622 break;
623 DPRINTFN(4, ("ulptwrite: transfer %d bytes\n", n));
624 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, 0,
625 USBD_NO_TIMEOUT, bufp, &n);
626 if (err) {
627 DPRINTFN(3, ("ulptwrite: error=%d\n", err));
628 error = EIO;
629 break;
630 }
631 }
632
633 return error;
634 }
635
636 static int
637 ulptwrite(dev_t dev, struct uio *uio, int flags)
638 {
639 struct ulpt_softc *sc;
640 int error;
641
642 sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev));
643
644 if (sc->sc_dying)
645 return EIO;
646
647 sc->sc_refcnt++;
648 error = ulpt_do_write(sc, uio, flags);
649 if (--sc->sc_refcnt < 0)
650 usb_detach_wakeupold(sc->sc_dev);
651 return error;
652 }
653
654 /*
655 * Perform a read operation according to the given uio.
656 * This should respect nonblocking I/O status.
657 *
658 * XXX Doing a short read when more data is available seems to be
659 * problematic. See
660 * http://www.freebsd.org/cgi/query-pr.cgi?pr=91538&cat= for a fix.
661 * However, this will be unnecessary given a proper fix for the next
662 * problem, and most actual callers read a lot.
663 *
664 * XXX This code should interact properly with select/poll, and that
665 * requires the USB transactions to be queued and function before the
666 * user does a read. Read will then consume data from a buffer, and
667 * not interact with the device. See ucom.c for an example of how to
668 * do this.
669 */
670 static int
671 ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags)
672 {
673 uint32_t n, nread, nreq;
674 int error = 0, nonblocking, timeout;
675 void *bufp;
676 struct usbd_xfer *xfer;
677 usbd_status err = USBD_NORMAL_COMPLETION;
678
679 /* XXX Resolve with background reader process. KASSERT? */
680 if (sc->sc_in_pipe == NULL)
681 return EIO;
682
683 if (flags & IO_NDELAY)
684 nonblocking = 1;
685 else
686 nonblocking = 0;
687
688 if (nonblocking)
689 timeout = USBD_DEFAULT_TIMEOUT; /* 5 ms */
690 else
691 timeout = USBD_NO_TIMEOUT;
692
693 DPRINTFN(3, ("ulptread nonblocking=%d uio_reside=%ld timeout=%d\n",
694 nonblocking, (u_long)uio->uio_resid, timeout));
695
696 xfer = sc->sc_in_xfer;
697 bufp = sc->sc_in_buf;
698 nread = 0;
699 while ((nreq = uimin(ULPT_BSIZE, uio->uio_resid)) != 0) {
700 KASSERT(error == 0);
701 if (error != 0) {
702 printf("ulptread: pre-switch error %d != 0", error);
703 goto done;
704 }
705
706 /*
707 * XXX Even with the short timeout, this will sleep,
708 * but it should be adequately prompt in practice.
709 */
710 n = nreq;
711 DPRINTFN(4, ("ulptread: transfer %d bytes, nonblocking=%d timeout=%d\n",
712 n, nonblocking, timeout));
713 err = usbd_bulk_transfer(xfer, sc->sc_in_pipe,
714 USBD_SHORT_XFER_OK, timeout, bufp, &n);
715
716 DPRINTFN(4, ("ulptread: transfer complete nreq %d n %d nread %d err %d\n",
717 nreq, n, nread, err));
718 /*
719 * Process "err" return, jumping to done if we set "error".
720 */
721 switch (err) {
722 case USBD_NORMAL_COMPLETION:
723 if (n == 0) {
724 DPRINTFN(3, ("ulptread: NORMAL n==0\n"));
725 }
726 break;
727
728 case USBD_SHORT_XFER:
729 /* We said SHORT_XFER_OK, so shouldn't happen. */
730 DPRINTFN(3, ("ulptread: SHORT n=%d\n", n));
731 break;
732
733 case USBD_TIMEOUT:
734 if (nonblocking == 0) {
735 /* XXX Cannot happen; perhaps KASSERT. */
736 printf("ulptread: timeout in blocking mode\n");
737 error = EIO;
738 goto done;
739 }
740
741 DPRINTFN(3, ("ulptread: TIMEOUT n %d nread %d error %d\n",
742 n, nread, error));
743 /*
744 * Don't set error until we understand why
745 * this happens.
746 */
747 break;
748
749 case USBD_INTERRUPTED:
750 /*
751 * The sleep in usbd_bulk_transfer was
752 * interrupted. Reflect it to the caller so
753 * that reading can be interrupted.
754 */
755 error = EINTR;
756 DPRINTFN(3, ("ulptread: EINTR error %d\n", error));
757 goto done;
758 break;
759
760 default:
761 /* Assume all other return codes are really errors. */
762 error = EIO;
763 DPRINTFN(3, ("ulptread: n %d err %d error %d\n",
764 n, err, error));
765 goto done;
766 break;
767 }
768 /* XXX KASSERT */
769 if (error != 0) {
770 printf("ulptread: post-switch error %d != 0", error);
771 goto done;
772 }
773
774 if (n > 0) {
775 /*
776 * Record progress to enable later choosing
777 * between short reads and EWOULDBLOCK.
778 */
779 nread += n;
780
781 /* Copy to userspace, giving up on any error. */
782 error = uiomove(bufp, n, uio);
783 if (error != 0)
784 break;
785 } else {
786 /*
787 * We read 0 bytes, and therefore are done,
788 * even if we aren't in nonblocking mode.
789 */
790 if (error == 0 && nread == 0)
791 error = EWOULDBLOCK;
792 DPRINTFN(3, ("ulptread: read 0=>done error %d\n",
793 error));
794 goto done;
795 }
796
797 /*
798 * A short transfer indicates no more data will be
799 * forthcoming. Terminate this read regardless of
800 * whether we are in nonblocking mode. XXX Reconsider
801 * for blocking mode; maybe we should continue to
802 * block, but maybe it just doesn't make senes to do
803 * blocking reads from devices like this.
804 */
805 if (err == USBD_SHORT_XFER) {
806 DPRINTFN(3, ("ulptread: SHORT=>done n %d nread %d err %d error %d\n",
807 n, nread, err, error));
808 break;
809 }
810 }
811
812 done:
813 DPRINTFN(3, ("ulptread: finished n %d nread %d err %d error %d\n",
814 n, nread, err, error));
815 return error;
816 }
817
818 static int
819 ulptread(dev_t dev, struct uio *uio, int flags)
820 {
821 struct ulpt_softc *sc;
822 int error;
823
824 sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev));
825
826 if (sc->sc_dying)
827 return EIO;
828
829 sc->sc_refcnt++;
830 error = ulpt_do_read(sc, uio, flags);
831 if (--sc->sc_refcnt < 0)
832 usb_detach_wakeupold(sc->sc_dev);
833 return error;
834 }
835
836 static void
837 ulpt_read_cb(struct usbd_xfer *xfer, void *priv,
838 usbd_status status)
839 {
840 usbd_status err;
841 uint32_t n;
842 void *xsc;
843 struct ulpt_softc *sc;
844
845 usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err);
846 sc = xsc;
847
848 DPRINTFN(4, ("ulpt_read_cb: start sc=%p, err=%d n=%d\n", sc, err, n));
849
850 #ifdef ULPT_DEBUG
851 if (!err && n > 0)
852 DPRINTFN(3, ("ulpt_tick: discarding %d bytes\n", n));
853 #endif
854 if (!err || err == USBD_TIMEOUT)
855 callout_reset(&sc->sc_read_callout, hz / ULPT_READS_PER_SEC,
856 ulpt_tick, sc);
857 }
858
859 /*
860 * For devices which are not opened for reading, this function is
861 * called continuously to start read bulk transfers to avoid the
862 * printer overflowing its output buffer.
863 *
864 * XXX This should be adapted for continuous reads to allow select to
865 * work; see do_ulpt_read().
866 */
867 static void
868 ulpt_tick(void *xsc)
869 {
870 struct ulpt_softc *sc = xsc;
871 usbd_status err __unused;
872
873 if (sc == NULL || sc->sc_dying)
874 return;
875
876 usbd_setup_xfer(sc->sc_in_xfer, sc, sc->sc_in_buf, ULPT_BSIZE,
877 USBD_SHORT_XFER_OK, ULPT_READ_TIMO, ulpt_read_cb);
878 err = usbd_transfer(sc->sc_in_xfer);
879 DPRINTFN(3, ("ulpt_tick: sc=%p err=%d\n", sc, err));
880 }
881
882 static int
883 ulptioctl(dev_t dev, u_long cmd, void *data,
884 int flag, struct lwp *l)
885 {
886 #if 0
887 struct ulpt_softc *sc;
888
889 sc = device_lookup_private(&ulpt_cd, ULPTUNIT(dev));
890 #endif
891
892 switch (cmd) {
893 case FIONBIO:
894 return 0;
895 }
896
897 return ENODEV;
898 }
899
900 #if 0
901 /* XXX This does not belong here. */
902 /*
903 * Print select parts of a IEEE 1284 device ID.
904 */
905 void
906 ieee1284_print_id(char *str)
907 {
908 char *p, *q;
909
910 for (p = str-1; p; p = strchr(p, ';')) {
911 p++; /* skip ';' */
912 if (strncmp(p, "MFG:", 4) == 0 ||
913 strncmp(p, "MANUFACTURER:", 14) == 0 ||
914 strncmp(p, "MDL:", 4) == 0 ||
915 strncmp(p, "MODEL:", 6) == 0) {
916 q = strchr(p, ';');
917 if (q)
918 printf("%.*s", (int)(q - p + 1), p);
919 }
920 }
921 }
922 #endif
923