[REFACTOR] Buffer: move getting next queue element into separate function
[kernel/swap-modules.git] / parser / swap_msg_parser.c
1 /*
2  *  SWAP Parser
3  *  modules/parser/swap_msg_parser.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013  Vyacheslav Cherkashin, Vitaliy Cherepanov: SWAP Parser implement
22  *
23  */
24
25
26 #include <linux/module.h>
27 #include <linux/vmalloc.h>
28 #include <asm/uaccess.h>
29
30 #include "parser_defs.h"
31 #include "msg_buf.h"
32 #include "msg_cmd.h"
33
34 #include <driver/driver_to_msg.h>
35 #include <driver/swap_ioctl.h>
36
37 enum MSG_ID {
38         MSG_KEEP_ALIVE          = 0x0001,
39         MSG_START               = 0x0002,
40         MSG_STOP                = 0x0003,
41         MSG_CONFIG              = 0x0004,
42         MSG_SWAP_INST_ADD       = 0x0008,
43         MSG_SWAP_INST_REMOVE    = 0x0009
44 };
45
46 struct basic_msg_fmt {
47         u32 msg_id;
48         u32 len;
49 } __attribute__((packed));
50
51 static int msg_handler(void __user *msg)
52 {
53         int ret;
54         u32 size;
55         enum MSG_ID msg_id;
56         struct msg_buf mb;
57         void __user *payload;
58         struct basic_msg_fmt bmf;
59
60         ret = copy_from_user(&bmf, (void*)msg, sizeof(bmf));
61         if (ret)
62                 return ret;
63
64         size = bmf.len;
65         ret = init_mb(&mb, size);
66         if (ret)
67                 return ret;
68
69         payload = msg + sizeof(bmf);
70         if (size) {
71                 ret = copy_from_user(mb.begin, (void*)payload, size);
72                 if (ret)
73                         goto uninit;
74         }
75
76         msg_id = bmf.msg_id;
77         switch (msg_id) {
78         case MSG_KEEP_ALIVE:
79                 print_parse_debug("MSG_KEEP_ALIVE. size=%d\n", size);
80                 ret = msg_keep_alive(&mb);
81                 break;
82         case MSG_START:
83                 print_parse_debug("MSG_START. size=%d\n", size);
84                 ret = msg_start(&mb);
85                 break;
86         case MSG_STOP:
87                 print_parse_debug("MSG_STOP. size=%d\n", size);
88                 ret = msg_stop(&mb);
89                 break;
90         case MSG_CONFIG:
91                 print_parse_debug("MSG_CONFIG. size=%d\n", size);
92                 ret = msg_config(&mb);
93                 break;
94         case MSG_SWAP_INST_ADD:
95                 print_parse_debug("MSG_SWAP_INST_ADD. size=%d\n", size);
96                 ret = msg_swap_inst_add(&mb);
97                 break;
98         case MSG_SWAP_INST_REMOVE:
99                 print_parse_debug("MSG_SWAP_INST_REMOVE. size=%d\n", size);
100                 ret = msg_swap_inst_remove(&mb);
101                 break;
102         default:
103                 print_err("incorrect message ID [%u]. size=%d\n", msg_id, size);
104                 ret = -EINVAL;
105                 break;
106         }
107
108 uninit:
109         uninit_mb(&mb);
110         return ret;
111 }
112
113 static void register_msg_handler(void)
114 {
115         set_msg_handler(msg_handler);
116 }
117
118 static void unregister_msg_handler(void)
119 {
120         set_msg_handler(NULL);
121 }
122
123 static int __init swap_parser_init(void)
124 {
125         int ret;
126         register_msg_handler();
127
128         ret = init_cmd();
129
130         return ret;
131 }
132
133 static void __exit swap_parser_exit(void)
134 {
135         uninit_cmd();
136         unregister_msg_handler();
137 }
138
139 module_init(swap_parser_init);
140 module_exit(swap_parser_exit);
141
142 MODULE_LICENSE("GPL");