1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * compat.c - A series of functions to make it easier to convert drivers that use
4  *            the old isapnp APIs. If possible use the new APIs instead.
5  *
6  * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/isapnp.h>
11 #include <linux/string.h>
12 
pnp_convert_id(char * buf,unsigned short vendor,unsigned short device)13 static void pnp_convert_id(char *buf, unsigned short vendor,
14 			   unsigned short device)
15 {
16 	sprintf(buf, "%c%c%c%x%x%x%x",
17 		'A' + ((vendor >> 2) & 0x3f) - 1,
18 		'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
19 		'A' + ((vendor >> 8) & 0x1f) - 1,
20 		(device >> 4) & 0x0f, device & 0x0f,
21 		(device >> 12) & 0x0f, (device >> 8) & 0x0f);
22 }
23 
pnp_find_dev(struct pnp_card * card,unsigned short vendor,unsigned short function,struct pnp_dev * from)24 struct pnp_dev *pnp_find_dev(struct pnp_card *card, unsigned short vendor,
25 			     unsigned short function, struct pnp_dev *from)
26 {
27 	char id[8];
28 	char any[8];
29 
30 	pnp_convert_id(id, vendor, function);
31 	pnp_convert_id(any, ISAPNP_ANY_ID, ISAPNP_ANY_ID);
32 	if (card == NULL) {	/* look for a logical device from all cards */
33 		struct list_head *list;
34 
35 		list = pnp_global.next;
36 		if (from)
37 			list = from->global_list.next;
38 
39 		while (list != &pnp_global) {
40 			struct pnp_dev *dev = global_to_pnp_dev(list);
41 
42 			if (compare_pnp_id(dev->id, id) ||
43 			    (memcmp(id, any, 7) == 0))
44 				return dev;
45 			list = list->next;
46 		}
47 	} else {
48 		struct list_head *list;
49 
50 		list = card->devices.next;
51 		if (from) {
52 			list = from->card_list.next;
53 			if (from->card != card)	/* something is wrong */
54 				return NULL;
55 		}
56 		while (list != &card->devices) {
57 			struct pnp_dev *dev = card_to_pnp_dev(list);
58 
59 			if (compare_pnp_id(dev->id, id))
60 				return dev;
61 			list = list->next;
62 		}
63 	}
64 	return NULL;
65 }
66 
67 EXPORT_SYMBOL(pnp_find_dev);
68