ulpt.c revision 1.28 1 /* $NetBSD: ulpt.c,v 1.28 1999/11/12 00:34: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 cdev_decl(ulpt);
121 #elif defined(__FreeBSD__)
122 static d_open_t ulptopen;
123 static d_close_t ulptclose;
124 static d_write_t ulptwrite;
125 static d_ioctl_t ulptioctl;
126
127 #define ULPT_CDEV_MAJOR 113
128
129 static struct cdevsw ulpt_cdevsw = {
130 /* open */ ulptopen,
131 /* close */ ulptclose,
132 /* read */ noread,
133 /* write */ ulptwrite,
134 /* ioctl */ ulptioctl,
135 /* poll */ nopoll,
136 /* mmap */ nommap,
137 /* strategy */ nostrategy,
138 /* name */ "ulpt",
139 /* maj */ ULPT_CDEV_MAJOR,
140 /* dump */ nodump,
141 /* psize */ nopsize,
142 /* flags */ 0,
143 /* bmaj */ -1
144 };
145 #endif
146
147 void ulpt_disco __P((void *));
148
149 int ulpt_do_write __P((struct ulpt_softc *, struct uio *uio, int));
150 int ulpt_status __P((struct ulpt_softc *));
151 void ulpt_reset __P((struct ulpt_softc *));
152 int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
153
154 void ieee1284_print_id __P((char *));
155
156 #define ULPTUNIT(s) (minor(s) & 0x1f)
157 #define ULPTFLAGS(s) (minor(s) & 0xe0)
158
159
160 USB_DECLARE_DRIVER(ulpt);
161
162 USB_MATCH(ulpt)
163 {
164 USB_MATCH_START(ulpt, uaa);
165 usb_interface_descriptor_t *id;
166
167 DPRINTFN(10,("ulpt_match\n"));
168 if (uaa->iface == NULL)
169 return (UMATCH_NONE);
170 id = usbd_get_interface_descriptor(uaa->iface);
171 if (id != NULL &&
172 id->bInterfaceClass == UCLASS_PRINTER &&
173 id->bInterfaceSubClass == USUBCLASS_PRINTER &&
174 (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
175 id->bInterfaceProtocol == UPROTO_PRINTER_BI))
176 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
177 return (UMATCH_NONE);
178 }
179
180 USB_ATTACH(ulpt)
181 {
182 USB_ATTACH_START(ulpt, sc, uaa);
183 usbd_device_handle dev = uaa->device;
184 usbd_interface_handle iface = uaa->iface;
185 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
186 char devinfo[1024];
187 usb_endpoint_descriptor_t *ed;
188 usbd_status err;
189
190 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
191 usbd_devinfo(dev, 0, devinfo);
192 USB_ATTACH_SETUP;
193 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
194 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
195
196 /* Figure out which endpoint is the bulk out endpoint. */
197 ed = usbd_interface2endpoint_descriptor(iface, 0);
198 if (ed == NULL)
199 goto nobulk;
200 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
201 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
202 /* In case we are using a bidir protocol... */
203 ed = usbd_interface2endpoint_descriptor(iface, 1);
204 if (ed == NULL)
205 goto nobulk;
206 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
207 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
208 goto nobulk;
209 }
210 sc->sc_bulk = ed->bEndpointAddress;
211 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
212
213 sc->sc_iface = iface;
214 err = usbd_interface2device_handle(iface, &sc->sc_udev);
215 if (err) {
216 sc->sc_dying = 1;
217 USB_ATTACH_ERROR_RETURN;
218 }
219 sc->sc_ifaceno = id->bInterfaceNumber;
220
221 #if 0
222 /*
223 * This code is disabled because for some mysterious it causes
224 * printing not to work. But only sometimes, and mostly with
225 * UHCI and less often with OHCI. *sigh*
226 */
227 {
228 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
229 usb_device_request_t req;
230 int len, alen;
231
232 req.bmRequestType = UT_READ_CLASS_INTERFACE;
233 req.bRequest = UR_GET_DEVICE_ID;
234 USETW(req.wValue, cd->bConfigurationValue);
235 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
236 USETW(req.wLength, sizeof devinfo - 1);
237 err = usbd_do_request_flags(dev, &req, devinfo,USBD_SHORT_XFER_OK,
238 &alen);
239 if (err) {
240 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
241 } else if (alen <= 2) {
242 printf("%s: empty device id, no printer connected?\n",
243 USBDEVNAME(sc->sc_dev));
244 } else {
245 /* devinfo now contains an IEEE-1284 device ID */
246 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
247 if (len > sizeof devinfo - 3)
248 len = sizeof devinfo - 3;
249 devinfo[len] = 0;
250 printf("%s: device id <", USBDEVNAME(sc->sc_dev));
251 ieee1284_print_id(devinfo+2);
252 printf(">\n");
253 }
254 }
255 #endif
256
257 #if defined(__FreeBSD__)
258 sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
259 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
260 sc->dev_noprime = make_dev(&ulpt_cdevsw,
261 device_get_unit(self)|ULPT_NOPRIME,
262 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
263 #endif
264
265 USB_ATTACH_SUCCESS_RETURN;
266
267 nobulk:
268 printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
269 sc->sc_dying = 1;
270 USB_ATTACH_ERROR_RETURN;
271 }
272
273 #if defined(__NetBSD__) || defined(__OpenBSD__)
274 int
275 ulpt_activate(self, act)
276 device_ptr_t self;
277 enum devact act;
278 {
279 struct ulpt_softc *sc = (struct ulpt_softc *)self;
280
281 switch (act) {
282 case DVACT_ACTIVATE:
283 return (EOPNOTSUPP);
284 break;
285
286 case DVACT_DEACTIVATE:
287 sc->sc_dying = 1;
288 break;
289 }
290 return (0);
291 }
292 #endif
293
294 USB_DETACH(ulpt)
295 {
296 USB_DETACH_START(ulpt, sc);
297 int s;
298 #if defined(__NetBSD__) || defined(__OpenBSD__)
299 int maj, mn;
300
301 DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
302 #elif defined(__FreeBSD__)
303 DPRINTF(("ulpt_detach: sc=%p\n", sc));
304 #endif
305
306 sc->sc_dying = 1;
307 if (sc->sc_bulkpipe != NULL)
308 usbd_abort_pipe(sc->sc_bulkpipe);
309
310 s = splusb();
311 if (--sc->sc_refcnt >= 0) {
312 /* There is noone to wake, aborting the pipe is enough */
313 /* Wait for processes to go away. */
314 usb_detach_wait(USBDEV(sc->sc_dev));
315 }
316 splx(s);
317
318 #if defined(__NetBSD__) || defined(__OpenBSD__)
319 /* locate the major number */
320 for (maj = 0; maj < nchrdev; maj++)
321 if (cdevsw[maj].d_open == ulptopen)
322 break;
323
324 /* Nuke the vnodes for any open instances (calls close). */
325 mn = self->dv_unit;
326 vdevgone(maj, mn, mn, VCHR);
327 #elif defined(__FreeBSD__)
328 /* XXX not implemented yet */
329
330 remove_dev(sc->dev);
331 remove_dev(sc->dev_noprime);
332 #endif
333
334 return (0);
335 }
336
337 int
338 ulpt_status(sc)
339 struct ulpt_softc *sc;
340 {
341 usb_device_request_t req;
342 usbd_status err;
343 u_char status;
344
345 req.bmRequestType = UT_READ_CLASS_INTERFACE;
346 req.bRequest = UR_GET_PORT_STATUS;
347 USETW(req.wValue, 0);
348 USETW(req.wIndex, sc->sc_ifaceno);
349 USETW(req.wLength, 1);
350 err = usbd_do_request(sc->sc_udev, &req, &status);
351 DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, err));
352 if (!err)
353 return (status);
354 else
355 return (0);
356 }
357
358 void
359 ulpt_reset(sc)
360 struct ulpt_softc *sc;
361 {
362 usb_device_request_t req;
363
364 DPRINTFN(1, ("ulpt_reset\n"));
365 req.bmRequestType = UT_WRITE_CLASS_OTHER;
366 req.bRequest = UR_SOFT_RESET;
367 USETW(req.wValue, 0);
368 USETW(req.wIndex, sc->sc_ifaceno);
369 USETW(req.wLength, 0);
370 (void)usbd_do_request(sc->sc_udev, &req, 0);
371 }
372
373 /*
374 * Reset the printer, then wait until it's selected and not busy.
375 */
376 int
377 ulptopen(dev, flag, mode, p)
378 dev_t dev;
379 int flag;
380 int mode;
381 struct proc *p;
382 {
383 u_char flags = ULPTFLAGS(dev);
384 struct ulpt_softc *sc;
385 usbd_status err;
386 int spin, error;
387
388 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
389
390 if (sc->sc_iface == NULL || sc->sc_dying)
391 return (ENXIO);
392
393 if (sc->sc_state)
394 return (EBUSY);
395
396 sc->sc_state = ULPT_INIT;
397 sc->sc_flags = flags;
398 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
399
400 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
401 /* Ignoring these flags might not be a good idea */
402 if ((flags & ~ULPT_NOPRIME) != 0)
403 printf("ulptopen: flags ignored: %b\n", flags,
404 "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
405 #endif
406
407
408 if ((flags & ULPT_NOPRIME) == 0)
409 ulpt_reset(sc);
410
411 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
412 if (spin >= TIMEOUT) {
413 sc->sc_state = 0;
414 return (EBUSY);
415 }
416
417 /* wait 1/4 second, give up if we get a signal */
418 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
419 if (error != EWOULDBLOCK) {
420 sc->sc_state = 0;
421 return (error);
422 }
423 }
424
425 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
426 if (err) {
427 sc->sc_state = 0;
428 return (EIO);
429 }
430
431 sc->sc_state = ULPT_OPEN;
432
433 DPRINTF(("ulptopen: done\n"));
434 return (0);
435 }
436
437 int
438 ulpt_statusmsg(status, sc)
439 u_char status;
440 struct ulpt_softc *sc;
441 {
442 u_char new;
443
444 status = (status ^ LPS_INVERT) & LPS_MASK;
445 new = status & ~sc->sc_laststatus;
446 sc->sc_laststatus = status;
447
448 if (new & LPS_SELECT)
449 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
450 else if (new & LPS_NOPAPER)
451 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
452 else if (new & LPS_NERR)
453 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
454
455 return (status);
456 }
457
458 int
459 ulptclose(dev, flag, mode, p)
460 dev_t dev;
461 int flag;
462 int mode;
463 struct proc *p;
464 {
465 struct ulpt_softc *sc;
466
467 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
468
469 if (sc->sc_state != ULPT_OPEN)
470 /* We are being forced to close before the open completed. */
471 return (0);
472
473 usbd_close_pipe(sc->sc_bulkpipe);
474 sc->sc_bulkpipe = 0;
475
476 sc->sc_state = 0;
477
478 DPRINTF(("ulptclose: closed\n"));
479 return (0);
480 }
481
482 int
483 ulpt_do_write(sc, uio, flags)
484 struct ulpt_softc *sc;
485 struct uio *uio;
486 int flags;
487 {
488 u_int32_t n;
489 int error = 0;
490 void *bufp;
491 usbd_xfer_handle xfer;
492 usbd_status err;
493
494 DPRINTF(("ulptwrite\n"));
495 xfer = usbd_alloc_request(sc->sc_udev);
496 if (xfer == NULL)
497 return (ENOMEM);
498 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
499 if (bufp == NULL) {
500 usbd_free_request(xfer);
501 return (ENOMEM);
502 }
503 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
504 ulpt_statusmsg(ulpt_status(sc), sc);
505 error = uiomove(bufp, n, uio);
506 if (error)
507 break;
508 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
509 err = usbd_bulk_transfer(xfer, sc->sc_bulkpipe, USBD_NO_COPY,
510 USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
511 if (err) {
512 DPRINTF(("ulptwrite: error=%d\n", err));
513 error = EIO;
514 break;
515 }
516 }
517 usbd_free_request(xfer);
518
519 return (error);
520 }
521
522 int
523 ulptwrite(dev, uio, flags)
524 dev_t dev;
525 struct uio *uio;
526 int flags;
527 {
528 struct ulpt_softc *sc;
529 int error;
530
531 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
532
533 if (sc->sc_dying)
534 return (EIO);
535
536 sc->sc_refcnt++;
537 error = ulpt_do_write(sc, uio, flags);
538 if (--sc->sc_refcnt < 0)
539 usb_detach_wakeup(USBDEV(sc->sc_dev));
540 return (error);
541 }
542
543 int
544 ulptioctl(dev, cmd, data, flag, p)
545 dev_t dev;
546 u_long cmd;
547 caddr_t data;
548 int flag;
549 struct proc *p;
550 {
551 int error = 0;
552
553 switch (cmd) {
554 default:
555 error = ENODEV;
556 break;
557 }
558
559 return (error);
560 }
561
562 #if 0
563 /* XXX This does not belong here. */
564 /*
565 * Print select parts of a IEEE 1284 device ID.
566 */
567 void
568 ieee1284_print_id(str)
569 char *str;
570 {
571 char *p, *q;
572
573 for (p = str-1; p; p = strchr(p, ';')) {
574 p++; /* skip ';' */
575 if (strncmp(p, "MFG:", 4) == 0 ||
576 strncmp(p, "MANUFACTURER:", 14) == 0 ||
577 strncmp(p, "MDL:", 4) == 0 ||
578 strncmp(p, "MODEL:", 6) == 0) {
579 q = strchr(p, ';');
580 if (q)
581 printf("%.*s", (int)(q - p + 1), p);
582 }
583 }
584 }
585 #endif
586
587 #if defined(__FreeBSD__)
588 DEV_DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass,
589 ulpt_cdevsw, usbd_driver_load, 0);
590 #endif
591