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