ulpt.c revision 1.8 1 /* $NetBSD: ulpt.c,v 1.8 1998/12/26 12:53:02 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 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #if defined(__NetBSD__)
46 #include <sys/device.h>
47 #include <sys/ioctl.h>
48 #elif defined(__FreeBSD__)
49 #include <sys/ioccom.h>
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 #endif
53 #include <sys/uio.h>
54 #include <sys/conf.h>
55 #include <sys/syslog.h>
56
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdivar.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
63
64 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */
65 #define STEP hz/4
66
67 #define LPTPRI (PZERO+8)
68 #define ULPT_BSIZE 1024
69
70 #ifdef USB_DEBUG
71 #define DPRINTF(x) if (ulptdebug) printf x
72 #define DPRINTFN(n,x) if (ulptdebug>(n)) printf x
73 int ulptdebug = 0;
74 #else
75 #define DPRINTF(x)
76 #define DPRINTFN(n,x)
77 #endif
78
79 #define UR_GET_DEVICE_ID 0
80 #define UR_GET_PORT_STATUS 1
81 #define UR_SOFT_RESET 2
82
83 #define LPS_NERR 0x08 /* printer no error */
84 #define LPS_SELECT 0x10 /* printer selected */
85 #define LPS_NOPAPER 0x20 /* printer out of paper */
86 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
87 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
88
89 struct ulpt_softc {
90 bdevice sc_dev;
91 usbd_device_handle sc_udev; /* device */
92 usbd_interface_handle sc_iface; /* interface */
93 int sc_ifaceno;
94 usbd_pipe_handle sc_bulkpipe; /* bulk pipe */
95 int sc_bulk;
96
97 u_char sc_state;
98 #define ULPT_OPEN 0x01 /* device is open */
99 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
100 #define ULPT_INIT 0x04 /* waiting to initialize for open */
101 u_char sc_flags;
102 #define ULPT_NOPRIME 0x40 /* don't prime on open */
103 u_char sc_laststatus;
104 };
105
106 int ulptopen __P((dev_t, int, int, struct proc *));
107 int ulptclose __P((dev_t, int, int, struct proc *p));
108 int ulptwrite __P((dev_t, struct uio *uio, int));
109 int ulptioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
110 void ulpt_disco __P((void *));
111
112 int ulpt_status __P((struct ulpt_softc *));
113 void ulpt_reset __P((struct ulpt_softc *));
114 int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
115
116 #define ULPTUNIT(s) (minor(s) & 0x1f)
117 #define ULPTFLAGS(s) (minor(s) & 0xe0)
118
119 USB_DECLARE_DRIVER(ulpt);
120
121 USB_MATCH(ulpt)
122 {
123 USB_MATCH_START(ulpt, uaa);
124 usb_interface_descriptor_t *id;
125
126 DPRINTFN(10,("ulpt_match\n"));
127 if (!uaa->iface)
128 return (UMATCH_NONE);
129 id = usbd_get_interface_descriptor(uaa->iface);
130 if (id &&
131 id->bInterfaceClass == UCLASS_PRINTER &&
132 id->bInterfaceSubClass == USUBCLASS_PRINTER &&
133 (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
134 id->bInterfaceProtocol == UPROTO_PRINTER_BI))
135 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
136 return (UMATCH_NONE);
137 }
138
139 USB_ATTACH(ulpt)
140 {
141 USB_ATTACH_START(ulpt, sc, uaa);
142 usbd_device_handle dev = uaa->device;
143 usbd_interface_handle iface = uaa->iface;
144 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
145 #if 0
146 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
147 usb_device_request_t req;
148 #endif
149 char devinfo[1024];
150 usb_endpoint_descriptor_t *ed;
151 usbd_status r;
152
153 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
154 usbd_devinfo(dev, 0, devinfo);
155 USB_ATTACH_SETUP;
156 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
157 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
158
159 /* Figure out which endpoint is the bulk out endpoint. */
160 ed = usbd_interface2endpoint_descriptor(iface, 0);
161 if (!ed)
162 goto nobulk;
163 if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
164 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
165 /* In case we are using a bidir protocol... */
166 ed = usbd_interface2endpoint_descriptor(iface, 1);
167 if (!ed)
168 goto nobulk;
169 if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
170 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
171 goto nobulk;
172 }
173 sc->sc_bulk = ed->bEndpointAddress;
174 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
175
176 sc->sc_iface = iface;
177 r = usbd_interface2device_handle(iface, &sc->sc_udev);
178 if (r != USBD_NORMAL_COMPLETION)
179 USB_ATTACH_ERROR_RETURN;
180 sc->sc_ifaceno = id->bInterfaceNumber;
181
182 #if 0
183 XXX needs a different way to read the id string since the length
184 is unknown. usbd_do_request() returns error on a short transfer.
185 req.bmRequestType = UT_READ_CLASS_INTERFACE;
186 req.bRequest = UR_GET_DEVICE_ID;
187 USETW(req.wValue, cd->bConfigurationValue);
188 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
189 USETW(req.wLength, sizeof devinfo - 1);
190 r = usbd_do_request(dev, &req, devinfo);
191 if (r == USBD_NORMAL_COMPLETION) {
192 int len;
193 char *idstr;
194 len = (devinfo[0] << 8) | (devinfo[1] & 0xff);
195 /* devinfo now contains an IEEE-1284 device ID */
196 idstr = devinfo+2;
197 idstr[len] = 0;
198 printf("%s: device id <%s>\n", USBDEVNAME(sc->sc_dev), idstr);
199 } else {
200 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
201 }
202 #endif
203
204 USB_ATTACH_SUCCESS_RETURN;
205
206 nobulk:
207 printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
208 USB_ATTACH_ERROR_RETURN;
209 }
210
211 int
212 ulpt_status(sc)
213 struct ulpt_softc *sc;
214 {
215 usb_device_request_t req;
216 usbd_status r;
217 u_char status;
218
219 req.bmRequestType = UT_READ_CLASS_INTERFACE;
220 req.bRequest = UR_GET_PORT_STATUS;
221 USETW(req.wValue, 0);
222 USETW(req.wIndex, sc->sc_ifaceno);
223 USETW(req.wLength, 1);
224 r = usbd_do_request(sc->sc_udev, &req, &status);
225 DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
226 if (r == USBD_NORMAL_COMPLETION)
227 return (status);
228 else
229 return (0);
230 }
231
232 void
233 ulpt_reset(sc)
234 struct ulpt_softc *sc;
235 {
236 usb_device_request_t req;
237
238 DPRINTFN(1, ("ulpt_reset\n"));
239 req.bmRequestType = UT_WRITE_CLASS_OTHER;
240 req.bRequest = UR_SOFT_RESET;
241 USETW(req.wValue, 0);
242 USETW(req.wIndex, sc->sc_ifaceno);
243 USETW(req.wLength, 0);
244 (void)usbd_do_request(sc->sc_udev, &req, 0);
245 }
246
247 /*
248 * Reset the printer, then wait until it's selected and not busy.
249 */
250 int
251 ulptopen(dev, flag, mode, p)
252 dev_t dev;
253 int flag;
254 int mode;
255 struct proc *p;
256 {
257 u_char flags = ULPTFLAGS(dev);
258 usbd_status r;
259 int spin, error;
260 USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
261
262 if (!sc || !sc->sc_iface)
263 return ENXIO;
264
265 if (sc->sc_state)
266 return EBUSY;
267
268 sc->sc_state = ULPT_INIT;
269 sc->sc_flags = flags;
270 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
271
272 if ((flags & ULPT_NOPRIME) == 0)
273 ulpt_reset(sc);
274
275 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
276 if (spin >= TIMEOUT) {
277 sc->sc_state = 0;
278 return EBUSY;
279 }
280
281 /* wait 1/4 second, give up if we get a signal */
282 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
283 if (error != EWOULDBLOCK) {
284 sc->sc_state = 0;
285 return error;
286 }
287 }
288
289 r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
290 if (r != USBD_NORMAL_COMPLETION) {
291 sc->sc_state = 0;
292 return (EIO);
293 }
294
295 sc->sc_state = ULPT_OPEN;
296
297 DPRINTF(("ulptopen: done\n"));
298 return (0);
299 }
300
301 int
302 ulpt_statusmsg(status, sc)
303 u_char status;
304 struct ulpt_softc *sc;
305 {
306 u_char new;
307
308 status = (status ^ LPS_INVERT) & LPS_MASK;
309 new = status & ~sc->sc_laststatus;
310 sc->sc_laststatus = status;
311
312 if (new & LPS_SELECT)
313 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
314 else if (new & LPS_NOPAPER)
315 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
316 else if (new & LPS_NERR)
317 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
318
319 return status;
320 }
321
322 int
323 ulptclose(dev, flag, mode, p)
324 dev_t dev;
325 int flag;
326 int mode;
327 struct proc *p;
328 {
329 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
330
331 usbd_close_pipe(sc->sc_bulkpipe);
332
333 sc->sc_state = 0;
334
335 DPRINTF(("ulptclose: closed\n"));
336 return (0);
337 }
338
339 int
340 ulptwrite(dev, uio, flags)
341 dev_t dev;
342 struct uio *uio;
343 int flags;
344 {
345 size_t n;
346 int error = 0;
347 char buf[ULPT_BSIZE];
348 usbd_request_handle reqh;
349 usbd_status r;
350 USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
351
352 DPRINTF(("ulptwrite\n"));
353 reqh = usbd_alloc_request();
354 if (reqh == 0)
355 return (ENOMEM);
356 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
357 ulpt_statusmsg(ulpt_status(sc), sc);
358 error = uiomove(buf, n, uio);
359 if (error)
360 break;
361 /* XXX use callback to enable interrupt? */
362 r = usbd_setup_request(reqh, sc->sc_bulkpipe, 0, buf, n,
363 0, USBD_NO_TIMEOUT, 0);
364 if (r != USBD_NORMAL_COMPLETION) {
365 error = EIO;
366 break;
367 }
368 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
369 r = usbd_sync_transfer(reqh);
370 if (r != USBD_NORMAL_COMPLETION) {
371 DPRINTF(("ulptwrite: error=%d\n", r));
372 usbd_clear_endpoint_stall(sc->sc_bulkpipe);
373 error = EIO;
374 break;
375 }
376 }
377 usbd_free_request(reqh);
378 return (error);
379 }
380
381 int
382 ulptioctl(dev, cmd, data, flag, p)
383 dev_t dev;
384 u_long cmd;
385 caddr_t data;
386 int flag;
387 struct proc *p;
388 {
389 int error = 0;
390
391 switch (cmd) {
392 default:
393 error = ENODEV;
394 }
395
396 return error;
397 }
398
399 #if defined(__FreeBSD__)
400 static int
401 ulpt_detach(device_t self)
402 {
403 struct ulpt_softc *sc = device_get_softc(self);
404 char *devinfo = (char *) device_get_desc(self);
405
406 if (devinfo) {
407 device_set_desc(self, NULL);
408 free(devinfo, M_USB);
409 }
410 return 0;
411 }
412
413 DRIVER_MODULE(ulpt, usb, ulpt_driver, ulpt_devclass, usb_driver_load, 0);
414 #endif
415