ulpt.c revision 1.54 1 /* $NetBSD: ulpt.c,v 1.54 2002/09/27 15:37:37 provos 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 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.54 2002/09/27 15:37:37 provos Exp $");
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #if defined(__NetBSD__) || defined(__OpenBSD__)
53 #include <sys/device.h>
54 #include <sys/ioctl.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/ioccom.h>
57 #include <sys/module.h>
58 #include <sys/bus.h>
59 #endif
60 #include <sys/uio.h>
61 #include <sys/conf.h>
62 #include <sys/vnode.h>
63 #include <sys/syslog.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/usbdevs.h>
69 #include <dev/usb/usb_quirks.h>
70
71 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
72 #define STEP hz/4
73
74 #define LPTPRI (PZERO+8)
75 #define ULPT_BSIZE 16384
76
77 #ifdef ULPT_DEBUG
78 #define DPRINTF(x) if (ulptdebug) logprintf x
79 #define DPRINTFN(n,x) if (ulptdebug>(n)) logprintf x
80 int ulptdebug = 0;
81 #else
82 #define DPRINTF(x)
83 #define DPRINTFN(n,x)
84 #endif
85
86 #define UR_GET_DEVICE_ID 0
87 #define UR_GET_PORT_STATUS 1
88 #define UR_SOFT_RESET 2
89
90 #define LPS_NERR 0x08 /* printer no error */
91 #define LPS_SELECT 0x10 /* printer selected */
92 #define LPS_NOPAPER 0x20 /* printer out of paper */
93 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
94 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
95
96 struct ulpt_softc {
97 USBBASEDEVICE sc_dev;
98 usbd_device_handle sc_udev; /* device */
99 usbd_interface_handle sc_iface; /* interface */
100 int sc_ifaceno;
101
102 int sc_out;
103 usbd_pipe_handle sc_out_pipe; /* bulk out pipe */
104
105 int sc_in;
106 usbd_pipe_handle sc_in_pipe; /* bulk in pipe */
107 usbd_xfer_handle sc_in_xfer1;
108 usbd_xfer_handle sc_in_xfer2;
109 u_char sc_junk[64]; /* somewhere to dump input */
110
111 u_char sc_state;
112 #define ULPT_OPEN 0x01 /* device is open */
113 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
114 #define ULPT_INIT 0x04 /* waiting to initialize for open */
115 u_char sc_flags;
116 #define ULPT_NOPRIME 0x40 /* don't prime on open */
117 u_char sc_laststatus;
118
119 int sc_refcnt;
120 u_char sc_dying;
121
122 #if defined(__FreeBSD__)
123 dev_t dev;
124 dev_t dev_noprime;
125 #endif
126 };
127
128 #if defined(__NetBSD__)
129 dev_type_open(ulptopen);
130 dev_type_close(ulptclose);
131 dev_type_write(ulptwrite);
132 dev_type_ioctl(ulptioctl);
133
134 const struct cdevsw ulpt_cdevsw = {
135 ulptopen, ulptclose, noread, ulptwrite, ulptioctl,
136 nostop, notty, nopoll, nommap,
137 };
138 #elif defined(__OpenBSD__)
139 cdev_decl(ulpt);
140 #elif defined(__FreeBSD__)
141 Static d_open_t ulptopen;
142 Static d_close_t ulptclose;
143 Static d_write_t ulptwrite;
144 Static d_ioctl_t ulptioctl;
145
146 #define ULPT_CDEV_MAJOR 113
147
148 Static struct cdevsw ulpt_cdevsw = {
149 /* open */ ulptopen,
150 /* close */ ulptclose,
151 /* read */ noread,
152 /* write */ ulptwrite,
153 /* ioctl */ ulptioctl,
154 /* poll */ nopoll,
155 /* mmap */ nommap,
156 /* strategy */ nostrategy,
157 /* name */ "ulpt",
158 /* maj */ ULPT_CDEV_MAJOR,
159 /* dump */ nodump,
160 /* psize */ nopsize,
161 /* flags */ 0,
162 #if !defined(__FreeBSD__) || (__FreeBSD__ < 5)
163 /* bmaj */ -1
164 #endif
165 };
166 #endif
167
168 void ulpt_disco(void *);
169
170 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
171 int ulpt_status(struct ulpt_softc *);
172 void ulpt_reset(struct ulpt_softc *);
173 int ulpt_statusmsg(u_char, struct ulpt_softc *);
174
175 #if 0
176 void ieee1284_print_id(char *);
177 #endif
178
179 #define ULPTUNIT(s) (minor(s) & 0x1f)
180 #define ULPTFLAGS(s) (minor(s) & 0xe0)
181
182
183 USB_DECLARE_DRIVER(ulpt);
184
185 USB_MATCH(ulpt)
186 {
187 USB_MATCH_START(ulpt, uaa);
188 usb_interface_descriptor_t *id;
189
190 DPRINTFN(10,("ulpt_match\n"));
191 if (uaa->iface == NULL)
192 return (UMATCH_NONE);
193 id = usbd_get_interface_descriptor(uaa->iface);
194 if (id != NULL &&
195 id->bInterfaceClass == UICLASS_PRINTER &&
196 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
197 (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
198 id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
199 id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
200 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
201 return (UMATCH_NONE);
202 }
203
204 USB_ATTACH(ulpt)
205 {
206 USB_ATTACH_START(ulpt, sc, uaa);
207 usbd_device_handle dev = uaa->device;
208 usbd_interface_handle iface = uaa->iface;
209 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
210 usb_interface_descriptor_t *id, *iend;
211 usb_config_descriptor_t *cdesc;
212 usbd_status err;
213 char devinfo[1024];
214 usb_endpoint_descriptor_t *ed;
215 u_int8_t epcount;
216 int i, altno;
217
218 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
219 usbd_devinfo(dev, 0, devinfo);
220 USB_ATTACH_SETUP;
221 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
222 devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
223
224 /* XXX
225 * Stepping through the alternate settings needs to be abstracted out.
226 */
227 cdesc = usbd_get_config_descriptor(dev);
228 if (cdesc == NULL) {
229 printf("%s: failed to get configuration descriptor\n",
230 USBDEVNAME(sc->sc_dev));
231 USB_ATTACH_ERROR_RETURN;
232 }
233 iend = (usb_interface_descriptor_t *)
234 ((char *)cdesc + UGETW(cdesc->wTotalLength));
235 #ifdef DIAGNOSTIC
236 if (ifcd < (usb_interface_descriptor_t *)cdesc ||
237 ifcd >= iend)
238 panic("ulpt: iface desc out of range");
239 #endif
240 /* Step through all the descriptors looking for bidir mode */
241 for (id = ifcd, altno = 0;
242 id < iend;
243 id = (void *)((char *)id + id->bLength)) {
244 if (id->bDescriptorType == UDESC_INTERFACE &&
245 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
246 if (id->bInterfaceClass == UICLASS_PRINTER &&
247 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
248 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
249 id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
250 goto found;
251 altno++;
252 }
253 }
254 id = ifcd; /* not found, use original */
255 found:
256 if (id != ifcd) {
257 /* Found a new bidir setting */
258 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
259 err = usbd_set_interface(iface, altno);
260 if (err) {
261 printf("%s: setting alternate interface failed\n",
262 USBDEVNAME(sc->sc_dev));
263 sc->sc_dying = 1;
264 USB_ATTACH_ERROR_RETURN;
265 }
266 }
267
268 epcount = 0;
269 (void)usbd_endpoint_count(iface, &epcount);
270
271 sc->sc_in = -1;
272 sc->sc_out = -1;
273 for (i = 0; i < epcount; i++) {
274 ed = usbd_interface2endpoint_descriptor(iface, i);
275 if (ed == NULL) {
276 printf("%s: couldn't get ep %d\n",
277 USBDEVNAME(sc->sc_dev), 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 printf("%s: could not find bulk out endpoint\n",
290 USBDEVNAME(sc->sc_dev));
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 printf("%s: using %s-directional mode\n", USBDEVNAME(sc->sc_dev),
301 sc->sc_in >= 0 ? "bi" : "uni");
302
303 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
304
305 sc->sc_iface = iface;
306 sc->sc_ifaceno = id->bInterfaceNumber;
307 sc->sc_udev = dev;
308
309 #if 0
310 /*
311 * This code is disabled because for some mysterious reason it causes
312 * printing not to work. But only sometimes, and mostly with
313 * UHCI and less often with OHCI. *sigh*
314 */
315 {
316 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
317 usb_device_request_t req;
318 int len, alen;
319
320 req.bmRequestType = UT_READ_CLASS_INTERFACE;
321 req.bRequest = UR_GET_DEVICE_ID;
322 USETW(req.wValue, cd->bConfigurationValue);
323 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
324 USETW(req.wLength, sizeof devinfo - 1);
325 err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
326 &alen, USBD_DEFAULT_TIMEOUT);
327 if (err) {
328 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
329 } else if (alen <= 2) {
330 printf("%s: empty device id, no printer connected?\n",
331 USBDEVNAME(sc->sc_dev));
332 } else {
333 /* devinfo now contains an IEEE-1284 device ID */
334 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
335 if (len > sizeof devinfo - 3)
336 len = sizeof devinfo - 3;
337 devinfo[len] = 0;
338 printf("%s: device id <", USBDEVNAME(sc->sc_dev));
339 ieee1284_print_id(devinfo+2);
340 printf(">\n");
341 }
342 }
343 #endif
344
345 #if defined(__FreeBSD__)
346 sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
347 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
348 sc->dev_noprime = make_dev(&ulpt_cdevsw,
349 device_get_unit(self)|ULPT_NOPRIME,
350 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
351 #endif
352
353 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
354 USBDEV(sc->sc_dev));
355
356 USB_ATTACH_SUCCESS_RETURN;
357 }
358
359 #if defined(__NetBSD__) || defined(__OpenBSD__)
360 int
361 ulpt_activate(device_ptr_t self, enum devact act)
362 {
363 struct ulpt_softc *sc = (struct ulpt_softc *)self;
364
365 switch (act) {
366 case DVACT_ACTIVATE:
367 return (EOPNOTSUPP);
368
369 case DVACT_DEACTIVATE:
370 sc->sc_dying = 1;
371 break;
372 }
373 return (0);
374 }
375 #endif
376
377 USB_DETACH(ulpt)
378 {
379 USB_DETACH_START(ulpt, sc);
380 int s;
381 #if defined(__NetBSD__) || defined(__OpenBSD__)
382 int maj, mn;
383 #elif defined(__FreeBSD__)
384 struct vnode *vp;
385 #endif
386
387 DPRINTF(("ulpt_detach: sc=%p\n", sc));
388
389 sc->sc_dying = 1;
390 if (sc->sc_out_pipe != NULL)
391 usbd_abort_pipe(sc->sc_out_pipe);
392 if (sc->sc_in_pipe != NULL)
393 usbd_abort_pipe(sc->sc_in_pipe);
394
395 s = splusb();
396 if (--sc->sc_refcnt >= 0) {
397 /* There is noone to wake, aborting the pipe is enough */
398 /* Wait for processes to go away. */
399 usb_detach_wait(USBDEV(sc->sc_dev));
400 }
401 splx(s);
402
403 #if defined(__NetBSD__) || defined(__OpenBSD__)
404 /* locate the major number */
405 #if defined(__NetBSD__)
406 maj = cdevsw_lookup_major(&ulpt_cdevsw);
407 #elif defined(__OpenBSD__)
408 for (maj = 0; maj < nchrdev; maj++)
409 if (cdevsw[maj].d_open == ulptopen)
410 break;
411 #endif
412
413 /* Nuke the vnodes for any open instances (calls close). */
414 mn = self->dv_unit;
415 vdevgone(maj, mn, mn, 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 static void
479 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
480 {
481 struct ulpt_softc *sc = priv;
482
483 DPRINTFN(2,("ulpt_input: got some data\n"));
484 /* Do it again. */
485 if (xfer == sc->sc_in_xfer1)
486 usbd_transfer(sc->sc_in_xfer2);
487 else
488 usbd_transfer(sc->sc_in_xfer1);
489 }
490
491 int ulptusein = 1;
492
493 /*
494 * Reset the printer, then wait until it's selected and not busy.
495 */
496 int
497 ulptopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
498 {
499 u_char flags = ULPTFLAGS(dev);
500 struct ulpt_softc *sc;
501 usbd_status err;
502 int spin, error;
503
504 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
505
506 if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
507 return (ENXIO);
508
509 if (sc->sc_state)
510 return (EBUSY);
511
512 sc->sc_state = ULPT_INIT;
513 sc->sc_flags = flags;
514 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
515
516 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
517 /* Ignoring these flags might not be a good idea */
518 if ((flags & ~ULPT_NOPRIME) != 0)
519 printf("ulptopen: flags ignored: %b\n", flags,
520 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
521 #endif
522
523
524 error = 0;
525 sc->sc_refcnt++;
526
527 if ((flags & ULPT_NOPRIME) == 0)
528 ulpt_reset(sc);
529
530 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
531 DPRINTF(("ulpt_open: waiting a while\n"));
532 if (spin >= TIMEOUT) {
533 error = EBUSY;
534 sc->sc_state = 0;
535 goto done;
536 }
537
538 /* wait 1/4 second, give up if we get a signal */
539 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
540 if (error != EWOULDBLOCK) {
541 sc->sc_state = 0;
542 goto done;
543 }
544
545 if (sc->sc_dying) {
546 error = ENXIO;
547 sc->sc_state = 0;
548 goto done;
549 }
550 }
551
552 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
553 if (err) {
554 sc->sc_state = 0;
555 error = EIO;
556 goto done;
557 }
558 if (ulptusein && sc->sc_in != -1) {
559 DPRINTF(("ulpt_open: open input pipe\n"));
560 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
561 if (err) {
562 error = EIO;
563 usbd_close_pipe(sc->sc_out_pipe);
564 sc->sc_out_pipe = NULL;
565 sc->sc_state = 0;
566 goto done;
567 }
568 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
569 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
570 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
571 error = ENOMEM;
572 if (sc->sc_in_xfer1 != NULL) {
573 usbd_free_xfer(sc->sc_in_xfer1);
574 sc->sc_in_xfer1 = NULL;
575 }
576 if (sc->sc_in_xfer2 != NULL) {
577 usbd_free_xfer(sc->sc_in_xfer2);
578 sc->sc_in_xfer2 = NULL;
579 }
580 usbd_close_pipe(sc->sc_out_pipe);
581 sc->sc_out_pipe = NULL;
582 usbd_close_pipe(sc->sc_in_pipe);
583 sc->sc_in_pipe = NULL;
584 sc->sc_state = 0;
585 goto done;
586 }
587 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
588 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
589 USBD_NO_TIMEOUT, ulpt_input);
590 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
591 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
592 USBD_NO_TIMEOUT, ulpt_input);
593 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
594 }
595
596 sc->sc_state = ULPT_OPEN;
597
598 done:
599 if (--sc->sc_refcnt < 0)
600 usb_detach_wakeup(USBDEV(sc->sc_dev));
601
602 DPRINTF(("ulptopen: done, error=%d\n", error));
603 return (error);
604 }
605
606 int
607 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
608 {
609 u_char new;
610
611 status = (status ^ LPS_INVERT) & LPS_MASK;
612 new = status & ~sc->sc_laststatus;
613 sc->sc_laststatus = status;
614
615 if (new & LPS_SELECT)
616 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
617 else if (new & LPS_NOPAPER)
618 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
619 else if (new & LPS_NERR)
620 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
621
622 return (status);
623 }
624
625 int
626 ulptclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
627 {
628 struct ulpt_softc *sc;
629
630 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
631
632 if (sc->sc_state != ULPT_OPEN)
633 /* We are being forced to close before the open completed. */
634 return (0);
635
636 if (sc->sc_out_pipe != NULL) {
637 usbd_close_pipe(sc->sc_out_pipe);
638 sc->sc_out_pipe = NULL;
639 }
640 if (sc->sc_in_pipe != NULL) {
641 usbd_abort_pipe(sc->sc_in_pipe);
642 usbd_close_pipe(sc->sc_in_pipe);
643 sc->sc_in_pipe = NULL;
644 if (sc->sc_in_xfer1 != NULL) {
645 usbd_free_xfer(sc->sc_in_xfer1);
646 sc->sc_in_xfer1 = NULL;
647 }
648 if (sc->sc_in_xfer2 != NULL) {
649 usbd_free_xfer(sc->sc_in_xfer2);
650 sc->sc_in_xfer2 = NULL;
651 }
652 }
653
654 sc->sc_state = 0;
655
656 DPRINTF(("ulptclose: closed\n"));
657 return (0);
658 }
659
660 int
661 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
662 {
663 u_int32_t n;
664 int error = 0;
665 void *bufp;
666 usbd_xfer_handle xfer;
667 usbd_status err;
668
669 DPRINTF(("ulptwrite\n"));
670 xfer = usbd_alloc_xfer(sc->sc_udev);
671 if (xfer == NULL)
672 return (ENOMEM);
673 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
674 if (bufp == NULL) {
675 usbd_free_xfer(xfer);
676 return (ENOMEM);
677 }
678 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
679 ulpt_statusmsg(ulpt_status(sc), sc);
680 error = uiomove(bufp, n, uio);
681 if (error)
682 break;
683 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
684 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
685 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
686 if (err) {
687 DPRINTF(("ulptwrite: error=%d\n", err));
688 error = EIO;
689 break;
690 }
691 }
692 usbd_free_xfer(xfer);
693
694 return (error);
695 }
696
697 int
698 ulptwrite(dev_t dev, struct uio *uio, int flags)
699 {
700 struct ulpt_softc *sc;
701 int error;
702
703 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
704
705 if (sc->sc_dying)
706 return (EIO);
707
708 sc->sc_refcnt++;
709 error = ulpt_do_write(sc, uio, flags);
710 if (--sc->sc_refcnt < 0)
711 usb_detach_wakeup(USBDEV(sc->sc_dev));
712 return (error);
713 }
714
715 int
716 ulptioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
717 {
718 int error = 0;
719
720 switch (cmd) {
721 default:
722 error = ENODEV;
723 }
724
725 return (error);
726 }
727
728 #if 0
729 /* XXX This does not belong here. */
730 /*
731 * Print select parts of a IEEE 1284 device ID.
732 */
733 void
734 ieee1284_print_id(char *str)
735 {
736 char *p, *q;
737
738 for (p = str-1; p; p = strchr(p, ';')) {
739 p++; /* skip ';' */
740 if (strncmp(p, "MFG:", 4) == 0 ||
741 strncmp(p, "MANUFACTURER:", 14) == 0 ||
742 strncmp(p, "MDL:", 4) == 0 ||
743 strncmp(p, "MODEL:", 6) == 0) {
744 q = strchr(p, ';');
745 if (q)
746 printf("%.*s", (int)(q - p + 1), p);
747 }
748 }
749 }
750 #endif
751
752 #if defined(__FreeBSD__)
753 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
754 #endif
755