blob: a9b2b459d7ad67c39c3f626cb8d749692677525e [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-Hartman199d68d2014-08-30 16:20:22 -070059/* For SP1 hardware, we are going to "hardcode" each device to have all logical
60 * blocks in order to be able to address them as one unified "unit". Then
61 * higher up layers will then be able to talk to them as one logical block and
62 * properly know how they are hooked together (i.e. which i2c port is on the
63 * same module as the gpio pins, etc.)
64 *
65 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -050066 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -070067 */
68
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070069struct greybus_host_device;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -070070struct svc_msg;
Alex Elder3c3cef42014-11-17 18:08:30 -060071struct gbuf;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070072
Alex Elder06a4a062014-11-18 13:26:40 -060073/* Buffers allocated from the host driver will be aligned to this multiple */
74#define GB_BUFFER_ALIGN sizeof(u32)
75
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070076/* Greybus "Host driver" structure, needed by a host controller driver to be
77 * able to handle both SVC control as well as "real" greybus messages
78 */
79struct greybus_host_driver {
80 size_t hd_priv_size;
81
Alex Elderd2a259f2014-11-18 13:26:42 -060082 void *(*buffer_alloc)(unsigned int size, gfp_t gfp_mask);
Alex Elder9ec54112014-11-18 13:26:43 -060083 void (*buffer_free)(void *buffer);
Alex Eldera9163b22014-11-18 13:26:44 -060084 int (*submit_gbuf)(struct gbuf *gbuf, gfp_t gfp_mask);
85 void (*buffer_cancel)(void *cookie);
Alex Elder0db32a62014-09-24 05:16:14 -050086 int (*submit_svc)(struct svc_msg *svc_msg,
Greg Kroah-Hartmanf036e052014-09-19 19:13:33 -070087 struct greybus_host_device *hd);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070088};
89
90struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070091 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -070092 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070093 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070094
Alex Eldere1e9dbd2014-10-01 21:54:11 -050095 struct list_head modules;
Alex Elder2c43ce42014-11-17 08:08:44 -060096 struct list_head connections;
Alex Elder177404b2014-10-03 14:14:24 -050097 struct ida cport_id_map;
Matt Porter98f4ab22014-10-21 01:52:27 -040098 u8 device_id;
Alex Elder1bb3c722014-10-02 12:30:03 -050099
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700100 /* Private data for the host driver */
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700101 unsigned long hd_priv[0] __aligned(sizeof(s64));
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700102};
103
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700104struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700105 struct device *parent);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700106void greybus_remove_hd(struct greybus_host_device *hd);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800107
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800108struct greybus_driver {
109 const char *name;
110
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500111 int (*probe)(struct gb_module *gmod,
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700112 const struct greybus_module_id *id);
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500113 void (*disconnect)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800114
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500115 int (*suspend)(struct gb_module *gmod, pm_message_t message);
116 int (*resume)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800117
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700118 const struct greybus_module_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800119
120 struct device_driver driver;
121};
122#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
123
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800124/* Don't call these directly, use the module_greybus_driver() macro instead */
125int greybus_register_driver(struct greybus_driver *driver,
126 struct module *module, const char *mod_name);
127void greybus_deregister(struct greybus_driver *driver);
128
129/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
130#define greybus_register(driver) \
131 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
132
133/**
134 * module_greybus_driver() - Helper macro for registering a Greybus driver
135 * @__greybus_driver: greybus_driver structure
136 *
137 * Helper macro for Greybus drivers to set up proper module init / exit
138 * functions. Replaces module_init() and module_exit() and keeps people from
139 * printing pointless things to the kernel log when their driver is loaded.
140 */
141#define module_greybus_driver(__greybus_driver) \
142 module_driver(__greybus_driver, greybus_register, greybus_deregister)
143
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800144int greybus_disabled(void);
145
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700146/* Internal functions to gb module, move to internal .h file eventually. */
147
Greg Kroah-Hartmand0cfd102014-09-21 19:10:39 -0700148void gb_add_module(struct greybus_host_device *hd, u8 module_id,
149 u8 *data, int size);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700150void gb_remove_module(struct greybus_host_device *hd, u8 module_id);
Viresh Kumar676daaf2014-11-14 17:25:07 +0530151void gb_remove_modules(struct greybus_host_device *hd);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700152
Alex Elder51c75fd2014-09-26 20:55:35 -0500153int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700154int gb_ap_init(void);
155void gb_ap_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700156int gb_debugfs_init(void);
157void gb_debugfs_cleanup(void);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700158int gb_gbuf_init(void);
159void gb_gbuf_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700160
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800161extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700162extern const struct attribute_group *greybus_module_groups[];
163
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800164int gb_uart_device_init(struct gb_connection *connection);
165void gb_uart_device_exit(struct gb_connection *connection);
166
Alex Elder525f1462014-10-22 02:04:31 -0500167int svc_set_route_send(struct gb_interface *interface,
168 struct greybus_host_device *hd);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800169
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800170extern struct device_type greybus_module_type;
171extern struct device_type greybus_interface_type;
172extern struct device_type greybus_connection_type;
173
174static inline int is_gb_module(const struct device *dev)
175{
176 return dev->type == &greybus_module_type;
177}
178
179static inline int is_gb_interface(const struct device *dev)
180{
181 return dev->type == &greybus_interface_type;
182}
183
184static inline int is_gb_connection(const struct device *dev)
185{
186 return dev->type == &greybus_connection_type;
187}
188
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800189#endif /* __KERNEL__ */
190#endif /* __LINUX_GREYBUS_H */