Linux 4.18.10
[linux/fpc-iii.git] / drivers / misc / mic / bus / mic_bus.c
blob77b16ca668460157cf15cfa8f767e56d590b5e73
1 /*
2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2014 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
18 * Intel MIC Bus driver.
20 * This implementation is very similar to the the virtio bus driver
21 * implementation @ drivers/virtio/virtio.c
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/idr.h>
26 #include <linux/mic_bus.h>
28 static ssize_t device_show(struct device *d,
29 struct device_attribute *attr, char *buf)
31 struct mbus_device *dev = dev_to_mbus(d);
32 return sprintf(buf, "0x%04x\n", dev->id.device);
34 static DEVICE_ATTR_RO(device);
36 static ssize_t vendor_show(struct device *d,
37 struct device_attribute *attr, char *buf)
39 struct mbus_device *dev = dev_to_mbus(d);
40 return sprintf(buf, "0x%04x\n", dev->id.vendor);
42 static DEVICE_ATTR_RO(vendor);
44 static ssize_t modalias_show(struct device *d,
45 struct device_attribute *attr, char *buf)
47 struct mbus_device *dev = dev_to_mbus(d);
48 return sprintf(buf, "mbus:d%08Xv%08X\n",
49 dev->id.device, dev->id.vendor);
51 static DEVICE_ATTR_RO(modalias);
53 static struct attribute *mbus_dev_attrs[] = {
54 &dev_attr_device.attr,
55 &dev_attr_vendor.attr,
56 &dev_attr_modalias.attr,
57 NULL,
59 ATTRIBUTE_GROUPS(mbus_dev);
61 static inline int mbus_id_match(const struct mbus_device *dev,
62 const struct mbus_device_id *id)
64 if (id->device != dev->id.device && id->device != MBUS_DEV_ANY_ID)
65 return 0;
67 return id->vendor == MBUS_DEV_ANY_ID || id->vendor == dev->id.vendor;
71 * This looks through all the IDs a driver claims to support. If any of them
72 * match, we return 1 and the kernel will call mbus_dev_probe().
74 static int mbus_dev_match(struct device *dv, struct device_driver *dr)
76 unsigned int i;
77 struct mbus_device *dev = dev_to_mbus(dv);
78 const struct mbus_device_id *ids;
80 ids = drv_to_mbus(dr)->id_table;
81 for (i = 0; ids[i].device; i++)
82 if (mbus_id_match(dev, &ids[i]))
83 return 1;
84 return 0;
87 static int mbus_uevent(struct device *dv, struct kobj_uevent_env *env)
89 struct mbus_device *dev = dev_to_mbus(dv);
91 return add_uevent_var(env, "MODALIAS=mbus:d%08Xv%08X",
92 dev->id.device, dev->id.vendor);
95 static int mbus_dev_probe(struct device *d)
97 int err;
98 struct mbus_device *dev = dev_to_mbus(d);
99 struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
101 err = drv->probe(dev);
102 if (!err)
103 if (drv->scan)
104 drv->scan(dev);
105 return err;
108 static int mbus_dev_remove(struct device *d)
110 struct mbus_device *dev = dev_to_mbus(d);
111 struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
113 drv->remove(dev);
114 return 0;
117 static struct bus_type mic_bus = {
118 .name = "mic_bus",
119 .match = mbus_dev_match,
120 .dev_groups = mbus_dev_groups,
121 .uevent = mbus_uevent,
122 .probe = mbus_dev_probe,
123 .remove = mbus_dev_remove,
126 int mbus_register_driver(struct mbus_driver *driver)
128 driver->driver.bus = &mic_bus;
129 return driver_register(&driver->driver);
131 EXPORT_SYMBOL_GPL(mbus_register_driver);
133 void mbus_unregister_driver(struct mbus_driver *driver)
135 driver_unregister(&driver->driver);
137 EXPORT_SYMBOL_GPL(mbus_unregister_driver);
139 static void mbus_release_dev(struct device *d)
141 struct mbus_device *mbdev = dev_to_mbus(d);
142 kfree(mbdev);
145 struct mbus_device *
146 mbus_register_device(struct device *pdev, int id, const struct dma_map_ops *dma_ops,
147 struct mbus_hw_ops *hw_ops, int index,
148 void __iomem *mmio_va)
150 int ret;
151 struct mbus_device *mbdev;
153 mbdev = kzalloc(sizeof(*mbdev), GFP_KERNEL);
154 if (!mbdev)
155 return ERR_PTR(-ENOMEM);
157 mbdev->mmio_va = mmio_va;
158 mbdev->dev.parent = pdev;
159 mbdev->id.device = id;
160 mbdev->id.vendor = MBUS_DEV_ANY_ID;
161 mbdev->dev.dma_ops = dma_ops;
162 mbdev->dev.dma_mask = &mbdev->dev.coherent_dma_mask;
163 dma_set_mask(&mbdev->dev, DMA_BIT_MASK(64));
164 mbdev->dev.release = mbus_release_dev;
165 mbdev->hw_ops = hw_ops;
166 mbdev->dev.bus = &mic_bus;
167 mbdev->index = index;
168 dev_set_name(&mbdev->dev, "mbus-dev%u", mbdev->index);
170 * device_register() causes the bus infrastructure to look for a
171 * matching driver.
173 ret = device_register(&mbdev->dev);
174 if (ret)
175 goto free_mbdev;
176 return mbdev;
177 free_mbdev:
178 put_device(&mbdev->dev);
179 return ERR_PTR(ret);
181 EXPORT_SYMBOL_GPL(mbus_register_device);
183 void mbus_unregister_device(struct mbus_device *mbdev)
185 device_unregister(&mbdev->dev);
187 EXPORT_SYMBOL_GPL(mbus_unregister_device);
189 static int __init mbus_init(void)
191 return bus_register(&mic_bus);
194 static void __exit mbus_exit(void)
196 bus_unregister(&mic_bus);
199 core_initcall(mbus_init);
200 module_exit(mbus_exit);
202 MODULE_AUTHOR("Intel Corporation");
203 MODULE_DESCRIPTION("Intel(R) MIC Bus driver");
204 MODULE_LICENSE("GPL v2");