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