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