Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6.git] / drivers / pnp / base.h
1 /*
2  * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
3  *      Bjorn Helgaas <bjorn.helgaas@hp.com>
4  */
5
6 extern spinlock_t pnp_lock;
7 extern struct device_attribute pnp_interface_attrs[];
8 void *pnp_alloc(long size);
9
10 int pnp_register_protocol(struct pnp_protocol *protocol);
11 void pnp_unregister_protocol(struct pnp_protocol *protocol);
12
13 #define PNP_EISA_ID_MASK 0x7fffffff
14 void pnp_eisa_id_to_string(u32 id, char *str);
15 struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *, int id, char *pnpid);
16 struct pnp_card *pnp_alloc_card(struct pnp_protocol *, int id, char *pnpid);
17
18 int pnp_add_device(struct pnp_dev *dev);
19 struct pnp_id *pnp_add_id(struct pnp_dev *dev, char *id);
20
21 int pnp_add_card(struct pnp_card *card);
22 void pnp_remove_card(struct pnp_card *card);
23 int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev);
24 void pnp_remove_card_device(struct pnp_dev *dev);
25
26 struct pnp_port {
27         resource_size_t min;    /* min base number */
28         resource_size_t max;    /* max base number */
29         resource_size_t align;  /* align boundary */
30         resource_size_t size;   /* size of range */
31         unsigned char flags;    /* port flags */
32 };
33
34 #define PNP_IRQ_NR 256
35 typedef struct { DECLARE_BITMAP(bits, PNP_IRQ_NR); } pnp_irq_mask_t;
36
37 struct pnp_irq {
38         pnp_irq_mask_t map;     /* bitmap for IRQ lines */
39         unsigned char flags;    /* IRQ flags */
40 };
41
42 struct pnp_dma {
43         unsigned char map;      /* bitmask for DMA channels */
44         unsigned char flags;    /* DMA flags */
45 };
46
47 struct pnp_mem {
48         resource_size_t min;    /* min base number */
49         resource_size_t max;    /* max base number */
50         resource_size_t align;  /* align boundary */
51         resource_size_t size;   /* size of range */
52         unsigned char flags;    /* memory flags */
53 };
54
55 #define PNP_OPTION_DEPENDENT            0x80000000
56 #define PNP_OPTION_SET_MASK             0xffff
57 #define PNP_OPTION_SET_SHIFT            12
58 #define PNP_OPTION_PRIORITY_MASK        0xfff
59 #define PNP_OPTION_PRIORITY_SHIFT       0
60
61 #define PNP_RES_PRIORITY_PREFERRED      0
62 #define PNP_RES_PRIORITY_ACCEPTABLE     1
63 #define PNP_RES_PRIORITY_FUNCTIONAL     2
64 #define PNP_RES_PRIORITY_INVALID        PNP_OPTION_PRIORITY_MASK
65
66 struct pnp_option {
67         struct list_head list;
68         unsigned int flags;     /* independent/dependent, set, priority */
69
70         unsigned long type;     /* IORESOURCE_{IO,MEM,IRQ,DMA} */
71         union {
72                 struct pnp_port port;
73                 struct pnp_irq irq;
74                 struct pnp_dma dma;
75                 struct pnp_mem mem;
76         } u;
77 };
78
79 int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
80                               pnp_irq_mask_t *map, unsigned char flags);
81 int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
82                               unsigned char map, unsigned char flags);
83 int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
84                                resource_size_t min, resource_size_t max,
85                                resource_size_t align, resource_size_t size,
86                                unsigned char flags);
87 int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
88                               resource_size_t min, resource_size_t max,
89                               resource_size_t align, resource_size_t size,
90                               unsigned char flags);
91
92 static inline int pnp_option_is_dependent(struct pnp_option *option)
93 {
94         return option->flags & PNP_OPTION_DEPENDENT ? 1 : 0;
95 }
96
97 static inline unsigned int pnp_option_set(struct pnp_option *option)
98 {
99         return (option->flags >> PNP_OPTION_SET_SHIFT) & PNP_OPTION_SET_MASK;
100 }
101
102 static inline unsigned int pnp_option_priority(struct pnp_option *option)
103 {
104         return (option->flags >> PNP_OPTION_PRIORITY_SHIFT) &
105             PNP_OPTION_PRIORITY_MASK;
106 }
107
108 static inline unsigned int pnp_new_dependent_set(struct pnp_dev *dev,
109                                                  int priority)
110 {
111         unsigned int flags;
112
113         if (priority > PNP_RES_PRIORITY_FUNCTIONAL) {
114                 dev_warn(&dev->dev, "invalid dependent option priority %d "
115                          "clipped to %d", priority,
116                          PNP_RES_PRIORITY_INVALID);
117                 priority = PNP_RES_PRIORITY_INVALID;
118         }
119
120         flags = PNP_OPTION_DEPENDENT |
121             ((dev->num_dependent_sets & PNP_OPTION_SET_MASK) <<
122                 PNP_OPTION_SET_SHIFT) |
123             ((priority & PNP_OPTION_PRIORITY_MASK) <<
124                 PNP_OPTION_PRIORITY_SHIFT);
125
126         dev->num_dependent_sets++;
127
128         return flags;
129 }
130
131 char *pnp_option_priority_name(struct pnp_option *option);
132 void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option);
133
134 void pnp_init_resources(struct pnp_dev *dev);
135
136 void pnp_fixup_device(struct pnp_dev *dev);
137 void pnp_free_options(struct pnp_dev *dev);
138 int __pnp_add_device(struct pnp_dev *dev);
139 void __pnp_remove_device(struct pnp_dev *dev);
140
141 int pnp_check_port(struct pnp_dev *dev, struct resource *res);
142 int pnp_check_mem(struct pnp_dev *dev, struct resource *res);
143 int pnp_check_irq(struct pnp_dev *dev, struct resource *res);
144 int pnp_check_dma(struct pnp_dev *dev, struct resource *res);
145
146 char *pnp_resource_type_name(struct resource *res);
147 void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc);
148
149 void pnp_free_resources(struct pnp_dev *dev);
150 unsigned long pnp_resource_type(struct resource *res);
151
152 struct pnp_resource {
153         struct list_head list;
154         struct resource res;
155 };
156
157 void pnp_free_resource(struct pnp_resource *pnp_res);
158
159 struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
160                                           int flags);
161 struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
162                                           int flags);
163 struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
164                                          resource_size_t start,
165                                          resource_size_t end, int flags);
166 struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
167                                           resource_size_t start,
168                                           resource_size_t end, int flags);
169 struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev,
170                                           resource_size_t start,
171                                           resource_size_t end);
172
173 extern int pnp_debug;
174
175 #if defined(CONFIG_PNP_DEBUG_MESSAGES)
176 #define pnp_dbg(dev, format, arg...)                                    \
177         ({ if (pnp_debug) dev_printk(KERN_DEBUG, dev, format, ## arg); 0; })
178 #else
179 #define pnp_dbg(dev, format, arg...)                                    \
180         ({ if (0) dev_printk(KERN_DEBUG, dev, format, ## arg); 0; })
181 #endif