ulpt.c revision 1.79.6.3 1 /* $NetBSD: ulpt.c,v 1.79.6.3 2008/06/02 13:23:55 mjf 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.79.6.3 2008/06/02 13:23:55 mjf 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 #define ULPTRESET(s) ((s) + 64)
197
198
199 USB_DECLARE_DRIVER(ulpt);
200
201 USB_MATCH(ulpt)
202 {
203 USB_IFMATCH_START(ulpt, uaa);
204
205 /* XXX Print something useful, or don't. */
206 DPRINTFN(10,("ulpt_match\n"));
207
208 if (uaa->class == UICLASS_PRINTER &&
209 uaa->subclass == UISUBCLASS_PRINTER &&
210 (uaa->proto == UIPROTO_PRINTER_UNI ||
211 uaa->proto == UIPROTO_PRINTER_BI ||
212 uaa->proto == UIPROTO_PRINTER_1284))
213 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
214 return (UMATCH_NONE);
215 }
216
217 USB_ATTACH(ulpt)
218 {
219 USB_IFATTACH_START(ulpt, sc, uaa);
220 usbd_device_handle dev = uaa->device;
221 usbd_interface_handle iface = uaa->iface;
222 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
223 const usb_interface_descriptor_t *id;
224 usbd_status err;
225 char *devinfop;
226 usb_endpoint_descriptor_t *ed;
227 u_int8_t epcount;
228 int i, altno, maj;
229 usbd_desc_iter_t iter;
230
231 sc->sc_dev = self;
232
233 devinfop = usbd_devinfo_alloc(dev, 0);
234 USB_ATTACH_SETUP;
235 aprint_normal_dev(self, "%s, iclass %d/%d\n",
236 devinfop, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
237 usbd_devinfo_free(devinfop);
238
239 /* Loop through descriptors looking for a bidir mode. */
240 usb_desc_iter_init(dev, &iter);
241 for (altno = 0;;) {
242 id = (const usb_interface_descriptor_t *)usb_desc_iter_next(&iter);
243 if (!id)
244 break;
245 if (id->bDescriptorType == UDESC_INTERFACE &&
246 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
247 if (id->bInterfaceClass == UICLASS_PRINTER &&
248 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
249 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*||
250 id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/))
251 goto found;
252 altno++;
253 }
254 }
255 id = ifcd; /* not found, use original */
256 found:
257 if (id != ifcd) {
258 /* Found a new bidir setting */
259 DPRINTFN(1, ("ulpt_attach: set altno = %d\n", altno));
260 err = usbd_set_interface(iface, altno);
261 if (err) {
262 aprint_error_dev(self,
263 "setting alternate interface failed\n");
264 sc->sc_dying = 1;
265 USB_ATTACH_ERROR_RETURN;
266 }
267 }
268
269 epcount = 0;
270 (void)usbd_endpoint_count(iface, &epcount);
271
272 sc->sc_in = -1;
273 sc->sc_out = -1;
274 for (i = 0; i < epcount; i++) {
275 ed = usbd_interface2endpoint_descriptor(iface, i);
276 if (ed == NULL) {
277 aprint_error_dev(self, "couldn't get ep %d\n", i);
278 USB_ATTACH_ERROR_RETURN;
279 }
280 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
281 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
282 sc->sc_in = ed->bEndpointAddress;
283 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
284 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
285 sc->sc_out = ed->bEndpointAddress;
286 }
287 }
288 if (sc->sc_out == -1) {
289 aprint_error_dev(self, "could not find bulk out endpoint\n");
290 sc->sc_dying = 1;
291 USB_ATTACH_ERROR_RETURN;
292 }
293
294 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
295 /* This device doesn't handle reading properly. */
296 sc->sc_in = -1;
297 }
298
299 aprint_normal_dev(self, "using %s-directional mode\n",
300 sc->sc_in >= 0 ? "bi" : "uni");
301
302 sc->sc_iface = iface;
303 sc->sc_ifaceno = id->bInterfaceNumber;
304 sc->sc_udev = dev;
305
306 #if 0
307 /*
308 * This code is disabled because for some mysterious reason it causes
309 * printing not to work. But only sometimes, and mostly with
310 * UHCI and less often with OHCI. *sigh*
311 */
312 {
313 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
314 usb_device_request_t req;
315 int len, alen;
316
317 req.bmRequestType = UT_READ_CLASS_INTERFACE;
318 req.bRequest = UR_GET_DEVICE_ID;
319 USETW(req.wValue, cd->bConfigurationValue);
320 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
321 USETW(req.wLength, DEVINFOSIZE - 1);
322 err = usbd_do_request_flags(dev, &req, devinfop, USBD_SHORT_XFER_OK,
323 &alen, USBD_DEFAULT_TIMEOUT);
324 if (err) {
325 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
326 } else if (alen <= 2) {
327 printf("%s: empty device id, no printer connected?\n",
328 USBDEVNAME(sc->sc_dev));
329 } else {
330 /* devinfop now contains an IEEE-1284 device ID */
331 len = ((devinfop[0] & 0xff) << 8) | (devinfop[1] & 0xff);
332 if (len > DEVINFOSIZE - 3)
333 len = DEVINFOSIZE - 3;
334 devinfop[len] = 0;
335 printf("%s: device id <", USBDEVNAME(sc->sc_dev));
336 ieee1284_print_id(devinfop+2);
337 printf(">\n");
338 }
339 }
340 #endif
341
342 #if defined(__FreeBSD__)
343 sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
344 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
345 sc->dev_noprime = make_dev(&ulpt_cdevsw,
346 device_get_unit(self)|ULPT_NOPRIME,
347 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
348 #endif
349
350 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
351 USBDEV(sc->sc_dev));
352
353 DPRINTFN(1, ("ulpt_attach: sc=%p in=%d out=%d\n",
354 sc, sc->sc_out, sc->sc_in));
355
356 maj = cdevsw_lookup_major(&ulpt_cdevsw);
357 device_register_name(makedev(maj, device_unit(self)), self, true,
358 DEV_OTHER, "ulpt%d", device_unit(self));
359 device_register_name(makedev(maj, ULPTRESET(device_unit(self))),
360 self, true, DEV_OTHER, "ulpn%d", device_unit(self));
361
362 USB_ATTACH_SUCCESS_RETURN;
363 }
364
365 #if defined(__NetBSD__) || defined(__OpenBSD__)
366 int
367 ulpt_activate(device_ptr_t self, enum devact act)
368 {
369 struct ulpt_softc *sc = device_private(self);
370
371 switch (act) {
372 case DVACT_ACTIVATE:
373 return (EOPNOTSUPP);
374
375 case DVACT_DEACTIVATE:
376 sc->sc_dying = 1;
377 break;
378 }
379 return (0);
380 }
381 #endif
382
383 USB_DETACH(ulpt)
384 {
385 USB_DETACH_START(ulpt, sc);
386 int s;
387 #if defined(__NetBSD__) || defined(__OpenBSD__)
388 int maj, mn;
389 #elif defined(__FreeBSD__)
390 struct vnode *vp;
391 #endif
392
393 DPRINTFN(1, ("ulpt_detach: sc=%p\n", sc));
394
395 device_deregister_all(self);
396
397 sc->sc_dying = 1;
398 if (sc->sc_out_pipe != NULL)
399 usbd_abort_pipe(sc->sc_out_pipe);
400 if (sc->sc_in_pipe != NULL)
401 usbd_abort_pipe(sc->sc_in_pipe);
402
403 s = splusb();
404 if (--sc->sc_refcnt >= 0) {
405 /* There is noone to wake, aborting the pipe is enough */
406 /* Wait for processes to go away. */
407 usb_detach_wait(USBDEV(sc->sc_dev));
408 }
409 splx(s);
410
411 #if defined(__NetBSD__) || defined(__OpenBSD__)
412 /* locate the major number */
413 #if defined(__NetBSD__)
414 maj = cdevsw_lookup_major(&ulpt_cdevsw);
415 #elif defined(__OpenBSD__)
416 for (maj = 0; maj < nchrdev; maj++)
417 if (cdevsw[maj].d_open == ulptopen)
418 break;
419 #endif
420
421 /* Nuke the vnodes for any open instances (calls close). */
422 mn = device_unit(self);
423 vdevgone(maj, mn, mn, VCHR);
424 vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
425 #elif defined(__FreeBSD__)
426 vp = SLIST_FIRST(&sc->dev->si_hlist);
427 if (vp)
428 VOP_REVOKE(vp, REVOKEALL);
429 vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
430 if (vp)
431 VOP_REVOKE(vp, REVOKEALL);
432
433 destroy_dev(sc->dev);
434 destroy_dev(sc->dev_noprime);
435 #endif
436
437 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
438 USBDEV(sc->sc_dev));
439
440 return (0);
441 }
442
443 int
444 ulpt_status(struct ulpt_softc *sc)
445 {
446 usb_device_request_t req;
447 usbd_status err;
448 u_char status;
449
450 req.bmRequestType = UT_READ_CLASS_INTERFACE;
451 req.bRequest = UR_GET_PORT_STATUS;
452 USETW(req.wValue, 0);
453 USETW(req.wIndex, sc->sc_ifaceno);
454 USETW(req.wLength, 1);
455 err = usbd_do_request(sc->sc_udev, &req, &status);
456 DPRINTFN(2, ("ulpt_status: status=0x%02x err=%d\n", status, err));
457 if (!err)
458 return (status);
459 else
460 return (0);
461 }
462
463 void
464 ulpt_reset(struct ulpt_softc *sc)
465 {
466 usb_device_request_t req;
467
468 DPRINTFN(2, ("ulpt_reset\n"));
469 req.bRequest = UR_SOFT_RESET;
470 USETW(req.wValue, 0);
471 USETW(req.wIndex, sc->sc_ifaceno);
472 USETW(req.wLength, 0);
473
474 /*
475 * There was a mistake in the USB printer 1.0 spec that gave the
476 * request type as UT_WRITE_CLASS_OTHER; it should have been
477 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
478 * so we try both.
479 */
480 req.bmRequestType = UT_WRITE_CLASS_OTHER;
481 if (usbd_do_request(sc->sc_udev, &req, 0)) { /* 1.0 */
482 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
483 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
484 }
485 }
486
487 int ulptusein = 1;
488
489 /*
490 * Reset the printer, then wait until it's selected and not busy.
491 */
492 int
493 ulptopen(dev_t dev, int flag, int mode, struct lwp *l)
494 {
495 u_char flags = ULPTFLAGS(dev);
496 struct ulpt_softc *sc;
497 usbd_status err;
498 int spin, error;
499
500 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
501
502 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
503 return (ENXIO);
504
505 if (sc->sc_state)
506 return (EBUSY);
507
508 sc->sc_state = ULPT_INIT;
509 sc->sc_flags = flags;
510 DPRINTFN(2, ("ulptopen: flags=0x%x\n", (unsigned)flags));
511
512 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
513 /* Ignoring these flags might not be a good idea */
514 if ((flags & ~ULPT_NOPRIME) != 0)
515 printf("ulptopen: flags ignored: %b\n", flags,
516 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
517 #endif
518
519
520 error = 0;
521 sc->sc_refcnt++;
522
523 #if 0 /* XXX causes some printers to disconnect */
524 if ((flags & ULPT_NOPRIME) == 0)
525 ulpt_reset(sc);
526 #endif
527
528 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
529 DPRINTFN(2, ("ulpt_open: waiting a while\n"));
530 if (spin >= TIMEOUT) {
531 error = EBUSY;
532 sc->sc_state = 0;
533 goto done;
534 }
535
536 /* wait 1/4 second, give up if we get a signal */
537 error = tsleep((void *)sc, LPTPRI | PCATCH, "ulptop", STEP);
538 if (error != EWOULDBLOCK) {
539 sc->sc_state = 0;
540 goto done;
541 }
542
543 if (sc->sc_dying) {
544 error = ENXIO;
545 sc->sc_state = 0;
546 goto done;
547 }
548 }
549
550 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
551 if (err) {
552 error = EIO;
553 goto err0;
554 }
555 sc->sc_out_xfer = usbd_alloc_xfer(sc->sc_udev);
556 if (sc->sc_out_xfer == NULL) {
557 error = ENOMEM;
558 goto err1;
559 }
560 sc->sc_out_buf = usbd_alloc_buffer(sc->sc_out_xfer, ULPT_BSIZE);
561 if (sc->sc_out_buf == NULL) {
562 error = ENOMEM;
563 goto err2;
564 }
565
566 if (ulptusein && sc->sc_in != -1) {
567 DPRINTFN(2, ("ulpt_open: opening input pipe %d\n", sc->sc_in));
568 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
569 if (err) {
570 error = EIO;
571 goto err2;
572 }
573 sc->sc_in_xfer = usbd_alloc_xfer(sc->sc_udev);
574 if (sc->sc_in_xfer == NULL) {
575 error = ENOMEM;
576 goto err3;
577 }
578 sc->sc_in_buf = usbd_alloc_buffer(sc->sc_in_xfer, ULPT_BSIZE);
579 if (sc->sc_in_buf == NULL) {
580 error = ENOMEM;
581 goto err4;
582 }
583
584 /* If it's not opened for read then set up a reader. */
585 if (!(flag & FREAD)) {
586 DPRINTFN(2, ("ulpt_open: start read callout\n"));
587 usb_callout_init(sc->sc_read_callout);
588 usb_callout(sc->sc_read_callout, hz/5, ulpt_tick, sc);
589 sc->sc_has_callout = 1;
590 }
591 }
592
593 sc->sc_state = ULPT_OPEN;
594 goto done;
595
596 err4:
597 usbd_free_xfer(sc->sc_in_xfer);
598 sc->sc_in_xfer = NULL;
599 err3:
600 usbd_close_pipe(sc->sc_in_pipe);
601 sc->sc_in_pipe = NULL;
602 err2:
603 usbd_free_xfer(sc->sc_out_xfer);
604 sc->sc_out_xfer = NULL;
605 err1:
606 usbd_close_pipe(sc->sc_out_pipe);
607 sc->sc_out_pipe = NULL;
608 err0:
609 sc->sc_state = 0;
610
611 done:
612 if (--sc->sc_refcnt < 0)
613 usb_detach_wakeup(USBDEV(sc->sc_dev));
614
615 DPRINTFN(2, ("ulptopen: done, error=%d\n", error));
616 return (error);
617 }
618
619 /*
620 * XXX Document return value semantics.
621 */
622 int
623 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
624 {
625 u_char new;
626
627 status = (status ^ LPS_INVERT) & LPS_MASK;
628 new = status & ~sc->sc_laststatus;
629 sc->sc_laststatus = status;
630
631 if (new & LPS_SELECT)
632 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
633 if (new & LPS_NOPAPER)
634 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
635 if (new & LPS_NERR)
636 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
637
638 return (status);
639 }
640
641 int
642 ulptclose(dev_t dev, int flag, int mode,
643 struct lwp *l)
644 {
645 struct ulpt_softc *sc;
646
647 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
648
649 if (sc->sc_state != ULPT_OPEN)
650 /* We are being forced to close before the open completed. */
651 return (0);
652
653 if (sc->sc_has_callout) {
654 DPRINTFN(2, ("ulptclose: stopping read callout\n"));
655 usb_uncallout(sc->sc_read_callout, ulpt_tick, sc);
656 sc->sc_has_callout = 0;
657 }
658
659 if (sc->sc_out_pipe != NULL) {
660 usbd_abort_pipe(sc->sc_out_pipe);
661 usbd_close_pipe(sc->sc_out_pipe);
662 sc->sc_out_pipe = NULL;
663 }
664 if (sc->sc_out_xfer != NULL) {
665 usbd_free_xfer(sc->sc_out_xfer);
666 sc->sc_out_xfer = NULL;
667 }
668
669 if (sc->sc_in_pipe != NULL) {
670 usbd_abort_pipe(sc->sc_in_pipe);
671 usbd_close_pipe(sc->sc_in_pipe);
672 sc->sc_in_pipe = NULL;
673 }
674 if (sc->sc_in_xfer != NULL) {
675 usbd_free_xfer(sc->sc_in_xfer);
676 sc->sc_in_xfer = NULL;
677 }
678
679 sc->sc_state = 0;
680
681 DPRINTFN(2, ("ulptclose: closed\n"));
682 return (0);
683 }
684
685 int
686 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
687 {
688 u_int32_t n;
689 int error = 0;
690 void *bufp;
691 usbd_xfer_handle xfer;
692 usbd_status err;
693
694 DPRINTFN(3, ("ulptwrite\n"));
695 xfer = sc->sc_out_xfer;
696 bufp = sc->sc_out_buf;
697 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
698 ulpt_statusmsg(ulpt_status(sc), sc);
699 error = uiomove(bufp, n, uio);
700 if (error)
701 break;
702 DPRINTFN(4, ("ulptwrite: transfer %d bytes\n", n));
703 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
704 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
705 if (err) {
706 DPRINTFN(3, ("ulptwrite: error=%d\n", err));
707 error = EIO;
708 break;
709 }
710 }
711
712 return (error);
713 }
714
715 int
716 ulptwrite(dev_t dev, struct uio *uio, int flags)
717 {
718 struct ulpt_softc *sc;
719 int error;
720
721 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
722
723 if (sc->sc_dying)
724 return (EIO);
725
726 sc->sc_refcnt++;
727 error = ulpt_do_write(sc, uio, flags);
728 if (--sc->sc_refcnt < 0)
729 usb_detach_wakeup(USBDEV(sc->sc_dev));
730 return (error);
731 }
732
733 /*
734 * Perform a read operation according to the given uio.
735 * This should respect nonblocking I/O status.
736 *
737 * XXX Doing a short read when more data is available seems to be
738 * problematic. See
739 * http://www.freebsd.org/cgi/query-pr.cgi?pr=91538&cat= for a fix.
740 * However, this will be unnecessary given a proper fix for the next
741 * problem, and most actual callers read a lot.
742 *
743 * XXX This code should interact properly with select/poll, and that
744 * requires the USB transactions to be queued and function before the
745 * user does a read. Read will then consume data from a buffer, and
746 * not interact with the device. See ucom.c for an example of how to
747 * do this.
748 */
749 int
750 ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags)
751 {
752 u_int32_t n, nread, nreq;
753 int error = 0, nonblocking, timeout;
754 void *bufp;
755 usbd_xfer_handle xfer;
756 usbd_status err = USBD_NORMAL_COMPLETION;
757
758 /* XXX Resolve with background reader process. KASSERT? */
759 if (sc->sc_in_pipe == NULL)
760 return EIO;
761
762 if (flags & IO_NDELAY)
763 nonblocking = 1;
764 else
765 nonblocking = 0;
766
767 if (nonblocking)
768 timeout = USBD_DEFAULT_TIMEOUT; /* 5 ms */
769 else
770 timeout = USBD_NO_TIMEOUT;
771
772 DPRINTFN(3, ("ulptread nonblocking=%d uio_reside=%d timeout=%d\n",
773 nonblocking, uio->uio_resid, timeout));
774
775 xfer = sc->sc_in_xfer;
776 bufp = sc->sc_in_buf;
777 nread = 0;
778 while ((nreq = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
779 KASSERT(error == 0);
780 if (error != 0) {
781 printf("ulptread: pre-switch error %d != 0", error);
782 goto done;
783 }
784
785 /*
786 * XXX Even with the short timeout, this will tsleep,
787 * but it should be adequately prompt in practice.
788 */
789 n = nreq;
790 DPRINTFN(4, ("ulptread: transfer %d bytes, nonblocking=%d timeout=%d\n",
791 n, nonblocking, timeout));
792 err = usbd_bulk_transfer(xfer, sc->sc_in_pipe,
793 USBD_NO_COPY | USBD_SHORT_XFER_OK,
794 timeout, bufp, &n, "ulptrd");
795
796 DPRINTFN(4, ("ulptread: transfer complete nreq %d n %d nread %d err %d\n",
797 nreq, n, nread, err));
798 /*
799 * Process "err" return, jumping to done if we set "error".
800 */
801 switch (err) {
802 case USBD_NORMAL_COMPLETION:
803 if (n == 0) {
804 DPRINTFN(3, ("ulptread: NORMAL n==0\n"));
805 }
806 break;
807
808 case USBD_SHORT_XFER:
809 /* We said SHORT_XFER_OK, so shouldn't happen. */
810 DPRINTFN(3, ("ulptread: SHORT n=%d\n", n));
811 break;
812
813 case USBD_TIMEOUT:
814 if (nonblocking == 0) {
815 /* XXX Cannot happen; perhaps KASSERT. */
816 printf("ulptread: timeout in blocking mode\n");
817 error = EIO;
818 goto done;
819 }
820
821 DPRINTFN(3, ("ulptread: TIMEOUT n %d nread %d error %d\n",
822 n, nread, error));
823 /*
824 * Don't set error until we understand why
825 * this happens.
826 */
827 break;
828
829 case USBD_INTERRUPTED:
830 /*
831 * The tsleep in usbd_bulk_transfer was
832 * interrupted. Reflect it to the caller so
833 * that reading can be interrupted.
834 */
835 error = EINTR;
836 DPRINTFN(3, ("ulptread: EINTR error %d\n", error));
837 goto done;
838 break;
839
840 default:
841 /* Assume all other return codes are really errors. */
842 error = EIO;
843 DPRINTFN(3, ("ulptread: n %d err %d error %d\n",
844 n, err, error));
845 goto done;
846 break;
847 }
848 /* XXX KASSERT */
849 if (error != 0) {
850 printf("ulptread: post-switch error %d != 0", error);
851 goto done;
852 }
853
854 if (n > 0) {
855 /*
856 * Record progress to enable later choosing
857 * between short reads and EWOULDBLOCK.
858 */
859 nread += n;
860
861 /* Copy to userspace, giving up on any error. */
862 error = uiomove(bufp, n, uio);
863 if (error != 0)
864 break;
865 } else {
866 /*
867 * We read 0 bytes, and therefore are done,
868 * even if we aren't in nonblocking mode.
869 */
870 if (error == 0 && nread == 0)
871 error = EWOULDBLOCK;
872 DPRINTFN(3, ("ulptread: read 0=>done error %d\n",
873 error));
874 goto done;
875 }
876
877 /*
878 * A short transfer indicates no more data will be
879 * forthcoming. Terminate this read regardless of
880 * whether we are in nonblocking mode. XXX Reconsider
881 * for blocking mode; maybe we should continue to
882 * block, but maybe it just doesn't make senes to do
883 * blocking reads from devices like this.
884 */
885 if (err == USBD_SHORT_XFER) {
886 DPRINTFN(3, ("ulptread: SHORT=>done n %d nread %d err %d error %d\n",
887 n, nread, err, error));
888 break;
889 }
890 }
891
892 done:
893 DPRINTFN(3, ("ulptread: finished n %d nread %d err %d error %d\n",
894 n, nread, err, error));
895 return (error);
896 }
897
898 int
899 ulptread(dev_t dev, struct uio *uio, int flags)
900 {
901 struct ulpt_softc *sc;
902 int error;
903
904 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
905
906 if (sc->sc_dying)
907 return (EIO);
908
909 sc->sc_refcnt++;
910 error = ulpt_do_read(sc, uio, flags);
911 if (--sc->sc_refcnt < 0)
912 usb_detach_wakeup(USBDEV(sc->sc_dev));
913 return (error);
914 }
915
916 void
917 ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
918 usbd_status status)
919 {
920 usbd_status err;
921 u_int32_t n;
922 usbd_private_handle xsc;
923 struct ulpt_softc *sc;
924
925 usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err);
926 sc = xsc;
927
928 DPRINTFN(4, ("ulpt_read_cb: start sc=%p, err=%d n=%d\n", sc, err, n));
929
930 #ifdef ULPT_DEBUG
931 if (!err && n > 0)
932 DPRINTFN(3, ("ulpt_tick: discarding %d bytes\n", n));
933 #endif
934 if (!err || err == USBD_TIMEOUT)
935 usb_callout(sc->sc_read_callout, hz / ULPT_READS_PER_SEC,
936 ulpt_tick, sc);
937 }
938
939 /*
940 * For devices which are not opened for reading, this function is
941 * called continuously to start read bulk transfers to avoid the
942 * printer overflowing its output buffer.
943 *
944 * XXX This should be adapted for continuous reads to allow select to
945 * work; see do_ulpt_read().
946 */
947 void
948 ulpt_tick(void *xsc)
949 {
950 struct ulpt_softc *sc = xsc;
951 usbd_status err;
952
953 if (sc == NULL || sc->sc_dying)
954 return;
955
956 usbd_setup_xfer(sc->sc_in_xfer, sc->sc_in_pipe, sc, sc->sc_in_buf,
957 ULPT_BSIZE, USBD_NO_COPY | USBD_SHORT_XFER_OK,
958 ULPT_READ_TIMO, ulpt_read_cb);
959 err = usbd_transfer(sc->sc_in_xfer);
960 DPRINTFN(3, ("ulpt_tick: sc=%p err=%d\n", sc, err));
961 }
962
963 int
964 ulptioctl(dev_t dev, u_long cmd, void *data,
965 int flag, struct lwp *l)
966 {
967 struct ulpt_softc *sc;
968
969 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
970
971 switch (cmd) {
972 case FIONBIO:
973 return 0;
974 }
975
976 return ENODEV;
977 }
978
979 #if 0
980 /* XXX This does not belong here. */
981 /*
982 * Print select parts of a IEEE 1284 device ID.
983 */
984 void
985 ieee1284_print_id(char *str)
986 {
987 char *p, *q;
988
989 for (p = str-1; p; p = strchr(p, ';')) {
990 p++; /* skip ';' */
991 if (strncmp(p, "MFG:", 4) == 0 ||
992 strncmp(p, "MANUFACTURER:", 14) == 0 ||
993 strncmp(p, "MDL:", 4) == 0 ||
994 strncmp(p, "MODEL:", 6) == 0) {
995 q = strchr(p, ';');
996 if (q)
997 printf("%.*s", (int)(q - p + 1), p);
998 }
999 }
1000 }
1001 #endif
1002
1003 #if defined(__FreeBSD__)
1004 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
1005 #endif
1006