]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/ulpt.c
This commit was generated by cvs2svn to compensate for changes in r50276,
[FreeBSD/FreeBSD.git] / sys / dev / usb / ulpt.c
1 /*      $NetBSD: ulpt.c,v 1.11 1999/01/10 11:13:36 augustss Exp $       */
2 /*      $FreeBSD$       */
3
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (augustss@carlstedt.se) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 /*
42  * Printer Class spec: http://www.usb.org/developers/data/usbprn10.pdf
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #if defined(__NetBSD__)
51 #include <sys/device.h>
52 #include <sys/ioctl.h>
53 #elif defined(__FreeBSD__)
54 #include <sys/ioccom.h>
55 #include <sys/module.h>
56 #include <sys/bus.h>
57 #endif
58 #include <sys/uio.h>
59 #include <sys/conf.h>
60 #include <sys/syslog.h>
61
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdevs.h>
66 #include <dev/usb/usb_quirks.h>
67
68 #define TIMEOUT         hz*16   /* wait up to 16 seconds for a ready */
69 #define STEP            hz/4
70
71 #define LPTPRI          (PZERO+8)
72 #define ULPT_BSIZE      1024
73
74 #ifdef ULPT_DEBUG
75 #define DPRINTF(x)      if (ulptdebug) logprintf x
76 #define DPRINTFN(n,x)   if (ulptdebug>(n)) logprintf x
77 int     ulptdebug = 1;
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n,x)
81 #endif
82
83 #define UR_GET_DEVICE_ID 0
84 #define UR_GET_PORT_STATUS 1
85 #define UR_SOFT_RESET 2
86
87 #define LPS_NERR                0x08    /* printer no error */
88 #define LPS_SELECT              0x10    /* printer selected */
89 #define LPS_NOPAPER             0x20    /* printer out of paper */
90 #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
91 #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
92
93 struct ulpt_softc {
94         bdevice sc_dev;
95         usbd_device_handle sc_udev;     /* device */
96         usbd_interface_handle sc_iface; /* interface */
97         int sc_ifaceno;
98         usbd_pipe_handle sc_bulkpipe;   /* bulk pipe */
99         int sc_bulk;
100
101         u_char sc_state;
102 #define ULPT_OPEN       0x01    /* device is open */
103 #define ULPT_OBUSY      0x02    /* printer is busy doing output */
104 #define ULPT_INIT       0x04    /* waiting to initialize for open */
105         u_char sc_flags;
106 #if defined(__NetBSD__)
107 #define ULPT_NOPRIME    0x40    /* don't prime on open */
108 #elif defined(__FreeBSD__)
109 /* values taken from i386/isa/lpt.c */
110 #define ULPT_POS_INIT   0x04    /* if we are a postive init signal */
111 #define ULPT_POS_ACK    0x08    /* if we are a positive going ack */
112 #define ULPT_NOPRIME    0x10    /* don't prime the printer at all */
113 #define ULPT_PRIMEOPEN  0x20    /* prime on every open */
114 #define ULPT_AUTOLF     0x40    /* tell printer to do an automatic lf */
115 #define ULPT_BYPASS     0x80    /* bypass  printer ready checks */
116 #endif
117         u_char sc_laststatus;
118 };
119
120 #if defined(__NetBSD__)
121 int ulptopen __P((dev_t, int, int, struct proc *));
122 int ulptclose __P((dev_t, int, int, struct proc *p));
123 int ulptwrite __P((dev_t, struct uio *uio, int));
124 int ulptioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
125 #elif defined(__FreeBSD__)
126 static d_open_t ulptopen;
127 static d_close_t ulptclose;
128 static d_write_t ulptwrite;
129 static d_ioctl_t ulptioctl;
130
131 #define ULPT_CDEV_MAJOR 113
132
133 static struct cdevsw ulpt_cdevsw = {
134         /* open */      ulptopen,
135         /* close */     ulptclose,
136         /* read */      noread,
137         /* write */     ulptwrite,
138         /* ioctl */     ulptioctl,
139         /* stop */      nostop,
140         /* reset */     noreset,
141         /* devtotty */  nodevtotty,
142         /* poll */      nopoll,
143         /* mmap */      nommap,
144         /* strategy */  nostrategy,
145         /* name */      "ulpt",
146         /* parms */     noparms,
147         /* maj */       ULPT_CDEV_MAJOR,
148         /* dump */      nodump,
149         /* psize */     nopsize,
150         /* flags */     0,
151         /* maxio */     0,
152         /* bmaj */      -1
153 };
154 #endif
155
156 int ulpt_status __P((struct ulpt_softc *));
157 void ulpt_reset __P((struct ulpt_softc *));
158 int ulpt_statusmsg __P((u_char, struct ulpt_softc *));
159
160 #if defined(__NetBSD__)
161 #define ULPTUNIT(s)     (minor(s) & 0x1f)
162 #define ULPTFLAGS(s)    (minor(s) & 0xe0)
163 #elif defined(__FreeBSD__)
164 /* defines taken from i386/isa/lpt.c */
165 #define ULPTUNIT(s)     (minor(s) & 0x03)
166 #define ULPTFLAGS(s)    (minor(s) & 0xfc)
167 #endif
168
169 USB_DECLARE_DRIVER(ulpt);
170
171 USB_MATCH(ulpt)
172 {
173         USB_MATCH_START(ulpt, uaa);
174         usb_interface_descriptor_t *id;
175         
176         DPRINTFN(10,("ulpt_match\n"));
177         if (!uaa->iface)
178                 return (UMATCH_NONE);
179         id = usbd_get_interface_descriptor(uaa->iface);
180         if (id &&
181             id->bInterfaceClass == UCLASS_PRINTER &&
182             id->bInterfaceSubClass == USUBCLASS_PRINTER &&
183             (id->bInterfaceProtocol == UPROTO_PRINTER_UNI ||
184              id->bInterfaceProtocol == UPROTO_PRINTER_BI))
185                 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
186         return (UMATCH_NONE);
187 }
188
189 USB_ATTACH(ulpt)
190 {
191         USB_ATTACH_START(ulpt, sc, uaa);
192         usbd_device_handle dev = uaa->device;
193         usbd_interface_handle iface = uaa->iface;
194         usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
195         usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
196         usb_device_request_t req;
197         char devinfo[1024];
198         usb_endpoint_descriptor_t *ed;
199         usbd_status r;
200         
201         DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
202         usbd_devinfo(dev, 0, devinfo);
203         USB_ATTACH_SETUP;
204         printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
205                devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
206
207         /* Figure out which endpoint is the bulk out endpoint. */
208         ed = usbd_interface2endpoint_descriptor(iface, 0);
209         if (!ed)
210                 goto nobulk;
211         if ((ed->bEndpointAddress & UE_DIR) != UE_OUT ||
212             (ed->bmAttributes & UE_XFERTYPE) != UE_BULK) {
213                 /* In case we are using a bidir protocol... */
214                 ed = usbd_interface2endpoint_descriptor(iface, 1);
215                 if (!ed)
216                         goto nobulk;
217                 if ((ed->bEndpointAddress & UE_IN) != UE_OUT ||
218                     (ed->bmAttributes & UE_XFERTYPE) != UE_BULK)
219                         goto nobulk;
220         }
221         sc->sc_bulk = ed->bEndpointAddress;
222         DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_bulk));
223
224         sc->sc_iface = iface;
225         r = usbd_interface2device_handle(iface, &sc->sc_udev);
226         if (r != USBD_NORMAL_COMPLETION)
227                 USB_ATTACH_ERROR_RETURN;
228         sc->sc_ifaceno = id->bInterfaceNumber;
229
230         req.bmRequestType = UT_READ_CLASS_INTERFACE;
231         req.bRequest = UR_GET_DEVICE_ID;
232         USETW(req.wValue, cd->bConfigurationValue);
233         USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
234         USETW(req.wLength, sizeof devinfo - 1);
235         r = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK, NULL);
236         if (r == USBD_NORMAL_COMPLETION) {
237                 int len;
238                 char *idstr;
239                 len = (devinfo[0] << 8) | (devinfo[1] & 0xff);
240                 /* devinfo now contains an IEEE-1284 device ID */
241                 idstr = devinfo+2;
242                 idstr[len] = 0;
243                 printf("%s: device id <%s>\n", USBDEVNAME(sc->sc_dev), idstr);
244         } else {
245                 printf("%s: cannot get device id\n", USBDEVNAME(sc->sc_dev));
246         }
247
248         USB_ATTACH_SUCCESS_RETURN;
249
250  nobulk:
251         printf("%s: could not find bulk endpoint\n", USBDEVNAME(sc->sc_dev));
252         USB_ATTACH_ERROR_RETURN;
253 }
254
255 int
256 ulpt_status(sc)
257         struct ulpt_softc *sc;
258 {
259         usb_device_request_t req;
260         usbd_status r;
261         u_char status;
262
263         req.bmRequestType = UT_READ_CLASS_INTERFACE;
264         req.bRequest = UR_GET_PORT_STATUS;
265         USETW(req.wValue, 0);
266         USETW(req.wIndex, sc->sc_ifaceno);
267         USETW(req.wLength, 1);
268         r = usbd_do_request(sc->sc_udev, &req, &status);
269         DPRINTFN(1, ("ulpt_status: status=0x%02x r=%d\n", status, r));
270         if (r == USBD_NORMAL_COMPLETION)
271                 return (status);
272         else
273                 return (0);
274 }
275
276 void
277 ulpt_reset(sc)
278         struct ulpt_softc *sc;
279 {
280         usb_device_request_t req;
281
282         DPRINTFN(1, ("ulpt_reset\n"));
283         req.bmRequestType = UT_WRITE_CLASS_OTHER;
284         req.bRequest = UR_SOFT_RESET;
285         USETW(req.wValue, 0);
286         USETW(req.wIndex, sc->sc_ifaceno);
287         USETW(req.wLength, 0);
288         (void)usbd_do_request(sc->sc_udev, &req, 0);
289 }
290
291 /*
292  * Reset the printer, then wait until it's selected and not busy.
293  */
294 int
295 ulptopen(dev, flag, mode, p)
296         dev_t dev;
297         int flag;
298         int mode;
299         struct proc *p;
300 {
301         u_char flags = ULPTFLAGS(dev);
302         usbd_status r;
303         int spin, error;
304         USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
305
306         if (!sc || !sc->sc_iface)
307                 return ENXIO;
308
309         if (sc->sc_state)
310                 return EBUSY;
311
312         sc->sc_state = ULPT_INIT;
313         sc->sc_flags = flags;
314         DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
315
316 #if defined(ULPT_DEBUG) && defined(__FreeBSD__)
317         /* Ignoring these flags might not be a good idea */
318         if ((flags & ~ULPT_NOPRIME) != 0)
319                 printf("ulptopen: flags ignored: %b\n", flags,
320                         "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
321 #endif
322         if ((flags & ULPT_NOPRIME) == 0)
323                 ulpt_reset(sc);
324
325         for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
326                 if (spin >= TIMEOUT) {
327                         sc->sc_state = 0;
328                         return EBUSY;
329                 }
330
331                 /* wait 1/4 second, give up if we get a signal */
332                 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "ulptop", STEP);
333                 if (error != EWOULDBLOCK) {
334                         sc->sc_state = 0;
335                         return error;
336                 }
337         }
338
339         r = usbd_open_pipe(sc->sc_iface, sc->sc_bulk, 0, &sc->sc_bulkpipe);
340         if (r != USBD_NORMAL_COMPLETION) {
341                 sc->sc_state = 0;
342                 return (EIO);
343         }
344
345         sc->sc_state = ULPT_OPEN;
346
347         DPRINTF(("ulptopen: done\n"));
348         return (0);
349 }
350
351 int
352 ulpt_statusmsg(status, sc)
353         u_char status;
354         struct ulpt_softc *sc;
355 {
356         u_char new;
357
358         status = (status ^ LPS_INVERT) & LPS_MASK;
359         new = status & ~sc->sc_laststatus;
360         sc->sc_laststatus = status;
361
362         if (new & LPS_SELECT)
363                 log(LOG_NOTICE, "%s: offline\n", USBDEVNAME(sc->sc_dev));
364         else if (new & LPS_NOPAPER)
365                 log(LOG_NOTICE, "%s: out of paper\n", USBDEVNAME(sc->sc_dev));
366         else if (new & LPS_NERR)
367                 log(LOG_NOTICE, "%s: output error\n", USBDEVNAME(sc->sc_dev));
368
369         return status;
370 }
371
372 int
373 ulptclose(dev, flag, mode, p)
374         dev_t dev;
375         int flag;
376         int mode;
377         struct proc *p;
378 {
379         USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
380
381         usbd_close_pipe(sc->sc_bulkpipe);
382
383         sc->sc_state = 0;
384
385         return (0);
386 }
387
388 int
389 ulptwrite(dev, uio, flags)
390         dev_t dev;
391         struct uio *uio;
392         int flags;
393 {
394         size_t n;
395         int error = 0;
396         char buf[ULPT_BSIZE];
397         usbd_request_handle reqh;
398         usbd_status r;
399         USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
400
401         reqh = usbd_alloc_request();
402         if (reqh == 0)
403                 return (ENOMEM);
404         while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
405                 ulpt_statusmsg(ulpt_status(sc), sc);
406                 error = uiomove(buf, n, uio);
407                 if (error)
408                         break;
409                 /* XXX use callback to enable interrupt? */
410                 r = usbd_setup_request(reqh, sc->sc_bulkpipe, 0, buf, n,
411                                        0, USBD_NO_TIMEOUT, 0);
412                 if (r != USBD_NORMAL_COMPLETION) {
413                         error = EIO;
414                         break;
415                 }
416                 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
417                 r = usbd_sync_transfer(reqh);
418                 if (r != USBD_NORMAL_COMPLETION) {
419                         DPRINTFN(1, ("ulptwrite: error=%d\n", r));
420                         usbd_clear_endpoint_stall(sc->sc_bulkpipe);
421                         error = EIO;
422                         break;
423                 }
424         }
425         usbd_free_request(reqh);
426         return (error);
427 }
428
429 int
430 ulptioctl(dev, cmd, data, flag, p)
431         dev_t dev;
432         u_long cmd;
433         caddr_t data;
434         int flag;
435         struct proc *p;
436 {
437         int error = 0;
438
439         switch (cmd) {
440         default:
441                 error = ENODEV;
442         }
443
444         return error;
445 }
446
447 #if defined(__FreeBSD__)
448 static int
449 ulpt_detach(device_t self)
450 {       
451         DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
452         device_set_desc(self, NULL);
453         return 0;
454 }
455
456 DEV_DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, ulpt_cdevsw, usbd_driver_load, 0);
457 #endif