ucom.c revision 1.144 1 /* $NetBSD: ucom.c,v 1.144 2025/10/10 17:33:16 skrll 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * This code is very heavily based on the 16550 driver, com.c.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ucom.c,v 1.144 2025/10/10 17:33:16 skrll Exp $");
38
39 #ifdef _KERNEL_OPT
40 #include "opt_ntp.h"
41 #include "opt_usb.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/ioctl.h>
48 #include <sys/conf.h>
49 #include <sys/tty.h>
50 #include <sys/file.h>
51 #include <sys/select.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/device.h>
55 #include <sys/poll.h>
56 #include <sys/queue.h>
57 #include <sys/kauth.h>
58 #include <sys/sysctl.h>
59 #include <sys/timepps.h>
60 #include <sys/rndsource.h>
61
62 #include <dev/usb/usb.h>
63
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbdevs.h>
67 #include <dev/usb/usb_quirks.h>
68 #include <dev/usb/usbhist.h>
69
70 #include <dev/usb/ucomvar.h>
71
72 #include "ucom.h"
73 #include "locators.h"
74 #include "ioconf.h"
75
76 #if NUCOM > 0
77
78 #ifdef USB_DEBUG
79 #ifndef UCOM_DEBUG
80 #define ucomdebug 0
81 #else
82 int ucomdebug = 0;
83
84 SYSCTL_SETUP(sysctl_hw_ucom_setup, "sysctl hw.ucom setup")
85 {
86 int err;
87 const struct sysctlnode *rnode;
88 const struct sysctlnode *cnode;
89
90 err = sysctl_createv(clog, 0, NULL, &rnode,
91 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ucom",
92 SYSCTL_DESCR("ucom global controls"),
93 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
94
95 if (err)
96 goto fail;
97
98 /* control debugging printfs */
99 err = sysctl_createv(clog, 0, &rnode, &cnode,
100 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
101 "debug", SYSCTL_DESCR("Enable debugging output"),
102 NULL, 0, &ucomdebug, sizeof(ucomdebug), CTL_CREATE, CTL_EOL);
103 if (err)
104 goto fail;
105
106 return;
107 fail:
108 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
109 }
110
111 #endif /* UCOM_DEBUG */
112 #endif /* USB_DEBUG */
113
114 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(ucomdebug,1,FMT,A,B,C,D)
115 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(ucomdebug,N,FMT,A,B,C,D)
116 #define UCOMHIST_FUNC() USBHIST_FUNC()
117 #define UCOMHIST_CALLED(name) USBHIST_CALLED(ucomdebug)
118
119 #define UCOMCALLUNIT_MASK TTCALLUNIT_MASK
120 #define UCOMUNIT_MASK TTUNIT_MASK
121 #define UCOMDIALOUT_MASK TTDIALOUT_MASK
122
123 #define UCOMCALLUNIT(x) TTCALLUNIT(x)
124 #define UCOMUNIT(x) TTUNIT(x)
125 #define UCOMDIALOUT(x) TTDIALOUT(x)
126 #define ucom_unit tty_unit
127
128 /*
129 * XXX: We can submit multiple input/output buffers to the usb stack
130 * to improve throughput, but the usb stack is too lame to deal with this
131 * in a number of places.
132 */
133 #define UCOM_IN_BUFFS 1
134 #define UCOM_OUT_BUFFS 1
135
136 struct ucom_buffer {
137 SIMPLEQ_ENTRY(ucom_buffer) ub_link;
138 struct usbd_xfer *ub_xfer;
139 u_char *ub_data;
140 u_int ub_len;
141 u_int ub_index;
142 };
143
144 struct ucom_softc {
145 device_t sc_dev; /* base device */
146
147 struct usbd_device * sc_udev; /* USB device */
148 struct usbd_interface * sc_iface; /* data interface */
149
150 int sc_bulkin_no; /* bulk in endpoint address */
151 struct usbd_pipe * sc_bulkin_pipe;/* bulk in pipe */
152 u_int sc_ibufsize; /* read buffer size */
153 u_int sc_ibufsizepad; /* read buffer size padded */
154 struct ucom_buffer sc_ibuff[UCOM_IN_BUFFS];
155 SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_empty;
156 SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_full;
157
158 int sc_bulkout_no; /* bulk out endpoint address */
159 struct usbd_pipe * sc_bulkout_pipe;/* bulk out pipe */
160 u_int sc_obufsize; /* write buffer size */
161 u_int sc_opkthdrlen; /* header length of */
162 struct ucom_buffer sc_obuff[UCOM_OUT_BUFFS];
163 SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_free;
164 SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_full;
165
166 void *sc_si;
167
168 const struct ucom_methods *sc_methods;
169 void *sc_parent;
170 int sc_portno;
171
172 struct tty *sc_tty; /* our tty */
173 u_char sc_lsr;
174 u_char sc_msr;
175 u_char sc_mcr;
176 volatile u_char sc_rx_stopped;
177 u_char sc_rx_unblock;
178 u_char sc_tx_stopped;
179 int sc_swflags;
180
181 enum ucom_state {
182 UCOM_DEAD,
183 UCOM_ATTACHED,
184 UCOM_OPENING,
185 UCOM_OPEN
186 } sc_state;
187 bool sc_closing; /* software is closing */
188 bool sc_dying; /* hardware is gone */
189
190 struct pps_state sc_pps_state; /* pps state */
191
192 krndsource_t sc_rndsource; /* random source */
193
194 kmutex_t sc_lock;
195 kcondvar_t sc_statecv;
196
197 struct timeval sc_hup_time;
198 };
199
200 dev_type_open(ucomopen);
201 dev_type_cancel(ucomcancel);
202 dev_type_close(ucomclose);
203 dev_type_read(ucomread);
204 dev_type_write(ucomwrite);
205 dev_type_ioctl(ucomioctl);
206 dev_type_stop(ucomstop);
207 dev_type_tty(ucomtty);
208 dev_type_poll(ucompoll);
209
210 const struct cdevsw ucom_cdevsw = {
211 .d_open = ucomopen,
212 .d_cancel = ucomcancel,
213 .d_close = ucomclose,
214 .d_read = ucomread,
215 .d_write = ucomwrite,
216 .d_ioctl = ucomioctl,
217 .d_stop = ucomstop,
218 .d_tty = ucomtty,
219 .d_poll = ucompoll,
220 .d_mmap = nommap,
221 .d_kqfilter = ttykqfilter,
222 .d_discard = nodiscard,
223 .d_cfdriver = &ucom_cd,
224 .d_devtounit = ucom_unit,
225 .d_flag = D_TTY | D_MPSAFE
226 };
227
228 static void ucom_cleanup(struct ucom_softc *, int);
229 static int ucomparam(struct tty *, struct termios *);
230 static int ucomhwiflow(struct tty *, int);
231 static void ucomstart(struct tty *);
232 static void ucom_shutdown(struct ucom_softc *);
233 static void ucom_dtr(struct ucom_softc *, int);
234 static void ucom_rts(struct ucom_softc *, int);
235 static void ucom_break(struct ucom_softc *, int);
236 static void tiocm_to_ucom(struct ucom_softc *, u_long, int);
237 static int ucom_to_tiocm(struct ucom_softc *);
238
239 static void ucomreadcb(struct usbd_xfer *, void *, usbd_status);
240 static void ucom_submit_write(struct ucom_softc *, struct ucom_buffer *);
241 static void ucom_write_status(struct ucom_softc *, struct ucom_buffer *,
242 usbd_status);
243
244 static void ucomwritecb(struct usbd_xfer *, void *, usbd_status);
245 static void ucom_read_complete(struct ucom_softc *);
246 static int ucomsubmitread(struct ucom_softc *, struct ucom_buffer *);
247 static void ucom_softintr(void *);
248
249 int ucom_match(device_t, cfdata_t, void *);
250 void ucom_attach(device_t, device_t, void *);
251 int ucom_detach(device_t, int);
252
253 CFATTACH_DECL_NEW(ucom, sizeof(struct ucom_softc), ucom_match, ucom_attach,
254 ucom_detach, NULL);
255
256 int
257 ucom_match(device_t parent, cfdata_t match, void *aux)
258 {
259 return 1;
260 }
261
262 void
263 ucom_attach(device_t parent, device_t self, void *aux)
264 {
265 struct ucom_softc *sc = device_private(self);
266 struct ucom_attach_args *ucaa = aux;
267
268 UCOMHIST_FUNC(); UCOMHIST_CALLED();
269
270 if (ucaa->ucaa_info != NULL)
271 aprint_normal(": %s", ucaa->ucaa_info);
272 aprint_normal("\n");
273
274 prop_dictionary_set_int32(device_properties(self), "port",
275 ucaa->ucaa_portno);
276
277 sc->sc_dev = self;
278 sc->sc_udev = ucaa->ucaa_device;
279 sc->sc_iface = ucaa->ucaa_iface;
280 sc->sc_bulkout_no = ucaa->ucaa_bulkout;
281 sc->sc_bulkin_no = ucaa->ucaa_bulkin;
282 sc->sc_ibufsize = ucaa->ucaa_ibufsize;
283 sc->sc_ibufsizepad = ucaa->ucaa_ibufsizepad;
284 sc->sc_obufsize = ucaa->ucaa_obufsize;
285 sc->sc_opkthdrlen = ucaa->ucaa_opkthdrlen;
286 sc->sc_methods = ucaa->ucaa_methods;
287 sc->sc_parent = ucaa->ucaa_arg;
288 sc->sc_portno = ucaa->ucaa_portno;
289
290 sc->sc_lsr = 0;
291 sc->sc_msr = 0;
292 sc->sc_mcr = 0;
293 sc->sc_tx_stopped = 0;
294 sc->sc_swflags = 0;
295 sc->sc_closing = false;
296 sc->sc_dying = false;
297 sc->sc_state = UCOM_DEAD;
298
299 sc->sc_si = softint_establish(SOFTINT_USB, ucom_softintr, sc);
300 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
301 cv_init(&sc->sc_statecv, "ucomstate");
302
303 rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
304 RND_TYPE_TTY, RND_FLAG_DEFAULT);
305
306 SIMPLEQ_INIT(&sc->sc_ibuff_empty);
307 SIMPLEQ_INIT(&sc->sc_ibuff_full);
308 SIMPLEQ_INIT(&sc->sc_obuff_free);
309 SIMPLEQ_INIT(&sc->sc_obuff_full);
310
311 memset(sc->sc_ibuff, 0, sizeof(sc->sc_ibuff));
312 memset(sc->sc_obuff, 0, sizeof(sc->sc_obuff));
313
314 DPRINTF("open pipes in=%jd out=%jd",
315 sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0);
316
317 struct ucom_buffer *ub;
318 usbd_status err;
319 int error;
320
321 /* Open the bulk pipes */
322 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no,
323 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
324 if (err) {
325 DPRINTF("open bulk in error (addr %jd), err=%jd",
326 sc->sc_bulkin_no, err, 0, 0);
327 error = EIO;
328 goto fail_0;
329 }
330 /* Allocate input buffers */
331 for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
332 ub++) {
333 error = usbd_create_xfer(sc->sc_bulkin_pipe,
334 sc->sc_ibufsizepad, 0, 0,
335 &ub->ub_xfer);
336 if (error)
337 goto fail_1;
338 ub->ub_data = usbd_get_buffer(ub->ub_xfer);
339 }
340
341 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
342 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
343 if (err) {
344 DPRINTF("open bulk out error (addr %jd), err=%jd",
345 sc->sc_bulkout_no, err, 0, 0);
346 error = EIO;
347 goto fail_1;
348 }
349 for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
350 ub++) {
351 error = usbd_create_xfer(sc->sc_bulkout_pipe,
352 sc->sc_obufsize, 0, 0, &ub->ub_xfer);
353 if (error)
354 goto fail_2;
355 ub->ub_data = usbd_get_buffer(ub->ub_xfer);
356 SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
357 }
358
359 struct tty *tp = tty_alloc();
360 tp->t_oproc = ucomstart;
361 tp->t_param = ucomparam;
362 tp->t_hwiflow = ucomhwiflow;
363 sc->sc_tty = tp;
364
365 DPRINTF("tty_attach %#jx", (uintptr_t)tp, 0, 0, 0);
366 tty_attach(tp);
367
368 if (!pmf_device_register(self, NULL, NULL))
369 aprint_error_dev(self, "couldn't establish power handler\n");
370
371 sc->sc_state = UCOM_ATTACHED;
372 return;
373
374 fail_2:
375 for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
376 ub++) {
377 if (ub->ub_xfer)
378 usbd_destroy_xfer(ub->ub_xfer);
379 }
380 usbd_close_pipe(sc->sc_bulkout_pipe);
381 sc->sc_bulkout_pipe = NULL;
382
383 fail_1:
384 for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
385 ub++) {
386 if (ub->ub_xfer)
387 usbd_destroy_xfer(ub->ub_xfer);
388 }
389 usbd_close_pipe(sc->sc_bulkin_pipe);
390 sc->sc_bulkin_pipe = NULL;
391
392 fail_0:
393 aprint_error_dev(self, "attach failed, error=%d\n", error);
394 }
395
396 int
397 ucom_detach(device_t self, int flags)
398 {
399 struct ucom_softc *sc = device_private(self);
400 struct tty *tp = sc->sc_tty;
401 int maj, mn;
402 int i;
403
404 UCOMHIST_FUNC(); UCOMHIST_CALLED();
405
406 DPRINTF("sc=%#jx flags=%jd tp=%#jx", (uintptr_t)sc, flags,
407 (uintptr_t)tp, 0);
408 DPRINTF("... pipe=%jd,%jd", sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0);
409
410 /* Prevent new opens from hanging. */
411 mutex_enter(&sc->sc_lock);
412 sc->sc_dying = true;
413 mutex_exit(&sc->sc_lock);
414
415 pmf_device_deregister(self);
416
417 /* tty is now off. */
418 if (tp != NULL) {
419 ttylock(tp);
420 CLR(tp->t_state, TS_CARR_ON);
421 CLR(tp->t_cflag, CLOCAL | MDMBUF);
422 ttyunlock(tp);
423 }
424
425 /* locate the major number */
426 maj = cdevsw_lookup_major(&ucom_cdevsw);
427
428 /* Nuke the vnodes for any open instances. */
429 mn = device_unit(self);
430 DPRINTF("maj=%jd mn=%jd", maj, mn, 0, 0);
431 vdevgone(maj, mn, mn, VCHR);
432 vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
433 vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
434
435 softint_disestablish(sc->sc_si);
436
437 /* Detach and free the tty. */
438 if (tp != NULL) {
439 tty_detach(tp);
440 tty_free(tp);
441 sc->sc_tty = NULL;
442 }
443
444 for (i = 0; i < UCOM_IN_BUFFS; i++) {
445 if (sc->sc_ibuff[i].ub_xfer != NULL)
446 usbd_destroy_xfer(sc->sc_ibuff[i].ub_xfer);
447 }
448
449 for (i = 0; i < UCOM_OUT_BUFFS; i++) {
450 if (sc->sc_obuff[i].ub_xfer != NULL)
451 usbd_destroy_xfer(sc->sc_obuff[i].ub_xfer);
452 }
453
454 if (sc->sc_bulkin_pipe != NULL) {
455 usbd_close_pipe(sc->sc_bulkin_pipe);
456 sc->sc_bulkin_pipe = NULL;
457 }
458
459 if (sc->sc_bulkout_pipe != NULL) {
460 usbd_close_pipe(sc->sc_bulkout_pipe);
461 sc->sc_bulkout_pipe = NULL;
462 }
463
464 /* Detach the random source */
465 rnd_detach_source(&sc->sc_rndsource);
466
467 mutex_destroy(&sc->sc_lock);
468 cv_destroy(&sc->sc_statecv);
469
470 return 0;
471 }
472
473 void
474 ucom_shutdown(struct ucom_softc *sc)
475 {
476 struct tty *tp = sc->sc_tty;
477
478 UCOMHIST_FUNC(); UCOMHIST_CALLED();
479
480 /*
481 * Hang up if necessary. Wait a bit, so the other side has time to
482 * notice even if we immediately open the port again.
483 */
484 if (ISSET(tp->t_cflag, HUPCL)) {
485 ucom_dtr(sc, 0);
486 mutex_enter(&sc->sc_lock);
487 microuptime(&sc->sc_hup_time);
488 sc->sc_hup_time.tv_sec++;
489 mutex_exit(&sc->sc_lock);
490 }
491 }
492
493 /*
494 * ucomopen(dev, flag, mode, l)
495 *
496 * Called when anyone tries to open /dev/ttyU? for an existing
497 * ucom? instance that has completed attach. The attach may have
498 * failed, though, or there may be concurrent detach or close in
499 * progress, so fail if attach failed (no sc_tty) or detach has
500 * begun (sc_dying), or wait if there's a concurrent close in
501 * progress before reopening.
502 */
503 int
504 ucomopen(dev_t dev, int flag, int mode, struct lwp *l)
505 {
506 const int unit = UCOMUNIT(dev);
507 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
508 int error = 0;
509
510 UCOMHIST_FUNC(); UCOMHIST_CALLED();
511
512 mutex_enter(&sc->sc_lock);
513 if (sc->sc_dying) {
514 DPRINTF("... dying", 0, 0, 0, 0);
515 mutex_exit(&sc->sc_lock);
516 return ENXIO;
517 }
518
519 if (!device_is_active(sc->sc_dev)) {
520 mutex_exit(&sc->sc_lock);
521 return ENXIO;
522 }
523
524 struct tty *tp = sc->sc_tty;
525 if (tp == NULL) {
526 DPRINTF("... not attached", 0, 0, 0, 0);
527 mutex_exit(&sc->sc_lock);
528 return ENXIO;
529 }
530
531 DPRINTF("unit=%jd, tp=%#jx", unit, (uintptr_t)tp, 0, 0);
532
533 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
534 mutex_exit(&sc->sc_lock);
535 return EBUSY;
536 }
537
538 /*
539 * If the previous use had set DTR on close, wait up to one
540 * second for the other side to notice we hung up. After
541 * sleeping, the tty may have been revoked, so restart the
542 * whole operation.
543 *
544 * XXX The wchan is not ttclose but maybe should be.
545 */
546 if (timerisset(&sc->sc_hup_time)) {
547 struct timeval now, delta;
548 int ms, ticks;
549
550 microuptime(&now);
551 if (timercmp(&now, &sc->sc_hup_time, <)) {
552 timersub(&sc->sc_hup_time, &now, &delta);
553 ms = MIN(INT_MAX - 1000, delta.tv_sec*1000);
554 ms += howmany(delta.tv_usec, 1000);
555 ticks = MAX(1, MIN(INT_MAX, mstohz(ms)));
556 (void)cv_timedwait(&sc->sc_statecv, &sc->sc_lock,
557 ticks);
558 mutex_exit(&sc->sc_lock);
559 return ERESTART;
560 }
561 timerclear(&sc->sc_hup_time);
562 }
563
564 /*
565 * Wait while the device is initialized by the
566 * first opener or cleaned up by the last closer.
567 */
568 enum ucom_state state = sc->sc_state;
569 if (state == UCOM_OPENING || sc->sc_closing) {
570 if (flag & FNONBLOCK)
571 error = EWOULDBLOCK;
572 else
573 error = cv_wait_sig(&sc->sc_statecv, &sc->sc_lock);
574 mutex_exit(&sc->sc_lock);
575 return error ? error : ERESTART;
576 }
577 KASSERTMSG(state == UCOM_OPEN || state == UCOM_ATTACHED,
578 "state is %d", state);
579
580 /*
581 * If this is the first open, then make sure the pipes are
582 * running and perform any initialization needed.
583 */
584 bool firstopen = (state == UCOM_ATTACHED);
585 if (firstopen) {
586 KASSERT(!ISSET(tp->t_state, TS_ISOPEN));
587 KASSERT(tp->t_wopen == 0);
588
589 tp->t_dev = dev;
590 sc->sc_state = UCOM_OPENING;
591 mutex_exit(&sc->sc_lock);
592
593 if (sc->sc_methods->ucom_open != NULL) {
594 error = sc->sc_methods->ucom_open(sc->sc_parent,
595 sc->sc_portno);
596 if (error)
597 goto bad;
598 }
599
600 ucom_status_change(sc);
601
602 /* Clear PPS capture state on first open. */
603 mutex_spin_enter(&timecounter_lock);
604 memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
605 sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
606 pps_init(&sc->sc_pps_state);
607 mutex_spin_exit(&timecounter_lock);
608
609 /*
610 * Initialize the termios status to the defaults. Add in the
611 * sticky bits from TIOCSFLAGS.
612 */
613 struct termios t;
614
615 t.c_ispeed = 0;
616 t.c_ospeed = TTYDEF_SPEED;
617 t.c_cflag = TTYDEF_CFLAG;
618 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
619 SET(t.c_cflag, CLOCAL);
620 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
621 SET(t.c_cflag, CRTSCTS);
622 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
623 SET(t.c_cflag, MDMBUF);
624 /* Make sure ucomparam() will do something. */
625 tp->t_ospeed = 0;
626 (void) ucomparam(tp, &t);
627 tp->t_iflag = TTYDEF_IFLAG;
628 tp->t_oflag = TTYDEF_OFLAG;
629 tp->t_lflag = TTYDEF_LFLAG;
630 ttychars(tp);
631 ttsetwater(tp);
632
633 /*
634 * Turn on DTR. We must always do this, even if carrier is not
635 * present, because otherwise we'd have to use TIOCSDTR
636 * immediately after setting CLOCAL, which applications do not
637 * expect. We always assert DTR while the device is open
638 * unless explicitly requested to deassert it. Ditto RTS.
639 */
640 ucom_dtr(sc, 1);
641 ucom_rts(sc, 1);
642
643 mutex_enter(&sc->sc_lock);
644 sc->sc_rx_unblock = 0;
645 sc->sc_rx_stopped = 0;
646 sc->sc_tx_stopped = 0;
647
648 /*
649 * Either after ucom was attached, or after ucomclose(), this
650 * list must be empty, otherwise ucomsubmitread() will corrupt
651 * it by queueing an ucom_buffer that is already queued.
652 */
653 KASSERT(SIMPLEQ_EMPTY(&sc->sc_ibuff_empty));
654
655 for (size_t i = 0; i < UCOM_IN_BUFFS; i++) {
656 struct ucom_buffer *ub = &sc->sc_ibuff[i];
657 error = ucomsubmitread(sc, ub);
658 if (error) {
659 mutex_exit(&sc->sc_lock);
660 goto bad;
661 }
662 }
663 }
664 mutex_exit(&sc->sc_lock);
665
666 DPRINTF("unit=%jd, tp=%#jx dialout %jd nonblock %jd", unit,
667 (uintptr_t)tp, !!UCOMDIALOUT(dev), !!ISSET(flag, O_NONBLOCK));
668 error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
669 if (error)
670 goto bad;
671
672 error = (*tp->t_linesw->l_open)(dev, tp);
673 if (error)
674 goto bad;
675
676 /*
677 * Success! If this was the first open, notify waiters that
678 * the tty is open for business.
679 */
680 if (firstopen) {
681 mutex_enter(&sc->sc_lock);
682 KASSERT(sc->sc_state == UCOM_OPENING);
683 sc->sc_state = UCOM_OPEN;
684 cv_broadcast(&sc->sc_statecv);
685 mutex_exit(&sc->sc_lock);
686 }
687 return 0;
688
689 bad:
690 /*
691 * Failure! If this was the first open, hang up, abort pipes,
692 * and notify waiters that we're not opening after all.
693 */
694 if (firstopen) {
695 ucom_cleanup(sc, flag);
696
697 mutex_enter(&sc->sc_lock);
698 KASSERT(sc->sc_state == UCOM_OPENING);
699 sc->sc_state = UCOM_ATTACHED;
700 cv_broadcast(&sc->sc_statecv);
701 mutex_exit(&sc->sc_lock);
702 }
703 return error;
704 }
705
706 /*
707 * ucomcancel(dev, flag, mode, l)
708 *
709 * Called on revoke or last close. Must interrupt any pending I/O
710 * operations and make them fail promptly; once they have all
711 * finished (except possibly new opens), ucomclose will be called.
712 * We set sc_closing to block new opens until ucomclose runs.
713 */
714 int
715 ucomcancel(dev_t dev, int flag, int mode, struct lwp *l)
716 {
717 const int unit = UCOMUNIT(dev);
718 struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
719
720 UCOMHIST_FUNC(); UCOMHIST_CALLED();
721
722 DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0);
723
724 /*
725 * This can run at any time before ucomclose on any device
726 * node, even if never attached or if attach failed, so we may
727 * not have a softc or a tty.
728 */
729 if (sc == NULL)
730 return 0;
731 struct tty *tp = sc->sc_tty;
732 if (tp == NULL)
733 return 0;
734
735 /*
736 * Mark the device closing so opens block until we're done
737 * closing. Wake them up so they start over at the top -- if
738 * we're closing because we're detaching, they need to wake up
739 * and notice it's time to fail.
740 */
741 mutex_enter(&sc->sc_lock);
742 sc->sc_closing = true;
743 cv_broadcast(&sc->sc_statecv);
744 mutex_exit(&sc->sc_lock);
745
746 /*
747 * Cancel any pending tty I/O operations, causing them to wake
748 * up and fail promptly, and preventing any new ones from
749 * starting to wait until we have finished closing.
750 */
751 ttycancel(tp);
752
753 return 0;
754 }
755
756 /*
757 * ucomclose(dev, flag, mode, l)
758 *
759 * Called after ucomcancel, when all prior operations on the /dev
760 * node have completed. Only new opens may be in progress at this
761 * point, but they will block until sc_closing is set to false.
762 */
763 int
764 ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
765 {
766 const int unit = UCOMUNIT(dev);
767 struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
768 int error = 0;
769
770 UCOMHIST_FUNC(); UCOMHIST_CALLED();
771
772 DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0);
773
774 /*
775 * This can run at any time after ucomcancel on any device
776 * node, even if never attached or if attach failed, so we may
777 * not have a softc or a tty.
778 */
779 if (sc == NULL)
780 return 0;
781 struct tty *tp = sc->sc_tty;
782 if (tp == NULL)
783 return 0;
784
785 /*
786 * Close the tty, causing anyone waiting for it to wake, and
787 * hang up the phone.
788 */
789 ucom_cleanup(sc, flag);
790
791 /*
792 * ttyclose should have cleared TS_ISOPEN and interrupted all
793 * pending opens, which should have completed by now.
794 */
795 ttylock(tp);
796 KASSERT(!ISSET(tp->t_state, TS_ISOPEN));
797 KASSERT(tp->t_wopen == 0);
798 ttyunlock(tp);
799
800 /*
801 * Close any device-specific state.
802 */
803 if (sc->sc_methods->ucom_close != NULL) {
804 sc->sc_methods->ucom_close(sc->sc_parent,
805 sc->sc_portno);
806 }
807
808 /*
809 * We're now closed. Can reopen after this point, so resume
810 * transfers, mark us no longer closing, and notify anyone
811 * waiting in open. The state may be OPEN or ATTACHED at this
812 * point -- OPEN if the device was already open when we closed
813 * it, ATTACHED if we interrupted it in the process of opening.
814 */
815 mutex_enter(&sc->sc_lock);
816 KASSERTMSG(sc->sc_state == UCOM_ATTACHED || sc->sc_state == UCOM_OPEN,
817 "%s sc=%p state=%d", device_xname(sc->sc_dev), sc, sc->sc_state);
818 KASSERT(sc->sc_closing);
819 sc->sc_state = UCOM_ATTACHED;
820 sc->sc_closing = false;
821 cv_broadcast(&sc->sc_statecv);
822 mutex_exit(&sc->sc_lock);
823
824 return error;
825 }
826
827 int
828 ucomread(dev_t dev, struct uio *uio, int flag)
829 {
830 const int unit = UCOMUNIT(dev);
831 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
832 struct tty *tp = sc->sc_tty;
833
834 UCOMHIST_FUNC(); UCOMHIST_CALLED();
835
836 return (*tp->t_linesw->l_read)(tp, uio, flag);
837 }
838
839 int
840 ucomwrite(dev_t dev, struct uio *uio, int flag)
841 {
842 const int unit = UCOMUNIT(dev);
843 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
844 struct tty *tp = sc->sc_tty;
845
846 UCOMHIST_FUNC(); UCOMHIST_CALLED();
847
848 return (*tp->t_linesw->l_write)(tp, uio, flag);
849 }
850
851 int
852 ucompoll(dev_t dev, int events, struct lwp *l)
853 {
854 const int unit = UCOMUNIT(dev);
855 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
856 struct tty *tp = sc->sc_tty;
857
858 UCOMHIST_FUNC(); UCOMHIST_CALLED();
859
860 return (*tp->t_linesw->l_poll)(tp, events, l);
861 }
862
863 struct tty *
864 ucomtty(dev_t dev)
865 {
866 const int unit = UCOMUNIT(dev);
867 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
868
869 return sc->sc_tty;
870 }
871
872 int
873 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
874 {
875 const int unit = UCOMUNIT(dev);
876 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
877 struct tty *tp = sc->sc_tty;
878 int error;
879
880 UCOMHIST_FUNC(); UCOMHIST_CALLED();
881
882 DPRINTF("cmd=0x%08jx", cmd, 0, 0, 0);
883
884 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
885 if (error != EPASSTHROUGH)
886 return error;
887
888 error = ttioctl(tp, cmd, data, flag, l);
889 if (error != EPASSTHROUGH)
890 return error;
891
892 if (sc->sc_methods->ucom_ioctl != NULL) {
893 error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
894 sc->sc_portno, cmd, data, flag, l->l_proc);
895 if (error != EPASSTHROUGH)
896 return error;
897 }
898
899 error = 0;
900
901 DPRINTF("our cmd=0x%08jx", cmd, 0, 0, 0);
902
903 switch (cmd) {
904 case TIOCSBRK:
905 ucom_break(sc, 1);
906 break;
907
908 case TIOCCBRK:
909 ucom_break(sc, 0);
910 break;
911
912 case TIOCSDTR:
913 ucom_dtr(sc, 1);
914 break;
915
916 case TIOCCDTR:
917 ucom_dtr(sc, 0);
918 break;
919
920 case TIOCGFLAGS:
921 mutex_enter(&sc->sc_lock);
922 *(int *)data = sc->sc_swflags;
923 mutex_exit(&sc->sc_lock);
924 break;
925
926 case TIOCSFLAGS:
927 error = kauth_authorize_device_tty(l->l_cred,
928 KAUTH_DEVICE_TTY_PRIVSET, tp);
929 if (error)
930 break;
931 mutex_enter(&sc->sc_lock);
932 sc->sc_swflags = *(int *)data;
933 mutex_exit(&sc->sc_lock);
934 break;
935
936 case TIOCMSET:
937 case TIOCMBIS:
938 case TIOCMBIC:
939 tiocm_to_ucom(sc, cmd, *(int *)data);
940 break;
941
942 case TIOCMGET:
943 *(int *)data = ucom_to_tiocm(sc);
944 break;
945
946 case PPS_IOC_CREATE:
947 case PPS_IOC_DESTROY:
948 case PPS_IOC_GETPARAMS:
949 case PPS_IOC_SETPARAMS:
950 case PPS_IOC_GETCAP:
951 case PPS_IOC_FETCH:
952 #ifdef PPS_SYNC
953 case PPS_IOC_KCBIND:
954 #endif
955 mutex_spin_enter(&timecounter_lock);
956 error = pps_ioctl(cmd, data, &sc->sc_pps_state);
957 mutex_spin_exit(&timecounter_lock);
958 break;
959
960 default:
961 error = EPASSTHROUGH;
962 break;
963 }
964
965 return error;
966 }
967
968 static void
969 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
970 {
971 u_char combits;
972
973 combits = 0;
974 if (ISSET(ttybits, TIOCM_DTR))
975 SET(combits, UMCR_DTR);
976 if (ISSET(ttybits, TIOCM_RTS))
977 SET(combits, UMCR_RTS);
978
979 mutex_enter(&sc->sc_lock);
980 switch (how) {
981 case TIOCMBIC:
982 CLR(sc->sc_mcr, combits);
983 break;
984
985 case TIOCMBIS:
986 SET(sc->sc_mcr, combits);
987 break;
988
989 case TIOCMSET:
990 CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
991 SET(sc->sc_mcr, combits);
992 break;
993 }
994 u_char mcr = sc->sc_mcr;
995 mutex_exit(&sc->sc_lock);
996
997 if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
998 ucom_dtr(sc, (mcr & UMCR_DTR) != 0);
999 if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
1000 ucom_rts(sc, (mcr & UMCR_RTS) != 0);
1001 }
1002
1003 static int
1004 ucom_to_tiocm(struct ucom_softc *sc)
1005 {
1006 u_char combits;
1007 int ttybits = 0;
1008
1009 mutex_enter(&sc->sc_lock);
1010 combits = sc->sc_mcr;
1011 if (ISSET(combits, UMCR_DTR))
1012 SET(ttybits, TIOCM_DTR);
1013 if (ISSET(combits, UMCR_RTS))
1014 SET(ttybits, TIOCM_RTS);
1015
1016 combits = sc->sc_msr;
1017 if (ISSET(combits, UMSR_DCD))
1018 SET(ttybits, TIOCM_CD);
1019 if (ISSET(combits, UMSR_CTS))
1020 SET(ttybits, TIOCM_CTS);
1021 if (ISSET(combits, UMSR_DSR))
1022 SET(ttybits, TIOCM_DSR);
1023 if (ISSET(combits, UMSR_RI | UMSR_TERI))
1024 SET(ttybits, TIOCM_RI);
1025
1026 #if 0
1027 XXX;
1028 if (sc->sc_ier != 0)
1029 SET(ttybits, TIOCM_LE);
1030 #endif
1031 mutex_exit(&sc->sc_lock);
1032
1033 return ttybits;
1034 }
1035
1036 static void
1037 ucom_break(struct ucom_softc *sc, int onoff)
1038 {
1039 UCOMHIST_FUNC(); UCOMHIST_CALLED();
1040
1041 DPRINTF("onoff=%jd", onoff, 0, 0, 0);
1042
1043 if (sc->sc_methods->ucom_set != NULL)
1044 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
1045 UCOM_SET_BREAK, onoff);
1046 }
1047
1048 static void
1049 ucom_dtr(struct ucom_softc *sc, int onoff)
1050 {
1051 UCOMHIST_FUNC(); UCOMHIST_CALLED();
1052
1053 DPRINTF("onoff=%jd", onoff, 0, 0, 0);
1054
1055 if (sc->sc_methods->ucom_set != NULL)
1056 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
1057 UCOM_SET_DTR, onoff);
1058 }
1059
1060 static void
1061 ucom_rts(struct ucom_softc *sc, int onoff)
1062 {
1063 UCOMHIST_FUNC(); UCOMHIST_CALLED();
1064
1065 DPRINTF("onoff=%jd", onoff, 0, 0, 0);
1066
1067 if (sc->sc_methods->ucom_set != NULL)
1068 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
1069 UCOM_SET_RTS, onoff);
1070 }
1071
1072 void
1073 ucom_status_change(struct ucom_softc *sc)
1074 {
1075 struct tty *tp = sc->sc_tty;
1076
1077 if (sc->sc_methods->ucom_get_status != NULL) {
1078 u_char msr, lsr;
1079
1080 sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
1081 &lsr, &msr);
1082 mutex_enter(&sc->sc_lock);
1083 u_char old_msr = sc->sc_msr;
1084
1085 sc->sc_lsr = lsr;
1086 sc->sc_msr = msr;
1087 mutex_exit(&sc->sc_lock);
1088
1089 if (ISSET((msr ^ old_msr), UMSR_DCD)) {
1090 mutex_spin_enter(&timecounter_lock);
1091 pps_capture(&sc->sc_pps_state);
1092 pps_event(&sc->sc_pps_state,
1093 (sc->sc_msr & UMSR_DCD) ?
1094 PPS_CAPTUREASSERT :
1095 PPS_CAPTURECLEAR);
1096 mutex_spin_exit(&timecounter_lock);
1097
1098 (*tp->t_linesw->l_modem)(tp, ISSET(msr, UMSR_DCD));
1099 }
1100 } else {
1101 mutex_enter(&sc->sc_lock);
1102 sc->sc_lsr = 0;
1103 /* Assume DCD is present, if we have no chance to check it. */
1104 sc->sc_msr = UMSR_DCD;
1105 mutex_exit(&sc->sc_lock);
1106 }
1107 }
1108
1109 static int
1110 ucomparam(struct tty *tp, struct termios *t)
1111 {
1112 const int unit = UCOMUNIT(tp->t_dev);
1113 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
1114 int error = 0;
1115
1116 /* XXX should take tty lock around touching tp */
1117
1118 UCOMHIST_FUNC(); UCOMHIST_CALLED();
1119
1120 /* Check requested parameters. */
1121 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
1122 error = EINVAL;
1123 goto out;
1124 }
1125
1126 /*
1127 * For the console, always force CLOCAL and !HUPCL, so that the port
1128 * is always active.
1129 */
1130 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
1131 SET(t->c_cflag, CLOCAL);
1132 CLR(t->c_cflag, HUPCL);
1133 }
1134
1135 /*
1136 * If there were no changes, don't do anything. This avoids dropping
1137 * input and improves performance when all we did was frob things like
1138 * VMIN and VTIME.
1139 */
1140 if (tp->t_ospeed == t->c_ospeed &&
1141 tp->t_cflag == t->c_cflag) {
1142 goto out;
1143 }
1144
1145 /* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
1146
1147 /* And copy to tty. */
1148 tp->t_ispeed = 0;
1149 tp->t_ospeed = t->c_ospeed;
1150 tp->t_cflag = t->c_cflag;
1151
1152 if (sc->sc_methods->ucom_param != NULL) {
1153 error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
1154 t);
1155 if (error)
1156 goto out;
1157 }
1158
1159 /* XXX worry about CHWFLOW */
1160
1161 /*
1162 * Update the tty layer's idea of the carrier bit, in case we changed
1163 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1164 * explicit request.
1165 */
1166 DPRINTF("l_modem", 0, 0, 0, 0);
1167 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
1168
1169 #if 0
1170 XXX what if the hardware is not open
1171 if (!ISSET(t->c_cflag, CHWFLOW)) {
1172 if (sc->sc_tx_stopped) {
1173 sc->sc_tx_stopped = 0;
1174 ucomstart(tp);
1175 }
1176 }
1177 #endif
1178 out:
1179 return error;
1180 }
1181
1182 static int
1183 ucomhwiflow(struct tty *tp, int block)
1184 {
1185 const int unit = UCOMUNIT(tp->t_dev);
1186 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
1187 int old;
1188
1189 UCOMHIST_FUNC(); UCOMHIST_CALLED();
1190
1191 KASSERT(&sc->sc_lock);
1192 KASSERT(ttylocked(tp));
1193
1194 old = sc->sc_rx_stopped;
1195 sc->sc_rx_stopped = (u_char)block;
1196
1197 if (old && !block) {
1198 sc->sc_rx_unblock = 1;
1199 kpreempt_disable();
1200 softint_schedule(sc->sc_si);
1201 kpreempt_enable();
1202 }
1203
1204 return 1;
1205 }
1206
1207 static void
1208 ucomstart(struct tty *tp)
1209 {
1210 const int unit = UCOMUNIT(tp->t_dev);
1211 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
1212 struct ucom_buffer *ub;
1213 u_char *data;
1214 int cnt;
1215
1216 UCOMHIST_FUNC(); UCOMHIST_CALLED();
1217
1218 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1219 goto out;
1220 if (sc->sc_tx_stopped)
1221 goto out;
1222
1223 if (!ttypull(tp))
1224 goto out;
1225
1226 /* Grab the first contiguous region of buffer space. */
1227 data = tp->t_outq.c_cf;
1228 cnt = ndqb(&tp->t_outq, 0);
1229
1230 if (cnt == 0)
1231 goto out;
1232
1233 ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
1234 if (ub == NULL) {
1235 SET(tp->t_state, TS_BUSY);
1236 goto out;
1237 }
1238
1239 SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
1240
1241 if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
1242 SET(tp->t_state, TS_BUSY);
1243
1244 if (cnt > sc->sc_obufsize)
1245 cnt = sc->sc_obufsize;
1246
1247 if (sc->sc_methods->ucom_write != NULL)
1248 sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
1249 ub->ub_data, data, &cnt);
1250 else
1251 memcpy(ub->ub_data, data, cnt);
1252
1253 ub->ub_len = cnt;
1254 ub->ub_index = 0;
1255
1256 SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
1257
1258 kpreempt_disable();
1259 softint_schedule(sc->sc_si);
1260 kpreempt_enable();
1261
1262 out:
1263 DPRINTF("... done", 0, 0, 0, 0);
1264 return;
1265 }
1266
1267 void
1268 ucomstop(struct tty *tp, int flag)
1269 {
1270 #if 0
1271 const int unit = UCOMUNIT(tp->t_dev);
1272 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
1273
1274 mutex_enter(&sc->sc_lock);
1275 ttylock(tp);
1276 if (ISSET(tp->t_state, TS_BUSY)) {
1277 /* obuff_full -> obuff_free? */
1278 /* sc->sc_tx_stopped = 1; */
1279 if (!ISSET(tp->t_state, TS_TTSTOP))
1280 SET(tp->t_state, TS_FLUSH);
1281 }
1282 ttyunlock(tp);
1283 mutex_exit(&sc->sc_lock);
1284 #endif
1285 }
1286
1287 static void
1288 ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
1289 usbd_status err)
1290 {
1291 struct tty *tp = sc->sc_tty;
1292 uint32_t cc = ub->ub_len;
1293
1294 KASSERT(mutex_owned(&sc->sc_lock));
1295
1296 switch (err) {
1297 case USBD_IN_PROGRESS:
1298 ub->ub_index = ub->ub_len;
1299 break;
1300 case USBD_STALLED:
1301 ub->ub_index = 0;
1302 kpreempt_disable();
1303 softint_schedule(sc->sc_si);
1304 kpreempt_enable();
1305 break;
1306 case USBD_NORMAL_COMPLETION:
1307 usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
1308 rnd_add_uint32(&sc->sc_rndsource, cc);
1309 /*FALLTHROUGH*/
1310 default:
1311 SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
1312 SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
1313 cc -= sc->sc_opkthdrlen;
1314
1315 ttylock(tp);
1316 CLR(tp->t_state, TS_BUSY);
1317 if (ISSET(tp->t_state, TS_FLUSH))
1318 CLR(tp->t_state, TS_FLUSH);
1319 else
1320 ndflush(&tp->t_outq, cc);
1321 ttyunlock(tp);
1322
1323 if (err != USBD_CANCELLED && err != USBD_IOERROR &&
1324 !sc->sc_closing) {
1325 if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
1326 ucom_submit_write(sc, ub);
1327
1328 ttylock(tp);
1329 (*tp->t_linesw->l_start)(tp);
1330 ttyunlock(tp);
1331 }
1332 break;
1333 }
1334 }
1335
1336 static void
1337 ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
1338 {
1339
1340 KASSERT(mutex_owned(&sc->sc_lock));
1341
1342 usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, ub->ub_len,
1343 0, USBD_NO_TIMEOUT, ucomwritecb);
1344
1345 ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
1346 }
1347
1348 static void
1349 ucomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status)
1350 {
1351 struct ucom_softc *sc = (struct ucom_softc *)p;
1352
1353 mutex_enter(&sc->sc_lock);
1354 ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
1355 mutex_exit(&sc->sc_lock);
1356
1357 }
1358
1359 static void
1360 ucom_softintr(void *arg)
1361 {
1362 UCOMHIST_FUNC(); UCOMHIST_CALLED();
1363
1364 struct ucom_softc *sc = arg;
1365 struct tty *tp = sc->sc_tty;
1366
1367 mutex_enter(&sc->sc_lock);
1368 ttylock(tp);
1369 if (!ISSET(tp->t_state, TS_ISOPEN)) {
1370 ttyunlock(tp);
1371 mutex_exit(&sc->sc_lock);
1372 return;
1373 }
1374 ttyunlock(tp);
1375
1376 struct ucom_buffer *ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
1377
1378 if (ub != NULL && ub->ub_index == 0)
1379 ucom_submit_write(sc, ub);
1380
1381 if (sc->sc_rx_unblock)
1382 ucom_read_complete(sc);
1383
1384 mutex_exit(&sc->sc_lock);
1385 }
1386
1387 static void
1388 ucom_read_complete(struct ucom_softc *sc)
1389 {
1390 int (*rint)(int, struct tty *);
1391 struct ucom_buffer *ub;
1392 struct tty *tp;
1393
1394 KASSERT(mutex_owned(&sc->sc_lock));
1395
1396 tp = sc->sc_tty;
1397 rint = tp->t_linesw->l_rint;
1398 ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
1399
1400 while (ub != NULL && !sc->sc_rx_stopped) {
1401
1402 /* XXX ttyinput takes ttylock */
1403 while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
1404 /* Give characters to tty layer. */
1405 if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
1406 /* Overflow: drop remainder */
1407 ub->ub_index = ub->ub_len;
1408 } else
1409 ub->ub_index++;
1410 }
1411
1412 if (ub->ub_index == ub->ub_len) {
1413 SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
1414 ucomsubmitread(sc, ub);
1415 ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
1416 }
1417 }
1418
1419 sc->sc_rx_unblock = (ub != NULL);
1420 }
1421
1422 static int
1423 ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
1424 {
1425 usbd_status err;
1426
1427 KASSERT(mutex_owned(&sc->sc_lock));
1428
1429 usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, sc->sc_ibufsize,
1430 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ucomreadcb);
1431
1432 if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
1433 /* XXX: Recover from this, please! */
1434 device_printf(sc->sc_dev, "%s: err=%s\n",
1435 __func__, usbd_errstr(err));
1436 return EIO;
1437 }
1438
1439 SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
1440
1441 return 0;
1442 }
1443
1444 static void
1445 ucomreadcb(struct usbd_xfer *xfer, void *p, usbd_status status)
1446 {
1447 struct ucom_softc *sc = (struct ucom_softc *)p;
1448 struct ucom_buffer *ub;
1449 uint32_t cc;
1450 u_char *cp;
1451
1452 UCOMHIST_FUNC(); UCOMHIST_CALLED();
1453
1454 mutex_enter(&sc->sc_lock);
1455
1456 /*
1457 * Always remove the ucom_buffer from the sc_ibuff_empty list
1458 * regardless of transfer status. Leaving it on the list on
1459 * USBD_CANCELLED or USBD_IOERROR would cause corruption
1460 * next time the device is opened.
1461 *
1462 * USBD_CANCELLED is a reported when the device is closed
1463 * via ucomclose() -> usbd_abort_pipe() -> /usbd_xfer_abort()
1464 */
1465 ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
1466 SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
1467
1468 if (status == USBD_CANCELLED || status == USBD_IOERROR ||
1469 sc->sc_closing) {
1470 DPRINTF("... done (status %jd closing %jd)",
1471 status, sc->sc_closing, 0, 0);
1472 mutex_exit(&sc->sc_lock);
1473 return;
1474 }
1475
1476 if (status != USBD_NORMAL_COMPLETION) {
1477 if (status == USBD_STALLED) {
1478 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1479 } else {
1480 device_printf(sc->sc_dev,
1481 "ucomreadcb: wonky status=%s\n",
1482 usbd_errstr(status));
1483 }
1484
1485 DPRINTF("... done (status %jd)", status, 0, 0, 0);
1486 /* re-adds ub to sc_ibuff_empty */
1487 ucomsubmitread(sc, ub);
1488 mutex_exit(&sc->sc_lock);
1489 return;
1490 }
1491
1492 usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
1493
1494 #ifdef UCOM_DEBUG
1495 /* This is triggered by uslsa(4) occasionally. */
1496 if ((ucomdebug > 0) && (cc == 0)) {
1497 device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n");
1498 }
1499 #endif
1500 KDASSERT(cp == ub->ub_data);
1501
1502 rnd_add_uint32(&sc->sc_rndsource, cc);
1503
1504 if (sc->sc_state != UCOM_OPEN) {
1505 /* Go around again - we're not quite ready */
1506 /* re-adds ub to sc_ibuff_empty */
1507 ucomsubmitread(sc, ub);
1508 mutex_exit(&sc->sc_lock);
1509 DPRINTF("... done (not open)", 0, 0, 0, 0);
1510 return;
1511 }
1512
1513 mutex_exit(&sc->sc_lock);
1514 if (sc->sc_methods->ucom_read != NULL) {
1515 sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
1516 &cp, &cc);
1517 ub->ub_index = (u_int)(cp - ub->ub_data);
1518 } else
1519 ub->ub_index = 0;
1520
1521 ub->ub_len = cc;
1522
1523 mutex_enter(&sc->sc_lock);
1524 SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
1525 ucom_read_complete(sc);
1526 mutex_exit(&sc->sc_lock);
1527
1528 DPRINTF("... done", 0, 0, 0, 0);
1529 }
1530
1531 static void
1532 ucom_cleanup(struct ucom_softc *sc, int flag)
1533 {
1534 struct tty *tp = sc->sc_tty;
1535
1536 UCOMHIST_FUNC(); UCOMHIST_CALLED();
1537
1538 DPRINTF("closing pipes", 0, 0, 0, 0);
1539
1540 /*
1541 * Close the tty and interrupt any pending opens waiting for
1542 * carrier so they restart or give up. This may flush data.
1543 */
1544 (*tp->t_linesw->l_close)(tp, flag);
1545 ttyclose(tp);
1546
1547 /*
1548 * Interrupt any pending xfers and cause them to fail promptly.
1549 * New xfers will only be submitted under the lock after
1550 * sc_closing is cleared.
1551 */
1552 usbd_abort_pipe(sc->sc_bulkin_pipe);
1553 usbd_abort_pipe(sc->sc_bulkout_pipe);
1554
1555 /*
1556 * Hang up the phone and start the timer before we can make a
1557 * call again, if necessary.
1558 */
1559 ucom_shutdown(sc);
1560 }
1561
1562 #endif /* NUCOM > 0 */
1563
1564 int
1565 ucomprint(void *aux, const char *pnp)
1566 {
1567 struct ucom_attach_args *ucaa = aux;
1568
1569 if (pnp)
1570 aprint_normal("ucom at %s", pnp);
1571 if (ucaa->ucaa_portno != UCOM_UNK_PORTNO)
1572 aprint_normal(" portno %d", ucaa->ucaa_portno);
1573 return UNCONF;
1574 }
1575
1576 int
1577 ucomsubmatch(device_t parent, cfdata_t cf,
1578 const int *ldesc, void *aux)
1579 {
1580 struct ucom_attach_args *ucaa = aux;
1581
1582 if (ucaa->ucaa_portno != UCOM_UNK_PORTNO &&
1583 cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
1584 cf->cf_loc[UCOMBUSCF_PORTNO] != ucaa->ucaa_portno)
1585 return 0;
1586 return config_match(parent, cf, aux);
1587 }
1588