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