greybus: driver matching: Greybus drivers bind to interface blocks, not modules
[cascardo/linux.git] / drivers / staging / greybus / greybus.h
1 /*
2  * Greybus driver and device API
3  *
4  * Copyright 2014 Google Inc.
5  *
6  * Released under the GPLv2 only.
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/idr.h>
21
22 #include "kernel_ver.h"
23 #include "greybus_id.h"
24 #include "greybus_manifest.h"
25 #include "manifest.h"
26 #include "interface_block.h"
27 #include "interface.h"
28 #include "connection.h"
29 #include "protocol.h"
30 #include "operation.h"
31
32
33 /* Matches up with the Greybus Protocol specification document */
34 #define GREYBUS_VERSION_MAJOR   0x00
35 #define GREYBUS_VERSION_MINOR   0x01
36
37 #define GREYBUS_DEVICE_ID_MATCH_DEVICE \
38         (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
39
40 #define GREYBUS_DEVICE(v, p)                                    \
41         .match_flags    = GREYBUS_DEVICE_ID_MATCH_DEVICE,       \
42         .vendor         = (v),                                  \
43         .product        = (p),
44
45 #define GREYBUS_DEVICE_SERIAL(s)                                \
46         .match_flags    = GREYBUS_DEVICE_ID_MATCH_SERIAL,       \
47         .serial_number  = (s),
48
49 /* XXX I couldn't get my Kconfig file to be noticed for out-of-tree build */
50 #ifndef CONFIG_HOST_DEV_CPORT_ID_MAX
51 #define CONFIG_HOST_DEV_CPORT_ID_MAX 128
52 #endif /* !CONFIG_HOST_DEV_CPORT_ID_MAX */
53
54 /* Maximum number of CPorts usable by a host device */
55 /* XXX This should really be determined by the AP module manifest */
56 #define HOST_DEV_CPORT_ID_MAX   CONFIG_HOST_DEV_CPORT_ID_MAX
57 #define CPORT_ID_BAD            U16_MAX         /* UniPro max id is 4095 */
58
59 /* For SP1 hardware, we are going to "hardcode" each device to have all logical
60  * blocks in order to be able to address them as one unified "unit".  Then
61  * higher up layers will then be able to talk to them as one logical block and
62  * properly know how they are hooked together (i.e. which i2c port is on the
63  * same module as the gpio pins, etc.)
64  *
65  * So, put the "private" data structures here in greybus.h and link to them off
66  * of the "main" gb_module structure.
67  */
68
69 struct greybus_host_device;
70 struct svc_msg;
71
72 /*
73  * When the Greybus code allocates a buffer it sets aside bytes
74  * prior to the beginning of the payload area for the host device's
75  * exclusive use.  The size is specified by hd->buffer_headroom, and
76  * which can't be greater than GB_BUFFER_HEADROOM_MAX.
77  */
78 #define GB_BUFFER_HEADROOM_MAX          sizeof(u64)
79
80 /* Buffers allocated from the host driver will be aligned to this multiple */
81 #define GB_BUFFER_ALIGN sizeof(u32)
82
83 /* Greybus "Host driver" structure, needed by a host controller driver to be
84  * able to handle both SVC control as well as "real" greybus messages
85  */
86 struct greybus_host_driver {
87         size_t  hd_priv_size;
88
89         void *(*buffer_send)(struct greybus_host_device *hd, u16 dest_cport_id,
90                         void *buffer, size_t buffer_size, gfp_t gfp_mask);
91         void (*buffer_cancel)(void *cookie);
92         int (*submit_svc)(struct svc_msg *svc_msg,
93                             struct greybus_host_device *hd);
94 };
95
96 struct greybus_host_device {
97         struct kref kref;
98         struct device *parent;
99         const struct greybus_host_driver *driver;
100
101         struct list_head modules;
102         struct list_head connections;
103         struct ida cport_id_map;
104         u8 device_id;
105
106         /* Host device buffer constraints */
107         size_t buffer_headroom;
108         size_t buffer_size_max;
109
110         /* Private data for the host driver */
111         unsigned long hd_priv[0] __aligned(sizeof(s64));
112 };
113
114 struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
115                                               struct device *parent);
116 void greybus_remove_hd(struct greybus_host_device *hd);
117
118 struct greybus_driver {
119         const char *name;
120
121         int (*probe)(struct gb_interface_block *gb_ib,
122                      const struct greybus_interface_block_id *id);
123         void (*disconnect)(struct gb_interface_block *gb_ib);
124
125         int (*suspend)(struct gb_interface_block *gb_ib, pm_message_t message);
126         int (*resume)(struct gb_interface_block *gb_ib);
127
128         const struct greybus_interface_block_id *id_table;
129
130         struct device_driver driver;
131 };
132 #define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
133
134 /* Don't call these directly, use the module_greybus_driver() macro instead */
135 int greybus_register_driver(struct greybus_driver *driver,
136                             struct module *module, const char *mod_name);
137 void greybus_deregister(struct greybus_driver *driver);
138
139 /* define to get proper THIS_MODULE and KBUILD_MODNAME values */
140 #define greybus_register(driver) \
141         greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
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 /* Internal functions to gb module, move to internal .h file eventually. */
157
158 void gb_add_module(struct greybus_host_device *hd, u8 module_id,
159                    u8 *data, int size);
160 void gb_remove_module(struct greybus_host_device *hd, u8 module_id);
161 void gb_remove_modules(struct greybus_host_device *hd);
162
163 int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
164 int gb_ap_init(void);
165 void gb_ap_exit(void);
166 int gb_debugfs_init(void);
167 void gb_debugfs_cleanup(void);
168
169 extern struct bus_type greybus_bus_type;
170
171 int gb_uart_device_init(struct gb_connection *connection);
172 void gb_uart_device_exit(struct gb_connection *connection);
173
174 int svc_set_route_send(struct gb_interface *interface,
175                                struct greybus_host_device *hd);
176
177 extern struct device_type greybus_interface_block_type;
178 extern struct device_type greybus_interface_type;
179 extern struct device_type greybus_connection_type;
180
181 static inline int is_gb_interface_block(const struct device *dev)
182 {
183         return dev->type == &greybus_interface_block_type;
184 }
185
186 static inline int is_gb_interface(const struct device *dev)
187 {
188         return dev->type == &greybus_interface_type;
189 }
190
191 static inline int is_gb_connection(const struct device *dev)
192 {
193         return dev->type == &greybus_connection_type;
194 }
195
196 #endif /* __KERNEL__ */
197 #endif /* __LINUX_GREYBUS_H */