#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/fcntl.h> /* O_ACCMODE */
#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_*_user */
#include <linux/ioctl.h>
#include <linux/device.h>#include <linux/platform_device.h>
#include <linux/sysrq.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial_core.h>
#include <linux/serial.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/console.h>
#include <asm/io.h>
#include <asm/irq.h>//#include <asm/hardware.h>
//#include <asm/plat-s3c/regs-serial.h>
//#include <asm/arch/regs-gpio.h> #include <mach/hardware.h>
#include <plat/regs-serial.h>
#include <mach/regs-gpio.h> #define DEV_NAME "My_uart"
#define MY_UART_FIFO_SIZE 16 /* 串口FIFO的大小 */
#define RXSTAT_DUMMY_READ (0x10000000)
#define MAP_SIZE (0x100) /* 要映射的串口IO内存区大小 */ /*设备号为0,由系统自动分配主、次设备号*/
#define MY_UART_MAJOR 0
#define MY_UART_MINOR 0/* 串口发送中断号 */
#define TX_IRQ(port) ((port)->irq + 1)
/* 串口接收中断号 */
#define RX_IRQ(port) ((port)->irq)/* 允许串口接收字符的标志 */
#define tx_enabled(port) ((port)->unused[0])
/* 允许串口发送字符的标志 */
#define rx_enabled(port) ((port)->unused[1])/* 获取寄存器地址 */
#define portaddr(port, reg) ((port)->membase + (reg))/* 读8位宽的寄存器 */
#define rd_regb(port, reg) (ioread8(portaddr(port, reg)))
/* 读32位宽的寄存器 */
#define rd_regl(port, reg) (ioread32(portaddr(port, reg)))
/* 写8位宽的寄存器 */
#define wr_regb(port, reg, val) \
do { iowrite8(val, portaddr(port, reg)); } while(0)
/* 写32位宽的寄存器 */
#define wr_regl(port, reg, val) \
do { iowrite32(val, portaddr(port, reg)); } while(0) /* 串口的Tx FIFO缓存是否为空 */
unsigned int my_uart_tx_empty(struct uart_port *port)
{
int ret = 1;
unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
unsigned long ufcon = rd_regl(port, S3C2410_UFCON); if (ufcon & S3C2410_UFCON_FIFOMODE) /* 若使能了FIFO */
{
if ((ufstat & S3C2410_UFSTAT_TXMASK) != 0 || /* 0 <FIFO <=15 */
(ufstat & S3C2410_UFSTAT_TXFULL)) /* FIFO满 */
ret = 0;
}
else /* 若未使能了FIFO,则判断发送缓存和发送移位寄存器是否均为空 */
{
ret = rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
}

return ret;
}
void my_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
;
}/* 获取串口modem控制,因为uart2无modem控制,所以CTS、DSR直接返回有效 */
unsigned int my_uart_get_mctrl(struct uart_port *port)
{
return (TIOCM_CTS | TIOCM_DSR | TIOCM_CAR);
}
void my_uart_stop_tx(struct uart_port *port)
{
if (tx_enabled(port)){ /* 若串口已启动发送 */
disable_irq(TX_IRQ(port)); /* 禁止发送中断 */
tx_enabled(port) = 0; /* 设置串口为未启动发送 */
}
}
void my_uart_start_tx(struct uart_port *port)
{
if (!tx_enabled(port)){ /* 若串口未启动发送 */
enable_irq(TX_IRQ(port)); /* 使能发送中断 */
tx_enabled(port) = 1; /* 设置串口为已启动发送 */
}
}
void my_uart_send_xchar(struct uart_port *port, char ch){
printk(KERN_INFO "my_uart_enable_ms() undefined!\n");
}
void my_uart_stop_rx(struct uart_port *port)
{
if (rx_enabled(port)) /* 若串口已启动接收 */
{
disable_irq(RX_IRQ(port)); /* 禁止接收中断 */
rx_enabled(port) = 0; /* 设置串口为未启动接收 */
}
}/* 使能modem的状态信号 */
void my_uart_enable_ms(struct uart_port *port)
{
printk(KERN_INFO "my_uart_enable_ms() undefined!\n");
}void my_uart_break_ctl(struct uart_port *port, int break_state)
{
unsigned long flags;
unsigned int ucon; spin_lock_irqsave(&port->lock, flags);
ucon = rd_regl(port, S3C2410_UCON);
if (break_state)
ucon |= S3C2410_UCON_SBREAK;
else
ucon &= ~S3C2410_UCON_SBREAK; wr_regl(port, S3C2410_UCON, ucon);
spin_unlock_irqrestore(&port->lock, flags);
}/* 返回Rx FIFO已存多少数据 */
static int my_uart_rx_fifocnt(unsigned long ufstat)
{
/* 若Rx FIFO已满,返回FIFO的大小 */
if (ufstat & S3C2410_UFSTAT_RXFULL)
return MY_UART_FIFO_SIZE; /* 若FIFO未满,返回Rx FIFO已存了多少字节数据 */
return (ufstat & S3C2410_UFSTAT_RXMASK) >> S3C2410_UFSTAT_RXSHIFT;
} #define S3C2410_UERSTAT_PARITY (0x1000)
/* 串口接收中断处理函数,获取串口接收到的数据,并将这些数据递交给行规则层 */
static irqreturn_t my_uart_rx_chars(int irq, void *dev_id)
{
struct uart_port *port = dev_id;
struct tty_struct *tty = port->info->port.tty;
unsigned int ufcon, ch, flag, ufstat, uerstat;
int max_count = 64; /* 循环接收数据,最多一次中断接收64字节数据 */
while (max_count-- > 0)
{
ufcon = rd_regl(port, S3C2410_UFCON);
ufstat = rd_regl(port, S3C2410_UFSTAT); /* 若Rx FIFO无数据了,跳出循环 */
if (my_uart_rx_fifocnt(ufstat) == 0)
break; /* 读取Rx error状态寄存器 */
uerstat = rd_regl(port, S3C2410_UERSTAT);
/* 读取已接受到的数据 */
ch = rd_regb(port, S3C2410_URXH); /* insert the character into the buffer */
/* 先将tty标志设为正常 */
flag = TTY_NORMAL;
/* 递增接收字符计数器 */
port->icount.rx++; /* 判断是否存在Rx error
* if (unlikely(uerstat & S3C2410_UERSTAT_ANY))等同于
* if (uerstat & S3C2410_UERSTAT_ANY)
* 只是unlikely表示uerstat & S3C2410_UERSTAT_ANY的值为假的可能性大一些
* 另外还有一个likely(value)表示value的值为真的可能性更大一些
*/
if (unlikely(uerstat & S3C2410_UERSTAT_ANY))
{
/* 若break错误,递增icount.brk计算器 */
if (uerstat & S3C2410_UERSTAT_BREAK)
{
port->icount.brk++;
if (uart_handle_break(port))
goto ignore_char;
} /* 若frame错误,递增icount.frame计算器 */
if (uerstat & S3C2410_UERSTAT_FRAME)
port->icount.frame++;
/* 若overrun错误,递增icount.overrun计算器 */
if (uerstat & S3C2410_UERSTAT_OVERRUN)
port->icount.overrun++; /* 查看我们是否关心该Rx error
* port->read_status_mask保存着我们感兴趣的Rx error status
*/
uerstat &= port->read_status_mask; /* 若我们关心该Rx error,则将flag设置为对应的error flag */
if (uerstat & S3C2410_UERSTAT_BREAK)
flag = TTY_BREAK;
else if (uerstat & S3C2410_UERSTAT_PARITY)
flag = TTY_PARITY;
else if (uerstat & ( S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_OVERRUN))
flag = TTY_FRAME;
} /* 处理sys字符 */
if (uart_handle_sysrq_char(port, ch))
goto ignore_char; /* 将接收到的字符插入到tty设备的flip缓冲 */
uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN, ch, flag); ignore_char:
continue;
}

/* 刷新tty设备的flip缓冲,将接受到的数据传给行规则层 */
tty_flip_buffer_push(tty); return IRQ_HANDLED;
} /* 串口发送中断处理函数,将用户空间的数据(保存在环形缓冲xmit里)发送出去 */
static irqreturn_t my_uart_tx_chars(int irq, void *dev_id)
{
struct uart_port *port = dev_id;
struct circ_buf *xmit = &port->info->xmit; /* 获取环线缓冲 */
int count = 256; /* 若设置了xChar字符 */
if (port->x_char)
{
/* 将该xChar发送出去 */
wr_regb(port, S3C2410_UTXH, port->x_char);
/* 递增发送计数 */
port->icount.tx++;
/* 清除xChar */
port->x_char = 0;
/* 退出中断处理 */
goto out;
} /* 如果没有更多的字符需要发送(环形缓冲为空),
* 或者uart Tx已停止,
* 则停止uart并退出中断处理函数
*/
if (uart_circ_empty(xmit) || uart_tx_stopped(port))
{
my_uart_stop_tx(port);
goto out;
} /* 循环发送数据,直到环形缓冲为空,最多一次中断发送256字节数据 */
while (!uart_circ_empty(xmit) && count-- > 0)
{
/* 若Tx FIFO已满,退出循环 */
if (rd_regl(port, S3C2410_UFSTAT) & S3C2410_UFSTAT_TXFULL)
break; /* 将要发送的数据写入Tx FIFO */
wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
/* 移向循环缓冲中下一要发送的数据 */
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
port->icount.tx++;
} /* 如果环形缓冲区中剩余的字符少于WAKEUP_CHARS,唤醒上层 */
if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
uart_write_wakeup(port); /* 如果环形缓冲为空,则停止发送 */
if (uart_circ_empty(xmit))
my_uart_stop_tx(port); out:
return IRQ_HANDLED;
} /* 启动串口端口,在打开该驱动的设备文件时会调用该函数来申请串口中断,并设置串口为可接受,也可发送 */
int my_uart_startup(struct uart_port *port)
{
unsigned long flags;
int ret;
const char *portname = to_platform_device(port->dev)->name; /* 设置串口为不可接受,也不可发送 */
rx_enabled(port) = 0;
tx_enabled(port) = 0; spin_lock_irqsave(&port->lock, flags);
/* 申请接收中断 */
ret = request_irq(RX_IRQ(port), my_uart_rx_chars, 0, portname, port);
if (ret != 0)
{
printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
return ret;
} /* 设置串口为允许接收 */
rx_enabled(port) = 1; /* 申请发送中断 */
ret = request_irq(TX_IRQ(port), my_uart_tx_chars, 0, portname, port);
if (ret)
{
printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
rx_enabled(port) = 0;
free_irq(RX_IRQ(port), port);
goto err;
}

/* 设置串口为允许发送 */
tx_enabled(port) = 1; err:
spin_unlock_irqrestore(&port->lock, flags);
return ret;
}/* 关闭串口,在关闭驱动的设备文件时会调用该函数,释放串口中断 */
void my_uart_shutdown(struct uart_port *port)
{
rx_enabled(port) = 0; /* 设置串口为不允许接收 */
free_irq(RX_IRQ(port), port); /* 释放接收中断 */
tx_enabled(port) = 0; /* 设置串口为不允许发送 */
free_irq(TX_IRQ(port), port); /* 释放发送中断 */}
void my_uart_flush_buffer(struct uart_port *port)
{
printk(KERN_INFO "my_uart_flush_buffer() undefined!\n");
}/* 设置串口参数 */
void my_uart_set_termios(struct uart_port *port,
struct ktermios *termios,
struct ktermios *old)
{
unsigned long flags;
unsigned int baud, quot;
unsigned int ulcon, ufcon = 0;

/* 不支持moden控制信号线
* HUPCL: 关闭时挂断moden
* CMSPAR: mark or space (stick) parity
* CLOCAL: 忽略任何moden控制线
*/
termios->c_cflag &= ~(HUPCL | CMSPAR);
termios->c_cflag |= CLOCAL;

/* 获取用户设置的串口波特率,并计算分频数(串口波特率除数quot) */
baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
quot = port->custom_divisor;
else
quot = port->uartclk / baud / 16 - 1;

/* 设置数据字长 */
switch (termios->c_cflag & CSIZE)
{
case CS5:
ulcon = S3C2410_LCON_CS5;
break;
case CS6:
ulcon = S3C2410_LCON_CS6;
break;
case CS7:
ulcon = S3C2410_LCON_CS7;
break;
case CS8:
default:
ulcon = S3C2410_LCON_CS8;
break;
}

/* 是否要求设置两个停止位(CSTOPB) */
if (termios->c_cflag & CSTOPB)
ulcon |= S3C2410_LCON_STOPB;

/* 是否使用奇偶检验 */
if (termios->c_cflag & PARENB)
{
if (termios->c_cflag & PARODD) /* 奇校验 */
ulcon |= S3C2410_LCON_PODD;
else /* 偶校验 */
ulcon |= S3C2410_LCON_PEVEN;
}
else /* 无校验 */
{
ulcon |= S3C2410_LCON_PNONE;
}

if (port->fifosize > 1)
ufcon |= S3C2410_UFCON_FIFOMODE | S3C2410_UFCON_RXTRIG8;

spin_lock_irqsave(&port->lock, flags);

/* 设置FIFO控制寄存器、线控制寄存器和波特率除数寄存器 */
wr_regl(port, S3C2410_UFCON, ufcon);
wr_regl(port, S3C2410_ULCON, ulcon);
wr_regl(port, S3C2410_UBRDIV, quot);

/* 更新串口FIFO的超时时限 */
uart_update_timeout(port, termios->c_cflag, baud);

/* 设置我们感兴趣的Rx error */
port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
if (termios->c_iflag & INPCK)
port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;

/* 设置我们忽略的Rx error */
port->ignore_status_mask = 0;
if (termios->c_iflag & IGNPAR)
port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;

/* 若未设置CREAD(使用接收器),则忽略所有Rx error*/
if ((termios->c_cflag & CREAD) == 0)
port->ignore_status_mask |= RXSTAT_DUMMY_READ;

spin_unlock_irqrestore(&port->lock, flags);}
void my_uart_set_ldisc(struct uart_port *port)
{
printk(KERN_INFO "my_uart_set_ldisc() undefined!\n");}
void my_uart_pm(struct uart_port *port, unsigned int state,
unsigned int oldstate)
{
printk(KERN_INFO "my_uart_pm() undefined!\n");
}
int my_uart_set_wake(struct uart_port *port, unsigned int state)
{
printk(KERN_INFO "my_uart_set_wake() undefined!\n");
return 1;
}/*
* Return a string describing the type of the port
*/
const char *my_uart_type(struct uart_port *port)
{
/* 返回描述串口类型的字符串指针 */
return port->type == PORT_S3C2410 ? "gprs_uart:s3c2410_uart2" : NULL;
}/*
* Release IO and memory resources used by the port.
* This includes iounmap if necessary.
*/
void my_uart_release_port(struct uart_port *port)
{
/* 释放已分配IO内存 */
release_mem_region(port->mapbase, MAP_SIZE);
} /*
* Request IO and memory resources used by the port.
* This includes iomapping the port if necessary.
*/
/* 申请串口一些必要的资源,如IO端口/IO内存资源,必要时还可以重新映射串口端口 */
int my_uart_request_port(struct uart_port *port)
{
struct resource *res;
const char *name = to_platform_device(port->dev)->name;

/* request_mem_region请求分配IO内存,从开始port->mapbase,大小MAP_SIZE
* port->mapbase保存当前串口的寄存器基地址(物理)
* uart2: 0x50008000
*/
res = request_mem_region(port->mapbase, MAP_SIZE, name);
if (res == NULL)
{
printk(KERN_ERR"request_mem_region error: %p\n", res);
return -EBUSY;
}

return 0;
}
void my_uart_config_port(struct uart_port *port, int flags)
{
int retval;

/* 请求串口 */
retval = my_uart_request_port(port);
/* 设置串口类型 */
if (flags & UART_CONFIG_TYPE && retval == 0)
port->type = PORT_S3C2410;}
int my_uart_verify_port(struct uart_port *port, struct serial_struct *serial)
{
printk(KERN_INFO "my_uart_verify_port() undefined!\n");
return 1;
}
int my_uart_ioctl(struct uart_port *port, unsigned int data, unsigned long cnt)
{
printk(KERN_INFO "my_uart_ioctl() undefined!\n");
return 1;
}
#ifdef CONFIG_CONSOLE_POLL
void my_uart_poll_put_char(struct uart_port *port, unsigned char cnt)
{
printk(KERN_INFO "my_uart_poll_put_char() undefined!\n");
return 1;
}
int my_uart_poll_get_char(struct uart_port *port)
{
printk(KERN_INFO "my_uart_poll_get_char() undefined!\n");
return 1;
}
#endif/*The Uart operations struct*/
static struct uart_ops my_uart_ops ={
.tx_empty = my_uart_tx_empty,
.set_mctrl = my_uart_set_mctrl,
.get_mctrl = my_uart_get_mctrl,
.stop_tx = my_uart_stop_tx,
.start_tx = my_uart_start_tx,
.send_xchar = my_uart_send_xchar,
.stop_rx = my_uart_stop_rx,
.enable_ms = my_uart_enable_ms,
.break_ctl = my_uart_break_ctl,
.startup = my_uart_startup,
.shutdown = my_uart_shutdown,
.flush_buffer = my_uart_flush_buffer,
.set_termios = my_uart_set_termios,
.set_ldisc = my_uart_set_ldisc,
.pm = my_uart_pm,
.set_wake = my_uart_set_wake, /*
* Return a string describing the type of the port
*/
.type = my_uart_type, /*
* Release IO and memory resources used by the port.
* This includes iounmap if necessary.
*/
.release_port = my_uart_release_port, /*
* Request IO and memory resources used by the port.
* This includes iomapping the port if necessary.
*/
.request_port = my_uart_request_port,
.config_port = my_uart_config_port,
.verify_port = my_uart_verify_port,
.ioctl = my_uart_ioctl,
#ifdef CONFIG_CONSOLE_POLL
.poll_put_char = my_uart_poll_put_char,
.poll_get_char = my_uart_poll_get_char,
#endif
};/*串口驱动*/
static struct uart_driver my_uart_driver = {
.owner = THIS_MODULE,
.driver_name = DEV_NAME, /*驱动名字*/
.dev_name = DEV_NAME, /*设备名字*/
.major = MY_UART_MAJOR,
.minor = MY_UART_MINOR,
.nr = 1, /*串口的个数*/
};static struct uart_port my_uart_port = {
.irq = IRQ_S3CUART_RX2, /* IRQ */
.fifosize = MY_UART_FIFO_SIZE, /* Size of the FIFO */
.iotype = UPIO_MEM, /* IO memory */
.flags = UPF_BOOT_AUTOCONF, /* UART port flag */
.ops = &my_uart_ops, /* UART operations */
.line = 0, /* UART port number */
.lock = __SPIN_LOCK_UNLOCKED(my_uart_port.lock),
}; /* 初始化指定串口端口 */
static int my_uart_init_port(struct uart_port *port, struct platform_device *platdev)
{
unsigned long flags;
unsigned int gphcon;

if (platdev == NULL)
return -ENODEV; port->dev = &platdev->dev;
/* 设置串口波特率时钟频率 */
port->uartclk = clk_get_rate(clk_get(&platdev->dev, "pclk")); /* 设置串口的寄存器基地址(物理): 0x50008000 */
port->mapbase = S3C2410_PA_UART2;

/* 设置当前串口的寄存器基地址(虚拟): 0xF5008000 */
port->membase = S3C24XX_VA_UART + (S3C2410_PA_UART2 - S3C24XX_PA_UART); spin_lock_irqsave(&port->lock, flags);
wr_regl(port, S3C2410_UCON, S3C2410_UCON_DEFAULT);
wr_regl(port, S3C2410_ULCON, S3C2410_LCON_CS8 | S3C2410_LCON_PNONE);
wr_regl(port, S3C2410_UFCON, S3C2410_UFCON_FIFOMODE
| S3C2410_UFCON_RXTRIG8 | S3C2410_UFCON_RESETBOTH); /* 将I/O port H的gph6和gph7设置为TXD2和RXD2 */
gphcon = readl(S3C2410_GPHCON);
gphcon &= ~((0x5) << 12);
writel(gphcon, S3C2410_GPHCON);

spin_unlock_irqrestore(&port->lock, flags);

return 0;
} static int my_uart_probe(struct platform_device *dev)
{
int ret; /*初始化串口*/
ret = my_uart_init_port(&my_uart_port, dev);
if (ret != 0) {
printk(KERN_INFO "my_uart_init_port() error=%d\n", ret);
return ret;
} /*添加串口*/
ret = uart_add_one_port(&my_uart_driver, &my_uart_port);
if (ret != 0) {
printk(KERN_INFO "uart_add_one_port() error=%d\n", ret);
return ret;
} /* 将串口uart_port结构体保存在platform_device->dev->driver_data中 */
platform_set_drvdata(dev, &my_uart_port);

printk(KERN_INFO "Function my_uart_probe() is probe.\n");
return 0;
}
static int my_uart_remove(struct platform_device *device)
{
platform_set_drvdata(device, NULL);

/* 移除串口 */
uart_remove_one_port(&my_uart_driver, &my_uart_port); printk(KERN_INFO "Function my_uart_remove() is remove.\n");
return 0;
}
static int my_uart_suspen(struct platform_device *device, pm_message_t state )
{
printk(KERN_INFO "Function my_uart_suspen() is suspen.\n");
return 0;
}
static int my_uart_resume(struct platform_device *device)
{
printk(KERN_INFO "Function my_uart_resume() is resume.\n");
return 0;
} /* Platform driver for my_plat_drver */
static struct platform_driver my_platform_drver = {
.probe = my_uart_probe, /* Probe method主要职能是申请设备定义的资源(比如中断和IO内存空间)并初始化设备的硬件,注册设备的其它子系统资源(比如i2c的适配器结构体)*/
.remove = __exit_p(my_uart_remove), /* Detach method */
.suspend = my_uart_suspen, /* Power suspend */
.resume = my_uart_resume, /* Resume after a suspend */
.driver = {
.owner = THIS_MODULE,
.name = DEV_NAME, /* Driver name */
},
};/*platform详解见(设备与驱动的绑定方式)​
struct platform_device *my_platform_device;
static int __init my_init_module(void)
{
int ret = -1;
/*注册uart驱动*/
ret = uart_register_driver(&my_uart_driver);
if (ret < 0){
printk(KERN_ERR "my_init_module-->uart_register_driver() failed. ret=%d\n", ret);
return ret;
}
printk(KERN_WARNING "my_init_module-->uart_register_driver successed.\n"); /*注册platform设备,platform_device_register_simple()是直接动态构设备并注册*/
my_platform_device = platform_device_register_simple(DEV_NAME, 0, NULL, 0);
if (IS_ERR(my_platform_device)) {
ret = PTR_ERR(my_platform_device);
printk(KERN_ERR "my_init_module-->platform_device_register_simple() error. ret=%d\n", ret);
goto error_platform_device_register;
}
printk(KERN_INFO "my_init_module-->platform_device_register_simple() successed. \n"); /*platform 设备的另外一种注册方式
my_platform_device = platform_device_alloc(DEV_NAME, 1);
if (IS_ERR(my_platform_device)) {
ret = PTR_ERR(my_platform_device);
printk(KERN_ERR, "my_init_module()-->platform_device_alloc error. ret=%d\n", ret);
goto error_platform_device_register;
}
ret = platform_device_register(&my_platform_device);
if (ret < 0){
printk(KERN_ERR, "my_init_module()-->platform_device_register error. ret=%d\n", ret);
goto error_platform_device_register;
}
*/ ret = platform_driver_register(&my_platform_drver);
if (ret != 0) {
printk(KERN_INFO "my_init_module-->platform_driver_register() error. ret=%d\n", ret);
goto error_platform_driver_register;
} printk(KERN_INFO "my_init_module register success.\n");
return 0; error_platform_driver_register:
platform_driver_unregister(&my_platform_drver);
error_platform_device_register:
uart_unregister_driver(&my_uart_driver); return ret;
}static void __exit my_exit_module(void)
{
platform_driver_unregister(&my_platform_drver);
platform_device_unregister(my_platform_device);
uart_unregister_driver(&my_uart_driver);
printk(KERN_INFO "my_exit_module exit.\n");
}module_init(my_init_module);
module_exit(my_exit_module);
MODULE_AUTHOR("CL");
MODULE_LICENSE("Dual BSD/GPL");