Annotation of Gnu-Mach/device/chario.c, revision 1.1.1.3

1.1.1.2   root        1: /*
1.1       root        2:  * Mach Operating System
                      3:  * Copyright (c) 1993-1988 Carnegie Mellon University
                      4:  * All Rights Reserved.
1.1.1.2   root        5:  *
1.1       root        6:  * Permission to use, copy, modify and distribute this software and its
                      7:  * documentation is hereby granted, provided that both the copyright
                      8:  * notice and this permission notice appear in all copies of the
                      9:  * software, derivative works or modified versions, and any portions
                     10:  * thereof, and that both notices appear in supporting documentation.
1.1.1.2   root       11:  *
1.1       root       12:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
                     13:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
                     14:  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1.1.1.2   root       15:  *
1.1       root       16:  * Carnegie Mellon requests users of this software to return to
1.1.1.2   root       17:  *
1.1       root       18:  *  Software Distribution Coordinator  or  [email protected]
                     19:  *  School of Computer Science
                     20:  *  Carnegie Mellon University
                     21:  *  Pittsburgh PA 15213-3890
1.1.1.2   root       22:  *
1.1       root       23:  * any improvements or extensions that they make and grant Carnegie Mellon
                     24:  * the rights to redistribute these changes.
                     25:  */
                     26: /*
                     27:  *     Author: David B. Golub, Carnegie Mellon University
                     28:  *     Date:   8/88
                     29:  *
                     30:  *     TTY io.
                     31:  *     Compatibility with old TTY device drivers.
                     32:  */
                     33: 
                     34: #include <mach/kern_return.h>
                     35: #include <mach/mig_errors.h>
                     36: #include <mach/vm_param.h>
                     37: #include <machine/machspl.h>           /* spl definitions */
                     38: 
                     39: #include <ipc/ipc_port.h>
                     40: 
                     41: #include <kern/lock.h>
                     42: #include <kern/queue.h>
                     43: 
                     44: #include <vm/vm_map.h>
                     45: #include <vm/vm_kern.h>
                     46: 
                     47: #include <device/device_types.h>
                     48: #include <device/io_req.h>
                     49: #include <device/ds_routines.h>
                     50: #include "device_reply.h"
                     51: 
                     52: #include <device/tty.h>
                     53: 
                     54: /* If you change these, check that tty_outq_size and tty_inq_size
                     55:  * is greater than largest tthiwat entry.
                     56:  */
1.1.1.3 ! root       57: short  tthiwat[NSPEEDS] =
        !            58:    { 100,100,100,100,100,100,100,200,200,400,400,400,650,650,1300,2000,
        !            59:      2000,2000 };
        !            60: short  ttlowat[NSPEEDS] =
        !            61:    {  30, 30, 30, 30, 30, 30, 30, 50, 50,120,120,120,125,125, 125, 125,
        !            62:      125,125 };
1.1       root       63: 
                     64: /*
                     65:  * forward declarations
                     66:  */
                     67: void   queue_delayed_reply(
                     68:        queue_t, io_req_t, boolean_t (*)(io_req_t));
                     69: void   tty_output(struct tty *);
                     70: void   tty_flush(struct tty *, int);
                     71: boolean_t char_open_done(io_req_t);
                     72: boolean_t char_read_done(io_req_t);
                     73: boolean_t char_write_done(io_req_t);
1.1.1.2   root       74: void   ttstart(struct tty *tp);
1.1       root       75: 
                     76: /*
                     77:  * Fake 'line discipline' switch for the benefit of old code
                     78:  * that wants to call through it.
                     79:  */
                     80: struct ldisc_switch    linesw[] = {
                     81:        {
                     82:            char_read,
                     83:            char_write,
                     84:            ttyinput,
                     85:            ttymodem,
                     86:            tty_output
                     87:        }
                     88: };
                     89: 
                     90: /*
                     91:  * Sizes for input and output circular buffers.
                     92:  */
                     93: int    tty_inq_size = 4096;    /* big nuf */
                     94: int    tty_outq_size = 2048;   /* Must be bigger that tthiwat */
                     95: int    pdma_default = 1;       /* turn pseudo dma on by default */
                     96: 
                     97: /*
1.1.1.2   root       98:  * compute pseudo-dma tables
1.1       root       99:  */
                    100: 
                    101: int pdma_timeouts[NSPEEDS]; /* how many ticks in timeout */
                    102: int pdma_water_mark[NSPEEDS];
                    103: 
                    104: 
                    105: void chario_init(void)
                    106: {
                    107:   /* the basic idea with the timeouts is two allow enough
                    108:      time for a character to show up if data is coming in at full data rate
                    109:      plus a little slack. 2 ticks is considered slack
                    110:      Below 300 baud we just glob a character at a time */
                    111: #define _PR(x) ((hz/x) + 2)
                    112: 
                    113:   int i;
                    114: 
                    115:   for (i = B0; i < B300; i++)
                    116:     pdma_timeouts[i] = 0;
1.1.1.2   root      117: 
1.1       root      118:   pdma_timeouts[B300] = _PR(30);
                    119:   pdma_timeouts[B600] = _PR(60);
                    120:   pdma_timeouts[B1200] = _PR(120);
                    121:   pdma_timeouts[B1800] = _PR(180);
                    122:   pdma_timeouts[B2400] = _PR(240);
                    123:   pdma_timeouts[B4800] = _PR(480);
                    124:   pdma_timeouts[B9600] = _PR(960);
                    125:   pdma_timeouts[EXTA]  = _PR(1440); /* >14400 baud */
                    126:   pdma_timeouts[EXTB]  = _PR(1920); /* >19200 baud */
1.1.1.3 ! root      127:   pdma_timeouts[B57600] = _PR(5760);
        !           128:   pdma_timeouts[B115200] = _PR(11520);
1.1       root      129: 
                    130:   for (i = B0; i < B300; i++)
                    131:     pdma_water_mark[i] = 0;
                    132: 
                    133:   /* for the slow speeds, we try to buffer 0.02 of the baud rate
                    134:      (20% of the character rate). For the faster lines,
                    135:      we try to buffer 1/2 the input queue size */
                    136: 
                    137: #undef _PR
                    138: #define _PR(x) (0.20 * x)
                    139: 
                    140:   pdma_water_mark[B300] = _PR(120);
                    141:   pdma_water_mark[B600] = _PR(120);
                    142:   pdma_water_mark[B1200] = _PR(120);
                    143:   pdma_water_mark[B1800] = _PR(180);
                    144:   pdma_water_mark[B2400] = _PR(240);
                    145:   pdma_water_mark[B4800] = _PR(480);
                    146:   i = tty_inq_size/2;
                    147:   pdma_water_mark[B9600] = i;
                    148:   pdma_water_mark[EXTA]  = i; /* >14400 baud */
                    149:   pdma_water_mark[EXTB]  = i; /* >19200 baud */
1.1.1.3 ! root      150:   pdma_water_mark[B57600] = i;
        !           151:   pdma_water_mark[B115200] = i;
1.1       root      152: 
1.1.1.2   root      153:   return;
1.1       root      154: }
                    155: 
                    156: /*
                    157:  * Open TTY, waiting for CARR_ON.
                    158:  * No locks may be held.
                    159:  * May run on any CPU.
                    160:  */
                    161: io_return_t char_open(
                    162:        int             dev,
                    163:        struct tty *    tp,
                    164:        dev_mode_t      mode,
                    165:        io_req_t        ior)
                    166: {
                    167:        spl_t   s;
                    168:        io_return_t     rc = D_SUCCESS;
                    169: 
                    170:        s = spltty();
                    171:        simple_lock(&tp->t_lock);
                    172: 
                    173:        tp->t_dev = dev;
                    174: 
                    175:        if (tp->t_mctl)
                    176:                (*tp->t_mctl)(tp, TM_DTR, DMSET);
                    177: 
                    178:        if (pdma_default)
                    179:          tp->t_state |= TS_MIN;
                    180: 
                    181:        if ((tp->t_state & TS_CARR_ON) == 0) {
                    182:            /*
                    183:             * No carrier.
                    184:             */
                    185:            if (mode & D_NODELAY) {
                    186:                tp->t_state |= TS_ONDELAY;
                    187:            }
                    188:            else {
                    189:                /*
                    190:                 * Don`t return from open until carrier detected.
                    191:                 */
                    192:                tp->t_state |= TS_WOPEN;
                    193: 
                    194:                ior->io_dev_ptr = (char *)tp;
                    195: 
                    196:                queue_delayed_reply(&tp->t_delayed_open, ior, char_open_done);
                    197:                rc = D_IO_QUEUED;
                    198:                goto out;
                    199:            }
                    200:        }
                    201:        tp->t_state |= TS_ISOPEN;
                    202:        if (tp->t_mctl)
                    203:                (*tp->t_mctl)(tp, TM_RTS, DMBIS);
                    204: out:
                    205:        simple_unlock(&tp->t_lock);
                    206:        splx(s);
                    207:        return rc;
                    208: }
                    209: 
                    210: /*
                    211:  * Retry wait for CARR_ON for open.
                    212:  * No locks may be held.
                    213:  * May run on any CPU.
                    214:  */
                    215: boolean_t char_open_done(
                    216:        io_req_t        ior)
                    217: {
                    218:        register struct tty *tp = (struct tty *)ior->io_dev_ptr;
                    219:        spl_t s = spltty();
                    220: 
                    221:        simple_lock(&tp->t_lock);
                    222:        if ((tp->t_state & TS_ISOPEN) == 0) {
                    223:            queue_delayed_reply(&tp->t_delayed_open, ior, char_open_done);
                    224:            simple_unlock(&tp->t_lock);
                    225:            splx(s);
                    226:            return FALSE;
                    227:        }
                    228: 
                    229:        tp->t_state |= TS_ISOPEN;
                    230:        tp->t_state &= ~TS_WOPEN;
                    231: 
                    232:        if (tp->t_mctl)
                    233:                (*tp->t_mctl)(tp, TM_RTS, DMBIS);
                    234: 
                    235:        simple_unlock(&tp->t_lock);
                    236:        splx(s);
                    237: 
                    238:        ior->io_error = D_SUCCESS;
                    239:        (void) ds_open_done(ior);
                    240:        return TRUE;
                    241: }
                    242: 
                    243: boolean_t tty_close_open_reply(
                    244:        io_req_t        ior)
                    245: {
                    246:        ior->io_error = D_DEVICE_DOWN;
                    247:        (void) ds_open_done(ior);
                    248:        return TRUE;
                    249: }
                    250: 
                    251: /*
                    252:  * Write to TTY.
                    253:  * No locks may be held.
                    254:  * Calls device start routine; must already be on master if
                    255:  * device needs to run on master.
                    256:  */
                    257: io_return_t char_write(
                    258:        register struct tty *   tp,
                    259:        register io_req_t       ior)
                    260: {
                    261:        spl_t           s;
                    262:        register int    count;
                    263:        register char   *data;
                    264:        vm_offset_t     addr;
                    265:        io_return_t     rc = D_SUCCESS;
                    266: 
                    267:        data  = ior->io_data;
                    268:        count = ior->io_count;
                    269:        if (count == 0)
                    270:            return rc;
                    271: 
                    272:        if (!(ior->io_op & IO_INBAND)) {
                    273:            /*
                    274:             * Copy out-of-line data into kernel address space.
                    275:             * Since data is copied as page list, it will be
                    276:             * accessible.
                    277:             */
                    278:            vm_map_copy_t copy = (vm_map_copy_t) data;
                    279:            kern_return_t kr;
                    280: 
                    281:            kr = vm_map_copyout(device_io_map, &addr, copy);
                    282:            if (kr != KERN_SUCCESS)
                    283:                return kr;
                    284:            data = (char *) addr;
                    285:        }
                    286: 
                    287:        /*
                    288:         * Check for tty operating.
                    289:         */
                    290:        s = spltty();
                    291:        simple_lock(&tp->t_lock);
                    292: 
                    293:        if ((tp->t_state & TS_CARR_ON) == 0) {
                    294: 
                    295:            if ((tp->t_state & TS_ONDELAY) == 0) {
                    296:                /*
                    297:                 * No delayed writes - tell caller that device is down
                    298:                 */
                    299:                rc = D_IO_ERROR;
                    300:                goto out;
                    301:            }
                    302: 
                    303:            if (ior->io_mode & D_NOWAIT) {
                    304:                rc = D_WOULD_BLOCK;
                    305:                goto out;
                    306:            }
                    307:        }
                    308: 
                    309:        /*
                    310:         * Copy data into the output buffer.
                    311:         * Report the amount not copied.
                    312:         */
                    313: 
                    314:        ior->io_residual = b_to_q(data, count, &tp->t_outq);
                    315: 
                    316:        /*
                    317:         * Start hardware output.
                    318:         */
                    319: 
                    320:        tp->t_state &= ~TS_TTSTOP;
                    321:        tty_output(tp);
                    322: 
                    323:        if (tp->t_outq.c_cc > TTHIWAT(tp) ||
                    324:            (tp->t_state & TS_CARR_ON) == 0) {
                    325: 
                    326:            /*
                    327:             * Do not send reply until some characters have been sent.
                    328:             */
                    329:            ior->io_dev_ptr = (char *)tp;
                    330:            queue_delayed_reply(&tp->t_delayed_write, ior, char_write_done);
                    331: 
                    332:            rc = D_IO_QUEUED;
                    333:        }
                    334: out:
                    335:        simple_unlock(&tp->t_lock);
                    336:        splx(s);
                    337: 
                    338:        if (!(ior->io_op & IO_INBAND))
                    339:            (void) vm_deallocate(device_io_map, addr, ior->io_count);
                    340:        return rc;
                    341: }
                    342: 
                    343: /*
                    344:  * Retry wait for output queue emptied, for write.
                    345:  * No locks may be held.
                    346:  * May run on any CPU.
                    347:  */
                    348: boolean_t char_write_done(
                    349:        register io_req_t       ior)
                    350: {
                    351:        register struct tty *tp = (struct tty *)ior->io_dev_ptr;
                    352:        register spl_t s = spltty();
                    353: 
                    354:        simple_lock(&tp->t_lock);
                    355:        if (tp->t_outq.c_cc > TTHIWAT(tp) ||
                    356:            (tp->t_state & TS_CARR_ON) == 0) {
                    357: 
                    358:            queue_delayed_reply(&tp->t_delayed_write, ior, char_write_done);
                    359:            simple_unlock(&tp->t_lock);
                    360:            splx(s);
                    361:            return FALSE;
                    362:        }
                    363:        simple_unlock(&tp->t_lock);
                    364:        splx(s);
                    365: 
                    366:        if (IP_VALID(ior->io_reply_port)) {
                    367:          (void) (*((ior->io_op & IO_INBAND) ?
                    368:                    ds_device_write_reply_inband :
                    369:                    ds_device_write_reply))(ior->io_reply_port,
                    370:                                            ior->io_reply_port_type,
                    371:                                            ior->io_error,
                    372:                                            (int) (ior->io_total -
                    373:                                                   ior->io_residual));
                    374:        }
                    375:        mach_device_deallocate(ior->io_device);
                    376:        return TRUE;
                    377: }
                    378: 
                    379: boolean_t tty_close_write_reply(
                    380:        register io_req_t       ior)
                    381: {
                    382:        ior->io_residual = ior->io_count;
                    383:        ior->io_error = D_DEVICE_DOWN;
                    384:        (void) ds_write_done(ior);
                    385:        return TRUE;
                    386: }
                    387: 
                    388: /*
                    389:  * Read from TTY.
                    390:  * No locks may be held.
                    391:  * May run on any CPU - does not talk to device driver.
                    392:  */
                    393: io_return_t char_read(
                    394:        register struct tty *tp,
                    395:        register io_req_t ior)
                    396: {
                    397:        spl_t           s;
                    398:        kern_return_t   rc;
                    399: 
                    400:        /*
                    401:         * Allocate memory for read buffer.
                    402:         */
                    403:        rc = device_read_alloc(ior, (vm_size_t)ior->io_count);
                    404:        if (rc != KERN_SUCCESS)
                    405:            return rc;
                    406: 
                    407:        s = spltty();
                    408:        simple_lock(&tp->t_lock);
                    409:        if ((tp->t_state & TS_CARR_ON) == 0) {
                    410: 
                    411:            if ((tp->t_state & TS_ONDELAY) == 0) {
                    412:                /*
                    413:                 * No delayed writes - tell caller that device is down
                    414:                 */
                    415:                rc = D_IO_ERROR;
                    416:                goto out;
                    417:            }
                    418: 
                    419:            if (ior->io_mode & D_NOWAIT) {
                    420:                rc = D_WOULD_BLOCK;
                    421:                goto out;
                    422:            }
                    423: 
                    424:        }
                    425: 
                    426:        if (tp->t_inq.c_cc <= 0 ||
                    427:            (tp->t_state & TS_CARR_ON) == 0) {
                    428: 
                    429:            ior->io_dev_ptr = (char *)tp;
                    430:            queue_delayed_reply(&tp->t_delayed_read, ior, char_read_done);
                    431:            rc = D_IO_QUEUED;
                    432:            goto out;
                    433:        }
1.1.1.2   root      434: 
1.1       root      435:        ior->io_residual = ior->io_count - q_to_b(&tp->t_inq,
                    436:                                                  ior->io_data,
                    437:                                                  (int)ior->io_count);
                    438:        if (tp->t_state & TS_RTS_DOWN) {
                    439:            (*tp->t_mctl)(tp, TM_RTS, DMBIS);
                    440:            tp->t_state &= ~TS_RTS_DOWN;
                    441:        }
                    442: 
                    443:     out:
                    444:        simple_unlock(&tp->t_lock);
                    445:        splx(s);
                    446:        return rc;
                    447: }
                    448: 
                    449: /*
                    450:  * Retry wait for characters, for read.
                    451:  * No locks may be held.
                    452:  * May run on any CPU - does not talk to device driver.
                    453:  */
                    454: boolean_t char_read_done(
                    455:        register io_req_t       ior)
                    456: {
                    457:        register struct tty *tp = (struct tty *)ior->io_dev_ptr;
                    458:        register spl_t s = spltty();
                    459: 
                    460:        simple_lock(&tp->t_lock);
                    461: 
                    462:        if (tp->t_inq.c_cc <= 0 ||
                    463:            (tp->t_state & TS_CARR_ON) == 0) {
                    464: 
                    465:            queue_delayed_reply(&tp->t_delayed_read, ior, char_read_done);
                    466:            simple_unlock(&tp->t_lock);
                    467:            splx(s);
                    468:            return FALSE;
                    469:        }
                    470: 
                    471:        ior->io_residual = ior->io_count - q_to_b(&tp->t_inq,
                    472:                                                  ior->io_data,
                    473:                                                  (int)ior->io_count);
                    474:        if (tp->t_state & TS_RTS_DOWN) {
                    475:            (*tp->t_mctl)(tp, TM_RTS, DMBIS);
                    476:            tp->t_state &= ~TS_RTS_DOWN;
                    477:        }
                    478: 
                    479:        simple_unlock(&tp->t_lock);
                    480:        splx(s);
                    481: 
                    482:        (void) ds_read_done(ior);
                    483:        return TRUE;
                    484: }
                    485: 
                    486: boolean_t tty_close_read_reply(
                    487:        register io_req_t       ior)
                    488: {
                    489:        ior->io_residual = ior->io_count;
                    490:        ior->io_error = D_DEVICE_DOWN;
                    491:        (void) ds_read_done(ior);
                    492:        return TRUE;
                    493: }
                    494: 
                    495: /*
                    496:  * Close the tty.
                    497:  * Tty must be locked (at spltty).
                    498:  * Iff modem control should run on master.
                    499:  */
                    500: void ttyclose(
                    501:        register struct tty *tp)
                    502: {
                    503:        register io_req_t       ior;
                    504: 
                    505:        /*
                    506:         * Flush the read and write queues.  Signal
                    507:         * the open queue so that those waiting for open
                    508:         * to complete will see that the tty is closed.
                    509:         */
                    510:        while ((ior = (io_req_t)dequeue_head(&tp->t_delayed_read)) != 0) {
                    511:            ior->io_done = tty_close_read_reply;
                    512:            iodone(ior);
                    513:        }
                    514:        while ((ior = (io_req_t)dequeue_head(&tp->t_delayed_write)) != 0) {
                    515:            ior->io_done = tty_close_write_reply;
                    516:            iodone(ior);
                    517:        }
                    518:        while ((ior = (io_req_t)dequeue_head(&tp->t_delayed_open)) != 0) {
                    519:            ior->io_done = tty_close_open_reply;
                    520:            iodone(ior);
                    521:        }
                    522: 
                    523:        /* Close down modem */
                    524:        if (tp->t_mctl) {
                    525:                (*tp->t_mctl)(tp, TM_BRK|TM_RTS, DMBIC);
                    526:                if ((tp->t_state&(TS_HUPCLS|TS_WOPEN)) || (tp->t_state&TS_ISOPEN)==0)
                    527:                        (*tp->t_mctl)(tp, TM_HUP, DMSET);
                    528:        }
                    529: 
                    530:        /* only save buffering bit, and carrier */
                    531:        tp->t_state = tp->t_state & (TS_MIN|TS_CARR_ON);
                    532: }
                    533: 
                    534: /*
                    535:  * Port-death routine to clean up reply messages.
                    536:  */
                    537: boolean_t
                    538: tty_queue_clean(
                    539:        queue_t         q,
                    540:        ipc_port_t      port,
                    541:        boolean_t       (*routine)(io_req_t) )
                    542: {
                    543:        register io_req_t       ior;
                    544: 
                    545:        ior = (io_req_t)queue_first(q);
                    546:        while (!queue_end(q, (queue_entry_t)ior)) {
                    547:            if (ior->io_reply_port == port) {
                    548:                remqueue(q, (queue_entry_t)ior);
                    549:                ior->io_done = routine;
                    550:                iodone(ior);
                    551:                return TRUE;
                    552:            }
                    553:            ior = ior->io_next;
                    554:        }
                    555:        return FALSE;
                    556: }
                    557: 
                    558: /*
                    559:  * Handle port-death (dead reply port) for tty.
                    560:  * No locks may be held.
                    561:  * May run on any CPU.
                    562:  */
                    563: boolean_t
                    564: tty_portdeath(
                    565:        struct tty *    tp,
                    566:        ipc_port_t      port)
                    567: {
                    568:        register spl_t  spl = spltty();
                    569:        register boolean_t      result;
                    570: 
                    571:        simple_lock(&tp->t_lock);
                    572: 
                    573:        /*
                    574:         * The queues may never have been initialized
                    575:         */
                    576:        if (tp->t_delayed_read.next == 0) {
                    577:            result = FALSE;
                    578:        }
                    579:        else {
                    580:            result =
                    581:                tty_queue_clean(&tp->t_delayed_read,  port,
                    582:                                tty_close_read_reply)
                    583:             || tty_queue_clean(&tp->t_delayed_write, port,
                    584:                                tty_close_write_reply)
                    585:             || tty_queue_clean(&tp->t_delayed_open,  port,
                    586:                                tty_close_open_reply);
                    587:        }
                    588:        simple_unlock(&tp->t_lock);
                    589:        splx(spl);
                    590: 
                    591:        return result;
                    592: }
                    593: 
                    594: /*
                    595:  * Get TTY status.
                    596:  * No locks may be held.
                    597:  * May run on any CPU.
                    598:  */
                    599: io_return_t tty_get_status(
                    600:        register struct tty *tp,
                    601:        dev_flavor_t    flavor,
                    602:        int *           data,           /* pointer to OUT array */
                    603:        natural_t       *count)         /* out */
                    604: {
                    605:        spl_t           s;
                    606: 
                    607:        switch (flavor) {
                    608:            case TTY_STATUS:
                    609:            {
                    610:                register struct tty_status *tsp =
                    611:                        (struct tty_status *) data;
                    612: 
                    613:                if (*count < TTY_STATUS_COUNT)
                    614:                    return (D_INVALID_OPERATION);
                    615: 
                    616:                s = spltty();
                    617:                simple_lock(&tp->t_lock);
                    618: 
                    619:                tsp->tt_ispeed = tp->t_ispeed;
                    620:                tsp->tt_ospeed = tp->t_ospeed;
                    621:                tsp->tt_breakc = tp->t_breakc;
                    622:                tsp->tt_flags  = tp->t_flags;
                    623:                if (tp->t_state & TS_HUPCLS)
                    624:                    tsp->tt_flags |= TF_HUPCLS;
                    625: 
                    626:                simple_unlock(&tp->t_lock);
                    627:                splx(s);
                    628: 
                    629:                *count = TTY_STATUS_COUNT;
                    630:                break;
                    631: 
                    632:            }
                    633:            default:
                    634:                return D_INVALID_OPERATION;
                    635:        }
                    636:        return D_SUCCESS;
                    637: }
                    638: 
                    639: /*
                    640:  * Set TTY status.
                    641:  * No locks may be held.
                    642:  * Calls device start or stop routines; must already be on master if
                    643:  * device needs to run on master.
                    644:  */
                    645: io_return_t tty_set_status(
                    646:        register struct tty *tp,
                    647:        dev_flavor_t    flavor,
                    648:        int *           data,
                    649:        natural_t       count)
                    650: {
                    651:        int     s;
                    652: 
                    653:        switch (flavor) {
                    654:            case TTY_FLUSH:
                    655:            {
                    656:                register int    flags;
                    657:                if (count < TTY_FLUSH_COUNT)
                    658:                    return D_INVALID_OPERATION;
                    659: 
                    660:                flags = *data;
                    661:                if (flags == 0)
                    662:                    flags = D_READ | D_WRITE;
                    663: 
                    664:                s = spltty();
                    665:                simple_lock(&tp->t_lock);
                    666:                tty_flush(tp, flags);
                    667:                simple_unlock(&tp->t_lock);
                    668:                splx(s);
                    669: 
                    670:                break;
                    671:            }
                    672:            case TTY_STOP:
                    673:                /* stop output */
                    674:                s = spltty();
                    675:                simple_lock(&tp->t_lock);
                    676:                if ((tp->t_state & TS_TTSTOP) == 0) {
                    677:                    tp->t_state |= TS_TTSTOP;
                    678:                    (*tp->t_stop)(tp, 0);
                    679:                }
                    680:                simple_unlock(&tp->t_lock);
                    681:                splx(s);
                    682:                break;
                    683: 
                    684:            case TTY_START:
                    685:                /* start output */
                    686:                s = spltty();
                    687:                simple_lock(&tp->t_lock);
                    688:                if (tp->t_state & TS_TTSTOP) {
                    689:                    tp->t_state &= ~TS_TTSTOP;
                    690:                    tty_output(tp);
                    691:                }
                    692:                simple_unlock(&tp->t_lock);
                    693:                splx(s);
                    694:                break;
                    695: 
                    696:            case TTY_STATUS:
                    697:                /* set special characters and speed */
                    698:            {
                    699:                register struct tty_status *tsp;
                    700: 
                    701:                if (count < TTY_STATUS_COUNT)
                    702:                    return D_INVALID_OPERATION;
                    703: 
                    704:                tsp = (struct tty_status *)data;
                    705: 
                    706:                if (tsp->tt_ispeed < 0 ||
                    707:                    tsp->tt_ispeed >= NSPEEDS ||
                    708:                    tsp->tt_ospeed < 0 ||
                    709:                    tsp->tt_ospeed >= NSPEEDS)
                    710:                {
                    711:                    return D_INVALID_OPERATION;
                    712:                }
                    713: 
                    714:                s = spltty();
                    715:                simple_lock(&tp->t_lock);
                    716: 
                    717:                tp->t_ispeed = tsp->tt_ispeed;
                    718:                tp->t_ospeed = tsp->tt_ospeed;
                    719:                tp->t_breakc = tsp->tt_breakc;
                    720:                tp->t_flags  = tsp->tt_flags & ~TF_HUPCLS;
                    721:                if (tsp->tt_flags & TF_HUPCLS)
                    722:                    tp->t_state |= TS_HUPCLS;
                    723: 
                    724:                simple_unlock(&tp->t_lock);
                    725:                splx(s);
                    726:                break;
                    727:            }
                    728:            default:
                    729:                return D_INVALID_OPERATION;
                    730:        }
                    731:        return D_SUCCESS;
                    732: }
                    733: 
                    734: 
                    735: /*
                    736:  * [internal]
                    737:  * Queue IOR on reply queue, to wait for TTY operation.
                    738:  * TTY must be locked (at spltty).
                    739:  */
                    740: void queue_delayed_reply(
                    741:        queue_t         qh,
                    742:        io_req_t        ior,
                    743:        boolean_t       (*io_done)(io_req_t) )
                    744: {
                    745:        ior->io_done = io_done;
                    746:        enqueue_tail(qh, (queue_entry_t)ior);
                    747: }
                    748: 
                    749: /*
                    750:  * Retry delayed IO operations for TTY.
                    751:  * TTY containing queue must be locked (at spltty).
                    752:  */
                    753: void tty_queue_completion(
                    754:        register queue_t        qh)
                    755: {
                    756:        register io_req_t       ior;
                    757: 
                    758:        while ((ior = (io_req_t)dequeue_head(qh)) != 0) {
                    759:            iodone(ior);
                    760:        }
                    761: }
                    762: 
                    763: /*
                    764:  * Set the default special characters.
                    765:  * Since this routine is called whenever a tty has never been opened,
                    766:  * we can initialize the queues here.
                    767:  */
                    768: void ttychars(
                    769:        register struct tty *tp)
                    770: {
                    771:        if ((tp->t_flags & TS_INIT) == 0) {
                    772:            /*
                    773:             * Initialize queues
                    774:             */
                    775:            queue_init(&tp->t_delayed_open);
                    776:            queue_init(&tp->t_delayed_read);
                    777:            queue_init(&tp->t_delayed_write);
                    778: 
                    779:            /*
                    780:             * Initialize character buffers
                    781:             */
                    782:            cb_alloc(&tp->t_inq,  tty_inq_size);
                    783: 
                    784:            /* if we might do modem flow control */
                    785:            if (tp->t_mctl && tp->t_inq.c_hog > 30)
                    786:                tp->t_inq.c_hog -= 30;
                    787: 
                    788:            cb_alloc(&tp->t_outq, tty_outq_size);
                    789: 
                    790:            /*
                    791:             * Mark initialized
                    792:             */
                    793:            tp->t_state |= TS_INIT;
                    794:        }
                    795: 
                    796:        tp->t_breakc = 0;
                    797: }
                    798: 
                    799: /*
                    800:  * Flush all TTY queues.
                    801:  * Called at spltty, tty already locked.
                    802:  * Calls device STOP routine; must already be on master if
                    803:  * device needs to run on master.
                    804:  */
                    805: void tty_flush(
                    806:        register struct tty *tp,
                    807:        int     rw)
                    808: {
                    809:        if (rw & D_READ) {
                    810:            cb_clear(&tp->t_inq);
                    811:            tty_queue_completion(&tp->t_delayed_read);
                    812:        }
                    813:        if (rw & D_WRITE) {
                    814:            tp->t_state &= ~TS_TTSTOP;
                    815:            (*tp->t_stop)(tp, rw);
                    816:            cb_clear(&tp->t_outq);
                    817:            tty_queue_completion(&tp->t_delayed_write);
                    818:        }
                    819: }
1.1.1.2   root      820: 
1.1       root      821: /*
                    822:  * Restart character output after a delay timeout.
                    823:  * Calls device start routine - must be on master CPU.
                    824:  *
                    825:  *     Timeout routines are called only on master CPU.
                    826:  *     What if device runs on a different CPU?
                    827:  */
                    828: void ttrstrt(
                    829:        register struct tty *tp)
                    830: {
                    831:        register spl_t  s;
                    832: 
                    833:        s = spltty();
                    834:        simple_lock(&tp->t_lock);
                    835: 
                    836:        tp->t_state &= ~TS_TIMEOUT;
                    837:        ttstart (tp);
                    838: 
                    839:        simple_unlock(&tp->t_lock);
                    840:         splx(s);
                    841: }
                    842: 
                    843: /*
                    844:  * Start output on the typewriter. It is used from the top half
                    845:  * after some characters have been put on the output queue,
                    846:  * from the interrupt routine to transmit the next
                    847:  * character, and after a timeout has finished.
                    848:  *
                    849:  * Called at spltty, tty already locked.
                    850:  * Must be on master CPU if device runs on master.
                    851:  */
                    852: void ttstart(tp)
                    853:        register struct tty *tp;
                    854: {
                    855:        if ((tp->t_state & (TS_TIMEOUT|TS_TTSTOP|TS_BUSY)) == 0) {
                    856:            /*
                    857:             * Start up the hardware again
                    858:             */
                    859:            (*tp->t_start)(tp);
                    860: 
                    861:            /*
                    862:             * Wake up those waiting for write completion.
                    863:             */
                    864:            if (tp->t_outq.c_cc <= TTLOWAT(tp))
                    865:                tty_queue_completion(&tp->t_delayed_write);
                    866:        }
                    867: }
                    868: 
                    869: /*
                    870:  * Start character output, if the device is not busy or
                    871:  * stopped or waiting for a timeout.
                    872:  *
                    873:  * Called at spltty, tty already locked.
                    874:  * Must be on master CPU if device runs on master.
                    875:  */
                    876: void tty_output(
                    877:        register struct tty *tp)
                    878: {
                    879:        if ((tp->t_state & (TS_TIMEOUT|TS_TTSTOP|TS_BUSY)) == 0) {
                    880:            /*
                    881:             * Not busy.  Start output.
                    882:             */
                    883:            (*tp->t_start)(tp);
                    884: 
                    885:            /*
                    886:             * Wake up those waiting for write completion.
                    887:             */
                    888:            if (tp->t_outq.c_cc <= TTLOWAT(tp))
                    889:                tty_queue_completion(&tp->t_delayed_write);
                    890:        }
                    891: }
                    892: 
                    893: /*
                    894:  * Send any buffered recvd chars up to user
                    895:  */
                    896: void ttypush(
                    897:        register struct tty     *tp)
                    898: {
                    899:        spl_t   s = spltty();
                    900:        register int    state;
                    901: 
                    902:        simple_lock(&tp->t_lock);
                    903: 
                    904:        /*
1.1.1.2   root      905:          The pdma timeout has gone off.
1.1       root      906:          If no character has been received since the timeout
                    907:          was set, push any pending characters up.
                    908:          If any characters were received in the last interval
                    909:          then just reset the timeout and the character received bit.
                    910:          */
                    911: 
                    912:        state = tp->t_state;
                    913: 
                    914:        if (state & TS_MIN_TO)
                    915:          {
                    916:            if (state & TS_MIN_TO_RCV)
                    917:              { /* a character was received */
                    918:                tp->t_state = state & ~TS_MIN_TO_RCV;
                    919:                timeout(ttypush,tp,pdma_timeouts[tp->t_ispeed]);
                    920:              }
                    921:            else
                    922:              {
                    923:                tp->t_state = state & ~TS_MIN_TO;
                    924:                if (tp->t_inq.c_cc) /* pending characters */
                    925:                  tty_queue_completion(&tp->t_delayed_read);
                    926:              }
                    927:          }
                    928:        else
                    929:          {
                    930:            tp->t_state = state & ~TS_MIN_TO_RCV;/* sanity */
                    931:          }
                    932: 
                    933:        simple_unlock(&tp->t_lock);
                    934:        splx(s);
                    935: }
                    936: 
                    937: /*
                    938:  * Put input character on input queue.
                    939:  *
                    940:  * Called at spltty, tty already locked.
                    941:  */
                    942: void ttyinput(
                    943:        unsigned int    c,
                    944:        struct tty      *tp)
                    945: {
                    946:   if (tp->t_inq.c_cc >= tp->t_inq.c_hog) {
                    947:     /*
                    948:      * Do not want to overflow input queue
                    949:      */
                    950:     if (tp->t_mctl) {
                    951:        (*tp->t_mctl)(tp, TM_RTS, DMBIC);
                    952:        tp->t_state |= TS_RTS_DOWN;
                    953:     }
                    954:     tty_queue_completion(&tp->t_delayed_read);
                    955:     return;
                    956: 
                    957:   }
                    958: 
                    959:   c &= 0xff;
                    960: 
                    961:   (void) putc(c, &tp->t_inq);
                    962:   if ((tp->t_state & TS_MIN) == 0 ||
                    963:        tp->t_inq.c_cc > pdma_water_mark[tp->t_ispeed])
                    964:      {
                    965:          /*
                    966:           * No input buffering, or input minimum exceeded.
                    967:           * Grab a request from input queue and queue it
                    968:           * to io_done thread.
                    969:           */
                    970:          if (tp->t_state & TS_MIN_TO) {
                    971:            tp->t_state &= ~(TS_MIN_TO|TS_MIN_TO_RCV);
                    972:            untimeout(ttypush, tp);
                    973:          }
                    974:          tty_queue_completion(&tp->t_delayed_read);
                    975:       }
                    976:       else {
                    977:        /*
1.1.1.2   root      978:         * Not enough characters.
                    979:         * If no timeout is set, initiate the timeout
1.1       root      980:         * Otherwise set the character received during timeout interval
                    981:         * flag.
                    982:         * One alternative approach would be just to reset the timeout
                    983:         * into the future, but this involves making a timeout/untimeout
                    984:         * call on every character.
                    985:         */
                    986:        register int ptime = pdma_timeouts[tp->t_ispeed];
                    987:        if (ptime > 0)
                    988:          {
                    989:            if ((tp->t_state & TS_MIN_TO) == 0)
                    990:              {
                    991:                tp->t_state |= TS_MIN_TO;
                    992:                timeout(ttypush, tp, ptime);
                    993:              }
                    994:            else
                    995:              {
                    996:                tp->t_state |= TS_MIN_TO_RCV;
                    997:              }
                    998:          }
                    999:       }
                   1000: }
                   1001: 
                   1002: /*
                   1003:  * Put many characters on input queue.
                   1004:  *
                   1005:  * Called at spltty, tty already locked.
                   1006:  */
                   1007: void ttyinput_many(
                   1008:        struct tty      *tp,
                   1009:        unsigned char   *chars,
                   1010:        int             count)
                   1011: {
                   1012:        /*
1.1.1.2   root     1013:         * Do not want to overflow input queue
1.1       root     1014:         */
                   1015:        if (tp->t_inq.c_cc < tp->t_inq.c_hog)
                   1016:                count -= b_to_q( chars, count, &tp->t_inq);
                   1017: 
                   1018:        tty_queue_completion(&tp->t_delayed_read);
                   1019: }
                   1020: 
                   1021: 
                   1022: /*
                   1023:  * Handle modem control transition on a tty.
                   1024:  * Flag indicates new state of carrier.
                   1025:  * Returns FALSE if the line should be turned off.
                   1026:  *
                   1027:  * Called at spltty, tty already locked.
                   1028:  */
                   1029: boolean_t ttymodem(
                   1030:        struct tty *    tp,
                   1031:        boolean_t       carrier_up)
                   1032: {
                   1033:        if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_flags & MDMBUF)) {
                   1034:            /*
                   1035:             * Flow control by carrier.  Carrier down stops
                   1036:             * output; carrier up restarts output.
                   1037:             */
                   1038:            if (carrier_up) {
                   1039:                tp->t_state &= ~TS_TTSTOP;
                   1040:                tty_output(tp);
                   1041:            }
                   1042:            else if ((tp->t_state&TS_TTSTOP) == 0) {
                   1043:                tp->t_state |= TS_TTSTOP;
                   1044:                (*tp->t_stop)(tp, 0);
                   1045:            }
                   1046:        }
                   1047:        else if (carrier_up) {
                   1048:            /*
                   1049:             * Carrier now on.
                   1050:             */
                   1051:            tp->t_state |= TS_CARR_ON;
                   1052:            tt_open_wakeup(tp);
                   1053:        }
                   1054:        else {
                   1055:            /*
                   1056:             * Lost carrier.
                   1057:             */
                   1058:            tp->t_state &= ~TS_CARR_ON;
                   1059:            if (tp->t_state & TS_ISOPEN &&
                   1060:                (tp->t_flags & NOHANG) == 0)
                   1061:            {
                   1062:                /*
                   1063:                 * Hang up TTY if carrier drops.
                   1064:                 * Need to alert users, somehow...
                   1065:                 */
                   1066:                tty_flush(tp, D_READ|D_WRITE);
                   1067:                return FALSE;
                   1068:            }
                   1069:        }
                   1070:        return TRUE;
                   1071: }
                   1072: 
                   1073: /*
                   1074:  * Similarly, handle transitions on the ClearToSend
                   1075:  * signal.  Nowadays, it is used by many modems as
                   1076:  * a flow-control device: they turn it down to stop
                   1077:  * us from sending more chars.  We do the same with
                   1078:  * the RequestToSend signal. [Yes, that is exactly
                   1079:  * why those signals are defined in the standard.]
                   1080:  *
                   1081:  * Tty must be locked and on master.
                   1082:  */
                   1083: tty_cts(
                   1084:        struct tty *    tp,
                   1085:        boolean_t       cts_up)
                   1086: {
                   1087:        if (tp->t_state & TS_ISOPEN){
                   1088:                if (cts_up) {
                   1089:                        tp->t_state &= ~(TS_TTSTOP|TS_BUSY);
                   1090:                        tty_output(tp);
                   1091:                } else {
                   1092:                        tp->t_state |= (TS_TTSTOP|TS_BUSY);
                   1093:                        (*tp->t_stop)(tp, D_WRITE);
                   1094:                }
                   1095:        }
                   1096: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.