ulpt.c revision 1.78 1 /* $NetBSD: ulpt.c,v 1.78 2007/03/13 13:51:55 drochner 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 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*
42 * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: ulpt.c,v 1.78 2007/03/13 13:51:55 drochner Exp $");
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
53 #if defined(__NetBSD__) || defined(__OpenBSD__)
54 #include <sys/device.h>
55 #include <sys/ioctl.h>
56 #elif defined(__FreeBSD__)
57 #include <sys/ioccom.h>
58 #include <sys/module.h>
59 #include <sys/bus.h>
60 #endif
61 #include <sys/uio.h>
62 #include <sys/conf.h>
63 #include <sys/vnode.h>
64 #include <sys/syslog.h>
65
66 #include <machine/vmparam.h> /* PAGE_SIZE */
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdevs.h>
72 #include <dev/usb/usb_quirks.h>
73
74 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
75 #define STEP hz/4
76
77 #define LPTPRI (PZERO+8)
78 #define ULPT_BSIZE PAGE_SIZE
79
80 #define ULPT_READS_PER_SEC 5
81 #define ULPT_READ_TIMO 10
82
83 #ifdef ULPT_DEBUG
84 #define DPRINTF(x) if (ulptdebug) logprintf x
85 #define DPRINTFN(n,x) if (ulptdebug>(n)) logprintf x
86 int ulptdebug = 0;
87 #else
88 #define DPRINTF(x)
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;
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 DPRINTFN(10,("ulpt_match\n"));
205
206 if (uaa->class == UICLASS_PRINTER &&
207 uaa->subclass == UISUBCLASS_PRINTER &&
208 (uaa->proto == UIPROTO_PRINTER_UNI ||
209 uaa->proto == UIPROTO_PRINTER_BI ||
210 uaa->proto == UIPROTO_PRINTER_1284))
211 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
212 return (UMATCH_NONE);
213 }
214
215 USB_ATTACH(ulpt)
216 {
217 USB_IFATTACH_START(ulpt, sc, uaa);
218 usbd_device_handle dev = uaa->device;
219 usbd_interface_handle iface = uaa->iface;
220 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
221 const usb_interface_descriptor_t *id;
222 usbd_status err;
223 char *devinfop;
224 usb_endpoint_descriptor_t *ed;
225 u_int8_t epcount;
226 int i, altno;
227 usbd_desc_iter_t iter;
228
229 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
230
231 devinfop = usbd_devinfo_alloc(dev, 0);
232 USB_ATTACH_SETUP;
233 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
234 devinfop, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
235 usbd_devinfo_free(devinfop);
236
237 /* Loop through descriptors looking for a bidir mode. */
238 usb_desc_iter_init(dev, &iter);
239 for (altno = 0;;) {
240 id = (const usb_interface_descriptor_t *)usb_desc_iter_next(&iter);
241 if (!id)
242 break;
243 if (id->bDescriptorType == UDESC_INTERFACE &&
244 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
245 if (id->bInterfaceClass == UICLASS_PRINTER &&
246 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
247 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*||
248 id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/))
249 goto found;
250 altno++;
251 }
252 }
253 id = ifcd; /* not found, use original */
254 found:
255 if (id != ifcd) {
256 /* Found a new bidir setting */
257 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
258 err = usbd_set_interface(iface, altno);
259 if (err) {
260 printf("%s: setting alternate interface failed\n",
261 USBDEVNAME(sc->sc_dev));
262 sc->sc_dying = 1;
263 USB_ATTACH_ERROR_RETURN;
264 }
265 }
266
267 epcount = 0;
268 (void)usbd_endpoint_count(iface, &epcount);
269
270 sc->sc_in = -1;
271 sc->sc_out = -1;
272 for (i = 0; i < epcount; i++) {
273 ed = usbd_interface2endpoint_descriptor(iface, i);
274 if (ed == NULL) {
275 printf("%s: couldn't get ep %d\n",
276 USBDEVNAME(sc->sc_dev), i);
277 USB_ATTACH_ERROR_RETURN;
278 }
279 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
280 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
281 sc->sc_in = ed->bEndpointAddress;
282 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
283 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
284 sc->sc_out = ed->bEndpointAddress;
285 }
286 }
287 if (sc->sc_out == -1) {
288 printf("%s: could not find bulk out endpoint\n",
289 USBDEVNAME(sc->sc_dev));
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 printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
300 sc->sc_in >= 0 ? "bi" : "uni");
301
302 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
303
304 sc->sc_iface = iface;
305 sc->sc_ifaceno = id->bInterfaceNumber;
306 sc->sc_udev = dev;
307
308 #if 0
309 /*
310 * This code is disabled because for some mysterious reason it causes
311 * printing not to work. But only sometimes, and mostly with
312 * UHCI and less often with OHCI. *sigh*
313 */
314 {
315 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
316 usb_device_request_t req;
317 int len, alen;
318
319 req.bmRequestType = UT_READ_CLASS_INTERFACE;
320 req.bRequest = UR_GET_DEVICE_ID;
321 USETW(req.wValue, cd->bConfigurationValue);
322 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
323 USETW(req.wLength, DEVINFOSIZE - 1);
324 err = usbd_do_request_flags(dev, &req, devinfop, USBD_SHORT_XFER_OK,
325 &alen, USBD_DEFAULT_TIMEOUT);
326 if (err) {
327 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
328 } else if (alen <= 2) {
329 printf("%s: empty device id, no printer connected?\n",
330 USBDEVNAME(sc->sc_dev));
331 } else {
332 /* devinfop now contains an IEEE-1284 device ID */
333 len = ((devinfop[0] & 0xff) << 8) | (devinfop[1] & 0xff);
334 if (len > DEVINFOSIZE - 3)
335 len = DEVINFOSIZE - 3;
336 devinfop[len] = 0;
337 printf("%s: device id <", USBDEVNAME(sc->sc_dev));
338 ieee1284_print_id(devinfop+2);
339 printf(">\n");
340 }
341 }
342 #endif
343
344 #if defined(__FreeBSD__)
345 sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
346 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
347 sc->dev_noprime = make_dev(&ulpt_cdevsw,
348 device_get_unit(self)|ULPT_NOPRIME,
349 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
350 #endif
351
352 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
353 USBDEV(sc->sc_dev));
354
355 USB_ATTACH_SUCCESS_RETURN;
356 }
357
358 #if defined(__NetBSD__) || defined(__OpenBSD__)
359 int
360 ulpt_activate(device_ptr_t self, enum devact act)
361 {
362 struct ulpt_softc *sc = (struct ulpt_softc *)self;
363
364 switch (act) {
365 case DVACT_ACTIVATE:
366 return (EOPNOTSUPP);
367
368 case DVACT_DEACTIVATE:
369 sc->sc_dying = 1;
370 break;
371 }
372 return (0);
373 }
374 #endif
375
376 USB_DETACH(ulpt)
377 {
378 USB_DETACH_START(ulpt, sc);
379 int s;
380 #if defined(__NetBSD__) || defined(__OpenBSD__)
381 int maj, mn;
382 #elif defined(__FreeBSD__)
383 struct vnode *vp;
384 #endif
385
386 DPRINTF(("ulpt_detach: sc=%p\n", sc));
387
388 sc->sc_dying = 1;
389 if (sc->sc_out_pipe != NULL)
390 usbd_abort_pipe(sc->sc_out_pipe);
391 if (sc->sc_in_pipe != NULL)
392 usbd_abort_pipe(sc->sc_in_pipe);
393
394 s = splusb();
395 if (--sc->sc_refcnt >= 0) {
396 /* There is noone to wake, aborting the pipe is enough */
397 /* Wait for processes to go away. */
398 usb_detach_wait(USBDEV(sc->sc_dev));
399 }
400 splx(s);
401
402 #if defined(__NetBSD__) || defined(__OpenBSD__)
403 /* locate the major number */
404 #if defined(__NetBSD__)
405 maj = cdevsw_lookup_major(&ulpt_cdevsw);
406 #elif defined(__OpenBSD__)
407 for (maj = 0; maj < nchrdev; maj++)
408 if (cdevsw[maj].d_open == ulptopen)
409 break;
410 #endif
411
412 /* Nuke the vnodes for any open instances (calls close). */
413 mn = device_unit(self);
414 vdevgone(maj, mn, mn, VCHR);
415 vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
416 #elif defined(__FreeBSD__)
417 vp = SLIST_FIRST(&sc->dev->si_hlist);
418 if (vp)
419 VOP_REVOKE(vp, REVOKEALL);
420 vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
421 if (vp)
422 VOP_REVOKE(vp, REVOKEALL);
423
424 destroy_dev(sc->dev);
425 destroy_dev(sc->dev_noprime);
426 #endif
427
428 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
429 USBDEV(sc->sc_dev));
430
431 return (0);
432 }
433
434 int
435 ulpt_status(struct ulpt_softc *sc)
436 {
437 usb_device_request_t req;
438 usbd_status err;
439 u_char status;
440
441 req.bmRequestType = UT_READ_CLASS_INTERFACE;
442 req.bRequest = UR_GET_PORT_STATUS;
443 USETW(req.wValue, 0);
444 USETW(req.wIndex, sc->sc_ifaceno);
445 USETW(req.wLength, 1);
446 err = usbd_do_request(sc->sc_udev, &req, &status);
447 DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
448 if (!err)
449 return (status);
450 else
451 return (0);
452 }
453
454 void
455 ulpt_reset(struct ulpt_softc *sc)
456 {
457 usb_device_request_t req;
458
459 DPRINTFN(1, ("ulpt_reset\n"));
460 req.bRequest = UR_SOFT_RESET;
461 USETW(req.wValue, 0);
462 USETW(req.wIndex, sc->sc_ifaceno);
463 USETW(req.wLength, 0);
464
465 /*
466 * There was a mistake in the USB printer 1.0 spec that gave the
467 * request type as UT_WRITE_CLASS_OTHER; it should have been
468 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
469 * so we try both.
470 */
471 req.bmRequestType = UT_WRITE_CLASS_OTHER;
472 if (usbd_do_request(sc->sc_udev, &req, 0)) { /* 1.0 */
473 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
474 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
475 }
476 }
477
478 #if 0
479 static void
480 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
481 {
482 struct ulpt_softc *sc = priv;
483
484 DPRINTFN(2,("ulpt_input: got some data\n"));
485 /* Do it again. */
486 if (xfer == sc->sc_in_xfer1)
487 usbd_transfer(sc->sc_in_xfer2);
488 else
489 usbd_transfer(sc->sc_in_xfer1);
490 }
491 #endif
492
493 int ulptusein = 1;
494
495 /*
496 * Reset the printer, then wait until it's selected and not busy.
497 */
498 int
499 ulptopen(dev_t dev, int flag, int mode, struct lwp *l)
500 {
501 u_char flags = ULPTFLAGS(dev);
502 struct ulpt_softc *sc;
503 usbd_status err;
504 int spin, error;
505
506 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
507
508 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
509 return (ENXIO);
510
511 if (sc->sc_state)
512 return (EBUSY);
513
514 sc->sc_state = ULPT_INIT;
515 sc->sc_flags = flags;
516 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
517
518 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
519 /* Ignoring these flags might not be a good idea */
520 if ((flags & ~ULPT_NOPRIME) != 0)
521 printf("ulptopen: flags ignored: %b\n", flags,
522 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
523 #endif
524
525
526 error = 0;
527 sc->sc_refcnt++;
528
529 #if 0 /* XXX causes some printers to disconnect */
530 if ((flags & ULPT_NOPRIME) == 0)
531 ulpt_reset(sc);
532 #endif
533
534 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
535 DPRINTF(("ulpt_open: waiting a while\n"));
536 if (spin >= TIMEOUT) {
537 error = EBUSY;
538 sc->sc_state = 0;
539 goto done;
540 }
541
542 /* wait 1/4 second, give up if we get a signal */
543 error = tsleep((void *)sc, LPTPRI | PCATCH, "ulptop", STEP);
544 if (error != EWOULDBLOCK) {
545 sc->sc_state = 0;
546 goto done;
547 }
548
549 if (sc->sc_dying) {
550 error = ENXIO;
551 sc->sc_state = 0;
552 goto done;
553 }
554 }
555
556 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
557 if (err) {
558 error = EIO;
559 goto err0;
560 }
561 sc->sc_out_xfer = usbd_alloc_xfer(sc->sc_udev);
562 if (sc->sc_out_xfer == NULL) {
563 error = ENOMEM;
564 goto err1;
565 }
566 sc->sc_out_buf = usbd_alloc_buffer(sc->sc_out_xfer, ULPT_BSIZE);
567 if (sc->sc_out_buf == NULL) {
568 error = ENOMEM;
569 goto err2;
570 }
571
572 if (ulptusein && sc->sc_in != -1) {
573 DPRINTF(("ulpt_open: open input pipe\n"));
574 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
575 if (err) {
576 error = EIO;
577 goto err2;
578 }
579 sc->sc_in_xfer = usbd_alloc_xfer(sc->sc_udev);
580 if (sc->sc_in_xfer == NULL) {
581 error = ENOMEM;
582 goto err3;
583 }
584 sc->sc_in_buf = usbd_alloc_buffer(sc->sc_in_xfer, ULPT_BSIZE);
585 if (sc->sc_in_buf == NULL) {
586 error = ENOMEM;
587 goto err4;
588 }
589
590 /* If it's not opened for read then set up a reader. */
591 if (!(flag & FREAD)) {
592 DPRINTF(("ulpt_open: start read callout\n"));
593 usb_callout_init(sc->sc_read_callout);
594 usb_callout(sc->sc_read_callout, hz/5, ulpt_tick, sc);
595 sc->sc_has_callout = 1;
596 }
597 }
598
599 sc->sc_state = ULPT_OPEN;
600 goto done;
601
602 err4:
603 usbd_free_xfer(sc->sc_in_xfer);
604 sc->sc_in_xfer = NULL;
605 err3:
606 usbd_close_pipe(sc->sc_in_pipe);
607 sc->sc_in_pipe = NULL;
608 err2:
609 usbd_free_xfer(sc->sc_out_xfer);
610 sc->sc_out_xfer = NULL;
611 err1:
612 usbd_close_pipe(sc->sc_out_pipe);
613 sc->sc_out_pipe = NULL;
614 err0:
615 sc->sc_state = 0;
616
617 done:
618 if (--sc->sc_refcnt < 0)
619 usb_detach_wakeup(USBDEV(sc->sc_dev));
620
621 DPRINTF(("ulptopen: done, error=%d\n", error));
622 return (error);
623 }
624
625 int
626 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
627 {
628 u_char new;
629
630 status = (status ^ LPS_INVERT) & LPS_MASK;
631 new = status & ~sc->sc_laststatus;
632 sc->sc_laststatus = status;
633
634 if (new & LPS_SELECT)
635 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
636 else if (new & LPS_NOPAPER)
637 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
638 else if (new & LPS_NERR)
639 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
640
641 return (status);
642 }
643
644 int
645 ulptclose(dev_t dev, int flag, int mode,
646 struct lwp *l)
647 {
648 struct ulpt_softc *sc;
649
650 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
651
652 if (sc->sc_state != ULPT_OPEN)
653 /* We are being forced to close before the open completed. */
654 return (0);
655
656 if (sc->sc_has_callout) {
657 usb_uncallout(sc->sc_read_callout, ulpt_tick, sc);
658 sc->sc_has_callout = 0;
659 }
660
661 if (sc->sc_out_pipe != NULL) {
662 usbd_abort_pipe(sc->sc_out_pipe);
663 usbd_close_pipe(sc->sc_out_pipe);
664 sc->sc_out_pipe = NULL;
665 }
666 if (sc->sc_out_xfer != NULL) {
667 usbd_free_xfer(sc->sc_out_xfer);
668 sc->sc_out_xfer = NULL;
669 }
670
671 if (sc->sc_in_pipe != NULL) {
672 usbd_abort_pipe(sc->sc_in_pipe);
673 usbd_close_pipe(sc->sc_in_pipe);
674 sc->sc_in_pipe = NULL;
675 }
676 if (sc->sc_in_xfer != NULL) {
677 usbd_free_xfer(sc->sc_in_xfer);
678 sc->sc_in_xfer = NULL;
679 }
680
681 sc->sc_state = 0;
682
683 DPRINTF(("ulptclose: closed\n"));
684 return (0);
685 }
686
687 int
688 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
689 {
690 u_int32_t n;
691 int error = 0;
692 void *bufp;
693 usbd_xfer_handle xfer;
694 usbd_status err;
695
696 DPRINTF(("ulptwrite\n"));
697 xfer = sc->sc_out_xfer;
698 bufp = sc->sc_out_buf;
699 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
700 ulpt_statusmsg(ulpt_status(sc), sc);
701 error = uiomove(bufp, n, uio);
702 if (error)
703 break;
704 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
705 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
706 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
707 if (err) {
708 DPRINTF(("ulptwrite: error=%d\n", err));
709 error = EIO;
710 break;
711 }
712 }
713
714 return (error);
715 }
716
717 int
718 ulptwrite(dev_t dev, struct uio *uio, int flags)
719 {
720 struct ulpt_softc *sc;
721 int error;
722
723 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
724
725 if (sc->sc_dying)
726 return (EIO);
727
728 sc->sc_refcnt++;
729 error = ulpt_do_write(sc, uio, flags);
730 if (--sc->sc_refcnt < 0)
731 usb_detach_wakeup(USBDEV(sc->sc_dev));
732 return (error);
733 }
734
735 int
736 ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags)
737 {
738 u_int32_t n, on;
739 int error = 0;
740 void *bufp;
741 usbd_xfer_handle xfer;
742 usbd_status err;
743
744 DPRINTF(("ulptread\n"));
745
746 if (sc->sc_in_pipe == NULL)
747 return 0;
748
749 xfer = sc->sc_in_xfer;
750 bufp = sc->sc_in_buf;
751 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
752 DPRINTFN(1, ("ulptread: transfer %d bytes\n", n));
753 on = n;
754 err = usbd_bulk_transfer(xfer, sc->sc_in_pipe,
755 USBD_NO_COPY | USBD_SHORT_XFER_OK,
756 USBD_NO_TIMEOUT, bufp, &n, "ulptrd");
757 if (err) {
758 DPRINTF(("ulptread: error=%d\n", err));
759 error = EIO;
760 break;
761 }
762 error = uiomove(bufp, n, uio);
763 if (error)
764 break;
765 if (on != n)
766 break;
767 }
768
769 return (error);
770 }
771
772 int
773 ulptread(dev_t dev, struct uio *uio, int flags)
774 {
775 struct ulpt_softc *sc;
776 int error;
777
778 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
779
780 if (sc->sc_dying)
781 return (EIO);
782
783 sc->sc_refcnt++;
784 error = ulpt_do_read(sc, uio, flags);
785 if (--sc->sc_refcnt < 0)
786 usb_detach_wakeup(USBDEV(sc->sc_dev));
787 return (error);
788 }
789
790 void
791 ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
792 usbd_status status)
793 {
794 usbd_status err;
795 u_int32_t n;
796 usbd_private_handle xsc;
797 struct ulpt_softc *sc;
798
799 usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err);
800 sc = xsc;
801
802 DPRINTFN(1,("ulpt_read_cb: start sc=%p, err=%d n=%d\n", sc, err, n));
803
804 #ifdef ULPT_DEBUG
805 if (!err && n > 0)
806 DPRINTF(("ulpt_tick: discarding %d bytes\n", n));
807 #endif
808 if (!err || err == USBD_TIMEOUT)
809 usb_callout(sc->sc_read_callout, hz / ULPT_READS_PER_SEC,
810 ulpt_tick, sc);
811 }
812
813 void
814 ulpt_tick(void *xsc)
815 {
816 struct ulpt_softc *sc = xsc;
817 usbd_status err;
818
819 if (sc == NULL || sc->sc_dying)
820 return;
821
822 DPRINTFN(1,("ulpt_tick: start sc=%p\n", sc));
823
824 usbd_setup_xfer(sc->sc_in_xfer, sc->sc_in_pipe, sc, sc->sc_in_buf,
825 ULPT_BSIZE, USBD_NO_COPY | USBD_SHORT_XFER_OK,
826 ULPT_READ_TIMO, ulpt_read_cb);
827 err = usbd_transfer(sc->sc_in_xfer);
828 DPRINTFN(1,("ulpt_tick: err=%d\n", err));
829 }
830
831 int
832 ulptioctl(dev_t dev, u_long cmd, void *data,
833 int flag, struct lwp *l)
834 {
835 return ENODEV;
836 }
837
838 #if 0
839 /* XXX This does not belong here. */
840 /*
841 * Print select parts of a IEEE 1284 device ID.
842 */
843 void
844 ieee1284_print_id(char *str)
845 {
846 char *p, *q;
847
848 for (p = str-1; p; p = strchr(p, ';')) {
849 p++; /* skip ';' */
850 if (strncmp(p, "MFG:", 4) == 0 ||
851 strncmp(p, "MANUFACTURER:", 14) == 0 ||
852 strncmp(p, "MDL:", 4) == 0 ||
853 strncmp(p, "MODEL:", 6) == 0) {
854 q = strchr(p, ';');
855 if (q)
856 printf("%.*s", (int)(q - p + 1), p);
857 }
858 }
859 }
860 #endif
861
862 #if defined(__FreeBSD__)
863 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
864 #endif
865
866
867
868
869
870 #if 0
871 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
872 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
873 USBD_NO_TIMEOUT, ulpt_input);
874 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
875 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
876 USBD_NO_TIMEOUT, ulpt_input);
877 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
878 #endif
879