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