blob: 033c7abe96f045910ff2d87f3a0793f1d871ae8b [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;
Alex Elder2e353682014-09-23 12:46:36 -0500137 struct work_struct event;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800138 gbuf_complete_t complete;
139};
140
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800141
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700142/* For SP1 hardware, we are going to "hardcode" each device to have all logical
143 * blocks in order to be able to address them as one unified "unit". Then
144 * higher up layers will then be able to talk to them as one logical block and
145 * properly know how they are hooked together (i.e. which i2c port is on the
146 * same module as the gpio pins, etc.)
147 *
148 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500149 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700150 */
151
152struct gb_i2c_device;
153struct gb_gpio_device;
154struct gb_sdio_host;
155struct gb_tty;
156struct gb_usb_device;
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -0700157struct gb_battery;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700158struct greybus_host_device;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700159struct svc_msg;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700160
161/* Greybus "Host driver" structure, needed by a host controller driver to be
162 * able to handle both SVC control as well as "real" greybus messages
163 */
164struct greybus_host_driver {
165 size_t hd_priv_size;
166
Alex Elder9a6f6312014-10-06 06:53:11 -0500167 int (*alloc_gbuf_data)(struct gbuf *gbuf, unsigned int size,
168 gfp_t gfp_mask);
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700169 void (*free_gbuf_data)(struct gbuf *gbuf);
Alex Elder0db32a62014-09-24 05:16:14 -0500170 int (*submit_svc)(struct svc_msg *svc_msg,
Greg Kroah-Hartmanf036e052014-09-19 19:13:33 -0700171 struct greybus_host_device *hd);
Alex Elder61418b92014-10-16 06:35:29 -0500172 int (*submit_gbuf)(struct gbuf *gbuf, gfp_t gfp_mask);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700173};
174
175struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700176 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700177 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700178 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700179
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500180 struct list_head modules;
Alex Elderee9ebe42014-10-06 06:53:08 -0500181 struct rb_root connections;
Alex Elder177404b2014-10-03 14:14:24 -0500182 struct ida cport_id_map;
Greg Kroah-Hartman25b7b6d2014-10-06 20:29:40 -0700183 spinlock_t cport_id_map_lock;
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
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500245void greybus_remove_device(struct gb_module *gmod);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700246
247/* Internal functions to gb module, move to internal .h file eventually. */
248
Greg Kroah-Hartmand0cfd102014-09-21 19:10:39 -0700249void gb_add_module(struct greybus_host_device *hd, u8 module_id,
250 u8 *data, int size);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700251void gb_remove_module(struct greybus_host_device *hd, u8 module_id);
252
Alex Elder51c75fd2014-09-26 20:55:35 -0500253int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700254int gb_ap_init(void);
255void gb_ap_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700256int gb_debugfs_init(void);
257void gb_debugfs_cleanup(void);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700258int gb_gbuf_init(void);
259void gb_gbuf_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700260
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500261int gb_register_cport_complete(struct gb_module *gmod,
Alex Elder1cfc6672014-09-30 19:25:21 -0500262 gbuf_complete_t handler, u16 cport_id,
Greg Kroah-Hartman80e04f02014-09-13 18:20:54 -0700263 void *context);
Alex Elder1cfc6672014-09-30 19:25:21 -0500264void gb_deregister_cport_complete(u16 cport_id);
Greg Kroah-Hartman80e04f02014-09-13 18:20:54 -0700265
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700266extern const struct attribute_group *greybus_module_groups[];
267
Greg Kroah-Hartman712d6592014-09-01 09:51:51 -0700268/*
269 * Because we are allocating a data structure per "type" in the greybus device,
270 * we have static functions for this, not "dynamic" drivers like we really
271 * should in the end.
272 */
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500273int gb_i2c_probe(struct gb_module *gmod, const struct greybus_module_id *id);
274void gb_i2c_disconnect(struct gb_module *gmod);
275int gb_gpio_probe(struct gb_module *gmod, const struct greybus_module_id *id);
276void gb_gpio_disconnect(struct gb_module *gmod);
277int gb_sdio_probe(struct gb_module *gmod, const struct greybus_module_id *id);
278void gb_sdio_disconnect(struct gb_module *gmod);
279int gb_tty_probe(struct gb_module *gmod, const struct greybus_module_id *id);
280void gb_tty_disconnect(struct gb_module *gmod);
281int gb_battery_probe(struct gb_module *gmod,
282 const struct greybus_module_id *id);
283void gb_battery_disconnect(struct gb_module *gmod);
Greg Kroah-Hartman712d6592014-09-01 09:51:51 -0700284
285int gb_tty_init(void);
286void gb_tty_exit(void);
287
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700288
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800289
290#endif /* __KERNEL__ */
291#endif /* __LINUX_GREYBUS_H */