Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Greybus driver and device API |
| 3 | * |
Alex Elder | 4441f47 | 2015-05-22 12:59:16 -0500 | [diff] [blame] | 4 | * Copyright 2014-2015 Google Inc. |
| 5 | * Copyright 2014-2015 Linaro Ltd. |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #ifndef __LINUX_GREYBUS_H |
| 11 | #define __LINUX_GREYBUS_H |
| 12 | |
| 13 | #ifdef __KERNEL__ |
| 14 | |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 16 | #include <linux/types.h> |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 17 | #include <linux/list.h> |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 18 | #include <linux/slab.h> |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 19 | #include <linux/device.h> |
| 20 | #include <linux/module.h> |
Alex Elder | 177404b | 2014-10-03 14:14:24 -0500 | [diff] [blame] | 21 | #include <linux/idr.h> |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 22 | |
Alex Elder | 1bb3c72 | 2014-10-02 12:30:03 -0500 | [diff] [blame] | 23 | #include "kernel_ver.h" |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 24 | #include "greybus_id.h" |
Alex Elder | 05ad189 | 2014-09-09 13:55:03 -0500 | [diff] [blame] | 25 | #include "greybus_manifest.h" |
Alex Elder | 22e17ed | 2015-05-22 12:35:32 -0500 | [diff] [blame] | 26 | #include "greybus_protocols.h" |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 27 | #include "manifest.h" |
Greg Kroah-Hartman | 0f035ac | 2015-04-07 20:26:53 +0200 | [diff] [blame] | 28 | #include "endo.h" |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 29 | #include "svc.h" |
Greg Kroah-Hartman | df67155 | 2014-12-21 14:10:26 -0800 | [diff] [blame] | 30 | #include "module.h" |
Greg Kroah-Hartman | a93938a | 2014-12-19 14:56:30 -0800 | [diff] [blame] | 31 | #include "interface.h" |
Greg Kroah-Hartman | 3bdec69 | 2014-12-12 17:10:16 -0500 | [diff] [blame] | 32 | #include "bundle.h" |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 33 | #include "connection.h" |
Alex Elder | 4ccb6b7 | 2014-10-28 19:36:00 -0500 | [diff] [blame] | 34 | #include "protocol.h" |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 35 | #include "operation.h" |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 36 | |
| 37 | |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 38 | /* Matches up with the Greybus Protocol specification document */ |
Matt Porter | 52adb56 | 2014-09-18 15:25:43 -0400 | [diff] [blame] | 39 | #define GREYBUS_VERSION_MAJOR 0x00 |
| 40 | #define GREYBUS_VERSION_MINOR 0x01 |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 41 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 42 | #define GREYBUS_DEVICE_ID_MATCH_DEVICE \ |
Greg Kroah-Hartman | 8faa8da | 2014-10-06 20:34:48 -0700 | [diff] [blame] | 43 | (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT) |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 44 | |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 45 | #define GREYBUS_DEVICE(v, p) \ |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 46 | .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \ |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 47 | .vendor = (v), \ |
| 48 | .product = (p), |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 49 | |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 50 | #define GREYBUS_DEVICE_SERIAL(s) \ |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 51 | .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \ |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 52 | .serial_number = (s), |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 53 | |
Alex Elder | 1bb3c72 | 2014-10-02 12:30:03 -0500 | [diff] [blame] | 54 | /* XXX I couldn't get my Kconfig file to be noticed for out-of-tree build */ |
| 55 | #ifndef CONFIG_HOST_DEV_CPORT_ID_MAX |
| 56 | #define CONFIG_HOST_DEV_CPORT_ID_MAX 128 |
| 57 | #endif /* !CONFIG_HOST_DEV_CPORT_ID_MAX */ |
| 58 | |
| 59 | /* Maximum number of CPorts usable by a host device */ |
| 60 | /* XXX This should really be determined by the AP module manifest */ |
| 61 | #define HOST_DEV_CPORT_ID_MAX CONFIG_HOST_DEV_CPORT_ID_MAX |
| 62 | #define CPORT_ID_BAD U16_MAX /* UniPro max id is 4095 */ |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 63 | |
Greg Kroah-Hartman | 199d68d | 2014-08-30 16:20:22 -0700 | [diff] [blame] | 64 | /* For SP1 hardware, we are going to "hardcode" each device to have all logical |
| 65 | * blocks in order to be able to address them as one unified "unit". Then |
| 66 | * higher up layers will then be able to talk to them as one logical block and |
| 67 | * properly know how they are hooked together (i.e. which i2c port is on the |
| 68 | * same module as the gpio pins, etc.) |
| 69 | * |
| 70 | * So, put the "private" data structures here in greybus.h and link to them off |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 71 | * of the "main" gb_module structure. |
Greg Kroah-Hartman | 199d68d | 2014-08-30 16:20:22 -0700 | [diff] [blame] | 72 | */ |
| 73 | |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 74 | struct greybus_host_device; |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 75 | struct svc_msg; |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 76 | |
| 77 | /* Greybus "Host driver" structure, needed by a host controller driver to be |
| 78 | * able to handle both SVC control as well as "real" greybus messages |
| 79 | */ |
| 80 | struct greybus_host_driver { |
| 81 | size_t hd_priv_size; |
| 82 | |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 83 | void *(*message_send)(struct greybus_host_device *hd, u16 dest_cport_id, |
| 84 | struct gb_message *message, gfp_t gfp_mask); |
| 85 | void (*message_cancel)(void *cookie); |
Alex Elder | 0db32a6 | 2014-09-24 05:16:14 -0500 | [diff] [blame] | 86 | int (*submit_svc)(struct svc_msg *svc_msg, |
Greg Kroah-Hartman | f036e05 | 2014-09-19 19:13:33 -0700 | [diff] [blame] | 87 | struct greybus_host_device *hd); |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | struct greybus_host_device { |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 91 | struct kref kref; |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 92 | struct device *parent; |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 93 | const struct greybus_host_driver *driver; |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 94 | |
Greg Kroah-Hartman | 1cd56a8 | 2014-12-19 14:56:36 -0800 | [diff] [blame] | 95 | struct list_head interfaces; |
Alex Elder | 2c43ce4 | 2014-11-17 08:08:44 -0600 | [diff] [blame] | 96 | struct list_head connections; |
Alex Elder | 177404b | 2014-10-03 14:14:24 -0500 | [diff] [blame] | 97 | struct ida cport_id_map; |
Matt Porter | 98f4ab2 | 2014-10-21 01:52:27 -0400 | [diff] [blame] | 98 | u8 device_id; |
Alex Elder | 1bb3c72 | 2014-10-02 12:30:03 -0500 | [diff] [blame] | 99 | |
Alex Elder | 8b33730 | 2014-11-20 16:09:13 -0600 | [diff] [blame] | 100 | /* Host device buffer constraints */ |
Alex Elder | 8b33730 | 2014-11-20 16:09:13 -0600 | [diff] [blame] | 101 | size_t buffer_size_max; |
| 102 | |
Greg Kroah-Hartman | a4d9150 | 2015-04-07 20:27:15 +0200 | [diff] [blame] | 103 | struct gb_endo *endo; |
| 104 | |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 105 | /* Private data for the host driver */ |
Greg Kroah-Hartman | 8faa8da | 2014-10-06 20:34:48 -0700 | [diff] [blame] | 106 | unsigned long hd_priv[0] __aligned(sizeof(s64)); |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Greg Kroah-Hartman | 8faa8da | 2014-10-06 20:34:48 -0700 | [diff] [blame] | 109 | struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd, |
Johan Hovold | d933667 | 2015-05-19 11:22:43 +0200 | [diff] [blame] | 110 | struct device *parent, |
| 111 | size_t buffer_size_max); |
Alex Elder | eb765e4 | 2015-05-22 12:56:49 -0500 | [diff] [blame] | 112 | int greybus_endo_setup(struct greybus_host_device *hd, u16 endo_id, |
| 113 | u8 ap_intf_id); |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 114 | void greybus_remove_hd(struct greybus_host_device *hd); |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 115 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 116 | struct greybus_driver { |
| 117 | const char *name; |
| 118 | |
Viresh Kumar | 9f5f30e712 | 2015-04-01 20:32:04 +0530 | [diff] [blame] | 119 | int (*probe)(struct gb_bundle *bundle, |
| 120 | const struct greybus_bundle_id *id); |
| 121 | void (*disconnect)(struct gb_bundle *bundle); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 122 | |
Viresh Kumar | 9f5f30e712 | 2015-04-01 20:32:04 +0530 | [diff] [blame] | 123 | int (*suspend)(struct gb_bundle *bundle, pm_message_t message); |
| 124 | int (*resume)(struct gb_bundle *bundle); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 125 | |
Viresh Kumar | 9f5f30e712 | 2015-04-01 20:32:04 +0530 | [diff] [blame] | 126 | const struct greybus_bundle_id *id_table; |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 127 | |
| 128 | struct device_driver driver; |
| 129 | }; |
| 130 | #define to_greybus_driver(d) container_of(d, struct greybus_driver, driver) |
| 131 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 132 | /* Don't call these directly, use the module_greybus_driver() macro instead */ |
| 133 | int greybus_register_driver(struct greybus_driver *driver, |
| 134 | struct module *module, const char *mod_name); |
Alex Elder | fd1c2e5 | 2015-06-08 12:05:13 -0500 | [diff] [blame^] | 135 | void greybus_deregister_driver(struct greybus_driver *driver); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 136 | |
| 137 | /* define to get proper THIS_MODULE and KBUILD_MODNAME values */ |
| 138 | #define greybus_register(driver) \ |
| 139 | greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME) |
Alex Elder | fd1c2e5 | 2015-06-08 12:05:13 -0500 | [diff] [blame^] | 140 | #define greybus_deregister(driver) \ |
| 141 | greybus_deregister_driver(driver) |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * module_greybus_driver() - Helper macro for registering a Greybus driver |
| 145 | * @__greybus_driver: greybus_driver structure |
| 146 | * |
| 147 | * Helper macro for Greybus drivers to set up proper module init / exit |
| 148 | * functions. Replaces module_init() and module_exit() and keeps people from |
| 149 | * printing pointless things to the kernel log when their driver is loaded. |
| 150 | */ |
| 151 | #define module_greybus_driver(__greybus_driver) \ |
| 152 | module_driver(__greybus_driver, greybus_register, greybus_deregister) |
| 153 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 154 | int greybus_disabled(void); |
| 155 | |
Alex Elder | 51c75fd | 2014-09-26 20:55:35 -0500 | [diff] [blame] | 156 | int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length); |
Greg Kroah-Hartman | 45f3678 | 2014-09-14 11:40:35 -0700 | [diff] [blame] | 157 | int gb_ap_init(void); |
| 158 | void gb_ap_exit(void); |
Greg Kroah-Hartman | 48f7047 | 2015-03-27 11:38:06 +0100 | [diff] [blame] | 159 | void gb_debugfs_init(void); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 160 | void gb_debugfs_cleanup(void); |
Alexandre Bailon | e8f824b | 2015-03-18 15:42:51 +0100 | [diff] [blame] | 161 | struct dentry *gb_debugfs_get(void); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 162 | |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 163 | extern struct bus_type greybus_bus_type; |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 164 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 165 | int svc_set_route_send(struct gb_bundle *bundle, |
Alex Elder | 525f146 | 2014-10-22 02:04:31 -0500 | [diff] [blame] | 166 | struct greybus_host_device *hd); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 167 | |
Greg Kroah-Hartman | 0f035ac | 2015-04-07 20:26:53 +0200 | [diff] [blame] | 168 | extern struct device_type greybus_endo_type; |
Greg Kroah-Hartman | df67155 | 2014-12-21 14:10:26 -0800 | [diff] [blame] | 169 | extern struct device_type greybus_module_type; |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 170 | extern struct device_type greybus_interface_type; |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 171 | extern struct device_type greybus_bundle_type; |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 172 | extern struct device_type greybus_connection_type; |
| 173 | |
Greg Kroah-Hartman | 0f035ac | 2015-04-07 20:26:53 +0200 | [diff] [blame] | 174 | static inline int is_gb_endo(const struct device *dev) |
| 175 | { |
| 176 | return dev->type == &greybus_endo_type; |
| 177 | } |
| 178 | |
Greg Kroah-Hartman | df67155 | 2014-12-21 14:10:26 -0800 | [diff] [blame] | 179 | static inline int is_gb_module(const struct device *dev) |
| 180 | { |
| 181 | return dev->type == &greybus_module_type; |
| 182 | } |
| 183 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 184 | static inline int is_gb_interface(const struct device *dev) |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 185 | { |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 186 | return dev->type == &greybus_interface_type; |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 189 | static inline int is_gb_bundle(const struct device *dev) |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 190 | { |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 191 | return dev->type == &greybus_bundle_type; |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static inline int is_gb_connection(const struct device *dev) |
| 195 | { |
| 196 | return dev->type == &greybus_connection_type; |
| 197 | } |
| 198 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 199 | #endif /* __KERNEL__ */ |
| 200 | #endif /* __LINUX_GREYBUS_H */ |