treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / mic / bus / mic_bus.c
blobed9a8351c3bfd16762f566ce46a590db7925c1c0
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2014 Intel Corporation.
7 * Intel MIC Bus driver.
9 * This implementation is very similar to the the virtio bus driver
10 * implementation @ drivers/virtio/virtio.c
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/idr.h>
15 #include <linux/mic_bus.h>
17 static ssize_t device_show(struct device *d,
18 struct device_attribute *attr, char *buf)
20 struct mbus_device *dev = dev_to_mbus(d);
21 return sprintf(buf, "0x%04x\n", dev->id.device);
23 static DEVICE_ATTR_RO(device);
25 static ssize_t vendor_show(struct device *d,
26 struct device_attribute *attr, char *buf)
28 struct mbus_device *dev = dev_to_mbus(d);
29 return sprintf(buf, "0x%04x\n", dev->id.vendor);
31 static DEVICE_ATTR_RO(vendor);
33 static ssize_t modalias_show(struct device *d,
34 struct device_attribute *attr, char *buf)
36 struct mbus_device *dev = dev_to_mbus(d);
37 return sprintf(buf, "mbus:d%08Xv%08X\n",
38 dev->id.device, dev->id.vendor);
40 static DEVICE_ATTR_RO(modalias);
42 static struct attribute *mbus_dev_attrs[] = {
43 &dev_attr_device.attr,
44 &dev_attr_vendor.attr,
45 &dev_attr_modalias.attr,
46 NULL,
48 ATTRIBUTE_GROUPS(mbus_dev);
50 static inline int mbus_id_match(const struct mbus_device *dev,
51 const struct mbus_device_id *id)
53 if (id->device != dev->id.device && id->device != MBUS_DEV_ANY_ID)
54 return 0;
56 return id->vendor == MBUS_DEV_ANY_ID || id->vendor == dev->id.vendor;
60 * This looks through all the IDs a driver claims to support. If any of them
61 * match, we return 1 and the kernel will call mbus_dev_probe().
63 static int mbus_dev_match(struct device *dv, struct device_driver *dr)
65 unsigned int i;
66 struct mbus_device *dev = dev_to_mbus(dv);
67 const struct mbus_device_id *ids;
69 ids = drv_to_mbus(dr)->id_table;
70 for (i = 0; ids[i].device; i++)
71 if (mbus_id_match(dev, &ids[i]))
72 return 1;
73 return 0;
76 static int mbus_uevent(struct device *dv, struct kobj_uevent_env *env)
78 struct mbus_device *dev = dev_to_mbus(dv);
80 return add_uevent_var(env, "MODALIAS=mbus:d%08Xv%08X",
81 dev->id.device, dev->id.vendor);
84 static int mbus_dev_probe(struct device *d)
86 int err;
87 struct mbus_device *dev = dev_to_mbus(d);
88 struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
90 err = drv->probe(dev);
91 if (!err)
92 if (drv->scan)
93 drv->scan(dev);
94 return err;
97 static int mbus_dev_remove(struct device *d)
99 struct mbus_device *dev = dev_to_mbus(d);
100 struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
102 drv->remove(dev);
103 return 0;
106 static struct bus_type mic_bus = {
107 .name = "mic_bus",
108 .match = mbus_dev_match,
109 .dev_groups = mbus_dev_groups,
110 .uevent = mbus_uevent,
111 .probe = mbus_dev_probe,
112 .remove = mbus_dev_remove,
115 int mbus_register_driver(struct mbus_driver *driver)
117 driver->driver.bus = &mic_bus;
118 return driver_register(&driver->driver);
120 EXPORT_SYMBOL_GPL(mbus_register_driver);
122 void mbus_unregister_driver(struct mbus_driver *driver)
124 driver_unregister(&driver->driver);
126 EXPORT_SYMBOL_GPL(mbus_unregister_driver);
128 static void mbus_release_dev(struct device *d)
130 struct mbus_device *mbdev = dev_to_mbus(d);
131 kfree(mbdev);
134 struct mbus_device *
135 mbus_register_device(struct device *pdev, int id, const struct dma_map_ops *dma_ops,
136 struct mbus_hw_ops *hw_ops, int index,
137 void __iomem *mmio_va)
139 int ret;
140 struct mbus_device *mbdev;
142 mbdev = kzalloc(sizeof(*mbdev), GFP_KERNEL);
143 if (!mbdev)
144 return ERR_PTR(-ENOMEM);
146 mbdev->mmio_va = mmio_va;
147 mbdev->dev.parent = pdev;
148 mbdev->id.device = id;
149 mbdev->id.vendor = MBUS_DEV_ANY_ID;
150 mbdev->dev.dma_ops = dma_ops;
151 mbdev->dev.dma_mask = &mbdev->dev.coherent_dma_mask;
152 dma_set_mask(&mbdev->dev, DMA_BIT_MASK(64));
153 mbdev->dev.release = mbus_release_dev;
154 mbdev->hw_ops = hw_ops;
155 mbdev->dev.bus = &mic_bus;
156 mbdev->index = index;
157 dev_set_name(&mbdev->dev, "mbus-dev%u", mbdev->index);
159 * device_register() causes the bus infrastructure to look for a
160 * matching driver.
162 ret = device_register(&mbdev->dev);
163 if (ret)
164 goto free_mbdev;
165 return mbdev;
166 free_mbdev:
167 put_device(&mbdev->dev);
168 return ERR_PTR(ret);
170 EXPORT_SYMBOL_GPL(mbus_register_device);
172 void mbus_unregister_device(struct mbus_device *mbdev)
174 device_unregister(&mbdev->dev);
176 EXPORT_SYMBOL_GPL(mbus_unregister_device);
178 static int __init mbus_init(void)
180 return bus_register(&mic_bus);
183 static void __exit mbus_exit(void)
185 bus_unregister(&mic_bus);
188 core_initcall(mbus_init);
189 module_exit(mbus_exit);
191 MODULE_AUTHOR("Intel Corporation");
192 MODULE_DESCRIPTION("Intel(R) MIC Bus driver");
193 MODULE_LICENSE("GPL v2");