ucom.c revision 1.19 1 /* $NetBSD: ucom.c,v 1.19 2000/04/06 13:32:28 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/ioctl.h>
44 #include <sys/conf.h>
45 #include <sys/tty.h>
46 #include <sys/file.h>
47 #include <sys/select.h>
48 #include <sys/proc.h>
49 #include <sys/vnode.h>
50 #include <sys/device.h>
51 #include <sys/poll.h>
52
53 #include <dev/usb/usb.h>
54
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdevs.h>
58 #include <dev/usb/usb_quirks.h>
59
60 #include <dev/usb/ucomvar.h>
61
62 #include "ucom.h"
63
64 #if NUCOM > 0
65
66 #ifdef UCOM_DEBUG
67 #define DPRINTFN(n, x) if (ucomdebug > (n)) logprintf x
68 int ucomdebug = 0;
69 #else
70 #define DPRINTFN(n, x)
71 #endif
72 #define DPRINTF(x) DPRINTFN(0, x)
73
74 #define UCOMUNIT_MASK 0x3ffff
75 #define UCOMDIALOUT_MASK 0x80000
76 #define UCOMCALLUNIT_MASK 0x40000
77
78 #define UCOMUNIT(x) (minor(x) & UCOMUNIT_MASK)
79 #define UCOMDIALOUT(x) (minor(x) & UCOMDIALOUT_MASK)
80 #define UCOMCALLUNIT(x) (minor(x) & UCOMCALLUNIT_MASK)
81
82 struct ucom_softc {
83 USBBASEDEVICE sc_dev; /* base device */
84
85 usbd_device_handle sc_udev; /* USB device */
86
87 usbd_interface_handle sc_iface; /* data interface */
88
89 int sc_bulkin_no; /* bulk in endpoint address */
90 usbd_pipe_handle sc_bulkin_pipe; /* bulk in pipe */
91 usbd_xfer_handle sc_ixfer; /* read request */
92 u_char *sc_ibuf; /* read buffer */
93 u_int sc_ibufsize; /* read buffer size */
94
95 int sc_bulkout_no; /* bulk out endpoint address */
96 usbd_pipe_handle sc_bulkout_pipe;/* bulk out pipe */
97 usbd_xfer_handle sc_oxfer; /* write request */
98 u_char *sc_obuf; /* write buffer */
99 u_int sc_obufsize; /* write buffer size */
100
101 struct ucom_methods *sc_methods;
102 void *sc_parent;
103 int sc_portno;
104
105 struct tty *sc_tty; /* our tty */
106 u_char sc_lsr;
107 u_char sc_msr;
108 u_char sc_mcr;
109 u_char sc_tx_stopped;
110 int sc_swflags;
111
112 u_char sc_opening; /* lock during open */
113 int sc_refcnt;
114 u_char sc_dying; /* disconnecting */
115 };
116
117 cdev_decl(ucom);
118
119 Static void ucom_cleanup __P((struct ucom_softc *));
120 Static void ucom_hwiflow __P((struct ucom_softc *));
121 Static int ucomparam __P((struct tty *, struct termios *));
122 Static void ucomstart __P((struct tty *));
123 Static void ucom_shutdown __P((struct ucom_softc *));
124 Static int ucom_do_ioctl __P((struct ucom_softc *, u_long, caddr_t,
125 int, struct proc *));
126 Static void ucom_dtr __P((struct ucom_softc *, int));
127 Static void ucom_rts __P((struct ucom_softc *, int));
128 Static void ucom_break __P((struct ucom_softc *, int));
129 Static usbd_status ucomstartread __P((struct ucom_softc *));
130 Static void ucomreadcb __P((usbd_xfer_handle, usbd_private_handle,
131 usbd_status status));
132 Static void ucomwritecb __P((usbd_xfer_handle, usbd_private_handle,
133 usbd_status status));
134 Static void tiocm_to_ucom __P((struct ucom_softc *, int, int));
135 Static int ucom_to_tiocm __P((struct ucom_softc *));
136
137 USB_DECLARE_DRIVER(ucom);
138
139 USB_MATCH(ucom)
140 {
141 return (1);
142 }
143
144 USB_ATTACH(ucom)
145 {
146 struct ucom_softc *sc = (struct ucom_softc *)self;
147 struct ucom_attach_args *uca = aux;
148 struct tty *tp;
149
150 if (uca->portno != UCOM_UNK_PORTNO)
151 printf(": portno %d", uca->portno);
152 printf("\n");
153
154 sc->sc_udev = uca->device;
155 sc->sc_iface = uca->iface;
156 sc->sc_bulkout_no = uca->bulkout;
157 sc->sc_bulkin_no = uca->bulkin;
158 sc->sc_ibufsize = uca->ibufsize;
159 sc->sc_obufsize = uca->obufsize;
160 sc->sc_methods = uca->methods;
161 sc->sc_parent = uca->arg;
162 sc->sc_portno = uca->portno;
163
164 tp = ttymalloc();
165 tp->t_oproc = ucomstart;
166 tp->t_param = ucomparam;
167 sc->sc_tty = tp;
168
169 DPRINTF(("ucom_attach: tty_attach %p\n", tp));
170 tty_attach(tp);
171
172 USB_ATTACH_SUCCESS_RETURN;
173 }
174
175 USB_DETACH(ucom)
176 {
177 struct ucom_softc *sc = (struct ucom_softc *)self;
178 int maj, mn;
179 int s;
180
181 DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p\n",
182 sc, flags, sc->sc_tty));
183
184 sc->sc_dying = 1;
185
186 #ifdef DIAGNOSTIC
187 if (sc->sc_tty == NULL) {
188 DPRINTF(("ucom_detach: no tty\n"));
189 return (0);
190 }
191 #endif
192
193 s = splusb();
194 if (--sc->sc_refcnt >= 0) {
195 /* Wake everyone.. how? */
196 /* Wait for processes to go away. */
197 usb_detach_wait(USBDEV(sc->sc_dev));
198 }
199 splx(s);
200
201 /* locate the major number */
202 for (maj = 0; maj < nchrdev; maj++)
203 if (cdevsw[maj].d_open == ucomopen)
204 break;
205
206 /* Nuke the vnodes for any open instances. */
207 mn = self->dv_unit;
208 DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn));
209 vdevgone(maj, mn, mn, VCHR);
210 vdevgone(maj, mn, mn | UCOMDIALOUT_MASK, VCHR);
211 vdevgone(maj, mn, mn | UCOMCALLUNIT_MASK, VCHR);
212
213 /* Detach and free the tty. */
214 tty_detach(sc->sc_tty);
215 ttyfree(sc->sc_tty);
216 sc->sc_tty = 0;
217
218 return (0);
219 }
220
221 #if defined(__NetBSD__) || defined(__OpenBSD__)
222 int
223 ucom_activate(self, act)
224 device_ptr_t self;
225 enum devact act;
226 {
227 struct ucom_softc *sc = (struct ucom_softc *)self;
228
229 switch (act) {
230 case DVACT_ACTIVATE:
231 return (EOPNOTSUPP);
232 break;
233
234 case DVACT_DEACTIVATE:
235 sc->sc_dying = 1;
236 break;
237 }
238 return (0);
239 }
240 #endif
241
242 void
243 ucom_shutdown(sc)
244 struct ucom_softc *sc;
245 {
246 struct tty *tp = sc->sc_tty;
247
248 DPRINTF(("ucom_shutdown\n"));
249 /*
250 * Hang up if necessary. Wait a bit, so the other side has time to
251 * notice even if we immediately open the port again.
252 */
253 if (ISSET(tp->t_cflag, HUPCL)) {
254 ucom_dtr(sc, 0);
255 (void)tsleep(sc, TTIPRI, ttclos, hz);
256 }
257 }
258
259 int
260 ucomopen(dev, flag, mode, p)
261 dev_t dev;
262 int flag, mode;
263 struct proc *p;
264 {
265 int unit = UCOMUNIT(dev);
266 usbd_status err;
267 struct ucom_softc *sc;
268 struct tty *tp;
269 int s;
270 int error;
271
272 if (unit >= ucom_cd.cd_ndevs)
273 return (ENXIO);
274 sc = ucom_cd.cd_devs[unit];
275 if (sc == NULL)
276 return (ENXIO);
277
278 if (sc->sc_dying)
279 return (EIO);
280
281 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
282 return (ENXIO);
283
284 tp = sc->sc_tty;
285
286 DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
287
288 if (ISSET(tp->t_state, TS_ISOPEN) &&
289 ISSET(tp->t_state, TS_XCLUDE) &&
290 p->p_ucred->cr_uid != 0)
291 return (EBUSY);
292
293 s = spltty();
294
295 /*
296 * Do the following iff this is a first open.
297 */
298 while (sc->sc_opening)
299 tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
300
301 if (sc->sc_dying) {
302 splx(s);
303 return (EIO);
304 }
305 sc->sc_opening = 1;
306
307 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
308 struct termios t;
309
310 tp->t_dev = dev;
311
312 ucom_status_change(sc);
313
314 /*
315 * Initialize the termios status to the defaults. Add in the
316 * sticky bits from TIOCSFLAGS.
317 */
318 t.c_ispeed = 0;
319 t.c_ospeed = TTYDEF_SPEED;
320 t.c_cflag = TTYDEF_CFLAG;
321 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
322 SET(t.c_cflag, CLOCAL);
323 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
324 SET(t.c_cflag, CRTSCTS);
325 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
326 SET(t.c_cflag, MDMBUF);
327 /* Make sure ucomparam() will do something. */
328 tp->t_ospeed = 0;
329 (void) ucomparam(tp, &t);
330 tp->t_iflag = TTYDEF_IFLAG;
331 tp->t_oflag = TTYDEF_OFLAG;
332 tp->t_lflag = TTYDEF_LFLAG;
333 ttychars(tp);
334 ttsetwater(tp);
335
336 /*
337 * Turn on DTR. We must always do this, even if carrier is not
338 * present, because otherwise we'd have to use TIOCSDTR
339 * immediately after setting CLOCAL, which applications do not
340 * expect. We always assert DTR while the device is open
341 * unless explicitly requested to deassert it.
342 */
343 ucom_dtr(sc, 1);
344
345 // XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
346 ucom_hwiflow(sc);
347
348 DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
349 sc->sc_bulkin_no, sc->sc_bulkout_no));
350
351 /* Open the bulk pipes */
352 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
353 &sc->sc_bulkin_pipe);
354 if (err) {
355 DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
356 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
357 usbd_errstr(err)));
358 return (EIO);
359 }
360 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
361 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
362 if (err) {
363 DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
364 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
365 usbd_errstr(err)));
366 usbd_close_pipe(sc->sc_bulkin_pipe);
367 return (EIO);
368 }
369
370 /* Allocate a request and an input buffer and start reading. */
371 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
372 if (sc->sc_ixfer == NULL) {
373 usbd_close_pipe(sc->sc_bulkin_pipe);
374 usbd_close_pipe(sc->sc_bulkout_pipe);
375 return (ENOMEM);
376 }
377 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer, sc->sc_ibufsize);
378 if (sc->sc_ibuf == NULL) {
379 usbd_free_xfer(sc->sc_ixfer);
380 usbd_close_pipe(sc->sc_bulkin_pipe);
381 usbd_close_pipe(sc->sc_bulkout_pipe);
382 return (ENOMEM);
383 }
384
385 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
386 if (sc->sc_oxfer == NULL) {
387 usbd_free_xfer(sc->sc_ixfer);
388 usbd_close_pipe(sc->sc_bulkin_pipe);
389 usbd_close_pipe(sc->sc_bulkout_pipe);
390 return (ENOMEM);
391 }
392 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer, sc->sc_obufsize);
393 if (sc->sc_obuf == NULL) {
394 usbd_free_xfer(sc->sc_oxfer);
395 usbd_free_xfer(sc->sc_ixfer);
396 usbd_close_pipe(sc->sc_bulkin_pipe);
397 usbd_close_pipe(sc->sc_bulkout_pipe);
398 return (ENOMEM);
399 }
400
401 if (sc->sc_methods->ucom_open != NULL)
402 sc->sc_methods->ucom_open(sc->sc_parent, sc->sc_portno);
403
404 ucomstartread(sc);
405 }
406 sc->sc_opening = 0;
407 wakeup(&sc->sc_opening);
408 splx(s);
409
410 error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
411 if (error)
412 goto bad;
413
414 error = (*linesw[tp->t_line].l_open)(dev, tp);
415 if (error)
416 goto bad;
417
418 return (0);
419
420 bad:
421 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
422 /*
423 * We failed to open the device, and nobody else had it opened.
424 * Clean up the state as appropriate.
425 */
426 ucom_cleanup(sc);
427 }
428
429 return (error);
430 }
431
432 int
433 ucomclose(dev, flag, mode, p)
434 dev_t dev;
435 int flag, mode;
436 struct proc *p;
437 {
438 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
439 struct tty *tp = sc->sc_tty;
440
441 DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
442 if (!ISSET(tp->t_state, TS_ISOPEN))
443 return (0);
444
445 sc->sc_refcnt++;
446
447 (*linesw[tp->t_line].l_close)(tp, flag);
448 ttyclose(tp);
449
450 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
451 /*
452 * Although we got a last close, the device may still be in
453 * use; e.g. if this was the dialout node, and there are still
454 * processes waiting for carrier on the non-dialout node.
455 */
456 ucom_cleanup(sc);
457 }
458
459 if (sc->sc_methods->ucom_close != NULL)
460 sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
461
462 if (--sc->sc_refcnt < 0)
463 usb_detach_wakeup(USBDEV(sc->sc_dev));
464
465 return (0);
466 }
467
468 int
469 ucomread(dev, uio, flag)
470 dev_t dev;
471 struct uio *uio;
472 int flag;
473 {
474 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
475 struct tty *tp = sc->sc_tty;
476 int error;
477
478 if (sc->sc_dying)
479 return (EIO);
480
481 sc->sc_refcnt++;
482 error = ((*linesw[tp->t_line].l_read)(tp, uio, flag));
483 if (--sc->sc_refcnt < 0)
484 usb_detach_wakeup(USBDEV(sc->sc_dev));
485 return (error);
486 }
487
488 int
489 ucomwrite(dev, uio, flag)
490 dev_t dev;
491 struct uio *uio;
492 int flag;
493 {
494 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
495 struct tty *tp = sc->sc_tty;
496 int error;
497
498 if (sc->sc_dying)
499 return (EIO);
500
501 sc->sc_refcnt++;
502 error = ((*linesw[tp->t_line].l_write)(tp, uio, flag));
503 if (--sc->sc_refcnt < 0)
504 usb_detach_wakeup(USBDEV(sc->sc_dev));
505 return (error);
506 }
507
508 struct tty *
509 ucomtty(dev)
510 dev_t dev;
511 {
512 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
513 struct tty *tp = sc->sc_tty;
514
515 return (tp);
516 }
517
518 int
519 ucomioctl(dev, cmd, data, flag, p)
520 dev_t dev;
521 u_long cmd;
522 caddr_t data;
523 int flag;
524 struct proc *p;
525 {
526 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
527 int error;
528
529 sc->sc_refcnt++;
530 error = ucom_do_ioctl(sc, cmd, data, flag, p);
531 if (--sc->sc_refcnt < 0)
532 usb_detach_wakeup(USBDEV(sc->sc_dev));
533 return (error);
534 }
535
536 Static int
537 ucom_do_ioctl(sc, cmd, data, flag, p)
538 struct ucom_softc *sc;
539 u_long cmd;
540 caddr_t data;
541 int flag;
542 struct proc *p;
543 {
544 struct tty *tp = sc->sc_tty;
545 int error;
546 int s;
547
548 if (sc->sc_dying)
549 return (EIO);
550
551 DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
552
553 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
554 if (error >= 0)
555 return (error);
556
557 error = ttioctl(tp, cmd, data, flag, p);
558 if (error >= 0)
559 return (error);
560
561 if (sc->sc_methods->ucom_ioctl != NULL) {
562 error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
563 sc->sc_portno, cmd, data, flag, p);
564 if (error >= 0)
565 return (error);
566 }
567
568 error = 0;
569
570 DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
571 s = spltty();
572
573 switch (cmd) {
574 case TIOCSBRK:
575 ucom_break(sc, 1);
576 break;
577
578 case TIOCCBRK:
579 ucom_break(sc, 0);
580 break;
581
582 case TIOCSDTR:
583 ucom_dtr(sc, 1);
584 break;
585
586 case TIOCCDTR:
587 ucom_dtr(sc, 0);
588 break;
589
590 case TIOCGFLAGS:
591 *(int *)data = sc->sc_swflags;
592 break;
593
594 case TIOCSFLAGS:
595 error = suser(p->p_ucred, &p->p_acflag);
596 if (error)
597 break;
598 sc->sc_swflags = *(int *)data;
599 break;
600
601 case TIOCMSET:
602 case TIOCMBIS:
603 case TIOCMBIC:
604 tiocm_to_ucom(sc, cmd, *(int *)data);
605 break;
606
607 case TIOCMGET:
608 *(int *)data = ucom_to_tiocm(sc);
609 break;
610
611 default:
612 error = ENOTTY;
613 break;
614 }
615
616 splx(s);
617
618 return (error);
619 }
620
621 Static void
622 tiocm_to_ucom(sc, how, ttybits)
623 struct ucom_softc *sc;
624 int how, ttybits;
625 {
626 u_char combits;
627
628 combits = 0;
629 if (ISSET(ttybits, TIOCM_DTR))
630 SET(combits, UMCR_DTR);
631 if (ISSET(ttybits, TIOCM_RTS))
632 SET(combits, UMCR_RTS);
633
634 switch (how) {
635 case TIOCMBIC:
636 CLR(sc->sc_mcr, combits);
637 break;
638
639 case TIOCMBIS:
640 SET(sc->sc_mcr, combits);
641 break;
642
643 case TIOCMSET:
644 CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
645 SET(sc->sc_mcr, combits);
646 break;
647 }
648
649 ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
650 ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
651 }
652
653 Static int
654 ucom_to_tiocm(sc)
655 struct ucom_softc *sc;
656 {
657 u_char combits;
658 int ttybits = 0;
659
660 combits = sc->sc_mcr;
661 if (ISSET(combits, UMCR_DTR))
662 SET(ttybits, TIOCM_DTR);
663 if (ISSET(combits, UMCR_RTS))
664 SET(ttybits, TIOCM_RTS);
665
666 combits = sc->sc_msr;
667 if (ISSET(combits, UMSR_DCD))
668 SET(ttybits, TIOCM_CD);
669 if (ISSET(combits, UMSR_CTS))
670 SET(ttybits, TIOCM_CTS);
671 if (ISSET(combits, UMSR_DSR))
672 SET(ttybits, TIOCM_DSR);
673 if (ISSET(combits, UMSR_RI | UMSR_TERI))
674 SET(ttybits, TIOCM_RI);
675
676 #if 0
677 XXX;
678 if (sc->sc_ier != 0)
679 SET(ttybits, TIOCM_LE);
680 #endif
681
682 return (ttybits);
683 }
684
685 Static void
686 ucom_break(sc, onoff)
687 struct ucom_softc *sc;
688 int onoff;
689 {
690 DPRINTF(("ucom_break: onoff=%d\n", onoff));
691
692 if (sc->sc_methods->ucom_set != NULL)
693 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
694 UCOM_SET_BREAK, onoff);
695 }
696
697 Static void
698 ucom_dtr(sc, onoff)
699 struct ucom_softc *sc;
700 int onoff;
701 {
702 DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
703
704 if (sc->sc_methods->ucom_set != NULL)
705 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
706 UCOM_SET_DTR, onoff);
707 }
708
709 Static void
710 ucom_rts(sc, onoff)
711 struct ucom_softc *sc;
712 int onoff;
713 {
714 DPRINTF(("ucom_rts: onoff=%d\n", onoff));
715
716 if (sc->sc_methods->ucom_set != NULL)
717 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
718 UCOM_SET_RTS, onoff);
719 }
720
721 Static void
722 ucom_status_change(sc)
723 struct ucom_softc *sc;
724 {
725 if (sc->sc_methods->ucom_get_status != NULL) {
726 sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
727 &sc->sc_lsr, &sc->sc_msr);
728 } else {
729 sc->sc_lsr = 0;
730 sc->sc_msr = 0;
731 }
732 }
733
734 Static int
735 ucomparam(tp, t)
736 struct tty *tp;
737 struct termios *t;
738 {
739 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
740 int error;
741
742 if (sc->sc_dying)
743 return (EIO);
744
745 /* Check requested parameters. */
746 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
747 return (EINVAL);
748
749 /*
750 * For the console, always force CLOCAL and !HUPCL, so that the port
751 * is always active.
752 */
753 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
754 SET(t->c_cflag, CLOCAL);
755 CLR(t->c_cflag, HUPCL);
756 }
757
758 /*
759 * If there were no changes, don't do anything. This avoids dropping
760 * input and improves performance when all we did was frob things like
761 * VMIN and VTIME.
762 */
763 if (tp->t_ospeed == t->c_ospeed &&
764 tp->t_cflag == t->c_cflag)
765 return (0);
766
767 //XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
768
769 /* And copy to tty. */
770 tp->t_ispeed = 0;
771 tp->t_ospeed = t->c_ospeed;
772 tp->t_cflag = t->c_cflag;
773
774 if (sc->sc_methods->ucom_param != NULL) {
775 error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
776 t);
777 if (error)
778 return (error);
779 }
780
781 // XXX worry about CHWFLOW
782
783 /*
784 * Update the tty layer's idea of the carrier bit, in case we changed
785 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
786 * explicit request.
787 */
788 DPRINTF(("ucomparam: l_modem\n"));
789 (void) (*linesw[tp->t_line].l_modem)(tp, 1 /* XXX carrier */ );
790
791 #if 0
792 XXX what if the hardware is not open
793 if (!ISSET(t->c_cflag, CHWFLOW)) {
794 if (sc->sc_tx_stopped) {
795 sc->sc_tx_stopped = 0;
796 ucomstart(tp);
797 }
798 }
799 #endif
800
801 return (0);
802 }
803
804 /*
805 * (un)block input via hw flowcontrol
806 */
807 Static void
808 ucom_hwiflow(sc)
809 struct ucom_softc *sc;
810 {
811 DPRINTF(("ucom_hwiflow:\n"));
812 #if 0
813 XXX
814 bus_space_tag_t iot = sc->sc_iot;
815 bus_space_handle_t ioh = sc->sc_ioh;
816
817 if (sc->sc_mcr_rts == 0)
818 return;
819
820 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
821 CLR(sc->sc_mcr, sc->sc_mcr_rts);
822 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
823 } else {
824 SET(sc->sc_mcr, sc->sc_mcr_rts);
825 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
826 }
827 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
828 #endif
829 }
830
831 Static void
832 ucomstart(tp)
833 struct tty *tp;
834 {
835 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
836 usbd_status err;
837 int s;
838 u_char *data;
839 int cnt;
840
841 if (sc->sc_dying)
842 return;
843
844 s = spltty();
845 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
846 DPRINTFN(4,("ucomstart: no go, state=0x%x\n", tp->t_state));
847 goto out;
848 }
849 if (sc->sc_tx_stopped)
850 goto out;
851
852 if (tp->t_outq.c_cc <= tp->t_lowat) {
853 if (ISSET(tp->t_state, TS_ASLEEP)) {
854 CLR(tp->t_state, TS_ASLEEP);
855 wakeup(&tp->t_outq);
856 }
857 selwakeup(&tp->t_wsel);
858 if (tp->t_outq.c_cc == 0)
859 goto out;
860 }
861
862 /* Grab the first contiguous region of buffer space. */
863 data = tp->t_outq.c_cf;
864 cnt = ndqb(&tp->t_outq, 0);
865
866 if (cnt == 0) {
867 DPRINTF(("ucomstart: cnt==0\n"));
868 goto out;
869 }
870
871 SET(tp->t_state, TS_BUSY);
872
873 if (cnt > sc->sc_obufsize) {
874 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
875 cnt = sc->sc_obufsize;
876 }
877 memcpy(sc->sc_obuf, data, cnt);
878
879 DPRINTFN(4,("ucomstart: %d chars\n", cnt));
880 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
881 (usbd_private_handle)sc, sc->sc_obuf, cnt,
882 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
883 /* What can we do on error? */
884 err = usbd_transfer(sc->sc_oxfer);
885 #ifdef DIAGNOSTIC
886 if (err != USBD_IN_PROGRESS)
887 printf("ucomstart: err=%s\n", usbd_errstr(err));
888 #endif
889
890 out:
891 splx(s);
892 }
893
894 Static void
895 ucomstop(tp, flag)
896 struct tty *tp;
897 int flag;
898 {
899 DPRINTF(("ucomstop: flag=%d\n", flag));
900 #if 0
901 /*struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];*/
902 int s;
903
904 s = spltty();
905 if (ISSET(tp->t_state, TS_BUSY)) {
906 DPRINTF(("ucomstop: XXX\n"));
907 /* sc->sc_tx_stopped = 1; */
908 if (!ISSET(tp->t_state, TS_TTSTOP))
909 SET(tp->t_state, TS_FLUSH);
910 }
911 splx(s);
912 #endif
913 }
914
915 Static void
916 ucomwritecb(xfer, p, status)
917 usbd_xfer_handle xfer;
918 usbd_private_handle p;
919 usbd_status status;
920 {
921 struct ucom_softc *sc = (struct ucom_softc *)p;
922 struct tty *tp = sc->sc_tty;
923 u_int32_t cc;
924 int s;
925
926 DPRINTFN(5,("ucomwritecb: status=%d\n", status));
927
928 if (status == USBD_CANCELLED)
929 return;
930
931 if (status) {
932 DPRINTF(("ucomwritecb: status=%d\n", status));
933 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
934 /* XXX we should restart after some delay. */
935 return;
936 }
937
938 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
939 DPRINTFN(5,("ucomwritecb: cc=%d\n", cc));
940
941 s = spltty();
942 CLR(tp->t_state, TS_BUSY);
943 if (ISSET(tp->t_state, TS_FLUSH))
944 CLR(tp->t_state, TS_FLUSH);
945 else
946 ndflush(&tp->t_outq, cc);
947 (*linesw[tp->t_line].l_start)(tp);
948 splx(s);
949 }
950
951 Static usbd_status
952 ucomstartread(sc)
953 struct ucom_softc *sc;
954 {
955 usbd_status err;
956
957 DPRINTFN(5,("ucomstartread: start\n"));
958 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
959 (usbd_private_handle)sc,
960 sc->sc_ibuf, sc->sc_ibufsize,
961 USBD_SHORT_XFER_OK | USBD_NO_COPY,
962 USBD_NO_TIMEOUT, ucomreadcb);
963 err = usbd_transfer(sc->sc_ixfer);
964 if (err != USBD_IN_PROGRESS) {
965 DPRINTF(("ucomstartread: err=%s\n", usbd_errstr(err)));
966 return (err);
967 }
968 return (USBD_NORMAL_COMPLETION);
969 }
970
971 Static void
972 ucomreadcb(xfer, p, status)
973 usbd_xfer_handle xfer;
974 usbd_private_handle p;
975 usbd_status status;
976 {
977 struct ucom_softc *sc = (struct ucom_softc *)p;
978 struct tty *tp = sc->sc_tty;
979 int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
980 usbd_status err;
981 u_int32_t cc;
982 u_char *cp;
983 int s;
984
985 if (status == USBD_CANCELLED)
986 return;
987
988 if (status) {
989 DPRINTF(("ucomreadcb: status=%d\n", status));
990 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
991 /* XXX we should restart after some delay. */
992 return;
993 }
994
995 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
996 DPRINTFN(5,("ucomreadcb: got %d chars, tp=%p\n", cc, tp));
997 s = spltty();
998 /* Give characters to tty layer. */
999 while (cc-- > 0) {
1000 DPRINTFN(7,("ucomreadcb: char=0x%02x\n", *cp));
1001 if ((*rint)(*cp++, tp) == -1) {
1002 /* XXX what should we do? */
1003 printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
1004 cc);
1005 break;
1006 }
1007 }
1008 splx(s);
1009
1010 err = ucomstartread(sc);
1011 if (err) {
1012 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1013 /* XXX what should we dow now? */
1014 }
1015 }
1016
1017 Static void
1018 ucom_cleanup(sc)
1019 struct ucom_softc *sc;
1020 {
1021 DPRINTF(("ucom_cleanup: closing pipes\n"));
1022
1023 ucom_shutdown(sc);
1024 usbd_abort_pipe(sc->sc_bulkin_pipe);
1025 usbd_close_pipe(sc->sc_bulkin_pipe);
1026 usbd_abort_pipe(sc->sc_bulkout_pipe);
1027 usbd_close_pipe(sc->sc_bulkout_pipe);
1028 usbd_free_xfer(sc->sc_ixfer);
1029 usbd_free_xfer(sc->sc_oxfer);
1030 }
1031
1032 #endif /* NUCOM > 0 */
1033
1034 Static int
1035 ucomprint(aux, pnp)
1036 void *aux;
1037 const char *pnp;
1038 {
1039
1040 if (pnp)
1041 printf("ucom at %s\n", pnp);
1042 return (UNCONF);
1043 }
1044
1045 Static int
1046 ucomsubmatch(parent, cf, aux)
1047 struct device *parent;
1048 struct cfdata *cf;
1049 void *aux;
1050 {
1051 struct ucom_attach_args *uca = aux;
1052
1053 if (uca->portno != UCOM_UNK_PORTNO &&
1054 cf->ucomcf_portno != UCOM_UNK_PORTNO &&
1055 cf->ucomcf_portno != uca->portno)
1056 return (0);
1057 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1058 }
1059