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