blob: 5a7f6227c1e505595e77ab947d807c9053353d04 [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-Hartmandf671552014-12-21 14:10:26 -080027#include "module.h"
Greg Kroah-Hartmana93938a2014-12-19 14:56:30 -080028#include "interface.h"
Greg Kroah-Hartman3bdec692014-12-12 17:10:16 -050029#include "bundle.h"
Alex Elderc68adb22014-10-01 21:54:14 -050030#include "connection.h"
Alex Elder4ccb6b72014-10-28 19:36:00 -050031#include "protocol.h"
Alex Eldere88afa52014-10-01 21:54:15 -050032#include "operation.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080033
34
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070035/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040036#define GREYBUS_VERSION_MAJOR 0x00
37#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070038
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080039#define GREYBUS_DEVICE_ID_MATCH_DEVICE \
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -070040 (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080041
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070042#define GREYBUS_DEVICE(v, p) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080043 .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070044 .vendor = (v), \
45 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080046
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070047#define GREYBUS_DEVICE_SERIAL(s) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080048 .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070049 .serial_number = (s),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080050
Alex Elder1bb3c722014-10-02 12:30:03 -050051/* XXX I couldn't get my Kconfig file to be noticed for out-of-tree build */
52#ifndef CONFIG_HOST_DEV_CPORT_ID_MAX
53#define CONFIG_HOST_DEV_CPORT_ID_MAX 128
54#endif /* !CONFIG_HOST_DEV_CPORT_ID_MAX */
55
56/* Maximum number of CPorts usable by a host device */
57/* XXX This should really be determined by the AP module manifest */
58#define HOST_DEV_CPORT_ID_MAX CONFIG_HOST_DEV_CPORT_ID_MAX
59#define CPORT_ID_BAD U16_MAX /* UniPro max id is 4095 */
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080060
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -070061/* For SP1 hardware, we are going to "hardcode" each device to have all logical
62 * blocks in order to be able to address them as one unified "unit". Then
63 * higher up layers will then be able to talk to them as one logical block and
64 * properly know how they are hooked together (i.e. which i2c port is on the
65 * same module as the gpio pins, etc.)
66 *
67 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -050068 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -070069 */
70
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070071struct greybus_host_device;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -070072struct svc_msg;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070073
Alex Elder835fb5e2014-12-02 08:30:34 -060074/*
75 * When the Greybus code allocates a buffer it sets aside bytes
76 * prior to the beginning of the payload area for the host device's
77 * exclusive use. The size is specified by hd->buffer_headroom, and
78 * which can't be greater than GB_BUFFER_HEADROOM_MAX.
79 */
80#define GB_BUFFER_HEADROOM_MAX sizeof(u64)
81
Alex Elder06a4a062014-11-18 13:26:40 -060082/* Buffers allocated from the host driver will be aligned to this multiple */
83#define GB_BUFFER_ALIGN sizeof(u32)
84
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070085/* Greybus "Host driver" structure, needed by a host controller driver to be
86 * able to handle both SVC control as well as "real" greybus messages
87 */
88struct greybus_host_driver {
89 size_t hd_priv_size;
90
Alex Elder58a5bdc2014-11-18 13:26:46 -060091 void *(*buffer_send)(struct greybus_host_device *hd, u16 dest_cport_id,
92 void *buffer, size_t buffer_size, gfp_t gfp_mask);
Alex Eldera9163b22014-11-18 13:26:44 -060093 void (*buffer_cancel)(void *cookie);
Alex Elder0db32a62014-09-24 05:16:14 -050094 int (*submit_svc)(struct svc_msg *svc_msg,
Greg Kroah-Hartmanf036e052014-09-19 19:13:33 -070095 struct greybus_host_device *hd);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070096};
97
98struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070099 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700100 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700101 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700102
Greg Kroah-Hartman1cd56a82014-12-19 14:56:36 -0800103 struct list_head interfaces;
Alex Elder2c43ce42014-11-17 08:08:44 -0600104 struct list_head connections;
Alex Elder177404b2014-10-03 14:14:24 -0500105 struct ida cport_id_map;
Matt Porter98f4ab22014-10-21 01:52:27 -0400106 u8 device_id;
Alex Elder1bb3c722014-10-02 12:30:03 -0500107
Alex Elder8b337302014-11-20 16:09:13 -0600108 /* Host device buffer constraints */
109 size_t buffer_headroom;
110 size_t buffer_size_max;
111
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700112 /* Private data for the host driver */
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700113 unsigned long hd_priv[0] __aligned(sizeof(s64));
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700114};
115
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700116struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700117 struct device *parent);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700118void greybus_remove_hd(struct greybus_host_device *hd);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800119
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800120struct greybus_driver {
121 const char *name;
122
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800123 int (*probe)(struct gb_interface *intf,
Greg Kroah-Hartmanedb0e0b2014-12-19 14:56:32 -0800124 const struct greybus_interface_id *id);
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800125 void (*disconnect)(struct gb_interface *intf);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800126
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800127 int (*suspend)(struct gb_interface *intf, pm_message_t message);
128 int (*resume)(struct gb_interface *intf);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800129
Greg Kroah-Hartmanedb0e0b2014-12-19 14:56:32 -0800130 const struct greybus_interface_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800131
132 struct device_driver driver;
133};
134#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
135
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800136/* Don't call these directly, use the module_greybus_driver() macro instead */
137int greybus_register_driver(struct greybus_driver *driver,
138 struct module *module, const char *mod_name);
139void greybus_deregister(struct greybus_driver *driver);
140
141/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
142#define greybus_register(driver) \
143 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
144
145/**
146 * module_greybus_driver() - Helper macro for registering a Greybus driver
147 * @__greybus_driver: greybus_driver structure
148 *
149 * Helper macro for Greybus drivers to set up proper module init / exit
150 * functions. Replaces module_init() and module_exit() and keeps people from
151 * printing pointless things to the kernel log when their driver is loaded.
152 */
153#define module_greybus_driver(__greybus_driver) \
154 module_driver(__greybus_driver, greybus_register, greybus_deregister)
155
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800156int greybus_disabled(void);
157
Alex Elder51c75fd2014-09-26 20:55:35 -0500158int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700159int gb_ap_init(void);
160void gb_ap_exit(void);
Greg Kroah-Hartman48f70472015-03-27 11:38:06 +0100161void gb_debugfs_init(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700162void gb_debugfs_cleanup(void);
Alexandre Bailone8f824b2015-03-18 15:42:51 +0100163struct dentry *gb_debugfs_get(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700164
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800165extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700166
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800167int gb_uart_device_init(struct gb_connection *connection);
168void gb_uart_device_exit(struct gb_connection *connection);
169
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500170int svc_set_route_send(struct gb_bundle *bundle,
Alex Elder525f1462014-10-22 02:04:31 -0500171 struct greybus_host_device *hd);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800172
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -0800173extern struct device_type greybus_module_type;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800174extern struct device_type greybus_interface_type;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500175extern struct device_type greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800176extern struct device_type greybus_connection_type;
177
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -0800178static inline int is_gb_module(const struct device *dev)
179{
180 return dev->type == &greybus_module_type;
181}
182
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800183static inline int is_gb_interface(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800184{
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800185 return dev->type == &greybus_interface_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800186}
187
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500188static inline int is_gb_bundle(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800189{
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500190 return dev->type == &greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800191}
192
193static inline int is_gb_connection(const struct device *dev)
194{
195 return dev->type == &greybus_connection_type;
196}
197
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800198#endif /* __KERNEL__ */
199#endif /* __LINUX_GREYBUS_H */