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. |
| 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 Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 15 | #include <linux/types.h> |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 16 | #include <linux/list.h> |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 17 | #include <linux/slab.h> |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 18 | #include <linux/device.h> |
| 19 | #include <linux/module.h> |
Alex Elder | 177404b | 2014-10-03 14:14:24 -0500 | [diff] [blame] | 20 | #include <linux/idr.h> |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 21 | |
Alex Elder | 1bb3c72 | 2014-10-02 12:30:03 -0500 | [diff] [blame] | 22 | #include "kernel_ver.h" |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 23 | #include "greybus_id.h" |
Alex Elder | 05ad189 | 2014-09-09 13:55:03 -0500 | [diff] [blame] | 24 | #include "greybus_manifest.h" |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 25 | #include "manifest.h" |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 26 | #include "module.h" |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 27 | #include "interface.h" |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 28 | #include "connection.h" |
Alex Elder | 4ccb6b7 | 2014-10-28 19:36:00 -0500 | [diff] [blame] | 29 | #include "protocol.h" |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 30 | #include "operation.h" |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 31 | |
| 32 | |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 33 | /* Matches up with the Greybus Protocol specification document */ |
Matt Porter | 52adb56 | 2014-09-18 15:25:43 -0400 | [diff] [blame] | 34 | #define GREYBUS_VERSION_MAJOR 0x00 |
| 35 | #define GREYBUS_VERSION_MINOR 0x01 |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 36 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 37 | #define GREYBUS_DEVICE_ID_MATCH_DEVICE \ |
Greg Kroah-Hartman | 8faa8da | 2014-10-06 20:34:48 -0700 | [diff] [blame] | 38 | (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT) |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 39 | |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 40 | #define GREYBUS_DEVICE(v, p) \ |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 41 | .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \ |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 42 | .vendor = (v), \ |
| 43 | .product = (p), |
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_SERIAL(s) \ |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 46 | .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \ |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 47 | .serial_number = (s), |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 48 | |
Alex Elder | 1bb3c72 | 2014-10-02 12:30:03 -0500 | [diff] [blame] | 49 | /* 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-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 58 | |
Greg Kroah-Hartman | 199d68d | 2014-08-30 16:20:22 -0700 | [diff] [blame] | 59 | /* 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 Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 66 | * of the "main" gb_module structure. |
Greg Kroah-Hartman | 199d68d | 2014-08-30 16:20:22 -0700 | [diff] [blame] | 67 | */ |
| 68 | |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 69 | struct greybus_host_device; |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 70 | struct svc_msg; |
Alex Elder | 3c3cef4 | 2014-11-17 18:08:30 -0600 | [diff] [blame] | 71 | struct gbuf; |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 72 | |
Alex Elder | 06a4a06 | 2014-11-18 13:26:40 -0600 | [diff] [blame] | 73 | /* Buffers allocated from the host driver will be aligned to this multiple */ |
| 74 | #define GB_BUFFER_ALIGN sizeof(u32) |
| 75 | |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 76 | /* 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 | */ |
| 79 | struct greybus_host_driver { |
| 80 | size_t hd_priv_size; |
| 81 | |
Alex Elder | d2a259f | 2014-11-18 13:26:42 -0600 | [diff] [blame] | 82 | void *(*buffer_alloc)(unsigned int size, gfp_t gfp_mask); |
Alex Elder | 9ec5411 | 2014-11-18 13:26:43 -0600 | [diff] [blame] | 83 | void (*buffer_free)(void *buffer); |
Alex Elder | a9163b2 | 2014-11-18 13:26:44 -0600 | [diff] [blame^] | 84 | int (*submit_gbuf)(struct gbuf *gbuf, gfp_t gfp_mask); |
| 85 | void (*buffer_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 | |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 95 | struct list_head modules; |
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 | |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 100 | /* Private data for the host driver */ |
Greg Kroah-Hartman | 8faa8da | 2014-10-06 20:34:48 -0700 | [diff] [blame] | 101 | unsigned long hd_priv[0] __aligned(sizeof(s64)); |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Greg Kroah-Hartman | 8faa8da | 2014-10-06 20:34:48 -0700 | [diff] [blame] | 104 | 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] | 105 | struct device *parent); |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 106 | void greybus_remove_hd(struct greybus_host_device *hd); |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 107 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 108 | struct greybus_driver { |
| 109 | const char *name; |
| 110 | |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 111 | int (*probe)(struct gb_module *gmod, |
Greg Kroah-Hartman | 6584c8a | 2014-09-01 13:31:31 -0700 | [diff] [blame] | 112 | const struct greybus_module_id *id); |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 113 | void (*disconnect)(struct gb_module *gmod); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 114 | |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 115 | int (*suspend)(struct gb_module *gmod, pm_message_t message); |
| 116 | int (*resume)(struct gb_module *gmod); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 117 | |
Greg Kroah-Hartman | 6584c8a | 2014-09-01 13:31:31 -0700 | [diff] [blame] | 118 | const struct greybus_module_id *id_table; |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 119 | |
| 120 | struct device_driver driver; |
| 121 | }; |
| 122 | #define to_greybus_driver(d) container_of(d, struct greybus_driver, driver) |
| 123 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 124 | /* Don't call these directly, use the module_greybus_driver() macro instead */ |
| 125 | int greybus_register_driver(struct greybus_driver *driver, |
| 126 | struct module *module, const char *mod_name); |
| 127 | void 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-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 144 | int greybus_disabled(void); |
| 145 | |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 146 | /* Internal functions to gb module, move to internal .h file eventually. */ |
| 147 | |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 148 | void gb_add_module(struct greybus_host_device *hd, u8 module_id, |
| 149 | u8 *data, int size); |
Greg Kroah-Hartman | 85e0066 | 2014-09-21 18:17:12 -0700 | [diff] [blame] | 150 | void gb_remove_module(struct greybus_host_device *hd, u8 module_id); |
Viresh Kumar | 676daaf | 2014-11-14 17:25:07 +0530 | [diff] [blame] | 151 | void gb_remove_modules(struct greybus_host_device *hd); |
Greg Kroah-Hartman | 85e0066 | 2014-09-21 18:17:12 -0700 | [diff] [blame] | 152 | |
Alex Elder | 51c75fd | 2014-09-26 20:55:35 -0500 | [diff] [blame] | 153 | 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] | 154 | int gb_ap_init(void); |
| 155 | void gb_ap_exit(void); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 156 | int gb_debugfs_init(void); |
| 157 | void gb_debugfs_cleanup(void); |
Greg Kroah-Hartman | 45f3678 | 2014-09-14 11:40:35 -0700 | [diff] [blame] | 158 | int gb_gbuf_init(void); |
| 159 | void gb_gbuf_exit(void); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 160 | |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 161 | extern struct bus_type greybus_bus_type; |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 162 | extern const struct attribute_group *greybus_module_groups[]; |
| 163 | |
Greg Kroah-Hartman | aed0bc6 | 2014-10-27 17:32:34 +0800 | [diff] [blame] | 164 | int gb_uart_device_init(struct gb_connection *connection); |
| 165 | void gb_uart_device_exit(struct gb_connection *connection); |
| 166 | |
Alex Elder | 525f146 | 2014-10-22 02:04:31 -0500 | [diff] [blame] | 167 | int svc_set_route_send(struct gb_interface *interface, |
| 168 | struct greybus_host_device *hd); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 169 | |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 170 | extern struct device_type greybus_module_type; |
| 171 | extern struct device_type greybus_interface_type; |
| 172 | extern struct device_type greybus_connection_type; |
| 173 | |
| 174 | static inline int is_gb_module(const struct device *dev) |
| 175 | { |
| 176 | return dev->type == &greybus_module_type; |
| 177 | } |
| 178 | |
| 179 | static inline int is_gb_interface(const struct device *dev) |
| 180 | { |
| 181 | return dev->type == &greybus_interface_type; |
| 182 | } |
| 183 | |
| 184 | static inline int is_gb_connection(const struct device *dev) |
| 185 | { |
| 186 | return dev->type == &greybus_connection_type; |
| 187 | } |
| 188 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 189 | #endif /* __KERNEL__ */ |
| 190 | #endif /* __LINUX_GREYBUS_H */ |