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