1, struct char_dev_priv_data {
struct cdev cdev;
struct device *device;
};
static int __init my_test_init(void)
{
struct char_dev_priv_data *priv_data_p = kzalloc(sizeof(char_dev_priv_data ) * MY_CHAR_MAX, GFP_KERNEL);
err = alloc_chrdev_region(&first_devno, 0 ,MY_CHAR_MAX, "XXX_NAME");
major_devno = MAJOR(first_devno);
cdev_class = class_create(THIS_MODULE,“XXX_class”);
for (i = 0; i < MY_CHAR_MAX; i++) {
devno = MKDEV(major_devno, i);
cdev_init(devno, &char_dev_fops); //用户空间文件操作集open close read write
cdev = &(priv_data_p + i) -> cdev; //(priv_data_p + i) .cdev;
cdev->owner = THIS_MODULE;
err = cdev_add(cdev, devno, 1);
/* my_devs[i].dev_name: /dev/my_dev1, /dev/my_dev2, /dev/my_dev3 */
device = device_create(cdev_class, NULL, devno, NULL, my_devs[i].dev_name);
}
}
2, sprintf在大多数场合可以代替itoa。
sprintf(buf,“%02x %02x %02x”,0xA,0xB,0xC); 指定宽度,小写,右对齐。
debug_p = debug_buff;
for(i = 0; i < count; i++) {
debug_cnt = sprintf(debug_p, “%-2X”, data[i]);
debug_p += debug_cnt;
}
printk(KERN_EMERG "%s\n", debug_buff);
3,字符分割
if ( fgets(line, sizeof(line),count)) == NULL || line[strlen(line)-1]='\n' ); //文件读完前...
static const char com[2] = ",";
char *token;
token = strtok(line, com);
while (token !=NULL) { //一直按逗号分割
token = strtok(NULL, com);
}
4, (unsigned int)(*(buff + 2)) 改为(unsigned int)buff[2]
5,用grub2引导ISO硬盘安装ubuntu方法两则-爱码网
代码:
1.grub> loopback loop (hd0,1)/ubuntu.iso
2.grub> set root=(loop)
3.grub> linux /casper/vmlinuz boot=casper iso-scan/filename=/ubuntu.iso
4.grub> initrd /casper/initrd
5.grub> boot