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