tty open failed

tty_open

   ->     if (tty) { retval = tty_reopen(tty);}

           ->

static int tty_reopen(struct tty_struct *tty)
{
    struct tty_driver *driver = tty->driver;

    if (test_bit(TTY_CLOSING, &tty->flags) ||
            test_bit(TTY_HUPPING, &tty->flags) ||
            test_bit(TTY_LDISC_CHANGING, &tty->flags))
        return -EIO;

    if (driver->type == TTY_DRIVER_TYPE_PTY &&
        driver->subtype == PTY_TYPE_MASTER) {
        /*
         * special case for PTY masters: only one open permitted,
         * and the slave side open count is incremented as well.
         */
        if (tty->count)
            return -EIO;

        tty->link->count++;
    }
    tty->count++;

    mutex_lock(&tty->ldisc_mutex);
    WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
    mutex_unlock(&tty->ldisc_mutex);

    return 0;
}



/*---------------------------------------------------------------------------*/
static void smd_ch_irq_tasklet_handler(unsigned long data)
{
    unsigned char *ptr;
    int avail;
    struct tty_struct *tty;
    int index=data;
    unsigned long flags;
    struct smd_tty_info *tty_info = &driver_info->smd_tty[index];
    dbg("enter %s\n",__func__);
    spin_lock_irqsave(&driver_info->lock, flags);
    tty = tty_port_tty_get(&tty_info->port);
    if (!tty){
        spin_unlock_irqrestore(&driver_info->lock, flags);
        return;
    }
    avail = smd_stream_read_avail(tty_info->ch);
    if (!avail){
        spin_unlock_irqrestore(&driver_info->lock, flags);
        return;
    }
    avail = tty_prepare_flip_string(tty, &ptr, avail);
    if(!avail){
        spin_unlock_irqrestore(&driver_info->lock, flags);
        pr_err("smd_ch_irq_tasklet_handler: cannot get space from tty_buffer!\n");
        return;
    }
    smd_stream_read(tty_info->ch,ptr,avail);
    smd_send_intr(tty_info->a2b_int_rx);
    tty_flip_buffer_push(tty);
    tty_kref_put(tty);
    spin_unlock_irqrestore(&driver_info->lock, flags);
}

更改为:


/*---------------------------------------------------------------------------*/
static void smd_ch_irq_tasklet_handler(unsigned long data)
{
    unsigned char *ptr;
    int avail;
    struct tty_struct *tty;
    int index=data;
    unsigned long flags;
    struct smd_tty_info *tty_info = &driver_info->smd_tty[index];
    dbg("enter %s\n",__func__);
    spin_lock_irqsave(&driver_info->lock, flags);
    tty = tty_port_tty_get(&tty_info->port);
    if (!tty){
        spin_unlock_irqrestore(&driver_info->lock, flags);
        return;
    }
    avail = smd_stream_read_avail(tty_info->ch);
    if (!avail){
        tty_kref_put(tty);
        spin_unlock_irqrestore(&driver_info->lock, flags);
        return;
    }
    avail = tty_prepare_flip_string(tty, &ptr, avail);
    if(!avail){
        tty_kref_put(tty);
        spin_unlock_irqrestore(&driver_info->lock, flags);
        pr_err("smd_ch_irq_tasklet_handler: cannot get space from tty_buffer!\n");
        return;
    }
    smd_stream_read(tty_info->ch,ptr,avail);
    smd_send_intr(tty_info->a2b_int_rx);
    tty_flip_buffer_push(tty);
    tty_kref_put(tty);
    spin_unlock_irqrestore(&driver_info->lock, flags);
}

就是说tty_port_tty_get和tty_kref_put没有成对出现

具体的原因,有时间在填上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Linux串口驱动程序的源代码,仅用于参考: ```c #include <linux/module.h> #include <linux/serial.h> #include <linux/tty.h> #include <linux/tty_flip.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/errno.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/sched.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/uaccess.h> #define DRIVER_NAME "my_serial" #define MY_MAJOR 200 #define MY_MINOR 0 #define MY_DEV_COUNT 2 #define MY_BUFFER_SIZE 256 static struct cdev my_cdev; static struct tty_driver *my_tty_driver; static struct tty_port my_tty_port; static char *my_buffer; static int my_buffer_index = 0; static struct file_operations my_fops = { .owner = THIS_MODULE, .open = my_open, .release = my_release, .read = my_read, .write = my_write, .llseek = no_llseek, }; static int my_open(struct inode *inode, struct file *file) { tty_port_open(&my_tty_port, file); return 0; } static int my_release(struct inode *inode, struct file *file) { tty_port_close(&my_tty_port, file); return 0; } static ssize_t my_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { int read_count = 0; if (my_buffer_index > 0) { read_count = my_buffer_index; if (read_count > count) { read_count = count; } if (copy_to_user(buf, my_buffer, read_count)) { read_count = -EFAULT; } else { my_buffer_index = 0; } } return read_count; } static ssize_t my_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { int write_count = 0; if (count > MY_BUFFER_SIZE) { count = MY_BUFFER_SIZE; } write_count = tty_write_room(&my_tty_port); if (write_count > count) { write_count = count; } if (copy_from_user(my_buffer, buf, write_count)) { write_count = -EFAULT; } else { my_buffer_index = write_count; tty_port_tty_wakeup(&my_tty_port); } return write_count; } static int my_tty_install(struct tty_driver *driver, struct tty_struct *tty) { tty_port_init(&my_tty_port); my_tty_port.ops = &my_tty_port_ops; my_tty_port.tty = tty; tty->driver_data = &my_tty_port; return tty_port_install(&my_tty_port, driver, tty); } static void my_tty_remove(struct tty_driver *driver, struct tty_struct *tty) { tty_port_destroy(&my_tty_port); } static const struct tty_port_operations my_tty_port_ops = { .write = my_tty_write, .flush_buffer = my_tty_flush_buffer, .tiocmget = my_tty_tiocmget, .tiocmset = my_tty_tiocmset, }; static int my_tty_write(struct tty_port *port, const char *buf, int count) { int write_count = 0; if (my_buffer_index == 0) { write_count = tty_write_room(port); if (write_count > count) { write_count = count; } if (copy_from_user(my_buffer, buf, write_count)) { write_count = -EFAULT; } else { my_buffer_index = write_count; wake_up_interruptible(&port->write_wait); } } return write_count; } static void my_tty_flush_buffer(struct tty_port *port) { my_buffer_index = 0; } static int my_tty_tiocmget(struct tty_port *port) { return 0; } static int my_tty_tiocmset(struct tty_port *port, unsigned int set, unsigned int clear) { return 0; } static int __init my_serial_init(void) { int result = 0; dev_t devno = MKDEV(MY_MAJOR, MY_MINOR); result = register_chrdev_region(devno, MY_DEV_COUNT, DRIVER_NAME); if (result < 0) { printk(KERN_WARNING "Failed to register device number %d, error %d\n", MY_MAJOR, result); goto failed_register_region; } cdev_init(&my_cdev, &my_fops); my_cdev.owner = THIS_MODULE; result = cdev_add(&my_cdev, devno, MY_DEV_COUNT); if (result < 0) { printk(KERN_WARNING "Failed to add cdev, error %d\n", result); goto failed_add_cdev; } my_tty_driver = alloc_tty_driver(MY_DEV_COUNT); if (!my_tty_driver) { printk(KERN_WARNING "Failed to allocate tty driver\n"); goto failed_alloc_tty_driver; } my_tty_driver->driver_name = DRIVER_NAME; my_tty_driver->name = "ttyMY"; my_tty_driver->install = my_tty_install; my_tty_driver->remove = my_tty_remove; tty_set_operations(my_tty_driver, &my_tty_ops); result = tty_register_driver(my_tty_driver); if (result < 0) { printk(KERN_WARNING "Failed to register tty driver, error %d\n", result); goto failed_register_tty_driver; } my_buffer = kmalloc(MY_BUFFER_SIZE, GFP_KERNEL); if (!my_buffer) { printk(KERN_WARNING "Failed to allocate buffer\n"); result = -ENOMEM; goto failed_alloc_buffer; } return 0; failed_alloc_buffer: tty_unregister_driver(my_tty_driver); failed_register_tty_driver: put_tty_driver(my_tty_driver); failed_alloc_tty_driver: cdev_del(&my_cdev); failed_add_cdev: unregister_chrdev_region(devno, MY_DEV_COUNT); failed_register_region: return result; } static void __exit my_serial_exit(void) { kfree(my_buffer); tty_unregister_driver(my_tty_driver); put_tty_driver(my_tty_driver); cdev_del(&my_cdev); unregister_chrdev_region(MKDEV(MY_MAJOR, MY_MINOR), MY_DEV_COUNT); } module_init(my_serial_init); module_exit(my_serial_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("My Serial Driver"); ``` 这个驱动程序实现了一个简单的串口设备,它包括: 1. 定义了设备驱动程序的名称和设备号。 2. 实现了设备文件的 open、release、read 和 write 操作。 3. 定义了 tty_port 和 tty_driver 结构体以及它们的操作函数。 4. 实现了 tty_driver 的 install 和 remove 操作。 5. 注册了设备驱动程序,tty_driver 和 tty_port,以及字符设备。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值