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