69c9ed4ca64f85b8670e90be3cf36f4ff32c0e8b
[~tnikolova/compat/.git] / compat / compat-3.12.c
1 #include <linux/debugfs.h>
2 #include <linux/fs.h>
3 #include <linux/export.h>
4 #include <linux/pci.h>
5
6 #define debugfs_create_atomic_t LINUX_BACKPORT(debugfs_create_atomic_t)
7
8 static int debugfs_atomic_t_set(void *data, u64 val)
9 {
10         atomic_set((atomic_t *)data, val);
11         return 0;
12 }
13 static int debugfs_atomic_t_get(void *data, u64 *val)
14 {
15         *val = atomic_read((atomic_t *)data);
16         return 0;
17 }
18 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
19                         debugfs_atomic_t_set, "%lld\n");
20 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
21 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
22
23 /**
24  * debugfs_create_atomic_t - create a debugfs file that is used to read and
25  * write an atomic_t value
26  * @name: a pointer to a string containing the name of the file to create.
27  * @mode: the permission that the file should have
28  * @parent: a pointer to the parent dentry for this file.  This should be a
29  *          directory dentry if set.  If this parameter is %NULL, then the
30  *          file will be created in the root of the debugfs filesystem.
31  * @value: a pointer to the variable that the file should read to and write
32  *         from.
33  */
34 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
35                                  struct dentry *parent, atomic_t *value)
36 {
37         /* if there are no write bits set, make read only */
38         if (!(mode & S_IWUGO))
39                 return debugfs_create_file(name, mode, parent, value,
40                                         &fops_atomic_t_ro);
41         /* if there are no read bits set, make write only */
42         if (!(mode & S_IRUGO))
43                 return debugfs_create_file(name, mode, parent, value,
44                                         &fops_atomic_t_wo);
45
46         return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
47 }
48 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
49
50 const unsigned char pcie_link_speed[] = {
51         PCI_SPEED_UNKNOWN,              /* 0 */
52         PCIE_SPEED_2_5GT,               /* 1 */
53         PCIE_SPEED_5_0GT,               /* 2 */
54         PCIE_SPEED_8_0GT,               /* 3 */
55         PCI_SPEED_UNKNOWN,              /* 4 */
56         PCI_SPEED_UNKNOWN,              /* 5 */
57         PCI_SPEED_UNKNOWN,              /* 6 */
58         PCI_SPEED_UNKNOWN,              /* 7 */
59         PCI_SPEED_UNKNOWN,              /* 8 */
60         PCI_SPEED_UNKNOWN,              /* 9 */
61         PCI_SPEED_UNKNOWN,              /* A */
62         PCI_SPEED_UNKNOWN,              /* B */
63         PCI_SPEED_UNKNOWN,              /* C */
64         PCI_SPEED_UNKNOWN,              /* D */
65         PCI_SPEED_UNKNOWN,              /* E */
66         PCI_SPEED_UNKNOWN               /* F */
67 };
68
69 /**
70  * pcie_get_minimum_link - determine minimum link settings of a PCI device
71  * @dev: PCI device to query
72  * @speed: storage for minimum speed
73  * @width: storage for minimum width
74  *
75  * This function will walk up the PCI device chain and determine the minimum
76  * link width and speed of the device.
77  */
78 #define pcie_get_minimum_link LINUX_BACKPORT(pcie_get_minimum_link)
79 int pcie_get_minimum_link(struct pci_dev *dev, enum pci_bus_speed *speed,
80                           enum pcie_link_width *width)
81 {
82         int ret;
83
84         *speed = PCI_SPEED_UNKNOWN;
85         *width = PCIE_LNK_WIDTH_UNKNOWN;
86
87         while (dev) {
88                 u16 lnksta;
89                 enum pci_bus_speed next_speed;
90                 enum pcie_link_width next_width;
91
92                 ret = pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &lnksta);
93                 if (ret)
94                         return ret;
95
96                 next_speed = pcie_link_speed[lnksta & PCI_EXP_LNKSTA_CLS];
97                 next_width = (lnksta & PCI_EXP_LNKSTA_NLW) >>
98                         PCI_EXP_LNKSTA_NLW_SHIFT;
99
100                 if (next_speed < *speed)
101                         *speed = next_speed;
102
103                 if (next_width < *width)
104                         *width = next_width;
105
106                 dev = dev->bus->self;
107         }
108
109         return 0;
110 }
111 EXPORT_SYMBOL(pcie_get_minimum_link);