ulpt.c revision 1.24 1 /* $NetBSD: ulpt.c,v 1.24 1999/09/12 08:23:42 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 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/kernel.h>
48 #if defined(__NetBSD__)
49 #include <sys/device.h>
50 #include <sys/ioctl.h>
51 #elif defined(__FreeBSD__)
52 #include <sys/ioccom.h>
53 #include <sys/module.h>
54 #include <sys/bus.h>
55 #endif
56 #include <sys/uio.h>
57 #include <sys/conf.h>
58 #include <sys/vnode.h>
59 #include <sys/syslog.h>
60
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdevs.h>
65 #include <dev/usb/usb_quirks.h>
66
67 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
68 #define STEP hz/4
69
70 #define LPTPRI (PZERO+8)
71 #define ULPT_BSIZE 1024
72
73 #ifdef USB_DEBUG
74 #define DPRINTF(x) if (ulptdebug) logprintf x
75 #define DPRINTFN(n,x) if (ulptdebug>(n)) logprintf x
76 int ulptdebug = 0;
77 #else
78 #define DPRINTF(x)
79 #define DPRINTFN(n,x)
80 #endif
81
82 #define UR_GET_DEVICE_ID 0
83 #define UR_GET_PORT_STATUS 1
84 #define UR_SOFT_RESET 2
85
86 #define LPS_NERR 0x08 /* printer no error */
87 #define LPS_SELECT 0x10 /* printer selected */
88 #define LPS_NOPAPER 0x20 /* printer out of paper */
89 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
90 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
91
92 struct ulpt_softc {
93 USBBASEDEVICE sc_dev;
94 usbd_device_handle sc_udev; /* device */
95 usbd_interface_handle sc_iface; /* interface */
96 int sc_ifaceno;
97 usbd_pipe_handle sc_bulkpipe; /* bulk pipe */
98 int sc_bulk;
99
100 u_char sc_state;
101 #define ULPT_OPEN 0x01 /* device is open */
102 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
103 #define ULPT_INIT 0x04 /* waiting to initialize for open */
104 u_char sc_flags;
105 #define ULPT_NOPRIME 0x40 /* don't prime on open */
106 u_char sc_laststatus;
107
108 int sc_refcnt;
109 u_char sc_dying;
110 };
111
112 int ulptopen __P((dev_t, int, int, struct proc *));
113 int ulptclose __P((dev_t, int, int, struct proc *p));
114 int ulptwrite __P((dev_t, struct uio *uio, int));
115 int ulptioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
116 void ulpt_disco __P((void *));
117
118 int ulpt_do_write __P((struct ulpt_softc *, struct uio *uio, int));
119 int ulpt_status __P((struct ulpt_softc *));
120 void ulpt_reset __P((struct ulpt_softc *));
121 int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
122
123 void ieee1284_print_id __P((char *));
124
125 #define ULPTUNIT(s) (minor(s) & 0x1f)
126 #define ULPTFLAGS(s) (minor(s) & 0xe0)
127
128 USB_DECLARE_DRIVER(ulpt);
129
130 USB_MATCH(ulpt)
131 {
132 USB_MATCH_START(ulpt, uaa);
133 usb_interface_descriptor_t *id;
134
135 DPRINTFN(10,("ulpt_match\n"));
136 if (!uaa->iface)
137 return (UMATCH_NONE);
138 id = usbd_get_interface_descriptor(uaa->iface);
139 if (id &&
140 id->bInterfaceClass == UCLASS_PRINTER &&
141 id->bInterfaceSubClass == USUBCLASS_PRINTER &&
142 (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
143 id->bInterfaceProtocol == UPROTO_PRINTER_BI))
144 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
145 return (UMATCH_NONE);
146 }
147
148 USB_ATTACH(ulpt)
149 {
150 USB_ATTACH_START(ulpt, sc, uaa);
151 usbd_device_handle dev = uaa->device;
152 usbd_interface_handle iface = uaa->iface;
153 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
154 char devinfo[1024];
155 usb_endpoint_descriptor_t *ed;
156 usbd_status r;
157
158 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
159 usbd_devinfo(dev, 0, devinfo);
160 USB_ATTACH_SETUP;
161 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
162 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
163
164 /* Figure out which endpoint is the bulk out endpoint. */
165 ed = usbd_interface2endpoint_descriptor(iface, 0);
166 if (!ed)
167 goto nobulk;
168 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
169 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
170 /* In case we are using a bidir protocol... */
171 ed = usbd_interface2endpoint_descriptor(iface, 1);
172 if (!ed)
173 goto nobulk;
174 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT ||
175 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
176 goto nobulk;
177 }
178 sc->sc_bulk = ed->bEndpointAddress;
179 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
180
181 sc->sc_iface = iface;
182 r = usbd_interface2device_handle(iface, &sc->sc_udev);
183 if (r != USBD_NORMAL_COMPLETION) {
184 sc->sc_dying = 1;
185 USB_ATTACH_ERROR_RETURN;
186 }
187 sc->sc_ifaceno = id->bInterfaceNumber;
188
189 #if 0
190 /*
191 * This code is disabled because for some mysterious it causes
192 * printing not to work. But only sometimes, and mostly with
193 * UHCI and less often with OHCI. *sigh*
194 */
195 {
196 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
197 usb_device_request_t req;
198 int len, alen;
199
200 req.bmRequestType = UT_READ_CLASS_INTERFACE;
201 req.bRequest = UR_GET_DEVICE_ID;
202 USETW(req.wValue, cd->bConfigurationValue);
203 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
204 USETW(req.wLength, sizeof devinfo - 1);
205 r = usbd_do_request_flags(dev, &req, devinfo,USBD_SHORT_XFER_OK,&alen);
206 if (r != USBD_NORMAL_COMPLETION) {
207 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
208 } else if (alen <= 2) {
209 printf("%s: empty device id, no printer connected?\n",
210 USBDEVNAME(sc->sc_dev));
211 } else {
212 /* devinfo now contains an IEEE-1284 device ID */
213 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
214 if (len > sizeof devinfo - 3)
215 len = sizeof devinfo - 3;
216 devinfo[len] = 0;
217 printf("%s: device id <", USBDEVNAME(sc->sc_dev));
218 ieee1284_print_id(devinfo+2);
219 printf(">\n");
220 }
221 }
222 #endif
223
224 USB_ATTACH_SUCCESS_RETURN;
225
226 nobulk:
227 printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
228 sc->sc_dying = 1;
229 USB_ATTACH_ERROR_RETURN;
230 }
231
232 int
233 ulpt_activate(self, act)
234 device_ptr_t self;
235 enum devact act;
236 {
237 struct ulpt_softc *sc = (struct ulpt_softc *)self;
238
239 switch (act) {
240 case DVACT_ACTIVATE:
241 return (EOPNOTSUPP);
242 break;
243
244 case DVACT_DEACTIVATE:
245 sc->sc_dying = 1;
246 break;
247 }
248 return (0);
249 }
250
251 int
252 ulpt_detach(self, flags)
253 device_ptr_t self;
254 int flags;
255 {
256 struct ulpt_softc *sc = (struct ulpt_softc *)self;
257 int maj, mn;
258 int s;
259
260 DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
261
262 sc->sc_dying = 1;
263 if (sc->sc_bulkpipe)
264 usbd_abort_pipe(sc->sc_bulkpipe);
265
266 s = splusb();
267 if (--sc->sc_refcnt >= 0) {
268 /* There is noone to wake, aborting the pipe is enough */
269 /* Wait for processes to go away. */
270 usb_detach_wait(USBDEV(sc->sc_dev));
271 }
272 splx(s);
273
274 /* locate the major number */
275 for (maj = 0; maj < nchrdev; maj++)
276 if (cdevsw[maj].d_open == ulptopen)
277 break;
278
279 /* Nuke the vnodes for any open instances (calls close). */
280 mn = self->dv_unit;
281 vdevgone(maj, mn, mn, VCHR);
282
283 return (0);
284 }
285
286 int
287 ulpt_status(sc)
288 struct ulpt_softc *sc;
289 {
290 usb_device_request_t req;
291 usbd_status r;
292 u_char status;
293
294 req.bmRequestType = UT_READ_CLASS_INTERFACE;
295 req.bRequest = UR_GET_PORT_STATUS;
296 USETW(req.wValue, 0);
297 USETW(req.wIndex, sc->sc_ifaceno);
298 USETW(req.wLength, 1);
299 r = usbd_do_request(sc->sc_udev, &req, &status);
300 DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
301 if (r == USBD_NORMAL_COMPLETION)
302 return (status);
303 else
304 return (0);
305 }
306
307 void
308 ulpt_reset(sc)
309 struct ulpt_softc *sc;
310 {
311 usb_device_request_t req;
312
313 DPRINTFN(1, ("ulpt_reset\n"));
314 req.bmRequestType = UT_WRITE_CLASS_OTHER;
315 req.bRequest = UR_SOFT_RESET;
316 USETW(req.wValue, 0);
317 USETW(req.wIndex, sc->sc_ifaceno);
318 USETW(req.wLength, 0);
319 (void)usbd_do_request(sc->sc_udev, &req, 0);
320 }
321
322 /*
323 * Reset the printer, then wait until it's selected and not busy.
324 */
325 int
326 ulptopen(dev, flag, mode, p)
327 dev_t dev;
328 int flag;
329 int mode;
330 struct proc *p;
331 {
332 u_char flags = ULPTFLAGS(dev);
333 usbd_status r;
334 int spin, error;
335 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
336
337 if (!sc || !sc->sc_iface || sc->sc_dying)
338 return (ENXIO);
339
340 if (sc->sc_state)
341 return (EBUSY);
342
343 sc->sc_state = ULPT_INIT;
344 sc->sc_flags = flags;
345 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
346
347 if ((flags & ULPT_NOPRIME) == 0)
348 ulpt_reset(sc);
349
350 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
351 if (spin >= TIMEOUT) {
352 sc->sc_state = 0;
353 return (EBUSY);
354 }
355
356 /* wait 1/4 second, give up if we get a signal */
357 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
358 if (error != EWOULDBLOCK) {
359 sc->sc_state = 0;
360 return (error);
361 }
362 }
363
364 r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
365 if (r != USBD_NORMAL_COMPLETION) {
366 sc->sc_state = 0;
367 return (EIO);
368 }
369
370 sc->sc_state = ULPT_OPEN;
371
372 DPRINTF(("ulptopen: done\n"));
373 return (0);
374 }
375
376 int
377 ulpt_statusmsg(status, sc)
378 u_char status;
379 struct ulpt_softc *sc;
380 {
381 u_char new;
382
383 status = (status ^ LPS_INVERT) & LPS_MASK;
384 new = status & ~sc->sc_laststatus;
385 sc->sc_laststatus = status;
386
387 if (new & LPS_SELECT)
388 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
389 else if (new & LPS_NOPAPER)
390 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
391 else if (new & LPS_NERR)
392 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
393
394 return (status);
395 }
396
397 int
398 ulptclose(dev, flag, mode, p)
399 dev_t dev;
400 int flag;
401 int mode;
402 struct proc *p;
403 {
404 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
405
406 if (sc->sc_state != ULPT_OPEN)
407 /* We are being forced to close before the open completed. */
408 return (0);
409
410 usbd_close_pipe(sc->sc_bulkpipe);
411 sc->sc_bulkpipe = 0;
412
413 sc->sc_state = 0;
414
415 DPRINTF(("ulptclose: closed\n"));
416 return (0);
417 }
418
419 int
420 ulpt_do_write(sc, uio, flags)
421 struct ulpt_softc *sc;
422 struct uio *uio;
423 int flags;
424 {
425 u_int32_t n;
426 int error = 0;
427 void *bufp;
428 usbd_request_handle reqh;
429 usbd_status r;
430
431 DPRINTF(("ulptwrite\n"));
432 reqh = usbd_alloc_request(sc->sc_udev);
433 if (reqh == 0)
434 return (ENOMEM);
435 bufp = usbd_alloc_buffer(reqh, ULPT_BSIZE);
436 if (bufp == 0) {
437 usbd_free_request(reqh);
438 return (ENOMEM);
439 }
440 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
441 ulpt_statusmsg(ulpt_status(sc), sc);
442 error = uiomove(bufp, n, uio);
443 if (error)
444 break;
445 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
446 r = usbd_bulk_transfer(reqh, sc->sc_bulkpipe, USBD_NO_COPY,
447 USBD_NO_TIMEOUT, buf, &n, "ulptwr");
448 if (r != USBD_NORMAL_COMPLETION) {
449 DPRINTF(("ulptwrite: error=%d\n", r));
450 error = EIO;
451 break;
452 }
453 }
454 usbd_free_request(reqh);
455
456 return (error);
457 }
458
459 int
460 ulptwrite(dev, uio, flags)
461 dev_t dev;
462 struct uio *uio;
463 int flags;
464 {
465 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
466 int error;
467
468 if (sc->sc_dying)
469 return (EIO);
470
471 sc->sc_refcnt++;
472 error = ulpt_do_write(sc, uio, flags);
473 if (--sc->sc_refcnt < 0)
474 usb_detach_wakeup(USBDEV(sc->sc_dev));
475 return (error);
476 }
477
478 int
479 ulptioctl(dev, cmd, data, flag, p)
480 dev_t dev;
481 u_long cmd;
482 caddr_t data;
483 int flag;
484 struct proc *p;
485 {
486 int error = 0;
487
488 switch (cmd) {
489 default:
490 error = ENODEV;
491 }
492
493 return (error);
494 }
495
496 #if 0
497 /* XXX This does not belong here. */
498 /*
499 * Print select parts of a IEEE 1284 device ID.
500 */
501 void
502 ieee1284_print_id(str)
503 char *str;
504 {
505 char *p, *q;
506
507 for (p = str-1; p; p = strchr(p, ';')) {
508 p++; /* skip ';' */
509 if (strncmp(p, "MFG:", 4) == 0 ||
510 strncmp(p, "MANUFACTURER:", 14) == 0 ||
511 strncmp(p, "MDL:", 4) == 0 ||
512 strncmp(p, "MODEL:", 6) == 0) {
513 q = strchr(p, ';');
514 if (q)
515 printf("%.*s", (int)(q - p + 1), p);
516 }
517 }
518 }
519 #endif
520
521 #if defined(__FreeBSD__)
522 DRIVER_MODULE(ulpt, usb, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
523 #endif
524