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