blob: c2c5aa654e7ec31bbc1bd65b1f3a4fcd591448de [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
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-Hartman772149b2014-09-14 12:27:28 -070071 dev_dbg(hd->parent, "received invalid handshake type %d\n",
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070072 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
85static 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-Hartman772149b2014-09-14 12:27:28 -070089 dev_err(hd->parent, "Got an svc management message???\n");
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070090}
91
92static 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-Hartman772149b2014-09-14 12:27:28 -070099 dev_dbg(hd->parent, "module id %d added\n", module_id);
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700100 // FIXME - add the module to the system
101 break;
102
103 case SVC_HOTUNPLUG_EVENT:
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700104 dev_dbg(hd->parent, "module id %d removed\n", module_id);
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700105 // FIXME - remove the module from the system
106 break;
107
108 default:
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700109 dev_err(hd->parent,
110 "received invalid hotplug message type %d\n",
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700111 hotplug->hotplug_event);
112 break;
113 }
114}
115
116static 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-Hartman772149b2014-09-14 12:27:28 -0700120 dev_err(hd->parent, "Got an svc DDB message???\n");
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700121}
122
123static 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-Hartman772149b2014-09-14 12:27:28 -0700129 dev_err(hd->parent, "received invalid power type %d\n",
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700130 power->power_type);
131 return;
132 }
133
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700134 dev_dbg(hd->parent, "power status for module id %d is %d\n",
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700135 module_id, power->status.status);
136
137 // FIXME - do something with the power information, like update our
138 // battery information...
139}
140
141static 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-Hartman772149b2014-09-14 12:27:28 -0700145 dev_err(hd->parent, "Got an EPM message???\n");
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700146}
147
148static 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-Hartman772149b2014-09-14 12:27:28 -0700152 dev_err(hd->parent, "Got an suspend message???\n");
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700153}
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700154
155static 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-Hartman43cc32a2014-09-07 13:51:12 -0700164
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700165 /* 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-Hartman43cc32a2014-09-07 13:51:12 -0700168 return NULL;
169
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700170 return svc_msg;
171}
172
Greg Kroah-Hartmanb57b0622014-09-13 12:18:09 -0700173static void ap_process_event(struct work_struct *work)
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700174{
175 struct svc_msg *svc_msg;
176 struct greybus_host_device *hd;
Greg Kroah-Hartmanb57b0622014-09-13 12:18:09 -0700177 struct ap_msg *ap_msg;
178
179 ap_msg = container_of(work, struct ap_msg, event);
180 hd = ap_msg->hd;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700181
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700182 /* 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-Hartman8c53e072014-09-12 20:47:11 -0700189 /* Look at the message to figure out what to do with it */
Matt Porter710ecb02014-09-18 15:25:41 -0400190 switch (svc_msg->header.function_id) {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700191 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:
Matt Porter710ecb02014-09-18 15:25:41 -0400213 dev_err(hd->parent, "received invalid SVC function ID %d\n",
214 svc_msg->header.function_id);
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700215 }
216
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700217 /* clean the message up */
218 kfree(ap_msg->data);
219 kfree(ap_msg);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700220}
221
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700222int gb_new_ap_msg(u8 *data, int size, struct greybus_host_device *hd)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700223{
224 struct ap_msg *ap_msg;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700225
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-Hartman68f1fc42014-09-07 13:12:11 -0700247 ap_msg->hd = hd;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700248
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700249 INIT_WORK(&ap_msg->event, ap_process_event);
250 queue_work(ap_workqueue, &ap_msg->event);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700251
252 return 0;
253}
Greg Kroah-Hartmand94a44a2014-09-01 14:39:34 -0700254EXPORT_SYMBOL_GPL(gb_new_ap_msg);
255
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700256int gb_ap_init(void)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700257{
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700258 ap_workqueue = alloc_workqueue("greybus_ap", 0, 1);
259 if (!ap_workqueue)
260 return -ENOMEM;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700261
262 return 0;
263}
264
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700265void gb_ap_exit(void)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700266{
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700267 destroy_workqueue(ap_workqueue);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700268}
269
270