compat.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <linux/module.h>
  9. #include <linux/isapnp.h>
  10. #include <linux/string.h>
  11. static void pnp_convert_id(char *buf, unsigned short vendor,
  12. unsigned short device)
  13. {
  14. sprintf(buf, "%c%c%c%x%x%x%x",
  15. 'A' + ((vendor >> 2) & 0x3f) - 1,
  16. 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
  17. 'A' + ((vendor >> 8) & 0x1f) - 1,
  18. (device >> 4) & 0x0f, device & 0x0f,
  19. (device >> 12) & 0x0f, (device >> 8) & 0x0f);
  20. }
  21. struct pnp_card *pnp_find_card(unsigned short vendor, unsigned short device,
  22. struct pnp_card *from)
  23. {
  24. char id[8];
  25. char any[8];
  26. struct list_head *list;
  27. pnp_convert_id(id, vendor, device);
  28. pnp_convert_id(any, ISAPNP_ANY_ID, ISAPNP_ANY_ID);
  29. list = from ? from->global_list.next : pnp_cards.next;
  30. while (list != &pnp_cards) {
  31. struct pnp_card *card = global_to_pnp_card(list);
  32. if (compare_pnp_id(card->id, id) || (memcmp(id, any, 7) == 0))
  33. return card;
  34. list = list->next;
  35. }
  36. return NULL;
  37. }
  38. struct pnp_dev *pnp_find_dev(struct pnp_card *card, unsigned short vendor,
  39. unsigned short function, struct pnp_dev *from)
  40. {
  41. char id[8];
  42. char any[8];
  43. pnp_convert_id(id, vendor, function);
  44. pnp_convert_id(any, ISAPNP_ANY_ID, ISAPNP_ANY_ID);
  45. if (card == NULL) { /* look for a logical device from all cards */
  46. struct list_head *list;
  47. list = pnp_global.next;
  48. if (from)
  49. list = from->global_list.next;
  50. while (list != &pnp_global) {
  51. struct pnp_dev *dev = global_to_pnp_dev(list);
  52. if (compare_pnp_id(dev->id, id) ||
  53. (memcmp(id, any, 7) == 0))
  54. return dev;
  55. list = list->next;
  56. }
  57. } else {
  58. struct list_head *list;
  59. list = card->devices.next;
  60. if (from) {
  61. list = from->card_list.next;
  62. if (from->card != card) /* something is wrong */
  63. return NULL;
  64. }
  65. while (list != &card->devices) {
  66. struct pnp_dev *dev = card_to_pnp_dev(list);
  67. if (compare_pnp_id(dev->id, id))
  68. return dev;
  69. list = list->next;
  70. }
  71. }
  72. return NULL;
  73. }
  74. EXPORT_SYMBOL(pnp_find_card);
  75. EXPORT_SYMBOL(pnp_find_dev);