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