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