From 5c67885a2660e6403402851f912dd01331cb45d4 Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Sun, 23 Aug 2009 08:04:40 +0000 Subject: [PATCH] Add ttydisc_rint_simple(). I noticed several drivers in our tree don't actually care about parity and framing, such as pts(4), snp(4) (and my partially finished console driver). Instead of duplicating a lot of code, I think we'd better add a utility function for those drivers to quickly process a buffer of input. Also change pts(4) and snp(4) to use this function. --- sys/dev/snp/snp.c | 11 +++-------- sys/kern/tty_pts.c | 25 ++++++------------------- sys/kern/tty_ttydisc.c | 16 ++++++++++++++++ sys/sys/ttydisc.h | 1 + 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/sys/dev/snp/snp.c b/sys/dev/snp/snp.c index fcc70a5d84f3..37d996563f94 100644 --- a/sys/dev/snp/snp.c +++ b/sys/dev/snp/snp.c @@ -192,7 +192,7 @@ snp_write(struct cdev *dev, struct uio *uio, int flag) { struct snp_softc *ss; struct tty *tp; - int error, len, i; + int error, len; char in[SNP_INPUT_BUFSIZE]; error = devfs_get_cdevpriv((void **)&ss); @@ -223,14 +223,9 @@ snp_write(struct cdev *dev, struct uio *uio, int flag) * because we shouldn't bail out when we're running * close to the watermarks. */ - if (ttydisc_can_bypass(tp)) { - ttydisc_rint_bypass(tp, in, len); - } else { - for (i = 0; i < len; i++) - ttydisc_rint(tp, in[i], 0); - } - + ttydisc_rint_simple(tp, in, len); ttydisc_rint_done(tp); + tty_unlock(tp); } diff --git a/sys/kern/tty_pts.c b/sys/kern/tty_pts.c index d5045c17b4d9..c77cf2a1d828 100644 --- a/sys/kern/tty_pts.c +++ b/sys/kern/tty_pts.c @@ -215,25 +215,12 @@ ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred, */ MPASS(iblen > 0); do { - if (ttydisc_can_bypass(tp)) { - /* Store data at once. */ - rintlen = ttydisc_rint_bypass(tp, - ibstart, iblen); - ibstart += rintlen; - iblen -= rintlen; - - if (iblen == 0) { - /* All data written. */ - break; - } - } else { - error = ttydisc_rint(tp, *ibstart, 0); - if (error == 0) { - /* Character stored successfully. */ - ibstart++; - iblen--; - continue; - } + rintlen = ttydisc_rint_simple(tp, ibstart, iblen); + ibstart += rintlen; + iblen -= rintlen; + if (iblen == 0) { + /* All data written. */ + break; } /* Maybe the device isn't used anyway. */ diff --git a/sys/kern/tty_ttydisc.c b/sys/kern/tty_ttydisc.c index debaa1cc1e90..d79a2b761d56 100644 --- a/sys/kern/tty_ttydisc.c +++ b/sys/kern/tty_ttydisc.c @@ -1044,6 +1044,22 @@ ttydisc_rint(struct tty *tp, char c, int flags) return (0); } +size_t +ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len) +{ + const char *cbuf; + + if (ttydisc_can_bypass(tp)) + return (ttydisc_rint_bypass(tp, buf, len)); + + for (cbuf = buf; len-- > 0; cbuf++) { + if (ttydisc_rint(tp, *cbuf, 0) != 0) + break; + } + + return (cbuf - (const char *)buf); +} + size_t ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len) { diff --git a/sys/sys/ttydisc.h b/sys/sys/ttydisc.h index 2ea54666c56a..74a1a0ed927e 100644 --- a/sys/sys/ttydisc.h +++ b/sys/sys/ttydisc.h @@ -52,6 +52,7 @@ void ttydisc_optimize(struct tty *tp); void ttydisc_modem(struct tty *tp, int open); #define ttydisc_can_bypass(tp) ((tp)->t_flags & TF_BYPASS) int ttydisc_rint(struct tty *tp, char c, int flags); +size_t ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len); size_t ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len); void ttydisc_rint_done(struct tty *tp); size_t ttydisc_rint_poll(struct tty *tp);