blob: 732cc5e51dc4732b4a4937514f65de13fa11f263 [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 Eldere1e9dbd2014-10-01 21:54:11 -050020
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080021#include "greybus_id.h"
Alex Elder05ad1892014-09-09 13:55:03 -050022#include "greybus_manifest.h"
Alex Eldere1e9dbd2014-10-01 21:54:11 -050023#include "module.h"
Alex Elder8c12cde2014-10-01 21:54:12 -050024#include "interface.h"
Alex Elderef0d2ba2014-10-01 21:54:13 -050025#include "function.h"
Alex Elderc68adb22014-10-01 21:54:14 -050026#include "connection.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080027
28
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070029/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040030#define GREYBUS_VERSION_MAJOR 0x00
31#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070032
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080033#define GREYBUS_DEVICE_ID_MATCH_DEVICE \
34 (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
35
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070036#define GREYBUS_DEVICE(v, p) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080037 .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070038 .vendor = (v), \
39 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080040
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070041#define GREYBUS_DEVICE_SERIAL(s) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080042 .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070043 .serial_number = (s),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080044
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080045
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -070046/*
47 gbuf
48
49 This is the "main" data structure to send / receive Greybus messages
50
51 There are two different "views" of a gbuf structure:
52 - a greybus driver
53 - a greybus host controller
54
55 A Greybus driver needs to worry about the following:
56 - creating a gbuf
57 - putting data into a gbuf
58 - sending a gbuf to a device
59 - receiving a gbuf from a device
60
61 Creating a gbuf:
62 A greybus driver calls greybus_alloc_gbuf()
63 Putting data into a gbuf:
64 copy data into gbuf->transfer_buffer
65 Send a gbuf:
66 A greybus driver calls greybus_submit_gbuf()
67 The completion function in a gbuf will be called if the gbuf is successful
68 or not. That completion function runs in user context. After the
69 completion function is called, the gbuf must not be touched again as the
70 greybus core "owns" it. But, if a greybus driver wants to "hold on" to a
71 gbuf after the completion function has been called, a reference must be
72 grabbed on the gbuf with a call to greybus_get_gbuf(). When finished with
73 the gbuf, call greybus_free_gbuf() and when the last reference count is
74 dropped, it will be removed from the system.
75 Receive a gbuf:
76 A greybus driver calls gb_register_cport_complete() with a pointer to the
77 callback function to be called for when a gbuf is received from a specific
78 cport and device. That callback will be made in user context with a gbuf
79 when it is received. To stop receiving messages, call
80 gb_deregister_cport_complete() for a specific cport.
81
82
83 Greybus Host controller drivers need to provide
84 - a way to allocate the transfer buffer for a gbuf
85 - a way to free the transfer buffer for a gbuf when it is "finished"
86 - a way to submit gbuf for transmissions
87 - notify the core the gbuf is complete
88 - receive gbuf from the wire and submit them to the core
89 - a way to send and receive svc messages
90 Allocate a transfer buffer
91 the host controller function alloc_gbuf_data is called
92 Free a transfer buffer
93 the host controller function free_gbuf_data is called
94 Submit a gbuf to the hardware
95 the host controller function submit_gbuf is called
96 Notify the gbuf is complete
97 the host controller driver must call greybus_gbuf_finished()
98 Submit a SVC message to the hardware
99 the host controller function send_svc_msg is called
100 Receive gbuf messages
Alex Elder0db32a62014-09-24 05:16:14 -0500101 the host controller driver must call greybus_cport_in() with the data
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700102 Reveive SVC messages from the hardware
Alex Elder0db32a62014-09-24 05:16:14 -0500103 The host controller driver must call greybus_svc_in
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700104
105
106*/
107
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800108
109struct gbuf;
110
Alex Elder778c69c2014-09-22 19:19:03 -0500111struct gmod_string {
Greg Kroah-Hartman526c5c82014-09-01 16:03:31 -0700112 u16 length;
113 u8 id;
114 u8 string[0];
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800115};
116
117typedef void (*gbuf_complete_t)(struct gbuf *gbuf);
118
119struct gbuf {
120 struct kref kref;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700121 void *hdpriv;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800122
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500123 struct gb_module *gmod;
Alex Elder1cfc6672014-09-30 19:25:21 -0500124 u16 cport_id;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800125 int status;
126 void *transfer_buffer;
127 u32 transfer_flags; /* flags for the transfer buffer */
128 u32 transfer_buffer_length;
129 u32 actual_length;
130
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700131#define GBUF_DIRECTION_OUT 0
132#define GBUF_DIRECTION_IN 1
133 unsigned int direction : 1; /* 0 is out, 1 is in */
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800134
135 void *context;
Alex Elder2e353682014-09-23 12:46:36 -0500136 struct work_struct event;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800137 gbuf_complete_t complete;
138};
139
140/*
141 * gbuf->transfer_flags
142 */
143#define GBUF_FREE_BUFFER BIT(0) /* Free the transfer buffer with the gbuf */
144
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700145/* For SP1 hardware, we are going to "hardcode" each device to have all logical
146 * blocks in order to be able to address them as one unified "unit". Then
147 * higher up layers will then be able to talk to them as one logical block and
148 * properly know how they are hooked together (i.e. which i2c port is on the
149 * same module as the gpio pins, etc.)
150 *
151 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500152 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700153 */
154
155struct gb_i2c_device;
156struct gb_gpio_device;
157struct gb_sdio_host;
158struct gb_tty;
159struct gb_usb_device;
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -0700160struct gb_battery;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700161struct greybus_host_device;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700162struct svc_msg;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700163
164/* Greybus "Host driver" structure, needed by a host controller driver to be
165 * able to handle both SVC control as well as "real" greybus messages
166 */
167struct greybus_host_driver {
168 size_t hd_priv_size;
169
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700170 int (*alloc_gbuf_data)(struct gbuf *gbuf, unsigned int size, gfp_t gfp_mask);
171 void (*free_gbuf_data)(struct gbuf *gbuf);
Alex Elder0db32a62014-09-24 05:16:14 -0500172 int (*submit_svc)(struct svc_msg *svc_msg,
Greg Kroah-Hartmanf036e052014-09-19 19:13:33 -0700173 struct greybus_host_device *hd);
174 int (*submit_gbuf)(struct gbuf *gbuf, struct greybus_host_device *hd,
175 gfp_t gfp_mask);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700176};
177
178struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700179 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700180 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700181 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700182
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500183 struct list_head modules;
Alex Elderc68adb22014-10-01 21:54:14 -0500184 struct list_head connections;
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500185
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700186 /* Private data for the host driver */
187 unsigned long hd_priv[0] __attribute__ ((aligned(sizeof(s64))));
188};
189
190struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *host_driver,
191 struct device *parent);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700192void greybus_remove_hd(struct greybus_host_device *hd);
Alex Elder1cfc6672014-09-30 19:25:21 -0500193void greybus_cport_in(struct greybus_host_device *hd, u16 cport_id,
194 u8 *data, size_t length);
Greg Kroah-Hartman9c8d3af2014-09-13 11:09:35 -0700195void greybus_gbuf_finished(struct gbuf *gbuf);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700196
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500197struct gbuf *greybus_alloc_gbuf(struct gb_module *gmod, u16 cport_id,
Alex Elderecf7d572014-10-01 21:54:10 -0500198 gbuf_complete_t complete, unsigned int size,
199 gfp_t gfp_mask, void *context);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800200void greybus_free_gbuf(struct gbuf *gbuf);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700201struct gbuf *greybus_get_gbuf(struct gbuf *gbuf);
202#define greybus_put_gbuf greybus_free_gbuf
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800203
204int greybus_submit_gbuf(struct gbuf *gbuf, gfp_t mem_flags);
205int greybus_kill_gbuf(struct gbuf *gbuf);
206
207
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800208struct greybus_driver {
209 const char *name;
210
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500211 int (*probe)(struct gb_module *gmod,
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700212 const struct greybus_module_id *id);
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500213 void (*disconnect)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800214
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500215 int (*suspend)(struct gb_module *gmod, pm_message_t message);
216 int (*resume)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800217
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700218 const struct greybus_module_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800219
220 struct device_driver driver;
221};
222#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
223
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800224/* Don't call these directly, use the module_greybus_driver() macro instead */
225int greybus_register_driver(struct greybus_driver *driver,
226 struct module *module, const char *mod_name);
227void greybus_deregister(struct greybus_driver *driver);
228
229/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
230#define greybus_register(driver) \
231 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
232
233/**
234 * module_greybus_driver() - Helper macro for registering a Greybus driver
235 * @__greybus_driver: greybus_driver structure
236 *
237 * Helper macro for Greybus drivers to set up proper module init / exit
238 * functions. Replaces module_init() and module_exit() and keeps people from
239 * printing pointless things to the kernel log when their driver is loaded.
240 */
241#define module_greybus_driver(__greybus_driver) \
242 module_driver(__greybus_driver, greybus_register, greybus_deregister)
243
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800244int greybus_disabled(void);
245
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500246void greybus_remove_device(struct gb_module *gmod);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700247
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500248const u8 *greybus_string(struct gb_module *gmod, int id);
Greg Kroah-Hartman21ee4112014-09-01 18:57:42 -0700249
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700250/* Internal functions to gb module, move to internal .h file eventually. */
251
Greg Kroah-Hartmand0cfd102014-09-21 19:10:39 -0700252void gb_add_module(struct greybus_host_device *hd, u8 module_id,
253 u8 *data, int size);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700254void gb_remove_module(struct greybus_host_device *hd, u8 module_id);
255
Alex Elder51c75fd2014-09-26 20:55:35 -0500256int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700257int gb_ap_init(void);
258void gb_ap_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700259int gb_debugfs_init(void);
260void gb_debugfs_cleanup(void);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700261int gb_gbuf_init(void);
262void gb_gbuf_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700263
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500264int gb_register_cport_complete(struct gb_module *gmod,
Alex Elder1cfc6672014-09-30 19:25:21 -0500265 gbuf_complete_t handler, u16 cport_id,
Greg Kroah-Hartman80e04f02014-09-13 18:20:54 -0700266 void *context);
Alex Elder1cfc6672014-09-30 19:25:21 -0500267void gb_deregister_cport_complete(u16 cport_id);
Greg Kroah-Hartman80e04f02014-09-13 18:20:54 -0700268
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700269extern const struct attribute_group *greybus_module_groups[];
270
Greg Kroah-Hartman712d6592014-09-01 09:51:51 -0700271/*
272 * Because we are allocating a data structure per "type" in the greybus device,
273 * we have static functions for this, not "dynamic" drivers like we really
274 * should in the end.
275 */
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500276int gb_i2c_probe(struct gb_module *gmod, const struct greybus_module_id *id);
277void gb_i2c_disconnect(struct gb_module *gmod);
278int gb_gpio_probe(struct gb_module *gmod, const struct greybus_module_id *id);
279void gb_gpio_disconnect(struct gb_module *gmod);
280int gb_sdio_probe(struct gb_module *gmod, const struct greybus_module_id *id);
281void gb_sdio_disconnect(struct gb_module *gmod);
282int gb_tty_probe(struct gb_module *gmod, const struct greybus_module_id *id);
283void gb_tty_disconnect(struct gb_module *gmod);
284int gb_battery_probe(struct gb_module *gmod,
285 const struct greybus_module_id *id);
286void gb_battery_disconnect(struct gb_module *gmod);
Greg Kroah-Hartman712d6592014-09-01 09:51:51 -0700287
288int gb_tty_init(void);
289void gb_tty_exit(void);
290
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700291
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800292
293#endif /* __KERNEL__ */
294#endif /* __LINUX_GREYBUS_H */