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