xref: /linux-6.15/include/linux/pnp.h (revision eb2bce7f)
1 /*
2  * Linux Plug and Play Support
3  * Copyright by Adam Belay <[email protected]>
4  *
5  */
6 
7 #ifndef _LINUX_PNP_H
8 #define _LINUX_PNP_H
9 
10 #ifdef __KERNEL__
11 
12 #include <linux/device.h>
13 #include <linux/list.h>
14 #include <linux/errno.h>
15 #include <linux/mod_devicetable.h>
16 
17 #define PNP_MAX_PORT		8
18 #define PNP_MAX_MEM		4
19 #define PNP_MAX_IRQ		2
20 #define PNP_MAX_DMA		2
21 #define PNP_NAME_LEN		50
22 
23 struct pnp_protocol;
24 struct pnp_dev;
25 
26 
27 /*
28  * Resource Management
29  */
30 
31 /* Use these instead of directly reading pnp_dev to get resource information */
32 #define pnp_port_start(dev,bar)   ((dev)->res.port_resource[(bar)].start)
33 #define pnp_port_end(dev,bar)     ((dev)->res.port_resource[(bar)].end)
34 #define pnp_port_flags(dev,bar)   ((dev)->res.port_resource[(bar)].flags)
35 #define pnp_port_valid(dev,bar) \
36 	((pnp_port_flags((dev),(bar)) & (IORESOURCE_IO | IORESOURCE_UNSET)) \
37 		== IORESOURCE_IO)
38 #define pnp_port_len(dev,bar) \
39 	((pnp_port_start((dev),(bar)) == 0 &&	\
40 	  pnp_port_end((dev),(bar)) ==		\
41 	  pnp_port_start((dev),(bar))) ? 0 :	\
42 	  					\
43 	 (pnp_port_end((dev),(bar)) -		\
44 	  pnp_port_start((dev),(bar)) + 1))
45 
46 #define pnp_mem_start(dev,bar)   ((dev)->res.mem_resource[(bar)].start)
47 #define pnp_mem_end(dev,bar)     ((dev)->res.mem_resource[(bar)].end)
48 #define pnp_mem_flags(dev,bar)   ((dev)->res.mem_resource[(bar)].flags)
49 #define pnp_mem_valid(dev,bar) \
50 	((pnp_mem_flags((dev),(bar)) & (IORESOURCE_MEM | IORESOURCE_UNSET)) \
51 		== IORESOURCE_MEM)
52 #define pnp_mem_len(dev,bar) \
53 	((pnp_mem_start((dev),(bar)) == 0 &&	\
54 	  pnp_mem_end((dev),(bar)) ==		\
55 	  pnp_mem_start((dev),(bar))) ? 0 :	\
56 	  					\
57 	 (pnp_mem_end((dev),(bar)) -		\
58 	  pnp_mem_start((dev),(bar)) + 1))
59 
60 #define pnp_irq(dev,bar)	 ((dev)->res.irq_resource[(bar)].start)
61 #define pnp_irq_flags(dev,bar)	 ((dev)->res.irq_resource[(bar)].flags)
62 #define pnp_irq_valid(dev,bar) \
63 	((pnp_irq_flags((dev),(bar)) & (IORESOURCE_IRQ | IORESOURCE_UNSET)) \
64 		== IORESOURCE_IRQ)
65 
66 #define pnp_dma(dev,bar)	 ((dev)->res.dma_resource[(bar)].start)
67 #define pnp_dma_flags(dev,bar)	 ((dev)->res.dma_resource[(bar)].flags)
68 #define pnp_dma_valid(dev,bar) \
69 	((pnp_dma_flags((dev),(bar)) & (IORESOURCE_DMA | IORESOURCE_UNSET)) \
70 		== IORESOURCE_DMA)
71 
72 #define PNP_PORT_FLAG_16BITADDR	(1<<0)
73 #define PNP_PORT_FLAG_FIXED	(1<<1)
74 
75 struct pnp_port {
76 	unsigned short min;		/* min base number */
77 	unsigned short max;		/* max base number */
78 	unsigned char align;		/* align boundary */
79 	unsigned char size;		/* size of range */
80 	unsigned char flags;		/* port flags */
81 	unsigned char pad;		/* pad */
82 	struct pnp_port *next;		/* next port */
83 };
84 
85 #define PNP_IRQ_NR 256
86 struct pnp_irq {
87 	DECLARE_BITMAP(map, PNP_IRQ_NR); /* bitmaks for IRQ lines */
88 	unsigned char flags;		/* IRQ flags */
89 	unsigned char pad;		/* pad */
90 	struct pnp_irq *next;		/* next IRQ */
91 };
92 
93 struct pnp_dma {
94 	unsigned char map;		/* bitmask for DMA channels */
95 	unsigned char flags;		/* DMA flags */
96 	struct pnp_dma *next;		/* next port */
97 };
98 
99 struct pnp_mem {
100 	unsigned int min;		/* min base number */
101 	unsigned int max;		/* max base number */
102 	unsigned int align;		/* align boundary */
103 	unsigned int size;		/* size of range */
104 	unsigned char flags;		/* memory flags */
105 	unsigned char pad;		/* pad */
106 	struct pnp_mem *next;		/* next memory resource */
107 };
108 
109 #define PNP_RES_PRIORITY_PREFERRED	0
110 #define PNP_RES_PRIORITY_ACCEPTABLE	1
111 #define PNP_RES_PRIORITY_FUNCTIONAL	2
112 #define PNP_RES_PRIORITY_INVALID	65535
113 
114 struct pnp_option {
115 	unsigned short priority;	/* priority */
116 	struct pnp_port *port;		/* first port */
117 	struct pnp_irq *irq;		/* first IRQ */
118 	struct pnp_dma *dma;		/* first DMA */
119 	struct pnp_mem *mem;		/* first memory resource */
120 	struct pnp_option *next;	/* used to chain dependent resources */
121 };
122 
123 struct pnp_resource_table {
124 	struct resource port_resource[PNP_MAX_PORT];
125 	struct resource mem_resource[PNP_MAX_MEM];
126 	struct resource dma_resource[PNP_MAX_DMA];
127 	struct resource irq_resource[PNP_MAX_IRQ];
128 };
129 
130 
131 /*
132  * Device Managemnt
133  */
134 
135 struct pnp_card {
136 	struct device dev;		/* Driver Model device interface */
137 	unsigned char number;		/* used as an index, must be unique */
138 	struct list_head global_list;	/* node in global list of cards */
139 	struct list_head protocol_list;	/* node in protocol's list of cards */
140 	struct list_head devices;	/* devices attached to the card */
141 
142 	struct pnp_protocol * protocol;
143 	struct pnp_id * id;		/* contains supported EISA IDs*/
144 
145 	char name[PNP_NAME_LEN];	/* contains a human-readable name */
146 	unsigned char	pnpver;		/* Plug & Play version */
147 	unsigned char	productver;	/* product version */
148 	unsigned int	serial;		/* serial number */
149 	unsigned char	checksum;	/* if zero - checksum passed */
150 	struct proc_dir_entry *procdir;	/* directory entry in /proc/bus/isapnp */
151 };
152 
153 #define global_to_pnp_card(n) list_entry(n, struct pnp_card, global_list)
154 #define protocol_to_pnp_card(n) list_entry(n, struct pnp_card, protocol_list)
155 #define to_pnp_card(n) container_of(n, struct pnp_card, dev)
156 #define pnp_for_each_card(card) \
157 	for((card) = global_to_pnp_card(pnp_cards.next); \
158 	(card) != global_to_pnp_card(&pnp_cards); \
159 	(card) = global_to_pnp_card((card)->global_list.next))
160 
161 struct pnp_card_link {
162 	struct pnp_card * card;
163 	struct pnp_card_driver * driver;
164 	void * driver_data;
165 	pm_message_t pm_state;
166 };
167 
168 static inline void *pnp_get_card_drvdata (struct pnp_card_link *pcard)
169 {
170 	return pcard->driver_data;
171 }
172 
173 static inline void pnp_set_card_drvdata (struct pnp_card_link *pcard, void *data)
174 {
175 	pcard->driver_data = data;
176 }
177 
178 struct pnp_dev {
179 	struct device dev;		/* Driver Model device interface */
180 	unsigned char number;		/* used as an index, must be unique */
181 	int status;
182 
183 	struct list_head global_list;	/* node in global list of devices */
184 	struct list_head protocol_list;	/* node in list of device's protocol */
185 	struct list_head card_list;	/* node in card's list of devices */
186 	struct list_head rdev_list;	/* node in cards list of requested devices */
187 
188 	struct pnp_protocol * protocol;
189 	struct pnp_card * card;		/* card the device is attached to, none if NULL */
190 	struct pnp_driver * driver;
191 	struct pnp_card_link * card_link;
192 
193 	struct pnp_id	* id;	/* supported EISA IDs*/
194 
195 	int active;
196 	int capabilities;
197 	struct pnp_option * independent;
198 	struct pnp_option * dependent;
199 	struct pnp_resource_table res;
200 
201 	char name[PNP_NAME_LEN];	/* contains a human-readable name */
202 	unsigned short	regs;		/* ISAPnP: supported registers */
203 	int 		flags;		/* used by protocols */
204 	struct proc_dir_entry *procent;	/* device entry in /proc/bus/isapnp */
205 	void *data;
206 };
207 
208 #define global_to_pnp_dev(n) list_entry(n, struct pnp_dev, global_list)
209 #define card_to_pnp_dev(n) list_entry(n, struct pnp_dev, card_list)
210 #define protocol_to_pnp_dev(n) list_entry(n, struct pnp_dev, protocol_list)
211 #define	to_pnp_dev(n) container_of(n, struct pnp_dev, dev)
212 #define pnp_for_each_dev(dev) \
213 	for((dev) = global_to_pnp_dev(pnp_global.next); \
214 	(dev) != global_to_pnp_dev(&pnp_global); \
215 	(dev) = global_to_pnp_dev((dev)->global_list.next))
216 #define card_for_each_dev(card,dev) \
217 	for((dev) = card_to_pnp_dev((card)->devices.next); \
218 	(dev) != card_to_pnp_dev(&(card)->devices); \
219 	(dev) = card_to_pnp_dev((dev)->card_list.next))
220 #define pnp_dev_name(dev) (dev)->name
221 
222 static inline void *pnp_get_drvdata (struct pnp_dev *pdev)
223 {
224 	return dev_get_drvdata(&pdev->dev);
225 }
226 
227 static inline void pnp_set_drvdata (struct pnp_dev *pdev, void *data)
228 {
229 	dev_set_drvdata(&pdev->dev, data);
230 }
231 
232 struct pnp_fixup {
233 	char id[7];
234 	void (*quirk_function)(struct pnp_dev *dev);	/* fixup function */
235 };
236 
237 /* config parameters */
238 #define PNP_CONFIG_NORMAL	0x0001
239 #define PNP_CONFIG_FORCE	0x0002	/* disables validity checking */
240 
241 /* capabilities */
242 #define PNP_READ		0x0001
243 #define PNP_WRITE		0x0002
244 #define PNP_DISABLE		0x0004
245 #define PNP_CONFIGURABLE	0x0008
246 #define PNP_REMOVABLE		0x0010
247 
248 #define pnp_can_read(dev)	(((dev)->protocol) && ((dev)->protocol->get) && \
249 				 ((dev)->capabilities & PNP_READ))
250 #define pnp_can_write(dev)	(((dev)->protocol) && ((dev)->protocol->set) && \
251 				 ((dev)->capabilities & PNP_WRITE))
252 #define pnp_can_disable(dev)	(((dev)->protocol) && ((dev)->protocol->disable) && \
253 				 ((dev)->capabilities & PNP_DISABLE))
254 #define pnp_can_configure(dev)	((!(dev)->active) && \
255 				 ((dev)->capabilities & PNP_CONFIGURABLE))
256 
257 #ifdef CONFIG_ISAPNP
258 extern struct pnp_protocol isapnp_protocol;
259 #define pnp_device_is_isapnp(dev) ((dev)->protocol == (&isapnp_protocol))
260 #else
261 #define pnp_device_is_isapnp(dev) 0
262 #endif
263 
264 #ifdef CONFIG_PNPBIOS
265 extern struct pnp_protocol pnpbios_protocol;
266 #define pnp_device_is_pnpbios(dev) ((dev)->protocol == (&pnpbios_protocol))
267 #else
268 #define pnp_device_is_pnpbios(dev) 0
269 #endif
270 
271 
272 /* status */
273 #define PNP_READY		0x0000
274 #define PNP_ATTACHED		0x0001
275 #define PNP_BUSY		0x0002
276 #define PNP_FAULTY		0x0004
277 
278 /* isapnp specific macros */
279 
280 #define isapnp_card_number(dev)	((dev)->card ? (dev)->card->number : -1)
281 #define isapnp_csn_number(dev)  ((dev)->number)
282 
283 /*
284  * Driver Management
285  */
286 
287 struct pnp_id {
288 	char id[PNP_ID_LEN];
289 	struct pnp_id * next;
290 };
291 
292 struct pnp_driver {
293 	char * name;
294 	const struct pnp_device_id *id_table;
295 	unsigned int flags;
296 	int  (*probe)  (struct pnp_dev *dev, const struct pnp_device_id *dev_id);
297 	void (*remove) (struct pnp_dev *dev);
298 	int  (*suspend) (struct pnp_dev *dev, pm_message_t state);
299 	int  (*resume) (struct pnp_dev *dev);
300 	struct device_driver driver;
301 };
302 
303 #define	to_pnp_driver(drv) container_of(drv, struct pnp_driver, driver)
304 
305 struct pnp_card_driver {
306 	struct list_head global_list;
307 	char * name;
308 	const struct pnp_card_device_id *id_table;
309 	unsigned int flags;
310 	int  (*probe)  (struct pnp_card_link *card, const struct pnp_card_device_id *card_id);
311 	void (*remove) (struct pnp_card_link *card);
312 	int  (*suspend) (struct pnp_card_link *card, pm_message_t state);
313 	int  (*resume) (struct pnp_card_link *card);
314 	struct pnp_driver link;
315 };
316 
317 #define	to_pnp_card_driver(drv) container_of(drv, struct pnp_card_driver, link)
318 
319 /* pnp driver flags */
320 #define PNP_DRIVER_RES_DO_NOT_CHANGE	0x0001	/* do not change the state of the device */
321 #define PNP_DRIVER_RES_DISABLE		0x0003	/* ensure the device is disabled */
322 
323 
324 /*
325  * Protocol Management
326  */
327 
328 struct pnp_protocol {
329 	struct list_head	protocol_list;
330 	char		      * name;
331 
332 	/* resource control functions */
333 	int (*get)(struct pnp_dev *dev, struct pnp_resource_table *res);
334 	int (*set)(struct pnp_dev *dev, struct pnp_resource_table *res);
335 	int (*disable)(struct pnp_dev *dev);
336 
337 	/* used by pnp layer only (look but don't touch) */
338 	unsigned char		number;		/* protocol number*/
339 	struct device		dev;		/* link to driver model */
340 	struct list_head	cards;
341 	struct list_head	devices;
342 };
343 
344 #define to_pnp_protocol(n) list_entry(n, struct pnp_protocol, protocol_list)
345 #define protocol_for_each_card(protocol,card) \
346 	for((card) = protocol_to_pnp_card((protocol)->cards.next); \
347 	(card) != protocol_to_pnp_card(&(protocol)->cards); \
348 	(card) = protocol_to_pnp_card((card)->protocol_list.next))
349 #define protocol_for_each_dev(protocol,dev) \
350 	for((dev) = protocol_to_pnp_dev((protocol)->devices.next); \
351 	(dev) != protocol_to_pnp_dev(&(protocol)->devices); \
352 	(dev) = protocol_to_pnp_dev((dev)->protocol_list.next))
353 
354 
355 extern struct bus_type pnp_bus_type;
356 
357 #if defined(CONFIG_PNP)
358 
359 /* device management */
360 int pnp_register_protocol(struct pnp_protocol *protocol);
361 void pnp_unregister_protocol(struct pnp_protocol *protocol);
362 int pnp_add_device(struct pnp_dev *dev);
363 int pnp_device_attach(struct pnp_dev *pnp_dev);
364 void pnp_device_detach(struct pnp_dev *pnp_dev);
365 extern struct list_head pnp_global;
366 
367 /* multidevice card support */
368 int pnp_add_card(struct pnp_card *card);
369 void pnp_remove_card(struct pnp_card *card);
370 int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev);
371 void pnp_remove_card_device(struct pnp_dev *dev);
372 int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card);
373 struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from);
374 void pnp_release_card_device(struct pnp_dev * dev);
375 int pnp_register_card_driver(struct pnp_card_driver * drv);
376 void pnp_unregister_card_driver(struct pnp_card_driver * drv);
377 extern struct list_head pnp_cards;
378 
379 /* resource management */
380 struct pnp_option * pnp_register_independent_option(struct pnp_dev *dev);
381 struct pnp_option * pnp_register_dependent_option(struct pnp_dev *dev, int priority);
382 int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data);
383 int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data);
384 int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data);
385 int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data);
386 void pnp_init_resource_table(struct pnp_resource_table *table);
387 int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res, int mode);
388 int pnp_auto_config_dev(struct pnp_dev *dev);
389 int pnp_validate_config(struct pnp_dev *dev);
390 int pnp_start_dev(struct pnp_dev *dev);
391 int pnp_stop_dev(struct pnp_dev *dev);
392 int pnp_activate_dev(struct pnp_dev *dev);
393 int pnp_disable_dev(struct pnp_dev *dev);
394 void pnp_resource_change(struct resource *resource, resource_size_t start,
395 				resource_size_t size);
396 
397 /* protocol helpers */
398 int pnp_is_active(struct pnp_dev * dev);
399 int compare_pnp_id(struct pnp_id * pos, const char * id);
400 int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev);
401 int pnp_register_driver(struct pnp_driver *drv);
402 void pnp_unregister_driver(struct pnp_driver *drv);
403 
404 #else
405 
406 /* device management */
407 static inline int pnp_register_protocol(struct pnp_protocol *protocol) { return -ENODEV; }
408 static inline void pnp_unregister_protocol(struct pnp_protocol *protocol) { }
409 static inline int pnp_init_device(struct pnp_dev *dev) { return -ENODEV; }
410 static inline int pnp_add_device(struct pnp_dev *dev) { return -ENODEV; }
411 static inline int pnp_device_attach(struct pnp_dev *pnp_dev) { return -ENODEV; }
412 static inline void pnp_device_detach(struct pnp_dev *pnp_dev) { ; }
413 
414 /* multidevice card support */
415 static inline int pnp_add_card(struct pnp_card *card) { return -ENODEV; }
416 static inline void pnp_remove_card(struct pnp_card *card) { ; }
417 static inline int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev) { return -ENODEV; }
418 static inline void pnp_remove_card_device(struct pnp_dev *dev) { ; }
419 static inline int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card) { return -ENODEV; }
420 static inline struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from) { return NULL; }
421 static inline void pnp_release_card_device(struct pnp_dev * dev) { ; }
422 static inline int pnp_register_card_driver(struct pnp_card_driver * drv) { return -ENODEV; }
423 static inline void pnp_unregister_card_driver(struct pnp_card_driver * drv) { ; }
424 
425 /* resource management */
426 static inline struct pnp_option * pnp_register_independent_option(struct pnp_dev *dev) { return NULL; }
427 static inline struct pnp_option * pnp_register_dependent_option(struct pnp_dev *dev, int priority) { return NULL; }
428 static inline int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data) { return -ENODEV; }
429 static inline int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data) { return -ENODEV; }
430 static inline int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data) { return -ENODEV; }
431 static inline int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data) { return -ENODEV; }
432 static inline void pnp_init_resource_table(struct pnp_resource_table *table) { }
433 static inline int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res, int mode) { return -ENODEV; }
434 static inline int pnp_auto_config_dev(struct pnp_dev *dev) { return -ENODEV; }
435 static inline int pnp_validate_config(struct pnp_dev *dev) { return -ENODEV; }
436 static inline int pnp_start_dev(struct pnp_dev *dev) { return -ENODEV; }
437 static inline int pnp_stop_dev(struct pnp_dev *dev) { return -ENODEV; }
438 static inline int pnp_activate_dev(struct pnp_dev *dev) { return -ENODEV; }
439 static inline int pnp_disable_dev(struct pnp_dev *dev) { return -ENODEV; }
440 static inline void pnp_resource_change(struct resource *resource,
441 					resource_size_t start,
442 					resource_size_t size) { }
443 
444 /* protocol helpers */
445 static inline int pnp_is_active(struct pnp_dev * dev) { return 0; }
446 static inline int compare_pnp_id(struct pnp_id * pos, const char * id) { return -ENODEV; }
447 static inline int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev) { return -ENODEV; }
448 static inline int pnp_register_driver(struct pnp_driver *drv) { return -ENODEV; }
449 static inline void pnp_unregister_driver(struct pnp_driver *drv) { ; }
450 
451 #endif /* CONFIG_PNP */
452 
453 
454 #define pnp_err(format, arg...) printk(KERN_ERR "pnp: " format "\n" , ## arg)
455 #define pnp_info(format, arg...) printk(KERN_INFO "pnp: " format "\n" , ## arg)
456 #define pnp_warn(format, arg...) printk(KERN_WARNING "pnp: " format "\n" , ## arg)
457 
458 #ifdef CONFIG_PNP_DEBUG
459 #define pnp_dbg(format, arg...) printk(KERN_DEBUG "pnp: " format "\n" , ## arg)
460 #else
461 #define pnp_dbg(format, arg...) do {} while (0)
462 #endif
463 
464 #endif /* __KERNEL__ */
465 
466 #endif /* _LINUX_PNP_H */
467