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