ucom.c revision 1.73 1 /* $NetBSD: ucom.c,v 1.73 2007/11/19 18:51:50 ad 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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39 /*
40 * This code is very heavily based on the 16550 driver, com.c.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ucom.c,v 1.73 2007/11/19 18:51:50 ad Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/ioctl.h>
50 #include <sys/conf.h>
51 #include <sys/tty.h>
52 #include <sys/file.h>
53 #include <sys/select.h>
54 #include <sys/proc.h>
55 #include <sys/vnode.h>
56 #include <sys/device.h>
57 #include <sys/poll.h>
58 #include <sys/kauth.h>
59 #if defined(__NetBSD__)
60 #include "rnd.h"
61 #if NRND > 0
62 #include <sys/rnd.h>
63 #endif
64 #endif
65
66 #include <dev/usb/usb.h>
67
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdi_util.h>
70 #include <dev/usb/usbdevs.h>
71 #include <dev/usb/usb_quirks.h>
72
73 #include <dev/usb/ucomvar.h>
74
75 #include "ucom.h"
76
77 #include "locators.h"
78
79 #if NUCOM > 0
80
81 #ifdef UCOM_DEBUG
82 #define DPRINTFN(n, x) if (ucomdebug > (n)) logprintf x
83 int ucomdebug = 0;
84 #else
85 #define DPRINTFN(n, x)
86 #endif
87 #define DPRINTF(x) DPRINTFN(0, x)
88
89 #define UCOMUNIT_MASK 0x3ffff
90 #define UCOMDIALOUT_MASK 0x80000
91 #define UCOMCALLUNIT_MASK 0x40000
92
93 #define UCOMUNIT(x) (minor(x) & UCOMUNIT_MASK)
94 #define UCOMDIALOUT(x) (minor(x) & UCOMDIALOUT_MASK)
95 #define UCOMCALLUNIT(x) (minor(x) & UCOMCALLUNIT_MASK)
96
97 struct ucom_softc {
98 USBBASEDEVICE sc_dev; /* base device */
99
100 usbd_device_handle sc_udev; /* USB device */
101
102 usbd_interface_handle sc_iface; /* data interface */
103
104 int sc_bulkin_no; /* bulk in endpoint address */
105 usbd_pipe_handle sc_bulkin_pipe; /* bulk in pipe */
106 usbd_xfer_handle sc_ixfer; /* read request */
107 u_char *sc_ibuf; /* read buffer */
108 u_int sc_ibufsize; /* read buffer size */
109 u_int sc_ibufsizepad; /* read buffer size padded */
110
111 int sc_bulkout_no; /* bulk out endpoint address */
112 usbd_pipe_handle sc_bulkout_pipe;/* bulk out pipe */
113 usbd_xfer_handle sc_oxfer; /* write request */
114 u_char *sc_obuf; /* write buffer */
115 u_int sc_obufsize; /* write buffer size */
116 u_int sc_opkthdrlen; /* header length of
117 * output packet */
118
119 struct ucom_methods *sc_methods;
120 void *sc_parent;
121 int sc_portno;
122
123 struct tty *sc_tty; /* our tty */
124 u_char sc_lsr;
125 u_char sc_msr;
126 u_char sc_mcr;
127 u_char sc_tx_stopped;
128 int sc_swflags;
129
130 u_char sc_opening; /* lock during open */
131 int sc_refcnt;
132 u_char sc_dying; /* disconnecting */
133
134 #if defined(__NetBSD__) && NRND > 0
135 rndsource_element_t sc_rndsource; /* random source */
136 #endif
137 };
138
139 dev_type_open(ucomopen);
140 dev_type_close(ucomclose);
141 dev_type_read(ucomread);
142 dev_type_write(ucomwrite);
143 dev_type_ioctl(ucomioctl);
144 dev_type_stop(ucomstop);
145 dev_type_tty(ucomtty);
146 dev_type_poll(ucompoll);
147
148 const struct cdevsw ucom_cdevsw = {
149 ucomopen, ucomclose, ucomread, ucomwrite, ucomioctl,
150 ucomstop, ucomtty, ucompoll, nommap, ttykqfilter, D_TTY
151 };
152
153 Static void ucom_cleanup(struct ucom_softc *);
154 Static void ucom_hwiflow(struct ucom_softc *);
155 Static int ucomparam(struct tty *, struct termios *);
156 Static void ucomstart(struct tty *);
157 Static void ucom_shutdown(struct ucom_softc *);
158 Static int ucom_do_ioctl(struct ucom_softc *, u_long, void *,
159 int, struct lwp *);
160 Static void ucom_dtr(struct ucom_softc *, int);
161 Static void ucom_rts(struct ucom_softc *, int);
162 Static void ucom_break(struct ucom_softc *, int);
163 Static usbd_status ucomstartread(struct ucom_softc *);
164 Static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
165 Static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
166 Static void tiocm_to_ucom(struct ucom_softc *, u_long, int);
167 Static int ucom_to_tiocm(struct ucom_softc *);
168
169 USB_DECLARE_DRIVER(ucom);
170
171 USB_MATCH(ucom)
172 {
173 return (1);
174 }
175
176 USB_ATTACH(ucom)
177 {
178 struct ucom_softc *sc = (struct ucom_softc *)self;
179 struct ucom_attach_args *uca = aux;
180 struct tty *tp;
181
182 if (uca->info != NULL)
183 printf(": %s", uca->info);
184 printf("\n");
185
186 sc->sc_udev = uca->device;
187 sc->sc_iface = uca->iface;
188 sc->sc_bulkout_no = uca->bulkout;
189 sc->sc_bulkin_no = uca->bulkin;
190 sc->sc_ibufsize = uca->ibufsize;
191 sc->sc_ibufsizepad = uca->ibufsizepad;
192 sc->sc_obufsize = uca->obufsize;
193 sc->sc_opkthdrlen = uca->opkthdrlen;
194 sc->sc_methods = uca->methods;
195 sc->sc_parent = uca->arg;
196 sc->sc_portno = uca->portno;
197
198 tp = ttymalloc();
199 tp->t_oproc = ucomstart;
200 tp->t_param = ucomparam;
201 sc->sc_tty = tp;
202
203 DPRINTF(("ucom_attach: tty_attach %p\n", tp));
204 tty_attach(tp);
205
206 #if defined(__NetBSD__) && NRND > 0
207 rnd_attach_source(&sc->sc_rndsource, USBDEVNAME(sc->sc_dev),
208 RND_TYPE_TTY, 0);
209 #endif
210
211 USB_ATTACH_SUCCESS_RETURN;
212 }
213
214 USB_DETACH(ucom)
215 {
216 struct ucom_softc *sc = (struct ucom_softc *)self;
217 struct tty *tp = sc->sc_tty;
218 int maj, mn;
219 int s;
220
221 DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p, pipe=%d,%d\n",
222 sc, flags, tp, sc->sc_bulkin_no, sc->sc_bulkout_no));
223
224 sc->sc_dying = 1;
225
226 if (sc->sc_bulkin_pipe != NULL)
227 usbd_abort_pipe(sc->sc_bulkin_pipe);
228 if (sc->sc_bulkout_pipe != NULL)
229 usbd_abort_pipe(sc->sc_bulkout_pipe);
230
231 s = splusb();
232 if (--sc->sc_refcnt >= 0) {
233 /* Wake up anyone waiting */
234 if (tp != NULL) {
235 mutex_spin_enter(&tty_lock);
236 CLR(tp->t_state, TS_CARR_ON);
237 CLR(tp->t_cflag, CLOCAL | MDMBUF);
238 ttyflush(tp, FREAD|FWRITE);
239 mutex_spin_exit(&tty_lock);
240 }
241 /* Wait for processes to go away. */
242 usb_detach_wait(USBDEV(sc->sc_dev));
243 }
244 splx(s);
245
246 /* locate the major number */
247 maj = cdevsw_lookup_major(&ucom_cdevsw);
248
249 /* Nuke the vnodes for any open instances. */
250 mn = device_unit(self);
251 DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn));
252 vdevgone(maj, mn, mn, VCHR);
253 vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
254 vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
255
256 /* Detach and free the tty. */
257 if (tp != NULL) {
258 tty_detach(tp);
259 ttyfree(tp);
260 sc->sc_tty = NULL;
261 }
262
263 /* Detach the random source */
264 #if defined(__NetBSD__) && NRND > 0
265 rnd_detach_source(&sc->sc_rndsource);
266 #endif
267
268 return (0);
269 }
270
271 int
272 ucom_activate(device_ptr_t self, enum devact act)
273 {
274 struct ucom_softc *sc = (struct ucom_softc *)self;
275
276 DPRINTFN(5,("ucom_activate: %d\n", act));
277
278 switch (act) {
279 case DVACT_ACTIVATE:
280 return (EOPNOTSUPP);
281
282 case DVACT_DEACTIVATE:
283 sc->sc_dying = 1;
284 break;
285 }
286 return (0);
287 }
288
289 void
290 ucom_shutdown(struct ucom_softc *sc)
291 {
292 struct tty *tp = sc->sc_tty;
293
294 DPRINTF(("ucom_shutdown\n"));
295 /*
296 * Hang up if necessary. Wait a bit, so the other side has time to
297 * notice even if we immediately open the port again.
298 */
299 if (ISSET(tp->t_cflag, HUPCL)) {
300 ucom_dtr(sc, 0);
301 (void)tsleep(sc, TTIPRI, ttclos, hz);
302 }
303 }
304
305 int
306 ucomopen(dev_t dev, int flag, int mode, struct lwp *l)
307 {
308 int unit = UCOMUNIT(dev);
309 usbd_status err;
310 struct ucom_softc *sc;
311 struct tty *tp;
312 int s;
313 int error;
314
315 if (unit >= ucom_cd.cd_ndevs)
316 return (ENXIO);
317 sc = ucom_cd.cd_devs[unit];
318 if (sc == NULL)
319 return (ENXIO);
320
321 if (sc->sc_dying)
322 return (EIO);
323
324 if (!device_is_active(&sc->sc_dev))
325 return (ENXIO);
326
327 tp = sc->sc_tty;
328
329 DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
330
331 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
332 return (EBUSY);
333
334 s = spltty();
335
336 /*
337 * Do the following iff this is a first open.
338 */
339 while (sc->sc_opening)
340 tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
341
342 if (sc->sc_dying) {
343 splx(s);
344 return (EIO);
345 }
346 sc->sc_opening = 1;
347
348 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
349 struct termios t;
350
351 tp->t_dev = dev;
352
353 if (sc->sc_methods->ucom_open != NULL) {
354 error = sc->sc_methods->ucom_open(sc->sc_parent,
355 sc->sc_portno);
356 if (error) {
357 ucom_cleanup(sc);
358 sc->sc_opening = 0;
359 wakeup(&sc->sc_opening);
360 splx(s);
361 return (error);
362 }
363 }
364
365 ucom_status_change(sc);
366
367 /*
368 * Initialize the termios status to the defaults. Add in the
369 * sticky bits from TIOCSFLAGS.
370 */
371 t.c_ispeed = 0;
372 t.c_ospeed = TTYDEF_SPEED;
373 t.c_cflag = TTYDEF_CFLAG;
374 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
375 SET(t.c_cflag, CLOCAL);
376 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
377 SET(t.c_cflag, CRTSCTS);
378 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
379 SET(t.c_cflag, MDMBUF);
380 /* Make sure ucomparam() will do something. */
381 tp->t_ospeed = 0;
382 (void) ucomparam(tp, &t);
383 tp->t_iflag = TTYDEF_IFLAG;
384 tp->t_oflag = TTYDEF_OFLAG;
385 tp->t_lflag = TTYDEF_LFLAG;
386 ttychars(tp);
387 ttsetwater(tp);
388
389 /*
390 * Turn on DTR. We must always do this, even if carrier is not
391 * present, because otherwise we'd have to use TIOCSDTR
392 * immediately after setting CLOCAL, which applications do not
393 * expect. We always assert DTR while the device is open
394 * unless explicitly requested to deassert it. Ditto RTS.
395 */
396 ucom_dtr(sc, 1);
397 ucom_rts(sc, 1);
398
399 /* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
400 ucom_hwiflow(sc);
401
402 DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
403 sc->sc_bulkin_no, sc->sc_bulkout_no));
404
405 /* Open the bulk pipes */
406 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
407 &sc->sc_bulkin_pipe);
408 if (err) {
409 DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
410 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
411 usbd_errstr(err)));
412 error = EIO;
413 goto fail_0;
414 }
415 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
416 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
417 if (err) {
418 DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
419 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
420 usbd_errstr(err)));
421 error = EIO;
422 goto fail_1;
423 }
424
425 /* Allocate a request and an input buffer and start reading. */
426 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
427 if (sc->sc_ixfer == NULL) {
428 error = ENOMEM;
429 goto fail_2;
430 }
431
432 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
433 sc->sc_ibufsizepad);
434 if (sc->sc_ibuf == NULL) {
435 error = ENOMEM;
436 goto fail_3;
437 }
438
439 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
440 if (sc->sc_oxfer == NULL) {
441 error = ENOMEM;
442 goto fail_3;
443 }
444
445 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
446 sc->sc_obufsize +
447 sc->sc_opkthdrlen);
448 if (sc->sc_obuf == NULL) {
449 error = ENOMEM;
450 goto fail_4;
451 }
452
453 ucomstartread(sc);
454 }
455 sc->sc_opening = 0;
456 wakeup(&sc->sc_opening);
457 splx(s);
458
459 error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
460 if (error)
461 goto bad;
462
463 error = (*tp->t_linesw->l_open)(dev, tp);
464 if (error)
465 goto bad;
466
467 return (0);
468
469 fail_4:
470 usbd_free_xfer(sc->sc_oxfer);
471 sc->sc_oxfer = NULL;
472 fail_3:
473 usbd_free_xfer(sc->sc_ixfer);
474 sc->sc_ixfer = NULL;
475 fail_2:
476 usbd_close_pipe(sc->sc_bulkout_pipe);
477 sc->sc_bulkout_pipe = NULL;
478 fail_1:
479 usbd_close_pipe(sc->sc_bulkin_pipe);
480 sc->sc_bulkin_pipe = NULL;
481 fail_0:
482 sc->sc_opening = 0;
483 wakeup(&sc->sc_opening);
484 splx(s);
485 return (error);
486
487 bad:
488 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
489 /*
490 * We failed to open the device, and nobody else had it opened.
491 * Clean up the state as appropriate.
492 */
493 ucom_cleanup(sc);
494 }
495
496 return (error);
497 }
498
499 int
500 ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
501 {
502 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
503 struct tty *tp = sc->sc_tty;
504
505 DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
506 if (!ISSET(tp->t_state, TS_ISOPEN))
507 return (0);
508
509 sc->sc_refcnt++;
510
511 (*tp->t_linesw->l_close)(tp, flag);
512 ttyclose(tp);
513
514 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
515 /*
516 * Although we got a last close, the device may still be in
517 * use; e.g. if this was the dialout node, and there are still
518 * processes waiting for carrier on the non-dialout node.
519 */
520 ucom_cleanup(sc);
521 }
522
523 if (sc->sc_methods->ucom_close != NULL)
524 sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
525
526 if (--sc->sc_refcnt < 0)
527 usb_detach_wakeup(USBDEV(sc->sc_dev));
528
529 return (0);
530 }
531
532 int
533 ucomread(dev_t dev, struct uio *uio, int flag)
534 {
535 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
536 struct tty *tp = sc->sc_tty;
537 int error;
538
539 if (sc->sc_dying)
540 return (EIO);
541
542 sc->sc_refcnt++;
543 error = ((*tp->t_linesw->l_read)(tp, uio, flag));
544 if (--sc->sc_refcnt < 0)
545 usb_detach_wakeup(USBDEV(sc->sc_dev));
546 return (error);
547 }
548
549 int
550 ucomwrite(dev_t dev, struct uio *uio, int flag)
551 {
552 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
553 struct tty *tp = sc->sc_tty;
554 int error;
555
556 if (sc->sc_dying)
557 return (EIO);
558
559 sc->sc_refcnt++;
560 error = ((*tp->t_linesw->l_write)(tp, uio, flag));
561 if (--sc->sc_refcnt < 0)
562 usb_detach_wakeup(USBDEV(sc->sc_dev));
563 return (error);
564 }
565
566 int
567 ucompoll(dev_t dev, int events, struct lwp *l)
568 {
569 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
570 struct tty *tp = sc->sc_tty;
571 int revents;
572
573 if (sc->sc_dying)
574 return (POLLHUP);
575
576 sc->sc_refcnt++;
577 revents = ((*tp->t_linesw->l_poll)(tp, events, l));
578 if (--sc->sc_refcnt < 0)
579 usb_detach_wakeup(USBDEV(sc->sc_dev));
580 return (revents);
581 }
582
583 struct tty *
584 ucomtty(dev_t dev)
585 {
586 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
587 struct tty *tp = sc->sc_tty;
588
589 return (tp);
590 }
591
592 int
593 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
594 {
595 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)];
596 int error;
597
598 sc->sc_refcnt++;
599 error = ucom_do_ioctl(sc, cmd, data, flag, l);
600 if (--sc->sc_refcnt < 0)
601 usb_detach_wakeup(USBDEV(sc->sc_dev));
602 return (error);
603 }
604
605 Static int
606 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, void *data,
607 int flag, struct lwp *l)
608 {
609 struct tty *tp = sc->sc_tty;
610 int error;
611 int s;
612
613 if (sc->sc_dying)
614 return (EIO);
615
616 DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
617
618 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
619 if (error != EPASSTHROUGH)
620 return (error);
621
622 error = ttioctl(tp, cmd, data, flag, l);
623 if (error != EPASSTHROUGH)
624 return (error);
625
626 if (sc->sc_methods->ucom_ioctl != NULL) {
627 error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
628 sc->sc_portno, cmd, data, flag, l->l_proc);
629 if (error != EPASSTHROUGH)
630 return (error);
631 }
632
633 error = 0;
634
635 DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
636 s = spltty();
637
638 switch (cmd) {
639 case TIOCSBRK:
640 ucom_break(sc, 1);
641 break;
642
643 case TIOCCBRK:
644 ucom_break(sc, 0);
645 break;
646
647 case TIOCSDTR:
648 ucom_dtr(sc, 1);
649 break;
650
651 case TIOCCDTR:
652 ucom_dtr(sc, 0);
653 break;
654
655 case TIOCGFLAGS:
656 *(int *)data = sc->sc_swflags;
657 break;
658
659 case TIOCSFLAGS:
660 error = kauth_authorize_device_tty(l->l_cred,
661 KAUTH_DEVICE_TTY_PRIVSET, tp);
662 if (error)
663 break;
664 sc->sc_swflags = *(int *)data;
665 break;
666
667 case TIOCMSET:
668 case TIOCMBIS:
669 case TIOCMBIC:
670 tiocm_to_ucom(sc, cmd, *(int *)data);
671 break;
672
673 case TIOCMGET:
674 *(int *)data = ucom_to_tiocm(sc);
675 break;
676
677 default:
678 error = EPASSTHROUGH;
679 break;
680 }
681
682 splx(s);
683
684 return (error);
685 }
686
687 Static void
688 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
689 {
690 u_char combits;
691
692 combits = 0;
693 if (ISSET(ttybits, TIOCM_DTR))
694 SET(combits, UMCR_DTR);
695 if (ISSET(ttybits, TIOCM_RTS))
696 SET(combits, UMCR_RTS);
697
698 switch (how) {
699 case TIOCMBIC:
700 CLR(sc->sc_mcr, combits);
701 break;
702
703 case TIOCMBIS:
704 SET(sc->sc_mcr, combits);
705 break;
706
707 case TIOCMSET:
708 CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
709 SET(sc->sc_mcr, combits);
710 break;
711 }
712
713 if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
714 ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
715 if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
716 ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
717 }
718
719 Static int
720 ucom_to_tiocm(struct ucom_softc *sc)
721 {
722 u_char combits;
723 int ttybits = 0;
724
725 combits = sc->sc_mcr;
726 if (ISSET(combits, UMCR_DTR))
727 SET(ttybits, TIOCM_DTR);
728 if (ISSET(combits, UMCR_RTS))
729 SET(ttybits, TIOCM_RTS);
730
731 combits = sc->sc_msr;
732 if (ISSET(combits, UMSR_DCD))
733 SET(ttybits, TIOCM_CD);
734 if (ISSET(combits, UMSR_CTS))
735 SET(ttybits, TIOCM_CTS);
736 if (ISSET(combits, UMSR_DSR))
737 SET(ttybits, TIOCM_DSR);
738 if (ISSET(combits, UMSR_RI | UMSR_TERI))
739 SET(ttybits, TIOCM_RI);
740
741 #if 0
742 XXX;
743 if (sc->sc_ier != 0)
744 SET(ttybits, TIOCM_LE);
745 #endif
746
747 return (ttybits);
748 }
749
750 Static void
751 ucom_break(sc, onoff)
752 struct ucom_softc *sc;
753 int onoff;
754 {
755 DPRINTF(("ucom_break: onoff=%d\n", onoff));
756
757 if (sc->sc_methods->ucom_set != NULL)
758 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
759 UCOM_SET_BREAK, onoff);
760 }
761
762 Static void
763 ucom_dtr(struct ucom_softc *sc, int onoff)
764 {
765 DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
766
767 if (sc->sc_methods->ucom_set != NULL)
768 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
769 UCOM_SET_DTR, onoff);
770 }
771
772 Static void
773 ucom_rts(struct ucom_softc *sc, int onoff)
774 {
775 DPRINTF(("ucom_rts: onoff=%d\n", onoff));
776
777 if (sc->sc_methods->ucom_set != NULL)
778 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
779 UCOM_SET_RTS, onoff);
780 }
781
782 void
783 ucom_status_change(struct ucom_softc *sc)
784 {
785 struct tty *tp = sc->sc_tty;
786 u_char old_msr;
787
788 if (sc->sc_methods->ucom_get_status != NULL) {
789 old_msr = sc->sc_msr;
790 sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
791 &sc->sc_lsr, &sc->sc_msr);
792 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD))
793 (*tp->t_linesw->l_modem)(tp,
794 ISSET(sc->sc_msr, UMSR_DCD));
795 } else {
796 sc->sc_lsr = 0;
797 /* Assume DCD is present, if we have no chance to check it. */
798 sc->sc_msr = UMSR_DCD;
799 }
800 }
801
802 Static int
803 ucomparam(struct tty *tp, struct termios *t)
804 {
805 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
806 int error;
807
808 if (sc->sc_dying)
809 return (EIO);
810
811 /* Check requested parameters. */
812 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
813 return (EINVAL);
814
815 /*
816 * For the console, always force CLOCAL and !HUPCL, so that the port
817 * is always active.
818 */
819 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
820 SET(t->c_cflag, CLOCAL);
821 CLR(t->c_cflag, HUPCL);
822 }
823
824 /*
825 * If there were no changes, don't do anything. This avoids dropping
826 * input and improves performance when all we did was frob things like
827 * VMIN and VTIME.
828 */
829 if (tp->t_ospeed == t->c_ospeed &&
830 tp->t_cflag == t->c_cflag)
831 return (0);
832
833 /* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
834
835 /* And copy to tty. */
836 tp->t_ispeed = 0;
837 tp->t_ospeed = t->c_ospeed;
838 tp->t_cflag = t->c_cflag;
839
840 if (sc->sc_methods->ucom_param != NULL) {
841 error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
842 t);
843 if (error)
844 return (error);
845 }
846
847 /* XXX worry about CHWFLOW */
848
849 /*
850 * Update the tty layer's idea of the carrier bit, in case we changed
851 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
852 * explicit request.
853 */
854 DPRINTF(("ucomparam: l_modem\n"));
855 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
856
857 #if 0
858 XXX what if the hardware is not open
859 if (!ISSET(t->c_cflag, CHWFLOW)) {
860 if (sc->sc_tx_stopped) {
861 sc->sc_tx_stopped = 0;
862 ucomstart(tp);
863 }
864 }
865 #endif
866
867 return (0);
868 }
869
870 /*
871 * (un)block input via hw flowcontrol
872 */
873 Static void
874 ucom_hwiflow(struct ucom_softc *sc)
875 {
876 DPRINTF(("ucom_hwiflow:\n"));
877 #if 0
878 XXX
879 bus_space_tag_t iot = sc->sc_iot;
880 bus_space_handle_t ioh = sc->sc_ioh;
881
882 if (sc->sc_mcr_rts == 0)
883 return;
884
885 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
886 CLR(sc->sc_mcr, sc->sc_mcr_rts);
887 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
888 } else {
889 SET(sc->sc_mcr, sc->sc_mcr_rts);
890 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
891 }
892 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
893 #endif
894 }
895
896 Static void
897 ucomstart(struct tty *tp)
898 {
899 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
900 usbd_status err;
901 int s;
902 u_char *data;
903 int cnt;
904
905 if (sc->sc_dying)
906 return;
907
908 s = spltty();
909 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
910 DPRINTFN(4,("ucomstart: no go, state=0x%x\n", tp->t_state));
911 goto out;
912 }
913 if (sc->sc_tx_stopped)
914 goto out;
915
916 if (!ttypull(tp))
917 goto out;
918
919 /* Grab the first contiguous region of buffer space. */
920 data = tp->t_outq.c_cf;
921 cnt = ndqb(&tp->t_outq, 0);
922
923 if (cnt == 0) {
924 DPRINTF(("ucomstart: cnt==0\n"));
925 goto out;
926 }
927
928 SET(tp->t_state, TS_BUSY);
929
930 if (cnt > sc->sc_obufsize) {
931 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
932 cnt = sc->sc_obufsize;
933 }
934 if (sc->sc_methods->ucom_write != NULL)
935 sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
936 sc->sc_obuf, data, &cnt);
937 else
938 memcpy(sc->sc_obuf, data, cnt);
939
940 DPRINTFN(4,("ucomstart: %d chars\n", cnt));
941 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
942 (usbd_private_handle)sc, sc->sc_obuf, cnt,
943 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
944 /* What can we do on error? */
945 err = usbd_transfer(sc->sc_oxfer);
946 #ifdef DIAGNOSTIC
947 if (err != USBD_IN_PROGRESS)
948 printf("ucomstart: err=%s\n", usbd_errstr(err));
949 #endif
950
951 out:
952 splx(s);
953 }
954
955 void
956 ucomstop(struct tty *tp, int flag)
957 {
958 DPRINTF(("ucomstop: flag=%d\n", flag));
959 #if 0
960 /*struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];*/
961 int s;
962
963 s = spltty();
964 if (ISSET(tp->t_state, TS_BUSY)) {
965 DPRINTF(("ucomstop: XXX\n"));
966 /* sc->sc_tx_stopped = 1; */
967 if (!ISSET(tp->t_state, TS_TTSTOP))
968 SET(tp->t_state, TS_FLUSH);
969 }
970 splx(s);
971 #endif
972 }
973
974 Static void
975 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
976 {
977 struct ucom_softc *sc = (struct ucom_softc *)p;
978 struct tty *tp = sc->sc_tty;
979 u_int32_t cc;
980 int s;
981
982 DPRINTFN(5,("ucomwritecb: status=%d\n", status));
983
984 if (status == USBD_CANCELLED || sc->sc_dying)
985 goto error;
986
987 if (status) {
988 DPRINTF(("ucomwritecb: status=%d\n", status));
989 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
990 /* XXX we should restart after some delay. */
991 goto error;
992 }
993
994 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
995 #if defined(__NetBSD__) && NRND > 0
996 rnd_add_uint32(&sc->sc_rndsource, cc);
997 #endif
998 DPRINTFN(5,("ucomwritecb: cc=%d\n", cc));
999 /* convert from USB bytes to tty bytes */
1000 cc -= sc->sc_opkthdrlen;
1001
1002 s = spltty();
1003 CLR(tp->t_state, TS_BUSY);
1004 if (ISSET(tp->t_state, TS_FLUSH))
1005 CLR(tp->t_state, TS_FLUSH);
1006 else
1007 ndflush(&tp->t_outq, cc);
1008 (*tp->t_linesw->l_start)(tp);
1009 splx(s);
1010 return;
1011
1012 error:
1013 s = spltty();
1014 CLR(tp->t_state, TS_BUSY);
1015 splx(s);
1016 }
1017
1018 Static usbd_status
1019 ucomstartread(struct ucom_softc *sc)
1020 {
1021 usbd_status err;
1022
1023 DPRINTFN(5,("ucomstartread: start\n"));
1024 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
1025 (usbd_private_handle)sc,
1026 sc->sc_ibuf, sc->sc_ibufsize,
1027 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1028 USBD_NO_TIMEOUT, ucomreadcb);
1029 err = usbd_transfer(sc->sc_ixfer);
1030 if (err != USBD_IN_PROGRESS) {
1031 DPRINTF(("ucomstartread: err=%s\n", usbd_errstr(err)));
1032 return (err);
1033 }
1034 return (USBD_NORMAL_COMPLETION);
1035 }
1036
1037 Static void
1038 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
1039 {
1040 struct ucom_softc *sc = (struct ucom_softc *)p;
1041 struct tty *tp = sc->sc_tty;
1042 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1043 usbd_status err;
1044 u_int32_t cc;
1045 u_char *cp;
1046 int s;
1047
1048 DPRINTFN(5,("ucomreadcb: status=%d\n", status));
1049
1050 if (status == USBD_CANCELLED || status == USBD_IOERROR ||
1051 sc->sc_dying) {
1052 DPRINTF(("ucomreadcb: dying\n"));
1053 /* Send something to wake upper layer */
1054 s = spltty();
1055 (*rint)('\n', tp);
1056 mutex_spin_enter(&tty_lock); /* XXX */
1057 ttwakeup(tp);
1058 mutex_spin_exit(&tty_lock); /* XXX */
1059 splx(s);
1060 return;
1061 }
1062
1063 if (status) {
1064 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
1065 /* XXX we should restart after some delay. */
1066 return;
1067 }
1068
1069 usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
1070 #if defined(__NetBSD__) && NRND > 0
1071 rnd_add_uint32(&sc->sc_rndsource, cc);
1072 #endif
1073 DPRINTFN(5,("ucomreadcb: got %d chars, tp=%p\n", cc, tp));
1074 if (sc->sc_methods->ucom_read != NULL)
1075 sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
1076 &cp, &cc);
1077
1078 s = spltty();
1079 /* Give characters to tty layer. */
1080 while (cc-- > 0) {
1081 DPRINTFN(7,("ucomreadcb: char=0x%02x\n", *cp));
1082 if ((*rint)(*cp++, tp) == -1) {
1083 /* XXX what should we do? */
1084 printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
1085 cc);
1086 break;
1087 }
1088 }
1089 splx(s);
1090
1091 err = ucomstartread(sc);
1092 if (err) {
1093 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1094 /* XXX what should we dow now? */
1095 }
1096 }
1097
1098 Static void
1099 ucom_cleanup(struct ucom_softc *sc)
1100 {
1101 DPRINTF(("ucom_cleanup: closing pipes\n"));
1102
1103 ucom_shutdown(sc);
1104 if (sc->sc_bulkin_pipe != NULL) {
1105 usbd_abort_pipe(sc->sc_bulkin_pipe);
1106 usbd_close_pipe(sc->sc_bulkin_pipe);
1107 sc->sc_bulkin_pipe = NULL;
1108 }
1109 if (sc->sc_bulkout_pipe != NULL) {
1110 usbd_abort_pipe(sc->sc_bulkout_pipe);
1111 usbd_close_pipe(sc->sc_bulkout_pipe);
1112 sc->sc_bulkout_pipe = NULL;
1113 }
1114 if (sc->sc_ixfer != NULL) {
1115 usbd_free_xfer(sc->sc_ixfer);
1116 sc->sc_ixfer = NULL;
1117 }
1118 if (sc->sc_oxfer != NULL) {
1119 usbd_free_xfer(sc->sc_oxfer);
1120 sc->sc_oxfer = NULL;
1121 }
1122 }
1123
1124 #endif /* NUCOM > 0 */
1125
1126 int
1127 ucomprint(void *aux, const char *pnp)
1128 {
1129 struct ucom_attach_args *uca = aux;
1130
1131 if (pnp)
1132 aprint_normal("ucom at %s", pnp);
1133 if (uca->portno != UCOM_UNK_PORTNO)
1134 aprint_normal(" portno %d", uca->portno);
1135 return (UNCONF);
1136 }
1137
1138 int
1139 ucomsubmatch(struct device *parent, struct cfdata *cf,
1140 const int *ldesc, void *aux)
1141 {
1142 struct ucom_attach_args *uca = aux;
1143
1144 if (uca->portno != UCOM_UNK_PORTNO &&
1145 cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
1146 cf->cf_loc[UCOMBUSCF_PORTNO] != uca->portno)
1147 return (0);
1148 return (config_match(parent, cf, aux));
1149 }
1150