1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2015 Intel Corporation.
7 * Intel MIC COSM Bus Driver
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/idr.h>
14 /* Unique numbering for cosm devices. */
15 static DEFINE_IDA(cosm_index_ida
);
17 static int cosm_dev_probe(struct device
*d
)
19 struct cosm_device
*dev
= dev_to_cosm(d
);
20 struct cosm_driver
*drv
= drv_to_cosm(dev
->dev
.driver
);
22 return drv
->probe(dev
);
25 static int cosm_dev_remove(struct device
*d
)
27 struct cosm_device
*dev
= dev_to_cosm(d
);
28 struct cosm_driver
*drv
= drv_to_cosm(dev
->dev
.driver
);
34 static struct bus_type cosm_bus
= {
36 .probe
= cosm_dev_probe
,
37 .remove
= cosm_dev_remove
,
40 int cosm_register_driver(struct cosm_driver
*driver
)
42 driver
->driver
.bus
= &cosm_bus
;
43 return driver_register(&driver
->driver
);
45 EXPORT_SYMBOL_GPL(cosm_register_driver
);
47 void cosm_unregister_driver(struct cosm_driver
*driver
)
49 driver_unregister(&driver
->driver
);
51 EXPORT_SYMBOL_GPL(cosm_unregister_driver
);
53 static inline void cosm_release_dev(struct device
*d
)
55 struct cosm_device
*cdev
= dev_to_cosm(d
);
61 cosm_register_device(struct device
*pdev
, struct cosm_hw_ops
*hw_ops
)
63 struct cosm_device
*cdev
;
66 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
68 return ERR_PTR(-ENOMEM
);
70 cdev
->dev
.parent
= pdev
;
71 cdev
->dev
.release
= cosm_release_dev
;
72 cdev
->hw_ops
= hw_ops
;
73 dev_set_drvdata(&cdev
->dev
, cdev
);
74 cdev
->dev
.bus
= &cosm_bus
;
76 /* Assign a unique device index and hence name */
77 ret
= ida_simple_get(&cosm_index_ida
, 0, 0, GFP_KERNEL
);
83 dev_set_name(&cdev
->dev
, "cosm-dev%u", cdev
->index
);
85 ret
= device_register(&cdev
->dev
);
90 ida_simple_remove(&cosm_index_ida
, cdev
->index
);
92 put_device(&cdev
->dev
);
95 EXPORT_SYMBOL_GPL(cosm_register_device
);
97 void cosm_unregister_device(struct cosm_device
*dev
)
99 int index
= dev
->index
; /* save for after device release */
101 device_unregister(&dev
->dev
);
102 ida_simple_remove(&cosm_index_ida
, index
);
104 EXPORT_SYMBOL_GPL(cosm_unregister_device
);
106 struct cosm_device
*cosm_find_cdev_by_id(int id
)
108 struct device
*dev
= subsys_find_device_by_id(&cosm_bus
, id
, NULL
);
110 return dev
? container_of(dev
, struct cosm_device
, dev
) : NULL
;
112 EXPORT_SYMBOL_GPL(cosm_find_cdev_by_id
);
114 static int __init
cosm_init(void)
116 return bus_register(&cosm_bus
);
119 static void __exit
cosm_exit(void)
121 bus_unregister(&cosm_bus
);
122 ida_destroy(&cosm_index_ida
);
125 core_initcall(cosm_init
);
126 module_exit(cosm_exit
);
128 MODULE_AUTHOR("Intel Corporation");
129 MODULE_DESCRIPTION("Intel(R) MIC card OS state management bus driver");
130 MODULE_LICENSE("GPL v2");