DEVFS - Bring in Alex's GSOC kernel adjustments.
[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.25 2008/02/11 16:56:53 dillon 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/module.h>
53 #include <sys/bus.h>
54 #include <sys/uio.h>
55 #include <sys/conf.h>
56 #include <sys/device.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/thread2.h>
60
61 #include <bus/usb/usb.h>
62 #include <bus/usb/usbdi.h>
63 #include <bus/usb/usbdi_util.h>
64 #include <bus/usb/usb_quirks.h>
65
66 #define TIMEOUT         hz*16   /* wait up to 16 seconds for a ready */
67 #define STEP            hz/4
68
69 #define ULPT_BSIZE      16384
70
71 #ifdef USB_DEBUG
72 #define DPRINTF(x)      if (ulptdebug) kprintf x
73 #define DPRINTFN(n,x)   if (ulptdebug>(n)) kprintf x
74 int     ulptdebug = 0;
75 SYSCTL_NODE(_hw_usb, OID_AUTO, ulpt, CTLFLAG_RW, 0, "USB ulpt");
76 SYSCTL_INT(_hw_usb_ulpt, OID_AUTO, debug, CTLFLAG_RW,
77            &ulptdebug, 0, "ulpt debug level");
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         device_t sc_dev;
95         usbd_device_handle sc_udev;     /* device */
96         usbd_interface_handle sc_iface; /* interface */
97         int sc_ifaceno;
98
99         int sc_out;
100         usbd_pipe_handle sc_out_pipe;   /* bulk out pipe */
101
102         int sc_in;
103         usbd_pipe_handle sc_in_pipe;    /* bulk in pipe */
104         usbd_xfer_handle sc_in_xfer1;
105         usbd_xfer_handle sc_in_xfer2;
106         u_char sc_junk[64];     /* somewhere to dump input */
107
108         u_char sc_state;
109 #define ULPT_OPEN       0x01    /* device is open */
110 #define ULPT_OBUSY      0x02    /* printer is busy doing output */
111 #define ULPT_INIT       0x04    /* waiting to initialize for open */
112         u_char sc_flags;
113 #define ULPT_NOPRIME    0x40    /* don't prime on open */
114         u_char sc_laststatus;
115
116         int sc_refcnt;
117         u_char sc_dying;
118         int vendor;
119         int product;
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 static device_probe_t ulpt_match;
153 static device_attach_t ulpt_attach;
154 static device_detach_t ulpt_detach;
155
156 static devclass_t ulpt_devclass;
157
158 static kobj_method_t ulpt_methods[] = {
159         DEVMETHOD(device_probe, ulpt_match),
160         DEVMETHOD(device_attach, ulpt_attach),
161         DEVMETHOD(device_detach, ulpt_detach),
162         {0,0}
163 };
164
165 static driver_t ulpt_driver = {
166         "ulpt",
167         ulpt_methods,
168         sizeof(struct ulpt_softc)
169 };
170
171 MODULE_DEPEND(ulpt, usb, 1, 1, 1);
172
173 static int
174 ulpt_match(device_t self)
175 {
176         struct usb_attach_arg *uaa = device_get_ivars(self);
177         usb_interface_descriptor_t *id;
178
179         DPRINTFN(10,("ulpt_match\n"));
180         if (uaa->iface == NULL)
181                 return (UMATCH_NONE);
182         id = usbd_get_interface_descriptor(uaa->iface);
183         if (id != NULL &&
184             id->bInterfaceClass == UICLASS_PRINTER &&
185             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
186             (id->bInterfaceProtocol == UIPROTO_PRINTER_UNI ||
187              id->bInterfaceProtocol == UIPROTO_PRINTER_BI ||
188              id->bInterfaceProtocol == UIPROTO_PRINTER_1284))
189                 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
190         return (UMATCH_NONE);
191 }
192
193 static int
194 ulpt_attach(device_t self)
195 {
196         struct ulpt_softc *sc = device_get_softc(self);
197         struct usb_attach_arg *uaa = device_get_ivars(self);
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         usb_endpoint_descriptor_t *ed;
205         u_int8_t epcount;
206         int i, altno;
207
208         DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
209         sc->sc_dev = self;
210
211         /* XXX
212          * Stepping through the alternate settings needs to be abstracted out.
213          */
214         cdesc = usbd_get_config_descriptor(dev);
215         if (cdesc == NULL) {
216                 kprintf("%s: failed to get configuration descriptor\n",
217                        device_get_nameunit(sc->sc_dev));
218                 return ENXIO;
219         }
220         iend = (usb_interface_descriptor_t *)
221                    ((char *)cdesc + UGETW(cdesc->wTotalLength));
222 #ifdef DIAGNOSTIC
223         if (ifcd < (usb_interface_descriptor_t *)cdesc ||
224             ifcd >= iend)
225                 panic("ulpt: iface desc out of range");
226 #endif
227         /* Step through all the descriptors looking for bidir mode */
228         for (id = ifcd, altno = 0;
229              id < iend;
230              id = (void *)((char *)id + id->bLength)) {
231                 if (id->bDescriptorType == UDESC_INTERFACE &&
232                     id->bInterfaceNumber == ifcd->bInterfaceNumber) {
233                         if (id->bInterfaceClass == UICLASS_PRINTER &&
234                             id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
235                             (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /* ||
236                              id->bInterfaceProtocol == UIPROTO_PRINTER_1284 */))
237                                 goto found;
238                         altno++;
239                 }
240         }
241         id = ifcd;              /* not found, use original */
242  found:
243         if (id != ifcd) {
244                 /* Found a new bidir setting */
245                 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
246                 err = usbd_set_interface(iface, altno);
247                 if (err) {
248                         kprintf("%s: setting alternate interface failed\n",
249                                device_get_nameunit(sc->sc_dev));
250                         sc->sc_dying = 1;
251                         return ENXIO;
252                 }
253         }
254
255         epcount = 0;
256         (void)usbd_endpoint_count(iface, &epcount);
257
258         sc->sc_in = -1;
259         sc->sc_out = -1;
260         sc->vendor = uaa->vendor;
261         sc->product = uaa->product;
262         for (i = 0; i < epcount; i++) {
263                 ed = usbd_interface2endpoint_descriptor(iface, i);
264                 if (ed == NULL) {
265                         kprintf("%s: couldn't get ep %d\n",
266                             device_get_nameunit(sc->sc_dev), i);
267                         return ENXIO;
268                 }
269                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
270                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
271                         sc->sc_in = ed->bEndpointAddress;
272                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
273                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
274                         sc->sc_out = ed->bEndpointAddress;
275                 }
276         }
277         if (sc->sc_out == -1) {
278                 kprintf("%s: could not find bulk out endpoint\n",
279                     device_get_nameunit(sc->sc_dev));
280                 sc->sc_dying = 1;
281                 return ENXIO;
282         }
283
284         if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
285                 /* This device doesn't handle reading properly. */
286                 sc->sc_in = -1;
287         }
288
289         kprintf("%s: using %s-directional mode\n", device_get_nameunit(sc->sc_dev),
290                sc->sc_in >= 0 ? "bi" : "uni");
291
292         DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
293
294         sc->sc_iface = iface;
295         sc->sc_ifaceno = id->bInterfaceNumber;
296         sc->sc_udev = dev;
297
298         dev_ops_add(&ulpt_ops, -1, device_get_unit(self));
299         make_dev(&ulpt_ops, device_get_unit(self),
300                 UID_ROOT, GID_OPERATOR, 0644, "ulpt%d", device_get_unit(self));
301         make_dev(&ulpt_ops,
302                 device_get_unit(self)|ULPT_NOPRIME,
303                 UID_ROOT, GID_OPERATOR, 0644, "unlpt%d", device_get_unit(self));
304
305         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
306                            sc->sc_dev);
307
308         return 0;
309 }
310
311 static int
312 ulpt_detach(device_t self)
313 {
314         struct ulpt_softc *sc = device_get_softc(self);
315
316         DPRINTF(("ulpt_detach: sc=%p\n", sc));
317
318         sc->sc_dying = 1;
319         if (sc->sc_out_pipe != NULL)
320                 usbd_abort_pipe(sc->sc_out_pipe);
321         if (sc->sc_in_pipe != NULL)
322                 usbd_abort_pipe(sc->sc_in_pipe);
323
324         /*
325          * Wait for any ongoing operations to complete before we actually
326          * close things down.
327          */
328
329         crit_enter();
330         --sc->sc_refcnt;
331         if (sc->sc_refcnt >= 0) {
332                 kprintf("%s: waiting for idle\n", device_get_nameunit(sc->sc_dev));
333                 while (sc->sc_refcnt >= 0)
334                         usb_detach_wait(sc->sc_dev);
335                 kprintf("%s: idle wait done\n", device_get_nameunit(sc->sc_dev));
336         }
337         crit_exit();
338
339         dev_ops_remove_minor(&ulpt_ops, /*-1, */device_get_unit(self));
340
341         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
342                            sc->sc_dev);
343
344         return (0);
345 }
346
347 int
348 ulpt_status(struct ulpt_softc *sc)
349 {
350         usb_device_request_t req;
351         usbd_status err;
352         u_char status;
353
354         req.bmRequestType = UT_READ_CLASS_INTERFACE;
355         req.bRequest = UR_GET_PORT_STATUS;
356         USETW(req.wValue, 0);
357         USETW(req.wIndex, sc->sc_ifaceno);
358         USETW(req.wLength, 1);
359         err = usbd_do_request(sc->sc_udev, &req, &status);
360         DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
361         if (!err)
362                 return (status);
363         else
364                 return (0);
365 }
366
367 void
368 ulpt_reset(struct ulpt_softc *sc)
369 {
370         usb_device_request_t req;
371         int other_fails;
372
373         /* The Brother HL1240 doesn't handle UT_WRITE_CLASS_OTHER */
374         other_fails = (sc->vendor == 0x04f9 && sc->product == 0x0006);
375
376         DPRINTFN(1, ("ulpt_reset\n"));
377         req.bRequest = UR_SOFT_RESET;
378         USETW(req.wValue, 0);
379         USETW(req.wIndex, sc->sc_ifaceno);
380         USETW(req.wLength, 0);
381
382         /*
383          * There was a mistake in the USB printer 1.0 spec that gave the
384          * request type as UT_WRITE_CLASS_OTHER; it should have been
385          * UT_WRITE_CLASS_INTERFACE.  Many printers use the old one,
386          * so we try both.
387          */
388         req.bmRequestType = UT_WRITE_CLASS_OTHER;
389         /* Some printers don't handle UT_WRITE_CLASS_OTHER */
390         if (other_fails || usbd_do_request(sc->sc_udev, &req, 0)) {/* 1.0 */
391                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
392                 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
393         }
394 }
395
396 static void
397 ulpt_input(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
398 {
399         struct ulpt_softc *sc = priv;
400         u_int32_t count;
401
402         /* Don't loop on errors or 0-length input. */
403         usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
404         if (status != USBD_NORMAL_COMPLETION || count == 0)
405                 return;
406
407         DPRINTFN(2,("ulpt_input: got some data\n"));
408         /* Do it again. */
409         if (xfer == sc->sc_in_xfer1)
410                 usbd_transfer(sc->sc_in_xfer2);
411         else
412                 usbd_transfer(sc->sc_in_xfer1);
413 }
414
415 int ulptusein = 1;
416
417 /*
418  * Reset the printer, then wait until it's selected and not busy.
419  */
420 int
421 ulptopen(struct dev_open_args *ap)
422 {
423         cdev_t dev = ap->a_head.a_dev;
424         u_char flags = ULPTFLAGS(dev);
425         struct ulpt_softc *sc;
426         usbd_status err;
427         int spin, error;
428
429         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
430         if (sc == NULL)
431                 return (ENXIO);
432
433         if (sc == NULL || sc->sc_iface == NULL || sc->sc_dying)
434                 return (ENXIO);
435
436         if (sc->sc_state)
437                 return (EBUSY);
438
439         sc->sc_state = ULPT_INIT;
440         sc->sc_flags = flags;
441         DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
442
443 #if defined(USB_DEBUG)
444         /* Ignoring these flags might not be a good idea */
445         if ((flags & ~ULPT_NOPRIME) != 0)
446                 kprintf("ulptopen: flags ignored: %b\n", flags,
447                         "\20\3POS_INIT\4POS_ACK\6PRIME_OPEN\7AUTOLF\10BYPASS");
448 #endif
449
450         error = 0;
451         sc->sc_refcnt++;
452
453         if ((flags & ULPT_NOPRIME) == 0) {
454                 ulpt_reset(sc);
455                 if (sc->sc_dying) {
456                         error = ENXIO;
457                         sc->sc_state = 0;
458                         goto done;
459                 }
460         }
461
462         for (spin = 0; (ulpt_status(sc) & LPS_SELECT) == 0; spin += STEP) {
463                 DPRINTF(("ulpt_open: waiting a while\n"));
464                 if (spin >= TIMEOUT) {
465                         error = EBUSY;
466                         sc->sc_state = 0;
467                         goto done;
468                 }
469
470                 /* wait 1/4 second, give up if we get a signal */
471                 error = tsleep((caddr_t)sc, PCATCH, "ulptop", STEP);
472                 if (error != EWOULDBLOCK) {
473                         sc->sc_state = 0;
474                         goto done;
475                 }
476
477                 if (sc->sc_dying) {
478                         error = ENXIO;
479                         sc->sc_state = 0;
480                         goto done;
481                 }
482         }
483
484         err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
485         if (err) {
486                 sc->sc_state = 0;
487                 error = EIO;
488                 goto done;
489         }
490
491         if (ulptusein && sc->sc_in != -1) {
492                 DPRINTF(("ulpt_open: open input pipe\n"));
493                 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
494                 if (err) {
495                         error = EIO;
496                         usbd_close_pipe(sc->sc_out_pipe);
497                         sc->sc_out_pipe = NULL;
498                         sc->sc_state = 0;
499                         goto done;
500                 }
501                 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
502                 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
503                 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
504                         error = ENOMEM;
505                         if (sc->sc_in_xfer1 != NULL) {
506                                 usbd_free_xfer(sc->sc_in_xfer1);
507                                 sc->sc_in_xfer1 = NULL;
508                         }
509                         if (sc->sc_in_xfer2 != NULL) {
510                                 usbd_free_xfer(sc->sc_in_xfer2);
511                                 sc->sc_in_xfer2 = NULL;
512                         }
513                         usbd_close_pipe(sc->sc_out_pipe);
514                         sc->sc_out_pipe = NULL;
515                         usbd_close_pipe(sc->sc_in_pipe);
516                         sc->sc_in_pipe = NULL;
517                         sc->sc_state = 0;
518                         goto done;
519                 }
520                 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
521                     sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
522                     USBD_NO_TIMEOUT, ulpt_input);
523                 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
524                     sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
525                     USBD_NO_TIMEOUT, ulpt_input);
526                 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
527         }
528
529         sc->sc_state = ULPT_OPEN;
530
531 done:
532         if (--sc->sc_refcnt < 0)
533                 usb_detach_wakeup(sc->sc_dev);
534
535         DPRINTF(("ulptopen: done, error=%d\n", error));
536         return (error);
537 }
538
539 int
540 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
541 {
542         u_char new;
543
544         status = (status ^ LPS_INVERT) & LPS_MASK;
545         new = status & ~sc->sc_laststatus;
546         sc->sc_laststatus = status;
547
548         if (new & LPS_SELECT)
549                 log(LOG_NOTICE, "%s: offline\n", device_get_nameunit(sc->sc_dev));
550         else if (new & LPS_NOPAPER)
551                 log(LOG_NOTICE, "%s: out of paper\n", device_get_nameunit(sc->sc_dev));
552         else if (new & LPS_NERR)
553                 log(LOG_NOTICE, "%s: output error\n", device_get_nameunit(sc->sc_dev));
554
555         return (status);
556 }
557
558 int
559 ulptclose(struct dev_close_args *ap)
560 {
561         cdev_t dev = ap->a_head.a_dev;
562         struct ulpt_softc *sc;
563
564         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
565
566         if (sc->sc_state != ULPT_OPEN)
567                 /* We are being forced to close before the open completed. */
568                 return (0);
569
570         if (sc->sc_out_pipe != NULL) {
571                 usbd_close_pipe(sc->sc_out_pipe);
572                 sc->sc_out_pipe = NULL;
573         }
574         if (sc->sc_in_pipe != NULL) {
575                 usbd_abort_pipe(sc->sc_in_pipe);
576                 usbd_close_pipe(sc->sc_in_pipe);
577                 sc->sc_in_pipe = NULL;
578                 if (sc->sc_in_xfer1 != NULL) {
579                         usbd_free_xfer(sc->sc_in_xfer1);
580                         sc->sc_in_xfer1 = NULL;
581                 }
582                 if (sc->sc_in_xfer2 != NULL) {
583                         usbd_free_xfer(sc->sc_in_xfer2);
584                         sc->sc_in_xfer2 = NULL;
585                 }
586         }
587
588         sc->sc_state = 0;
589
590         DPRINTF(("ulptclose: closed\n"));
591         return (0);
592 }
593
594 int
595 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
596 {
597         u_int32_t n;
598         int error = 0;
599         void *bufp;
600         usbd_xfer_handle xfer;
601         usbd_status err;
602
603         DPRINTF(("ulptwrite\n"));
604         xfer = usbd_alloc_xfer(sc->sc_udev);
605         if (xfer == NULL)
606                 return (ENOMEM);
607         bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
608         if (bufp == NULL) {
609                 usbd_free_xfer(xfer);
610                 return (ENOMEM);
611         }
612         while ((n = min(ULPT_BSIZE, uio->uio_resid)) != 0) {
613                 ulpt_statusmsg(ulpt_status(sc), sc);
614                 error = uiomove(bufp, n, uio);
615                 if (error)
616                         break;
617                 DPRINTFN(1, ("ulptwrite: transfer %d bytes\n", n));
618                 err = usbd_bulk_transfer(xfer, sc->sc_out_pipe, USBD_NO_COPY,
619                           USBD_NO_TIMEOUT, bufp, &n, "ulptwr");
620                 if (err) {
621                         DPRINTF(("ulptwrite: error=%d\n", err));
622                         error = EIO;
623                         break;
624                 }
625         }
626         usbd_free_xfer(xfer);
627
628         return (error);
629 }
630
631 int
632 ulptwrite(struct dev_write_args *ap)
633 {
634         cdev_t dev = ap->a_head.a_dev;
635         struct ulpt_softc *sc;
636         int error;
637
638         sc = devclass_get_softc(ulpt_devclass, ULPTUNIT(dev));
639
640         if (sc->sc_dying)
641                 return (EIO);
642
643         sc->sc_refcnt++;
644         error = ulpt_do_write(sc, ap->a_uio, ap->a_ioflag);
645         if (--sc->sc_refcnt < 0)
646                 usb_detach_wakeup(sc->sc_dev);
647         return (error);
648 }
649
650 int
651 ulptioctl(struct dev_ioctl_args *ap)
652 {
653         int error = 0;
654
655         switch (ap->a_cmd) {
656         default:
657                 error = ENODEV;
658         }
659
660         return (error);
661 }
662
663 #if 0
664 /* XXX This does not belong here. */
665 /*
666  * Print select parts of an IEEE 1284 device ID.
667  */
668 void
669 ieee1284_print_id(char *str)
670 {
671         char *p, *q;
672
673         for (p = str-1; p; p = strchr(p, ';')) {
674                 p++;            /* skip ';' */
675                 if (strncmp(p, "MFG:", 4) == 0 ||
676                     strncmp(p, "MANUFACTURER:", 14) == 0 ||
677                     strncmp(p, "MDL:", 4) == 0 ||
678                     strncmp(p, "MODEL:", 6) == 0) {
679                         q = strchr(p, ';');
680                         if (q)
681                                 kprintf("%.*s", (int)(q - p + 1), p);
682                 }
683         }
684 }
685 #endif
686
687 DRIVER_MODULE(ulpt, uhub, ulpt_driver, ulpt_devclass, usbd_driver_load, 0);
688