blob: 617d55ca7e1d3152de48b073fa7593629de99fe3 [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 Eldere88afa52014-10-01 21:54:15 -050029#include "operation.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080030
31
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070032/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040033#define GREYBUS_VERSION_MAJOR 0x00
34#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070035
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080036#define GREYBUS_DEVICE_ID_MATCH_DEVICE \
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -070037 (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080038
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070039#define GREYBUS_DEVICE(v, p) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080040 .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070041 .vendor = (v), \
42 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080043
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070044#define GREYBUS_DEVICE_SERIAL(s) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080045 .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070046 .serial_number = (s),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080047
Alex Elder1bb3c722014-10-02 12:30:03 -050048/* XXX I couldn't get my Kconfig file to be noticed for out-of-tree build */
49#ifndef CONFIG_HOST_DEV_CPORT_ID_MAX
50#define CONFIG_HOST_DEV_CPORT_ID_MAX 128
51#endif /* !CONFIG_HOST_DEV_CPORT_ID_MAX */
52
53/* Maximum number of CPorts usable by a host device */
54/* XXX This should really be determined by the AP module manifest */
55#define HOST_DEV_CPORT_ID_MAX CONFIG_HOST_DEV_CPORT_ID_MAX
56#define CPORT_ID_BAD U16_MAX /* UniPro max id is 4095 */
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080057
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -070058/*
59 gbuf
60
61 This is the "main" data structure to send / receive Greybus messages
62
63 There are two different "views" of a gbuf structure:
64 - a greybus driver
65 - a greybus host controller
66
67 A Greybus driver needs to worry about the following:
68 - creating a gbuf
69 - putting data into a gbuf
70 - sending a gbuf to a device
71 - receiving a gbuf from a device
72
73 Creating a gbuf:
74 A greybus driver calls greybus_alloc_gbuf()
75 Putting data into a gbuf:
76 copy data into gbuf->transfer_buffer
77 Send a gbuf:
78 A greybus driver calls greybus_submit_gbuf()
79 The completion function in a gbuf will be called if the gbuf is successful
80 or not. That completion function runs in user context. After the
81 completion function is called, the gbuf must not be touched again as the
82 greybus core "owns" it. But, if a greybus driver wants to "hold on" to a
83 gbuf after the completion function has been called, a reference must be
84 grabbed on the gbuf with a call to greybus_get_gbuf(). When finished with
85 the gbuf, call greybus_free_gbuf() and when the last reference count is
86 dropped, it will be removed from the system.
87 Receive a gbuf:
88 A greybus driver calls gb_register_cport_complete() with a pointer to the
89 callback function to be called for when a gbuf is received from a specific
90 cport and device. That callback will be made in user context with a gbuf
91 when it is received. To stop receiving messages, call
92 gb_deregister_cport_complete() for a specific cport.
93
94
95 Greybus Host controller drivers need to provide
96 - a way to allocate the transfer buffer for a gbuf
97 - a way to free the transfer buffer for a gbuf when it is "finished"
98 - a way to submit gbuf for transmissions
99 - notify the core the gbuf is complete
100 - receive gbuf from the wire and submit them to the core
101 - a way to send and receive svc messages
102 Allocate a transfer buffer
103 the host controller function alloc_gbuf_data is called
104 Free a transfer buffer
105 the host controller function free_gbuf_data is called
106 Submit a gbuf to the hardware
107 the host controller function submit_gbuf is called
108 Notify the gbuf is complete
109 the host controller driver must call greybus_gbuf_finished()
110 Submit a SVC message to the hardware
111 the host controller function send_svc_msg is called
112 Receive gbuf messages
Alex Elder0db32a62014-09-24 05:16:14 -0500113 the host controller driver must call greybus_cport_in() with the data
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700114 Reveive SVC messages from the hardware
Alex Elder0db32a62014-09-24 05:16:14 -0500115 The host controller driver must call greybus_svc_in
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700116
117
118*/
119
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800120
121struct gbuf;
122
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800123typedef void (*gbuf_complete_t)(struct gbuf *gbuf);
124
125struct gbuf {
126 struct kref kref;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800127
Alex Elder6eb3f4b2014-10-06 06:53:10 -0500128 struct gb_connection *connection;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800129 int status;
130 void *transfer_buffer;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800131 u32 transfer_buffer_length;
132 u32 actual_length;
133
Alex Elder9a6f6312014-10-06 06:53:11 -0500134 bool outbound; /* AP-relative data direction */
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800135
136 void *context;
137 gbuf_complete_t complete;
138};
139
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800140
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700141/* For SP1 hardware, we are going to "hardcode" each device to have all logical
142 * blocks in order to be able to address them as one unified "unit". Then
143 * higher up layers will then be able to talk to them as one logical block and
144 * properly know how they are hooked together (i.e. which i2c port is on the
145 * same module as the gpio pins, etc.)
146 *
147 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500148 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700149 */
150
151struct gb_i2c_device;
152struct gb_gpio_device;
153struct gb_sdio_host;
154struct gb_tty;
155struct gb_usb_device;
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -0700156struct gb_battery;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700157struct greybus_host_device;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700158struct svc_msg;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700159
160/* Greybus "Host driver" structure, needed by a host controller driver to be
161 * able to handle both SVC control as well as "real" greybus messages
162 */
163struct greybus_host_driver {
164 size_t hd_priv_size;
165
Alex Elder9a6f6312014-10-06 06:53:11 -0500166 int (*alloc_gbuf_data)(struct gbuf *gbuf, unsigned int size,
167 gfp_t gfp_mask);
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700168 void (*free_gbuf_data)(struct gbuf *gbuf);
Alex Elder0db32a62014-09-24 05:16:14 -0500169 int (*submit_svc)(struct svc_msg *svc_msg,
Greg Kroah-Hartmanf036e052014-09-19 19:13:33 -0700170 struct greybus_host_device *hd);
Alex Elder61418b92014-10-16 06:35:29 -0500171 int (*submit_gbuf)(struct gbuf *gbuf, gfp_t gfp_mask);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700172};
173
174struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700175 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700176 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700177 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700178
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500179 struct list_head modules;
Alex Elderee9ebe42014-10-06 06:53:08 -0500180 struct rb_root connections;
Alex Elder177404b2014-10-03 14:14:24 -0500181 struct ida cport_id_map;
Greg Kroah-Hartman25b7b6d2014-10-06 20:29:40 -0700182 spinlock_t cport_id_map_lock;
Matt Porter98f4ab22014-10-21 01:52:27 -0400183 u8 device_id;
Alex Elder1bb3c722014-10-02 12:30:03 -0500184
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700185 /* Private data for the host driver */
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700186 unsigned long hd_priv[0] __aligned(sizeof(s64));
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700187};
188
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700189struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700190 struct device *parent);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700191void greybus_remove_hd(struct greybus_host_device *hd);
Alex Elder1cfc6672014-09-30 19:25:21 -0500192void greybus_cport_in(struct greybus_host_device *hd, u16 cport_id,
193 u8 *data, size_t length);
Greg Kroah-Hartman9c8d3af2014-09-13 11:09:35 -0700194void greybus_gbuf_finished(struct gbuf *gbuf);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700195
Alex Elder6eb3f4b2014-10-06 06:53:10 -0500196struct gbuf *greybus_alloc_gbuf(struct gb_connection *connection,
Alex Elderecf7d572014-10-01 21:54:10 -0500197 gbuf_complete_t complete, unsigned int size,
Alex Elder9a6f6312014-10-06 06:53:11 -0500198 bool outbound, gfp_t gfp_mask, void *context);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800199void greybus_free_gbuf(struct gbuf *gbuf);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700200struct gbuf *greybus_get_gbuf(struct gbuf *gbuf);
201#define greybus_put_gbuf greybus_free_gbuf
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800202
203int greybus_submit_gbuf(struct gbuf *gbuf, gfp_t mem_flags);
204int greybus_kill_gbuf(struct gbuf *gbuf);
205
206
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800207struct greybus_driver {
208 const char *name;
209
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500210 int (*probe)(struct gb_module *gmod,
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700211 const struct greybus_module_id *id);
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500212 void (*disconnect)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800213
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500214 int (*suspend)(struct gb_module *gmod, pm_message_t message);
215 int (*resume)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800216
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700217 const struct greybus_module_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800218
219 struct device_driver driver;
220};
221#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
222
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800223/* Don't call these directly, use the module_greybus_driver() macro instead */
224int greybus_register_driver(struct greybus_driver *driver,
225 struct module *module, const char *mod_name);
226void greybus_deregister(struct greybus_driver *driver);
227
228/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
229#define greybus_register(driver) \
230 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
231
232/**
233 * module_greybus_driver() - Helper macro for registering a Greybus driver
234 * @__greybus_driver: greybus_driver structure
235 *
236 * Helper macro for Greybus drivers to set up proper module init / exit
237 * functions. Replaces module_init() and module_exit() and keeps people from
238 * printing pointless things to the kernel log when their driver is loaded.
239 */
240#define module_greybus_driver(__greybus_driver) \
241 module_driver(__greybus_driver, greybus_register, greybus_deregister)
242
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800243int greybus_disabled(void);
244
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700245/* Internal functions to gb module, move to internal .h file eventually. */
246
Greg Kroah-Hartmand0cfd102014-09-21 19:10:39 -0700247void gb_add_module(struct greybus_host_device *hd, u8 module_id,
248 u8 *data, int size);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700249void gb_remove_module(struct greybus_host_device *hd, u8 module_id);
250
Alex Elder51c75fd2014-09-26 20:55:35 -0500251int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700252int gb_ap_init(void);
253void gb_ap_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700254int gb_debugfs_init(void);
255void gb_debugfs_cleanup(void);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700256int gb_gbuf_init(void);
257void gb_gbuf_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700258
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500259int gb_register_cport_complete(struct gb_module *gmod,
Alex Elder1cfc6672014-09-30 19:25:21 -0500260 gbuf_complete_t handler, u16 cport_id,
Greg Kroah-Hartman80e04f02014-09-13 18:20:54 -0700261 void *context);
Alex Elder1cfc6672014-09-30 19:25:21 -0500262void gb_deregister_cport_complete(u16 cport_id);
Greg Kroah-Hartman80e04f02014-09-13 18:20:54 -0700263
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700264extern const struct attribute_group *greybus_module_groups[];
265
Alex Elder574341c2014-10-16 06:35:35 -0500266int gb_i2c_device_init(struct gb_connection *connection);
Alex Elder697e55d2014-10-20 23:01:04 -0500267void gb_i2c_device_exit(struct gb_connection *connection);
268
Greg Kroah-Hartman2bb7eae2014-10-20 15:24:57 +0800269int gb_battery_device_init(struct gb_connection *connection);
Alex Elder697e55d2014-10-20 23:01:04 -0500270void gb_battery_device_exit(struct gb_connection *connection);
271
Alex Elderbb2e1c92014-10-16 06:35:39 -0500272int gb_gpio_controller_init(struct gb_connection *connection);
Alex Elder697e55d2014-10-20 23:01:04 -0500273void gb_gpio_controller_exit(struct gb_connection *connection);
Alex Elder574341c2014-10-16 06:35:35 -0500274
Greg Kroah-Hartman712d6592014-09-01 09:51:51 -0700275int gb_tty_init(void);
276void gb_tty_exit(void);
277
Alex Elder525f1462014-10-22 02:04:31 -0500278int svc_set_route_send(struct gb_interface *interface,
279 struct greybus_host_device *hd);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800280
281#endif /* __KERNEL__ */
282#endif /* __LINUX_GREYBUS_H */