blob: 2a60de0cec89c0368c57741b5c53e34bb6df917a [file] [log] [blame]
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -07001/*
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-Hartman45f36782014-09-14 11:40:35 -070017#include <linux/workqueue.h>
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -070018#include <linux/device.h>
Greg Kroah-Hartmanb9b2a462014-08-31 17:43:38 -070019#include "svc_msg.h"
Alex Elder05ad1892014-09-09 13:55:03 -050020#include "greybus_manifest.h"
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -070021#include "greybus.h"
22
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -070023struct ap_msg {
24 u8 *data;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -070025 size_t size;
26 struct greybus_host_device *hd;
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -070027 struct work_struct event;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -070028};
29
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -070030static struct workqueue_struct *ap_workqueue;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -070031
Matt Porter710ecb02014-09-18 15:25:41 -040032static struct svc_msg *svc_msg_alloc(enum svc_function_id id)
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070033{
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 Porter710ecb02014-09-18 15:25:41 -040040 // FIXME - verify we are only sending function IDs we should be
41 svc_msg->header.function_id = id;
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070042 return svc_msg;
43}
44
45static void svc_msg_free(struct svc_msg *svc_msg)
46{
47 kfree(svc_msg);
48}
49
50static 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
63static void svc_handshake(struct svc_function_handshake *handshake,
64 struct greybus_host_device *hd)
65{
66 struct svc_msg *svc_msg;
67
Matt Portere94e1712014-09-18 15:25:42 -040068 /* 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-Hartman8c53e072014-09-12 20:47:11 -070077 if (handshake->handshake_type != SVC_HANDSHAKE_SVC_HELLO) {
78 /* we don't know what to do with this, log it and return */
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -070079 dev_dbg(hd->parent, "received invalid handshake type %d\n",
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070080 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
93static 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-Hartman772149b2014-09-14 12:27:28 -070097 dev_err(hd->parent, "Got an svc management message???\n");
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070098}
99
100static 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-Hartman772149b2014-09-14 12:27:28 -0700107 dev_dbg(hd->parent, "module id %d added\n", module_id);
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700108 // FIXME - add the module to the system
109 break;
110
111 case SVC_HOTUNPLUG_EVENT:
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700112 dev_dbg(hd->parent, "module id %d removed\n", module_id);
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700113 // FIXME - remove the module from the system
114 break;
115
116 default:
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700117 dev_err(hd->parent,
118 "received invalid hotplug message type %d\n",
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700119 hotplug->hotplug_event);
120 break;
121 }
122}
123
124static 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-Hartman772149b2014-09-14 12:27:28 -0700128 dev_err(hd->parent, "Got an svc DDB message???\n");
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700129}
130
131static 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-Hartman772149b2014-09-14 12:27:28 -0700137 dev_err(hd->parent, "received invalid power type %d\n",
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700138 power->power_type);
139 return;
140 }
141
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700142 dev_dbg(hd->parent, "power status for module id %d is %d\n",
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700143 module_id, power->status.status);
144
145 // FIXME - do something with the power information, like update our
146 // battery information...
147}
148
149static 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-Hartman772149b2014-09-14 12:27:28 -0700153 dev_err(hd->parent, "Got an EPM message???\n");
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700154}
155
156static 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-Hartman772149b2014-09-14 12:27:28 -0700160 dev_err(hd->parent, "Got an suspend message???\n");
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700161}
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700162
163static 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-Hartman43cc32a2014-09-07 13:51:12 -0700172
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700173 return svc_msg;
174}
175
Greg Kroah-Hartmanb57b0622014-09-13 12:18:09 -0700176static void ap_process_event(struct work_struct *work)
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700177{
178 struct svc_msg *svc_msg;
179 struct greybus_host_device *hd;
Greg Kroah-Hartmanb57b0622014-09-13 12:18:09 -0700180 struct ap_msg *ap_msg;
181
182 ap_msg = container_of(work, struct ap_msg, event);
183 hd = ap_msg->hd;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700184
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700185 /* 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-Hartman8c53e072014-09-12 20:47:11 -0700192 /* Look at the message to figure out what to do with it */
Matt Porter710ecb02014-09-18 15:25:41 -0400193 switch (svc_msg->header.function_id) {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700194 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 Porter710ecb02014-09-18 15:25:41 -0400216 dev_err(hd->parent, "received invalid SVC function ID %d\n",
217 svc_msg->header.function_id);
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700218 }
219
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700220 /* clean the message up */
221 kfree(ap_msg->data);
222 kfree(ap_msg);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700223}
224
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700225int gb_new_ap_msg(u8 *data, int size, struct greybus_host_device *hd)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700226{
227 struct ap_msg *ap_msg;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700228
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-Hartman68f1fc42014-09-07 13:12:11 -0700250 ap_msg->hd = hd;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700251
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700252 INIT_WORK(&ap_msg->event, ap_process_event);
253 queue_work(ap_workqueue, &ap_msg->event);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700254
255 return 0;
256}
Greg Kroah-Hartmand94a44a2014-09-01 14:39:34 -0700257EXPORT_SYMBOL_GPL(gb_new_ap_msg);
258
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700259int gb_ap_init(void)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700260{
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700261 ap_workqueue = alloc_workqueue("greybus_ap", 0, 1);
262 if (!ap_workqueue)
263 return -ENOMEM;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700264
265 return 0;
266}
267
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700268void gb_ap_exit(void)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700269{
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700270 destroy_workqueue(ap_workqueue);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700271}
272
273