da951a86b8d745c64a6619c11a188f4f6bee0bf9
[dragonfly.git] / sys / dev / usbmisc / ulpt / ulpt.c
1 /*
2  * $NetBSD: ulpt.c,v 1.55 2002/10/23 09:14:01 jdolecek Exp $
3  * $FreeBSD: src/sys/dev/usb/ulpt.c,v 1.59 2003/09/28 20:48:13 phk Exp $
4  * $DragonFly: src/sys/dev/usbmisc/ulpt/ulpt.c,v 1.21 2007/07/02 06:43:31 hasso Exp $
5  */
6
7 /*
8  * Copyright (c) 1998 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Lennart Augustsson (lennart@augustsson.net) at
13  * Carlstedt Research & Technology.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *        This product includes software developed by the NetBSD
26  *        Foundation, Inc. and its contributors.
27  * 4. Neither the name of The NetBSD Foundation nor the names of its
28  *    contributors may be used to endorse or promote products derived
29  *    from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  */
43
44 /*
45  * Printer Class spec: http://www.usb.org/developers/data/devclass/usbprint109.PDF
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/ioccom.h>
53 #include <sys/module.h>
54 #include <sys/bus.h>
55 #include <sys/uio.h>
56 #include <sys/conf.h>
57 #include <sys/device.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
60 #include <sys/thread2.h>
61
62 #include <bus/usb/usb.h>
63 #include <bus/usb/usbdi.h>
64 #include <bus/usb/usbdi_util.h>
65 #include <bus/usb/usbdevs.h>
66 #include <bus/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 ULPT_BSIZE      16384
72
73 #ifdef USB_DEBUG
74 #define DPRINTF(x)      if (ulptdebug) kprintf x
75 #define DPRINTFN(n,x)   if (ulptdebug>(n)) kprintf x
76 int     ulptdebug = 0;
77 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
78 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
79            &ulptdebug, 0, "ulpt debug level");
80 #else
81 #define DPRINTF(x)
82 #define DPRINTFN(n,x)
83 #endif
84
85 #define UR_GET_DEVICE_ID 0
86 #define UR_GET_PORT_STATUS 1
87 #define UR_SOFT_RESET 2
88
89 #define LPS_NERR                0x08    /* printer no error */
90 #define LPS_SELECT              0x10    /* printer selected */
91 #define LPS_NOPAPER             0x20    /* printer out of paper */
92 #define LPS_INVERT      (LPS_SELECT|LPS_NERR)
93 #define LPS_MASK        (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
94
95 struct ulpt_softc {
96         device_t sc_dev;
97         usbd_device_handle sc_udev;     /* device */
98         usbd_interface_handle sc_iface; /* interface */
99         int sc_ifaceno;
100
101         int sc_out;
102         usbd_pipe_handle sc_out_pipe;   /* bulk out pipe */
103
104         int sc_in;
105         usbd_pipe_handle sc_in_pipe;    /* bulk in pipe */
106         usbd_xfer_handle sc_in_xfer1;
107         usbd_xfer_handle sc_in_xfer2;
108         u_char sc_junk[64];     /* somewhere to dump input */
109
110         u_char sc_state;
111 #define ULPT_OPEN       0x01    /* device is open */
112 #define ULPT_OBUSY      0x02    /* printer is busy doing output */
113 #define ULPT_INIT       0x04    /* waiting to initialize for open */
114         u_char sc_flags;
115 #define ULPT_NOPRIME    0x40    /* don't prime on open */
116         u_char sc_laststatus;
117
118         int sc_refcnt;
119         u_char sc_dying;
120 };
121
122 static d_open_t ulptopen;
123 static d_close_t ulptclose;
124 static d_write_t ulptwrite;
125 static d_ioctl_t ulptioctl;
126
127 #define ULPT_CDEV_MAJOR 113
128
129 static struct dev_ops ulpt_ops = {
130         { "ulpt", ULPT_CDEV_MAJOR, 0 },
131         .d_open =       ulptopen,
132         .d_close =      ulptclose,
133         .d_write =      ulptwrite,
134         .d_ioctl =      ulptioctl,
135 };
136
137 void ulpt_disco(void *);
138
139 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
140 int ulpt_status(struct ulpt_softc *);
141 void ulpt_reset(struct ulpt_softc *);
142 int ulpt_statusmsg(u_char, struct ulpt_softc *);
143
144 #if 0
145 void ieee1284_print_id(char *);
146 #endif
147
148 #define ULPTUNIT(s)     (minor(s) & 0x1f)
149 #define ULPTFLAGS(s)    (minor(s) & 0xe0)
150
151
152 USB_DECLARE_DRIVER(ulpt);
153
154 static int
155 ulpt_match(device_t self)
156 {
157         struct usb_attach_arg *uaa = device_get_ivars(self);
158         usb_interface_descriptor_t *id;
159
160         DPRINTFN(10,("ulpt_match\n"));
161         if (uaa->iface == NULL)
162                 return (UMATCH_NONE);
163         id = usbd_get_interface_descriptor(uaa->iface);
164         if (id != NULL &&
165             id->bInterfaceClass == UICLASS_PRINTER &&
166             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
167             (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
168              id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
169              id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
170                 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
171         return (UMATCH_NONE);
172 }
173
174 static int
175 ulpt_attach(device_t self)
176 {
177         struct ulpt_softc *sc = device_get_softc(self);
178         struct usb_attach_arg *uaa = device_get_ivars(self);
179         usbd_device_handle dev = uaa->device;
180         usbd_interface_handle iface = uaa->iface;
181         usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
182         usb_interface_descriptor_t *id, *iend;
183         usb_config_descriptor_t *cdesc;
184         usbd_status err;
185         char devinfo[1024];
186         usb_endpoint_descriptor_t *ed;
187         u_int8_t epcount;
188         int i, altno;
189
190         DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
191         usbd_devinfo(dev, 0, devinfo);
192         sc->sc_dev = self;
193         device_set_desc_copy(self, devinfo);
194         kprintf("%s: %s, iclass %d/%d\n", device_get_nameunit(sc->sc_dev),
195                devinfo, ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
196
197         /* XXX
198          * Stepping through the alternate settings needs to be abstracted out.
199          */
200         cdesc = usbd_get_config_descriptor(dev);
201         if (cdesc == NULL) {
202                 kprintf("%s: failed to get configuration descriptor\n",
203                        device_get_nameunit(sc->sc_dev));
204                 return ENXIO;
205         }
206         iend = (usb_interface_descriptor_t *)
207                    ((char *)cdesc + UGETW(cdesc->wTotalLength));
208 #ifdef DIAGNOSTIC
209         if (ifcd < (usb_interface_descriptor_t *)cdesc ||
210             ifcd >= iend)
211                 panic("ulpt: iface desc out of range");
212 #endif
213         /* Step through all the descriptors looking for bidir mode */
214         for (id = ifcd, altno = 0;
215              id < iend;
216              id = (void *)((char *)id + id->bLength)) {
217                 if (id->bDescriptorType == UDESC_INTERFACE &&
218                     id->bInterfaceNumber == ifcd->bInterfaceNumber) {
219                         if (id->bInterfaceClass == UICLASS_PRINTER &&
220                             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
221                             (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* ||
222                              id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */))
223                                 goto found;
224                         altno++;
225                 }
226         }
227         id = ifcd;              /* not found, use original */
228  found:
229         if (id != ifcd) {
230                 /* Found a new bidir setting */
231                 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
232                 err = usbd_set_interface(iface, altno);
233                 if (err) {
234                         kprintf("%s: setting alternate interface failed\n",
235                                device_get_nameunit(sc->sc_dev));
236                         sc->sc_dying = 1;
237                         return ENXIO;
238                 }
239         }
240
241         epcount = 0;
242         (void)usbd_endpoint_count(iface, &epcount);
243
244         sc->sc_in = -1;
245         sc->sc_out = -1;
246         for (i = 0; i < epcount; i++) {
247                 ed = usbd_interface2endpoint_descriptor(iface, i);
248                 if (ed == NULL) {
249                         kprintf("%s: couldn't get ep %d\n",
250                             device_get_nameunit(sc->sc_dev), i);
251                         return ENXIO;
252                 }
253                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
254                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
255                         sc->sc_in = ed->bEndpointAddress;
256                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
257                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
258                         sc->sc_out = ed->bEndpointAddress;
259                 }
260         }
261         if (sc->sc_out == -1) {
262                 kprintf("%s: could not find bulk out endpoint\n",
263                     device_get_nameunit(sc->sc_dev));
264                 sc->sc_dying = 1;
265                 return ENXIO;
266         }
267
268         if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
269                 /* This device doesn't handle reading properly. */
270                 sc->sc_in = -1;
271         }
272
273         kprintf("%s: using %s-directional mode\n", device_get_nameunit(sc->sc_dev),
274                sc->sc_in >= 0 ? "bi" : "uni");
275
276         DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
277
278         sc->sc_iface = iface;
279         sc->sc_ifaceno = id->bInterfaceNumber;
280         sc->sc_udev = dev;
281
282 #if 0
283 /*
284  * This code is disabled because for some mysterious reason it causes
285  * printing not to work.  But only sometimes, and mostly with
286  * UHCI and less often with OHCI.  *sigh*
287  */
288         {
289         usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
290         usb_device_request_t req;
291         int len, alen;
292
293         req.bmRequestType = UT_READ_CLASS_INTERFACE;
294         req.bRequest = UR_GET_DEVICE_ID;
295         USETW(req.wValue, cd->bConfigurationValue);
296         USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
297         USETW(req.wLength, sizeof devinfo - 1);
298         err = usbd_do_request_flags(dev, &req, devinfo, USBD_SHORT_XFER_OK,
299                   &alen, USBD_DEFAULT_TIMEOUT);
300         if (err) {
301                 kprintf("%s: cannot get device id\n", device_get_nameunit(sc->sc_dev));
302         } else if (alen <= 2) {
303                 kprintf("%s: empty device id, no printer connected?\n",
304                        device_get_nameunit(sc->sc_dev));
305         } else {
306                 /* devinfo now contains an IEEE-1284 device ID */
307                 len = ((devinfo[0] & 0xff) << 8) | (devinfo[1] & 0xff);
308                 if (len > sizeof devinfo - 3)
309                         len = sizeof devinfo - 3;
310                 devinfo[len] = 0;
311                 kprintf("%s: device id <", device_get_nameunit(sc->sc_dev));
312                 ieee1284_print_id(devinfo+2);
313                 kprintf(">\n");
314         }
315         }
316 #endif
317
318         dev_ops_add(&ulpt_ops, -1, device_get_unit(self));
319         make_dev(&ulpt_ops, device_get_unit(self),
320                 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
321         make_dev(&ulpt_ops,
322                 device_get_unit(self)|ULPT_NOPRIME,
323                 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
324
325         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
326                            sc->sc_dev);
327
328         return 0;
329 }
330
331 static int
332 ulpt_detach(device_t self)
333 {
334         struct ulpt_softc *sc = device_get_softc(self);
335
336         DPRINTF(("ulpt_detach: sc=%p\n", sc));
337
338         sc->sc_dying = 1;
339         if (sc->sc_out_pipe != NULL)
340                 usbd_abort_pipe(sc->sc_out_pipe);
341         if (sc->sc_in_pipe != NULL)
342                 usbd_abort_pipe(sc->sc_in_pipe);
343
344         /*
345          * Wait for any ongoing operations to complete before we actually
346          * close things down.
347          */
348
349         crit_enter();
350         --sc->sc_refcnt;
351         if (sc->sc_refcnt >= 0) {
352                 kprintf("%s: waiting for idle\n", device_get_nameunit(sc->sc_dev));
353                 while (sc->sc_refcnt >= 0)
354                         usb_detach_wait(sc->sc_dev);
355                 kprintf("%s: idle wait done\n", device_get_nameunit(sc->sc_dev));
356         }
357         crit_exit();
358
359         dev_ops_remove(&ulpt_ops, -1, device_get_unit(self));
360
361         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
362                            sc->sc_dev);
363
364         return (0);
365 }
366
367 int
368 ulpt_status(struct ulpt_softc *sc)
369 {
370         usb_device_request_t req;
371         usbd_status err;
372         u_char status;
373
374         req.bmRequestType = UT_READ_CLASS_INTERFACE;
375         req.bRequest = UR_GET_PORT_STATUS;
376         USETW(req.wValue, 0);
377         USETW(req.wIndex, sc->sc_ifaceno);
378         USETW(req.wLength, 1);
379         err = usbd_do_request(sc->sc_udev, &req, &status);
380         DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
381         if (!err)
382                 return (status);
383         else
384                 return (0);
385 }
386
387 void
388 ulpt_reset(struct ulpt_softc *sc)
389 {
390         usb_device_request_t req;
391
392         DPRINTFN(1, ("ulpt_reset\n"));
393         req.bRequest = UR_SOFT_RESET;
394         USETW(req.wValue, 0);
395         USETW(req.wIndex, sc->sc_ifaceno);
396         USETW(req.wLength, 0);
397
398         /*
399          * There was a mistake in the USB printer 1.0 spec that gave the
400          * request type as UT_WRITE_CLASS_OTHER; it should have been
401          * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
402          * so we try both.
403          */
404         req.bmRequestType = UT_WRITE_CLASS_OTHER;
405         if (usbd_do_request(sc->sc_udev, &req, 0)) {    /* 1.0 */
406                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
407                 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
408         }
409 }
410
411 static void
412 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
413 {
414         struct ulpt_softc *sc = priv;
415         u_int32_t count;
416
417         /* Don't loop on errors or 0-length input. */
418         usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
419         if (status != USBD_NORMAL_COMPLETION || count == 0)
420                 return;
421
422         DPRINTFN(2,("ulpt_input: got some data\n"));
423         /* Do it again. */
424         if (xfer == sc->sc_in_xfer1)
425                 usbd_transfer(sc->sc_in_xfer2);
426         else
427                 usbd_transfer(sc->sc_in_xfer1);
428 }
429
430 int ulptusein = 1;
431
432 /*
433  * Reset the printer, then wait until it's selected and not busy.
434  */
435 int
436 ulptopen(struct dev_open_args *ap)
437 {
438         cdev_t dev = ap->a_head.a_dev;
439         u_char flags = ULPTFLAGS(dev);
440         struct ulpt_softc *sc;
441         usbd_status err;
442         int spin, error;
443
444         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
445         if (sc == NULL)
446                 return (ENXIO);
447
448         if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
449                 return (ENXIO);
450
451         if (sc->sc_state)
452                 return (EBUSY);
453
454         sc->sc_state = ULPT_INIT;
455         sc->sc_flags = flags;
456         DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
457
458 #if defined(USB_DEBUG)
459         /* Ignoring these flags might not be a good idea */
460         if ((flags & ~ULPT_NOPRIME) != 0)
461                 kprintf("ulptopen: flags ignored: %b\n", flags,
462                         "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
463 #endif
464
465         error = 0;
466         sc->sc_refcnt++;
467
468         if ((flags & ULPT_NOPRIME) == 0) {
469                 ulpt_reset(sc);
470                 if (sc->sc_dying) {
471                         error = ENXIO;
472                         sc->sc_state = 0;
473                         goto done;
474                 }
475         }
476
477         for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
478                 DPRINTF(("ulpt_open: waiting a while\n"));
479                 if (spin >= TIMEOUT) {
480                         error = EBUSY;
481                         sc->sc_state = 0;
482                         goto done;
483                 }
484
485                 /* wait 1/4 second, give up if we get a signal */
486                 error = tsleep((caddr_t)sc, PCATCH, "ulptop", STEP);
487                 if (error != EWOULDBLOCK) {
488                         sc->sc_state = 0;
489                         goto done;
490                 }
491
492                 if (sc->sc_dying) {
493                         error = ENXIO;
494                         sc->sc_state = 0;
495                         goto done;
496                 }
497         }
498
499         err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
500         if (err) {
501                 sc->sc_state = 0;
502                 error = EIO;
503                 goto done;
504         }
505
506         if (ulptusein && sc->sc_in != -1) {
507                 DPRINTF(("ulpt_open: open input pipe\n"));
508                 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
509                 if (err) {
510                         error = EIO;
511                         usbd_close_pipe(sc->sc_out_pipe);
512                         sc->sc_out_pipe = NULL;
513                         sc->sc_state = 0;
514                         goto done;
515                 }
516                 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
517                 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
518                 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
519                         error = ENOMEM;
520                         if (sc->sc_in_xfer1 != NULL) {
521                                 usbd_free_xfer(sc->sc_in_xfer1);
522                                 sc->sc_in_xfer1 = NULL;
523                         }
524                         if (sc->sc_in_xfer2 != NULL) {
525                                 usbd_free_xfer(sc->sc_in_xfer2);
526                                 sc->sc_in_xfer2 = NULL;
527                         }
528                         usbd_close_pipe(sc->sc_out_pipe);
529                         sc->sc_out_pipe = NULL;
530                         usbd_close_pipe(sc->sc_in_pipe);
531                         sc->sc_in_pipe = NULL;
532                         sc->sc_state = 0;
533                         goto done;
534                 }
535                 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
536                     sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
537                     USBD_NO_TIMEOUT, ulpt_input);
538                 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
539                     sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
540                     USBD_NO_TIMEOUT, ulpt_input);
541                 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
542         }
543
544         sc->sc_state = ULPT_OPEN;
545
546 done:
547         if (--sc->sc_refcnt < 0)
548                 usb_detach_wakeup(sc->sc_dev);
549
550         DPRINTF(("ulptopen: done, error=%d\n", error));
551         return (error);
552 }
553
554 int
555 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
556 {
557         u_char new;
558
559         status = (status ^ LPS_INVERT) & LPS_MASK;
560         new = status & ~sc->sc_laststatus;
561         sc->sc_laststatus = status;
562
563         if (new & LPS_SELECT)
564                 log(LOG_NOTICE, "%s: offline\n", device_get_nameunit(sc->sc_dev));
565         else if (new & LPS_NOPAPER)
566                 log(LOG_NOTICE, "%s: out of paper\n", device_get_nameunit(sc->sc_dev));
567         else if (new & LPS_NERR)
568                 log(LOG_NOTICE, "%s: output error\n", device_get_nameunit(sc->sc_dev));
569
570         return (status);
571 }
572
573 int
574 ulptclose(struct dev_close_args *ap)
575 {
576         cdev_t dev = ap->a_head.a_dev;
577         struct ulpt_softc *sc;
578
579         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
580
581         if (sc->sc_state != ULPT_OPEN)
582                 /* We are being forced to close before the open completed. */
583                 return (0);
584
585         if (sc->sc_out_pipe != NULL) {
586                 usbd_close_pipe(sc->sc_out_pipe);
587                 sc->sc_out_pipe = NULL;
588         }
589         if (sc->sc_in_pipe != NULL) {
590                 usbd_abort_pipe(sc->sc_in_pipe);
591                 usbd_close_pipe(sc->sc_in_pipe);
592                 sc->sc_in_pipe = NULL;
593                 if (sc->sc_in_xfer1 != NULL) {
594                         usbd_free_xfer(sc->sc_in_xfer1);
595                         sc->sc_in_xfer1 = NULL;
596                 }
597                 if (sc->sc_in_xfer2 != NULL) {
598                         usbd_free_xfer(sc->sc_in_xfer2);
599                         sc->sc_in_xfer2 = NULL;
600                 }
601         }
602
603         sc->sc_state = 0;
604
605         DPRINTF(("ulptclose: closed\n"));
606         return (0);
607 }
608
609 int
610 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
611 {
612         u_int32_t n;
613         int error = 0;
614         void *bufp;
615         usbd_xfer_handle xfer;
616         usbd_status err;
617
618         DPRINTF(("ulptwrite\n"));
619         xfer = usbd_alloc_xfer(sc->sc_udev);
620         if (xfer == NULL)
621                 return (ENOMEM);
622         bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
623         if (bufp == NULL) {
624                 usbd_free_xfer(xfer);
625                 return (ENOMEM);
626         }
627         while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
628                 ulpt_statusmsg(ulpt_status(sc), sc);
629                 error = uiomove(bufp, n, uio);
630                 if (error)
631                         break;
632                 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
633                 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
634                           USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
635                 if (err) {
636                         DPRINTF(("ulptwrite: error=%d\n", err));
637                         error = EIO;
638                         break;
639                 }
640         }
641         usbd_free_xfer(xfer);
642
643         return (error);
644 }
645
646 int
647 ulptwrite(struct dev_write_args *ap)
648 {
649         cdev_t dev = ap->a_head.a_dev;
650         struct ulpt_softc *sc;
651         int error;
652
653         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
654
655         if (sc->sc_dying)
656                 return (EIO);
657
658         sc->sc_refcnt++;
659         error = ulpt_do_write(sc, ap->a_uio, ap->a_ioflag);
660         if (--sc->sc_refcnt < 0)
661                 usb_detach_wakeup(sc->sc_dev);
662         return (error);
663 }
664
665 int
666 ulptioctl(struct dev_ioctl_args *ap)
667 {
668         int error = 0;
669
670         switch (ap->a_cmd) {
671         default:
672                 error = ENODEV;
673         }
674
675         return (error);
676 }
677
678 #if 0
679 /* XXX This does not belong here. */
680 /*
681  * Print select parts of an IEEE 1284 device ID.
682  */
683 void
684 ieee1284_print_id(char *str)
685 {
686         char *p, *q;
687
688         for (p = str-1; p; p = strchr(p, ';')) {
689                 p++;            /* skip ';' */
690                 if (strncmp(p, "MFG:", 4) == 0 ||
691                     strncmp(p, "MANUFACTURER:", 14) == 0 ||
692                     strncmp(p, "MDL:", 4) == 0 ||
693                     strncmp(p, "MODEL:", 6) == 0) {
694                         q = strchr(p, ';');
695                         if (q)
696                                 kprintf("%.*s", (int)(q - p + 1), p);
697                 }
698         }
699 }
700 #endif
701
702 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
703