ldd3 chapter3 字元驅動簡單記錄

2021-07-13 17:39:48 字數 4371 閱讀 6887

動態分配節點號:

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,

const char *name)

刪除節點號:

void unregister_chrdev_region(dev_t from, unsigned count)
動態分配cdev:

struct cdev *cdev_alloc(void)
初始化cdev,並繫結file_operations:

void cdev_init(struct cdev *cdev, const struct file_operations *fops)
將cdev和節點號繫結:

int cdev_add(struct cdev *, dev_t, unsigned);
刪除cdev:

void cdev_del(struct cdev *p)
**:

#include #include #include #include #include #include #include #include #include #include loff_t scull_llseek(struct file *filp, loff_t f_ops, int flag);

ssize_t scull_read(struct file *filp, char __user * buf, size_t count, loff_t *f_ops);

ssize_t scull_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_ops);

int scull_ioctl(struct inode *p_inode, struct file *filp, unsigned int cmd, unsigned long args);

int scull_open(struct inode *p_inode, struct file *filp);

int scull_release(struct inode *p_inode, struct file *filp);

struct file_operations scull_fops =

;struct scull_qset

;struct scull_dev

;struct scull_dev g_scull_dev;

//根據index 返回p_qset結構,如果沒有就建立乙個

struct scull_qset *scull_follow(struct scull_dev* dev, int index)

} p_qset = (struct scull_qset *)kmalloc(sizeof(struct scull_qset), gfp_kernel);

if (p_qset == null)

return null;

memset(p_qset, 0, sizeof(struct scull_qset));

p_qset->data = kmalloc(dev->qset * sizeof(char *), gfp_kernel);

if (p_qset->data == null)

memset(p_qset->data, 0, dev->qset * sizeof(char *));

list_add_tail(&p_qset->qset_node, &dev->qset_head);

return p_qset;

}int scull_trim(struct scull_dev *dev)

list_del(p_qset_node);

kfree(p_qset);

} dev->size = 0;

return 0;

}loff_t scull_llseek(struct file *filp, loff_t f_ops, int flag)

ssize_t scull_read(struct file *filp, char __user * buf, size_t count, loff_t *f_ops)

p_qset = scull_follow(dev, qset_index);

if (p_qset == null || p_qset->data == null || p_qset->data[quan_index] == null)

if (count > (dev->quantum - quan_rest))

if (copy_to_user(buf, p_qset->data[quan_index] + quan_rest, count))

*f_ops += count;

retval = count;

out:

up(&dev->sem);

return retval;

}ssize_t scull_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_ops)

if (count > (dev->quantum - quan_rest))

if (copy_from_user(p_qset->data[quan_index] + quan_rest, buf, count))

*f_ops += count;

retval = count;

if (dev->size < *f_ops)

dev->size = *f_ops;

out:

up(&dev->sem);

return retval;

}int scull_ioctl(struct inode *p_inode, struct file *filp, unsigned int cmd, unsigned long args)

int scull_open(struct inode *p_inode, struct file *filp)

//如果以trunc模式開啟,則將dev內容清除

if (filp->f_flags & o_trunc)

printk(kern_err"file size = %ld\n",dev->size);

return 0;

}int scull_release(struct inode *p_inode, struct file *filp)

static int scull_init(void)

//初始化cdev結構,並繫結file_operations和節點號

cdev_init(&g_scull_dev.cdev, &scull_fops);

cdev_add(&g_scull_dev.cdev, g_scull_dev.dev_t, 4);

printk(kern_err"major = %d,minor = %d\n",major(g_scull_dev.dev_t),minor(g_scull_dev.dev_t));

//初始化裝置結構體

g_scull_dev.qset = 4096;

g_scull_dev.quantum = 4096;

init_list_head(&g_scull_dev.qset_head);

sema_init(&g_scull_dev.sem, 1);

return 0;

}static void scull_exit(void)

module_init(scull_init);

module_exit(scull_exit);

module_license("dual bsd/gpl");

makfile:

obj-m += scull.o

kdir := /home/linux-2.6.32.2

pwd = $(shell pwd)

all:

make -c $(kdir) m=$(pwd) modules

clean:

rm -rf *.o

測試:

《程式設計實踐》chapter3字串

albert einstein alberteinstein albert einstein albert einstein albert einstein albert einstein a hello a 0 3 hel 操作符 表示式 abc def abcdef ab 2 abab ab 0...

Swift文件Chapter 3 字串和字元

swift的字串型別由string型別表示,也有多種訪問方式,例如以character集合方式訪問。字串的字面量是一對雙引號包裹著的具有固定順序的字符集。一對三個引號包裹著的字串是多行字串字面量。字串開始位置為三引號後一行 意味著第乙個換行符不會計入 結束位置是三引號前一行 意味著三引號前的換行符不...

Chapter3 字串 向量和陣列

vector 初始化有多種方法,下面將這些方法詳細羅列 vector v1 定義了乙個空vector,潛在元素是t型別,執行預設初始化 vector v2 v1 v2是v1的拷貝 vector v2 v1 同上 vector v3 n,val v3包含n個重複的元素,每個的值都是val vector...