Linux errno.h和delay.h

本文总结了Linux内核3.14.52中<errno.h>定义的错误代码及其含义,并介绍了delay.h文件中的延时函数mdelay(), udelay()和ndelay(),特别指出ndelay()实现原理及限制。

两个小知识点总结,基于Linux3.14.52:

1、<errno.h>位于linux-3.14.52\include\uapi\asm-generic


#define	EPERM		 1	/* Operation not permitted */
#define	ENOENT		 2	/* No such file or directory */
#define	ESRCH		 3	/* No such process */
#define	EINTR		 4	/* Interrupted system call */
#define	EIO		 5	/* I/O error */
#define	ENXIO		 6	/* No such device or address */
#define	E2BIG		 7	/* Argument list too long */
#define	ENOEXEC		 8	/* Exec format error */
#define	EBADF		 9	/* Bad file number */
#define	ECHILD		10	/* No child processes */
#define	EAGAIN		11	/* Try again */
#define	ENOMEM		12	/* Out of memory */
#define	EACCES		13	/* Permission denied */
#define	EFAULT		14	/* Bad address */
#define	ENOTBLK		15	/* Block device required */
#define	EBUSY		16	/* Device or resource busy */
#define	EEXIST		17	/* File exists */
#define	EXDEV		18	/* Cross-device link */
#define	ENODEV		19	/* No such device */
#define	ENOTDIR		20	/* Not a directory */
#define	EISDIR		21	/* Is a directory */
#define	EINVAL		22	/* Invalid argument */
#define	ENFILE		23	/* File table overflow */
#define	EMFILE		24	/* Too many open files */
#define	ENOTTY		25	/* Not a typewriter */
#define	ETXTBSY		26	/* Text file busy */
#define	EFBIG		27	/* File too large */
#define	ENOSPC		28	/* No space left on device */
#define	ESPIPE		29	/* Illegal seek */
#define	EROFS		30	/* Read-only file system */
#define	EMLINK		31	/* Too many links */
#define	EPIPE		32	/* Broken pipe */
#define	EDOM		33	/* Math argument out of domain of func */
#define	ERANGE		34	/* Math result not representable */
#define	EDEADLK		35	/* Resource deadlock would occur */
#define	ENAMETOOLONG	36	/* File name too long */
#define	ENOLCK		37	/* No record locks available */
#define	ENOSYS		38	/* Function not implemented */
#define	ENOTEMPTY	39	/* Directory not empty */
#define	ELOOP		40	/* Too many symbolic links encountered */
#define	EWOULDBLOCK	EAGAIN	/* Operation would block */
#define	ENOMSG		42	/* No message of desired type */
#define	EIDRM		43	/* Identifier removed */
#define	ECHRNG		44	/* Channel number out of range */
#define	EL2NSYNC	45	/* Level 2 not synchronized */
#define	EL3HLT		46	/* Level 3 halted */
#define	EL3RST		47	/* Level 3 reset */
#define	ELNRNG		48	/* Link number out of range */
#define	EUNATCH		49	/* Protocol driver not attached */
#define	ENOCSI		50	/* No CSI structure available */
#define	EL2HLT		51	/* Level 2 halted */
#define	EBADE		52	/* Invalid exchange */
#define	EBADR		53	/* Invalid request descriptor */
#define	EXFULL		54	/* Exchange full */
#define	ENOANO		55	/* No anode */
#define	EBADRQC		56	/* Invalid request code */
#define	EBADSLT		57	/* Invalid slot */

#define	EDEADLOCK	EDEADLK

#define	EBFONT		59	/* Bad font file format */
#define	ENOSTR		60	/* Device not a stream */
#define	ENODATA		61	/* No data available */
#define	ETIME		62	/* Timer expired */
#define	ENOSR		63	/* Out of streams resources */
#define	ENONET		64	/* Machine is not on the network */
#define	ENOPKG		65	/* Package not installed */
#define	EREMOTE		66	/* Object is remote */
#define	ENOLINK		67	/* Link has been severed */
#define	EADV		68	/* Advertise error */
#define	ESRMNT		69	/* Srmount error */
#define	ECOMM		70	/* Communication error on send */
#define	EPROTO		71	/* Protocol error */
#define	EMULTIHOP	72	/* Multihop attempted */
#define	EDOTDOT		73	/* RFS specific error */
#define	EBADMSG		74	/* Not a data message */
#define	EOVERFLOW	75	/* Value too large for defined data type */
#define	ENOTUNIQ	76	/* Name not unique on network */
#define	EBADFD		77	/* File descriptor in bad state */
#define	EREMCHG		78	/* Remote address changed */
#define	ELIBACC		79	/* Can not access a needed shared library */
#define	ELIBBAD		80	/* Accessing a corrupted shared library */
#define	ELIBSCN		81	/* .lib section in a.out corrupted */
#define	ELIBMAX		82	/* Attempting to link in too many shared libraries */
#define	ELIBEXEC	83	/* Cannot exec a shared library directly */
#define	EILSEQ		84	/* Illegal byte sequence */
#define	ERESTART	85	/* Interrupted system call should be restarted */
#define	ESTRPIPE	86	/* Streams pipe error */
#define	EUSERS		87	/* Too many users */
#define	ENOTSOCK	88	/* Socket operation on non-socket */
#define	EDESTADDRREQ	89	/* Destination address required */
#define	EMSGSIZE	90	/* Message too long */
#define	EPROTOTYPE	91	/* Protocol wrong type for socket */
#define	ENOPROTOOPT	92	/* Protocol not available */
#define	EPROTONOSUPPORT	93	/* Protocol not supported */
#define	ESOCKTNOSUPPORT	94	/* Socket type not supported */
#define	EOPNOTSUPP	95	/* Operation not supported on transport endpoint */
#define	EPFNOSUPPORT	96	/* Protocol family not supported */
#define	EAFNOSUPPORT	97	/* Address family not supported by protocol */
#define	EADDRINUSE	98	/* Address already in use */
#define	EADDRNOTAVAIL	99	/* Cannot assign requested address */
#define	ENETDOWN	100	/* Network is down */
#define	ENETUNREACH	101	/* Network is unreachable */
#define	ENETRESET	102	/* Network dropped connection because of reset */
#define	ECONNABORTED	103	/* Software caused connection abort */
#define	ECONNRESET	104	/* Connection reset by peer */
#define	ENOBUFS		105	/* No buffer space available */
#define	EISCONN		106	/* Transport endpoint is already connected */
#define	ENOTCONN	107	/* Transport endpoint is not connected */
#define	ESHUTDOWN	108	/* Cannot send after transport endpoint shutdown */
#define	ETOOMANYREFS	109	/* Too many references: cannot splice */
#define	ETIMEDOUT	110	/* Connection timed out */
#define	ECONNREFUSED	111	/* Connection refused */
#define	EHOSTDOWN	112	/* Host is down */
#define	EHOSTUNREACH	113	/* No route to host */
#define	EALREADY	114	/* Operation already in progress */
#define	EINPROGRESS	115	/* Operation now in progress */
#define	ESTALE		116	/* Stale file handle */
#define	EUCLEAN		117	/* Structure needs cleaning */
#define	ENOTNAM		118	/* Not a XENIX named type file */
#define	ENAVAIL		119	/* No XENIX semaphores available */
#define	EISNAM		120	/* Is a named type file */
#define	EREMOTEIO	121	/* Remote I/O error */
#define	EDQUOT		122	/* Quota exceeded */

#define	ENOMEDIUM	123	/* No medium found */
#define	EMEDIUMTYPE	124	/* Wrong medium type */
#define	ECANCELED	125	/* Operation Canceled */
#define	ENOKEY		126	/* Required key not available */
#define	EKEYEXPIRED	127	/* Key has expired */
#define	EKEYREVOKED	128	/* Key has been revoked */
#define	EKEYREJECTED	129	/* Key was rejected by service */

/* for robust mutexes */
#define	EOWNERDEAD	130	/* Owner died */
#define	ENOTRECOVERABLE	131	/* State not recoverable */

#define ERFKILL		132	/* Operation not possible due to RF-kill */

#define EHWPOISON	133	/* Memory page has hardware error */



2、delay.h位于inux-3.14.52\include\linux,其中包括mdelay()、udelay()和ndelay()三个函数,分别代表ms、us、ns级别的延时。但是,ndelay()是假的!!!

Linux只能提供微秒级别的延时,所谓纳秒,其实也是利用比它大一级的微秒延时做的。所以ndelay(1)和ndelay(999)是一样的,都将转换为udelay(1)

#ifndef mdelay
#define mdelay(n)    (/
(__builtin_constant_p(n) && (n)<=MAX_UDELAY_MS) ? udelay((n)*1000) : /
({unsigned long __ms=(n); while (__ms--) udelay(1000);}))
#endif

#ifndef ndelay
#define ndelay(x) udelay(((x)+999)/1000)
#endif


#include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/sched.h> #include <linux/delay.h> #include <linux/platform_device.h> #include <linux/spinlock.h> #include <asm/mach-types.h> #include <linux/gpio.h> #include <linux/poll.h> #include <asm/gpio.h> #include <linux/jiffies.h> #include <linux/irq.h> #include <linux/interrupt.h> #include <asm/delay.h> #include <linux/clk.h> #include <mach/gpio.h> #include <mach/soc.h> #include <mach/platform.h> #include <linux/miscdevice.h> #include <linux/fs.h> #include <asm/uaccess.h> #define DEVICE_NAME "switchk7_ctl" #define GPIOC_NAME "switchk_gpio" #define GPIO_E31 (PAD_GPIO_E + 31) static int switchk7_open(struct inode *inode,struct file *filp) { int ret = nonseekable_open(inode, filp); if (ret < 0) { return ret; } return ret; } /* 关闭文件 */ static int switchk7_release(struct inode *inode,struct file *filp) { return 0; } static ssize_t switchk7_read(struct file *filp, char *buffer, size_t count, loff_t *f_pos) { ssize_t len; char str[2]; unsigned char ucKey = 0; ucKey = gpio_get_value(GPIO_E31); len = sprintf(str, "%d\n", ucKey); if (count >= len) { int r = copy_to_user(buffer, str, len); return r ? r : len; } return ucKey; } static struct file_operations smartxc4418_switchk7_fops = { .owner = THIS_MODULE, .open = switchk7_open, .read = switchk7_read, .release = switchk7_release, }; static struct miscdevice misc = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &smartxc4418_switchk7_fops, }; static int __init smartxc4418_switchk7_dev_init(void) { int ret; ret = gpio_request(GPIO_E31, GPIOC_NAME); if (ret) { printk("request GPIO %d for button_ctl failed\n", GPIO_E31); return ret; } ret = misc_register(&misc); printk ("[***" DEVICE_NAME "***]:initialized\n"); return ret; } static void __exit smartxc4418_switchk7_dev_exit(void) { misc_deregister(&misc); gpio_free(GPIO_E31); /* 卸载设备驱动所占有的资源 */ printk("button_ctl driver uninstalled\n"); } module_init(smartxc4418_switchk7_dev_init);/* 初始化设备驱动程序的入口 */ module_exit(smartxc4418_switchk7_dev_exit);/* 卸载设备驱动程序的入口 */ MODULE_LICENSE("GPL"); MODULE_AUTHOR("LiJian CUMT."); 控制/dev/relay_ctl/dev/switchk7_ctl的具体C语言代码 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <fcntl.h> #include <linux/fs.h> #include <errno.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> #include <string.h> static int fdc = -1; int main(int argc, char* argv[]) { int fd,len,ret; fdc=open("/dev/relay_ctl",O_RDWR); unsigned char buffer[2]; unsigned char result; fd = open("/dev/switchk7_ctl", 0); if (fd < 0) { perror("open switchk7 device:"); return 1; } while(1) { ret = read(fd, buffer, sizeof(buffer)); if(ret <= 0) { printf("Switch K7 read failed!\n"); sleep(1); } else { len = sscanf(buffer, "%d\n", &result); printf("result : %d\n",result); if(result==0) { printf("K7:0"); ioctl(fdc, 0, 1); ioctl(fdc, 1, 1); } else if(result==1){ printf("K7:1"); ioctl(fdc, 0, 0); ioctl(fdc, 1, 0); } else if(result==2){ printf("K7:0"); ioctl(fdc, 0, 1); ioctl(fdc, 1, 1); } else if(result==3){ printf("K7:1"); ioctl(fdc, 0, 0); ioctl(fdc, 1, 0); } printf("\n\n"); sleep(2); } } close(fd); return 0; } 修改为控制蜂鸣器
最新发布
12-31
#include <linux/types.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/init.h> #include <linux/module.h> #include <linux/errno.h> #include <linux/gpio.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/of.h> #include <linux/of_address.h> #include <linux/of_gpio.h> #include <asm/uaccess.h> #include <asm/io.h> #include <linux/platform_device.h> struct led_driver_data { uint32_t nr; uint32_t ng; uint32_t nb; uint32_t nw; uint32_t s1; uint32_t s2; uint32_t s3; uint32_t nr_flag; uint32_t ng_flag; uint32_t nb_flag; uint32_t nw_flag; uint32_t s1_flag; uint32_t s2_flag; uint32_t s3_flag; struct device *dev; }; struct led_driver_data *g_data; #define SPRD_LEDS_OF_DEVICE_ID_NAME "sprd,led_cpe" #define SPRD_LEDS_SYSFS_GROUP_NAME "led_cpe" #define LED_LOGI(fmt, ...)\ pr_info("[LED] %s:" fmt "", __func__, ##__VA_ARGS__) #define LED_LOGD(fmt, ...)\ pr_debug("[LED] %s:" fmt "", __func__, ##__VA_ARGS__) #define LED_LOGE(fmt, ...)\ pr_err("[LED] %s:" fmt "", __func__, ##__VA_ARGS__) static ssize_t nr_show(struct device *dev, struct device_attribute *attr, char *buf) { static uint32_t gpio_nr = 0; gpio_nr = gpio_get_value(g_data->nr); LED_LOGI("get gpio_nr=%d", gpio_nr); return sprintf(buf, "%d", gpio_nr); } static ssize_t nr_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { static uint32_t gpio_nr = 0; if (kstrtouint(buf, 10, &gpio_nr) != 0) return -EINVAL; LED_LOGI("set gpio_nr=%d, count=%zu", gpio_nr, count); if(gpio_nr == 1) { gpio_set_value(g_data->nr, gpio_nr); } else if(gpio_nr == 0){ gpio_set_value(g_data->nr, gpio_nr); } return count; } static ssize_t ng_show(struct device *dev, struct device_attribute *attr, char *buf) { static uint32_t gpio_ng = 0; gpio_ng = g
03-22
/** *my first driver * */ #include <linux/types.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/ide.h> #include <linux/init.h> #include <linux/module.h> #include <linux/errno.h> #include <linux/gpio.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/of.h> #include <linux/of_address.h> #include <linux/of_irq.h> #define LED_MAJOR 200 //主设备号 #define LED_NAME "LED" //驱动名称 #define NEWCHRLED_NAME "newchrled" #define NEWCHRLED_COUNT 1 struct newchrled_dev { struct cdev cdev; struct class *class;/*类:为了自动创建节点*/ struct device *device;/*设备:为了自动创建节点*/ dev_t devid; //设备号 int major; //主设备号 int minor; //次设备号 }; struct newchrled_dev newchrled; //led设备 #if 0 backlight { compatible = "pwm-backlight"; pwms = <&pwm1 0 5000000>; brightness-levels = <0 4 8 16 32 64 128 255>; default-brightness-level = <7>; status = "okay"; }; #endif static int __init dtsof_init(void) { int ret = 0; // /*1、找到backlight节点,/路径是:/backlight /*/ struct device_node *bl_nd = NULL;/*节点*/ struct property *comppro = NULL; const char **str = NULL; u32 def_value = 0; /*内核动态申请内存*/ u32 *brival; u32 elemsize = 0; bl_nd = of_find_node_by_path("/backlight"); if (bl_nd == NULL){ /*失败*/ ret = -EINVAL; goto fail_findnd; } /*2、获取属性*/ comppro = of_find_property(bl_nd,"compatible",NULL); if (comppro == NULL){ /*失败*/ ret = -EINVAL; goto fail_findpro; }else{ printk("compatible = %s",(char*)comppro->value); } ret = of_property_read_string(bl_nd,"status",str); if (ret < 0){ goto fail_rs; } /*3、获取数字属性值*/ ret = of_property_read_u32(bl_nd,"default-brightness-level",&def_value); if (ret<0){ goto fail_read32; }else{ printk("default-brightness-level = %d \r\n",def_value); } /*4、获取数组类型的属性*/ elemsize = of_property_count_elems_of_size(bl_nd,"brightness-levels",sizeof(u32)); if (elemsize<0){ ret = -ENAVAIL; goto fail_readele; }else{ printk("brightness-levels = %d \r\n",ret); } /*申请内存*/ brival = kmalloc(elemsize * sizeof(u32),GFP_KERNEL); if(!brival){ ret = -EINVAL; goto fail_mem; } /*获取数组*/ ret = of_property_read_u32_array(bl_nd,"brightness-levels",brival,elemsize); if (ret<0){ ret = -EINVAL; goto fail_read32array; } else { u8 i = 0; for (i = 0; i < elemsize; i++){ printk("brightness-levels[%d] = %d \r\n",i,*(brival+i)); } } kfree(brival); return 0; fail_read32: fail_findnd: fail_findpro: fail_rs: fail_readele: fail_mem: fail_read32array: return ret; } static void __exit dtsof_exit(void) { } //模块加载函数 module_init(dtsof_init); //模块卸载 module_exit(dtsof_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("qhy"); 解释详细
03-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值