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