ulpt.c revision 1.27 1 /* $NetBSD: ulpt.c,v 1.27 1999/10/13 08:10:57 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Printer Class spec: http://www.usb.org/developers/data/usbprn10.pdf
42 */
43
44 /* XXX Note in the manpage the ULPT_NOPRIME version of the printer */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/kernel.h>
50 #if defined(__NetBSD__)
51 #include <sys/device.h>
52 #include <sys/ioctl.h>
53 #elif defined(__FreeBSD__)
54 #include <sys/ioccom.h>
55 #include <sys/module.h>
56 #include <sys/bus.h>
57 #endif
58 #include <sys/uio.h>
59 #include <sys/conf.h>
60 #include <sys/vnode.h>
61 #include <sys/syslog.h>
62
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbdevs.h>
67 #include <dev/usb/usb_quirks.h>
68
69 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
70 #define STEP hz/4
71
72 #define LPTPRI (PZERO+8)
73 #define ULPT_BSIZE 16384
74
75 #ifdef ULPT_DEBUG
76 #define DPRINTF(x) if (ulptdebug) logprintf x
77 #define DPRINTFN(n,x) if (ulptdebug>(n)) logprintf x
78 int ulptdebug = 0;
79 #else
80 #define DPRINTF(x)
81 #define DPRINTFN(n,x)
82 #endif
83
84 #define UR_GET_DEVICE_ID 0
85 #define UR_GET_PORT_STATUS 1
86 #define UR_SOFT_RESET 2
87
88 #define LPS_NERR 0x08 /* printer no error */
89 #define LPS_SELECT 0x10 /* printer selected */
90 #define LPS_NOPAPER 0x20 /* printer out of paper */
91 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
92 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
93
94 struct ulpt_softc {
95 USBBASEDEVICE sc_dev;
96 usbd_device_handle sc_udev; /* device */
97 usbd_interface_handle sc_iface; /* interface */
98 int sc_ifaceno;
99 usbd_pipe_handle sc_bulkpipe; /* bulk pipe */
100 int sc_bulk;
101
102 u_char sc_state;
103 #define ULPT_OPEN 0x01 /* device is open */
104 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
105 #define ULPT_INIT 0x04 /* waiting to initialize for open */
106 u_char sc_flags;
107 #define ULPT_NOPRIME 0x40 /* don't prime on open */
108 u_char sc_laststatus;
109
110 int sc_refcnt;
111 u_char sc_dying;
112
113 #if defined(__FreeBSD__)
114 dev_t dev;
115 dev_t dev_noprime;
116 #endif
117 };
118
119 #if defined(__NetBSD__) || defined(__OpenBSD__)
120 int ulptopen __P((dev_t, int, int, struct proc *));
121 int ulptclose __P((dev_t, int, int, struct proc *p));
122 int ulptwrite __P((dev_t, struct uio *uio, int));
123 int ulptioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
124 #elif defined(__FreeBSD__)
125 static d_open_t ulptopen;
126 static d_close_t ulptclose;
127 static d_write_t ulptwrite;
128 static d_ioctl_t ulptioctl;
129
130 #define ULPT_CDEV_MAJOR 113
131
132 static struct cdevsw ulpt_cdevsw = {
133 /* open */ ulptopen,
134 /* close */ ulptclose,
135 /* read */ noread,
136 /* write */ ulptwrite,
137 /* ioctl */ ulptioctl,
138 /* poll */ nopoll,
139 /* mmap */ nommap,
140 /* strategy */ nostrategy,
141 /* name */ "ulpt",
142 /* maj */ ULPT_CDEV_MAJOR,
143 /* dump */ nodump,
144 /* psize */ nopsize,
145 /* flags */ 0,
146 /* bmaj */ -1
147 };
148 #endif
149
150 void ulpt_disco __P((void *));
151
152 int ulpt_do_write __P((struct ulpt_softc *, struct uio *uio, int));
153 int ulpt_status __P((struct ulpt_softc *));
154 void ulpt_reset __P((struct ulpt_softc *));
155 int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
156
157 void ieee1284_print_id __P((char *));
158
159 #define ULPTUNIT(s) (minor(s) & 0x1f)
160 #define ULPTFLAGS(s) (minor(s) & 0xe0)
161
162
163 USB_DECLARE_DRIVER(ulpt);
164
165 USB_MATCH(ulpt)
166 {
167 USB_MATCH_START(ulpt, uaa);
168 usb_interface_descriptor_t *id;
169
170 DPRINTFN(10,("ulpt_match\n"));
171 if (!uaa->iface)
172 return (UMATCH_NONE);
173 id = usbd_get_interface_descriptor(uaa->iface);
174 if (id &&
175 id->bInterfaceClass == UCLASS_PRINTER &&
176 id->bInterfaceSubClass == USUBCLASS_PRINTER &&
177 (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
178 id->bInterfaceProtocol == UPROTO_PRINTER_BI))
179 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
180 return (UMATCH_NONE);
181 }
182
183 USB_ATTACH(ulpt)
184 {
185 USB_ATTACH_START(ulpt, sc, uaa);
186 usbd_device_handle dev = uaa->device;
187 usbd_interface_handle iface = uaa->iface;
188 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
189 char devinfo[1024];
190 usb_endpoint_descriptor_t *ed;
191 usbd_status r;
192
193 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
194 usbd_devinfo(dev, 0, devinfo);
195 USB_ATTACH_SETUP;
196 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
197 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
198
199 /* Figure out which endpoint is the bulk out endpoint. */
200 ed = usbd_interface2endpoint_descriptor(iface, 0);
201 if (!ed)
202 goto nobulk;
203 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
204 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
205 /* In case we are using a bidir protocol... */
206 ed = usbd_interface2endpoint_descriptor(iface, 1);
207 if (!ed)
208 goto nobulk;
209 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
210 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
211 goto nobulk;
212 }
213 sc->sc_bulk = ed->bEndpointAddress;
214 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
215
216 sc->sc_iface = iface;
217 r = usbd_interface2device_handle(iface, &sc->sc_udev);
218 if (r != USBD_NORMAL_COMPLETION) {
219 sc->sc_dying = 1;
220 USB_ATTACH_ERROR_RETURN;
221 }
222 sc->sc_ifaceno = id->bInterfaceNumber;
223
224 #if 0
225 /*
226 * This code is disabled because for some mysterious it causes
227 * printing not to work. But only sometimes, and mostly with
228 * UHCI and less often with OHCI. *sigh*
229 */
230 {
231 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
232 usb_device_request_t req;
233 int len, alen;
234
235 req.bmRequestType = UT_READ_CLASS_INTERFACE;
236 req.bRequest = UR_GET_DEVICE_ID;
237 USETW(req.wValue, cd->bConfigurationValue);
238 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
239 USETW(req.wLength, sizeof devinfo - 1);
240 r = usbd_do_request_flags(dev, &req, devinfo,USBD_SHORT_XFER_OK,&alen);
241 if (r != USBD_NORMAL_COMPLETION) {
242 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
243 } else if (alen <= 2) {
244 printf("%s: empty device id, no printer connected?\n",
245 USBDEVNAME(sc->sc_dev));
246 } else {
247 /* devinfo now contains an IEEE-1284 device ID */
248 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
249 if (len > sizeof devinfo - 3)
250 len = sizeof devinfo - 3;
251 devinfo[len] = 0;
252 printf("%s: device id <", USBDEVNAME(sc->sc_dev));
253 ieee1284_print_id(devinfo+2);
254 printf(">\n");
255 }
256 }
257 #endif
258
259 #if defined(__FreeBSD__)
260 sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
261 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
262 sc->dev_noprime = make_dev(&ulpt_cdevsw,
263 device_get_unit(self)|ULPT_NOPRIME,
264 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
265 #endif
266
267 USB_ATTACH_SUCCESS_RETURN;
268
269 nobulk:
270 printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
271 sc->sc_dying = 1;
272 USB_ATTACH_ERROR_RETURN;
273 }
274
275 #if defined(__NetBSD__) || defined(__OpenBSD__)
276 int
277 ulpt_activate(self, act)
278 device_ptr_t self;
279 enum devact act;
280 {
281 struct ulpt_softc *sc = (struct ulpt_softc *)self;
282
283 switch (act) {
284 case DVACT_ACTIVATE:
285 return (EOPNOTSUPP);
286 break;
287
288 case DVACT_DEACTIVATE:
289 sc->sc_dying = 1;
290 break;
291 }
292 return (0);
293 }
294 #endif
295
296 USB_DETACH(ulpt)
297 {
298 USB_DETACH_START(ulpt, sc);
299 int s;
300 #if defined(__NetBSD__) || defined(__OpenBSD__)
301 int maj, mn;
302
303 DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
304 #elif defined(__FreeBSD__)
305 DPRINTF(("ulpt_detach: sc=%p\n", sc));
306 #endif
307
308 sc->sc_dying = 1;
309 if (sc->sc_bulkpipe)
310 usbd_abort_pipe(sc->sc_bulkpipe);
311
312 s = splusb();
313 if (--sc->sc_refcnt >= 0) {
314 /* There is noone to wake, aborting the pipe is enough */
315 /* Wait for processes to go away. */
316 usb_detach_wait(USBDEV(sc->sc_dev));
317 }
318 splx(s);
319
320 #if defined(__NetBSD__) || defined(__OpenBSD__)
321 /* locate the major number */
322 for (maj = 0; maj < nchrdev; maj++)
323 if (cdevsw[maj].d_open == ulptopen)
324 break;
325
326 /* Nuke the vnodes for any open instances (calls close). */
327 mn = self->dv_unit;
328 vdevgone(maj, mn, mn, VCHR);
329 #elif defined(__FreeBSD__)
330 /* XXX not implemented yet */
331
332 remove_dev(sc->dev);
333 remove_dev(sc->dev_noprime);
334 #endif
335
336 return (0);
337 }
338
339 int
340 ulpt_status(sc)
341 struct ulpt_softc *sc;
342 {
343 usb_device_request_t req;
344 usbd_status r;
345 u_char status;
346
347 req.bmRequestType = UT_READ_CLASS_INTERFACE;
348 req.bRequest = UR_GET_PORT_STATUS;
349 USETW(req.wValue, 0);
350 USETW(req.wIndex, sc->sc_ifaceno);
351 USETW(req.wLength, 1);
352 r = usbd_do_request(sc->sc_udev, &req, &status);
353 DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
354 if (r == USBD_NORMAL_COMPLETION)
355 return (status);
356 else
357 return (0);
358 }
359
360 void
361 ulpt_reset(sc)
362 struct ulpt_softc *sc;
363 {
364 usb_device_request_t req;
365
366 DPRINTFN(1, ("ulpt_reset\n"));
367 req.bmRequestType = UT_WRITE_CLASS_OTHER;
368 req.bRequest = UR_SOFT_RESET;
369 USETW(req.wValue, 0);
370 USETW(req.wIndex, sc->sc_ifaceno);
371 USETW(req.wLength, 0);
372 (void)usbd_do_request(sc->sc_udev, &req, 0);
373 }
374
375 /*
376 * Reset the printer, then wait until it's selected and not busy.
377 */
378 int
379 ulptopen(dev, flag, mode, p)
380 dev_t dev;
381 int flag;
382 int mode;
383 struct proc *p;
384 {
385 u_char flags = ULPTFLAGS(dev);
386 struct ulpt_softc *sc;
387 usbd_status r;
388 int spin, error;
389
390 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
391
392 if (!sc->sc_iface || sc->sc_dying)
393 return (ENXIO);
394
395 if (sc->sc_state)
396 return (EBUSY);
397
398 sc->sc_state = ULPT_INIT;
399 sc->sc_flags = flags;
400 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
401
402 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
403 /* Ignoring these flags might not be a good idea */
404 if ((flags & ~ULPT_NOPRIME) != 0)
405 printf("ulptopen: flags ignored: %b\n", flags,
406 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
407 #endif
408
409
410 if ((flags & ULPT_NOPRIME) == 0)
411 ulpt_reset(sc);
412
413 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
414 if (spin >= TIMEOUT) {
415 sc->sc_state = 0;
416 return (EBUSY);
417 }
418
419 /* wait 1/4 second, give up if we get a signal */
420 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
421 if (error != EWOULDBLOCK) {
422 sc->sc_state = 0;
423 return (error);
424 }
425 }
426
427 r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
428 if (r != USBD_NORMAL_COMPLETION) {
429 sc->sc_state = 0;
430 return (EIO);
431 }
432
433 sc->sc_state = ULPT_OPEN;
434
435 DPRINTF(("ulptopen: done\n"));
436 return (0);
437 }
438
439 int
440 ulpt_statusmsg(status, sc)
441 u_char status;
442 struct ulpt_softc *sc;
443 {
444 u_char new;
445
446 status = (status ^ LPS_INVERT) & LPS_MASK;
447 new = status & ~sc->sc_laststatus;
448 sc->sc_laststatus = status;
449
450 if (new & LPS_SELECT)
451 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
452 else if (new & LPS_NOPAPER)
453 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
454 else if (new & LPS_NERR)
455 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
456
457 return (status);
458 }
459
460 int
461 ulptclose(dev, flag, mode, p)
462 dev_t dev;
463 int flag;
464 int mode;
465 struct proc *p;
466 {
467 struct ulpt_softc *sc;
468
469 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
470
471 if (sc->sc_state != ULPT_OPEN)
472 /* We are being forced to close before the open completed. */
473 return (0);
474
475 usbd_close_pipe(sc->sc_bulkpipe);
476 sc->sc_bulkpipe = 0;
477
478 sc->sc_state = 0;
479
480 DPRINTF(("ulptclose: closed\n"));
481 return (0);
482 }
483
484 int
485 ulpt_do_write(sc, uio, flags)
486 struct ulpt_softc *sc;
487 struct uio *uio;
488 int flags;
489 {
490 u_int32_t n;
491 int error = 0;
492 void *bufp;
493 usbd_request_handle reqh;
494 usbd_status r;
495
496 DPRINTF(("ulptwrite\n"));
497 reqh = usbd_alloc_request(sc->sc_udev);
498 if (reqh == 0)
499 return (ENOMEM);
500 bufp = usbd_alloc_buffer(reqh, ULPT_BSIZE);
501 if (bufp == 0) {
502 usbd_free_request(reqh);
503 return (ENOMEM);
504 }
505 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
506 ulpt_statusmsg(ulpt_status(sc), sc);
507 error = uiomove(bufp, n, uio);
508 if (error)
509 break;
510 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
511 r = usbd_bulk_transfer(reqh, sc->sc_bulkpipe, USBD_NO_COPY,
512 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
513 if (r != USBD_NORMAL_COMPLETION) {
514 DPRINTF(("ulptwrite: error=%d\n", r));
515 error = EIO;
516 break;
517 }
518 }
519 usbd_free_request(reqh);
520
521 return (error);
522 }
523
524 int
525 ulptwrite(dev, uio, flags)
526 dev_t dev;
527 struct uio *uio;
528 int flags;
529 {
530 struct ulpt_softc *sc;
531 int error;
532
533 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
534
535 if (sc->sc_dying)
536 return (EIO);
537
538 sc->sc_refcnt++;
539 error = ulpt_do_write(sc, uio, flags);
540 if (--sc->sc_refcnt < 0)
541 usb_detach_wakeup(USBDEV(sc->sc_dev));
542 return (error);
543 }
544
545 int
546 ulptioctl(dev, cmd, data, flag, p)
547 dev_t dev;
548 u_long cmd;
549 caddr_t data;
550 int flag;
551 struct proc *p;
552 {
553 int error = 0;
554
555 switch (cmd) {
556 default:
557 error = ENODEV;
558 }
559
560 return (error);
561 }
562
563 #if 0
564 /* XXX This does not belong here. */
565 /*
566 * Print select parts of a IEEE 1284 device ID.
567 */
568 void
569 ieee1284_print_id(str)
570 char *str;
571 {
572 char *p, *q;
573
574 for (p = str-1; p; p = strchr(p, ';')) {
575 p++; /* skip ';' */
576 if (strncmp(p, "MFG:", 4) == 0 ||
577 strncmp(p, "MANUFACTURER:", 14) == 0 ||
578 strncmp(p, "MDL:", 4) == 0 ||
579 strncmp(p, "MODEL:", 6) == 0) {
580 q = strchr(p, ';');
581 if (q)
582 printf("%.*s", (int)(q - p + 1), p);
583 }
584 }
585 }
586 #endif
587
588 #if defined(__FreeBSD__)
589 DEV_DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass,
590 ulpt_cdevsw, usbd_driver_load, 0);
591 #endif
592