Linux 5.1.15
[linux/fpc-iii.git] / drivers / misc / mic / bus / scif_bus.c
bloba444db5f61fe5bf3aed0b25f610751711aa4d930
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 * Intel Symmetric Communications Interface Bus driver.
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/idr.h>
20 #include <linux/dma-mapping.h>
22 #include "scif_bus.h"
24 static ssize_t device_show(struct device *d,
25 struct device_attribute *attr, char *buf)
27 struct scif_hw_dev *dev = dev_to_scif(d);
29 return sprintf(buf, "0x%04x\n", dev->id.device);
31 static DEVICE_ATTR_RO(device);
33 static ssize_t vendor_show(struct device *d,
34 struct device_attribute *attr, char *buf)
36 struct scif_hw_dev *dev = dev_to_scif(d);
38 return sprintf(buf, "0x%04x\n", dev->id.vendor);
40 static DEVICE_ATTR_RO(vendor);
42 static ssize_t modalias_show(struct device *d,
43 struct device_attribute *attr, char *buf)
45 struct scif_hw_dev *dev = dev_to_scif(d);
47 return sprintf(buf, "scif:d%08Xv%08X\n",
48 dev->id.device, dev->id.vendor);
50 static DEVICE_ATTR_RO(modalias);
52 static struct attribute *scif_dev_attrs[] = {
53 &dev_attr_device.attr,
54 &dev_attr_vendor.attr,
55 &dev_attr_modalias.attr,
56 NULL,
58 ATTRIBUTE_GROUPS(scif_dev);
60 static inline int scif_id_match(const struct scif_hw_dev *dev,
61 const struct scif_hw_dev_id *id)
63 if (id->device != dev->id.device && id->device != SCIF_DEV_ANY_ID)
64 return 0;
66 return id->vendor == SCIF_DEV_ANY_ID || id->vendor == dev->id.vendor;
70 * This looks through all the IDs a driver claims to support. If any of them
71 * match, we return 1 and the kernel will call scif_dev_probe().
73 static int scif_dev_match(struct device *dv, struct device_driver *dr)
75 unsigned int i;
76 struct scif_hw_dev *dev = dev_to_scif(dv);
77 const struct scif_hw_dev_id *ids;
79 ids = drv_to_scif(dr)->id_table;
80 for (i = 0; ids[i].device; i++)
81 if (scif_id_match(dev, &ids[i]))
82 return 1;
83 return 0;
86 static int scif_uevent(struct device *dv, struct kobj_uevent_env *env)
88 struct scif_hw_dev *dev = dev_to_scif(dv);
90 return add_uevent_var(env, "MODALIAS=scif:d%08Xv%08X",
91 dev->id.device, dev->id.vendor);
94 static int scif_dev_probe(struct device *d)
96 struct scif_hw_dev *dev = dev_to_scif(d);
97 struct scif_driver *drv = drv_to_scif(dev->dev.driver);
99 return drv->probe(dev);
102 static int scif_dev_remove(struct device *d)
104 struct scif_hw_dev *dev = dev_to_scif(d);
105 struct scif_driver *drv = drv_to_scif(dev->dev.driver);
107 drv->remove(dev);
108 return 0;
111 static struct bus_type scif_bus = {
112 .name = "scif_bus",
113 .match = scif_dev_match,
114 .dev_groups = scif_dev_groups,
115 .uevent = scif_uevent,
116 .probe = scif_dev_probe,
117 .remove = scif_dev_remove,
120 int scif_register_driver(struct scif_driver *driver)
122 driver->driver.bus = &scif_bus;
123 return driver_register(&driver->driver);
125 EXPORT_SYMBOL_GPL(scif_register_driver);
127 void scif_unregister_driver(struct scif_driver *driver)
129 driver_unregister(&driver->driver);
131 EXPORT_SYMBOL_GPL(scif_unregister_driver);
133 static void scif_release_dev(struct device *d)
135 struct scif_hw_dev *sdev = dev_to_scif(d);
137 kfree(sdev);
140 struct scif_hw_dev *
141 scif_register_device(struct device *pdev, int id, const struct dma_map_ops *dma_ops,
142 struct scif_hw_ops *hw_ops, u8 dnode, u8 snode,
143 struct mic_mw *mmio, struct mic_mw *aper, void *dp,
144 void __iomem *rdp, struct dma_chan **chan, int num_chan,
145 bool card_rel_da)
147 int ret;
148 struct scif_hw_dev *sdev;
150 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
151 if (!sdev)
152 return ERR_PTR(-ENOMEM);
154 sdev->dev.parent = pdev;
155 sdev->id.device = id;
156 sdev->id.vendor = SCIF_DEV_ANY_ID;
157 sdev->dev.dma_ops = dma_ops;
158 sdev->dev.release = scif_release_dev;
159 sdev->hw_ops = hw_ops;
160 sdev->dnode = dnode;
161 sdev->snode = snode;
162 dev_set_drvdata(&sdev->dev, sdev);
163 sdev->dev.bus = &scif_bus;
164 sdev->mmio = mmio;
165 sdev->aper = aper;
166 sdev->dp = dp;
167 sdev->rdp = rdp;
168 sdev->dev.dma_mask = &sdev->dev.coherent_dma_mask;
169 dma_set_mask(&sdev->dev, DMA_BIT_MASK(64));
170 sdev->dma_ch = chan;
171 sdev->num_dma_ch = num_chan;
172 sdev->card_rel_da = card_rel_da;
173 dev_set_name(&sdev->dev, "scif-dev%u", sdev->dnode);
175 * device_register() causes the bus infrastructure to look for a
176 * matching driver.
178 ret = device_register(&sdev->dev);
179 if (ret)
180 goto free_sdev;
181 return sdev;
182 free_sdev:
183 put_device(&sdev->dev);
184 return ERR_PTR(ret);
186 EXPORT_SYMBOL_GPL(scif_register_device);
188 void scif_unregister_device(struct scif_hw_dev *sdev)
190 device_unregister(&sdev->dev);
192 EXPORT_SYMBOL_GPL(scif_unregister_device);
194 static int __init scif_init(void)
196 return bus_register(&scif_bus);
199 static void __exit scif_exit(void)
201 bus_unregister(&scif_bus);
204 core_initcall(scif_init);
205 module_exit(scif_exit);
207 MODULE_AUTHOR("Intel Corporation");
208 MODULE_DESCRIPTION("Intel(R) SCIF Bus driver");
209 MODULE_LICENSE("GPL v2");