]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/ulpt.c
This commit was generated by cvs2svn to compensate for changes in r169962,
[FreeBSD/FreeBSD.git] / sys / dev / usb / ulpt.c
1 /*      $NetBSD: ulpt.c,v 1.60 2003/10/04 21:19:50 augustss Exp $       */
2
3 /*-
4  * Copyright (c) 1998, 2003 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 (lennart@augustsson.net) 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/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 /*
44  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
45  */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
52 #if defined(__NetBSD__) || defined(__OpenBSD__)
53 #include <sys/device.h>
54 #include <sys/ioctl.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/ioccom.h>
57 #include <sys/module.h>
58 #include <sys/bus.h>
59 #endif
60 #include <sys/uio.h>
61 #include <sys/conf.h>
62 #include <sys/syslog.h>
63 #include <sys/sysctl.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include "usbdevs.h"
69 #include <dev/usb/usb_quirks.h>
70
71 #define TIMEOUT         hz*16   /* wait up to 16 seconds for a ready */
72 #define STEP            hz/4
73
74 #define LPTPRI          (PZERO+8)
75 #define ULPT_BSIZE      PAGE_SIZE
76
77 #define ULPT_READS_PER_SEC 5
78 #define ULPT_READ_TIMO 10
79
80 #ifdef USB_DEBUG
81 #define DPRINTF(x)      if (ulptdebug) logprintf x
82 #define DPRINTFN(n,x)   if (ulptdebug>(n)) logprintf x
83 int     ulptdebug = 0;
84 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
85 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
86            &ulptdebug, 0, "ulpt debug level");
87 #else
88 #define DPRINTF(x)
89 #define DPRINTFN(n,x)
90 #endif
91
92 #define UR_GET_DEVICE_ID 0
93 #define UR_GET_PORT_STATUS 1
94 #define UR_SOFT_RESET 2
95
96 #define LPS_NERR                0x08    /* printer no error */
97 #define LPS_SELECT              0x10    /* printer selected */
98 #define LPS_NOPAPER             0x20    /* printer out of paper */
99 #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
100 #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
101
102 struct ulpt_softc {
103         device_t sc_dev;
104         usbd_device_handle sc_udev;     /* device */
105         usbd_interface_handle sc_iface; /* interface */
106         int sc_ifaceno;
107
108         int sc_out;
109         usbd_pipe_handle sc_out_pipe;   /* bulk out pipe */
110         usbd_xfer_handle sc_out_xfer;
111         void *sc_out_buf;
112
113         int sc_in;
114         usbd_pipe_handle sc_in_pipe;    /* bulk in pipe */
115         usbd_xfer_handle sc_in_xfer;
116         void *sc_in_buf;
117
118         usb_callout_t sc_read_callout;
119         int sc_has_callout;
120
121         u_char sc_state;
122 #define ULPT_OPEN       0x01    /* device is open */
123 #define ULPT_OBUSY      0x02    /* printer is busy doing output */
124 #define ULPT_INIT       0x04    /* waiting to initialize for open */
125         u_char sc_flags;
126 #define ULPT_NOPRIME    0x40    /* don't prime on open */
127         u_char sc_laststatus;
128
129         int sc_refcnt;
130         u_char sc_dying;
131
132 #if defined(__FreeBSD__)
133         struct cdev *dev;
134         struct cdev *dev_noprime;
135 #endif
136 };
137
138 #if defined(__NetBSD__)
139 dev_type_open(ulptopen);
140 dev_type_close(ulptclose);
141 dev_type_write(ulptwrite);
142 dev_type_read(ulptread);
143 dev_type_ioctl(ulptioctl);
144
145 const struct cdevsw ulpt_cdevsw = {
146         ulptopen, ulptclose, ulptread, ulptwrite, ulptioctl,
147         nostop, notty, nopoll, nommap, nokqfilter,
148 };
149 #elif defined(__OpenBSD__)
150 cdev_decl(ulpt);
151 #elif defined(__FreeBSD__)
152 static d_open_t ulptopen;
153 static d_close_t ulptclose;
154 static d_write_t ulptwrite;
155 static d_read_t ulptread;
156 static d_ioctl_t ulptioctl;
157
158
159 static struct cdevsw ulpt_cdevsw = {
160         .d_version =    D_VERSION,
161         .d_flags =      D_NEEDGIANT,
162         .d_open =       ulptopen,
163         .d_close =      ulptclose,
164         .d_write =      ulptwrite,
165         .d_read =       ulptread,
166         .d_ioctl =      ulptioctl,
167         .d_name =       "ulpt",
168 #if __FreeBSD_version < 500014
169         .d_bmaj         -1
170 #endif
171 };
172 #endif
173
174 void ulpt_disco(void *);
175
176 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
177 int ulpt_do_read(struct ulpt_softc *, struct uio *uio, int);
178 int ulpt_status(struct ulpt_softc *);
179 void ulpt_reset(struct ulpt_softc *);
180 int ulpt_statusmsg(u_char, struct ulpt_softc *);
181 void ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
182                   usbd_status status);
183 void ulpt_tick(void *xsc);
184
185 #if 0
186 void ieee1284_print_id(char *);
187 #endif
188
189 #define ULPTUNIT(s)     (minor(s) & 0x1f)
190 #define ULPTFLAGS(s)    (minor(s) & 0xe0)
191
192
193 USB_DECLARE_DRIVER(ulpt);
194
195 USB_MATCH(ulpt)
196 {
197         USB_MATCH_START(ulpt, uaa);
198         usb_interface_descriptor_t *id;
199
200         DPRINTFN(10,("ulpt_match\n"));
201         if (uaa->iface == NULL)
202                 return (UMATCH_NONE);
203         id = usbd_get_interface_descriptor(uaa->iface);
204         if (id != NULL &&
205             id->bInterfaceClass == UICLASS_PRINTER &&
206             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
207             (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
208              id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
209              id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
210                 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
211         return (UMATCH_NONE);
212 }
213
214 USB_ATTACH(ulpt)
215 {
216         USB_ATTACH_START(ulpt, sc, uaa);
217         usbd_device_handle dev = uaa->device;
218         usbd_interface_handle iface = uaa->iface;
219         usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
220         usb_interface_descriptor_t *id, *iend;
221         usb_config_descriptor_t *cdesc;
222         usbd_status err;
223         char devinfo[1024];
224         usb_endpoint_descriptor_t *ed;
225         u_int8_t epcount;
226         int i, altno;
227
228         DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
229         usbd_devinfo(uaa->device, USBD_SHOW_INTERFACE_CLASS, devinfo);
230         USB_ATTACH_SETUP;
231
232         /* XXX
233          * Stepping through the alternate settings needs to be abstracted out.
234          */
235         cdesc = usbd_get_config_descriptor(dev);
236         if (cdesc == NULL) {
237                 printf("%s: failed to get configuration descriptor\n",
238                        device_get_nameunit(sc->sc_dev));
239                 USB_ATTACH_ERROR_RETURN;
240         }
241         iend = (usb_interface_descriptor_t *)
242                    ((char *)cdesc + UGETW(cdesc->wTotalLength));
243 #ifdef DIAGNOSTIC
244         if (ifcd < (usb_interface_descriptor_t *)cdesc ||
245             ifcd >= iend)
246                 panic("ulpt: iface desc out of range");
247 #endif
248         /* Step through all the descriptors looking for bidir mode */
249         for (id = ifcd, altno = 0;
250              id < iend;
251              id = (void *)((char *)id + id->bLength)) {
252                 if (id->bDescriptorType == UDESC_INTERFACE &&
253                     id->bInterfaceNumber == ifcd->bInterfaceNumber) {
254                         if (id->bInterfaceClass == UICLASS_PRINTER &&
255                             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
256                             (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* ||
257                              id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */))
258                                 goto found;
259                         altno++;
260                 }
261         }
262         id = ifcd;              /* not found, use original */
263  found:
264         if (id != ifcd) {
265                 /* Found a new bidir setting */
266                 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
267                 err = usbd_set_interface(iface, altno);
268                 if (err) {
269                         printf("%s: setting alternate interface failed\n",
270                                device_get_nameunit(sc->sc_dev));
271                         sc->sc_dying = 1;
272                         USB_ATTACH_ERROR_RETURN;
273                 }
274         }
275
276         epcount = 0;
277         (void)usbd_endpoint_count(iface, &epcount);
278
279         sc->sc_in = -1;
280         sc->sc_out = -1;
281         for (i = 0; i < epcount; i++) {
282                 ed = usbd_interface2endpoint_descriptor(iface, i);
283                 if (ed == NULL) {
284                         printf("%s: couldn't get ep %d\n",
285                             device_get_nameunit(sc->sc_dev), i);
286                         USB_ATTACH_ERROR_RETURN;
287                 }
288                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
289                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
290                         sc->sc_in = ed->bEndpointAddress;
291                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
292                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
293                         sc->sc_out = ed->bEndpointAddress;
294                 }
295         }
296         if (sc->sc_out == -1) {
297                 printf("%s: could not find bulk out endpoint\n",
298                     device_get_nameunit(sc->sc_dev));
299                 sc->sc_dying = 1;
300                 USB_ATTACH_ERROR_RETURN;
301         }
302
303         if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
304                 /* This device doesn't handle reading properly. */
305                 sc->sc_in = -1;
306         }
307
308         printf("%s: using %s-directional mode\n", device_get_nameunit(sc->sc_dev),
309                sc->sc_in >= 0 ? "bi" : "uni");
310
311         DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
312
313         sc->sc_iface = iface;
314         sc->sc_ifaceno = id->bInterfaceNumber;
315         sc->sc_udev = dev;
316
317 #if 0
318 /*
319  * This code is disabled because for some mysterious reason it causes
320  * printing not to work.  But only sometimes, and mostly with
321  * UHCI and less often with OHCI.  *sigh*
322  */
323         {
324         usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
325         usb_device_request_t req;
326         int len, alen;
327
328         req.bmRequestType = UT_READ_CLASS_INTERFACE;
329         req.bRequest = UR_GET_DEVICE_ID;
330         USETW(req.wValue, cd->bConfigurationValue);
331         USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
332         USETW(req.wLength, sizeof devinfo - 1);
333         err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
334                   &alen, USBD_DEFAULT_TIMEOUT);
335         if (err) {
336                 printf("%s: cannot get device id\n", device_get_nameunit(sc->sc_dev));
337         } else if (alen <= 2) {
338                 printf("%s: empty device id, no printer connected?\n",
339                        device_get_nameunit(sc->sc_dev));
340         } else {
341                 /* devinfo now contains an IEEE-1284 device ID */
342                 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
343                 if (len > sizeof devinfo - 3)
344                         len = sizeof devinfo - 3;
345                 devinfo[len] = 0;
346                 printf("%s: device id <", device_get_nameunit(sc->sc_dev));
347                 ieee1284_print_id(devinfo+2);
348                 printf(">\n");
349         }
350         }
351 #endif
352
353 #if defined(__FreeBSD__)
354         sc->dev = make_dev(&ulpt_cdevsw, device_get_unit(self),
355                 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
356         sc->dev_noprime = make_dev(&ulpt_cdevsw,
357                 device_get_unit(self)|ULPT_NOPRIME,
358                 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
359 #endif
360
361         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
362                            USBDEV(sc->sc_dev));
363
364         USB_ATTACH_SUCCESS_RETURN;
365 }
366
367 #if defined(__NetBSD__) || defined(__OpenBSD__)
368 int
369 ulpt_activate(device_t self, enum devact act)
370 {
371         struct ulpt_softc *sc = (struct ulpt_softc *)self;
372
373         switch (act) {
374         case DVACT_ACTIVATE:
375                 return (EOPNOTSUPP);
376
377         case DVACT_DEACTIVATE:
378                 sc->sc_dying = 1;
379                 break;
380         }
381         return (0);
382 }
383 #endif
384
385 USB_DETACH(ulpt)
386 {
387         USB_DETACH_START(ulpt, sc);
388         int s;
389 #if defined(__NetBSD__) || defined(__OpenBSD__)
390         int maj, mn;
391 #endif
392
393 #if defined(__NetBSD__) || defined(__OpenBSD__)
394         DPRINTF(("ulpt_detach: sc=%p flags=%d\n", sc, flags));
395 #elif defined(__FreeBSD__)
396         DPRINTF(("ulpt_detach: sc=%p\n", sc));
397 #endif
398
399         sc->sc_dying = 1;
400         if (sc->sc_out_pipe != NULL)
401                 usbd_abort_pipe(sc->sc_out_pipe);
402         if (sc->sc_in_pipe != NULL)
403                 usbd_abort_pipe(sc->sc_in_pipe);
404
405         s = splusb();
406         if (--sc->sc_refcnt >= 0) {
407                 /* There is noone to wake, aborting the pipe is enough */
408                 /* Wait for processes to go away. */
409                 usb_detach_wait(USBDEV(sc->sc_dev));
410         }
411         splx(s);
412
413 #if defined(__NetBSD__) || defined(__OpenBSD__)
414         /* locate the major number */
415 #if defined(__NetBSD__)
416         maj = cdevsw_lookup_major(&ulpt_cdevsw);
417 #elif defined(__OpenBSD__)
418         for (maj = 0; maj < nchrdev; maj++)
419                 if (cdevsw[maj].d_open == ulptopen)
420                         break;
421 #endif
422
423         /* Nuke the vnodes for any open instances (calls close). */
424         mn = self->dv_unit;
425         vdevgone(maj, mn, mn, VCHR);
426 #elif defined(__FreeBSD__)
427         destroy_dev(sc->dev);
428         destroy_dev(sc->dev_noprime);
429 #endif
430
431         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
432                            USBDEV(sc->sc_dev));
433
434         return (0);
435 }
436
437 int
438 ulpt_status(struct ulpt_softc *sc)
439 {
440         usb_device_request_t req;
441         usbd_status err;
442         u_char status;
443
444         req.bmRequestType = UT_READ_CLASS_INTERFACE;
445         req.bRequest = UR_GET_PORT_STATUS;
446         USETW(req.wValue, 0);
447         USETW(req.wIndex, sc->sc_ifaceno);
448         USETW(req.wLength, 1);
449         err = usbd_do_request(sc->sc_udev, &req, &status);
450         DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
451         if (!err)
452                 return (status);
453         else
454                 return (0);
455 }
456
457 void
458 ulpt_reset(struct ulpt_softc *sc)
459 {
460         usb_device_request_t req;
461
462         DPRINTFN(1, ("ulpt_reset\n"));
463         req.bRequest = UR_SOFT_RESET;
464         USETW(req.wValue, 0);
465         USETW(req.wIndex, sc->sc_ifaceno);
466         USETW(req.wLength, 0);
467
468         /*
469          * There was a mistake in the USB printer 1.0 spec that gave the
470          * request type as UT_WRITE_CLASS_OTHER; it should have been
471          * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
472          * so we try both.
473          */
474         req.bmRequestType = UT_WRITE_CLASS_OTHER;
475         if (usbd_do_request(sc->sc_udev, &req, 0)) {    /* 1.0 */
476                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
477                 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
478         }
479 }
480 #if 0
481 static void
482 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
483 {
484         struct ulpt_softc *sc = priv;
485         u_int32_t count;
486
487         /* Don't loop on errors or 0-length input. */
488         usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
489         if (status != USBD_NORMAL_COMPLETION || count == 0)
490                 return;
491
492         DPRINTFN(2,("ulpt_input: got some data\n"));
493         /* Do it again. */
494         if (xfer == sc->sc_in_xfer1)
495                 usbd_transfer(sc->sc_in_xfer2);
496         else
497                 usbd_transfer(sc->sc_in_xfer1);
498 }
499 #endif
500
501 int ulptusein = 1;
502
503 /*
504  * Reset the printer, then wait until it's selected and not busy.
505  */
506 int
507 ulptopen(struct cdev *dev, int flag, int mode, usb_proc_ptr p)
508 {
509         u_char flags = ULPTFLAGS(dev);
510         struct ulpt_softc *sc;
511         usbd_status err;
512         int error;
513
514         USB_GET_SC_OPEN(ulpt, ULPTUNIT(dev), sc);
515
516         if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
517                 return (ENXIO);
518
519         if (sc->sc_state)
520                 return (EBUSY);
521
522         sc->sc_state = ULPT_INIT;
523         sc->sc_flags = flags;
524         DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
525
526 #if defined(USB_DEBUG) && defined(__FreeBSD__)
527         /* Ignoring these flags might not be a good idea */
528         if ((flags & ~ULPT_NOPRIME) != 0)
529                 printf("ulptopen: flags ignored: %b\n", flags,
530                         "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
531 #endif
532
533
534         error = 0;
535         sc->sc_refcnt++;
536
537         if ((flags & ULPT_NOPRIME) == 0) {
538                 ulpt_reset(sc);
539                 if (sc->sc_dying) {
540                         error = ENXIO;
541                         sc->sc_state = 0;
542                         goto done;
543                 }
544         }
545
546         err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
547         if (err) {
548                 error = EIO;
549                 goto err0;
550         }
551         sc->sc_out_xfer = usbd_alloc_xfer(sc->sc_udev);
552         if (sc->sc_out_xfer == NULL) {
553                 error = ENOMEM;
554                 goto err1;
555         }
556         sc->sc_out_buf = usbd_alloc_buffer(sc->sc_out_xfer, ULPT_BSIZE);
557         if (sc->sc_out_buf == NULL) {
558                 error = ENOMEM;
559                 goto err2;
560         }
561
562         if (ulptusein && sc->sc_in != -1) {
563                 DPRINTF(("ulpt_open: open input pipe\n"));
564                 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
565                 if (err) {
566                         error = EIO;
567                         goto err2;
568                 }
569                 sc->sc_in_xfer = usbd_alloc_xfer(sc->sc_udev);
570                 if (sc->sc_in_xfer == NULL) {
571                         error = ENOMEM;
572                         goto err3;
573                 }
574                 sc->sc_in_buf = usbd_alloc_buffer(sc->sc_in_xfer, ULPT_BSIZE);
575                 if (sc->sc_in_buf == NULL) {
576                         error = ENOMEM;
577                         goto err4;
578                 }
579
580                 /* If it's not opened for read the set up a reader. */
581                 if (!(flag & FREAD)) {
582                         DPRINTF(("ulpt_open: start read callout\n"));
583                         usb_callout_init(sc->sc_read_callout);
584                         usb_callout(sc->sc_read_callout, hz/5, ulpt_tick, sc);
585                         sc->sc_has_callout = 1;
586                 }
587         }
588
589         sc->sc_state = ULPT_OPEN;
590         goto done;
591
592  err4:
593         usbd_free_xfer(sc->sc_in_xfer);
594         sc->sc_in_xfer = NULL;
595  err3:
596         usbd_close_pipe(sc->sc_in_pipe);
597         sc->sc_in_pipe = NULL;
598  err2:
599         usbd_free_xfer(sc->sc_out_xfer);
600         sc->sc_out_xfer = NULL;
601  err1:
602         usbd_close_pipe(sc->sc_out_pipe);
603         sc->sc_out_pipe = NULL;
604  err0:
605         sc->sc_state = 0;
606
607  done:
608         if (--sc->sc_refcnt < 0)
609                 usb_detach_wakeup(USBDEV(sc->sc_dev));
610
611         DPRINTF(("ulptopen: done, error=%d\n", error));
612         return (error);
613 }
614
615 int
616 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
617 {
618         u_char new;
619
620         status = (status ^ LPS_INVERT) & LPS_MASK;
621         new = status & ~sc->sc_laststatus;
622         sc->sc_laststatus = status;
623
624         if (new & LPS_SELECT)
625                 log(LOG_NOTICE, "%s: offline\n", device_get_nameunit(sc->sc_dev));
626         else if (new & LPS_NOPAPER)
627                 log(LOG_NOTICE, "%s: out of paper\n", device_get_nameunit(sc->sc_dev));
628         else if (new & LPS_NERR)
629                 log(LOG_NOTICE, "%s: output error\n", device_get_nameunit(sc->sc_dev));
630
631         return (status);
632 }
633
634 int
635 ulptclose(struct cdev *dev, int flag, int mode, usb_proc_ptr p)
636 {
637         struct ulpt_softc *sc;
638
639         USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
640
641         if (sc->sc_state != ULPT_OPEN)
642                 /* We are being forced to close before the open completed. */
643                 return (0);
644
645         if (sc->sc_has_callout) {
646                 usb_uncallout(sc->sc_read_callout, ulpt_tick, sc);
647                 sc->sc_has_callout = 0;
648         }
649
650         if (sc->sc_out_pipe != NULL) {
651                 usbd_abort_pipe(sc->sc_out_pipe);
652                 usbd_close_pipe(sc->sc_out_pipe);
653                 sc->sc_out_pipe = NULL;
654         }
655         if (sc->sc_out_xfer != NULL) {
656                 usbd_free_xfer(sc->sc_out_xfer);
657                 sc->sc_out_xfer = NULL;
658         }
659
660         if (sc->sc_in_pipe != NULL) {
661                 usbd_abort_pipe(sc->sc_in_pipe);
662                 usbd_close_pipe(sc->sc_in_pipe);
663                 sc->sc_in_pipe = NULL;
664         }
665         if (sc->sc_in_xfer != NULL) {
666                 usbd_free_xfer(sc->sc_in_xfer);
667                 sc->sc_in_xfer = NULL;
668         }
669
670         sc->sc_state = 0;
671
672         DPRINTF(("ulptclose: closed\n"));
673         return (0);
674 }
675
676 int
677 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
678 {
679         u_int32_t n;
680         int error = 0;
681         void *bufp;
682         usbd_xfer_handle xfer;
683         usbd_status err;
684
685         DPRINTF(("ulptwrite\n"));
686         xfer = sc->sc_out_xfer;
687         bufp = sc->sc_out_buf;
688         while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
689                 ulpt_statusmsg(ulpt_status(sc), sc);
690                 error = uiomove(bufp, n, uio);
691                 if (error)
692                         break;
693                 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
694                 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
695                           USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
696                 if (err) {
697                         DPRINTF(("ulptwrite: error=%d\n", err));
698                         error = EIO;
699                         break;
700                 }
701         }
702
703         return (error);
704 }
705
706 int
707 ulptwrite(struct cdev *dev, struct uio *uio, int flags)
708 {
709         struct ulpt_softc *sc;
710         int error;
711
712         USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
713
714         if (sc->sc_dying)
715                 return (EIO);
716
717         sc->sc_refcnt++;
718         error = ulpt_do_write(sc, uio, flags);
719         if (--sc->sc_refcnt < 0)
720                 usb_detach_wakeup(USBDEV(sc->sc_dev));
721         return (error);
722 }
723
724 int
725 ulpt_do_read(struct ulpt_softc *sc, struct uio *uio, int flags)
726 {
727         u_int32_t n, on;
728         int error = 0;
729         void *bufp;
730         usbd_xfer_handle xfer;
731         usbd_status err;
732
733         DPRINTF(("ulptread\n"));
734
735         if (sc->sc_in_pipe == NULL)
736                 return 0;
737
738         xfer = sc->sc_in_xfer;
739         bufp = sc->sc_in_buf;
740         while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
741                 DPRINTFN(1, ("ulptread: transfer %d bytes\n", n));
742                 on = n;
743                 err = usbd_bulk_transfer(xfer, sc->sc_in_pipe,
744                           USBD_NO_COPY | USBD_SHORT_XFER_OK,
745                           USBD_NO_TIMEOUT, bufp, &n, "ulptrd");
746                 if (err) {
747                         DPRINTF(("ulptread: error=%d\n", err));
748                         error = EIO;
749                         break;
750                 }
751                 error = uiomove(bufp, n, uio);
752                 if (error)
753                         break;
754                 if (on != n)
755                         break;
756         }
757
758         return (error);
759 }
760
761 int
762 ulptread(struct cdev *dev, struct uio *uio, int flags)
763 {
764         struct ulpt_softc *sc;
765         int error;
766
767         USB_GET_SC(ulpt, ULPTUNIT(dev), sc);
768
769         if (sc->sc_dying)
770                 return (EIO);
771
772         sc->sc_refcnt++;
773         error = ulpt_do_read(sc, uio, flags);
774         if (--sc->sc_refcnt < 0)
775                 usb_detach_wakeup(USBDEV(sc->sc_dev));
776         return (error);
777 }
778
779 void
780 ulpt_read_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
781              usbd_status status)
782 {
783         usbd_status err;
784         u_int32_t n;
785         usbd_private_handle xsc;
786         struct ulpt_softc *sc;
787
788         usbd_get_xfer_status(xfer, &xsc, NULL, &n, &err);
789         sc = xsc;
790
791         DPRINTFN(1,("ulpt_read_cb: start sc=%p, err=%d n=%d\n", sc, err, n));
792
793 #ifdef ULPT_DEBUG
794         if (!err && n > 0)
795                 DPRINTF(("ulpt_tick: discarding %d bytes\n", n));
796 #endif
797         if (!err || err == USBD_TIMEOUT)
798                 usb_callout(sc->sc_read_callout, hz / ULPT_READS_PER_SEC,
799                             ulpt_tick, sc);
800 }
801
802 void
803 ulpt_tick(void *xsc)
804 {
805         struct ulpt_softc *sc = xsc;
806         usbd_status err;
807
808         if (sc == NULL || sc->sc_dying)
809                 return;
810
811         DPRINTFN(1,("ulpt_tick: start sc=%p\n", sc));
812
813         usbd_setup_xfer(sc->sc_in_xfer, sc->sc_in_pipe, sc, sc->sc_in_buf,
814                         ULPT_BSIZE, USBD_NO_COPY | USBD_SHORT_XFER_OK,
815                         ULPT_READ_TIMO, ulpt_read_cb);
816         err = usbd_transfer(sc->sc_in_xfer);
817         DPRINTFN(1,("ulpt_tick: err=%d\n", err));
818 }
819
820 int
821 ulptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
822 {
823         int error = 0;
824
825         switch (cmd) {
826         default:
827                 error = ENODEV;
828         }
829
830         return (error);
831 }
832
833 #if 0
834 /* XXX This does not belong here. */
835 /*
836  * Print select parts of an IEEE 1284 device ID.
837  */
838 void
839 ieee1284_print_id(char *str)
840 {
841         char *p, *q;
842
843         for (p = str-1; p; p = strchr(p, ';')) {
844                 p++;            /* skip ';' */
845                 if (strncmp(p, "MFG:", 4) == 0 ||
846                     strncmp(p, "MANUFACTURER:", 14) == 0 ||
847                     strncmp(p, "MDL:", 4) == 0 ||
848                     strncmp(p, "MODEL:", 6) == 0) {
849                         q = strchr(p, ';');
850                         if (q)
851                                 printf("%.*s", (int)(q - p + 1), p);
852                 }
853         }
854 }
855 #endif
856
857 #if defined(__FreeBSD__)
858 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
859 #endif