ulpt.c revision 1.14 1 /* $NetBSD: ulpt.c,v 1.14 1999/08/14 14:49:32 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 bdevice 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 #define ULPTUNIT(s) (minor(s) & 0x1f)
124 #define ULPTFLAGS(s) (minor(s) & 0xe0)
125
126 USB_DECLARE_DRIVER(ulpt);
127
128 USB_MATCH(ulpt)
129 {
130 USB_MATCH_START(ulpt, uaa);
131 usb_interface_descriptor_t *id;
132
133 DPRINTFN(10,("ulpt_match\n"));
134 if (!uaa->iface)
135 return (UMATCH_NONE);
136 id = usbd_get_interface_descriptor(uaa->iface);
137 if (id &&
138 id->bInterfaceClass == UCLASS_PRINTER &&
139 id->bInterfaceSubClass == USUBCLASS_PRINTER &&
140 (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
141 id->bInterfaceProtocol == UPROTO_PRINTER_BI))
142 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
143 return (UMATCH_NONE);
144 }
145
146 USB_ATTACH(ulpt)
147 {
148 USB_ATTACH_START(ulpt, sc, uaa);
149 usbd_device_handle dev = uaa->device;
150 usbd_interface_handle iface = uaa->iface;
151 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
152 #if 0
153 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
154 usb_device_request_t req;
155 #endif
156 char devinfo[1024];
157 usb_endpoint_descriptor_t *ed;
158 usbd_status r;
159
160 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
161 usbd_devinfo(dev, 0, devinfo);
162 USB_ATTACH_SETUP;
163 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
164 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
165
166 /* Figure out which endpoint is the bulk out endpoint. */
167 ed = usbd_interface2endpoint_descriptor(iface, 0);
168 if (!ed)
169 goto nobulk;
170 if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
171 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
172 /* In case we are using a bidir protocol... */
173 ed = usbd_interface2endpoint_descriptor(iface, 1);
174 if (!ed)
175 goto nobulk;
176 if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
177 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
178 goto nobulk;
179 }
180 sc->sc_bulk = ed->bEndpointAddress;
181 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
182
183 sc->sc_iface = iface;
184 r = usbd_interface2device_handle(iface, &sc->sc_udev);
185 if (r != USBD_NORMAL_COMPLETION) {
186 sc->sc_dying = 1;
187 USB_ATTACH_ERROR_RETURN;
188 }
189 sc->sc_ifaceno = id->bInterfaceNumber;
190
191 #if 0
192 XXX needs a different way to read the id string since the length
193 is unknown. usbd_do_request() returns error on a short transfer.
194 req.bmRequestType = UT_READ_CLASS_INTERFACE;
195 req.bRequest = UR_GET_DEVICE_ID;
196 USETW(req.wValue, cd->bConfigurationValue);
197 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
198 USETW(req.wLength, sizeof devinfo - 1);
199 r = usbd_do_request(dev, &req, devinfo);
200 if (r == USBD_NORMAL_COMPLETION) {
201 int len;
202 char *idstr;
203 len = (devinfo[0] << 8) | (devinfo[1] & 0xff);
204 /* devinfo now contains an IEEE-1284 device ID */
205 idstr = devinfo+2;
206 idstr[len] = 0;
207 printf("%s: device id <%s>\n", USBDEVNAME(sc->sc_dev), idstr);
208 } else {
209 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
210 }
211 #endif
212
213 USB_ATTACH_SUCCESS_RETURN;
214
215 nobulk:
216 printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
217 sc->sc_dying = 1;
218 USB_ATTACH_ERROR_RETURN;
219 }
220
221 int
222 ulpt_activate(self, act)
223 struct device *self;
224 enum devact act;
225 {
226 return (0);
227 }
228
229 int
230 ulpt_detach(self, flags)
231 struct device *self;
232 int flags;
233 {
234 struct ulpt_softc *sc = (struct ulpt_softc *)self;
235 int maj, mn;
236 int s;
237
238 DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
239
240 sc->sc_dying = 1;
241 if (sc->sc_bulkpipe)
242 usbd_abort_pipe(sc->sc_bulkpipe);
243
244 s = splusb();
245 if (--sc->sc_refcnt >= 0) {
246 /* There is noone to wake, aborting the pipe is enough */
247 /* Wait for processes to go away. */
248 usb_detach_wait(&sc->sc_dev);
249 }
250 splx(s);
251
252 /* locate the major number */
253 for (maj = 0; maj < nchrdev; maj++)
254 if (cdevsw[maj].d_open == ulptopen)
255 break;
256
257 /* Nuke the vnodes for any open instances (calls close). */
258 mn = self->dv_unit;
259 vdevgone(maj, mn, mn, VCHR);
260
261 return (0);
262 }
263
264 int
265 ulpt_status(sc)
266 struct ulpt_softc *sc;
267 {
268 usb_device_request_t req;
269 usbd_status r;
270 u_char status;
271
272 req.bmRequestType = UT_READ_CLASS_INTERFACE;
273 req.bRequest = UR_GET_PORT_STATUS;
274 USETW(req.wValue, 0);
275 USETW(req.wIndex, sc->sc_ifaceno);
276 USETW(req.wLength, 1);
277 r = usbd_do_request(sc->sc_udev, &req, &status);
278 DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
279 if (r == USBD_NORMAL_COMPLETION)
280 return (status);
281 else
282 return (0);
283 }
284
285 void
286 ulpt_reset(sc)
287 struct ulpt_softc *sc;
288 {
289 usb_device_request_t req;
290
291 DPRINTFN(1, ("ulpt_reset\n"));
292 req.bmRequestType = UT_WRITE_CLASS_OTHER;
293 req.bRequest = UR_SOFT_RESET;
294 USETW(req.wValue, 0);
295 USETW(req.wIndex, sc->sc_ifaceno);
296 USETW(req.wLength, 0);
297 (void)usbd_do_request(sc->sc_udev, &req, 0);
298 }
299
300 /*
301 * Reset the printer, then wait until it's selected and not busy.
302 */
303 int
304 ulptopen(dev, flag, mode, p)
305 dev_t dev;
306 int flag;
307 int mode;
308 struct proc *p;
309 {
310 u_char flags = ULPTFLAGS(dev);
311 usbd_status r;
312 int spin, error;
313 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
314
315 if (!sc || !sc->sc_iface || sc->sc_dying)
316 return (ENXIO);
317
318 if (sc->sc_state)
319 return (EBUSY);
320
321 sc->sc_state = ULPT_INIT;
322 sc->sc_flags = flags;
323 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
324
325 if ((flags & ULPT_NOPRIME) == 0)
326 ulpt_reset(sc);
327
328 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
329 if (spin >= TIMEOUT) {
330 sc->sc_state = 0;
331 return (EBUSY);
332 }
333
334 /* wait 1/4 second, give up if we get a signal */
335 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
336 if (error != EWOULDBLOCK) {
337 sc->sc_state = 0;
338 return (error);
339 }
340 }
341
342 r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
343 if (r != USBD_NORMAL_COMPLETION) {
344 sc->sc_state = 0;
345 return (EIO);
346 }
347
348 sc->sc_state = ULPT_OPEN;
349
350 DPRINTF(("ulptopen: done\n"));
351 return (0);
352 }
353
354 int
355 ulpt_statusmsg(status, sc)
356 u_char status;
357 struct ulpt_softc *sc;
358 {
359 u_char new;
360
361 status = (status ^ LPS_INVERT) & LPS_MASK;
362 new = status & ~sc->sc_laststatus;
363 sc->sc_laststatus = status;
364
365 if (new & LPS_SELECT)
366 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
367 else if (new & LPS_NOPAPER)
368 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
369 else if (new & LPS_NERR)
370 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
371
372 return (status);
373 }
374
375 int
376 ulptclose(dev, flag, mode, p)
377 dev_t dev;
378 int flag;
379 int mode;
380 struct proc *p;
381 {
382 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
383
384 if (sc->sc_state != ULPT_OPEN)
385 /* We are being forced to close before the open completed. */
386 return (0);
387
388 usbd_close_pipe(sc->sc_bulkpipe);
389 sc->sc_bulkpipe = 0;
390
391 sc->sc_state = 0;
392
393 DPRINTF(("ulptclose: closed\n"));
394 return (0);
395 }
396
397 int
398 ulpt_do_write(sc, uio, flags)
399 struct ulpt_softc *sc;
400 struct uio *uio;
401 int flags;
402 {
403 size_t n;
404 int error = 0;
405 char buf[ULPT_BSIZE];
406 usbd_request_handle reqh;
407 usbd_status r;
408
409 DPRINTF(("ulptwrite\n"));
410 reqh = usbd_alloc_request();
411 if (reqh == 0)
412 return (ENOMEM);
413 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
414 ulpt_statusmsg(ulpt_status(sc), sc);
415 error = uiomove(buf, n, uio);
416 if (error)
417 break;
418 /* XXX use callback to enable interrupt? */
419 r = usbd_setup_request(reqh, sc->sc_bulkpipe, 0, buf, n,
420 0, USBD_NO_TIMEOUT, 0);
421 if (r != USBD_NORMAL_COMPLETION) {
422 error = EIO;
423 break;
424 }
425 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
426 r = usbd_sync_transfer(reqh);
427 if (r != USBD_NORMAL_COMPLETION) {
428 DPRINTF(("ulptwrite: error=%d\n", r));
429 usbd_clear_endpoint_stall(sc->sc_bulkpipe);
430 error = EIO;
431 break;
432 }
433 }
434 usbd_free_request(reqh);
435
436 return (error);
437 }
438
439 int
440 ulptwrite(dev, uio, flags)
441 dev_t dev;
442 struct uio *uio;
443 int flags;
444 {
445 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
446 int error;
447
448 sc->sc_refcnt++;
449 error = ulpt_do_write(sc, uio, flags);
450 if (--sc->sc_refcnt < 0)
451 usb_detach_wakeup(&sc->sc_dev);
452 return (error);
453 }
454
455 int
456 ulptioctl(dev, cmd, data, flag, p)
457 dev_t dev;
458 u_long cmd;
459 caddr_t data;
460 int flag;
461 struct proc *p;
462 {
463 int error = 0;
464
465 switch (cmd) {
466 default:
467 error = ENODEV;
468 }
469
470 return error;
471 }
472
473 #if defined(__FreeBSD__)
474 DRIVER_MODULE(ulpt, usb, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
475 #endif
476