ulpt.c revision 1.63 1 /* $NetBSD: ulpt.c,v 1.63 2004/06/14 13:52:55 augustss 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.63 2004/06/14 13:52:55 augustss 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,
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_MATCH_START(ulpt, uaa);
203 usb_interface_descriptor_t *id;
204
205 DPRINTFN(10,("ulpt_match\n"));
206 if (uaa->iface == NULL)
207 return (UMATCH_NONE);
208 id = usbd_get_interface_descriptor(uaa->iface);
209 if (id != NULL &&
210 id->bInterfaceClass == UICLASS_PRINTER &&
211 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
212 (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
213 id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
214 id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
215 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
216 return (UMATCH_NONE);
217 }
218
219 USB_ATTACH(ulpt)
220 {
221 USB_ATTACH_START(ulpt, sc, uaa);
222 usbd_device_handle dev = uaa->device;
223 usbd_interface_handle iface = uaa->iface;
224 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
225 usb_interface_descriptor_t *id, *iend;
226 usb_config_descriptor_t *cdesc;
227 usbd_status err;
228 char devinfo[1024];
229 usb_endpoint_descriptor_t *ed;
230 u_int8_t epcount;
231 int i, altno;
232
233 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
234 usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
235 USB_ATTACH_SETUP;
236 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
237 devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
238
239 /* XXX
240 * Stepping through the alternate settings needs to be abstracted out.
241 */
242 cdesc = usbd_get_config_descriptor(dev);
243 if (cdesc == NULL) {
244 printf("%s: failed to get configuration descriptor\n",
245 USBDEVNAME(sc->sc_dev));
246 USB_ATTACH_ERROR_RETURN;
247 }
248 iend = (usb_interface_descriptor_t *)
249 ((char *)cdesc + UGETW(cdesc->wTotalLength));
250 #ifdef DIAGNOSTIC
251 if (ifcd < (usb_interface_descriptor_t *)cdesc ||
252 ifcd >= iend)
253 panic("ulpt: iface desc out of range");
254 #endif
255
256 /* Step through all the descriptors looking for bidir mode */
257 for (id = ifcd, altno = 0;
258 id < iend;
259 id = (void *)((char *)id + id->bLength)) {
260 if (id->bDescriptorType == UDESC_INTERFACE &&
261 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
262 if (id->bInterfaceClass == UICLASS_PRINTER &&
263 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
264 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*||
265 id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/))
266 goto found;
267 altno++;
268 }
269 }
270 id = ifcd; /* not found, use original */
271 found:
272 if (id != ifcd) {
273 /* Found a new bidir setting */
274 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
275 err = usbd_set_interface(iface, altno);
276 if (err) {
277 printf("%s: setting alternate interface failed\n",
278 USBDEVNAME(sc->sc_dev));
279 sc->sc_dying = 1;
280 USB_ATTACH_ERROR_RETURN;
281 }
282 }
283
284 epcount = 0;
285 (void)usbd_endpoint_count(iface, &epcount);
286
287 sc->sc_in = -1;
288 sc->sc_out = -1;
289 for (i = 0; i < epcount; i++) {
290 ed = usbd_interface2endpoint_descriptor(iface, i);
291 if (ed == NULL) {
292 printf("%s: couldn't get ep %d\n",
293 USBDEVNAME(sc->sc_dev), i);
294 USB_ATTACH_ERROR_RETURN;
295 }
296 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
297 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
298 sc->sc_in = ed->bEndpointAddress;
299 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
300 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
301 sc->sc_out = ed->bEndpointAddress;
302 }
303 }
304 if (sc->sc_out == -1) {
305 printf("%s: could not find bulk out endpoint\n",
306 USBDEVNAME(sc->sc_dev));
307 sc->sc_dying = 1;
308 USB_ATTACH_ERROR_RETURN;
309 }
310
311 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
312 /* This device doesn't handle reading properly. */
313 sc->sc_in = -1;
314 }
315
316 printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
317 sc->sc_in >= 0 ? "bi" : "uni");
318
319 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
320
321 sc->sc_iface = iface;
322 sc->sc_ifaceno = id->bInterfaceNumber;
323 sc->sc_udev = dev;
324
325 #if 0
326 /*
327 * This code is disabled because for some mysterious reason it causes
328 * printing not to work. But only sometimes, and mostly with
329 * UHCI and less often with OHCI. *sigh*
330 */
331 {
332 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
333 usb_device_request_t req;
334 int len, alen;
335
336 req.bmRequestType = UT_READ_CLASS_INTERFACE;
337 req.bRequest = UR_GET_DEVICE_ID;
338 USETW(req.wValue, cd->bConfigurationValue);
339 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
340 USETW(req.wLength, sizeof devinfo - 1);
341 err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
342 &alen, USBD_DEFAULT_TIMEOUT);
343 if (err) {
344 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
345 } else if (alen <= 2) {
346 printf("%s: empty device id, no printer connected?\n",
347 USBDEVNAME(sc->sc_dev));
348 } else {
349 /* devinfo now contains an IEEE-1284 device ID */
350 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
351 if (len > sizeof devinfo - 3)
352 len = sizeof devinfo - 3;
353 devinfo[len] = 0;
354 printf("%s: device id <", USBDEVNAME(sc->sc_dev));
355 ieee1284_print_id(devinfo+2);
356 printf(">\n");
357 }
358 }
359 #endif
360
361 #if defined(__FreeBSD__)
362 sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
363 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
364 sc->dev_noprime = make_dev(&ulpt_cdevsw,
365 device_get_unit(self)|ULPT_NOPRIME,
366 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
367 #endif
368
369 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
370 USBDEV(sc->sc_dev));
371
372 USB_ATTACH_SUCCESS_RETURN;
373 }
374
375 #if defined(__NetBSD__) || defined(__OpenBSD__)
376 int
377 ulpt_activate(device_ptr_t self, enum devact act)
378 {
379 struct ulpt_softc *sc = (struct ulpt_softc *)self;
380
381 switch (act) {
382 case DVACT_ACTIVATE:
383 return (EOPNOTSUPP);
384
385 case DVACT_DEACTIVATE:
386 sc->sc_dying = 1;
387 break;
388 }
389 return (0);
390 }
391 #endif
392
393 USB_DETACH(ulpt)
394 {
395 USB_DETACH_START(ulpt, sc);
396 int s;
397 #if defined(__NetBSD__) || defined(__OpenBSD__)
398 int maj, mn;
399 #elif defined(__FreeBSD__)
400 struct vnode *vp;
401 #endif
402
403 DPRINTF(("ulpt_detach: sc=%p\n", sc));
404
405 sc->sc_dying = 1;
406 if (sc->sc_out_pipe != NULL)
407 usbd_abort_pipe(sc->sc_out_pipe);
408 if (sc->sc_in_pipe != NULL)
409 usbd_abort_pipe(sc->sc_in_pipe);
410
411 s = splusb();
412 if (--sc->sc_refcnt >= 0) {
413 /* There is noone to wake, aborting the pipe is enough */
414 /* Wait for processes to go away. */
415 usb_detach_wait(USBDEV(sc->sc_dev));
416 }
417 splx(s);
418
419 #if defined(__NetBSD__) || defined(__OpenBSD__)
420 /* locate the major number */
421 #if defined(__NetBSD__)
422 maj = cdevsw_lookup_major(&ulpt_cdevsw);
423 #elif defined(__OpenBSD__)
424 for (maj = 0; maj < nchrdev; maj++)
425 if (cdevsw[maj].d_open == ulptopen)
426 break;
427 #endif
428
429 /* Nuke the vnodes for any open instances (calls close). */
430 mn = self->dv_unit;
431 vdevgone(maj, mn, mn, VCHR);
432 vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
433 #elif defined(__FreeBSD__)
434 vp = SLIST_FIRST(&sc->dev->si_hlist);
435 if (vp)
436 VOP_REVOKE(vp, REVOKEALL);
437 vp = SLIST_FIRST(&sc->dev_noprime->si_hlist);
438 if (vp)
439 VOP_REVOKE(vp, REVOKEALL);
440
441 destroy_dev(sc->dev);
442 destroy_dev(sc->dev_noprime);
443 #endif
444
445 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
446 USBDEV(sc->sc_dev));
447
448 return (0);
449 }
450
451 int
452 ulpt_status(struct ulpt_softc *sc)
453 {
454 usb_device_request_t req;
455 usbd_status err;
456 u_char status;
457
458 req.bmRequestType = UT_READ_CLASS_INTERFACE;
459 req.bRequest = UR_GET_PORT_STATUS;
460 USETW(req.wValue, 0);
461 USETW(req.wIndex, sc->sc_ifaceno);
462 USETW(req.wLength, 1);
463 err = usbd_do_request(sc->sc_udev, &req, &status);
464 DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
465 if (!err)
466 return (status);
467 else
468 return (0);
469 }
470
471 void
472 ulpt_reset(struct ulpt_softc *sc)
473 {
474 usb_device_request_t req;
475
476 DPRINTFN(1, ("ulpt_reset\n"));
477 req.bRequest = UR_SOFT_RESET;
478 USETW(req.wValue, 0);
479 USETW(req.wIndex, sc->sc_ifaceno);
480 USETW(req.wLength, 0);
481
482 /*
483 * There was a mistake in the USB printer 1.0 spec that gave the
484 * request type as UT_WRITE_CLASS_OTHER; it should have been
485 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
486 * so we try both.
487 */
488 req.bmRequestType = UT_WRITE_CLASS_OTHER;
489 if (usbd_do_request(sc->sc_udev, &req, 0)) { /* 1.0 */
490 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
491 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
492 }
493 }
494
495 #if 0
496 static void
497 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
498 {
499 struct ulpt_softc *sc = priv;
500
501 DPRINTFN(2,("ulpt_input: got some data\n"));
502 /* Do it again. */
503 if (xfer == sc->sc_in_xfer1)
504 usbd_transfer(sc->sc_in_xfer2);
505 else
506 usbd_transfer(sc->sc_in_xfer1);
507 }
508 #endif
509
510 int ulptusein = 1;
511
512 /*
513 * Reset the printer, then wait until it's selected and not busy.
514 */
515 int
516 ulptopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
517 {
518 u_char flags = ULPTFLAGS(dev);
519 struct ulpt_softc *sc;
520 usbd_status err;
521 int spin, error;
522
523 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
524
525 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
526 return (ENXIO);
527
528 if (sc->sc_state)
529 return (EBUSY);
530
531 sc->sc_state = ULPT_INIT;
532 sc->sc_flags = flags;
533 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
534
535 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
536 /* Ignoring these flags might not be a good idea */
537 if ((flags & ~ULPT_NOPRIME) != 0)
538 printf("ulptopen: flags ignored: %b\n", flags,
539 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
540 #endif
541
542
543 error = 0;
544 sc->sc_refcnt++;
545
546 if ((flags & ULPT_NOPRIME) == 0)
547 ulpt_reset(sc);
548
549 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
550 DPRINTF(("ulpt_open: waiting a while\n"));
551 if (spin >= TIMEOUT) {
552 error = EBUSY;
553 sc->sc_state = 0;
554 goto done;
555 }
556
557 /* wait 1/4 second, give up if we get a signal */
558 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
559 if (error != EWOULDBLOCK) {
560 sc->sc_state = 0;
561 goto done;
562 }
563
564 if (sc->sc_dying) {
565 error = ENXIO;
566 sc->sc_state = 0;
567 goto done;
568 }
569 }
570
571 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
572 if (err) {
573 error = EIO;
574 goto err0;
575 }
576 sc->sc_out_xfer = usbd_alloc_xfer(sc->sc_udev);
577 if (sc->sc_out_xfer == NULL) {
578 error = ENOMEM;
579 goto err1;
580 }
581 sc->sc_out_buf = usbd_alloc_buffer(sc->sc_out_xfer, ULPT_BSIZE);
582 if (sc->sc_out_buf == NULL) {
583 error = ENOMEM;
584 goto err2;
585 }
586
587 if (ulptusein && sc->sc_in != -1) {
588 DPRINTF(("ulpt_open: open input pipe\n"));
589 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
590 if (err) {
591 error = EIO;
592 goto err2;
593 }
594 sc->sc_in_xfer = usbd_alloc_xfer(sc->sc_udev);
595 if (sc->sc_in_xfer == NULL) {
596 error = ENOMEM;
597 goto err3;
598 }
599 sc->sc_in_buf = usbd_alloc_buffer(sc->sc_in_xfer, ULPT_BSIZE);
600 if (sc->sc_in_buf == NULL) {
601 error = ENOMEM;
602 goto err4;
603 }
604
605 /* If it's not opened for read the set up a reader. */
606 if (!(flags & FREAD)) {
607 DPRINTF(("ulpt_open: start read callout\n"));
608 usb_callout_init(sc->sc_read_callout);
609 usb_callout(sc->sc_read_callout, hz/5, ulpt_tick, sc);
610 sc->sc_has_callout = 1;
611 }
612 }
613
614 sc->sc_state = ULPT_OPEN;
615 goto done;
616
617 err4:
618 usbd_free_xfer(sc->sc_in_xfer);
619 sc->sc_in_xfer = NULL;
620 err3:
621 usbd_close_pipe(sc->sc_in_pipe);
622 sc->sc_in_pipe = NULL;
623 err2:
624 usbd_free_xfer(sc->sc_out_xfer);
625 sc->sc_out_xfer = NULL;
626 err1:
627 usbd_close_pipe(sc->sc_out_pipe);
628 sc->sc_out_pipe = NULL;
629 err0:
630 sc->sc_state = 0;
631
632 done:
633 if (--sc->sc_refcnt < 0)
634 usb_detach_wakeup(USBDEV(sc->sc_dev));
635
636 DPRINTF(("ulptopen: done, error=%d\n", error));
637 return (error);
638 }
639
640 int
641 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
642 {
643 u_char new;
644
645 status = (status ^ LPS_INVERT) & LPS_MASK;
646 new = status & ~sc->sc_laststatus;
647 sc->sc_laststatus = status;
648
649 if (new & LPS_SELECT)
650 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
651 else if (new & LPS_NOPAPER)
652 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
653 else if (new & LPS_NERR)
654 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
655
656 return (status);
657 }
658
659 int
660 ulptclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
661 {
662 struct ulpt_softc *sc;
663
664 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
665
666 if (sc->sc_state != ULPT_OPEN)
667 /* We are being forced to close before the open completed. */
668 return (0);
669
670 if (sc->sc_has_callout) {
671 usb_uncallout(sc->sc_read_callout, ulpt_tick, sc);
672 sc->sc_has_callout = 0;
673 }
674
675 if (sc->sc_out_pipe != NULL) {
676 usbd_abort_pipe(sc->sc_out_pipe);
677 usbd_close_pipe(sc->sc_out_pipe);
678 sc->sc_out_pipe = NULL;
679 }
680 if (sc->sc_out_xfer != NULL) {
681 usbd_free_xfer(sc->sc_out_xfer);
682 sc->sc_out_xfer = NULL;
683 }
684
685 if (sc->sc_in_pipe != NULL) {
686 usbd_abort_pipe(sc->sc_in_pipe);
687 usbd_close_pipe(sc->sc_in_pipe);
688 sc->sc_in_pipe = NULL;
689 }
690 if (sc->sc_in_xfer != NULL) {
691 usbd_free_xfer(sc->sc_in_xfer);
692 sc->sc_in_xfer = NULL;
693 }
694
695 sc->sc_state = 0;
696
697 DPRINTF(("ulptclose: closed\n"));
698 return (0);
699 }
700
701 int
702 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
703 {
704 u_int32_t n;
705 int error = 0;
706 void *bufp;
707 usbd_xfer_handle xfer;
708 usbd_status err;
709
710 DPRINTF(("ulptwrite\n"));
711 xfer = sc->sc_out_xfer;
712 bufp = sc->sc_out_buf;
713 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
714 ulpt_statusmsg(ulpt_status(sc), sc);
715 error = uiomove(bufp, n, uio);
716 if (error)
717 break;
718 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
719 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
720 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
721 if (err) {
722 DPRINTF(("ulptwrite: error=%d\n", err));
723 error = EIO;
724 break;
725 }
726 }
727
728 return (error);
729 }
730
731 int
732 ulptwrite(dev_t dev, struct uio *uio, int flags)
733 {
734 struct ulpt_softc *sc;
735 int error;
736
737 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
738
739 if (sc->sc_dying)
740 return (EIO);
741
742 sc->sc_refcnt++;
743 error = ulpt_do_write(sc, uio, flags);
744 if (--sc->sc_refcnt < 0)
745 usb_detach_wakeup(USBDEV(sc->sc_dev));
746 return (error);
747 }
748
749 int
750 ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags)
751 {
752 u_int32_t n, on;
753 int error = 0;
754 void *bufp;
755 usbd_xfer_handle xfer;
756 usbd_status err;
757
758 DPRINTF(("ulptread\n"));
759
760 if (sc->sc_in_pipe == NULL)
761 return 0;
762
763 xfer = sc->sc_in_xfer;
764 bufp = sc->sc_in_buf;
765 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
766 DPRINTFN(1, ("ulptread: transfer %d bytes\n", n));
767 on = n;
768 err = usbd_bulk_transfer(xfer, sc->sc_in_pipe,
769 USBD_NO_COPY | USBD_SHORT_XFER_OK,
770 USBD_NO_TIMEOUT, bufp, &n, "ulptrd");
771 if (err) {
772 DPRINTF(("ulptread: error=%d\n", err));
773 error = EIO;
774 break;
775 }
776 error = uiomove(bufp, n, uio);
777 if (error)
778 break;
779 if (on != n)
780 break;
781 }
782
783 return (error);
784 }
785
786 int
787 ulptread(dev_t dev, struct uio *uio, int flags)
788 {
789 struct ulpt_softc *sc;
790 int error;
791
792 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
793
794 if (sc->sc_dying)
795 return (EIO);
796
797 sc->sc_refcnt++;
798 error = ulpt_do_read(sc, uio, flags);
799 if (--sc->sc_refcnt < 0)
800 usb_detach_wakeup(USBDEV(sc->sc_dev));
801 return (error);
802 }
803
804 void
805 ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
806 usbd_status status)
807 {
808 usbd_status err;
809 u_int32_t n;
810 usbd_private_handle xsc;
811 struct ulpt_softc *sc;
812
813 usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err);
814 sc = xsc;
815
816 DPRINTFN(1,("ulpt_read_cb: start sc=%p, err=%d n=%d\n", sc, err, n));
817
818 #ifdef ULPT_DEBUG
819 if (!err && n > 0)
820 DPRINTF(("ulpt_tick: discarding %d bytes\n", n));
821 #endif
822 if (!err || err == USBD_TIMEOUT)
823 usb_callout(sc->sc_read_callout, hz / ULPT_READS_PER_SEC,
824 ulpt_tick, sc);
825 }
826
827 void
828 ulpt_tick(void *xsc)
829 {
830 struct ulpt_softc *sc = xsc;
831 usbd_status err;
832
833 if (sc == NULL || sc->sc_dying)
834 return;
835
836 DPRINTFN(1,("ulpt_tick: start sc=%p\n", sc));
837
838 usbd_setup_xfer(sc->sc_in_xfer, sc->sc_in_pipe, sc, sc->sc_in_buf,
839 ULPT_BSIZE, USBD_NO_COPY | USBD_SHORT_XFER_OK,
840 ULPT_READ_TIMO, ulpt_read_cb);
841 err = usbd_transfer(sc->sc_in_xfer);
842 DPRINTFN(1,("ulpt_tick: err=%d\n", err));
843 }
844
845 int
846 ulptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
847 {
848 int error = 0;
849
850 switch (cmd) {
851 default:
852 error = ENODEV;
853 }
854
855 return (error);
856 }
857
858 #if 0
859 /* XXX This does not belong here. */
860 /*
861 * Print select parts of a IEEE 1284 device ID.
862 */
863 void
864 ieee1284_print_id(char *str)
865 {
866 char *p, *q;
867
868 for (p = str-1; p; p = strchr(p, ';')) {
869 p++; /* skip ';' */
870 if (strncmp(p, "MFG:", 4) == 0 ||
871 strncmp(p, "MANUFACTURER:", 14) == 0 ||
872 strncmp(p, "MDL:", 4) == 0 ||
873 strncmp(p, "MODEL:", 6) == 0) {
874 q = strchr(p, ';');
875 if (q)
876 printf("%.*s", (int)(q - p + 1), p);
877 }
878 }
879 }
880 #endif
881
882 #if defined(__FreeBSD__)
883 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
884 #endif
885
886
887
888
889
890 #if 0
891 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
892 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
893 USBD_NO_TIMEOUT, ulpt_input);
894 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
895 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
896 USBD_NO_TIMEOUT, ulpt_input);
897 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
898 #endif
899