| Commit | Line | Data |
|---|---|---|
| 7e74f00e MD |
1 | /* |
| 2 | * $NetBSD: ucom.c,v 1.39 2001/08/16 22:31:24 augustss Exp $ | |
| 1550dfd9 MD |
3 | * $NetBSD: ucom.c,v 1.40 2001/11/13 06:24:54 lukem Exp $ |
| 4 | * $FreeBSD: src/sys/dev/usb/ucom.c,v 1.35 2003/11/16 11:58:21 akiyama Exp $ | |
| 3e997bb7 | 5 | * $DragonFly: src/sys/dev/usbmisc/ucom/ucom.c,v 1.32 2007/11/05 19:09:43 hasso Exp $ |
| 7e74f00e | 6 | */ |
| 984263bc MD |
7 | /*- |
| 8 | * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>. | |
| 9 | * All rights reserved. | |
| 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 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 30 | * SUCH DAMAGE. | |
| 31 | */ | |
| 32 | ||
| 33 | /* | |
| 34 | * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. | |
| 35 | * All rights reserved. | |
| 36 | * | |
| 37 | * This code is derived from software contributed to The NetBSD Foundation | |
| 38 | * by Lennart Augustsson (lennart@augustsson.net) at | |
| 39 | * Carlstedt Research & Technology. | |
| 40 | * | |
| 41 | * Redistribution and use in source and binary forms, with or without | |
| 42 | * modification, are permitted provided that the following conditions | |
| 43 | * are met: | |
| 44 | * 1. Redistributions of source code must retain the above copyright | |
| 45 | * notice, this list of conditions and the following disclaimer. | |
| 46 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 47 | * notice, this list of conditions and the following disclaimer in the | |
| 48 | * documentation and/or other materials provided with the distribution. | |
| 49 | * 3. All advertising materials mentioning features or use of this software | |
| 50 | * must display the following acknowledgement: | |
| 51 | * This product includes software developed by the NetBSD | |
| 52 | * Foundation, Inc. and its contributors. | |
| 53 | * 4. Neither the name of The NetBSD Foundation nor the names of its | |
| 54 | * contributors may be used to endorse or promote products derived | |
| 55 | * from this software without specific prior written permission. | |
| 56 | * | |
| 57 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
| 58 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
| 59 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 60 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
| 61 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 62 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 63 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 64 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 65 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 66 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 67 | * POSSIBILITY OF SUCH DAMAGE. | |
| 68 | */ | |
| 69 | ||
| 984263bc MD |
70 | #include <sys/param.h> |
| 71 | #include <sys/systm.h> | |
| 72 | #include <sys/kernel.h> | |
| 73 | #include <sys/malloc.h> | |
| 74 | #include <sys/bus.h> | |
| 984263bc MD |
75 | #include <sys/fcntl.h> |
| 76 | #include <sys/conf.h> | |
| 77 | #include <sys/tty.h> | |
| 78 | #include <sys/clist.h> | |
| 79 | #include <sys/file.h> | |
| 984263bc | 80 | #include <sys/select.h> |
| 984263bc | 81 | #include <sys/proc.h> |
| 895c1f85 | 82 | #include <sys/priv.h> |
| 984263bc MD |
83 | #include <sys/poll.h> |
| 84 | #include <sys/sysctl.h> | |
| 4e01b467 | 85 | #include <sys/thread2.h> |
| 984263bc | 86 | |
| 1f2de5d4 MD |
87 | #include <bus/usb/usb.h> |
| 88 | #include <bus/usb/usbcdc.h> | |
| 984263bc | 89 | |
| 1f2de5d4 MD |
90 | #include <bus/usb/usbdi.h> |
| 91 | #include <bus/usb/usbdi_util.h> | |
| 1f2de5d4 | 92 | #include <bus/usb/usb_quirks.h> |
| 984263bc | 93 | |
| 1f2de5d4 | 94 | #include "ucomvar.h" |
| 984263bc MD |
95 | |
| 96 | #ifdef USB_DEBUG | |
| 97 | static int ucomdebug = 0; | |
| 98 | SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom"); | |
| 99 | SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW, | |
| 100 | &ucomdebug, 0, "ucom debug level"); | |
| 101 | #define DPRINTF(x) do { \ | |
| 102 | if (ucomdebug) \ | |
| fc1ef497 | 103 | kprintf x; \ |
| 984263bc MD |
104 | } while (0) |
| 105 | ||
| 106 | #define DPRINTFN(n, x) do { \ | |
| 107 | if (ucomdebug > (n)) \ | |
| fc1ef497 | 108 | kprintf x; \ |
| 984263bc MD |
109 | } while (0) |
| 110 | #else | |
| 111 | #define DPRINTF(x) | |
| 112 | #define DPRINTFN(n, x) | |
| 113 | #endif | |
| 114 | ||
| 6ed427ca HT |
115 | static d_open_t ucomopen; |
| 116 | static d_close_t ucomclose; | |
| 117 | static d_read_t ucomread; | |
| 118 | static d_write_t ucomwrite; | |
| 119 | static d_ioctl_t ucomioctl; | |
| 984263bc MD |
120 | |
| 121 | #define UCOM_CDEV_MAJOR 138 | |
| 122 | ||
| fef8985e MD |
123 | static struct dev_ops ucom_ops = { |
| 124 | { "ucom", UCOM_CDEV_MAJOR, D_TTY | D_KQFILTER }, | |
| 125 | .d_open = ucomopen, | |
| 126 | .d_close = ucomclose, | |
| 127 | .d_read = ucomread, | |
| 128 | .d_write = ucomwrite, | |
| 129 | .d_ioctl = ucomioctl, | |
| 130 | .d_poll = ttypoll, | |
| a32446b7 MD |
131 | .d_kqfilter = ttykqfilter, |
| 132 | .d_revoke = ttyrevoke | |
| 984263bc MD |
133 | }; |
| 134 | ||
| 6ed427ca HT |
135 | static void ucom_cleanup(struct ucom_softc *); |
| 136 | static int ucomctl(struct ucom_softc *, int, int); | |
| 137 | static int ucomparam(struct tty *, struct termios *); | |
| 138 | static void ucomstart(struct tty *); | |
| 139 | static void ucomstop(struct tty *, int); | |
| 140 | static void ucom_shutdown(struct ucom_softc *); | |
| 141 | static void ucom_dtr(struct ucom_softc *, int); | |
| 142 | static void ucom_rts(struct ucom_softc *, int); | |
| 143 | static void ucom_break(struct ucom_softc *, int); | |
| 144 | static usbd_status ucomstartread(struct ucom_softc *); | |
| 145 | static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status); | |
| 146 | static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status); | |
| 147 | static void ucomstopread(struct ucom_softc *); | |
| 984263bc MD |
148 | static void disc_optim(struct tty *, struct termios *, struct ucom_softc *); |
| 149 | ||
| 150 | devclass_t ucom_devclass; | |
| 151 | ||
| 152 | static moduledata_t ucom_mod = { | |
| 153 | "ucom", | |
| 154 | NULL, | |
| 155 | NULL | |
| 156 | }; | |
| 157 | ||
| 158 | DECLARE_MODULE(ucom, ucom_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); | |
| 159 | MODULE_DEPEND(ucom, usb, 1, 1, 1); | |
| 160 | MODULE_VERSION(ucom, UCOM_MODVER); | |
| 161 | ||
| 162 | int | |
| 163 | ucom_attach(struct ucom_softc *sc) | |
| 164 | { | |
| 165 | struct tty *tp; | |
| 166 | int unit; | |
| b13267a5 | 167 | cdev_t dev; |
| 984263bc MD |
168 | |
| 169 | unit = device_get_unit(sc->sc_dev); | |
| 170 | ||
| 171 | sc->sc_tty = tp = ttymalloc(sc->sc_tty); | |
| 172 | tp->t_oproc = ucomstart; | |
| 173 | tp->t_param = ucomparam; | |
| 174 | tp->t_stop = ucomstop; | |
| 175 | ||
| 176 | DPRINTF(("ucom_attach: tty_attach tp = %p\n", tp)); | |
| 177 | ||
| 178 | DPRINTF(("ucom_attach: make_dev: ucom%d\n", unit)); | |
| 179 | ||
| fef8985e | 180 | dev = make_dev(&ucom_ops, unit | UCOM_CALLOUT_MASK, |
| 984263bc MD |
181 | UID_UUCP, GID_DIALER, 0660, |
| 182 | "ucom%d", unit); | |
| e4c9c0c8 | 183 | dev->si_tty = tp; |
| 984263bc MD |
184 | |
| 185 | return (0); | |
| 186 | } | |
| 187 | ||
| 188 | int | |
| 189 | ucom_detach(struct ucom_softc *sc) | |
| 190 | { | |
| 191 | struct tty *tp = sc->sc_tty; | |
| e4c9c0c8 | 192 | int unit; |
| 984263bc MD |
193 | |
| 194 | DPRINTF(("ucom_detach: sc = %p, tp = %p\n", sc, sc->sc_tty)); | |
| 195 | ||
| 196 | sc->sc_dying = 1; | |
| 197 | ||
| 198 | if (sc->sc_bulkin_pipe != NULL) | |
| 199 | usbd_abort_pipe(sc->sc_bulkin_pipe); | |
| 200 | if (sc->sc_bulkout_pipe != NULL) | |
| 201 | usbd_abort_pipe(sc->sc_bulkout_pipe); | |
| 202 | ||
| 203 | if (tp != NULL) { | |
| 204 | if (tp->t_state & TS_ISOPEN) { | |
| 205 | device_printf(sc->sc_dev, | |
| 206 | "still open, forcing close\n"); | |
| 207 | (*linesw[tp->t_line].l_close)(tp, 0); | |
| 984263bc | 208 | ttyclose(tp); |
| 984263bc MD |
209 | } |
| 210 | } else { | |
| 211 | DPRINTF(("ucom_detach: no tty\n")); | |
| 212 | return (0); | |
| 213 | } | |
| 214 | ||
| 4e01b467 | 215 | crit_enter(); |
| 984263bc MD |
216 | if (--sc->sc_refcnt >= 0) { |
| 217 | /* Wait for processes to go away. */ | |
| 2939c544 | 218 | usb_detach_wait(sc->sc_dev); |
| 984263bc | 219 | } |
| 4e01b467 | 220 | crit_exit(); |
| 984263bc | 221 | |
| e4c9c0c8 | 222 | unit = device_get_unit(sc->sc_dev); |
| cd29885a MD |
223 | kprintf("devfs: Please check that only the right ucom devices were removed!!!\n"); |
| 224 | dev_ops_remove_minor(&ucom_ops, /*UCOMUNIT_MASK, */unit); | |
| 984263bc MD |
225 | |
| 226 | return (0); | |
| 227 | } | |
| 228 | ||
| 6ed427ca | 229 | static void |
| 984263bc MD |
230 | ucom_shutdown(struct ucom_softc *sc) |
| 231 | { | |
| 232 | struct tty *tp = sc->sc_tty; | |
| 233 | ||
| 234 | DPRINTF(("ucom_shutdown\n")); | |
| 235 | /* | |
| 236 | * Hang up if necessary. Wait a bit, so the other side has time to | |
| 237 | * notice even if we immediately open the port again. | |
| 238 | */ | |
| 239 | if (ISSET(tp->t_cflag, HUPCL)) { | |
| 240 | (void)ucomctl(sc, TIOCM_DTR, DMBIC); | |
| 377d4740 | 241 | (void)tsleep(sc, 0, "ucomsd", hz); |
| 984263bc MD |
242 | } |
| 243 | } | |
| 244 | ||
| 6ed427ca | 245 | static int |
| fef8985e | 246 | ucomopen(struct dev_open_args *ap) |
| 984263bc | 247 | { |
| b13267a5 | 248 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
249 | int unit = UCOMUNIT(dev); |
| 250 | struct ucom_softc *sc; | |
| 251 | usbd_status err; | |
| 252 | struct tty *tp; | |
| 984263bc | 253 | int error; |
| 41c20dac | 254 | |
| 1e089e7e HT |
255 | sc = devclass_get_softc(ucom_devclass, unit); |
| 256 | if (sc == NULL) | |
| 257 | return (ENXIO); | |
| 984263bc MD |
258 | |
| 259 | if (sc->sc_dying) | |
| 260 | return (ENXIO); | |
| 261 | ||
| 262 | tp = sc->sc_tty; | |
| 263 | ||
| 6ed427ca | 264 | DPRINTF(("%s: ucomopen: tp = %p\n", device_get_nameunit(sc->sc_dev), tp)); |
| 984263bc MD |
265 | |
| 266 | if (ISSET(tp->t_state, TS_ISOPEN) && | |
| 267 | ISSET(tp->t_state, TS_XCLUDE) && | |
| 895c1f85 | 268 | priv_check_cred(ap->a_cred, PRIV_ROOT, 0) |
| dadab5e9 | 269 | ) { |
| 984263bc | 270 | return (EBUSY); |
| dadab5e9 | 271 | } |
| 984263bc MD |
272 | |
| 273 | /* | |
| 274 | * Do the following iff this is a first open. | |
| 275 | */ | |
| a5950399 | 276 | crit_enter(); |
| 984263bc | 277 | while (sc->sc_opening) |
| 377d4740 | 278 | tsleep(&sc->sc_opening, 0, "ucomop", 0); |
| 984263bc MD |
279 | sc->sc_opening = 1; |
| 280 | ||
| 281 | if (!ISSET(tp->t_state, TS_ISOPEN)) { | |
| 282 | struct termios t; | |
| 283 | ||
| 284 | sc->sc_poll = 0; | |
| 285 | sc->sc_lsr = sc->sc_msr = sc->sc_mcr = 0; | |
| 286 | ||
| e4c9c0c8 | 287 | tp->t_dev = reference_dev(dev); |
| 984263bc MD |
288 | |
| 289 | /* | |
| 290 | * Initialize the termios status to the defaults. Add in the | |
| 291 | * sticky bits from TIOCSFLAGS. | |
| 292 | */ | |
| 293 | t.c_ispeed = 0; | |
| 294 | t.c_ospeed = TTYDEF_SPEED; | |
| 295 | t.c_cflag = TTYDEF_CFLAG; | |
| 296 | /* Make sure ucomparam() will do something. */ | |
| 297 | tp->t_ospeed = 0; | |
| 298 | (void)ucomparam(tp, &t); | |
| 299 | tp->t_iflag = TTYDEF_IFLAG; | |
| 300 | tp->t_oflag = TTYDEF_OFLAG; | |
| 301 | tp->t_lflag = TTYDEF_LFLAG; | |
| 302 | ttychars(tp); | |
| 303 | ttsetwater(tp); | |
| 304 | ||
| 305 | /* | |
| 306 | * Turn on DTR. We must always do this, even if carrier is not | |
| 307 | * present, because otherwise we'd have to use TIOCSDTR | |
| 308 | * immediately after setting CLOCAL, which applications do not | |
| 309 | * expect. We always assert DTR while the device is open | |
| 310 | * unless explicitly requested to deassert it. | |
| 311 | */ | |
| 312 | (void)ucomctl(sc, TIOCM_DTR | TIOCM_RTS, DMBIS); | |
| 313 | ||
| 314 | /* Device specific open */ | |
| 315 | if (sc->sc_callback->ucom_open != NULL) { | |
| 316 | error = sc->sc_callback->ucom_open(sc->sc_parent, | |
| 317 | sc->sc_portno); | |
| 318 | if (error) { | |
| 319 | ucom_cleanup(sc); | |
| 320 | sc->sc_opening = 0; | |
| 321 | wakeup(&sc->sc_opening); | |
| a5950399 | 322 | crit_exit(); |
| 984263bc MD |
323 | return (error); |
| 324 | } | |
| 325 | } | |
| 326 | ||
| 327 | DPRINTF(("ucomopen: open pipes in = %d out = %d\n", | |
| 328 | sc->sc_bulkin_no, sc->sc_bulkout_no)); | |
| 329 | ||
| 330 | /* Open the bulk pipes */ | |
| 331 | /* Bulk-in pipe */ | |
| 332 | err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0, | |
| 333 | &sc->sc_bulkin_pipe); | |
| 334 | if (err) { | |
| e3869ec7 | 335 | kprintf("%s: open bulk in error (addr %d): %s\n", |
| 6ed427ca | 336 | device_get_nameunit(sc->sc_dev), sc->sc_bulkin_no, |
| 984263bc MD |
337 | usbd_errstr(err)); |
| 338 | error = EIO; | |
| 339 | goto fail_0; | |
| 340 | } | |
| 341 | /* Bulk-out pipe */ | |
| 342 | err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no, | |
| 343 | USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe); | |
| 344 | if (err) { | |
| e3869ec7 | 345 | kprintf("%s: open bulk out error (addr %d): %s\n", |
| 6ed427ca | 346 | device_get_nameunit(sc->sc_dev), sc->sc_bulkout_no, |
| 984263bc MD |
347 | usbd_errstr(err)); |
| 348 | error = EIO; | |
| 349 | goto fail_1; | |
| 350 | } | |
| 351 | ||
| 352 | /* Allocate a request and an input buffer and start reading. */ | |
| 353 | sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev); | |
| 354 | if (sc->sc_ixfer == NULL) { | |
| 355 | error = ENOMEM; | |
| 356 | goto fail_2; | |
| 357 | } | |
| 358 | ||
| 359 | sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer, | |
| 360 | sc->sc_ibufsizepad); | |
| 361 | if (sc->sc_ibuf == NULL) { | |
| 362 | error = ENOMEM; | |
| 363 | goto fail_3; | |
| 364 | } | |
| 365 | ||
| 366 | sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev); | |
| 367 | if (sc->sc_oxfer == NULL) { | |
| 368 | error = ENOMEM; | |
| 369 | goto fail_3; | |
| 370 | } | |
| 371 | ||
| 372 | sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer, | |
| 373 | sc->sc_obufsize + | |
| 374 | sc->sc_opkthdrlen); | |
| 375 | if (sc->sc_obuf == NULL) { | |
| 376 | error = ENOMEM; | |
| 377 | goto fail_4; | |
| 378 | } | |
| 379 | ||
| 380 | /* | |
| 381 | * Handle initial DCD. | |
| 382 | */ | |
| 383 | if (ISSET(sc->sc_msr, UMSR_DCD) || | |
| 384 | (minor(dev) & UCOM_CALLOUT_MASK)) | |
| 385 | (*linesw[tp->t_line].l_modem)(tp, 1); | |
| 386 | ||
| 387 | ucomstartread(sc); | |
| 388 | } | |
| 389 | ||
| 390 | sc->sc_opening = 0; | |
| 391 | wakeup(&sc->sc_opening); | |
| a5950399 | 392 | crit_exit(); |
| 984263bc MD |
393 | |
| 394 | error = ttyopen(dev, tp); | |
| 395 | if (error) | |
| 396 | goto bad; | |
| 397 | ||
| 398 | error = (*linesw[tp->t_line].l_open)(dev, tp); | |
| 399 | if (error) | |
| 400 | goto bad; | |
| 401 | ||
| 402 | disc_optim(tp, &tp->t_termios, sc); | |
| 403 | ||
| 6ed427ca | 404 | DPRINTF(("%s: ucomopen: success\n", device_get_nameunit(sc->sc_dev))); |
| 984263bc MD |
405 | |
| 406 | sc->sc_poll = 1; | |
| 407 | sc->sc_refcnt++; | |
| 408 | ||
| 409 | return (0); | |
| 410 | ||
| 411 | fail_4: | |
| 412 | usbd_free_xfer(sc->sc_oxfer); | |
| 413 | sc->sc_oxfer = NULL; | |
| 414 | fail_3: | |
| 415 | usbd_free_xfer(sc->sc_ixfer); | |
| 416 | sc->sc_ixfer = NULL; | |
| 417 | fail_2: | |
| 418 | usbd_close_pipe(sc->sc_bulkout_pipe); | |
| 419 | sc->sc_bulkout_pipe = NULL; | |
| 420 | fail_1: | |
| 421 | usbd_close_pipe(sc->sc_bulkin_pipe); | |
| 422 | sc->sc_bulkin_pipe = NULL; | |
| 423 | fail_0: | |
| 424 | sc->sc_opening = 0; | |
| 425 | wakeup(&sc->sc_opening); | |
| a5950399 | 426 | crit_exit(); |
| 984263bc MD |
427 | return (error); |
| 428 | ||
| 429 | bad: | |
| 430 | if (!ISSET(tp->t_state, TS_ISOPEN)) { | |
| 431 | /* | |
| 432 | * We failed to open the device, and nobody else had it opened. | |
| 433 | * Clean up the state as appropriate. | |
| 434 | */ | |
| 435 | ucom_cleanup(sc); | |
| 436 | } | |
| 437 | ||
| 6ed427ca | 438 | DPRINTF(("%s: ucomopen: failed\n", device_get_nameunit(sc->sc_dev))); |
| 984263bc MD |
439 | |
| 440 | return (error); | |
| 441 | } | |
| 442 | ||
| 443 | static int | |
| fef8985e | 444 | ucomclose(struct dev_close_args *ap) |
| 984263bc | 445 | { |
| b13267a5 | 446 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
447 | struct ucom_softc *sc; |
| 448 | struct tty *tp; | |
| 984263bc | 449 | |
| 1e089e7e | 450 | sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev)); |
| 984263bc MD |
451 | |
| 452 | tp = sc->sc_tty; | |
| 453 | ||
| 454 | DPRINTF(("%s: ucomclose: unit = %d\n", | |
| 6ed427ca | 455 | device_get_nameunit(sc->sc_dev), UCOMUNIT(dev))); |
| 984263bc MD |
456 | |
| 457 | if (!ISSET(tp->t_state, TS_ISOPEN)) | |
| 458 | goto quit; | |
| 459 | ||
| a5950399 | 460 | crit_enter(); |
| fef8985e | 461 | (*linesw[tp->t_line].l_close)(tp, ap->a_fflag); |
| 984263bc MD |
462 | disc_optim(tp, &tp->t_termios, sc); |
| 463 | ttyclose(tp); | |
| a5950399 | 464 | crit_exit(); |
| 984263bc MD |
465 | |
| 466 | if (sc->sc_dying) | |
| 467 | goto quit; | |
| 468 | ||
| 469 | if (!ISSET(tp->t_state, TS_ISOPEN)) { | |
| 470 | /* | |
| 471 | * Although we got a last close, the device may still be in | |
| 472 | * use; e.g. if this was the dialout node, and there are still | |
| 473 | * processes waiting for carrier on the non-dialout node. | |
| 474 | */ | |
| 475 | ucom_cleanup(sc); | |
| 476 | } | |
| 477 | ||
| 478 | if (sc->sc_callback->ucom_close != NULL) | |
| 479 | sc->sc_callback->ucom_close(sc->sc_parent, sc->sc_portno); | |
| 480 | ||
| 77ab1ae2 MD |
481 | quit: |
| 482 | if (tp->t_dev) { | |
| 483 | release_dev(tp->t_dev); | |
| 484 | tp->t_dev = NULL; | |
| 485 | } | |
| 486 | ||
| 984263bc | 487 | if (--sc->sc_refcnt < 0) |
| 2939c544 | 488 | usb_detach_wakeup(sc->sc_dev); |
| 984263bc MD |
489 | |
| 490 | return (0); | |
| 491 | } | |
| 492 | ||
| 493 | static int | |
| fef8985e | 494 | ucomread(struct dev_read_args *ap) |
| 984263bc | 495 | { |
| b13267a5 | 496 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
497 | struct ucom_softc *sc; |
| 498 | struct tty *tp; | |
| 499 | int error; | |
| 500 | ||
| 1e089e7e | 501 | sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev)); |
| 984263bc MD |
502 | tp = sc->sc_tty; |
| 503 | ||
| fef8985e | 504 | DPRINTF(("ucomread: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag)); |
| 984263bc MD |
505 | |
| 506 | if (sc->sc_dying) | |
| 507 | return (EIO); | |
| 508 | ||
| fef8985e | 509 | error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag); |
| 984263bc MD |
510 | |
| 511 | DPRINTF(("ucomread: error = %d\n", error)); | |
| 512 | ||
| 513 | return (error); | |
| 514 | } | |
| 515 | ||
| 516 | static int | |
| fef8985e | 517 | ucomwrite(struct dev_write_args *ap) |
| 984263bc | 518 | { |
| b13267a5 | 519 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
520 | struct ucom_softc *sc; |
| 521 | struct tty *tp; | |
| 522 | int error; | |
| 523 | ||
| 1e089e7e | 524 | sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev)); |
| 984263bc MD |
525 | tp = sc->sc_tty; |
| 526 | ||
| fef8985e | 527 | DPRINTF(("ucomwrite: tp = %p, flag = 0x%x\n", tp, ap->a_ioflag)); |
| 984263bc MD |
528 | |
| 529 | if (sc->sc_dying) | |
| 530 | return (EIO); | |
| 531 | ||
| fef8985e | 532 | error = (*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag); |
| 984263bc MD |
533 | |
| 534 | DPRINTF(("ucomwrite: error = %d\n", error)); | |
| 535 | ||
| 536 | return (error); | |
| 537 | } | |
| 538 | ||
| 539 | static int | |
| fef8985e | 540 | ucomioctl(struct dev_ioctl_args *ap) |
| 984263bc | 541 | { |
| b13267a5 | 542 | cdev_t dev = ap->a_head.a_dev; |
| 984263bc MD |
543 | struct ucom_softc *sc; |
| 544 | struct tty *tp; | |
| 545 | int error; | |
| 984263bc MD |
546 | int d; |
| 547 | ||
| 1e089e7e | 548 | sc = devclass_get_softc(ucom_devclass, UCOMUNIT(dev)); |
| 984263bc MD |
549 | tp = sc->sc_tty; |
| 550 | ||
| 551 | if (sc->sc_dying) | |
| 552 | return (EIO); | |
| 553 | ||
| fef8985e | 554 | DPRINTF(("ucomioctl: cmd = 0x%08lx\n", ap->a_cmd)); |
| 984263bc | 555 | |
| fef8985e MD |
556 | error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, |
| 557 | ap->a_fflag, ap->a_cred); | |
| 62ade751 | 558 | if (error != ENOIOCTL) { |
| 984263bc MD |
559 | DPRINTF(("ucomioctl: l_ioctl: error = %d\n", error)); |
| 560 | return (error); | |
| 561 | } | |
| 562 | ||
| a5950399 | 563 | crit_enter(); |
| 62ade751 | 564 | |
| fef8985e | 565 | error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag); |
| 984263bc | 566 | disc_optim(tp, &tp->t_termios, sc); |
| 62ade751 | 567 | if (error != ENOIOCTL) { |
| a5950399 | 568 | crit_exit(); |
| 984263bc MD |
569 | DPRINTF(("ucomioctl: ttioctl: error = %d\n", error)); |
| 570 | return (error); | |
| 571 | } | |
| 572 | ||
| 573 | if (sc->sc_callback->ucom_ioctl != NULL) { | |
| 574 | error = sc->sc_callback->ucom_ioctl(sc->sc_parent, | |
| 575 | sc->sc_portno, | |
| fef8985e MD |
576 | ap->a_cmd, ap->a_data, |
| 577 | ap->a_fflag, curthread); | |
| a5950399 MD |
578 | if (error >= 0) { |
| 579 | crit_exit(); | |
| 984263bc | 580 | return (error); |
| a5950399 | 581 | } |
| 984263bc MD |
582 | } |
| 583 | ||
| 584 | error = 0; | |
| 585 | ||
| fef8985e | 586 | DPRINTF(("ucomioctl: our cmd = 0x%08lx\n", ap->a_cmd)); |
| 984263bc | 587 | |
| fef8985e | 588 | switch (ap->a_cmd) { |
| 984263bc MD |
589 | case TIOCSBRK: |
| 590 | DPRINTF(("ucomioctl: TIOCSBRK\n")); | |
| 591 | ucom_break(sc, 1); | |
| 592 | break; | |
| 593 | case TIOCCBRK: | |
| 594 | DPRINTF(("ucomioctl: TIOCCBRK\n")); | |
| 595 | ucom_break(sc, 0); | |
| 596 | break; | |
| 597 | ||
| 598 | case TIOCSDTR: | |
| 599 | DPRINTF(("ucomioctl: TIOCSDTR\n")); | |
| 600 | (void)ucomctl(sc, TIOCM_DTR, DMBIS); | |
| 601 | break; | |
| 602 | case TIOCCDTR: | |
| 603 | DPRINTF(("ucomioctl: TIOCCDTR\n")); | |
| 604 | (void)ucomctl(sc, TIOCM_DTR, DMBIC); | |
| 605 | break; | |
| 606 | ||
| 607 | case TIOCMSET: | |
| fef8985e | 608 | d = *(int *)ap->a_data; |
| 984263bc MD |
609 | DPRINTF(("ucomioctl: TIOCMSET, 0x%x\n", d)); |
| 610 | (void)ucomctl(sc, d, DMSET); | |
| 611 | break; | |
| 612 | case TIOCMBIS: | |
| fef8985e | 613 | d = *(int *)ap->a_data; |
| 984263bc MD |
614 | DPRINTF(("ucomioctl: TIOCMBIS, 0x%x\n", d)); |
| 615 | (void)ucomctl(sc, d, DMBIS); | |
| 616 | break; | |
| 617 | case TIOCMBIC: | |
| fef8985e | 618 | d = *(int *)ap->a_data; |
| 984263bc MD |
619 | DPRINTF(("ucomioctl: TIOCMBIC, 0x%x\n", d)); |
| 620 | (void)ucomctl(sc, d, DMBIC); | |
| 621 | break; | |
| 622 | case TIOCMGET: | |
| 623 | d = ucomctl(sc, 0, DMGET); | |
| 624 | DPRINTF(("ucomioctl: TIOCMGET, 0x%x\n", d)); | |
| fef8985e | 625 | *(int *)ap->a_data = d; |
| 984263bc MD |
626 | break; |
| 627 | ||
| 628 | default: | |
| fef8985e | 629 | DPRINTF(("ucomioctl: error: our cmd = 0x%08lx\n", ap->a_cmd)); |
| 984263bc MD |
630 | error = ENOTTY; |
| 631 | break; | |
| 632 | } | |
| 633 | ||
| a5950399 | 634 | crit_exit(); |
| 984263bc MD |
635 | |
| 636 | return (error); | |
| 637 | } | |
| 638 | ||
| 6ed427ca | 639 | static int |
| 984263bc MD |
640 | ucomctl(struct ucom_softc *sc, int bits, int how) |
| 641 | { | |
| 642 | int mcr; | |
| 643 | int msr; | |
| 644 | int onoff; | |
| 645 | ||
| 646 | DPRINTF(("ucomctl: bits = 0x%x, how = %d\n", bits, how)); | |
| 647 | ||
| 648 | if (how == DMGET) { | |
| 649 | SET(bits, TIOCM_LE); /* always set TIOCM_LE bit */ | |
| 650 | DPRINTF(("ucomctl: DMGET: LE")); | |
| 651 | ||
| 652 | mcr = sc->sc_mcr; | |
| 653 | if (ISSET(mcr, UMCR_DTR)) { | |
| 654 | SET(bits, TIOCM_DTR); | |
| 655 | DPRINTF((" DTR")); | |
| 656 | } | |
| 657 | if (ISSET(mcr, UMCR_RTS)) { | |
| 658 | SET(bits, TIOCM_RTS); | |
| 659 | DPRINTF((" RTS")); | |
| 660 | } | |
| 661 | ||
| 662 | msr = sc->sc_msr; | |
| 663 | if (ISSET(msr, UMSR_CTS)) { | |
| 664 | SET(bits, TIOCM_CTS); | |
| 665 | DPRINTF((" CTS")); | |
| 666 | } | |
| 667 | if (ISSET(msr, UMSR_DCD)) { | |
| 668 | SET(bits, TIOCM_CD); | |
| 669 | DPRINTF((" CD")); | |
| 670 | } | |
| 671 | if (ISSET(msr, UMSR_DSR)) { | |
| 672 | SET(bits, TIOCM_DSR); | |
| 673 | DPRINTF((" DSR")); | |
| 674 | } | |
| 675 | if (ISSET(msr, UMSR_RI)) { | |
| 676 | SET(bits, TIOCM_RI); | |
| 677 | DPRINTF((" RI")); | |
| 678 | } | |
| 679 | ||
| 680 | DPRINTF(("\n")); | |
| 681 | ||
| 682 | return (bits); | |
| 683 | } | |
| 684 | ||
| 685 | mcr = 0; | |
| 686 | if (ISSET(bits, TIOCM_DTR)) | |
| 687 | SET(mcr, UMCR_DTR); | |
| 688 | if (ISSET(bits, TIOCM_RTS)) | |
| 689 | SET(mcr, UMCR_RTS); | |
| 690 | ||
| 691 | switch (how) { | |
| 692 | case DMSET: | |
| 693 | sc->sc_mcr = mcr; | |
| 694 | break; | |
| 695 | case DMBIS: | |
| 696 | sc->sc_mcr |= mcr; | |
| 697 | break; | |
| 698 | case DMBIC: | |
| 699 | sc->sc_mcr &= ~mcr; | |
| 700 | break; | |
| 701 | } | |
| 702 | ||
| 703 | onoff = ISSET(sc->sc_mcr, UMCR_DTR) ? 1 : 0; | |
| 704 | ucom_dtr(sc, onoff); | |
| 705 | ||
| 706 | onoff = ISSET(sc->sc_mcr, UMCR_RTS) ? 1 : 0; | |
| 707 | ucom_rts(sc, onoff); | |
| 708 | ||
| 709 | return (0); | |
| 710 | } | |
| 711 | ||
| 6ed427ca | 712 | static void |
| 984263bc MD |
713 | ucom_break(struct ucom_softc *sc, int onoff) |
| 714 | { | |
| 715 | DPRINTF(("ucom_break: onoff = %d\n", onoff)); | |
| 716 | ||
| 717 | if (sc->sc_callback->ucom_set == NULL) | |
| 718 | return; | |
| 719 | sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno, | |
| 720 | UCOM_SET_BREAK, onoff); | |
| 721 | } | |
| 722 | ||
| 6ed427ca | 723 | static void |
| 984263bc MD |
724 | ucom_dtr(struct ucom_softc *sc, int onoff) |
| 725 | { | |
| 726 | DPRINTF(("ucom_dtr: onoff = %d\n", onoff)); | |
| 727 | ||
| 728 | if (sc->sc_callback->ucom_set == NULL) | |
| 729 | return; | |
| 1550dfd9 | 730 | sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno, |
| 984263bc MD |
731 | UCOM_SET_DTR, onoff); |
| 732 | } | |
| 733 | ||
| 6ed427ca | 734 | static void |
| 984263bc MD |
735 | ucom_rts(struct ucom_softc *sc, int onoff) |
| 736 | { | |
| 737 | DPRINTF(("ucom_rts: onoff = %d\n", onoff)); | |
| 738 | ||
| 739 | if (sc->sc_callback->ucom_set == NULL) | |
| 740 | return; | |
| 1550dfd9 | 741 | sc->sc_callback->ucom_set(sc->sc_parent, sc->sc_portno, |
| 984263bc MD |
742 | UCOM_SET_RTS, onoff); |
| 743 | } | |
| 744 | ||
| 745 | void | |
| 746 | ucom_status_change(struct ucom_softc *sc) | |
| 747 | { | |
| 748 | struct tty *tp = sc->sc_tty; | |
| 749 | u_char old_msr; | |
| 750 | int onoff; | |
| 751 | ||
| 752 | if (sc->sc_callback->ucom_get_status == NULL) { | |
| 753 | sc->sc_lsr = 0; | |
| 754 | sc->sc_msr = 0; | |
| 755 | return; | |
| 756 | } | |
| 757 | ||
| 758 | old_msr = sc->sc_msr; | |
| 759 | sc->sc_callback->ucom_get_status(sc->sc_parent, sc->sc_portno, | |
| 760 | &sc->sc_lsr, &sc->sc_msr); | |
| 761 | if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) { | |
| 762 | if (sc->sc_poll == 0) | |
| 763 | return; | |
| 764 | onoff = ISSET(sc->sc_msr, UMSR_DCD) ? 1 : 0; | |
| 765 | DPRINTF(("ucom_status_change: DCD changed to %d\n", onoff)); | |
| 766 | (*linesw[tp->t_line].l_modem)(tp, onoff); | |
| 767 | } | |
| 768 | } | |
| 769 | ||
| 6ed427ca | 770 | static int |
| 984263bc MD |
771 | ucomparam(struct tty *tp, struct termios *t) |
| 772 | { | |
| 773 | struct ucom_softc *sc; | |
| 774 | int error; | |
| 775 | usbd_status uerr; | |
| 776 | ||
| 1e089e7e | 777 | sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev)); |
| 984263bc MD |
778 | |
| 779 | if (sc->sc_dying) | |
| 780 | return (EIO); | |
| 781 | ||
| 782 | DPRINTF(("ucomparam: sc = %p\n", sc)); | |
| 783 | ||
| 784 | /* Check requested parameters. */ | |
| 785 | if (t->c_ospeed < 0) { | |
| 786 | DPRINTF(("ucomparam: negative ospeed\n")); | |
| 787 | return (EINVAL); | |
| 788 | } | |
| 789 | if (t->c_ispeed && t->c_ispeed != t->c_ospeed) { | |
| 790 | DPRINTF(("ucomparam: mismatch ispeed and ospeed\n")); | |
| 791 | return (EINVAL); | |
| 792 | } | |
| 793 | ||
| 794 | /* | |
| 795 | * If there were no changes, don't do anything. This avoids dropping | |
| 796 | * input and improves performance when all we did was frob things like | |
| 797 | * VMIN and VTIME. | |
| 798 | */ | |
| 799 | if (tp->t_ospeed == t->c_ospeed && | |
| 800 | tp->t_cflag == t->c_cflag) | |
| 801 | return (0); | |
| 802 | ||
| 803 | /* And copy to tty. */ | |
| 804 | tp->t_ispeed = 0; | |
| 805 | tp->t_ospeed = t->c_ospeed; | |
| 806 | tp->t_cflag = t->c_cflag; | |
| 807 | ||
| 808 | if (sc->sc_callback->ucom_param == NULL) | |
| 809 | return (0); | |
| 810 | ||
| 811 | ucomstopread(sc); | |
| 812 | ||
| 813 | error = sc->sc_callback->ucom_param(sc->sc_parent, sc->sc_portno, t); | |
| 814 | if (error) { | |
| 815 | DPRINTF(("ucomparam: callback: error = %d\n", error)); | |
| 816 | return (error); | |
| 817 | } | |
| 818 | ||
| 819 | ttsetwater(tp); | |
| 820 | ||
| 821 | if (t->c_cflag & CRTS_IFLOW) { | |
| 822 | sc->sc_state |= UCS_RTS_IFLOW; | |
| 823 | } else if (sc->sc_state & UCS_RTS_IFLOW) { | |
| 824 | sc->sc_state &= ~UCS_RTS_IFLOW; | |
| 825 | (void)ucomctl(sc, UMCR_RTS, DMBIS); | |
| 826 | } | |
| 827 | ||
| 828 | disc_optim(tp, t, sc); | |
| 829 | ||
| 830 | uerr = ucomstartread(sc); | |
| 831 | if (uerr != USBD_NORMAL_COMPLETION) | |
| 832 | return (EIO); | |
| 833 | ||
| 834 | return (0); | |
| 835 | } | |
| 836 | ||
| 6ed427ca | 837 | static void |
| 984263bc MD |
838 | ucomstart(struct tty *tp) |
| 839 | { | |
| 840 | struct ucom_softc *sc; | |
| 841 | struct cblock *cbp; | |
| 842 | usbd_status err; | |
| 984263bc MD |
843 | u_char *data; |
| 844 | int cnt; | |
| 845 | ||
| 1e089e7e | 846 | sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev)); |
| 984263bc MD |
847 | DPRINTF(("ucomstart: sc = %p\n", sc)); |
| 848 | ||
| 849 | if (sc->sc_dying) | |
| 850 | return; | |
| 851 | ||
| a5950399 | 852 | crit_enter(); |
| 984263bc MD |
853 | |
| 854 | if (tp->t_state & TS_TBLOCK) { | |
| 855 | if (ISSET(sc->sc_mcr, UMCR_RTS) && | |
| 856 | ISSET(sc->sc_state, UCS_RTS_IFLOW)) { | |
| 857 | DPRINTF(("ucomstart: clear RTS\n")); | |
| 858 | (void)ucomctl(sc, UMCR_RTS, DMBIC); | |
| 859 | } | |
| 860 | } else { | |
| 861 | if (!ISSET(sc->sc_mcr, UMCR_RTS) && | |
| 862 | tp->t_rawq.c_cc <= tp->t_ilowat && | |
| 863 | ISSET(sc->sc_state, UCS_RTS_IFLOW)) { | |
| 864 | DPRINTF(("ucomstart: set RTS\n")); | |
| 865 | (void)ucomctl(sc, UMCR_RTS, DMBIS); | |
| 866 | } | |
| 867 | } | |
| 868 | ||
| 869 | if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) { | |
| 870 | ttwwakeup(tp); | |
| 871 | DPRINTF(("ucomstart: stopped\n")); | |
| 872 | goto out; | |
| 873 | } | |
| 874 | ||
| 875 | if (tp->t_outq.c_cc <= tp->t_olowat) { | |
| 876 | if (ISSET(tp->t_state, TS_SO_OLOWAT)) { | |
| 877 | CLR(tp->t_state, TS_SO_OLOWAT); | |
| 878 | wakeup(TSA_OLOWAT(tp)); | |
| 879 | } | |
| 375a9af6 | 880 | selwakeup(&tp->t_wsel); |
| 984263bc MD |
881 | if (tp->t_outq.c_cc == 0) { |
| 882 | if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) == | |
| 883 | TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) { | |
| 884 | CLR(tp->t_state, TS_SO_OCOMPLETE); | |
| 885 | wakeup(TSA_OCOMPLETE(tp)); | |
| 886 | } | |
| 887 | goto out; | |
| 888 | } | |
| 889 | } | |
| 890 | ||
| 891 | /* Grab the first contiguous region of buffer space. */ | |
| 892 | data = tp->t_outq.c_cf; | |
| 893 | cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND); | |
| 894 | cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc); | |
| 895 | ||
| 896 | if (cnt == 0) { | |
| 897 | DPRINTF(("ucomstart: cnt == 0\n")); | |
| 898 | goto out; | |
| 899 | } | |
| 900 | ||
| 901 | SET(tp->t_state, TS_BUSY); | |
| 902 | ||
| 903 | if (cnt > sc->sc_obufsize) { | |
| 904 | DPRINTF(("ucomstart: big buffer %d chars\n", cnt)); | |
| 905 | cnt = sc->sc_obufsize; | |
| 906 | } | |
| 907 | if (sc->sc_callback->ucom_write != NULL) | |
| 908 | sc->sc_callback->ucom_write(sc->sc_parent, sc->sc_portno, | |
| 909 | sc->sc_obuf, data, &cnt); | |
| 910 | else | |
| 911 | memcpy(sc->sc_obuf, data, cnt); | |
| 912 | ||
| 913 | DPRINTF(("ucomstart: %d chars\n", cnt)); | |
| 1550dfd9 | 914 | usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe, |
| 984263bc MD |
915 | (usbd_private_handle)sc, sc->sc_obuf, cnt, |
| 916 | USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb); | |
| 917 | /* What can we do on error? */ | |
| 918 | err = usbd_transfer(sc->sc_oxfer); | |
| 919 | if (err != USBD_IN_PROGRESS) | |
| e3869ec7 | 920 | kprintf("ucomstart: err=%s\n", usbd_errstr(err)); |
| 984263bc MD |
921 | |
| 922 | ttwwakeup(tp); | |
| 923 | ||
| 924 | out: | |
| a5950399 | 925 | crit_exit(); |
| 984263bc MD |
926 | } |
| 927 | ||
| 6ed427ca | 928 | static void |
| 984263bc MD |
929 | ucomstop(struct tty *tp, int flag) |
| 930 | { | |
| 931 | struct ucom_softc *sc; | |
| 984263bc | 932 | |
| 1e089e7e | 933 | sc = devclass_get_softc(ucom_devclass, UCOMUNIT(tp->t_dev)); |
| 984263bc MD |
934 | |
| 935 | DPRINTF(("ucomstop: %d\n", flag)); | |
| 936 | ||
| 937 | if (flag & FREAD) { | |
| 6a76f45a MD |
938 | /* |
| 939 | * This is just supposed to flush pending receive data, | |
| 940 | * not stop the reception of data entirely! | |
| 941 | */ | |
| 984263bc MD |
942 | DPRINTF(("ucomstop: read\n")); |
| 943 | ucomstopread(sc); | |
| 6a76f45a | 944 | ucomstartread(sc); |
| 984263bc MD |
945 | } |
| 946 | ||
| 947 | if (flag & FWRITE) { | |
| 948 | DPRINTF(("ucomstop: write\n")); | |
| a5950399 | 949 | crit_enter(); |
| 984263bc MD |
950 | if (ISSET(tp->t_state, TS_BUSY)) { |
| 951 | /* XXX do what? */ | |
| 952 | if (!ISSET(tp->t_state, TS_TTSTOP)) | |
| 953 | SET(tp->t_state, TS_FLUSH); | |
| 954 | } | |
| a5950399 | 955 | crit_exit(); |
| 984263bc MD |
956 | } |
| 957 | ||
| 958 | DPRINTF(("ucomstop: done\n")); | |
| 959 | } | |
| 960 | ||
| 6ed427ca | 961 | static void |
| 984263bc MD |
962 | ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status) |
| 963 | { | |
| 964 | struct ucom_softc *sc = (struct ucom_softc *)p; | |
| 965 | struct tty *tp = sc->sc_tty; | |
| 966 | u_int32_t cc; | |
| 984263bc MD |
967 | |
| 968 | DPRINTF(("ucomwritecb: status = %d\n", status)); | |
| 969 | ||
| 970 | if (status == USBD_CANCELLED || sc->sc_dying) | |
| 971 | goto error; | |
| 972 | ||
| 973 | if (status != USBD_NORMAL_COMPLETION) { | |
| e3869ec7 | 974 | kprintf("%s: ucomwritecb: %s\n", |
| 6ed427ca | 975 | device_get_nameunit(sc->sc_dev), usbd_errstr(status)); |
| 984263bc MD |
976 | if (status == USBD_STALLED) |
| 977 | usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe); | |
| 6a76f45a MD |
978 | /* |
| 979 | * XXX. We may need a flag to sequence ucomstopread() and | |
| 980 | * ucomstartread() to handle the case where ucomstartread() | |
| 981 | * is called after ucomstopread() but before the request has | |
| 982 | * been properly canceled? | |
| 983 | */ | |
| 984263bc MD |
984 | goto error; |
| 985 | } | |
| 986 | ||
| 987 | usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL); | |
| 988 | DPRINTF(("ucomwritecb: cc = %d\n", cc)); | |
| 62ade751 | 989 | if (cc <= sc->sc_opkthdrlen) { |
| e3869ec7 | 990 | kprintf("%s: sent size too small, cc = %d\n", |
| 6ed427ca | 991 | device_get_nameunit(sc->sc_dev), cc); |
| 62ade751 MD |
992 | goto error; |
| 993 | } | |
| 984263bc MD |
994 | |
| 995 | /* convert from USB bytes to tty bytes */ | |
| 996 | cc -= sc->sc_opkthdrlen; | |
| 997 | ||
| a5950399 | 998 | crit_enter(); |
| 984263bc MD |
999 | CLR(tp->t_state, TS_BUSY); |
| 1000 | if (ISSET(tp->t_state, TS_FLUSH)) | |
| 1001 | CLR(tp->t_state, TS_FLUSH); | |
| 1002 | else | |
| 1003 | ndflush(&tp->t_outq, cc); | |
| 1004 | (*linesw[tp->t_line].l_start)(tp); | |
| a5950399 | 1005 | crit_exit(); |
| 984263bc MD |
1006 | |
| 1007 | return; | |
| 1008 | ||
| 1009 | error: | |
| a5950399 | 1010 | crit_enter(); |
| 984263bc | 1011 | CLR(tp->t_state, TS_BUSY); |
| a5950399 | 1012 | crit_exit(); |
| 984263bc MD |
1013 | return; |
| 1014 | } | |
| 1015 | ||
| 6ed427ca | 1016 | static usbd_status |
| 984263bc MD |
1017 | ucomstartread(struct ucom_softc *sc) |
| 1018 | { | |
| 1019 | usbd_status err; | |
| 1020 | ||
| 1021 | DPRINTF(("ucomstartread: start\n")); | |
| 1022 | ||
| 1023 | sc->sc_state &= ~UCS_RXSTOP; | |
| 1024 | ||
| 1025 | if (sc->sc_bulkin_pipe == NULL) | |
| 1026 | return (USBD_NORMAL_COMPLETION); | |
| 1027 | ||
| 1550dfd9 MD |
1028 | usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe, |
| 1029 | (usbd_private_handle)sc, | |
| 984263bc MD |
1030 | sc->sc_ibuf, sc->sc_ibufsize, |
| 1031 | USBD_SHORT_XFER_OK | USBD_NO_COPY, | |
| 1032 | USBD_NO_TIMEOUT, ucomreadcb); | |
| 1033 | ||
| 1034 | err = usbd_transfer(sc->sc_ixfer); | |
| 1035 | if (err != USBD_IN_PROGRESS) { | |
| 1036 | DPRINTF(("ucomstartread: err = %s\n", usbd_errstr(err))); | |
| 1037 | return (err); | |
| 1038 | } | |
| 1039 | ||
| 1040 | return (USBD_NORMAL_COMPLETION); | |
| 1041 | } | |
| 1042 | ||
| 6ed427ca | 1043 | static void |
| 984263bc MD |
1044 | ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status) |
| 1045 | { | |
| 1046 | struct ucom_softc *sc = (struct ucom_softc *)p; | |
| 1047 | struct tty *tp = sc->sc_tty; | |
| 1048 | int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint; | |
| 1049 | usbd_status err; | |
| 1050 | u_int32_t cc; | |
| 1051 | u_char *cp; | |
| 1052 | int lostcc; | |
| 984263bc MD |
1053 | |
| 1054 | DPRINTF(("ucomreadcb: status = %d\n", status)); | |
| 1055 | ||
| 1056 | if (status != USBD_NORMAL_COMPLETION) { | |
| 1057 | if (!(sc->sc_state & UCS_RXSTOP)) | |
| e3869ec7 | 1058 | kprintf("%s: ucomreadcb: %s\n", |
| 6ed427ca | 1059 | device_get_nameunit(sc->sc_dev), usbd_errstr(status)); |
| 984263bc MD |
1060 | if (status == USBD_STALLED) |
| 1061 | usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe); | |
| 1062 | /* XXX we should restart after some delay. */ | |
| 1063 | return; | |
| 1064 | } | |
| 1065 | ||
| 1066 | usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL); | |
| 1067 | DPRINTF(("ucomreadcb: got %d chars, tp = %p\n", cc, tp)); | |
| 62ade751 MD |
1068 | if (cc == 0) |
| 1069 | goto resubmit; | |
| 1070 | ||
| 1071 | if (sc->sc_callback->ucom_read != NULL) { | |
| 984263bc MD |
1072 | sc->sc_callback->ucom_read(sc->sc_parent, sc->sc_portno, |
| 1073 | &cp, &cc); | |
| 62ade751 MD |
1074 | } |
| 1075 | ||
| 1076 | if (cc > sc->sc_ibufsize) { | |
| e3869ec7 | 1077 | kprintf("%s: invalid receive data size, %d chars\n", |
| 6ed427ca | 1078 | device_get_nameunit(sc->sc_dev), cc); |
| 62ade751 MD |
1079 | goto resubmit; |
| 1080 | } | |
| 1081 | if (cc < 1) | |
| 1082 | goto resubmit; | |
| 984263bc | 1083 | |
| a5950399 | 1084 | crit_enter(); |
| 984263bc MD |
1085 | if (tp->t_state & TS_CAN_BYPASS_L_RINT) { |
| 1086 | if (tp->t_rawq.c_cc + cc > tp->t_ihiwat | |
| 1087 | && (sc->sc_state & UCS_RTS_IFLOW | |
| 1088 | || tp->t_iflag & IXOFF) | |
| 1089 | && !(tp->t_state & TS_TBLOCK)) | |
| 1090 | ttyblock(tp); | |
| 1091 | lostcc = b_to_q((char *)cp, cc, &tp->t_rawq); | |
| 1092 | tp->t_rawcc += cc; | |
| 6a76f45a MD |
1093 | if (sc->hotchar) { |
| 1094 | while (cc) { | |
| 1095 | if (*cp == sc->hotchar) | |
| 1096 | break; | |
| 1097 | --cc; | |
| 1098 | ++cp; | |
| 1099 | } | |
| 1100 | if (cc) | |
| 1101 | setsofttty(); | |
| 1102 | } | |
| 984263bc MD |
1103 | ttwakeup(tp); |
| 1104 | if (tp->t_state & TS_TTSTOP | |
| 1105 | && (tp->t_iflag & IXANY | |
| 1106 | || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { | |
| 1107 | tp->t_state &= ~TS_TTSTOP; | |
| 1108 | tp->t_lflag &= ~FLUSHO; | |
| 1109 | ucomstart(tp); | |
| 1110 | } | |
| 1111 | if (lostcc > 0) | |
| 6ed427ca | 1112 | kprintf("%s: lost %d chars\n", device_get_nameunit(sc->sc_dev), |
| 984263bc MD |
1113 | lostcc); |
| 1114 | } else { | |
| 1115 | /* Give characters to tty layer. */ | |
| 62ade751 MD |
1116 | while (cc > 0) { |
| 1117 | DPRINTFN(7, ("ucomreadcb: char = 0x%02x\n", *cp)); | |
| 1118 | if ((*rint)(*cp, tp) == -1) { | |
| 984263bc | 1119 | /* XXX what should we do? */ |
| e3869ec7 | 1120 | kprintf("%s: lost %d chars\n", |
| 6ed427ca | 1121 | device_get_nameunit(sc->sc_dev), cc); |
| 984263bc MD |
1122 | break; |
| 1123 | } | |
| 62ade751 MD |
1124 | cc--; |
| 1125 | cp++; | |
| 984263bc MD |
1126 | } |
| 1127 | } | |
| a5950399 | 1128 | crit_exit(); |
| 984263bc | 1129 | |
| 62ade751 | 1130 | resubmit: |
| 984263bc MD |
1131 | err = ucomstartread(sc); |
| 1132 | if (err) { | |
| 6ed427ca | 1133 | kprintf("%s: read start failed\n", device_get_nameunit(sc->sc_dev)); |
| 984263bc MD |
1134 | /* XXX what should we dow now? */ |
| 1135 | } | |
| 1136 | ||
| 1137 | if ((sc->sc_state & UCS_RTS_IFLOW) && !ISSET(sc->sc_mcr, UMCR_RTS) | |
| 1138 | && !(tp->t_state & TS_TBLOCK)) | |
| 1139 | ucomctl(sc, UMCR_RTS, DMBIS); | |
| 1140 | } | |
| 1141 | ||
| 6ed427ca | 1142 | static void |
| 984263bc MD |
1143 | ucom_cleanup(struct ucom_softc *sc) |
| 1144 | { | |
| 1145 | DPRINTF(("ucom_cleanup: closing pipes\n")); | |
| 1146 | ||
| 1147 | ucom_shutdown(sc); | |
| 1148 | if (sc->sc_bulkin_pipe != NULL) { | |
| 1149 | usbd_abort_pipe(sc->sc_bulkin_pipe); | |
| 1150 | usbd_close_pipe(sc->sc_bulkin_pipe); | |
| 1151 | sc->sc_bulkin_pipe = NULL; | |
| 1152 | } | |
| 1153 | if (sc->sc_bulkout_pipe != NULL) { | |
| 1154 | usbd_abort_pipe(sc->sc_bulkout_pipe); | |
| 1155 | usbd_close_pipe(sc->sc_bulkout_pipe); | |
| 1156 | sc->sc_bulkout_pipe = NULL; | |
| 1157 | } | |
| 1158 | if (sc->sc_ixfer != NULL) { | |
| 1159 | usbd_free_xfer(sc->sc_ixfer); | |
| 1160 | sc->sc_ixfer = NULL; | |
| 1161 | } | |
| 1162 | if (sc->sc_oxfer != NULL) { | |
| 1163 | usbd_free_xfer(sc->sc_oxfer); | |
| 1164 | sc->sc_oxfer = NULL; | |
| 1165 | } | |
| 1166 | } | |
| 1167 | ||
| 6ed427ca | 1168 | static void |
| 984263bc MD |
1169 | ucomstopread(struct ucom_softc *sc) |
| 1170 | { | |
| 1171 | usbd_status err; | |
| 1172 | ||
| 1173 | DPRINTF(("ucomstopread: enter\n")); | |
| 1174 | ||
| 1175 | if (!(sc->sc_state & UCS_RXSTOP)) { | |
| 62ade751 | 1176 | sc->sc_state |= UCS_RXSTOP; |
| 984263bc MD |
1177 | if (sc->sc_bulkin_pipe == NULL) { |
| 1178 | DPRINTF(("ucomstopread: bulkin pipe NULL\n")); | |
| 1179 | return; | |
| 1180 | } | |
| 984263bc MD |
1181 | err = usbd_abort_pipe(sc->sc_bulkin_pipe); |
| 1182 | if (err) { | |
| 1183 | DPRINTF(("ucomstopread: err = %s\n", | |
| 1184 | usbd_errstr(err))); | |
| 1185 | } | |
| 1186 | } | |
| 1187 | ||
| 1188 | DPRINTF(("ucomstopread: leave\n")); | |
| 1189 | } | |
| 1190 | ||
| 1191 | static void | |
| 1192 | disc_optim(struct tty *tp, struct termios *t, struct ucom_softc *sc) | |
| 1193 | { | |
| 1194 | if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) | |
| 1195 | && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) | |
| 1196 | && (!(t->c_iflag & PARMRK) | |
| 1197 | || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) | |
| 1198 | && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) | |
| 1199 | && linesw[tp->t_line].l_rint == ttyinput) { | |
| 1200 | DPRINTF(("disc_optim: bypass l_rint\n")); | |
| 1201 | tp->t_state |= TS_CAN_BYPASS_L_RINT; | |
| 1202 | } else { | |
| 1203 | DPRINTF(("disc_optim: can't bypass l_rint\n")); | |
| 1204 | tp->t_state &= ~TS_CAN_BYPASS_L_RINT; | |
| 1205 | } | |
| 1206 | sc->hotchar = linesw[tp->t_line].l_hotchar; | |
| 1207 | } |