blob: e0c5eb7bd13f4bd57c173be40efed88d776fcac5 [file] [log] [blame]
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08001/*
2 * Greybus driver and device API
3 *
4 * Copyright 2014 Google Inc.
Alex Eldera46e9672014-12-12 12:08:42 -06005 * Copyright 2014 Linaro Ltd.
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08006 *
7 * Released under the GPLv2 only.
8 */
9
10#ifndef __LINUX_GREYBUS_H
11#define __LINUX_GREYBUS_H
12
13#ifdef __KERNEL__
14
Alex Eldere1e9dbd2014-10-01 21:54:11 -050015#include <linux/kernel.h>
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070016#include <linux/types.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080017#include <linux/list.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050018#include <linux/slab.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080019#include <linux/device.h>
20#include <linux/module.h>
Alex Elder177404b2014-10-03 14:14:24 -050021#include <linux/idr.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050022
Alex Elder1bb3c722014-10-02 12:30:03 -050023#include "kernel_ver.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080024#include "greybus_id.h"
Alex Elder05ad1892014-09-09 13:55:03 -050025#include "greybus_manifest.h"
Alex Elderb09c94a2014-10-01 21:54:16 -050026#include "manifest.h"
Greg Kroah-Hartmana93938a2014-12-19 14:56:30 -080027#include "interface.h"
Greg Kroah-Hartman3bdec692014-12-12 17:10:16 -050028#include "bundle.h"
Alex Elderc68adb22014-10-01 21:54:14 -050029#include "connection.h"
Alex Elder4ccb6b72014-10-28 19:36:00 -050030#include "protocol.h"
Alex Eldere88afa52014-10-01 21:54:15 -050031#include "operation.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080032
33
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070034/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040035#define GREYBUS_VERSION_MAJOR 0x00
36#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070037
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080038#define GREYBUS_DEVICE_ID_MATCH_DEVICE \
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -070039 (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080040
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070041#define GREYBUS_DEVICE(v, p) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080042 .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070043 .vendor = (v), \
44 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080045
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070046#define GREYBUS_DEVICE_SERIAL(s) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080047 .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070048 .serial_number = (s),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080049
Alex Elder1bb3c722014-10-02 12:30:03 -050050/* XXX I couldn't get my Kconfig file to be noticed for out-of-tree build */
51#ifndef CONFIG_HOST_DEV_CPORT_ID_MAX
52#define CONFIG_HOST_DEV_CPORT_ID_MAX 128
53#endif /* !CONFIG_HOST_DEV_CPORT_ID_MAX */
54
55/* Maximum number of CPorts usable by a host device */
56/* XXX This should really be determined by the AP module manifest */
57#define HOST_DEV_CPORT_ID_MAX CONFIG_HOST_DEV_CPORT_ID_MAX
58#define CPORT_ID_BAD U16_MAX /* UniPro max id is 4095 */
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080059
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -070060/* For SP1 hardware, we are going to "hardcode" each device to have all logical
61 * blocks in order to be able to address them as one unified "unit". Then
62 * higher up layers will then be able to talk to them as one logical block and
63 * properly know how they are hooked together (i.e. which i2c port is on the
64 * same module as the gpio pins, etc.)
65 *
66 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -050067 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -070068 */
69
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070070struct greybus_host_device;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -070071struct svc_msg;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070072
Alex Elder835fb5e2014-12-02 08:30:34 -060073/*
74 * When the Greybus code allocates a buffer it sets aside bytes
75 * prior to the beginning of the payload area for the host device's
76 * exclusive use. The size is specified by hd->buffer_headroom, and
77 * which can't be greater than GB_BUFFER_HEADROOM_MAX.
78 */
79#define GB_BUFFER_HEADROOM_MAX sizeof(u64)
80
Alex Elder06a4a062014-11-18 13:26:40 -060081/* Buffers allocated from the host driver will be aligned to this multiple */
82#define GB_BUFFER_ALIGN sizeof(u32)
83
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070084/* Greybus "Host driver" structure, needed by a host controller driver to be
85 * able to handle both SVC control as well as "real" greybus messages
86 */
87struct greybus_host_driver {
88 size_t hd_priv_size;
89
Alex Elder58a5bdc2014-11-18 13:26:46 -060090 void *(*buffer_send)(struct greybus_host_device *hd, u16 dest_cport_id,
91 void *buffer, size_t buffer_size, gfp_t gfp_mask);
Alex Eldera9163b22014-11-18 13:26:44 -060092 void (*buffer_cancel)(void *cookie);
Alex Elder0db32a62014-09-24 05:16:14 -050093 int (*submit_svc)(struct svc_msg *svc_msg,
Greg Kroah-Hartmanf036e052014-09-19 19:13:33 -070094 struct greybus_host_device *hd);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070095};
96
97struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070098 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -070099 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700100 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700101
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500102 struct list_head modules;
Alex Elder2c43ce42014-11-17 08:08:44 -0600103 struct list_head connections;
Alex Elder177404b2014-10-03 14:14:24 -0500104 struct ida cport_id_map;
Matt Porter98f4ab22014-10-21 01:52:27 -0400105 u8 device_id;
Alex Elder1bb3c722014-10-02 12:30:03 -0500106
Alex Elder8b337302014-11-20 16:09:13 -0600107 /* Host device buffer constraints */
108 size_t buffer_headroom;
109 size_t buffer_size_max;
110
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700111 /* Private data for the host driver */
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700112 unsigned long hd_priv[0] __aligned(sizeof(s64));
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700113};
114
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700115struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700116 struct device *parent);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700117void greybus_remove_hd(struct greybus_host_device *hd);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800118
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800119struct greybus_driver {
120 const char *name;
121
Greg Kroah-Hartman4ec7b072014-12-11 17:10:56 -0500122 int (*probe)(struct gb_interface_block *gb_ib,
Greg Kroah-Hartman2f0c8aa2014-12-11 17:11:02 -0500123 const struct greybus_interface_block_id *id);
Greg Kroah-Hartman4ec7b072014-12-11 17:10:56 -0500124 void (*disconnect)(struct gb_interface_block *gb_ib);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800125
Greg Kroah-Hartman4ec7b072014-12-11 17:10:56 -0500126 int (*suspend)(struct gb_interface_block *gb_ib, pm_message_t message);
127 int (*resume)(struct gb_interface_block *gb_ib);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800128
Greg Kroah-Hartman2f0c8aa2014-12-11 17:11:02 -0500129 const struct greybus_interface_block_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800130
131 struct device_driver driver;
132};
133#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
134
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800135/* Don't call these directly, use the module_greybus_driver() macro instead */
136int greybus_register_driver(struct greybus_driver *driver,
137 struct module *module, const char *mod_name);
138void greybus_deregister(struct greybus_driver *driver);
139
140/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
141#define greybus_register(driver) \
142 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
143
144/**
145 * module_greybus_driver() - Helper macro for registering a Greybus driver
146 * @__greybus_driver: greybus_driver structure
147 *
148 * Helper macro for Greybus drivers to set up proper module init / exit
149 * functions. Replaces module_init() and module_exit() and keeps people from
150 * printing pointless things to the kernel log when their driver is loaded.
151 */
152#define module_greybus_driver(__greybus_driver) \
153 module_driver(__greybus_driver, greybus_register, greybus_deregister)
154
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800155int greybus_disabled(void);
156
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700157/* Internal functions to gb module, move to internal .h file eventually. */
158
Greg Kroah-Hartmand0cfd102014-09-21 19:10:39 -0700159void gb_add_module(struct greybus_host_device *hd, u8 module_id,
160 u8 *data, int size);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700161void gb_remove_module(struct greybus_host_device *hd, u8 module_id);
Viresh Kumar676daaf2014-11-14 17:25:07 +0530162void gb_remove_modules(struct greybus_host_device *hd);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700163
Alex Elder51c75fd2014-09-26 20:55:35 -0500164int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700165int gb_ap_init(void);
166void gb_ap_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700167int gb_debugfs_init(void);
168void gb_debugfs_cleanup(void);
169
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800170extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700171
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800172int gb_uart_device_init(struct gb_connection *connection);
173void gb_uart_device_exit(struct gb_connection *connection);
174
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500175int svc_set_route_send(struct gb_bundle *bundle,
Alex Elder525f1462014-10-22 02:04:31 -0500176 struct greybus_host_device *hd);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800177
Greg Kroah-Hartman4ec7b072014-12-11 17:10:56 -0500178extern struct device_type greybus_interface_block_type;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500179extern struct device_type greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800180extern struct device_type greybus_connection_type;
181
Greg Kroah-Hartman4ec7b072014-12-11 17:10:56 -0500182static inline int is_gb_interface_block(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800183{
Greg Kroah-Hartman4ec7b072014-12-11 17:10:56 -0500184 return dev->type == &greybus_interface_block_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800185}
186
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500187static inline int is_gb_bundle(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800188{
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500189 return dev->type == &greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800190}
191
192static inline int is_gb_connection(const struct device *dev)
193{
194 return dev->type == &greybus_connection_type;
195}
196
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800197#endif /* __KERNEL__ */
198#endif /* __LINUX_GREYBUS_H */