ec1e9834c13a0f3513a57ba95254ee28bc7ff0a1
[dragonfly.git] / sys / bus / u4b / serial / usb_serial.c
1 /*      $NetBSD: ucom.c,v 1.40 2001/11/13 06:24:54 lukem Exp $  */
2
3 /*-
4  * Copyright (c) 2001-2003, 2005, 2008
5  *      Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /*-
31  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
32  * All rights reserved.
33  *
34  * This code is derived from software contributed to The NetBSD Foundation
35  * by Lennart Augustsson (lennart@augustsson.net) at
36  * Carlstedt Research & Technology.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *        This product includes software developed by the NetBSD
49  *        Foundation, Inc. and its contributors.
50  * 4. Neither the name of The NetBSD Foundation nor the names of its
51  *    contributors may be used to endorse or promote products derived
52  *    from this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64  * POSSIBILITY OF SUCH DAMAGE.
65  */
66
67 #include <sys/stdint.h>
68 #include <sys/param.h>
69 #include <sys/queue.h>
70 #include <sys/types.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/bus.h>
74 #include <sys/module.h>
75 #include <sys/lock.h>
76 #include <sys/condvar.h>
77 #include <sys/sysctl.h>
78 #include <sys/unistd.h>
79 #include <sys/callout.h>
80 #include <sys/malloc.h>
81 #include <sys/priv.h>
82 #include <sys/cons.h>
83
84 #include <bus/u4b/usb.h>
85 #include <bus/u4b/usbdi.h>
86 #include <bus/u4b/usbdi_util.h>
87
88 #define USB_DEBUG_VAR ucom_debug
89 #include <bus/u4b/usb_debug.h>
90 #include <bus/u4b/usb_busdma.h>
91 #include <bus/u4b/usb_process.h>
92
93 #include <bus/u4b/serial/usb_serial.h>
94
95 //#include "opt_gdb.h"
96
97 static SYSCTL_NODE(_hw_usb, OID_AUTO, ucom, CTLFLAG_RW, 0, "USB ucom");
98
99 #ifdef USB_DEBUG
100 static int ucom_debug = 0;
101
102 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, debug, CTLFLAG_RW,
103     &ucom_debug, 0, "ucom debug level");
104 #endif
105
106 #define UCOM_CONS_BUFSIZE 1024
107
108 static uint8_t ucom_cons_rx_buf[UCOM_CONS_BUFSIZE];
109 static uint8_t ucom_cons_tx_buf[UCOM_CONS_BUFSIZE];
110
111 static unsigned int ucom_cons_rx_low = 0;
112 static unsigned int ucom_cons_rx_high = 0;
113
114 static unsigned int ucom_cons_tx_low = 0;
115 static unsigned int ucom_cons_tx_high = 0;
116
117 static int ucom_cons_unit = -1;
118 static int ucom_cons_subunit = 0;
119 static int ucom_cons_baud = 9600;
120 static struct ucom_softc *ucom_cons_softc = NULL;
121
122 TUNABLE_INT("hw.usb.ucom.cons_unit", &ucom_cons_unit);
123 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_unit, CTLFLAG_RW,
124     &ucom_cons_unit, 0, "console unit number");
125 TUNABLE_INT("hw.usb.ucom.cons_subunit", &ucom_cons_subunit);
126 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_subunit, CTLFLAG_RW,
127     &ucom_cons_subunit, 0, "console subunit number");
128 TUNABLE_INT("hw.usb.ucom.cons_baud", &ucom_cons_baud);
129 SYSCTL_INT(_hw_usb_ucom, OID_AUTO, cons_baud, CTLFLAG_RW,
130     &ucom_cons_baud, 0, "console baud rate");
131
132 static usb_proc_callback_t ucom_cfg_start_transfers;
133 static usb_proc_callback_t ucom_cfg_open;
134 static usb_proc_callback_t ucom_cfg_close;
135 static usb_proc_callback_t ucom_cfg_line_state;
136 static usb_proc_callback_t ucom_cfg_status_change;
137 static usb_proc_callback_t ucom_cfg_param;
138
139 static int      ucom_unit_alloc(void);
140 static void     ucom_unit_free(int);
141 static int      ucom_attach_tty(struct ucom_super_softc *, struct ucom_softc *);
142 static void     ucom_detach_tty(struct ucom_super_softc *, struct ucom_softc *);
143 static void     ucom_queue_command(struct ucom_softc *,
144                     usb_proc_callback_t *, struct termios *pt,
145                     struct usb_proc_msg *t0, struct usb_proc_msg *t1);
146 static void     ucom_shutdown(struct ucom_softc *);
147 static void     ucom_ring(struct ucom_softc *, uint8_t);
148 static void     ucom_break(struct ucom_softc *, uint8_t);
149 static void     ucom_dtr(struct ucom_softc *, uint8_t);
150 static void     ucom_rts(struct ucom_softc *, uint8_t);
151
152 static tsw_open_t ucom_open;
153 static tsw_close_t ucom_close;
154 static tsw_ioctl_t ucom_ioctl;
155 static tsw_modem_t ucom_modem;
156 static tsw_param_t ucom_param;
157 static tsw_outwakeup_t ucom_outwakeup;
158 static tsw_inwakeup_t ucom_inwakeup;
159 static tsw_free_t ucom_free;
160
161 static struct ttydevsw ucom_class = {
162         .tsw_flags = TF_INITLOCK | TF_CALLOUT,
163         .tsw_open = ucom_open,
164         .tsw_close = ucom_close,
165         .tsw_outwakeup = ucom_outwakeup,
166         .tsw_inwakeup = ucom_inwakeup,
167         .tsw_ioctl = ucom_ioctl,
168         .tsw_param = ucom_param,
169         .tsw_modem = ucom_modem,
170         .tsw_free = ucom_free,
171 };
172
173 MODULE_DEPEND(ucom, usb, 1, 1, 1);
174 MODULE_VERSION(ucom, 1);
175
176 #define UCOM_UNIT_MAX           128     /* maximum number of units */
177 #define UCOM_TTY_PREFIX         "U"
178
179 static struct unrhdr *ucom_unrhdr;
180 static struct lock ucom_lock;
181 static int ucom_close_refs;
182
183 static void
184 ucom_init(void *arg)
185 {
186         DPRINTF("\n");
187         ucom_unrhdr = new_unrhdr(0, UCOM_UNIT_MAX - 1, NULL);
188         lockinit(&ucom_lock, "UCOM LOCK", 0, 0);
189 }
190 SYSINIT(ucom_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ucom_init, NULL);
191
192 static void
193 ucom_uninit(void *arg)
194 {
195         struct unrhdr *hdr;
196         hdr = ucom_unrhdr;
197         ucom_unrhdr = NULL;
198
199         DPRINTF("\n");
200
201         if (hdr != NULL)
202                 delete_unrhdr(hdr);
203
204         lockuninit(&ucom_lock);
205 }
206 SYSUNINIT(ucom_uninit, SI_SUB_KLD - 2, SI_ORDER_ANY, ucom_uninit, NULL);
207
208 /*
209  * Mark a unit number (the X in cuaUX) as in use.
210  *
211  * Note that devices using a different naming scheme (see ucom_tty_name()
212  * callback) still use this unit allocation.
213  */
214 static int
215 ucom_unit_alloc(void)
216 {
217         int unit;
218
219         /* sanity checks */
220         if (ucom_unrhdr == NULL) {
221                 DPRINTF("ucom_unrhdr is NULL\n");
222                 return (-1);
223         }
224         unit = alloc_unr(ucom_unrhdr);
225         DPRINTF("unit %d is allocated\n", unit);
226         return (unit);
227 }
228
229 /*
230  * Mark the unit number as not in use.
231  */
232 static void
233 ucom_unit_free(int unit)
234 {
235         /* sanity checks */
236         if (unit < 0 || unit >= UCOM_UNIT_MAX || ucom_unrhdr == NULL) {
237                 DPRINTF("cannot free unit number\n");
238                 return;
239         }
240         DPRINTF("unit %d is freed\n", unit);
241         free_unr(ucom_unrhdr, unit);
242 }
243
244 /*
245  * Setup a group of one or more serial ports.
246  *
247  * The lock pointed to by "lock" is applied before all
248  * callbacks are called back. Also "lock" must be applied
249  * before calling into the ucom-layer!
250  */
251 int
252 ucom_attach(struct ucom_super_softc *ssc, struct ucom_softc *sc,
253     int subunits, void *parent,
254     const struct ucom_callback *callback, struct lock *lock)
255 {
256         int subunit;
257         int error = 0;
258
259         if ((sc == NULL) ||
260             (subunits <= 0) ||
261             (callback == NULL) ||
262             (lock == NULL)) {
263                 return (EINVAL);
264         }
265
266         /* allocate a uniq unit number */
267         ssc->sc_unit = ucom_unit_alloc();
268         if (ssc->sc_unit == -1)
269                 return (ENOMEM);
270
271         /* generate TTY name string */
272         ksnprintf(ssc->sc_ttyname, sizeof(ssc->sc_ttyname),
273             UCOM_TTY_PREFIX "%d", ssc->sc_unit);
274
275         /* create USB request handling process */
276         error = usb_proc_create(&ssc->sc_tq, lock, "ucom", USB_PRI_MED);
277         if (error) {
278                 ucom_unit_free(ssc->sc_unit);
279                 return (error);
280         }
281         ssc->sc_subunits = subunits;
282         ssc->sc_flag = UCOM_FLAG_ATTACHED |
283             UCOM_FLAG_FREE_UNIT;
284
285         if (callback->ucom_free == NULL)
286                 ssc->sc_flag |= UCOM_FLAG_WAIT_REFS;
287
288         /* increment reference count */
289         ucom_ref(ssc);
290
291         for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
292                 sc[subunit].sc_subunit = subunit;
293                 sc[subunit].sc_super = ssc;
294                 sc[subunit].sc_lock = lock;
295                 sc[subunit].sc_parent = parent;
296                 sc[subunit].sc_callback = callback;
297
298                 error = ucom_attach_tty(ssc, &sc[subunit]);
299                 if (error) {
300                         ucom_detach(ssc, &sc[0]);
301                         return (error);
302                 }
303                 /* increment reference count */
304                 ucom_ref(ssc);
305
306                 /* set subunit attached */
307                 sc[subunit].sc_flag |= UCOM_FLAG_ATTACHED;
308         }
309
310         DPRINTF("tp = %p, unit = %d, subunits = %d\n",
311                 sc->sc_tty, ssc->sc_unit, ssc->sc_subunits);
312
313         return (0);
314 }
315
316 /*
317  * The following function will do nothing if the structure pointed to
318  * by "ssc" and "sc" is zero or has already been detached.
319  */
320 void
321 ucom_detach(struct ucom_super_softc *ssc, struct ucom_softc *sc)
322 {
323         int subunit;
324
325         if (!(ssc->sc_flag & UCOM_FLAG_ATTACHED))
326                 return;         /* not initialized */
327
328         if (ssc->sc_sysctl_ttyname != NULL) {
329                 sysctl_remove_oid(ssc->sc_sysctl_ttyname, 1, 0);
330                 ssc->sc_sysctl_ttyname = NULL;
331         }
332
333         if (ssc->sc_sysctl_ttyports != NULL) {
334                 sysctl_remove_oid(ssc->sc_sysctl_ttyports, 1, 0);
335                 ssc->sc_sysctl_ttyports = NULL;
336         }
337
338         usb_proc_drain(&ssc->sc_tq);
339
340         for (subunit = 0; subunit < ssc->sc_subunits; subunit++) {
341                 if (sc[subunit].sc_flag & UCOM_FLAG_ATTACHED) {
342
343                         ucom_detach_tty(ssc, &sc[subunit]);
344
345                         /* avoid duplicate detach */
346                         sc[subunit].sc_flag &= ~UCOM_FLAG_ATTACHED;
347                 }
348         }
349         usb_proc_free(&ssc->sc_tq);
350
351         ucom_unref(ssc);
352
353         if (ssc->sc_flag & UCOM_FLAG_WAIT_REFS)
354                 ucom_drain(ssc);
355
356         /* make sure we don't detach twice */
357         ssc->sc_flag &= ~UCOM_FLAG_ATTACHED;
358 }
359
360 void
361 ucom_drain(struct ucom_super_softc *ssc)
362 {
363         lockmgr(&ucom_lock, LK_EXCLUSIVE);
364         while (ssc->sc_refs > 0) {
365                 printf("ucom: Waiting for a TTY device to close.\n");
366                 usb_pause_mtx(&ucom_lock, hz);
367         }
368         lockmgr(&ucom_lock, LK_RELEASE);
369 }
370
371 void
372 ucom_drain_all(void *arg)
373 {
374         lockmgr(&ucom_lock, LK_EXCLUSIVE);
375         while (ucom_close_refs > 0) {
376                 printf("ucom: Waiting for all detached TTY "
377                     "devices to have open fds closed.\n");
378                 usb_pause_mtx(&ucom_lock, hz);
379         }
380         lockmgr(&ucom_lock, LK_RELEASE);
381 }
382
383 static int
384 ucom_attach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
385 {
386         struct tty *tp;
387         char buf[32];                   /* temporary TTY device name buffer */
388
389         tp = tty_alloc_mutex(&ucom_class, sc, sc->sc_lock);
390         if (tp == NULL)
391                 return (ENOMEM);
392
393         /* Check if the client has a custom TTY name */
394         buf[0] = '\0';
395         if (sc->sc_callback->ucom_tty_name) {
396                 sc->sc_callback->ucom_tty_name(sc, buf,
397                     sizeof(buf), ssc->sc_unit, sc->sc_subunit);
398         }
399         if (buf[0] == 0) {
400                 /* Use default TTY name */
401                 if (ssc->sc_subunits > 1) {
402                         /* multiple modems in one */
403                         ksnprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u.%u",
404                             ssc->sc_unit, sc->sc_subunit);
405                 } else {
406                         /* single modem */
407                         ksnprintf(buf, sizeof(buf), UCOM_TTY_PREFIX "%u",
408                             ssc->sc_unit);
409                 }
410         }
411         tty_makedev(tp, NULL, "%s", buf);
412
413         sc->sc_tty = tp;
414
415         DPRINTF("ttycreate: %s\n", buf);
416
417         /* Check if this device should be a console */
418         if ((ucom_cons_softc == NULL) && 
419             (ssc->sc_unit == ucom_cons_unit) &&
420             (sc->sc_subunit == ucom_cons_subunit)) {
421
422                 DPRINTF("unit %d subunit %d is console",
423                     ssc->sc_unit, sc->sc_subunit);
424
425                 ucom_cons_softc = sc;
426
427                 tty_init_console(tp, ucom_cons_baud);
428
429                 UCOM_MTX_LOCK(ucom_cons_softc);
430                 ucom_cons_rx_low = 0;
431                 ucom_cons_rx_high = 0;
432                 ucom_cons_tx_low = 0;
433                 ucom_cons_tx_high = 0;
434                 sc->sc_flag |= UCOM_FLAG_CONSOLE;
435                 ucom_open(ucom_cons_softc->sc_tty);
436                 ucom_param(ucom_cons_softc->sc_tty, &tp->t_termios_init_in);
437                 UCOM_MTX_UNLOCK(ucom_cons_softc);
438         }
439
440         return (0);
441 }
442
443 static void
444 ucom_detach_tty(struct ucom_super_softc *ssc, struct ucom_softc *sc)
445 {
446         struct tty *tp = sc->sc_tty;
447
448         DPRINTF("sc = %p, tp = %p\n", sc, sc->sc_tty);
449
450         if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
451                 UCOM_MTX_LOCK(ucom_cons_softc);
452                 ucom_close(ucom_cons_softc->sc_tty);
453                 sc->sc_flag &= ~UCOM_FLAG_CONSOLE;
454                 UCOM_MTX_UNLOCK(ucom_cons_softc);
455                 ucom_cons_softc = NULL;
456         }
457
458         /* the config thread has been stopped when we get here */
459
460         UCOM_MTX_LOCK(sc);
461         sc->sc_flag |= UCOM_FLAG_GONE;
462         sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_LL_READY);
463         UCOM_MTX_UNLOCK(sc);
464
465         if (tp) {
466                 lockmgr(&ucom_lock, LK_EXCLUSIVE);
467                 ucom_close_refs++;
468                 lockmgr(&ucom_lock, LK_RELEASE);
469
470                 tty_lock(tp);
471
472                 ucom_close(tp); /* close, if any */
473
474                 tty_rel_gone(tp);
475
476                 UCOM_MTX_LOCK(sc);
477                 /*
478                  * make sure that read and write transfers are stopped
479                  */
480                 if (sc->sc_callback->ucom_stop_read) {
481                         (sc->sc_callback->ucom_stop_read) (sc);
482                 }
483                 if (sc->sc_callback->ucom_stop_write) {
484                         (sc->sc_callback->ucom_stop_write) (sc);
485                 }
486                 UCOM_MTX_UNLOCK(sc);
487         }
488 }
489
490 void
491 ucom_set_pnpinfo_usb(struct ucom_super_softc *ssc, device_t dev)
492 {
493         char buf[64];
494         uint8_t iface_index;
495         struct usb_attach_arg *uaa;
496
497         ksnprintf(buf, sizeof(buf), "ttyname=" UCOM_TTY_PREFIX
498             "%d ttyports=%d", ssc->sc_unit, ssc->sc_subunits);
499
500         /* Store the PNP info in the first interface for the device */
501         uaa = device_get_ivars(dev);
502         iface_index = uaa->info.bIfaceIndex;
503     
504         if (usbd_set_pnpinfo(uaa->device, iface_index, buf) != 0)
505                 device_printf(dev, "Could not set PNP info\n");
506
507         /*
508          * The following information is also replicated in the PNP-info
509          * string which is registered above:
510          */
511         if (ssc->sc_sysctl_ttyname == NULL) {
512                 ssc->sc_sysctl_ttyname = SYSCTL_ADD_STRING(NULL,
513                     SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
514                     OID_AUTO, "ttyname", CTLFLAG_RD, ssc->sc_ttyname, 0,
515                     "TTY device basename");
516         }
517         if (ssc->sc_sysctl_ttyports == NULL) {
518                 ssc->sc_sysctl_ttyports = SYSCTL_ADD_INT(NULL,
519                     SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
520                     OID_AUTO, "ttyports", CTLFLAG_RD,
521                     NULL, ssc->sc_subunits, "Number of ports");
522         }
523 }
524
525 static void
526 ucom_queue_command(struct ucom_softc *sc,
527     usb_proc_callback_t *fn, struct termios *pt,
528     struct usb_proc_msg *t0, struct usb_proc_msg *t1)
529 {
530         struct ucom_super_softc *ssc = sc->sc_super;
531         struct ucom_param_task *task;
532
533         UCOM_MTX_ASSERT(sc, MA_OWNED);
534
535         if (usb_proc_is_gone(&ssc->sc_tq)) {
536                 DPRINTF("proc is gone\n");
537                 return;         /* nothing to do */
538         }
539         /* 
540          * NOTE: The task cannot get executed before we drop the
541          * "sc_lock" lock. It is safe to update fields in the message
542          * structure after that the message got queued.
543          */
544         task = (struct ucom_param_task *)
545           usb_proc_msignal(&ssc->sc_tq, t0, t1);
546
547         /* Setup callback and softc pointers */
548         task->hdr.pm_callback = fn;
549         task->sc = sc;
550
551         /* 
552          * Make a copy of the termios. This field is only present if
553          * the "pt" field is not NULL.
554          */
555         if (pt != NULL)
556                 task->termios_copy = *pt;
557
558         /*
559          * Closing the device should be synchronous.
560          */
561         if (fn == ucom_cfg_close)
562                 usb_proc_mwait(&ssc->sc_tq, t0, t1);
563
564         /*
565          * In case of multiple configure requests,
566          * keep track of the last one!
567          */
568         if (fn == ucom_cfg_start_transfers)
569                 sc->sc_last_start_xfer = &task->hdr;
570 }
571
572 static void
573 ucom_shutdown(struct ucom_softc *sc)
574 {
575         struct tty *tp = sc->sc_tty;
576
577         UCOM_MTX_ASSERT(sc, MA_OWNED);
578
579         DPRINTF("\n");
580
581         /*
582          * Hang up if necessary:
583          */
584         if (tp->t_termios.c_cflag & HUPCL) {
585                 ucom_modem(tp, 0, SER_DTR);
586         }
587 }
588
589 /*
590  * Return values:
591  *    0: normal
592  * else: taskqueue is draining or gone
593  */
594 uint8_t
595 ucom_cfg_is_gone(struct ucom_softc *sc)
596 {
597         struct ucom_super_softc *ssc = sc->sc_super;
598
599         return (usb_proc_is_gone(&ssc->sc_tq));
600 }
601
602 static void
603 ucom_cfg_start_transfers(struct usb_proc_msg *_task)
604 {
605         struct ucom_cfg_task *task = 
606             (struct ucom_cfg_task *)_task;
607         struct ucom_softc *sc = task->sc;
608
609         if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
610                 return;
611         }
612         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
613                 /* TTY device closed */
614                 return;
615         }
616
617         if (_task == sc->sc_last_start_xfer)
618                 sc->sc_flag |= UCOM_FLAG_GP_DATA;
619
620         if (sc->sc_callback->ucom_start_read) {
621                 (sc->sc_callback->ucom_start_read) (sc);
622         }
623         if (sc->sc_callback->ucom_start_write) {
624                 (sc->sc_callback->ucom_start_write) (sc);
625         }
626 }
627
628 static void
629 ucom_start_transfers(struct ucom_softc *sc)
630 {
631         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
632                 return;
633         }
634         /*
635          * Make sure that data transfers are started in both
636          * directions:
637          */
638         if (sc->sc_callback->ucom_start_read) {
639                 (sc->sc_callback->ucom_start_read) (sc);
640         }
641         if (sc->sc_callback->ucom_start_write) {
642                 (sc->sc_callback->ucom_start_write) (sc);
643         }
644 }
645
646 static void
647 ucom_cfg_open(struct usb_proc_msg *_task)
648 {
649         struct ucom_cfg_task *task = 
650             (struct ucom_cfg_task *)_task;
651         struct ucom_softc *sc = task->sc;
652
653         DPRINTF("\n");
654
655         if (sc->sc_flag & UCOM_FLAG_LL_READY) {
656
657                 /* already opened */
658
659         } else {
660
661                 sc->sc_flag |= UCOM_FLAG_LL_READY;
662
663                 if (sc->sc_callback->ucom_cfg_open) {
664                         (sc->sc_callback->ucom_cfg_open) (sc);
665
666                         /* wait a little */
667                         usb_pause_mtx(sc->sc_lock, hz / 10);
668                 }
669         }
670 }
671
672 static int
673 ucom_open(struct tty *tp)
674 {
675         struct ucom_softc *sc = tty_softc(tp);
676         int error;
677
678         UCOM_MTX_ASSERT(sc, MA_OWNED);
679
680         if (sc->sc_flag & UCOM_FLAG_GONE) {
681                 return (ENXIO);
682         }
683         if (sc->sc_flag & UCOM_FLAG_HL_READY) {
684                 /* already opened */
685                 return (0);
686         }
687         DPRINTF("tp = %p\n", tp);
688
689         if (sc->sc_callback->ucom_pre_open) {
690                 /*
691                  * give the lower layer a chance to disallow TTY open, for
692                  * example if the device is not present:
693                  */
694                 error = (sc->sc_callback->ucom_pre_open) (sc);
695                 if (error) {
696                         return (error);
697                 }
698         }
699         sc->sc_flag |= UCOM_FLAG_HL_READY;
700
701         /* Disable transfers */
702         sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
703
704         sc->sc_lsr = 0;
705         sc->sc_msr = 0;
706         sc->sc_mcr = 0;
707
708         /* reset programmed line state */
709         sc->sc_pls_curr = 0;
710         sc->sc_pls_set = 0;
711         sc->sc_pls_clr = 0;
712
713         /* reset jitter buffer */
714         sc->sc_jitterbuf_in = 0;
715         sc->sc_jitterbuf_out = 0;
716
717         ucom_queue_command(sc, ucom_cfg_open, NULL,
718             &sc->sc_open_task[0].hdr,
719             &sc->sc_open_task[1].hdr);
720
721         /* Queue transfer enable command last */
722         ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
723             &sc->sc_start_task[0].hdr, 
724             &sc->sc_start_task[1].hdr);
725
726         ucom_modem(tp, SER_DTR | SER_RTS, 0);
727
728         ucom_ring(sc, 0);
729
730         ucom_break(sc, 0);
731
732         ucom_status_change(sc);
733
734         return (0);
735 }
736
737 static void
738 ucom_cfg_close(struct usb_proc_msg *_task)
739 {
740         struct ucom_cfg_task *task = 
741             (struct ucom_cfg_task *)_task;
742         struct ucom_softc *sc = task->sc;
743
744         DPRINTF("\n");
745
746         if (sc->sc_flag & UCOM_FLAG_LL_READY) {
747                 sc->sc_flag &= ~UCOM_FLAG_LL_READY;
748                 if (sc->sc_callback->ucom_cfg_close)
749                         (sc->sc_callback->ucom_cfg_close) (sc);
750         } else {
751                 /* already closed */
752         }
753 }
754
755 static void
756 ucom_close(struct tty *tp)
757 {
758         struct ucom_softc *sc = tty_softc(tp);
759
760         UCOM_MTX_ASSERT(sc, MA_OWNED);
761
762         DPRINTF("tp=%p\n", tp);
763
764         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
765                 DPRINTF("tp=%p already closed\n", tp);
766                 return;
767         }
768         ucom_shutdown(sc);
769
770         ucom_queue_command(sc, ucom_cfg_close, NULL,
771             &sc->sc_close_task[0].hdr,
772             &sc->sc_close_task[1].hdr);
773
774         sc->sc_flag &= ~(UCOM_FLAG_HL_READY | UCOM_FLAG_RTS_IFLOW);
775
776         if (sc->sc_callback->ucom_stop_read) {
777                 (sc->sc_callback->ucom_stop_read) (sc);
778         }
779 }
780
781 static void
782 ucom_inwakeup(struct tty *tp)
783 {
784         struct ucom_softc *sc = tty_softc(tp);
785         uint16_t pos;
786
787         if (sc == NULL)
788                 return;
789
790         UCOM_MTX_ASSERT(sc, MA_OWNED);
791
792         DPRINTF("tp=%p\n", tp);
793
794         if (ttydisc_can_bypass(tp) != 0 || 
795             (sc->sc_flag & UCOM_FLAG_HL_READY) == 0 ||
796             (sc->sc_flag & UCOM_FLAG_INWAKEUP) != 0) {
797                 return;
798         }
799
800         /* prevent recursion */
801         sc->sc_flag |= UCOM_FLAG_INWAKEUP;
802
803         pos = sc->sc_jitterbuf_out;
804
805         while (sc->sc_jitterbuf_in != pos) {
806                 int c;
807
808                 c = (char)sc->sc_jitterbuf[pos];
809
810                 if (ttydisc_rint(tp, c, 0) == -1)
811                         break;
812                 pos++;
813                 if (pos >= UCOM_JITTERBUF_SIZE)
814                         pos -= UCOM_JITTERBUF_SIZE;
815         }
816
817         sc->sc_jitterbuf_out = pos;
818
819         /* clear RTS in async fashion */
820         if ((sc->sc_jitterbuf_in == pos) && 
821             (sc->sc_flag & UCOM_FLAG_RTS_IFLOW))
822                 ucom_rts(sc, 0);
823
824         sc->sc_flag &= ~UCOM_FLAG_INWAKEUP;
825 }
826
827 static int
828 ucom_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
829 {
830         struct ucom_softc *sc = tty_softc(tp);
831         int error;
832
833         UCOM_MTX_ASSERT(sc, MA_OWNED);
834
835         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
836                 return (EIO);
837         }
838         DPRINTF("cmd = 0x%08lx\n", cmd);
839
840         switch (cmd) {
841 #if 0
842         case TIOCSRING:
843                 ucom_ring(sc, 1);
844                 error = 0;
845                 break;
846         case TIOCCRING:
847                 ucom_ring(sc, 0);
848                 error = 0;
849                 break;
850 #endif
851         case TIOCSBRK:
852                 ucom_break(sc, 1);
853                 error = 0;
854                 break;
855         case TIOCCBRK:
856                 ucom_break(sc, 0);
857                 error = 0;
858                 break;
859         default:
860                 if (sc->sc_callback->ucom_ioctl) {
861                         error = (sc->sc_callback->ucom_ioctl)
862                             (sc, cmd, data, 0, td);
863                 } else {
864                         error = ENOIOCTL;
865                 }
866                 break;
867         }
868         return (error);
869 }
870
871 static int
872 ucom_modem(struct tty *tp, int sigon, int sigoff)
873 {
874         struct ucom_softc *sc = tty_softc(tp);
875         uint8_t onoff;
876
877         UCOM_MTX_ASSERT(sc, MA_OWNED);
878
879         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
880                 return (0);
881         }
882         if ((sigon == 0) && (sigoff == 0)) {
883
884                 if (sc->sc_mcr & SER_DTR) {
885                         sigon |= SER_DTR;
886                 }
887                 if (sc->sc_mcr & SER_RTS) {
888                         sigon |= SER_RTS;
889                 }
890                 if (sc->sc_msr & SER_CTS) {
891                         sigon |= SER_CTS;
892                 }
893                 if (sc->sc_msr & SER_DCD) {
894                         sigon |= SER_DCD;
895                 }
896                 if (sc->sc_msr & SER_DSR) {
897                         sigon |= SER_DSR;
898                 }
899                 if (sc->sc_msr & SER_RI) {
900                         sigon |= SER_RI;
901                 }
902                 return (sigon);
903         }
904         if (sigon & SER_DTR) {
905                 sc->sc_mcr |= SER_DTR;
906         }
907         if (sigoff & SER_DTR) {
908                 sc->sc_mcr &= ~SER_DTR;
909         }
910         if (sigon & SER_RTS) {
911                 sc->sc_mcr |= SER_RTS;
912         }
913         if (sigoff & SER_RTS) {
914                 sc->sc_mcr &= ~SER_RTS;
915         }
916         onoff = (sc->sc_mcr & SER_DTR) ? 1 : 0;
917         ucom_dtr(sc, onoff);
918
919         onoff = (sc->sc_mcr & SER_RTS) ? 1 : 0;
920         ucom_rts(sc, onoff);
921
922         return (0);
923 }
924
925 static void
926 ucom_cfg_line_state(struct usb_proc_msg *_task)
927 {
928         struct ucom_cfg_task *task = 
929             (struct ucom_cfg_task *)_task;
930         struct ucom_softc *sc = task->sc;
931         uint8_t notch_bits;
932         uint8_t any_bits;
933         uint8_t prev_value;
934         uint8_t last_value;
935         uint8_t mask;
936
937         if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
938                 return;
939         }
940
941         mask = 0;
942         /* compute callback mask */
943         if (sc->sc_callback->ucom_cfg_set_dtr)
944                 mask |= UCOM_LS_DTR;
945         if (sc->sc_callback->ucom_cfg_set_rts)
946                 mask |= UCOM_LS_RTS;
947         if (sc->sc_callback->ucom_cfg_set_break)
948                 mask |= UCOM_LS_BREAK;
949         if (sc->sc_callback->ucom_cfg_set_ring)
950                 mask |= UCOM_LS_RING;
951
952         /* compute the bits we are to program */
953         notch_bits = (sc->sc_pls_set & sc->sc_pls_clr) & mask;
954         any_bits = (sc->sc_pls_set | sc->sc_pls_clr) & mask;
955         prev_value = sc->sc_pls_curr ^ notch_bits;
956         last_value = sc->sc_pls_curr;
957
958         /* reset programmed line state */
959         sc->sc_pls_curr = 0;
960         sc->sc_pls_set = 0;
961         sc->sc_pls_clr = 0;
962
963         /* ensure that we don't lose any levels */
964         if (notch_bits & UCOM_LS_DTR)
965                 sc->sc_callback->ucom_cfg_set_dtr(sc,
966                     (prev_value & UCOM_LS_DTR) ? 1 : 0);
967         if (notch_bits & UCOM_LS_RTS)
968                 sc->sc_callback->ucom_cfg_set_rts(sc,
969                     (prev_value & UCOM_LS_RTS) ? 1 : 0);
970         if (notch_bits & UCOM_LS_BREAK)
971                 sc->sc_callback->ucom_cfg_set_break(sc,
972                     (prev_value & UCOM_LS_BREAK) ? 1 : 0);
973         if (notch_bits & UCOM_LS_RING)
974                 sc->sc_callback->ucom_cfg_set_ring(sc,
975                     (prev_value & UCOM_LS_RING) ? 1 : 0);
976
977         /* set last value */
978         if (any_bits & UCOM_LS_DTR)
979                 sc->sc_callback->ucom_cfg_set_dtr(sc,
980                     (last_value & UCOM_LS_DTR) ? 1 : 0);
981         if (any_bits & UCOM_LS_RTS)
982                 sc->sc_callback->ucom_cfg_set_rts(sc,
983                     (last_value & UCOM_LS_RTS) ? 1 : 0);
984         if (any_bits & UCOM_LS_BREAK)
985                 sc->sc_callback->ucom_cfg_set_break(sc,
986                     (last_value & UCOM_LS_BREAK) ? 1 : 0);
987         if (any_bits & UCOM_LS_RING)
988                 sc->sc_callback->ucom_cfg_set_ring(sc,
989                     (last_value & UCOM_LS_RING) ? 1 : 0);
990 }
991
992 static void
993 ucom_line_state(struct ucom_softc *sc,
994     uint8_t set_bits, uint8_t clear_bits)
995 {
996         UCOM_MTX_ASSERT(sc, MA_OWNED);
997
998         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
999                 return;
1000         }
1001
1002         DPRINTF("on=0x%02x, off=0x%02x\n", set_bits, clear_bits);
1003
1004         /* update current programmed line state */
1005         sc->sc_pls_curr |= set_bits;
1006         sc->sc_pls_curr &= ~clear_bits;
1007         sc->sc_pls_set |= set_bits;
1008         sc->sc_pls_clr |= clear_bits;
1009
1010         /* defer driver programming */
1011         ucom_queue_command(sc, ucom_cfg_line_state, NULL,
1012             &sc->sc_line_state_task[0].hdr, 
1013             &sc->sc_line_state_task[1].hdr);
1014 }
1015
1016 static void
1017 ucom_ring(struct ucom_softc *sc, uint8_t onoff)
1018 {
1019         DPRINTF("onoff = %d\n", onoff);
1020
1021         if (onoff)
1022                 ucom_line_state(sc, UCOM_LS_RING, 0);
1023         else
1024                 ucom_line_state(sc, 0, UCOM_LS_RING);
1025 }
1026
1027 static void
1028 ucom_break(struct ucom_softc *sc, uint8_t onoff)
1029 {
1030         DPRINTF("onoff = %d\n", onoff);
1031
1032         if (onoff)
1033                 ucom_line_state(sc, UCOM_LS_BREAK, 0);
1034         else
1035                 ucom_line_state(sc, 0, UCOM_LS_BREAK);
1036 }
1037
1038 static void
1039 ucom_dtr(struct ucom_softc *sc, uint8_t onoff)
1040 {
1041         DPRINTF("onoff = %d\n", onoff);
1042
1043         if (onoff)
1044                 ucom_line_state(sc, UCOM_LS_DTR, 0);
1045         else
1046                 ucom_line_state(sc, 0, UCOM_LS_DTR);
1047 }
1048
1049 static void
1050 ucom_rts(struct ucom_softc *sc, uint8_t onoff)
1051 {
1052         DPRINTF("onoff = %d\n", onoff);
1053
1054         if (onoff)
1055                 ucom_line_state(sc, UCOM_LS_RTS, 0);
1056         else
1057                 ucom_line_state(sc, 0, UCOM_LS_RTS);
1058 }
1059
1060 static void
1061 ucom_cfg_status_change(struct usb_proc_msg *_task)
1062 {
1063         struct ucom_cfg_task *task = 
1064             (struct ucom_cfg_task *)_task;
1065         struct ucom_softc *sc = task->sc;
1066         struct tty *tp;
1067         uint8_t new_msr;
1068         uint8_t new_lsr;
1069         uint8_t onoff;
1070         uint8_t lsr_delta;
1071
1072         tp = sc->sc_tty;
1073
1074         UCOM_MTX_ASSERT(sc, MA_OWNED);
1075
1076         if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1077                 return;
1078         }
1079         if (sc->sc_callback->ucom_cfg_get_status == NULL) {
1080                 return;
1081         }
1082         /* get status */
1083
1084         new_msr = 0;
1085         new_lsr = 0;
1086
1087         (sc->sc_callback->ucom_cfg_get_status) (sc, &new_lsr, &new_msr);
1088
1089         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1090                 /* TTY device closed */
1091                 return;
1092         }
1093         onoff = ((sc->sc_msr ^ new_msr) & SER_DCD);
1094         lsr_delta = (sc->sc_lsr ^ new_lsr);
1095
1096         sc->sc_msr = new_msr;
1097         sc->sc_lsr = new_lsr;
1098
1099         if (onoff) {
1100
1101                 onoff = (sc->sc_msr & SER_DCD) ? 1 : 0;
1102
1103                 DPRINTF("DCD changed to %d\n", onoff);
1104
1105                 ttydisc_modem(tp, onoff);
1106         }
1107
1108         if ((lsr_delta & ULSR_BI) && (sc->sc_lsr & ULSR_BI)) {
1109
1110                 DPRINTF("BREAK detected\n");
1111
1112                 ttydisc_rint(tp, 0, TRE_BREAK);
1113                 ttydisc_rint_done(tp);
1114         }
1115
1116         if ((lsr_delta & ULSR_FE) && (sc->sc_lsr & ULSR_FE)) {
1117
1118                 DPRINTF("Frame error detected\n");
1119
1120                 ttydisc_rint(tp, 0, TRE_FRAMING);
1121                 ttydisc_rint_done(tp);
1122         }
1123
1124         if ((lsr_delta & ULSR_PE) && (sc->sc_lsr & ULSR_PE)) {
1125
1126                 DPRINTF("Parity error detected\n");
1127
1128                 ttydisc_rint(tp, 0, TRE_PARITY);
1129                 ttydisc_rint_done(tp);
1130         }
1131 }
1132
1133 void
1134 ucom_status_change(struct ucom_softc *sc)
1135 {
1136         UCOM_MTX_ASSERT(sc, MA_OWNED);
1137
1138         if (sc->sc_flag & UCOM_FLAG_CONSOLE)
1139                 return;         /* not supported */
1140
1141         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1142                 return;
1143         }
1144         DPRINTF("\n");
1145
1146         ucom_queue_command(sc, ucom_cfg_status_change, NULL,
1147             &sc->sc_status_task[0].hdr,
1148             &sc->sc_status_task[1].hdr);
1149 }
1150
1151 static void
1152 ucom_cfg_param(struct usb_proc_msg *_task)
1153 {
1154         struct ucom_param_task *task = 
1155             (struct ucom_param_task *)_task;
1156         struct ucom_softc *sc = task->sc;
1157
1158         if (!(sc->sc_flag & UCOM_FLAG_LL_READY)) {
1159                 return;
1160         }
1161         if (sc->sc_callback->ucom_cfg_param == NULL) {
1162                 return;
1163         }
1164
1165         (sc->sc_callback->ucom_cfg_param) (sc, &task->termios_copy);
1166
1167         /* wait a little */
1168         usb_pause_mtx(sc->sc_lock, hz / 10);
1169 }
1170
1171 static int
1172 ucom_param(struct tty *tp, struct termios *t)
1173 {
1174         struct ucom_softc *sc = tty_softc(tp);
1175         uint8_t opened;
1176         int error;
1177
1178         UCOM_MTX_ASSERT(sc, MA_OWNED);
1179
1180         opened = 0;
1181         error = 0;
1182
1183         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1184
1185                 /* XXX the TTY layer should call "open()" first! */
1186                 /*
1187                  * Not quite: Its ordering is partly backwards, but
1188                  * some parameters must be set early in ttydev_open(),
1189                  * possibly before calling ttydevsw_open().
1190                  */
1191                 error = ucom_open(tp);
1192                 if (error) {
1193                         goto done;
1194                 }
1195                 opened = 1;
1196         }
1197         DPRINTF("sc = %p\n", sc);
1198
1199         /* Check requested parameters. */
1200         if (t->c_ispeed && (t->c_ispeed != t->c_ospeed)) {
1201                 /* XXX c_ospeed == 0 is perfectly valid. */
1202                 DPRINTF("mismatch ispeed and ospeed\n");
1203                 error = EINVAL;
1204                 goto done;
1205         }
1206         t->c_ispeed = t->c_ospeed;
1207
1208         if (sc->sc_callback->ucom_pre_param) {
1209                 /* Let the lower layer verify the parameters */
1210                 error = (sc->sc_callback->ucom_pre_param) (sc, t);
1211                 if (error) {
1212                         DPRINTF("callback error = %d\n", error);
1213                         goto done;
1214                 }
1215         }
1216
1217         /* Disable transfers */
1218         sc->sc_flag &= ~UCOM_FLAG_GP_DATA;
1219
1220         /* Queue baud rate programming command first */
1221         ucom_queue_command(sc, ucom_cfg_param, t,
1222             &sc->sc_param_task[0].hdr,
1223             &sc->sc_param_task[1].hdr);
1224
1225         /* Queue transfer enable command last */
1226         ucom_queue_command(sc, ucom_cfg_start_transfers, NULL,
1227             &sc->sc_start_task[0].hdr, 
1228             &sc->sc_start_task[1].hdr);
1229
1230         if (t->c_cflag & CRTS_IFLOW) {
1231                 sc->sc_flag |= UCOM_FLAG_RTS_IFLOW;
1232         } else if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW) {
1233                 sc->sc_flag &= ~UCOM_FLAG_RTS_IFLOW;
1234                 ucom_modem(tp, SER_RTS, 0);
1235         }
1236 done:
1237         if (error) {
1238                 if (opened) {
1239                         ucom_close(tp);
1240                 }
1241         }
1242         return (error);
1243 }
1244
1245 static void
1246 ucom_outwakeup(struct tty *tp)
1247 {
1248         struct ucom_softc *sc = tty_softc(tp);
1249
1250         UCOM_MTX_ASSERT(sc, MA_OWNED);
1251
1252         DPRINTF("sc = %p\n", sc);
1253
1254         if (!(sc->sc_flag & UCOM_FLAG_HL_READY)) {
1255                 /* The higher layer is not ready */
1256                 return;
1257         }
1258         ucom_start_transfers(sc);
1259 }
1260
1261 /*------------------------------------------------------------------------*
1262  *      ucom_get_data
1263  *
1264  * Return values:
1265  * 0: No data is available.
1266  * Else: Data is available.
1267  *------------------------------------------------------------------------*/
1268 uint8_t
1269 ucom_get_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1270     uint32_t offset, uint32_t len, uint32_t *actlen)
1271 {
1272         struct usb_page_search res;
1273         struct tty *tp = sc->sc_tty;
1274         uint32_t cnt;
1275         uint32_t offset_orig;
1276
1277         UCOM_MTX_ASSERT(sc, MA_OWNED);
1278
1279         if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1280                 unsigned int temp;
1281
1282                 /* get total TX length */
1283
1284                 temp = ucom_cons_tx_high - ucom_cons_tx_low;
1285                 temp %= UCOM_CONS_BUFSIZE;
1286
1287                 /* limit TX length */
1288
1289                 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_tx_low))
1290                         temp = (UCOM_CONS_BUFSIZE - ucom_cons_tx_low);
1291
1292                 if (temp > len)
1293                         temp = len;
1294
1295                 /* copy in data */
1296
1297                 usbd_copy_in(pc, offset, ucom_cons_tx_buf + ucom_cons_tx_low, temp);
1298
1299                 /* update counters */
1300
1301                 ucom_cons_tx_low += temp;
1302                 ucom_cons_tx_low %= UCOM_CONS_BUFSIZE;
1303
1304                 /* store actual length */
1305
1306                 *actlen = temp;
1307
1308                 return (temp ? 1 : 0);
1309         }
1310
1311         if (tty_gone(tp) ||
1312             !(sc->sc_flag & UCOM_FLAG_GP_DATA)) {
1313                 actlen[0] = 0;
1314                 return (0);             /* multiport device polling */
1315         }
1316         offset_orig = offset;
1317
1318         while (len != 0) {
1319
1320                 usbd_get_page(pc, offset, &res);
1321
1322                 if (res.length > len) {
1323                         res.length = len;
1324                 }
1325                 /* copy data directly into USB buffer */
1326                 cnt = ttydisc_getc(tp, res.buffer, res.length);
1327
1328                 offset += cnt;
1329                 len -= cnt;
1330
1331                 if (cnt < res.length) {
1332                         /* end of buffer */
1333                         break;
1334                 }
1335         }
1336
1337         actlen[0] = offset - offset_orig;
1338
1339         DPRINTF("cnt=%d\n", actlen[0]);
1340
1341         if (actlen[0] == 0) {
1342                 return (0);
1343         }
1344         return (1);
1345 }
1346
1347 void
1348 ucom_put_data(struct ucom_softc *sc, struct usb_page_cache *pc,
1349     uint32_t offset, uint32_t len)
1350 {
1351         struct usb_page_search res;
1352         struct tty *tp = sc->sc_tty;
1353         char *buf;
1354         uint32_t cnt;
1355
1356         UCOM_MTX_ASSERT(sc, MA_OWNED);
1357
1358         if (sc->sc_flag & UCOM_FLAG_CONSOLE) {
1359                 unsigned int temp;
1360
1361                 /* get maximum RX length */
1362
1363                 temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_rx_high + ucom_cons_rx_low;
1364                 temp %= UCOM_CONS_BUFSIZE;
1365
1366                 /* limit RX length */
1367
1368                 if (temp > (UCOM_CONS_BUFSIZE - ucom_cons_rx_high))
1369                         temp = (UCOM_CONS_BUFSIZE - ucom_cons_rx_high);
1370
1371                 if (temp > len)
1372                         temp = len;
1373
1374                 /* copy out data */
1375
1376                 usbd_copy_out(pc, offset, ucom_cons_rx_buf + ucom_cons_rx_high, temp);
1377
1378                 /* update counters */
1379
1380                 ucom_cons_rx_high += temp;
1381                 ucom_cons_rx_high %= UCOM_CONS_BUFSIZE;
1382
1383                 return;
1384         }
1385
1386         if (tty_gone(tp))
1387                 return;                 /* multiport device polling */
1388
1389         if (len == 0)
1390                 return;                 /* no data */
1391
1392         /* set a flag to prevent recursation ? */
1393
1394         while (len > 0) {
1395
1396                 usbd_get_page(pc, offset, &res);
1397
1398                 if (res.length > len) {
1399                         res.length = len;
1400                 }
1401                 len -= res.length;
1402                 offset += res.length;
1403
1404                 /* pass characters to tty layer */
1405
1406                 buf = res.buffer;
1407                 cnt = res.length;
1408
1409                 /* first check if we can pass the buffer directly */
1410
1411                 if (ttydisc_can_bypass(tp)) {
1412
1413                         /* clear any jitter buffer */
1414                         sc->sc_jitterbuf_in = 0;
1415                         sc->sc_jitterbuf_out = 0;
1416
1417                         if (ttydisc_rint_bypass(tp, buf, cnt) != cnt) {
1418                                 DPRINTF("tp=%p, data lost\n", tp);
1419                         }
1420                         continue;
1421                 }
1422                 /* need to loop */
1423
1424                 for (cnt = 0; cnt != res.length; cnt++) {
1425                         if (sc->sc_jitterbuf_in != sc->sc_jitterbuf_out ||
1426                             ttydisc_rint(tp, buf[cnt], 0) == -1) {
1427                                 uint16_t end;
1428                                 uint16_t pos;
1429
1430                                 pos = sc->sc_jitterbuf_in;
1431                                 end = sc->sc_jitterbuf_out +
1432                                     UCOM_JITTERBUF_SIZE - 1;
1433                                 if (end >= UCOM_JITTERBUF_SIZE)
1434                                         end -= UCOM_JITTERBUF_SIZE;
1435
1436                                 for (; cnt != res.length; cnt++) {
1437                                         if (pos == end)
1438                                                 break;
1439                                         sc->sc_jitterbuf[pos] = buf[cnt];
1440                                         pos++;
1441                                         if (pos >= UCOM_JITTERBUF_SIZE)
1442                                                 pos -= UCOM_JITTERBUF_SIZE;
1443                                 }
1444
1445                                 sc->sc_jitterbuf_in = pos;
1446
1447                                 /* set RTS in async fashion */
1448                                 if (sc->sc_flag & UCOM_FLAG_RTS_IFLOW)
1449                                         ucom_rts(sc, 1);
1450
1451                                 DPRINTF("tp=%p, lost %d "
1452                                     "chars\n", tp, res.length - cnt);
1453                                 break;
1454                         }
1455                 }
1456         }
1457         ttydisc_rint_done(tp);
1458 }
1459
1460 static void
1461 ucom_free(void *xsc)
1462 {
1463         struct ucom_softc *sc = xsc;
1464
1465         if (sc->sc_callback->ucom_free != NULL)
1466                 sc->sc_callback->ucom_free(sc);
1467         else
1468                 ucom_unref(sc->sc_super);
1469
1470         lockmgr(&ucom_lock, LK_EXCLUSIVE);
1471         ucom_close_refs--;
1472         lockmgr(&ucom_lock, LK_RELEASE);
1473 }
1474
1475 static cn_probe_t ucom_cnprobe;
1476 static cn_init_t ucom_cninit;
1477 static cn_term_t ucom_cnterm;
1478 static cn_getc_t ucom_cngetc;
1479 static cn_putc_t ucom_cnputc;
1480 static cn_grab_t ucom_cngrab;
1481 static cn_ungrab_t ucom_cnungrab;
1482
1483 CONSOLE_DRIVER(ucom);
1484
1485 static void
1486 ucom_cnprobe(struct consdev  *cp)
1487 {
1488         if (ucom_cons_unit != -1)
1489                 cp->cn_pri = CN_NORMAL;
1490         else
1491                 cp->cn_pri = CN_DEAD;
1492
1493         strlcpy(cp->cn_name, "ucom", sizeof(cp->cn_name));
1494 }
1495
1496 static void
1497 ucom_cninit(struct consdev  *cp)
1498 {
1499 }
1500
1501 static void
1502 ucom_cnterm(struct consdev  *cp)
1503 {
1504 }
1505
1506 static void
1507 ucom_cngrab(struct consdev *cp)
1508 {
1509 }
1510
1511 static void
1512 ucom_cnungrab(struct consdev *cp)
1513 {
1514 }
1515
1516 static int
1517 ucom_cngetc(struct consdev *cd)
1518 {
1519         struct ucom_softc *sc = ucom_cons_softc;
1520         int c;
1521
1522         if (sc == NULL)
1523                 return (-1);
1524
1525         UCOM_MTX_LOCK(sc);
1526
1527         if (ucom_cons_rx_low != ucom_cons_rx_high) {
1528                 c = ucom_cons_rx_buf[ucom_cons_rx_low];
1529                 ucom_cons_rx_low ++;
1530                 ucom_cons_rx_low %= UCOM_CONS_BUFSIZE;
1531         } else {
1532                 c = -1;
1533         }
1534
1535         /* start USB transfers */
1536         ucom_outwakeup(sc->sc_tty);
1537
1538         UCOM_MTX_UNLOCK(sc);
1539
1540         /* poll if necessary */
1541         if (kdb_active && sc->sc_callback->ucom_poll)
1542                 (sc->sc_callback->ucom_poll) (sc);
1543
1544         return (c);
1545 }
1546
1547 static void
1548 ucom_cnputc(struct consdev *cd, int c)
1549 {
1550         struct ucom_softc *sc = ucom_cons_softc;
1551         unsigned int temp;
1552
1553         if (sc == NULL)
1554                 return;
1555
1556  repeat:
1557
1558         UCOM_MTX_LOCK(sc);
1559
1560         /* compute maximum TX length */
1561
1562         temp = (UCOM_CONS_BUFSIZE - 1) - ucom_cons_tx_high + ucom_cons_tx_low;
1563         temp %= UCOM_CONS_BUFSIZE;
1564
1565         if (temp) {
1566                 ucom_cons_tx_buf[ucom_cons_tx_high] = c;
1567                 ucom_cons_tx_high ++;
1568                 ucom_cons_tx_high %= UCOM_CONS_BUFSIZE;
1569         }
1570
1571         /* start USB transfers */
1572         ucom_outwakeup(sc->sc_tty);
1573
1574         UCOM_MTX_UNLOCK(sc);
1575
1576         /* poll if necessary */
1577         if (kdb_active && sc->sc_callback->ucom_poll) {
1578                 (sc->sc_callback->ucom_poll) (sc);
1579                 /* simple flow control */
1580                 if (temp == 0)
1581                         goto repeat;
1582         }
1583 }
1584
1585 /*------------------------------------------------------------------------*
1586  *      ucom_ref
1587  *
1588  * This function will increment the super UCOM reference count.
1589  *------------------------------------------------------------------------*/
1590 void
1591 ucom_ref(struct ucom_super_softc *ssc)
1592 {
1593         lockmgr(&ucom_lock, LK_EXCLUSIVE);
1594         ssc->sc_refs++;
1595         lockmgr(&ucom_lock, LK_RELEASE);
1596 }
1597
1598 /*------------------------------------------------------------------------*
1599  *      ucom_free_unit
1600  *
1601  * This function will free the super UCOM's allocated unit
1602  * number. This function can be called on a zero-initialized
1603  * structure. This function can be called multiple times.
1604  *------------------------------------------------------------------------*/
1605 static void
1606 ucom_free_unit(struct ucom_super_softc *ssc)
1607 {
1608         if (!(ssc->sc_flag & UCOM_FLAG_FREE_UNIT))
1609                 return;
1610
1611         ucom_unit_free(ssc->sc_unit);
1612
1613         ssc->sc_flag &= ~UCOM_FLAG_FREE_UNIT;
1614 }
1615
1616 /*------------------------------------------------------------------------*
1617  *      ucom_unref
1618  *
1619  * This function will decrement the super UCOM reference count.
1620  *
1621  * Return values:
1622  * 0: UCOM structures are still referenced.
1623  * Else: UCOM structures are no longer referenced.
1624  *------------------------------------------------------------------------*/
1625 int
1626 ucom_unref(struct ucom_super_softc *ssc)
1627 {
1628         int retval;
1629
1630         lockmgr(&ucom_lock, LK_EXCLUSIVE);
1631         retval = (ssc->sc_refs < 2);
1632         ssc->sc_refs--;
1633         lockmgr(&ucom_lock, LK_RELEASE);
1634
1635         if (retval)
1636                 ucom_free_unit(ssc);
1637
1638         return (retval);
1639 }
1640
1641 #if defined(GDB)
1642
1643 #include <gdb/gdb.h>
1644
1645 static gdb_probe_f ucom_gdbprobe;
1646 static gdb_init_f ucom_gdbinit;
1647 static gdb_term_f ucom_gdbterm;
1648 static gdb_getc_f ucom_gdbgetc;
1649 static gdb_putc_f ucom_gdbputc;
1650
1651 GDB_DBGPORT(sio, ucom_gdbprobe, ucom_gdbinit, ucom_gdbterm, ucom_gdbgetc, ucom_gdbputc);
1652
1653 static int
1654 ucom_gdbprobe(void)
1655 {
1656         return ((ucom_cons_softc != NULL) ? 0 : -1);
1657 }
1658
1659 static void
1660 ucom_gdbinit(void)
1661 {
1662 }
1663
1664 static void
1665 ucom_gdbterm(void)
1666 {
1667 }
1668
1669 static void
1670 ucom_gdbputc(int c)
1671 {
1672         ucom_cnputc(NULL, c);
1673 }
1674
1675 static int
1676 ucom_gdbgetc(void)
1677 {
1678         return (ucom_cngetc(NULL));
1679 }
1680
1681 #endif