blob: f92066625c1e0cb2a7a83f91b537dddfa84073ac [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>
17#include <linux/kthread.h>
18#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
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070032static 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
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 */
71 dev_dbg(&hd->dev, "received invalid handshake type %d\n",
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
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 */
89 dev_err(&hd->dev, "Got an svc management message???\n");
90}
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:
99 dev_dbg(&hd->dev, "module id %d added\n", module_id);
100 // FIXME - add the module to the system
101 break;
102
103 case SVC_HOTUNPLUG_EVENT:
104 dev_dbg(&hd->dev, "module id %d removed\n", module_id);
105 // FIXME - remove the module from the system
106 break;
107
108 default:
109 dev_err(&hd->dev, "received invalid hotplug message type %d\n",
110 hotplug->hotplug_event);
111 break;
112 }
113}
114
115static void svc_ddb(struct svc_function_ddb *ddb,
116 struct greybus_host_device *hd)
117{
118 /* What? An AP should not get this message */
119 dev_err(&hd->dev, "Got an svc DDB message???\n");
120}
121
122static void svc_power(struct svc_function_power *power,
123 struct greybus_host_device *hd)
124{
125 u8 module_id = power->module_id;
126
127 if (power->power_type != SVC_POWER_BATTERY_STATUS) {
128 dev_err(&hd->dev, "received invalid power type %d\n",
129 power->power_type);
130 return;
131 }
132
133 dev_dbg(&hd->dev, "power status for module id %d is %d\n",
134 module_id, power->status.status);
135
136 // FIXME - do something with the power information, like update our
137 // battery information...
138}
139
140static void svc_epm(struct svc_function_epm *epm,
141 struct greybus_host_device *hd)
142{
143 /* What? An AP should not get this message */
144 dev_err(&hd->dev, "Got an EPM message???\n");
145}
146
147static void svc_suspend(struct svc_function_suspend *suspend,
148 struct greybus_host_device *hd)
149{
150 /* What? An AP should not get this message */
151 dev_err(&hd->dev, "Got an suspend message???\n");
152}
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700153
154static struct svc_msg *convert_ap_message(struct ap_msg *ap_msg)
155{
156 struct svc_msg *svc_msg;
157
158 // FIXME - validate message, right now we are trusting the size and data
159 // from the AP, what could go wrong? :)
160 // for now, just cast the pointer and run away...
161
162 svc_msg = (struct svc_msg *)ap_msg->data;
Greg Kroah-Hartman43cc32a2014-09-07 13:51:12 -0700163
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700164 /* Verify the version is something we can handle with this code */
165 if ((svc_msg->header.version_major != GREYBUS_VERSION_MAJOR) &&
166 (svc_msg->header.version_minor != GREYBUS_VERSION_MINOR))
Greg Kroah-Hartman43cc32a2014-09-07 13:51:12 -0700167 return NULL;
168
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700169 return svc_msg;
170}
171
Greg Kroah-Hartmanb57b0622014-09-13 12:18:09 -0700172static void ap_process_event(struct work_struct *work)
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700173{
174 struct svc_msg *svc_msg;
175 struct greybus_host_device *hd;
Greg Kroah-Hartmanb57b0622014-09-13 12:18:09 -0700176 struct ap_msg *ap_msg;
177
178 ap_msg = container_of(work, struct ap_msg, event);
179 hd = ap_msg->hd;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700180
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700181 /* Turn the "raw" data into a real message */
182 svc_msg = convert_ap_message(ap_msg);
183 if (!svc_msg) {
184 // FIXME log an error???
185 return;
186 }
187
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700188 /* Look at the message to figure out what to do with it */
189 switch (svc_msg->header.type) {
190 case SVC_FUNCTION_HANDSHAKE:
191 svc_handshake(&svc_msg->handshake, hd);
192 break;
193 case SVC_FUNCTION_UNIPRO_NETWORK_MANAGEMENT:
194 svc_management(&svc_msg->management, hd);
195 break;
196 case SVC_FUNCTION_HOTPLUG:
197 svc_hotplug(&svc_msg->hotplug, hd);
198 break;
199 case SVC_FUNCTION_DDB:
200 svc_ddb(&svc_msg->ddb, hd);
201 break;
202 case SVC_FUNCTION_POWER:
203 svc_power(&svc_msg->power, hd);
204 break;
205 case SVC_FUNCTION_EPM:
206 svc_epm(&svc_msg->epm, hd);
207 break;
208 case SVC_FUNCTION_SUSPEND:
209 svc_suspend(&svc_msg->suspend, hd);
210 break;
211 default:
212 dev_err(&hd->dev, "received invalid SVC message type %d\n",
213 svc_msg->header.type);
214 }
215
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700216 /* clean the message up */
217 kfree(ap_msg->data);
218 kfree(ap_msg);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700219}
220
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700221int gb_new_ap_msg(u8 *data, int size, struct greybus_host_device *hd)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700222{
223 struct ap_msg *ap_msg;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700224
225 /*
226 * Totally naive copy the message into a new structure that we slowly
227 * create and add it to the list. Let's get this working, the odds of
228 * this being any "slow path" for AP messages is really low at this
229 * point in time, but you never know, so this comment is here to point
230 * out that maybe we should use a slab allocator, or even just not copy
231 * the data, but use it directly and force the urbs to be "new" each
232 * time.
233 */
234
235 /* Note - this can, and will, be called in interrupt context. */
236 ap_msg = kmalloc(sizeof(*ap_msg), GFP_ATOMIC);
237 if (!ap_msg)
238 return -ENOMEM;
239 ap_msg->data = kmalloc(size, GFP_ATOMIC);
240 if (!ap_msg->data) {
241 kfree(ap_msg);
242 return -ENOMEM;
243 }
244 memcpy(ap_msg->data, data, size);
245 ap_msg->size = size;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700246 ap_msg->hd = hd;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700247
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700248 INIT_WORK(&ap_msg->event, ap_process_event);
249 queue_work(ap_workqueue, &ap_msg->event);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700250
251 return 0;
252}
Greg Kroah-Hartmand94a44a2014-09-01 14:39:34 -0700253EXPORT_SYMBOL_GPL(gb_new_ap_msg);
254
Greg Kroah-Hartman9c8d3af2014-09-13 11:09:35 -0700255void greybus_cport_in_data(struct greybus_host_device *hd, int cport, u8 *data,
256 size_t length)
257{
258 // FIXME - implement...
259}
260EXPORT_SYMBOL_GPL(greybus_cport_in_data);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700261
262int gb_thread_init(void)
263{
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700264 ap_workqueue = alloc_workqueue("greybus_ap", 0, 1);
265 if (!ap_workqueue)
266 return -ENOMEM;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700267
268 return 0;
269}
270
271void gb_thread_destroy(void)
272{
Greg Kroah-Hartman88929c52014-09-13 11:35:02 -0700273 destroy_workqueue(ap_workqueue);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700274}
275
276