ulpt.c revision 1.7 1 /* $NetBSD: ulpt.c,v 1.7 1998/12/09 00:18:11 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 &&
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 void
147 ulpt_attach(parent, self, aux)
148 struct device *parent;
149 struct device *self;
150 void *aux;
151 {
152 struct ulpt_softc *sc = (struct ulpt_softc *)self;
153 struct usb_attach_arg *uaa = aux;
154 usbd_device_handle dev = uaa->device;
155 usbd_interface_handle iface = uaa->iface;
156 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
157 #if 0
158 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
159 usb_device_request_t req;
160 #endif
161 char devinfo[1024];
162 usb_endpoint_descriptor_t *ed;
163 usbd_status r;
164
165 DPRINTFN(2,("ulpt_attach: sc=%p\n", sc));
166 usbd_devinfo(dev, 0, devinfo);
167 printf("\n%s: %s, iclass %d/%d\n", sc->sc_dev.dv_xname,
168 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
169
170 /* Figure out which endpoint is the bulk out endpoint. */
171 ed = usbd_interface2endpoint_descriptor(iface, 0);
172 if (!ed)
173 goto nobulk;
174 if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
175 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
176 /* In case we are using a bidir protocol... */
177 ed = usbd_interface2endpoint_descriptor(iface, 1);
178 if (!ed)
179 goto nobulk;
180 if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
181 (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
182 goto nobulk;
183 }
184 sc->sc_bulk = ed->bEndpointAddress;
185 DPRINTFN(1, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
186
187 sc->sc_iface = iface;
188 r = usbd_interface2device_handle(iface, &sc->sc_udev);
189 if (r != USBD_NORMAL_COMPLETION)
190 return;
191 sc->sc_ifaceno = id->bInterfaceNumber;
192
193 #if 0
194 XXX needs a different way to read the id string since the length
195 is unknown. usbd_do_request() returns error on a short transfer.
196 req.bmRequestType = UT_READ_CLASS_INTERFACE;
197 req.bRequest = UR_GET_DEVICE_ID;
198 USETW(req.wValue, cd->bConfigurationValue);
199 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
200 USETW(req.wLength, sizeof devinfo - 1);
201 r = usbd_do_request(dev, &req, devinfo);
202 if (r == USBD_NORMAL_COMPLETION) {
203 int len;
204 char *idstr;
205 len = (devinfo[0] << 8) | (devinfo[1] & 0xff);
206 /* devinfo now contains an IEEE-1284 device ID */
207 idstr = devinfo+2;
208 idstr[len] = 0;
209 printf("%s: device id <%s>\n", sc->sc_dev.dv_xname, idstr);
210 } else {
211 printf("%s: cannot get device id\n", sc->sc_dev.dv_xname);
212 }
213 #endif
214
215 return;
216
217 nobulk:
218 printf("%s: could not find bulk endpoint\n", sc->sc_dev.dv_xname);
219 return;
220 }
221
222 int
223 ulpt_status(sc)
224 struct ulpt_softc *sc;
225 {
226 usb_device_request_t req;
227 usbd_status r;
228 u_char status;
229
230 req.bmRequestType = UT_READ_CLASS_INTERFACE;
231 req.bRequest = UR_GET_PORT_STATUS;
232 USETW(req.wValue, 0);
233 USETW(req.wIndex, sc->sc_ifaceno);
234 USETW(req.wLength, 1);
235 r = usbd_do_request(sc->sc_udev, &req, &status);
236 DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
237 if (r == USBD_NORMAL_COMPLETION)
238 return (status);
239 else
240 return (0);
241 }
242
243 void
244 ulpt_reset(sc)
245 struct ulpt_softc *sc;
246 {
247 usb_device_request_t req;
248
249 DPRINTFN(1, ("ulpt_reset\n"));
250 req.bmRequestType = UT_WRITE_CLASS_OTHER;
251 req.bRequest = UR_SOFT_RESET;
252 USETW(req.wValue, 0);
253 USETW(req.wIndex, sc->sc_ifaceno);
254 USETW(req.wLength, 0);
255 (void)usbd_do_request(sc->sc_udev, &req, 0);
256 }
257
258 /*
259 * Reset the printer, then wait until it's selected and not busy.
260 */
261 int
262 ulptopen(dev, flag, mode, p)
263 dev_t dev;
264 int flag;
265 int mode;
266 struct proc *p;
267 {
268 int unit = ULPTUNIT(dev);
269 u_char flags = ULPTFLAGS(dev);
270 struct ulpt_softc *sc;
271 usbd_status r;
272 int spin, error;
273
274 if (unit >= ulpt_cd.cd_ndevs)
275 return ENXIO;
276 sc = ulpt_cd.cd_devs[unit];
277 if (!sc || !sc->sc_iface)
278 return ENXIO;
279
280 if (sc->sc_state)
281 return EBUSY;
282
283 sc->sc_state = ULPT_INIT;
284 sc->sc_flags = flags;
285 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
286
287 if ((flags & ULPT_NOPRIME) == 0)
288 ulpt_reset(sc);
289
290 for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
291 if (spin >= TIMEOUT) {
292 sc->sc_state = 0;
293 return EBUSY;
294 }
295
296 /* wait 1/4 second, give up if we get a signal */
297 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
298 if (error != EWOULDBLOCK) {
299 sc->sc_state = 0;
300 return error;
301 }
302 }
303
304 r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
305 if (r != USBD_NORMAL_COMPLETION) {
306 sc->sc_state = 0;
307 return (EIO);
308 }
309
310 sc->sc_state = ULPT_OPEN;
311
312 DPRINTF(("ulptopen: done\n"));
313 return (0);
314 }
315
316 int
317 ulpt_statusmsg(status, sc)
318 u_char status;
319 struct ulpt_softc *sc;
320 {
321 u_char new;
322
323 status = (status ^ LPS_INVERT) & LPS_MASK;
324 new = status & ~sc->sc_laststatus;
325 sc->sc_laststatus = status;
326
327 if (new & LPS_SELECT)
328 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
329 else if (new & LPS_NOPAPER)
330 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
331 else if (new & LPS_NERR)
332 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
333
334 return status;
335 }
336
337 int
338 ulptclose(dev, flag, mode, p)
339 dev_t dev;
340 int flag;
341 int mode;
342 struct proc *p;
343 {
344 int unit = ULPTUNIT(dev);
345 struct ulpt_softc *sc = ulpt_cd.cd_devs[unit];
346
347 usbd_close_pipe(sc->sc_bulkpipe);
348
349 sc->sc_state = 0;
350
351 DPRINTF(("ulptclose: closed\n"));
352 return (0);
353 }
354
355 int
356 ulptwrite(dev, uio, flags)
357 dev_t dev;
358 struct uio *uio;
359 int flags;
360 {
361 struct ulpt_softc *sc = ulpt_cd.cd_devs[ULPTUNIT(dev)];
362 size_t n;
363 int error = 0;
364 char buf[ULPT_BSIZE];
365 usbd_request_handle reqh;
366 usbd_status r;
367
368 DPRINTF(("ulptwrite\n"));
369 reqh = usbd_alloc_request();
370 if (reqh == 0)
371 return (ENOMEM);
372 while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
373 ulpt_statusmsg(ulpt_status(sc), sc);
374 error = uiomove(buf, n, uio);
375 if (error)
376 break;
377 /* XXX use callback to enable interrupt? */
378 r = usbd_setup_request(reqh, sc->sc_bulkpipe, 0, buf, n,
379 0, USBD_NO_TIMEOUT, 0);
380 if (r != USBD_NORMAL_COMPLETION) {
381 error = EIO;
382 break;
383 }
384 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
385 r = usbd_sync_transfer(reqh);
386 if (r != USBD_NORMAL_COMPLETION) {
387 DPRINTF(("ulptwrite: error=%d\n", r));
388 usbd_clear_endpoint_stall(sc->sc_bulkpipe);
389 error = EIO;
390 break;
391 }
392 }
393 usbd_free_request(reqh);
394 return (error);
395 }
396
397 int
398 ulptioctl(dev, cmd, data, flag, p)
399 dev_t dev;
400 u_long cmd;
401 caddr_t data;
402 int flag;
403 struct proc *p;
404 {
405 int error = 0;
406
407 switch (cmd) {
408 default:
409 error = ENODEV;
410 }
411
412 return error;
413 }
414