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