[IMPROVE] Driver: implement kernel -> user connect 19/15619/4
authorAlexander Aksenov <a.aksenov@samsung.com>
Wed, 22 Jan 2014 11:27:26 +0000 (15:27 +0400)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Thu, 27 Feb 2014 12:42:10 +0000 (04:42 -0800)
Based on netlink

Change-Id: Id777adfb6f4dbc7689f63aa10eb8f6cdf7a8cb5c
Signed-off-by: Alexander Aksenov <a.aksenov@samsung.com>
driver/Kbuild
driver/swap_driver_errors.h
driver/swap_driver_module.c
driver/us_interaction.c [new file with mode: 0644]
driver/us_interaction.h [new file with mode: 0644]
driver/us_interaction_msg.h [new file with mode: 0644]

index bc4a023..855ddca 100644 (file)
@@ -5,4 +5,5 @@ obj-m := swap_driver.o
 swap_driver-y := swap_driver_module.o \
                      device_driver.o \
                      driver_to_buffer.o \
-                     swap_debugfs.o
+                     swap_debugfs.o \
+                     us_interaction.o
index fd6f7dd..e7cade7 100644 (file)
@@ -45,7 +45,10 @@ enum _swap_driver_errors {
        E_SD_WRONG_ARGS = 12,      /* Arguments, passed to the func, doesn't 
                                           pass sanity check */
        E_SD_NO_MEMORY = 13,            /* No memory to allocate */
-       E_SD_UNINIT_ERROR = 14    /* swap_buffer uninitialization error */
+       E_SD_UNINIT_ERROR = 14,    /* swap_buffer uninitialization error */
+       E_SD_NL_INIT_ERR = 15,     /* Netlink init error */
+       E_SD_NL_MSG_ERR = 16,      /* Netlink message send error */
+       E_SD_NO_DAEMON_PID = 17    /* No daemon pid in us_interaction */
 };
 
 #endif /* __SWAP_DRIVER_ERRORS_H__ */
index f0e4d6b..0d93ef8 100644 (file)
@@ -27,6 +27,7 @@
 #include "driver_defs.h"
 #include "device_driver.h"
 #include "swap_debugfs.h"
+#include "us_interaction.h"
 
 static int __init swap_driver_init(void)
 {
@@ -36,14 +37,29 @@ static int __init swap_driver_init(void)
        if (ret)
                return ret;
 
-       swap_device_init();
+       ret = swap_device_init();
+       if (ret)
+               goto dev_init_fail;
+
+       ret = us_interaction_create();
+       if (ret)
+               goto interact_create_fail;
+
        print_msg("Driver module initialized\n");
 
        return 0;
+
+dev_init_fail:
+       swap_debugfs_exit();
+interact_create_fail:
+       swap_device_exit();
+
+       return ret;
 }
 
 static void __exit swap_driver_exit(void)
 {
+       us_interaction_destroy();
        swap_device_exit();
        swap_debugfs_exit();
        print_msg("Driver module uninitialized\n");
diff --git a/driver/us_interaction.c b/driver/us_interaction.c
new file mode 100644 (file)
index 0000000..62f5635
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ *  SWAP device driver
+ *  modules/driver/us_interaction.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2014
+ *
+ * 2014         Alexander Aksenov <a.aksenov@samsung.com>: Driver user<-> kernel
+ *                                                  connect implement
+ *
+ */
+
+
+#include <linux/module.h>
+#include <linux/skbuff.h>
+#include <linux/types.h>
+#include <linux/connector.h>
+#include <linux/slab.h>
+
+#include "us_interaction.h"
+#include "us_interaction_msg.h"
+#include "swap_driver_errors.h"
+#include "kernel_operations.h"
+
+
+/* Connector id struct */
+static struct cb_id cn_swap_id = {CN_SWAP_IDX, CN_SWAP_VAL};
+
+/* Swap connector name */
+static const char cn_swap_name[] = "cn_swap";
+
+/* Send messages counter */
+static u32 msg_counter = 0;
+
+
+int us_interaction_send_msg(const void *data, size_t size)
+{
+       struct cn_msg *msg;
+       int ret;
+
+       msg = kzalloc(sizeof(*msg) + size, GFP_ATOMIC);
+       if (msg == NULL)
+               return -E_SD_NO_MEMORY;
+
+       memcpy(&msg->id, &cn_swap_id, sizeof(msg->id));
+       msg->seq = msg_counter;
+       msg->len = size;
+       memcpy(msg->data, data, msg->len);
+
+       ret = cn_netlink_send(msg, CN_DAEMON_GROUP, GFP_ATOMIC);
+       if (ret < 0)
+               goto fail_send;
+       kfree(msg);
+
+       msg_counter++;
+
+       return E_SD_SUCCESS;
+
+fail_send:
+       kfree(msg);
+
+       return ret;
+}
+
+static void us_interaction_recv_msg(struct cn_msg *msg,
+                                   struct netlink_skb_parms *nsp)
+{
+}
+
+int us_interaction_create(void)
+{
+       int res;
+
+       res = cn_add_callback(&cn_swap_id, cn_swap_name, us_interaction_recv_msg);
+       if (res)
+               return -E_SD_NL_INIT_ERR;
+
+       return E_SD_SUCCESS;
+}
+
+void us_interaction_destroy(void)
+{
+       cn_del_callback(&cn_swap_id);
+}
diff --git a/driver/us_interaction.h b/driver/us_interaction.h
new file mode 100644 (file)
index 0000000..f84137c
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ *  SWAP device driver
+ *  modules/driver/us_interaction.h
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2014
+ *
+ * 2014         Alexander Aksenov <a.aksenov@samsung.com>: Driver user<-> kernel
+ *                                                  connect implement
+ *
+ */
+
+#ifndef __US_INTERACTION_H__
+#define __US_INTERACTION_H__
+
+int us_interaction_create(void);
+void us_interaction_destroy(void);
+int us_interaction_send_msg(const void *data, size_t size);
+
+#endif /* __US_INTERACTION_H__ */
diff --git a/driver/us_interaction_msg.h b/driver/us_interaction_msg.h
new file mode 100644 (file)
index 0000000..c87a2c4
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ *  SWAP device driver
+ *  modules/driver/us_interaction_msg.h
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2014
+ *
+ * 2014         Alexander Aksenov <a.aksenov@samsung.com>: Driver user<-> kernel
+ *                                                  connect implement
+ *
+ */
+
+#ifndef __US_INTERACTION_MSG_H__
+#define __US_INTERACTION_MSG_H__
+
+#define CN_SWAP_IDX     0x22    /* Should be unique throughout the system */
+#define CN_SWAP_VAL     0x1     /* Just the same in kernel and user */
+#define CN_DAEMON_GROUP 0x1     /* Listener group. Connector works a bit faster
+                                 * when using one */
+
+enum us_interaction_k2u_msg_t {
+       US_INT_PAUSE_APPS = 1,      /* Make daemon pause apps */
+       US_INT_CONT_APPS = 2        /* Make daemon continue apps */
+};
+
+#endif /* __US_INTERACTION_MSG_H__ */