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