linux2.6.28-tty设备驱动学习(二)
来源:互联网 发布:情义我心知粤语版 编辑:程序博客网 时间:2024/05/20 04:47
struct tty_operations {
int (*open)(struct tty_struct * tty, struct file * filp);
void (*close)(struct tty_struct * tty, struct file * filp);
int (*write)(struct tty_struct * tty,
const unsigned char *buf, int count);
void (*put_char)(struct tty_struct *tty, unsigned char ch);
void (*flush_chars)(struct tty_struct *tty);
int (*write_room)(struct tty_struct *tty);
int (*chars_in_buffer)(struct tty_struct *tty);
int (*ioctl)(struct tty_struct *tty, struct file * file,
unsigned int cmd, unsigned long arg);
long (*compat_ioctl)(struct tty_struct *tty, struct file * file,
unsigned int cmd, unsigned long arg);
void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
void (*throttle)(struct tty_struct * tty);
void (*unthrottle)(struct tty_struct * tty);
void (*stop)(struct tty_struct *tty);
void (*start)(struct tty_struct *tty);
void (*hangup)(struct tty_struct *tty);
void (*break_ctl)(struct tty_struct *tty, int state);
void (*flush_buffer)(struct tty_struct *tty);
void (*set_ldisc)(struct tty_struct *tty);
void (*wait_until_sent)(struct tty_struct *tty, int timeout);
void (*send_xchar)(struct tty_struct *tty, char ch);
int (*read_proc)(char *page, char **start, off_t off,
int count, int *eof, void *data);
int (*write_proc)(struct file *file, const char __user *buffer,
unsigned long count, void *data);
int (*tiocmget)(struct tty_struct *tty, struct file *file);
int (*tiocmset)(struct tty_struct *tty, struct file *file,
unsigned int set, unsigned int clear);
};
/*
* 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<lanpeng722@gmail.com>
* 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);
/*******************************************************
* File Name: send.c
* Description: send data to serial_Port
* Date:
*******************************************************/
/******************头文件定义******************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#define max_buffer_size 100 /*定义缓冲区最大宽度*/
/*******************************************/
int fd; /*定义设备文件描述符*/
int flag_close;
int open_serial(int k)
{
if(k==0) /*tty设备选择*/
{
fd = open("/dev/ttty_lan0",O_RDWR|O_NOCTTY); /*读写方式打开设备*/
perror("open /dev/ttty_lan0");
}
else
{
fd = open("/dev/ttty_lan1",O_RDWR|O_NOCTTY);
perror("open /dev/ttty_lan1");
}
if(fd == -1) /*打开失败*/
return -1;
else
return 0;
}
/********************************************************************/
int main(int argc, char *argv[])
{
char sbuf[]={"Hello,this is a Serial_Port test!\n"};/*待发送的内容,以\n为结
束标志*/
int retv;
struct termios opt;
int length=sizeof(sbuf);/*发送缓冲区数据宽度*/
/*******************************************************************/
open_serial(0); /*打开设备1*/
/*******************************************************************/
printf("ready for sending data...\n"); /*准备开始发送数据*/
tcgetattr(fd,&opt);
cfmakeraw(&opt);
/*****************************************************************/
//cfsetispeed(&opt,B9600); /*波特率设置为9600bps*/
//cfsetospeed(&opt,B9600);
/*******************************************************************/
tcsetattr(fd,TCSANOW,&opt);
retv=write(fd,sbuf,length); /*接收数据*/
if(retv==-1)
{
perror("write");
}
printf("the number of char sent is %d\n",retv);
flag_close =close(fd);
if(flag_close == -1) /*判断是否成功关闭文件*/
printf("Close the Device failur!\n");
return 0;
}
/****************************结束***********************************/
/*******************************************************
*ilename:receive.c
* Description:Receive data from Serial_Port
* Date:
*******************************************************/
/*********************头文件定义***********************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include "math.h"
#define max_buffer_size 100 /*定义缓冲区最大宽度*/
/*********************************************************/
int fd, s;
int open_serial(int k)
{
if(k==0) /*tty设备选择*/
{
fd = open("/dev/ttty_lan0",O_RDWR|O_NOCTTY); /*读写方式打开设备*/
perror("open /dev/ttty_lan0");
}
else
{
fd = open("/dev/ttty_lan1",O_RDWR|O_NOCTTY);
perror("open /dev/ttty_lan1");
}
if(fd == -1) /*打开失败*/
return -1;
else
return 0;
}
/********************************************************************/
int main()
{
char hd[max_buffer_size],*rbuf; /*定义接收缓冲区*/
int flag_close, retv,i,ncount=0;
struct termios opt;
/*******************************************************************/
open_serial(0); /*打开设备1*/
/*******************************************************************/
tcgetattr(fd,&opt);
cfmakeraw(&opt);
/*****************************************************************/
//cfsetispeed(&opt,B9600); /*波特率设置为9600bps*/
//cfsetospeed(&opt,B9600);
/*******************************************************************/
tcsetattr(fd,TCSANOW,&opt);
rbuf=hd; /*数据保存*/
printf("ready for receiving data...\n");
retv=read(fd,rbuf,1); /*接收数据*/
if(retv==-1)
{
perror("read"); /*读状态标志判断*/
}
/*************************开始接收数据******************************/
while(*rbuf!='\n') /*判断数据是否接收完毕*/
{
ncount+=1;
rbuf++;
retv=read(fd,rbuf,1);
if(retv==-1)
{
perror("read");
}
}
/*******************************************************************/
printf("The data received is:\n"); /*输出接收到的数据*/
for(i=0;i<ncount;i++)
{
printf("%c",hd[i]);
}
printf("\n");
flag_close =close(fd);
if(flag_close == -1) /*判断是否成功关闭文件*/
printf("Close the Device failur!\n");
return 0;
}
/****************************结束***********************************/
上一篇:linux2.6.28-tty设备驱动学习(一)
下一篇:Linux2.6 DM9000驱动模编译为模块
- Linux 2.6.24内核注释
- Linux内核模块导出后无法调用...
- Linux/Unix inode、vnode、den...
- Linux2.6.32.2移植到Mini2440...
- linux2.6.37.4内核在XC2440开...
- shell中字符串操作
- shell中的特殊字符
- stagefright与opencore对比
- linux守护进程的几个关键地方...
- Android常用的一些make命令...
- 初学UNIX环境高级编程的,关于...
- chinaunix博客什么时候可以设...
- 一个访问量较大网站的服务器。...
- 收音机驱动与v4l2架构的关系,...
- 如何将linux驱动改为裸机驱动(...
- linux2.6.28-tty设备驱动学习(二)
- linux2.6.28-tty设备驱动学习(二) tty设备驱动详解
- 【转】linux2.6.28-tty设备驱动学习
- linux2.6.28-tty设备驱动学习(一)
- tty设备驱动程序 基于:linux2.6.28
- LINUX设备驱动之tty及console驱动(二)
- LINUX设备驱动之tty及console驱动(二)
- TTY设备驱动结构
- Linux终端tty设备驱动
- Linux终端tty设备驱动
- Linux终端tty设备驱动
- Linux终端tty设备驱动
- Linux终端tty设备驱动
- tty设备驱动注册简述
- linux2.6 设备驱动编写
- 字符设备驱动linux2.6
- linux2.6设备驱动编写
- Linux终端tty设备及驱动
- 记住这些话 他会帮你变得更完美
- 读《C缺陷与陷阱》之-函数声明理解
- 2.6内核字符设备驱动程序解析(续)
- shell十三问之exec 跟 source 差在哪? 解析
- linux2.6.28-tty设备驱动学习(一)
- linux2.6.28-tty设备驱动学习(二)
- Linux2.6 DM9000驱动模编译为模块
- 字符设备的信号量,阻塞和非阻塞,poll实现
- 进程间通信--信号量+共享内存
- ps -aux 详解
- 从PC总线到ARM的内部总线
- 控制函数assert.h, setjmp.h和signal.h
- ADS分散加载文件及其应用
- 收藏:让你吃惊的60个绝对得看的常识!!!!