ucom.c revision 1.24.2.2 1 /* $NetBSD: ucom.c,v 1.24.2.2 2000/09/09 02:11:30 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 ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
646 ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
647 }
648
649 Static int
650 ucom_to_tiocm(struct ucom_softc *sc)
651 {
652 u_char combits;
653 int ttybits = 0;
654
655 combits = sc->sc_mcr;
656 if (ISSET(combits, UMCR_DTR))
657 SET(ttybits, TIOCM_DTR);
658 if (ISSET(combits, UMCR_RTS))
659 SET(ttybits, TIOCM_RTS);
660
661 combits = sc->sc_msr;
662 if (ISSET(combits, UMSR_DCD))
663 SET(ttybits, TIOCM_CD);
664 if (ISSET(combits, UMSR_CTS))
665 SET(ttybits, TIOCM_CTS);
666 if (ISSET(combits, UMSR_DSR))
667 SET(ttybits, TIOCM_DSR);
668 if (ISSET(combits, UMSR_RI | UMSR_TERI))
669 SET(ttybits, TIOCM_RI);
670
671 #if 0
672 XXX;
673 if (sc->sc_ier != 0)
674 SET(ttybits, TIOCM_LE);
675 #endif
676
677 return (ttybits);
678 }
679
680 Static void
681 ucom_break(sc, onoff)
682 struct ucom_softc *sc;
683 int onoff;
684 {
685 DPRINTF(("ucom_break: onoff=%d\n", onoff));
686
687 if (sc->sc_methods->ucom_set != NULL)
688 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
689 UCOM_SET_BREAK, onoff);
690 }
691
692 Static void
693 ucom_dtr(struct ucom_softc *sc, int onoff)
694 {
695 DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
696
697 if (sc->sc_methods->ucom_set != NULL)
698 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
699 UCOM_SET_DTR, onoff);
700 }
701
702 Static void
703 ucom_rts(struct ucom_softc *sc, int onoff)
704 {
705 DPRINTF(("ucom_rts: onoff=%d\n", onoff));
706
707 if (sc->sc_methods->ucom_set != NULL)
708 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
709 UCOM_SET_RTS, onoff);
710 }
711
712 void
713 ucom_status_change(struct ucom_softc *sc)
714 {
715 if (sc->sc_methods->ucom_get_status != NULL) {
716 sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
717 &sc->sc_lsr, &sc->sc_msr);
718 } else {
719 sc->sc_lsr = 0;
720 sc->sc_msr = 0;
721 }
722 }
723
724 Static int
725 ucomparam(struct tty *tp, struct termios *t)
726 {
727 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
728 int error;
729
730 if (sc->sc_dying)
731 return (EIO);
732
733 /* Check requested parameters. */
734 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
735 return (EINVAL);
736
737 /*
738 * For the console, always force CLOCAL and !HUPCL, so that the port
739 * is always active.
740 */
741 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
742 SET(t->c_cflag, CLOCAL);
743 CLR(t->c_cflag, HUPCL);
744 }
745
746 /*
747 * If there were no changes, don't do anything. This avoids dropping
748 * input and improves performance when all we did was frob things like
749 * VMIN and VTIME.
750 */
751 if (tp->t_ospeed == t->c_ospeed &&
752 tp->t_cflag == t->c_cflag)
753 return (0);
754
755 //XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
756
757 /* And copy to tty. */
758 tp->t_ispeed = 0;
759 tp->t_ospeed = t->c_ospeed;
760 tp->t_cflag = t->c_cflag;
761
762 if (sc->sc_methods->ucom_param != NULL) {
763 error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
764 t);
765 if (error)
766 return (error);
767 }
768
769 // XXX worry about CHWFLOW
770
771 /*
772 * Update the tty layer's idea of the carrier bit, in case we changed
773 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
774 * explicit request.
775 */
776 DPRINTF(("ucomparam: l_modem\n"));
777 (void) (*linesw[tp->t_line].l_modem)(tp, 1 /* XXX carrier */ );
778
779 #if 0
780 XXX what if the hardware is not open
781 if (!ISSET(t->c_cflag, CHWFLOW)) {
782 if (sc->sc_tx_stopped) {
783 sc->sc_tx_stopped = 0;
784 ucomstart(tp);
785 }
786 }
787 #endif
788
789 return (0);
790 }
791
792 /*
793 * (un)block input via hw flowcontrol
794 */
795 Static void
796 ucom_hwiflow(struct ucom_softc *sc)
797 {
798 DPRINTF(("ucom_hwiflow:\n"));
799 #if 0
800 XXX
801 bus_space_tag_t iot = sc->sc_iot;
802 bus_space_handle_t ioh = sc->sc_ioh;
803
804 if (sc->sc_mcr_rts == 0)
805 return;
806
807 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
808 CLR(sc->sc_mcr, sc->sc_mcr_rts);
809 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
810 } else {
811 SET(sc->sc_mcr, sc->sc_mcr_rts);
812 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
813 }
814 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
815 #endif
816 }
817
818 Static void
819 ucomstart(struct tty *tp)
820 {
821 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];
822 usbd_status err;
823 int s;
824 u_char *data;
825 int cnt;
826
827 if (sc->sc_dying)
828 return;
829
830 s = spltty();
831 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
832 DPRINTFN(4,("ucomstart: no go, state=0x%x\n", tp->t_state));
833 goto out;
834 }
835 if (sc->sc_tx_stopped)
836 goto out;
837
838 if (tp->t_outq.c_cc <= tp->t_lowat) {
839 if (ISSET(tp->t_state, TS_ASLEEP)) {
840 CLR(tp->t_state, TS_ASLEEP);
841 wakeup(&tp->t_outq);
842 }
843 selwakeup(&tp->t_wsel);
844 if (tp->t_outq.c_cc == 0)
845 goto out;
846 }
847
848 /* Grab the first contiguous region of buffer space. */
849 data = tp->t_outq.c_cf;
850 cnt = ndqb(&tp->t_outq, 0);
851
852 if (cnt == 0) {
853 DPRINTF(("ucomstart: cnt==0\n"));
854 goto out;
855 }
856
857 SET(tp->t_state, TS_BUSY);
858
859 if (cnt > sc->sc_obufsize) {
860 DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
861 cnt = sc->sc_obufsize;
862 }
863 if (sc->sc_methods->ucom_write != NULL)
864 sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
865 sc->sc_obuf, data, &cnt);
866 else
867 memcpy(sc->sc_obuf, data, cnt);
868
869 DPRINTFN(4,("ucomstart: %d chars\n", cnt));
870 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
871 (usbd_private_handle)sc, sc->sc_obuf, cnt,
872 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
873 /* What can we do on error? */
874 err = usbd_transfer(sc->sc_oxfer);
875 #ifdef DIAGNOSTIC
876 if (err != USBD_IN_PROGRESS)
877 printf("ucomstart: err=%s\n", usbd_errstr(err));
878 #endif
879
880 out:
881 splx(s);
882 }
883
884 void
885 ucomstop(struct tty *tp, int flag)
886 {
887 DPRINTF(("ucomstop: flag=%d\n", flag));
888 #if 0
889 /*struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];*/
890 int s;
891
892 s = spltty();
893 if (ISSET(tp->t_state, TS_BUSY)) {
894 DPRINTF(("ucomstop: XXX\n"));
895 /* sc->sc_tx_stopped = 1; */
896 if (!ISSET(tp->t_state, TS_TTSTOP))
897 SET(tp->t_state, TS_FLUSH);
898 }
899 splx(s);
900 #endif
901 }
902
903 Static void
904 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
905 {
906 struct ucom_softc *sc = (struct ucom_softc *)p;
907 struct tty *tp = sc->sc_tty;
908 u_int32_t cc;
909 int s;
910
911 DPRINTFN(5,("ucomwritecb: status=%d\n", status));
912
913 if (status == USBD_CANCELLED)
914 return;
915
916 if (status) {
917 DPRINTF(("ucomwritecb: status=%d\n", status));
918 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
919 /* XXX we should restart after some delay. */
920 return;
921 }
922
923 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
924 DPRINTFN(5,("ucomwritecb: cc=%d\n", cc));
925 /* convert from USB bytes to tty bytes */
926 cc -= sc->sc_opkthdrlen;
927
928 s = spltty();
929 CLR(tp->t_state, TS_BUSY);
930 if (ISSET(tp->t_state, TS_FLUSH))
931 CLR(tp->t_state, TS_FLUSH);
932 else
933 ndflush(&tp->t_outq, cc);
934 (*linesw[tp->t_line].l_start)(tp);
935 splx(s);
936 }
937
938 Static usbd_status
939 ucomstartread(struct ucom_softc *sc)
940 {
941 usbd_status err;
942
943 DPRINTFN(5,("ucomstartread: start\n"));
944 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
945 (usbd_private_handle)sc,
946 sc->sc_ibuf, sc->sc_ibufsize,
947 USBD_SHORT_XFER_OK | USBD_NO_COPY,
948 USBD_NO_TIMEOUT, ucomreadcb);
949 err = usbd_transfer(sc->sc_ixfer);
950 if (err != USBD_IN_PROGRESS) {
951 DPRINTF(("ucomstartread: err=%s\n", usbd_errstr(err)));
952 return (err);
953 }
954 return (USBD_NORMAL_COMPLETION);
955 }
956
957 Static void
958 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
959 {
960 struct ucom_softc *sc = (struct ucom_softc *)p;
961 struct tty *tp = sc->sc_tty;
962 int (*rint)(int c, struct tty *tp) = linesw[tp->t_line].l_rint;
963 usbd_status err;
964 u_int32_t cc;
965 u_char *cp;
966 int s;
967
968 if (status == USBD_CANCELLED)
969 return;
970
971 if (status) {
972 DPRINTF(("ucomreadcb: status=%d\n", status));
973 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
974 /* XXX we should restart after some delay. */
975 return;
976 }
977
978 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
979 DPRINTFN(5,("ucomreadcb: got %d chars, tp=%p\n", cc, tp));
980 if (sc->sc_methods->ucom_read != NULL)
981 sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
982 &cp, &cc);
983
984 s = spltty();
985 /* Give characters to tty layer. */
986 while (cc-- > 0) {
987 DPRINTFN(7,("ucomreadcb: char=0x%02x\n", *cp));
988 if ((*rint)(*cp++, tp) == -1) {
989 /* XXX what should we do? */
990 printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
991 cc);
992 break;
993 }
994 }
995 splx(s);
996
997 err = ucomstartread(sc);
998 if (err) {
999 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
1000 /* XXX what should we dow now? */
1001 }
1002 }
1003
1004 Static void
1005 ucom_cleanup(struct ucom_softc *sc)
1006 {
1007 DPRINTF(("ucom_cleanup: closing pipes\n"));
1008
1009 ucom_shutdown(sc);
1010 usbd_abort_pipe(sc->sc_bulkin_pipe);
1011 usbd_close_pipe(sc->sc_bulkin_pipe);
1012 usbd_abort_pipe(sc->sc_bulkout_pipe);
1013 usbd_close_pipe(sc->sc_bulkout_pipe);
1014 usbd_free_xfer(sc->sc_ixfer);
1015 usbd_free_xfer(sc->sc_oxfer);
1016 }
1017
1018 #endif /* NUCOM > 0 */
1019
1020 int
1021 ucomprint(void *aux, const char *pnp)
1022 {
1023
1024 if (pnp)
1025 printf("ucom at %s\n", pnp);
1026 return (UNCONF);
1027 }
1028
1029 int
1030 ucomsubmatch(struct device *parent, struct cfdata *cf, void *aux)
1031 {
1032 struct ucom_attach_args *uca = aux;
1033
1034 if (uca->portno != UCOM_UNK_PORTNO &&
1035 cf->ucomcf_portno != UCOM_UNK_PORTNO &&
1036 cf->ucomcf_portno != uca->portno)
1037 return (0);
1038 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1039 }
1040