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