greybus: Merge branch 'master' into svc
[cascardo/linux.git] / drivers / staging / greybus / greybus.h
1 /*
2  * Greybus driver and device API
3  *
4  * Copyright 2014-2015 Google Inc.
5  * Copyright 2014-2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #ifndef __LINUX_GREYBUS_H
11 #define __LINUX_GREYBUS_H
12
13 #ifdef __KERNEL__
14
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>
22
23 #include "kernel_ver.h"
24 #include "greybus_id.h"
25 #include "greybus_manifest.h"
26 #include "greybus_protocols.h"
27 #include "manifest.h"
28 #include "endo.h"
29 #include "svc.h"
30 #include "module.h"
31 #include "control.h"
32 #include "interface.h"
33 #include "bundle.h"
34 #include "connection.h"
35 #include "protocol.h"
36 #include "operation.h"
37
38
39 /* Matches up with the Greybus Protocol specification document */
40 #define GREYBUS_VERSION_MAJOR   0x00
41 #define GREYBUS_VERSION_MINOR   0x01
42
43 #define GREYBUS_DEVICE_ID_MATCH_DEVICE \
44         (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
45
46 #define GREYBUS_DEVICE(v, p)                                    \
47         .match_flags    = GREYBUS_DEVICE_ID_MATCH_DEVICE,       \
48         .vendor         = (v),                                  \
49         .product        = (p),
50
51 #define GREYBUS_DEVICE_SERIAL(s)                                \
52         .match_flags    = GREYBUS_DEVICE_ID_MATCH_SERIAL,       \
53         .serial_number  = (s),
54
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 */
59
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 */
64
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.)
70  *
71  * So, put the "private" data structures here in greybus.h and link to them off
72  * of the "main" gb_module structure.
73  */
74
75 struct greybus_host_device;
76
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
79  */
80 struct greybus_host_driver {
81         size_t  hd_priv_size;
82
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);
86 };
87
88 struct greybus_host_device {
89         struct kref kref;
90         struct device *parent;
91         const struct greybus_host_driver *driver;
92
93         struct list_head interfaces;
94         struct list_head connections;
95         struct ida cport_id_map;
96         u8 device_id;
97
98         /* Host device buffer constraints */
99         size_t buffer_size_max;
100
101         struct gb_endo *endo;
102         struct gb_connection *initial_svc_connection;
103         struct gb_svc *svc;
104
105         /* Private data for the host driver */
106         unsigned long hd_priv[0] __aligned(sizeof(s64));
107 };
108
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,
113                         u8 ap_intf_id);
114 void greybus_remove_hd(struct greybus_host_device *hd);
115
116 struct greybus_driver {
117         const char *name;
118
119         int (*probe)(struct gb_bundle *bundle,
120                      const struct greybus_bundle_id *id);
121         void (*disconnect)(struct gb_bundle *bundle);
122
123         int (*suspend)(struct gb_bundle *bundle, pm_message_t message);
124         int (*resume)(struct gb_bundle *bundle);
125
126         const struct greybus_bundle_id *id_table;
127
128         struct device_driver driver;
129 };
130 #define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
131
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);
136
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)
142
143 /**
144  * module_greybus_driver() - Helper macro for registering a Greybus driver
145  * @__greybus_driver: greybus_driver structure
146  *
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.
150  */
151 #define module_greybus_driver(__greybus_driver) \
152         module_driver(__greybus_driver, greybus_register, greybus_deregister)
153
154 int greybus_disabled(void);
155
156 void gb_debugfs_init(void);
157 void gb_debugfs_cleanup(void);
158 struct dentry *gb_debugfs_get(void);
159
160 extern struct bus_type greybus_bus_type;
161
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;
167
168 static inline int is_gb_endo(const struct device *dev)
169 {
170         return dev->type == &greybus_endo_type;
171 }
172
173 static inline int is_gb_module(const struct device *dev)
174 {
175         return dev->type == &greybus_module_type;
176 }
177
178 static inline int is_gb_interface(const struct device *dev)
179 {
180         return dev->type == &greybus_interface_type;
181 }
182
183 static inline int is_gb_bundle(const struct device *dev)
184 {
185         return dev->type == &greybus_bundle_type;
186 }
187
188 static inline int is_gb_connection(const struct device *dev)
189 {
190         return dev->type == &greybus_connection_type;
191 }
192
193 static inline bool cport_id_valid(u16 cport_id)
194 {
195         return cport_id != CPORT_ID_BAD && cport_id <= CPORT_ID_MAX;
196 }
197
198 #endif /* __KERNEL__ */
199 #endif /* __LINUX_GREYBUS_H */