Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * PCI Backend - Handle special overlays for broken devices. |
| 3 | * |
| 4 | * Author: Ryan Wilson <hap9@epoch.ncsc.mil> |
| 5 | * Author: Chris Bookholt <hap10@epoch.ncsc.mil> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/pci.h> |
| 10 | #include "pciback.h" |
| 11 | #include "conf_space.h" |
| 12 | #include "conf_space_quirks.h" |
| 13 | |
| 14 | LIST_HEAD(pciback_quirks); |
| 15 | |
| 16 | static inline const struct pci_device_id * |
| 17 | match_one_device(const struct pci_device_id *id, const struct pci_dev *dev) |
| 18 | { |
| 19 | if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) && |
| 20 | (id->device == PCI_ANY_ID || id->device == dev->device) && |
| 21 | (id->subvendor == PCI_ANY_ID || |
| 22 | id->subvendor == dev->subsystem_vendor) && |
| 23 | (id->subdevice == PCI_ANY_ID || |
| 24 | id->subdevice == dev->subsystem_device) && |
| 25 | !((id->class ^ dev->class) & id->class_mask)) |
| 26 | return id; |
| 27 | return NULL; |
| 28 | } |
| 29 | |
| 30 | struct pciback_config_quirk *pciback_find_quirk(struct pci_dev *dev) |
| 31 | { |
| 32 | struct pciback_config_quirk *tmp_quirk; |
| 33 | |
| 34 | list_for_each_entry(tmp_quirk, &pciback_quirks, quirks_list) |
| 35 | if (match_one_device(&tmp_quirk->devid, dev) != NULL) |
| 36 | goto out; |
| 37 | tmp_quirk = NULL; |
| 38 | printk(KERN_DEBUG |
| 39 | "quirk didn't match any device pciback knows about\n"); |
| 40 | out: |
| 41 | return tmp_quirk; |
| 42 | } |
| 43 | |
| 44 | static inline void register_quirk(struct pciback_config_quirk *quirk) |
| 45 | { |
| 46 | list_add_tail(&quirk->quirks_list, &pciback_quirks); |
| 47 | } |
| 48 | |
| 49 | int pciback_field_is_dup(struct pci_dev *dev, unsigned int reg) |
| 50 | { |
| 51 | int ret = 0; |
| 52 | struct pciback_dev_data *dev_data = pci_get_drvdata(dev); |
| 53 | struct config_field_entry *cfg_entry; |
| 54 | |
| 55 | list_for_each_entry(cfg_entry, &dev_data->config_fields, list) { |
| 56 | if (OFFSET(cfg_entry) == reg) { |
| 57 | ret = 1; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | int pciback_config_quirks_add_field(struct pci_dev *dev, struct config_field |
| 65 | *field) |
| 66 | { |
| 67 | int err = 0; |
| 68 | |
| 69 | switch (field->size) { |
| 70 | case 1: |
| 71 | field->u.b.read = pciback_read_config_byte; |
| 72 | field->u.b.write = pciback_write_config_byte; |
| 73 | break; |
| 74 | case 2: |
| 75 | field->u.w.read = pciback_read_config_word; |
| 76 | field->u.w.write = pciback_write_config_word; |
| 77 | break; |
| 78 | case 4: |
| 79 | field->u.dw.read = pciback_read_config_dword; |
| 80 | field->u.dw.write = pciback_write_config_dword; |
| 81 | break; |
| 82 | default: |
| 83 | err = -EINVAL; |
| 84 | goto out; |
| 85 | } |
| 86 | |
| 87 | pciback_config_add_field(dev, field); |
| 88 | |
| 89 | out: |
| 90 | return err; |
| 91 | } |
| 92 | |
| 93 | int pciback_config_quirks_init(struct pci_dev *dev) |
| 94 | { |
| 95 | struct pciback_config_quirk *quirk; |
| 96 | int ret = 0; |
| 97 | |
| 98 | quirk = kzalloc(sizeof(*quirk), GFP_ATOMIC); |
| 99 | if (!quirk) { |
| 100 | ret = -ENOMEM; |
| 101 | goto out; |
| 102 | } |
| 103 | |
| 104 | quirk->devid.vendor = dev->vendor; |
| 105 | quirk->devid.device = dev->device; |
| 106 | quirk->devid.subvendor = dev->subsystem_vendor; |
| 107 | quirk->devid.subdevice = dev->subsystem_device; |
| 108 | quirk->devid.class = 0; |
| 109 | quirk->devid.class_mask = 0; |
| 110 | quirk->devid.driver_data = 0UL; |
| 111 | |
| 112 | quirk->pdev = dev; |
| 113 | |
| 114 | register_quirk(quirk); |
| 115 | out: |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | void pciback_config_field_free(struct config_field *field) |
| 120 | { |
| 121 | kfree(field); |
| 122 | } |
| 123 | |
| 124 | int pciback_config_quirk_release(struct pci_dev *dev) |
| 125 | { |
| 126 | struct pciback_config_quirk *quirk; |
| 127 | int ret = 0; |
| 128 | |
| 129 | quirk = pciback_find_quirk(dev); |
| 130 | if (!quirk) { |
| 131 | ret = -ENXIO; |
| 132 | goto out; |
| 133 | } |
| 134 | |
| 135 | list_del(&quirk->quirks_list); |
| 136 | kfree(quirk); |
| 137 | |
| 138 | out: |
| 139 | return ret; |
| 140 | } |