Subversion Repositories shark

Rev

Rev 846 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1049 mauro 1
/*
2
 *  linux/drivers/char/tty_io.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5
 */
6
 
7
#include <linuxcomp.h>
8
 
9
#include <linux/config.h>
10
#include <linux/types.h>
11
#include <linux/major.h>
12
#include <linux/errno.h>
13
#include <linux/signal.h>
14
#include <linux/fcntl.h>
15
#include <linux/sched.h>
16
#include <linux/interrupt.h>
17
#include <linux/tty.h>
18
#include <linux/tty_driver.h>
19
#include <linux/tty_flip.h>
20
#include <linux/devpts_fs.h>
21
#include <linux/file.h>
22
#include <linux/console.h>
23
#include <linux/timer.h>
24
#include <linux/ctype.h>
25
#include <linux/kd.h>
26
#include <linux/mm.h>
27
#include <linux/string.h>
28
#include <linux/slab.h>
29
#include <linux/poll.h>
30
#include <linux/proc_fs.h>
31
#include <linux/init.h>
32
#include <linux/module.h>
33
#include <linux/smp_lock.h>
34
#include <linux/device.h>
35
 
36
#include <asm/uaccess.h>
37
#include <asm/system.h>
38
#include <asm/bitops.h>
39
 
40
#include <linux/kbd_kern.h>
41
#include <linux/vt_kern.h>
42
#include <linux/selection.h>
43
#include <linux/devfs_fs_kernel.h>
44
 
45
#include <linux/kmod.h>
46
 
47
#undef TTY_DEBUG_HANGUP
48
 
49
#define TTY_PARANOIA_CHECK 1
50
#define CHECK_TTY_COUNT 1
51
 
52
struct tty_ldisc ldiscs[NR_LDISCS];
53
 
54
struct termios tty_std_termios = {      /* for the benefit of tty drivers  */
55
        .c_iflag = ICRNL | IXON,
56
        .c_oflag = OPOST | ONLCR,
57
        .c_cflag = B4800 | CS8 | CREAD | HUPCL,
58
        .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
59
                   ECHOCTL | ECHOKE | IEXTEN,
60
        .c_cc = INIT_C_CC
61
};
62
 
63
struct tty_driver *alloc_tty_driver(int lines)
64
{
65
        struct tty_driver *driver;
66
 
67
        driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
68
        if (driver) {
69
                memset(driver, 0, sizeof(struct tty_driver));
70
                driver->magic = TTY_DRIVER_MAGIC;
71
                driver->num = lines;
72
                /* later we'll move allocation of tables here */
73
        }
74
        return driver;
75
}
76
 
77
void put_tty_driver(struct tty_driver *driver)
78
{
79
        kfree(driver);
80
}
81
 
82
void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
83
{
84
        driver->open = op->open;
85
        driver->close = op->close;
86
        driver->write = op->write;
87
        driver->put_char = op->put_char;
88
        driver->flush_chars = op->flush_chars;
89
        driver->write_room = op->write_room;
90
        driver->chars_in_buffer = op->chars_in_buffer;
91
        driver->ioctl = op->ioctl;
92
        driver->set_termios = op->set_termios;
93
        driver->throttle = op->throttle;
94
        driver->unthrottle = op->unthrottle;
95
        driver->stop = op->stop;
96
        driver->start = op->start;
97
        driver->hangup = op->hangup;
98
        driver->break_ctl = op->break_ctl;
99
        driver->flush_buffer = op->flush_buffer;
100
        driver->set_ldisc = op->set_ldisc;
101
        driver->wait_until_sent = op->wait_until_sent;
102
        driver->send_xchar = op->send_xchar;
103
        driver->read_proc = op->read_proc;
104
        driver->write_proc = op->write_proc;
105
        driver->tiocmget = op->tiocmget;
106
        driver->tiocmset = op->tiocmset;
107
}
108
 
109
int tty_register_driver(struct tty_driver *driver)
110
{
111
        return 0;
112
}
113
 
114
int tty_unregister_driver(struct tty_driver *driver)
115
{
116
        return 0;
117
}
118
 
119
void tty_register_device(struct tty_driver *driver, unsigned index,
120
                         struct device *device)
121
{
122
 
123
}
124
 
125
void tty_unregister_device(struct tty_driver *driver, unsigned index)
126
{
127
 
128
}
129
 
130
/*
131
 * This routine is called out of the software interrupt to flush data
132
 * from the flip buffer to the line discipline.
133
 */
134
static void flush_to_ldisc(void *private_)
135
{
136
        struct tty_struct *tty = (struct tty_struct *) private_;
137
        unsigned char   *cp;
138
        char            *fp;
139
        int             count;
140
 
141
        if (tty->flip.buf_num) {
142
                cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
143
                fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
144
                tty->flip.buf_num = 0;
145
                tty->flip.char_buf_ptr = tty->flip.char_buf;
146
                tty->flip.flag_buf_ptr = tty->flip.flag_buf;
147
        } else {
148
                cp = tty->flip.char_buf;
149
                fp = tty->flip.flag_buf;
150
                tty->flip.buf_num = 1;
151
                tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
152
                tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
153
        }
154
        count = tty->flip.count;
155
        tty->flip.count = 0;
156
 
157
        tty->ldisc.receive_buf(tty, cp, fp, count);
158
}
159
 
160
 
161
void tty_flip_buffer_push(struct tty_struct *tty)
162
{
163
        flush_to_ldisc((void *) tty);
164
}
165
 
166
static struct tty_struct *alloc_tty_struct(void)
167
{
168
        struct tty_struct *tty;
169
 
170
        tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
171
        if (tty)
172
                memset(tty, 0, sizeof(struct tty_struct));
173
        return tty;
174
}
175
 
176
void do_tty_hangup(void *data)
177
{
178
 
179
}
180
 
181
/*
182
 * This subroutine initializes a tty structure.
183
 */
184
static void initialize_tty_struct(struct tty_struct *tty)
185
{
186
        memset(tty, 0, sizeof(struct tty_struct));
187
        tty->magic = TTY_MAGIC;
188
        tty->ldisc = ldiscs[N_TTY];
189
        tty->pgrp = -1;
190
        tty->flip.char_buf_ptr = tty->flip.char_buf;
191
        tty->flip.flag_buf_ptr = tty->flip.flag_buf;
192
        INIT_WORK(&tty->flip.work, flush_to_ldisc, tty);
193
        init_MUTEX(&tty->flip.pty_sem);
194
        init_waitqueue_head(&tty->write_wait);
195
        init_waitqueue_head(&tty->read_wait);
196
        INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
197
        //*sema_init(&tty->atomic_read, 1);
198
        //*sema_init(&tty->atomic_write, 1);
199
        spin_lock_init(&tty->read_lock);
200
        INIT_LIST_HEAD(&tty->tty_files);
201
        INIT_WORK(&tty->SAK_work, NULL, NULL);
202
}
203
 
204
extern int serial_open (struct tty_struct *tty, struct file * filp);
205
extern int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
206
 
207
static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
208
{
209
        const unsigned char *p;
210
        char *f, flags = TTY_NORMAL;
211
        int     i;
212
        char    buf[64];
213
        unsigned long cpuflags;
214
 
215
  if (!tty->read_buf)
216
                return;
217
 
218
  spin_lock_irqsave(&tty->read_lock, cpuflags);
219
  i = min(N_TTY_BUF_SIZE - tty->read_cnt, \
220
          N_TTY_BUF_SIZE - tty->read_head);
221
        i = min(count, i);
222
        memcpy(tty->read_buf + tty->read_head, cp, i);
223
        tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
224
  tty->read_cnt += i;
225
  spin_unlock_irqrestore(&tty->read_lock, cpuflags);
226
 
227
//**    for (i=0; i<count; i++)
228
//**        printk("%c", *(cp+i) );
229
}
230
 
231
/*
232
 * Check whether to call the driver.unthrottle function.
233
 * We test the TTY_THROTTLED bit first so that it always
234
 * indicates the current state.
235
 */
236
static void check_unthrottle(struct tty_struct * tty)
237
{
238
        if (tty->count &&
239
            test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
240
            tty->driver->unthrottle)
241
                tty->driver->unthrottle(tty);
242
}
243
 
244
/*
245
 * Reset the read buffer counters, clear the flags,
246
 * and make sure the driver is unthrottled. Called
247
 * from n_tty_open() and n_tty_flush_buffer().
248
 */
249
static void reset_buffer_flags(struct tty_struct *tty)
250
{
251
        unsigned long flags;
252
 
253
        spin_lock_irqsave(&tty->read_lock, flags);
254
        tty->read_head = tty->read_tail = tty->read_cnt = 0;
255
        spin_unlock_irqrestore(&tty->read_lock, flags);
256
        tty->canon_head = tty->canon_data = tty->erasing = 0;
257
        memset(&tty->read_flags, 0, sizeof tty->read_flags);
258
        check_unthrottle(tty);
259
}
260
 
261
int serial_usbport_open(void **private, int port_number)
262
{
263
        int retval;
264
        struct tty_struct *tty;
265
 
266
        tty = alloc_tty_struct();
267
        if(!tty)
268
                goto fail_no_mem;
269
 
270
        initialize_tty_struct (tty);
271
 
272
        tty->termios = kmalloc (sizeof(struct termios), GFP_KERNEL);
273
        *(tty->termios) = tty_std_termios;
274
        tty->index = port_number;      
275
        tty->ldisc.receive_buf = n_tty_receive_buf;
276
 
277
        if (!tty->read_buf) {
278
                tty->read_buf = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
279
                if (!tty->read_buf)
280
                        return -ENOMEM;
281
        }
282
        memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
283
        reset_buffer_flags(tty);
284
 
285
        retval = serial_open(tty, NULL);
286
        *private = (void*)tty;
287
        return retval;
288
 
289
fail_no_mem:
290
        return -ENOMEM;
291
}
292
 
293
int serial_usbport_write(void *private, const unsigned char *buf, int count)
294
{
295
        int retval;
296
        struct tty_struct *tty = (struct tty_struct*) private;
297
 
298
        retval = serial_write(tty, 0, buf, count);
299
        return retval;
300
}
301
 
302
int serial_usbport_read(void *private, char* data_in)
303
{
304
        char c;
305
        struct tty_struct *tty = (struct tty_struct*) private;
306
        unsigned long flags;
307
 
308
  if (tty->read_cnt)
309
  {
310
    c = tty->read_buf[tty->read_tail];
311
    tty->read_tail = ((tty->read_tail+1) & \
312
                      (N_TTY_BUF_SIZE-1));
313
    tty->read_cnt--;
314
    *data_in = c;
315
    return 1;
316
  }
317
  *data_in = 0;
318
  return 0;
319
}