Linux 4.18.10
[linux/fpc-iii.git] / drivers / misc / mic / bus / cosm_bus.c
blobd31d6c6e6cb1550e77b0dc63060f2684e6862600
1 /*
2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2015 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 COSM Bus Driver
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/idr.h>
23 #include "cosm_bus.h"
25 /* Unique numbering for cosm devices. */
26 static DEFINE_IDA(cosm_index_ida);
28 static int cosm_dev_probe(struct device *d)
30 struct cosm_device *dev = dev_to_cosm(d);
31 struct cosm_driver *drv = drv_to_cosm(dev->dev.driver);
33 return drv->probe(dev);
36 static int cosm_dev_remove(struct device *d)
38 struct cosm_device *dev = dev_to_cosm(d);
39 struct cosm_driver *drv = drv_to_cosm(dev->dev.driver);
41 drv->remove(dev);
42 return 0;
45 static struct bus_type cosm_bus = {
46 .name = "cosm_bus",
47 .probe = cosm_dev_probe,
48 .remove = cosm_dev_remove,
51 int cosm_register_driver(struct cosm_driver *driver)
53 driver->driver.bus = &cosm_bus;
54 return driver_register(&driver->driver);
56 EXPORT_SYMBOL_GPL(cosm_register_driver);
58 void cosm_unregister_driver(struct cosm_driver *driver)
60 driver_unregister(&driver->driver);
62 EXPORT_SYMBOL_GPL(cosm_unregister_driver);
64 static inline void cosm_release_dev(struct device *d)
66 struct cosm_device *cdev = dev_to_cosm(d);
68 kfree(cdev);
71 struct cosm_device *
72 cosm_register_device(struct device *pdev, struct cosm_hw_ops *hw_ops)
74 struct cosm_device *cdev;
75 int ret;
77 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
78 if (!cdev)
79 return ERR_PTR(-ENOMEM);
81 cdev->dev.parent = pdev;
82 cdev->dev.release = cosm_release_dev;
83 cdev->hw_ops = hw_ops;
84 dev_set_drvdata(&cdev->dev, cdev);
85 cdev->dev.bus = &cosm_bus;
87 /* Assign a unique device index and hence name */
88 ret = ida_simple_get(&cosm_index_ida, 0, 0, GFP_KERNEL);
89 if (ret < 0)
90 goto free_cdev;
92 cdev->index = ret;
93 cdev->dev.id = ret;
94 dev_set_name(&cdev->dev, "cosm-dev%u", cdev->index);
96 ret = device_register(&cdev->dev);
97 if (ret)
98 goto ida_remove;
99 return cdev;
100 ida_remove:
101 ida_simple_remove(&cosm_index_ida, cdev->index);
102 free_cdev:
103 put_device(&cdev->dev);
104 return ERR_PTR(ret);
106 EXPORT_SYMBOL_GPL(cosm_register_device);
108 void cosm_unregister_device(struct cosm_device *dev)
110 int index = dev->index; /* save for after device release */
112 device_unregister(&dev->dev);
113 ida_simple_remove(&cosm_index_ida, index);
115 EXPORT_SYMBOL_GPL(cosm_unregister_device);
117 struct cosm_device *cosm_find_cdev_by_id(int id)
119 struct device *dev = subsys_find_device_by_id(&cosm_bus, id, NULL);
121 return dev ? container_of(dev, struct cosm_device, dev) : NULL;
123 EXPORT_SYMBOL_GPL(cosm_find_cdev_by_id);
125 static int __init cosm_init(void)
127 return bus_register(&cosm_bus);
130 static void __exit cosm_exit(void)
132 bus_unregister(&cosm_bus);
133 ida_destroy(&cosm_index_ida);
136 core_initcall(cosm_init);
137 module_exit(cosm_exit);
139 MODULE_AUTHOR("Intel Corporation");
140 MODULE_DESCRIPTION("Intel(R) MIC card OS state management bus driver");
141 MODULE_LICENSE("GPL v2");