2 * Greybus driver and device API
4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #ifndef __LINUX_GREYBUS_H
11 #define __LINUX_GREYBUS_H
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/module.h>
21 #include <linux/idr.h>
23 #include "kernel_ver.h"
24 #include "greybus_id.h"
25 #include "greybus_manifest.h"
26 #include "greybus_protocols.h"
32 #include "interface.h"
34 #include "connection.h"
36 #include "operation.h"
39 /* Matches up with the Greybus Protocol specification document */
40 #define GREYBUS_VERSION_MAJOR 0x00
41 #define GREYBUS_VERSION_MINOR 0x01
43 #define GREYBUS_DEVICE_ID_MATCH_DEVICE \
44 (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
46 #define GREYBUS_DEVICE(v, p) \
47 .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \
51 #define GREYBUS_DEVICE_SERIAL(s) \
52 .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \
55 /* XXX I couldn't get my Kconfig file to be noticed for out-of-tree build */
56 #ifndef CONFIG_CPORT_ID_MAX
57 #define CONFIG_CPORT_ID_MAX 128
58 #endif /* !CONFIG_CPORT_ID_MAX */
60 /* Maximum number of CPorts usable by a host device */
61 /* XXX This should really be determined by the AP module manifest */
62 #define CPORT_ID_MAX CONFIG_CPORT_ID_MAX
63 #define CPORT_ID_BAD U16_MAX /* UniPro max id is 4095 */
65 /* For SP1 hardware, we are going to "hardcode" each device to have all logical
66 * blocks in order to be able to address them as one unified "unit". Then
67 * higher up layers will then be able to talk to them as one logical block and
68 * properly know how they are hooked together (i.e. which i2c port is on the
69 * same module as the gpio pins, etc.)
71 * So, put the "private" data structures here in greybus.h and link to them off
72 * of the "main" gb_module structure.
75 struct greybus_host_device;
77 /* Greybus "Host driver" structure, needed by a host controller driver to be
78 * able to handle both SVC control as well as "real" greybus messages
80 struct greybus_host_driver {
83 int (*message_send)(struct greybus_host_device *hd, u16 dest_cport_id,
84 struct gb_message *message, gfp_t gfp_mask);
85 void (*message_cancel)(struct gb_message *message);
88 struct greybus_host_device {
90 struct device *parent;
91 const struct greybus_host_driver *driver;
93 struct list_head interfaces;
94 struct list_head connections;
95 struct ida cport_id_map;
98 /* Host device buffer constraints */
99 size_t buffer_size_max;
101 struct gb_endo *endo;
102 struct gb_connection *initial_svc_connection;
105 /* Private data for the host driver */
106 unsigned long hd_priv[0] __aligned(sizeof(s64));
109 struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
110 struct device *parent,
111 size_t buffer_size_max);
112 int greybus_endo_setup(struct greybus_host_device *hd, u16 endo_id,
114 void greybus_remove_hd(struct greybus_host_device *hd);
116 struct greybus_driver {
119 int (*probe)(struct gb_bundle *bundle,
120 const struct greybus_bundle_id *id);
121 void (*disconnect)(struct gb_bundle *bundle);
123 int (*suspend)(struct gb_bundle *bundle, pm_message_t message);
124 int (*resume)(struct gb_bundle *bundle);
126 const struct greybus_bundle_id *id_table;
128 struct device_driver driver;
130 #define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
132 /* Don't call these directly, use the module_greybus_driver() macro instead */
133 int greybus_register_driver(struct greybus_driver *driver,
134 struct module *module, const char *mod_name);
135 void greybus_deregister_driver(struct greybus_driver *driver);
137 /* define to get proper THIS_MODULE and KBUILD_MODNAME values */
138 #define greybus_register(driver) \
139 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
140 #define greybus_deregister(driver) \
141 greybus_deregister_driver(driver)
144 * module_greybus_driver() - Helper macro for registering a Greybus driver
145 * @__greybus_driver: greybus_driver structure
147 * Helper macro for Greybus drivers to set up proper module init / exit
148 * functions. Replaces module_init() and module_exit() and keeps people from
149 * printing pointless things to the kernel log when their driver is loaded.
151 #define module_greybus_driver(__greybus_driver) \
152 module_driver(__greybus_driver, greybus_register, greybus_deregister)
154 int greybus_disabled(void);
156 void gb_debugfs_init(void);
157 void gb_debugfs_cleanup(void);
158 struct dentry *gb_debugfs_get(void);
160 extern struct bus_type greybus_bus_type;
162 extern struct device_type greybus_endo_type;
163 extern struct device_type greybus_module_type;
164 extern struct device_type greybus_interface_type;
165 extern struct device_type greybus_bundle_type;
166 extern struct device_type greybus_connection_type;
168 static inline int is_gb_endo(const struct device *dev)
170 return dev->type == &greybus_endo_type;
173 static inline int is_gb_module(const struct device *dev)
175 return dev->type == &greybus_module_type;
178 static inline int is_gb_interface(const struct device *dev)
180 return dev->type == &greybus_interface_type;
183 static inline int is_gb_bundle(const struct device *dev)
185 return dev->type == &greybus_bundle_type;
188 static inline int is_gb_connection(const struct device *dev)
190 return dev->type == &greybus_connection_type;
193 static inline bool cport_id_valid(u16 cport_id)
195 return cport_id != CPORT_ID_BAD && cport_id <= CPORT_ID_MAX;
198 #endif /* __KERNEL__ */
199 #endif /* __LINUX_GREYBUS_H */