分类: LINUX
2011-05-13 18:44:48
- drivers/input/serio/hp_sdc.c
- -------------------------------------
- if (request_region(hp_sdc.data_io, 2, hp_sdc_driver.name))
- goto err0;
- 如果request_region 分配端口成功,返回非空指针。
- request_region
- -------------------------------------
- ->./include/linux/ioport.h:
- #define request_region(start,n,name) \
- __request_region(&ioport_resource, (start), (n), (name), 0)
- =====================================
- ioport_resource
- -------------------------------------
- -->kernel/resource.c
- struct resource ioport_resource = {
- .name = "PCI IO",
- .start = 0,
- .end = IO_SPACE_LIMIT,
- .flags = IORESOURCE_IO,
- };
- EXPORT_SYMBOL(ioport_resource);
- __request_region
- -------------------------------------
- -->kernel/resource.c
- struct resource * __request_region(struct resource *parent,
- resource_size_t start, resource_size_t n,
- const char *name, int flags)
- {
- struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
- if (!res)return NULL;
- res->name = name;
- res->start = start;
- res->end = start + n - 1;
- res->flags = IORESOURCE_BUSY;
- res->flags |= flags;
- write_lock(&resource_lock);
- for (;;) {
- struct resource *conflict;
- conflict = __request_resource(parent, res);
- if (!conflict) break; //申请成功
- if (conflict != parent) {
- parent = conflict;
- if (!(conflict->flags & IORESOURCE_BUSY))
- continue;
- }
- /* Uhhuh, that didn't work out.. */
- kfree(res);
- res = NULL;
- break;
- }
- write_unlock(&resource_lock);
- return res;
- }
- EXPORT_SYMBOL(__request_region);
- =====================================
- kzalloc
- -------------------------------------
- --->include/linux/slab.h
- static inline void *kzalloc(size_t size, gfp_t flags)
- {
- return kmalloc(size, flags | __GFP_ZERO);
- }
- __request_resource
- -------------------------------------
- --->kernel/resource.c
- static struct resource *
- __request_resource(struct resource *root,
- struct resource *new)
- {
- resource_size_t start = new->start;
- resource_size_t end = new->end;
- struct resource *tmp, **p;
- if (end < start) return root;
- if (start < root->start) return root;
- if (end > root->end) return root;
- p = &root->child;
- for (;;) {
- tmp = *p;
- if (!tmp || tmp->start > end) {
- new->sibling = tmp;
- *p = new;
- new->parent = root;
- return NULL; //成功
- }
- p = &tmp->sibling;
- if (tmp->end < start) continue;
- return tmp;
- }
- }
- =====================================
- lib/iomap.c
- -------------------------------------
- #define PIO_OFFSET 0x10000UL //64k
- #define PIO_MASK 0x0ffffUL
- #define PIO_RESERVED 0x40000UL //256K
- #endif
- void __iomem *ioport_map(unsigned long port, unsigned int nr)
- {
- if (port > PIO_MASK)
- return NULL;
- return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
- }
- void ioport_unmap(void __iomem *addr)
- {
- /* Nothing to do */
- }
- __iomem
- -------------------------------------
- include/linux/compiler.h
- # define __iomem __attribute__((noderef, address_space(2)))
- //对iomem地址进行检查
- -------------------------------------
- ioport_map ioport_unmap 小结
- -------------------------------------
- 从port的范围(0-0xffff) 可以看到,映射的地址是10000-0x1ffff, 即(64k) -> (128k-1)的空间。
- 还有一个是PIO_RESERVED 40000,是保留的空间。
- 在搜代码的时候,同时也会在include/asm-generic/io.h文件中出现。而其上方有 #ifndef CONFIG_GENERIC_IOMAP,个人理解是这文件处理的是没有iomap的功能情况。里面的函数大都是空。
- ==============================================================================
- ***********************************************************
- ioread8() iowrite8()
- ***********************************************************
- lib/iomap.c
- -------------------------------------
- #define PIO_RESERVED 0x40000UL
- #define PIO_OFFSET 0x10000UL
- unsigned int ioread8(void __iomem *addr)
- {
- IO_COND(addr, return inb(port), return readb(addr));
- return 0xff;
- }
- void iowrite8(u8 val, void __iomem *addr)
- {
- IO_COND(addr, outb(val,port), writeb(val, addr));
- }
- EXPORT_SYMBOL(ioread8);
- EXPORT_SYMBOL(iowrite8);
- #define IO_COND(addr, is_pio, is_mmio) do { \
- unsigned long port = (unsigned long __force)addr; \
- if (port >= PIO_RESERVED) { \
- is_mmio; \
- } else if (port > PIO_OFFSET) { \
- port &= PIO_MASK; \
- is_pio; \
- } else \
- bad_io_access(port, #is_pio ); \
- } while (0)
- static void bad_io_access(unsigned long port, const char *access)
- {
- static int count = 10;
- if (count) {
- count--;
- WARN(1, KERN_ERR "Bad IO access at port %#lx (%s)\n", port, access);
- }
- }
- WARN
- -------------------------------------
- ->include/asm-generic/bug.h
- #define WARN(condition, format...) ({ \
- int __ret_warn_on = !!(condition); \
- unlikely(__ret_warn_on); \
- })
- ioread8(addr)意思就是
- if addr>=256K then readb(addr)
- else if addr >64K then inb(addr - 64K)
- else 打印错误
- 继续追踪 inb 和 readb
- =====================================
- include/asm-generic/io.h
- -------------------------------------
- #define readb __raw_readb
- static inline u8 __raw_readb(const volatile void __iomem *addr)
- {
- return *(const volatile u8 __force *) addr;
- }
- #define writeb __raw_writeb
- static inline void __raw_writeb(u8 b, volatile void __iomem *addr)
- {
- *(volatile u8 __force *) addr = b;
- }
- -------------------------------------
- static inline u8 inb(unsigned long addr)
- {
- return readb((volatile void __iomem *) addr);
- }
- static inline void outb(u8 b, unsigned long addr)
- {
- writeb(b, (volatile void __iomem *) addr);
- }
- arch/x86/boot.c
- -------------------------------------
- static inline u8 inb(u16 port)
- {
- u8 v;
- asm volatile("inb %1,%0" : "=a" (v) : "dN" (port));
- return v;
- }
- static inline void outb(u8 v, u16 port)
- {
- asm volatile("outb %0,%1" : : "a" (v), "dN" (port));
- }