2 * Greybus "AP" message loop handling
4 * Copyright 2014 Google Inc.
6 * Released under the GPLv2 only.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
20 #include "greybus_manifest.h"
26 struct greybus_host_device *hd;
27 struct list_head list;
30 static LIST_HEAD(ap_msg_list);
31 static spinlock_t ap_msg_list_lock;
32 static struct task_struct *ap_thread;
33 static wait_queue_head_t ap_wait;
35 static struct svc_msg *svc_msg_alloc(enum svc_function_type type)
37 struct svc_msg *svc_msg;
39 svc_msg = kzalloc((sizeof *svc_msg), GFP_KERNEL);
43 // FIXME - verify we are only sending message types we should be
44 svc_msg->header.type = type;
48 static void svc_msg_free(struct svc_msg *svc_msg)
53 static int svc_msg_send(struct svc_msg *svc_msg, struct greybus_host_device *hd)
57 // FIXME - Do we need to do more than just pass it to the hd and then
59 retval = hd->driver->send_svc_msg(svc_msg, hd);
61 svc_msg_free(svc_msg);
66 static void svc_handshake(struct svc_function_handshake *handshake,
67 struct greybus_host_device *hd)
69 struct svc_msg *svc_msg;
71 /* A new SVC communication channel, let's verify it was for us */
72 if (handshake->handshake_type != SVC_HANDSHAKE_SVC_HELLO) {
73 /* we don't know what to do with this, log it and return */
74 dev_dbg(&hd->dev, "received invalid handshake type %d\n",
75 handshake->handshake_type);
79 /* Send back a AP_HELLO message */
80 svc_msg = svc_msg_alloc(SVC_FUNCTION_HANDSHAKE);
84 svc_msg->handshake.handshake_type = SVC_HANDSHAKE_AP_HELLO;
85 svc_msg_send(svc_msg, hd);
88 static void svc_management(struct svc_function_unipro_management *management,
89 struct greybus_host_device *hd)
91 /* What? An AP should not get this message */
92 dev_err(&hd->dev, "Got an svc management message???\n");
95 static void svc_hotplug(struct svc_function_hotplug *hotplug,
96 struct greybus_host_device *hd)
98 u8 module_id = hotplug->module_id;
100 switch (hotplug->hotplug_event) {
101 case SVC_HOTPLUG_EVENT:
102 dev_dbg(&hd->dev, "module id %d added\n", module_id);
103 // FIXME - add the module to the system
106 case SVC_HOTUNPLUG_EVENT:
107 dev_dbg(&hd->dev, "module id %d removed\n", module_id);
108 // FIXME - remove the module from the system
112 dev_err(&hd->dev, "received invalid hotplug message type %d\n",
113 hotplug->hotplug_event);
118 static void svc_ddb(struct svc_function_ddb *ddb,
119 struct greybus_host_device *hd)
121 /* What? An AP should not get this message */
122 dev_err(&hd->dev, "Got an svc DDB message???\n");
125 static void svc_power(struct svc_function_power *power,
126 struct greybus_host_device *hd)
128 u8 module_id = power->module_id;
130 if (power->power_type != SVC_POWER_BATTERY_STATUS) {
131 dev_err(&hd->dev, "received invalid power type %d\n",
136 dev_dbg(&hd->dev, "power status for module id %d is %d\n",
137 module_id, power->status.status);
139 // FIXME - do something with the power information, like update our
140 // battery information...
143 static void svc_epm(struct svc_function_epm *epm,
144 struct greybus_host_device *hd)
146 /* What? An AP should not get this message */
147 dev_err(&hd->dev, "Got an EPM message???\n");
150 static void svc_suspend(struct svc_function_suspend *suspend,
151 struct greybus_host_device *hd)
153 /* What? An AP should not get this message */
154 dev_err(&hd->dev, "Got an suspend message???\n");
157 static struct svc_msg *convert_ap_message(struct ap_msg *ap_msg)
159 struct svc_msg *svc_msg;
161 // FIXME - validate message, right now we are trusting the size and data
162 // from the AP, what could go wrong? :)
163 // for now, just cast the pointer and run away...
165 svc_msg = (struct svc_msg *)ap_msg->data;
167 /* Verify the version is something we can handle with this code */
168 if ((svc_msg->header.version_major != GREYBUS_VERSION_MAJOR) &&
169 (svc_msg->header.version_minor != GREYBUS_VERSION_MINOR))
175 static void process_ap_message(struct ap_msg *ap_msg)
177 struct svc_msg *svc_msg;
178 struct greybus_host_device *hd;
180 /* Turn the "raw" data into a real message */
181 svc_msg = convert_ap_message(ap_msg);
183 // FIXME log an error???
189 /* Pass the message to the host controller */
190 // ap_msg->hd->driver->ap_msg(svc_msg, ap_msg->hd);
192 /* Look at the message to figure out what to do with it */
193 switch (svc_msg->header.type) {
194 case SVC_FUNCTION_HANDSHAKE:
195 svc_handshake(&svc_msg->handshake, hd);
197 case SVC_FUNCTION_UNIPRO_NETWORK_MANAGEMENT:
198 svc_management(&svc_msg->management, hd);
200 case SVC_FUNCTION_HOTPLUG:
201 svc_hotplug(&svc_msg->hotplug, hd);
203 case SVC_FUNCTION_DDB:
204 svc_ddb(&svc_msg->ddb, hd);
206 case SVC_FUNCTION_POWER:
207 svc_power(&svc_msg->power, hd);
209 case SVC_FUNCTION_EPM:
210 svc_epm(&svc_msg->epm, hd);
212 case SVC_FUNCTION_SUSPEND:
213 svc_suspend(&svc_msg->suspend, hd);
216 dev_err(&hd->dev, "received invalid SVC message type %d\n",
217 svc_msg->header.type);
223 static struct ap_msg *get_ap_msg(void)
225 struct ap_msg *ap_msg;
228 spin_lock_irqsave(&ap_msg_list_lock, flags);
230 ap_msg = list_first_entry_or_null(&ap_msg_list, struct ap_msg, list);
232 list_del(&ap_msg->list);
233 spin_unlock_irqrestore(&ap_msg_list_lock, flags);
238 static int ap_process_loop(void *data)
240 struct ap_msg *ap_msg;
242 while (!kthread_should_stop()) {
243 wait_event_interruptible(ap_wait, kthread_should_stop());
245 if (kthread_should_stop())
248 /* Get some data off of the ap list and process it */
249 ap_msg = get_ap_msg();
253 process_ap_message(ap_msg);
255 /* clean the message up */
262 int gb_new_ap_msg(u8 *data, int size, struct greybus_host_device *hd)
264 struct ap_msg *ap_msg;
268 * Totally naive copy the message into a new structure that we slowly
269 * create and add it to the list. Let's get this working, the odds of
270 * this being any "slow path" for AP messages is really low at this
271 * point in time, but you never know, so this comment is here to point
272 * out that maybe we should use a slab allocator, or even just not copy
273 * the data, but use it directly and force the urbs to be "new" each
277 /* Note - this can, and will, be called in interrupt context. */
278 ap_msg = kmalloc(sizeof(*ap_msg), GFP_ATOMIC);
281 ap_msg->data = kmalloc(size, GFP_ATOMIC);
286 memcpy(ap_msg->data, data, size);
290 spin_lock_irqsave(&ap_msg_list_lock, flags);
291 list_add(&ap_msg->list, &ap_msg_list);
292 spin_unlock_irqrestore(&ap_msg_list_lock, flags);
294 /* kick our thread to handle the message */
295 wake_up_interruptible(&ap_wait);
299 EXPORT_SYMBOL_GPL(gb_new_ap_msg);
302 int gb_thread_init(void)
304 init_waitqueue_head(&ap_wait);
305 spin_lock_init(&ap_msg_list_lock);
307 ap_thread = kthread_run(ap_process_loop, NULL, "greybus_ap");
308 if (IS_ERR(ap_thread))
309 return PTR_ERR(ap_thread);
314 void gb_thread_destroy(void)
316 kthread_stop(ap_thread);