/*
* Copyright (c) 2009-~ Lan Peng
*
* This source code is released for free distribution under the terms of the
* GNU General Public License
*
* Author: Lan Peng<[email protected]>
* Created Time: 2010年07月26日 星期一 10時12分32秒
* File Name: tty_lan.c
*
* Description:
*/ #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/cdev.h> #include <linux/tty.h> #include <linux/fs.h> #include <linux/tty_driver.h> #include <linux/tty_flip.h> #include <linux/ioport.h> #include <linux/serial_reg.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lan");
#define TTY_LAN_MINORS_NUM 5 #define TTY_LAN_MAJOR 202 static int open_count = 0; static unsigned char *to; static struct tty_driver *tty_lan_driver; static struct tty_struct *tty_lan_struct;
static int tty_lan_open(struct tty_struct *tty, struct file *filp); static void tty_lan_close(struct tty_struct *tty, struct file *filp); static int tty_lan_write(struct tty_struct *tty, const unsigned char *buffer, int count); static int tty_lan_write_room(struct tty_struct *tty); static void tty_lan_set_termios(struct tty_struct *tty, struct ktermios * old); static int tty_lan_put_char(struct tty_struct *tty, unsigned char ch);
static struct tty_operations tty_lan_ops = {
.open = tty_lan_open,
.close = tty_lan_close,
.write = tty_lan_write,
.put_char = tty_lan_put_char,
.write_room = tty_lan_write_room,
.set_termios = tty_lan_set_termios, };
static int __init tty_lan_init(void) {
int i;
int retval;
tty_lan_driver = alloc_tty_driver(TTY_LAN_MINORS_NUM);
if(!tty_lan_driver)
return -ENOMEM;
tty_lan_driver->owner = THIS_MODULE;
tty_lan_driver->driver_name = "tty_lan";
tty_lan_driver->name = "ttty_lan";
tty_lan_driver->major = TTY_LAN_MAJOR,
tty_lan_driver->minor_start = 0;
tty_lan_driver->type = TTY_DRIVER_TYPE_SERIAL;
tty_lan_driver->subtype = SERIAL_TYPE_NORMAL;
tty_lan_driver->flags = TTY_DRIVER_REAL_RAW;
tty_lan_driver->init_termios = tty_std_termios;
tty_lan_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
tty_set_operations(tty_lan_driver, &tty_lan_ops);
retval = tty_register_driver(tty_lan_driver);
if(retval){
printk(KERN_ERR"Failed to register tty_lan_driver!\n");
put_tty_driver(tty_lan_driver);
return retval;
}
for(i = 0; i < TTY_LAN_MINORS_NUM; i++)
tty_register_device(tty_lan_driver, i, NULL);
return 0; }
static int tty_lan_open(struct tty_struct *tty, struct file *filp) {
if(open_count == 0){
printk("Open OK!\n");
}
tty_lan_struct = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
tty->low_latency = 1;
tty_lan_struct = tty;
return 0; }
static void tty_lan_close(struct tty_struct *tty, struct file *filp) {
printk("ClOSE OK!\n");
kfree(tty_lan_struct);
return; }
static int tty_lan_write(struct tty_struct *tty, const unsigned char *buffer, int count) {
int i;
int retval = count;
tty = tty_lan_struct;
printk(KERN_DEBUG "%s - \n", __FUNCTION__);
printk("count :%d\n", count);
printk("user write: %s ", buffer);
printk("\n");
tty_insert_flip_string(tty, buffer, count);
tty_flip_buffer_push(tty);
return retval; }
static int tty_lan_put_char(struct tty_struct *tty, unsigned char ch) {
printk("put_char :%c\n", ch);
return 0; }
static int tty_lan_write_room(struct tty_struct *tty) {
int room;
room = 255;
return room; }
static void tty_lan_set_termios(struct tty_struct *tty, struct ktermios *old) {
tty = tty_lan_struct;
if(tty->termios->c_cflag == old->c_cflag){
printk("Nothing to change!\n");
return ;
}
printk("There is something to Change............\n");
return ; }
static void __exit tty_lan_exit(void) {
int i;
for(i = 0; i < TTY_LAN_MINORS_NUM; i++)
tty_unregister_device(tty_lan_driver, i);
tty_unregister_driver(tty_lan_driver); }
module_init(tty_lan_init);
module_exit(tty_lan_exit);
|