rtw88: debug: Fix uninitialized memory in debugfs code
[linux-2.6-microblaze.git] / drivers / misc / mic / bus / mic_bus.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel MIC Platform Software Stack (MPSS)
4  *
5  * Copyright(c) 2014 Intel Corporation.
6  *
7  * Intel MIC Bus driver.
8  *
9  * This implementation is very similar to the the virtio bus driver
10  * implementation @ drivers/virtio/virtio.c
11  */
12 #include <linux/dma-map-ops.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/idr.h>
16 #include <linux/mic_bus.h>
17
18 static ssize_t device_show(struct device *d,
19                            struct device_attribute *attr, char *buf)
20 {
21         struct mbus_device *dev = dev_to_mbus(d);
22         return sprintf(buf, "0x%04x\n", dev->id.device);
23 }
24 static DEVICE_ATTR_RO(device);
25
26 static ssize_t vendor_show(struct device *d,
27                            struct device_attribute *attr, char *buf)
28 {
29         struct mbus_device *dev = dev_to_mbus(d);
30         return sprintf(buf, "0x%04x\n", dev->id.vendor);
31 }
32 static DEVICE_ATTR_RO(vendor);
33
34 static ssize_t modalias_show(struct device *d,
35                              struct device_attribute *attr, char *buf)
36 {
37         struct mbus_device *dev = dev_to_mbus(d);
38         return sprintf(buf, "mbus:d%08Xv%08X\n",
39                        dev->id.device, dev->id.vendor);
40 }
41 static DEVICE_ATTR_RO(modalias);
42
43 static struct attribute *mbus_dev_attrs[] = {
44         &dev_attr_device.attr,
45         &dev_attr_vendor.attr,
46         &dev_attr_modalias.attr,
47         NULL,
48 };
49 ATTRIBUTE_GROUPS(mbus_dev);
50
51 static inline int mbus_id_match(const struct mbus_device *dev,
52                                 const struct mbus_device_id *id)
53 {
54         if (id->device != dev->id.device && id->device != MBUS_DEV_ANY_ID)
55                 return 0;
56
57         return id->vendor == MBUS_DEV_ANY_ID || id->vendor == dev->id.vendor;
58 }
59
60 /*
61  * This looks through all the IDs a driver claims to support.  If any of them
62  * match, we return 1 and the kernel will call mbus_dev_probe().
63  */
64 static int mbus_dev_match(struct device *dv, struct device_driver *dr)
65 {
66         unsigned int i;
67         struct mbus_device *dev = dev_to_mbus(dv);
68         const struct mbus_device_id *ids;
69
70         ids = drv_to_mbus(dr)->id_table;
71         for (i = 0; ids[i].device; i++)
72                 if (mbus_id_match(dev, &ids[i]))
73                         return 1;
74         return 0;
75 }
76
77 static int mbus_uevent(struct device *dv, struct kobj_uevent_env *env)
78 {
79         struct mbus_device *dev = dev_to_mbus(dv);
80
81         return add_uevent_var(env, "MODALIAS=mbus:d%08Xv%08X",
82                               dev->id.device, dev->id.vendor);
83 }
84
85 static int mbus_dev_probe(struct device *d)
86 {
87         int err;
88         struct mbus_device *dev = dev_to_mbus(d);
89         struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
90
91         err = drv->probe(dev);
92         if (!err)
93                 if (drv->scan)
94                         drv->scan(dev);
95         return err;
96 }
97
98 static int mbus_dev_remove(struct device *d)
99 {
100         struct mbus_device *dev = dev_to_mbus(d);
101         struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
102
103         drv->remove(dev);
104         return 0;
105 }
106
107 static struct bus_type mic_bus = {
108         .name  = "mic_bus",
109         .match = mbus_dev_match,
110         .dev_groups = mbus_dev_groups,
111         .uevent = mbus_uevent,
112         .probe = mbus_dev_probe,
113         .remove = mbus_dev_remove,
114 };
115
116 int mbus_register_driver(struct mbus_driver *driver)
117 {
118         driver->driver.bus = &mic_bus;
119         return driver_register(&driver->driver);
120 }
121 EXPORT_SYMBOL_GPL(mbus_register_driver);
122
123 void mbus_unregister_driver(struct mbus_driver *driver)
124 {
125         driver_unregister(&driver->driver);
126 }
127 EXPORT_SYMBOL_GPL(mbus_unregister_driver);
128
129 static void mbus_release_dev(struct device *d)
130 {
131         struct mbus_device *mbdev = dev_to_mbus(d);
132         kfree(mbdev);
133 }
134
135 struct mbus_device *
136 mbus_register_device(struct device *pdev, int id, const struct dma_map_ops *dma_ops,
137                      struct mbus_hw_ops *hw_ops, int index,
138                      void __iomem *mmio_va)
139 {
140         int ret;
141         struct mbus_device *mbdev;
142
143         mbdev = kzalloc(sizeof(*mbdev), GFP_KERNEL);
144         if (!mbdev)
145                 return ERR_PTR(-ENOMEM);
146
147         mbdev->mmio_va = mmio_va;
148         mbdev->dev.parent = pdev;
149         mbdev->id.device = id;
150         mbdev->id.vendor = MBUS_DEV_ANY_ID;
151         mbdev->dev.dma_ops = dma_ops;
152         mbdev->dev.dma_mask = &mbdev->dev.coherent_dma_mask;
153         dma_set_mask(&mbdev->dev, DMA_BIT_MASK(64));
154         mbdev->dev.release = mbus_release_dev;
155         mbdev->hw_ops = hw_ops;
156         mbdev->dev.bus = &mic_bus;
157         mbdev->index = index;
158         dev_set_name(&mbdev->dev, "mbus-dev%u", mbdev->index);
159         /*
160          * device_register() causes the bus infrastructure to look for a
161          * matching driver.
162          */
163         ret = device_register(&mbdev->dev);
164         if (ret)
165                 goto free_mbdev;
166         return mbdev;
167 free_mbdev:
168         put_device(&mbdev->dev);
169         return ERR_PTR(ret);
170 }
171 EXPORT_SYMBOL_GPL(mbus_register_device);
172
173 void mbus_unregister_device(struct mbus_device *mbdev)
174 {
175         device_unregister(&mbdev->dev);
176 }
177 EXPORT_SYMBOL_GPL(mbus_unregister_device);
178
179 static int __init mbus_init(void)
180 {
181         return bus_register(&mic_bus);
182 }
183
184 static void __exit mbus_exit(void)
185 {
186         bus_unregister(&mic_bus);
187 }
188
189 core_initcall(mbus_init);
190 module_exit(mbus_exit);
191
192 MODULE_AUTHOR("Intel Corporation");
193 MODULE_DESCRIPTION("Intel(R) MIC Bus driver");
194 MODULE_LICENSE("GPL v2");