blob: b817c7615516c0dcc4374877ceaacc38a2b032af [file] [log] [blame]
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08001/*
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
Alex Eldere1e9dbd2014-10-01 21:54:11 -050014#include <linux/kernel.h>
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070015#include <linux/types.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080016#include <linux/list.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050017#include <linux/slab.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080018#include <linux/device.h>
19#include <linux/module.h>
Alex Elder177404b2014-10-03 14:14:24 -050020#include <linux/idr.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050021
Alex Elder1bb3c722014-10-02 12:30:03 -050022#include "kernel_ver.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080023#include "greybus_id.h"
Alex Elder05ad1892014-09-09 13:55:03 -050024#include "greybus_manifest.h"
Alex Elderb09c94a2014-10-01 21:54:16 -050025#include "manifest.h"
Alex Eldere1e9dbd2014-10-01 21:54:11 -050026#include "module.h"
Alex Elder8c12cde2014-10-01 21:54:12 -050027#include "interface.h"
Alex Elderc68adb22014-10-01 21:54:14 -050028#include "connection.h"
Alex Elder4ccb6b72014-10-28 19:36:00 -050029#include "protocol.h"
Alex Eldere88afa52014-10-01 21:54:15 -050030#include "operation.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080031
32
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070033/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040034#define GREYBUS_VERSION_MAJOR 0x00
35#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070036
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080037#define GREYBUS_DEVICE_ID_MATCH_DEVICE \
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -070038 (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080039
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070040#define GREYBUS_DEVICE(v, p) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080041 .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070042 .vendor = (v), \
43 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080044
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070045#define GREYBUS_DEVICE_SERIAL(s) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080046 .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070047 .serial_number = (s),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080048
Alex Elder1bb3c722014-10-02 12:30:03 -050049/* 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 */
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080058
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -070059/*
60 gbuf
61
62 This is the "main" data structure to send / receive Greybus messages
63
64 There are two different "views" of a gbuf structure:
65 - a greybus driver
66 - a greybus host controller
67
68 A Greybus driver needs to worry about the following:
69 - creating a gbuf
70 - putting data into a gbuf
71 - sending a gbuf to a device
72 - receiving a gbuf from a device
73
74 Creating a gbuf:
75 A greybus driver calls greybus_alloc_gbuf()
76 Putting data into a gbuf:
77 copy data into gbuf->transfer_buffer
78 Send a gbuf:
79 A greybus driver calls greybus_submit_gbuf()
80 The completion function in a gbuf will be called if the gbuf is successful
81 or not. That completion function runs in user context. After the
82 completion function is called, the gbuf must not be touched again as the
83 greybus core "owns" it. But, if a greybus driver wants to "hold on" to a
84 gbuf after the completion function has been called, a reference must be
85 grabbed on the gbuf with a call to greybus_get_gbuf(). When finished with
86 the gbuf, call greybus_free_gbuf() and when the last reference count is
87 dropped, it will be removed from the system.
88 Receive a gbuf:
89 A greybus driver calls gb_register_cport_complete() with a pointer to the
90 callback function to be called for when a gbuf is received from a specific
91 cport and device. That callback will be made in user context with a gbuf
92 when it is received. To stop receiving messages, call
93 gb_deregister_cport_complete() for a specific cport.
94
95
96 Greybus Host controller drivers need to provide
97 - a way to allocate the transfer buffer for a gbuf
98 - a way to free the transfer buffer for a gbuf when it is "finished"
99 - a way to submit gbuf for transmissions
100 - notify the core the gbuf is complete
101 - receive gbuf from the wire and submit them to the core
102 - a way to send and receive svc messages
103 Allocate a transfer buffer
104 the host controller function alloc_gbuf_data is called
105 Free a transfer buffer
106 the host controller function free_gbuf_data is called
107 Submit a gbuf to the hardware
108 the host controller function submit_gbuf is called
109 Notify the gbuf is complete
110 the host controller driver must call greybus_gbuf_finished()
111 Submit a SVC message to the hardware
112 the host controller function send_svc_msg is called
113 Receive gbuf messages
Alex Elder0db32a62014-09-24 05:16:14 -0500114 the host controller driver must call greybus_cport_in() with the data
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700115 Reveive SVC messages from the hardware
Alex Elder0db32a62014-09-24 05:16:14 -0500116 The host controller driver must call greybus_svc_in
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700117
118
119*/
120
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800121struct gbuf {
122 struct kref kref;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800123
Alex Elderef45fa32014-11-06 07:01:02 -0600124 struct gb_operation *operation;
Alex Elder63921d82014-11-17 08:08:41 -0600125 u16 dest_cport_id; /* Destination CPort id */
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800126 int status;
Alex Elder63921d82014-11-17 08:08:41 -0600127
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800128 void *transfer_buffer;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800129 u32 transfer_buffer_length;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800130
Greg Kroah-Hartmand8144882014-10-27 13:31:01 +0800131 void *hcd_data; /* for the HCD to track the gbuf */
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800132};
133
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800134
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700135/* For SP1 hardware, we are going to "hardcode" each device to have all logical
136 * blocks in order to be able to address them as one unified "unit". Then
137 * higher up layers will then be able to talk to them as one logical block and
138 * properly know how they are hooked together (i.e. which i2c port is on the
139 * same module as the gpio pins, etc.)
140 *
141 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500142 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700143 */
144
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700145struct greybus_host_device;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700146struct svc_msg;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700147
148/* Greybus "Host driver" structure, needed by a host controller driver to be
149 * able to handle both SVC control as well as "real" greybus messages
150 */
151struct greybus_host_driver {
152 size_t hd_priv_size;
153
Alex Elder9a6f6312014-10-06 06:53:11 -0500154 int (*alloc_gbuf_data)(struct gbuf *gbuf, unsigned int size,
155 gfp_t gfp_mask);
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700156 void (*free_gbuf_data)(struct gbuf *gbuf);
Alex Elder0db32a62014-09-24 05:16:14 -0500157 int (*submit_svc)(struct svc_msg *svc_msg,
Greg Kroah-Hartmanf036e052014-09-19 19:13:33 -0700158 struct greybus_host_device *hd);
Alex Elder61418b92014-10-16 06:35:29 -0500159 int (*submit_gbuf)(struct gbuf *gbuf, gfp_t gfp_mask);
Greg Kroah-Hartman4afbba02014-10-27 14:01:06 +0800160 void (*kill_gbuf)(struct gbuf *gbuf);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700161};
162
163struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700164 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700165 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700166 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700167
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500168 struct list_head modules;
Alex Elderee9ebe42014-10-06 06:53:08 -0500169 struct rb_root connections;
Alex Elder177404b2014-10-03 14:14:24 -0500170 struct ida cport_id_map;
Greg Kroah-Hartman25b7b6d2014-10-06 20:29:40 -0700171 spinlock_t cport_id_map_lock;
Matt Porter98f4ab22014-10-21 01:52:27 -0400172 u8 device_id;
Alex Elder1bb3c722014-10-02 12:30:03 -0500173
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700174 /* Private data for the host driver */
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700175 unsigned long hd_priv[0] __aligned(sizeof(s64));
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700176};
177
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700178struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700179 struct device *parent);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700180void greybus_remove_hd(struct greybus_host_device *hd);
Alex Elder1cfc6672014-09-30 19:25:21 -0500181void greybus_cport_in(struct greybus_host_device *hd, u16 cport_id,
182 u8 *data, size_t length);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700183
Alex Elderef45fa32014-11-06 07:01:02 -0600184struct gbuf *greybus_alloc_gbuf(struct gb_operation *operation,
Alex Elder63921d82014-11-17 08:08:41 -0600185 u16 dest_cport_id, unsigned int size,
Alex Elder5d2207e2014-11-06 07:01:06 -0600186 gfp_t gfp_mask);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800187void greybus_free_gbuf(struct gbuf *gbuf);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700188struct gbuf *greybus_get_gbuf(struct gbuf *gbuf);
189#define greybus_put_gbuf greybus_free_gbuf
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800190
191int greybus_submit_gbuf(struct gbuf *gbuf, gfp_t mem_flags);
Greg Kroah-Hartman4afbba02014-10-27 14:01:06 +0800192void greybus_kill_gbuf(struct gbuf *gbuf);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800193
194
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800195struct greybus_driver {
196 const char *name;
197
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500198 int (*probe)(struct gb_module *gmod,
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700199 const struct greybus_module_id *id);
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500200 void (*disconnect)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800201
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500202 int (*suspend)(struct gb_module *gmod, pm_message_t message);
203 int (*resume)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800204
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700205 const struct greybus_module_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800206
207 struct device_driver driver;
208};
209#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
210
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800211/* Don't call these directly, use the module_greybus_driver() macro instead */
212int greybus_register_driver(struct greybus_driver *driver,
213 struct module *module, const char *mod_name);
214void greybus_deregister(struct greybus_driver *driver);
215
216/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
217#define greybus_register(driver) \
218 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
219
220/**
221 * module_greybus_driver() - Helper macro for registering a Greybus driver
222 * @__greybus_driver: greybus_driver structure
223 *
224 * Helper macro for Greybus drivers to set up proper module init / exit
225 * functions. Replaces module_init() and module_exit() and keeps people from
226 * printing pointless things to the kernel log when their driver is loaded.
227 */
228#define module_greybus_driver(__greybus_driver) \
229 module_driver(__greybus_driver, greybus_register, greybus_deregister)
230
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800231int greybus_disabled(void);
232
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700233/* Internal functions to gb module, move to internal .h file eventually. */
234
Greg Kroah-Hartmand0cfd102014-09-21 19:10:39 -0700235void gb_add_module(struct greybus_host_device *hd, u8 module_id,
236 u8 *data, int size);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700237void gb_remove_module(struct greybus_host_device *hd, u8 module_id);
Viresh Kumar676daaf2014-11-14 17:25:07 +0530238void gb_remove_modules(struct greybus_host_device *hd);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700239
Alex Elder51c75fd2014-09-26 20:55:35 -0500240int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700241int gb_ap_init(void);
242void gb_ap_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700243int gb_debugfs_init(void);
244void gb_debugfs_cleanup(void);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700245int gb_gbuf_init(void);
246void gb_gbuf_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700247
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800248extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700249extern const struct attribute_group *greybus_module_groups[];
250
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800251int gb_uart_device_init(struct gb_connection *connection);
252void gb_uart_device_exit(struct gb_connection *connection);
253
Alex Elder525f1462014-10-22 02:04:31 -0500254int svc_set_route_send(struct gb_interface *interface,
255 struct greybus_host_device *hd);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800256
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800257extern struct device_type greybus_module_type;
258extern struct device_type greybus_interface_type;
259extern struct device_type greybus_connection_type;
260
261static inline int is_gb_module(const struct device *dev)
262{
263 return dev->type == &greybus_module_type;
264}
265
266static inline int is_gb_interface(const struct device *dev)
267{
268 return dev->type == &greybus_interface_type;
269}
270
271static inline int is_gb_connection(const struct device *dev)
272{
273 return dev->type == &greybus_connection_type;
274}
275
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800276#endif /* __KERNEL__ */
277#endif /* __LINUX_GREYBUS_H */