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, |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 64 | int payload_length, struct greybus_host_device *hd) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 65 | { |
| 66 | struct svc_msg *svc_msg; |
| 67 | |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 68 | if (payload_length != sizeof(struct svc_function_handshake)) { |
| 69 | dev_err(hd->parent, |
| 70 | "Illegal size of svc handshake message %d\n", |
| 71 | payload_length); |
| 72 | return; |
| 73 | } |
| 74 | |
Matt Porter | e94e171 | 2014-09-18 15:25:42 -0400 | [diff] [blame] | 75 | /* A new SVC communication channel, let's verify a supported version */ |
| 76 | if ((handshake->version_major != GREYBUS_VERSION_MAJOR) && |
| 77 | (handshake->version_minor != GREYBUS_VERSION_MINOR)) { |
| 78 | dev_dbg(hd->parent, "received invalid greybus version %d:%d\n", |
| 79 | handshake->version_major, handshake->version_minor); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | /* Validate that the handshake came from the SVC */ |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 84 | if (handshake->handshake_type != SVC_HANDSHAKE_SVC_HELLO) { |
| 85 | /* 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] | 86 | dev_dbg(hd->parent, "received invalid handshake type %d\n", |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 87 | handshake->handshake_type); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | /* Send back a AP_HELLO message */ |
| 92 | svc_msg = svc_msg_alloc(SVC_FUNCTION_HANDSHAKE); |
| 93 | if (!svc_msg) |
| 94 | return; |
| 95 | |
Matt Porter | be5064c | 2014-09-22 15:51:49 -0400 | [diff] [blame^] | 96 | svc_msg->header.function_id = SVC_FUNCTION_HANDSHAKE; |
| 97 | svc_msg->header.message_type = SVC_MSG_DATA; |
| 98 | svc_msg->header.payload_length = |
| 99 | cpu_to_le16(sizeof(struct svc_function_handshake)); |
| 100 | svc_msg->handshake.version_major = GREYBUS_VERSION_MAJOR; |
| 101 | svc_msg->handshake.version_minor = GREYBUS_VERSION_MINOR; |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 102 | svc_msg->handshake.handshake_type = SVC_HANDSHAKE_AP_HELLO; |
| 103 | svc_msg_send(svc_msg, hd); |
| 104 | } |
| 105 | |
| 106 | static void svc_management(struct svc_function_unipro_management *management, |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 107 | int payload_length, struct greybus_host_device *hd) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 108 | { |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 109 | if (payload_length != sizeof(struct svc_function_unipro_management)) { |
| 110 | dev_err(hd->parent, |
| 111 | "Illegal size of svc management message %d\n", |
| 112 | payload_length); |
| 113 | return; |
| 114 | } |
| 115 | |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 116 | /* What? An AP should not get this message */ |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 117 | dev_err(hd->parent, "Got an svc management message???\n"); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void svc_hotplug(struct svc_function_hotplug *hotplug, |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 121 | int payload_length, struct greybus_host_device *hd) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 122 | { |
| 123 | u8 module_id = hotplug->module_id; |
| 124 | |
| 125 | switch (hotplug->hotplug_event) { |
| 126 | case SVC_HOTPLUG_EVENT: |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 127 | /* Add a new module to the system */ |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 128 | if (payload_length < 0x03) { |
| 129 | /* Hotplug message is at lest 3 bytes big */ |
| 130 | dev_err(hd->parent, |
| 131 | "Illegal size of svc hotplug message %d\n", |
| 132 | payload_length); |
| 133 | return; |
| 134 | } |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 135 | dev_dbg(hd->parent, "module id %d added\n", module_id); |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 136 | gb_add_module(hd, module_id, hotplug->data, |
| 137 | payload_length - 0x02); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 138 | break; |
| 139 | |
| 140 | case SVC_HOTUNPLUG_EVENT: |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 141 | /* Remove a module from the system */ |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 142 | if (payload_length != 0x02) { |
| 143 | /* Hotunplug message is only 2 bytes big */ |
| 144 | dev_err(hd->parent, |
| 145 | "Illegal size of svc hotunplug message %d\n", |
| 146 | payload_length); |
| 147 | return; |
| 148 | } |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 149 | dev_dbg(hd->parent, "module id %d removed\n", module_id); |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 150 | gb_remove_module(hd, module_id); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 151 | break; |
| 152 | |
| 153 | default: |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 154 | dev_err(hd->parent, |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 155 | "Received invalid hotplug message type %d\n", |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 156 | hotplug->hotplug_event); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static void svc_ddb(struct svc_function_ddb *ddb, |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 162 | int payload_length, struct greybus_host_device *hd) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 163 | { |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 164 | /* |
| 165 | * Need to properly validate payload_length once we start |
| 166 | * to handle ddb messages, but for now, we don't, so no need to check |
| 167 | * anything. |
| 168 | */ |
| 169 | |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 170 | /* What? An AP should not get this message */ |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 171 | dev_err(hd->parent, "Got an svc DDB message???\n"); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void svc_power(struct svc_function_power *power, |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 175 | int payload_length, struct greybus_host_device *hd) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 176 | { |
| 177 | u8 module_id = power->module_id; |
| 178 | |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 179 | /* |
| 180 | * The AP is only allowed to get a Battery Status message, not a Battery |
| 181 | * Status Request |
| 182 | */ |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 183 | if (power->power_type != SVC_POWER_BATTERY_STATUS) { |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 184 | dev_err(hd->parent, "Received invalid power type %d\n", |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 185 | power->power_type); |
| 186 | return; |
| 187 | } |
| 188 | |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 189 | /* |
| 190 | * As struct struct svc_function_power_battery_status_request is 0 bytes |
| 191 | * big, we can just check the union of the whole structure to validate |
| 192 | * the size of this message. |
| 193 | */ |
| 194 | if (payload_length != sizeof(struct svc_function_power)) { |
| 195 | dev_err(hd->parent, |
| 196 | "Illegal size of svc power message %d\n", |
| 197 | payload_length); |
| 198 | return; |
| 199 | } |
| 200 | |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 201 | 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] | 202 | module_id, power->status.status); |
| 203 | |
| 204 | // FIXME - do something with the power information, like update our |
| 205 | // battery information... |
| 206 | } |
| 207 | |
| 208 | static void svc_epm(struct svc_function_epm *epm, |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 209 | int payload_length, struct greybus_host_device *hd) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 210 | { |
| 211 | /* What? An AP should not get this message */ |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 212 | dev_err(hd->parent, "Got an EPM message???\n"); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void svc_suspend(struct svc_function_suspend *suspend, |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 216 | int payload_length, struct greybus_host_device *hd) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 217 | { |
| 218 | /* What? An AP should not get this message */ |
Greg Kroah-Hartman | 772149b | 2014-09-14 12:27:28 -0700 | [diff] [blame] | 219 | dev_err(hd->parent, "Got an suspend message???\n"); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 220 | } |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 221 | |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 222 | static struct svc_msg *convert_ap_message(struct ap_msg *ap_msg, |
| 223 | struct greybus_host_device *hd) |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 224 | { |
| 225 | struct svc_msg *svc_msg; |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 226 | struct svc_msg_header *header; |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 227 | |
| 228 | svc_msg = (struct svc_msg *)ap_msg->data; |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 229 | header = &svc_msg->header; |
| 230 | |
| 231 | /* Validate the message type */ |
| 232 | if (header->message_type != SVC_MSG_DATA) { |
| 233 | dev_err(hd->parent, "message type %d received?\n", |
| 234 | header->message_type); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * The validation of the size of the message buffer happens in each |
| 240 | * svc_* function, due to the different types of messages, keeping the |
| 241 | * logic for each message only in one place. |
| 242 | */ |
Greg Kroah-Hartman | 43cc32a | 2014-09-07 13:51:12 -0700 | [diff] [blame] | 243 | |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 244 | return svc_msg; |
| 245 | } |
| 246 | |
Greg Kroah-Hartman | b57b062 | 2014-09-13 12:18:09 -0700 | [diff] [blame] | 247 | static void ap_process_event(struct work_struct *work) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 248 | { |
| 249 | struct svc_msg *svc_msg; |
| 250 | struct greybus_host_device *hd; |
Greg Kroah-Hartman | b57b062 | 2014-09-13 12:18:09 -0700 | [diff] [blame] | 251 | struct ap_msg *ap_msg; |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 252 | int payload_length; |
Greg Kroah-Hartman | b57b062 | 2014-09-13 12:18:09 -0700 | [diff] [blame] | 253 | |
| 254 | ap_msg = container_of(work, struct ap_msg, event); |
| 255 | hd = ap_msg->hd; |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 256 | |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 257 | /* Turn the "raw" data into a real message */ |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 258 | svc_msg = convert_ap_message(ap_msg, hd); |
| 259 | if (!svc_msg) |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 260 | return; |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 261 | |
| 262 | payload_length = le16_to_cpu(svc_msg->header.payload_length); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 263 | |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 264 | /* Look at the message to figure out what to do with it */ |
Matt Porter | 710ecb0 | 2014-09-18 15:25:41 -0400 | [diff] [blame] | 265 | switch (svc_msg->header.function_id) { |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 266 | case SVC_FUNCTION_HANDSHAKE: |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 267 | svc_handshake(&svc_msg->handshake, payload_length, hd); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 268 | break; |
| 269 | case SVC_FUNCTION_UNIPRO_NETWORK_MANAGEMENT: |
Greg Kroah-Hartman | 00c52e4 | 2014-09-21 18:19:54 -0700 | [diff] [blame] | 270 | svc_management(&svc_msg->management, payload_length, hd); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 271 | break; |
| 272 | case SVC_FUNCTION_HOTPLUG: |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 273 | svc_hotplug(&svc_msg->hotplug, payload_length, hd); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 274 | break; |
| 275 | case SVC_FUNCTION_DDB: |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 276 | svc_ddb(&svc_msg->ddb, payload_length, hd); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 277 | break; |
| 278 | case SVC_FUNCTION_POWER: |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 279 | svc_power(&svc_msg->power, payload_length, hd); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 280 | break; |
| 281 | case SVC_FUNCTION_EPM: |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 282 | svc_epm(&svc_msg->epm, payload_length, hd); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 283 | break; |
| 284 | case SVC_FUNCTION_SUSPEND: |
Greg Kroah-Hartman | d0cfd10 | 2014-09-21 19:10:39 -0700 | [diff] [blame] | 285 | svc_suspend(&svc_msg->suspend, payload_length, hd); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 286 | break; |
| 287 | default: |
Matt Porter | 710ecb0 | 2014-09-18 15:25:41 -0400 | [diff] [blame] | 288 | dev_err(hd->parent, "received invalid SVC function ID %d\n", |
| 289 | svc_msg->header.function_id); |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 292 | /* clean the message up */ |
| 293 | kfree(ap_msg->data); |
| 294 | kfree(ap_msg); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 297 | 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] | 298 | { |
| 299 | struct ap_msg *ap_msg; |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * Totally naive copy the message into a new structure that we slowly |
| 303 | * create and add it to the list. Let's get this working, the odds of |
| 304 | * this being any "slow path" for AP messages is really low at this |
| 305 | * point in time, but you never know, so this comment is here to point |
| 306 | * out that maybe we should use a slab allocator, or even just not copy |
| 307 | * the data, but use it directly and force the urbs to be "new" each |
| 308 | * time. |
| 309 | */ |
| 310 | |
| 311 | /* Note - this can, and will, be called in interrupt context. */ |
| 312 | ap_msg = kmalloc(sizeof(*ap_msg), GFP_ATOMIC); |
| 313 | if (!ap_msg) |
| 314 | return -ENOMEM; |
| 315 | ap_msg->data = kmalloc(size, GFP_ATOMIC); |
| 316 | if (!ap_msg->data) { |
| 317 | kfree(ap_msg); |
| 318 | return -ENOMEM; |
| 319 | } |
| 320 | memcpy(ap_msg->data, data, size); |
| 321 | ap_msg->size = size; |
Greg Kroah-Hartman | 68f1fc4 | 2014-09-07 13:12:11 -0700 | [diff] [blame] | 322 | ap_msg->hd = hd; |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 323 | |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 324 | INIT_WORK(&ap_msg->event, ap_process_event); |
| 325 | queue_work(ap_workqueue, &ap_msg->event); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
Greg Kroah-Hartman | d94a44a | 2014-09-01 14:39:34 -0700 | [diff] [blame] | 329 | EXPORT_SYMBOL_GPL(gb_new_ap_msg); |
| 330 | |
Greg Kroah-Hartman | 45f3678 | 2014-09-14 11:40:35 -0700 | [diff] [blame] | 331 | int gb_ap_init(void) |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 332 | { |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 333 | ap_workqueue = alloc_workqueue("greybus_ap", 0, 1); |
| 334 | if (!ap_workqueue) |
| 335 | return -ENOMEM; |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Greg Kroah-Hartman | 45f3678 | 2014-09-14 11:40:35 -0700 | [diff] [blame] | 340 | void gb_ap_exit(void) |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 341 | { |
Greg Kroah-Hartman | 88929c5 | 2014-09-13 11:35:02 -0700 | [diff] [blame] | 342 | destroy_workqueue(ap_workqueue); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |