1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Greybus driver and device API
4  *
5  * Copyright 2014-2015 Google Inc.
6  * Copyright 2014-2015 Linaro Ltd.
7  */
8 
9 #ifndef __LINUX_GREYBUS_H
10 #define __LINUX_GREYBUS_H
11 
12 #ifdef __KERNEL__
13 
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
18 #include <linux/device.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/idr.h>
22 
23 #include <linux/greybus/greybus_id.h>
24 #include <linux/greybus/greybus_manifest.h>
25 #include <linux/greybus/greybus_protocols.h>
26 #include <linux/greybus/manifest.h>
27 #include <linux/greybus/hd.h>
28 #include <linux/greybus/svc.h>
29 #include <linux/greybus/control.h>
30 #include <linux/greybus/module.h>
31 #include <linux/greybus/interface.h>
32 #include <linux/greybus/bundle.h>
33 #include <linux/greybus/connection.h>
34 #include <linux/greybus/operation.h>
35 
36 /* Matches up with the Greybus Protocol specification document */
37 #define GREYBUS_VERSION_MAJOR	0x00
38 #define GREYBUS_VERSION_MINOR	0x01
39 
40 #define GREYBUS_ID_MATCH_DEVICE \
41 	(GREYBUS_ID_MATCH_VENDOR | GREYBUS_ID_MATCH_PRODUCT)
42 
43 #define GREYBUS_DEVICE(v, p)					\
44 	.match_flags	= GREYBUS_ID_MATCH_DEVICE,		\
45 	.vendor		= (v),					\
46 	.product	= (p),
47 
48 #define GREYBUS_DEVICE_CLASS(c)					\
49 	.match_flags	= GREYBUS_ID_MATCH_CLASS,		\
50 	.class		= (c),
51 
52 /* Maximum number of CPorts */
53 #define CPORT_ID_MAX	4095		/* UniPro max id is 4095 */
54 #define CPORT_ID_BAD	U16_MAX
55 
56 struct greybus_driver {
57 	const char *name;
58 
59 	int (*probe)(struct gb_bundle *bundle,
60 		     const struct greybus_bundle_id *id);
61 	void (*disconnect)(struct gb_bundle *bundle);
62 
63 	const struct greybus_bundle_id *id_table;
64 
65 	struct device_driver driver;
66 };
67 #define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
68 
greybus_set_drvdata(struct gb_bundle * bundle,void * data)69 static inline void greybus_set_drvdata(struct gb_bundle *bundle, void *data)
70 {
71 	dev_set_drvdata(&bundle->dev, data);
72 }
73 
greybus_get_drvdata(struct gb_bundle * bundle)74 static inline void *greybus_get_drvdata(struct gb_bundle *bundle)
75 {
76 	return dev_get_drvdata(&bundle->dev);
77 }
78 
79 /* Don't call these directly, use the module_greybus_driver() macro instead */
80 int greybus_register_driver(struct greybus_driver *driver,
81 			    struct module *module, const char *mod_name);
82 void greybus_deregister_driver(struct greybus_driver *driver);
83 
84 /* define to get proper THIS_MODULE and KBUILD_MODNAME values */
85 #define greybus_register(driver) \
86 	greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
87 #define greybus_deregister(driver) \
88 	greybus_deregister_driver(driver)
89 
90 /**
91  * module_greybus_driver() - Helper macro for registering a Greybus driver
92  * @__greybus_driver: greybus_driver structure
93  *
94  * Helper macro for Greybus drivers to set up proper module init / exit
95  * functions.  Replaces module_init() and module_exit() and keeps people from
96  * printing pointless things to the kernel log when their driver is loaded.
97  */
98 #define module_greybus_driver(__greybus_driver)	\
99 	module_driver(__greybus_driver, greybus_register, greybus_deregister)
100 
101 int greybus_disabled(void);
102 
103 void gb_debugfs_init(void);
104 void gb_debugfs_cleanup(void);
105 struct dentry *gb_debugfs_get(void);
106 
107 extern struct bus_type greybus_bus_type;
108 
109 extern struct device_type greybus_hd_type;
110 extern struct device_type greybus_module_type;
111 extern struct device_type greybus_interface_type;
112 extern struct device_type greybus_control_type;
113 extern struct device_type greybus_bundle_type;
114 extern struct device_type greybus_svc_type;
115 
is_gb_host_device(const struct device * dev)116 static inline int is_gb_host_device(const struct device *dev)
117 {
118 	return dev->type == &greybus_hd_type;
119 }
120 
is_gb_module(const struct device * dev)121 static inline int is_gb_module(const struct device *dev)
122 {
123 	return dev->type == &greybus_module_type;
124 }
125 
is_gb_interface(const struct device * dev)126 static inline int is_gb_interface(const struct device *dev)
127 {
128 	return dev->type == &greybus_interface_type;
129 }
130 
is_gb_control(const struct device * dev)131 static inline int is_gb_control(const struct device *dev)
132 {
133 	return dev->type == &greybus_control_type;
134 }
135 
is_gb_bundle(const struct device * dev)136 static inline int is_gb_bundle(const struct device *dev)
137 {
138 	return dev->type == &greybus_bundle_type;
139 }
140 
is_gb_svc(const struct device * dev)141 static inline int is_gb_svc(const struct device *dev)
142 {
143 	return dev->type == &greybus_svc_type;
144 }
145 
cport_id_valid(struct gb_host_device * hd,u16 cport_id)146 static inline bool cport_id_valid(struct gb_host_device *hd, u16 cport_id)
147 {
148 	return cport_id != CPORT_ID_BAD && cport_id < hd->num_cports;
149 }
150 
151 #endif /* __KERNEL__ */
152 #endif /* __LINUX_GREYBUS_H */
153