Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Greybus "AP" message loop handling |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * |
| 6 | * Released under the GPLv2 only. |
| 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/uaccess.h> |
Greg Kroah-Hartman | 45f3678 | 2014-09-14 11:40:35 -0700 | [diff] [blame] | 17 | #include <linux/workqueue.h> |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 18 | #include <linux/device.h> |
Greg Kroah-Hartman | b9b2a46 | 2014-08-31 17:43:38 -0700 | [diff] [blame] | 19 | #include "svc_msg.h" |
Alex Elder | 05ad189 | 2014-09-09 13:55:03 -0500 | [diff] [blame] | 20 | #include "greybus_manifest.h" |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 21 | #include "greybus.h" |
| 22 | |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 23 | struct ap_msg { |
| 24 | u8 *data; |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 25 | size_t size; |
| 26 | struct greybus_host_device *hd; |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 27 | struct work_struct event; |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 30 | static struct workqueue_struct *ap_workqueue; |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 31 | |
Matt Porter | 710ecb0 | 2014-09-18 15:25:41 -0400 | [diff] [blame] | 32 | static struct svc_msg *svc_msg_alloc(enum svc_function_id id) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 33 | { |
| 34 | struct svc_msg *svc_msg; |
| 35 | |
| 36 | svc_msg = kzalloc((sizeof *svc_msg), GFP_KERNEL); |
| 37 | if (!svc_msg) |
| 38 | return NULL; |
| 39 | |
Matt Porter | 710ecb0 | 2014-09-18 15:25:41 -0400 | [diff] [blame] | 40 | // FIXME - verify we are only sending function IDs we should be |
| 41 | svc_msg->header.function_id = id; |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 42 | return svc_msg; |
| 43 | } |
| 44 | |
| 45 | static void svc_msg_free(struct svc_msg *svc_msg) |
| 46 | { |
| 47 | kfree(svc_msg); |
| 48 | } |
| 49 | |
| 50 | static int svc_msg_send(struct svc_msg *svc_msg, struct greybus_host_device *hd) |
| 51 | { |
| 52 | int retval; |
| 53 | |
| 54 | // FIXME - Do we need to do more than just pass it to the hd and then |
| 55 | // free it? |
| 56 | retval = hd->driver->send_svc_msg(svc_msg, hd); |
| 57 | |
| 58 | svc_msg_free(svc_msg); |
| 59 | return retval; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static void svc_handshake(struct svc_function_handshake *handshake, |
| 64 | struct greybus_host_device *hd) |
| 65 | { |
| 66 | struct svc_msg *svc_msg; |
| 67 | |
Matt Porter | e94e171 | 2014-09-18 15:25:42 -0400 | [diff] [blame] | 68 | /* A new SVC communication channel, let's verify a supported version */ |
| 69 | if ((handshake->version_major != GREYBUS_VERSION_MAJOR) && |
| 70 | (handshake->version_minor != GREYBUS_VERSION_MINOR)) { |
| 71 | dev_dbg(hd->parent, "received invalid greybus version %d:%d\n", |
| 72 | handshake->version_major, handshake->version_minor); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | /* Validate that the handshake came from the SVC */ |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 77 | if (handshake->handshake_type != SVC_HANDSHAKE_SVC_HELLO) { |
| 78 | /* we don't know what to do with this, log it and return */ |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 79 | dev_dbg(hd->parent, "received invalid handshake type %d\n", |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 80 | handshake->handshake_type); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | /* Send back a AP_HELLO message */ |
| 85 | svc_msg = svc_msg_alloc(SVC_FUNCTION_HANDSHAKE); |
| 86 | if (!svc_msg) |
| 87 | return; |
| 88 | |
| 89 | svc_msg->handshake.handshake_type = SVC_HANDSHAKE_AP_HELLO; |
| 90 | svc_msg_send(svc_msg, hd); |
| 91 | } |
| 92 | |
| 93 | static void svc_management(struct svc_function_unipro_management *management, |
| 94 | struct greybus_host_device *hd) |
| 95 | { |
| 96 | /* What? An AP should not get this message */ |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 97 | dev_err(hd->parent, "Got an svc management message???\n"); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void svc_hotplug(struct svc_function_hotplug *hotplug, |
| 101 | struct greybus_host_device *hd) |
| 102 | { |
| 103 | u8 module_id = hotplug->module_id; |
| 104 | |
| 105 | switch (hotplug->hotplug_event) { |
| 106 | case SVC_HOTPLUG_EVENT: |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 107 | dev_dbg(hd->parent, "module id %d added\n", module_id); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 108 | // FIXME - add the module to the system |
| 109 | break; |
| 110 | |
| 111 | case SVC_HOTUNPLUG_EVENT: |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 112 | dev_dbg(hd->parent, "module id %d removed\n", module_id); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 113 | // FIXME - remove the module from the system |
| 114 | break; |
| 115 | |
| 116 | default: |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 117 | dev_err(hd->parent, |
| 118 | "received invalid hotplug message type %d\n", |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 119 | hotplug->hotplug_event); |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static void svc_ddb(struct svc_function_ddb *ddb, |
| 125 | struct greybus_host_device *hd) |
| 126 | { |
| 127 | /* What? An AP should not get this message */ |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 128 | dev_err(hd->parent, "Got an svc DDB message???\n"); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void svc_power(struct svc_function_power *power, |
| 132 | struct greybus_host_device *hd) |
| 133 | { |
| 134 | u8 module_id = power->module_id; |
| 135 | |
| 136 | if (power->power_type != SVC_POWER_BATTERY_STATUS) { |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 137 | dev_err(hd->parent, "received invalid power type %d\n", |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 138 | power->power_type); |
| 139 | return; |
| 140 | } |
| 141 | |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 142 | dev_dbg(hd->parent, "power status for module id %d is %d\n", |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 143 | module_id, power->status.status); |
| 144 | |
| 145 | // FIXME - do something with the power information, like update our |
| 146 | // battery information... |
| 147 | } |
| 148 | |
| 149 | static void svc_epm(struct svc_function_epm *epm, |
| 150 | struct greybus_host_device *hd) |
| 151 | { |
| 152 | /* What? An AP should not get this message */ |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 153 | dev_err(hd->parent, "Got an EPM message???\n"); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void svc_suspend(struct svc_function_suspend *suspend, |
| 157 | struct greybus_host_device *hd) |
| 158 | { |
| 159 | /* What? An AP should not get this message */ |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 160 | dev_err(hd->parent, "Got an suspend message???\n"); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 161 | } |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 162 | |
| 163 | static struct svc_msg *convert_ap_message(struct ap_msg *ap_msg) |
| 164 | { |
| 165 | struct svc_msg *svc_msg; |
| 166 | |
| 167 | // FIXME - validate message, right now we are trusting the size and data |
| 168 | // from the AP, what could go wrong? :) |
| 169 | // for now, just cast the pointer and run away... |
| 170 | |
| 171 | svc_msg = (struct svc_msg *)ap_msg->data; |
Greg Kroah-Hartman | 43cc32a | 2014-09-07 13:51:12 -0700 | [diff] [blame] | 172 | |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 173 | return svc_msg; |
| 174 | } |
| 175 | |
Greg Kroah-Hartman | b57b062 | 2014-09-13 12:18:09 -0700 | [diff] [blame] | 176 | static void ap_process_event(struct work_struct *work) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 177 | { |
| 178 | struct svc_msg *svc_msg; |
| 179 | struct greybus_host_device *hd; |
Greg Kroah-Hartman | b57b062 | 2014-09-13 12:18:09 -0700 | [diff] [blame] | 180 | struct ap_msg *ap_msg; |
| 181 | |
| 182 | ap_msg = container_of(work, struct ap_msg, event); |
| 183 | hd = ap_msg->hd; |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 184 | |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 185 | /* Turn the "raw" data into a real message */ |
| 186 | svc_msg = convert_ap_message(ap_msg); |
| 187 | if (!svc_msg) { |
| 188 | // FIXME log an error??? |
| 189 | return; |
| 190 | } |
| 191 | |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 192 | /* Look at the message to figure out what to do with it */ |
Matt Porter | 710ecb0 | 2014-09-18 15:25:41 -0400 | [diff] [blame] | 193 | switch (svc_msg->header.function_id) { |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 194 | case SVC_FUNCTION_HANDSHAKE: |
| 195 | svc_handshake(&svc_msg->handshake, hd); |
| 196 | break; |
| 197 | case SVC_FUNCTION_UNIPRO_NETWORK_MANAGEMENT: |
| 198 | svc_management(&svc_msg->management, hd); |
| 199 | break; |
| 200 | case SVC_FUNCTION_HOTPLUG: |
| 201 | svc_hotplug(&svc_msg->hotplug, hd); |
| 202 | break; |
| 203 | case SVC_FUNCTION_DDB: |
| 204 | svc_ddb(&svc_msg->ddb, hd); |
| 205 | break; |
| 206 | case SVC_FUNCTION_POWER: |
| 207 | svc_power(&svc_msg->power, hd); |
| 208 | break; |
| 209 | case SVC_FUNCTION_EPM: |
| 210 | svc_epm(&svc_msg->epm, hd); |
| 211 | break; |
| 212 | case SVC_FUNCTION_SUSPEND: |
| 213 | svc_suspend(&svc_msg->suspend, hd); |
| 214 | break; |
| 215 | default: |
Matt Porter | 710ecb0 | 2014-09-18 15:25:41 -0400 | [diff] [blame] | 216 | dev_err(hd->parent, "received invalid SVC function ID %d\n", |
| 217 | svc_msg->header.function_id); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 220 | /* clean the message up */ |
| 221 | kfree(ap_msg->data); |
| 222 | kfree(ap_msg); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 225 | int gb_new_ap_msg(u8 *data, int size, struct greybus_host_device *hd) |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 226 | { |
| 227 | struct ap_msg *ap_msg; |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * Totally naive copy the message into a new structure that we slowly |
| 231 | * create and add it to the list. Let's get this working, the odds of |
| 232 | * this being any "slow path" for AP messages is really low at this |
| 233 | * point in time, but you never know, so this comment is here to point |
| 234 | * out that maybe we should use a slab allocator, or even just not copy |
| 235 | * the data, but use it directly and force the urbs to be "new" each |
| 236 | * time. |
| 237 | */ |
| 238 | |
| 239 | /* Note - this can, and will, be called in interrupt context. */ |
| 240 | ap_msg = kmalloc(sizeof(*ap_msg), GFP_ATOMIC); |
| 241 | if (!ap_msg) |
| 242 | return -ENOMEM; |
| 243 | ap_msg->data = kmalloc(size, GFP_ATOMIC); |
| 244 | if (!ap_msg->data) { |
| 245 | kfree(ap_msg); |
| 246 | return -ENOMEM; |
| 247 | } |
| 248 | memcpy(ap_msg->data, data, size); |
| 249 | ap_msg->size = size; |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 250 | ap_msg->hd = hd; |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 251 | |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 252 | INIT_WORK(&ap_msg->event, ap_process_event); |
| 253 | queue_work(ap_workqueue, &ap_msg->event); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
Greg Kroah-Hartman | d94a44a | 2014-09-01 14:39:34 -0700 | [diff] [blame] | 257 | EXPORT_SYMBOL_GPL(gb_new_ap_msg); |
| 258 | |
Greg Kroah-Hartman | 45f3678 | 2014-09-14 11:40:35 -0700 | [diff] [blame] | 259 | int gb_ap_init(void) |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 260 | { |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 261 | ap_workqueue = alloc_workqueue("greybus_ap", 0, 1); |
| 262 | if (!ap_workqueue) |
| 263 | return -ENOMEM; |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Greg Kroah-Hartman | 45f3678 | 2014-09-14 11:40:35 -0700 | [diff] [blame] | 268 | void gb_ap_exit(void) |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 269 | { |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 270 | destroy_workqueue(ap_workqueue); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |