Merge "[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         enum { size_max = 128 * 1024 * 1024 };
60
61         ret = copy_from_user(&bmf, (void*)msg, sizeof(bmf));
62         if (ret)
63                 return ret;
64
65         size = bmf.len;
66         if (size >= size_max) {
67                 printk("%s: too large message, size=%u\n", __func__, size);
68                 return -ENOMEM;
69         }
70
71         ret = init_mb(&mb, size);
72         if (ret)
73                 return ret;
74
75         payload = msg + sizeof(bmf);
76         if (size) {
77                 ret = copy_from_user(mb.begin, (void*)payload, size);
78                 if (ret)
79                         goto uninit;
80         }
81
82         msg_id = bmf.msg_id;
83         switch (msg_id) {
84         case MSG_KEEP_ALIVE:
85                 print_parse_debug("MSG_KEEP_ALIVE. size=%d\n", size);
86                 ret = msg_keep_alive(&mb);
87                 break;
88         case MSG_START:
89                 print_parse_debug("MSG_START. size=%d\n", size);
90                 ret = msg_start(&mb);
91                 break;
92         case MSG_STOP:
93                 print_parse_debug("MSG_STOP. size=%d\n", size);
94                 ret = msg_stop(&mb);
95                 break;
96         case MSG_CONFIG:
97                 print_parse_debug("MSG_CONFIG. size=%d\n", size);
98                 ret = msg_config(&mb);
99                 break;
100         case MSG_SWAP_INST_ADD:
101                 print_parse_debug("MSG_SWAP_INST_ADD. size=%d\n", size);
102                 ret = msg_swap_inst_add(&mb);
103                 break;
104         case MSG_SWAP_INST_REMOVE:
105                 print_parse_debug("MSG_SWAP_INST_REMOVE. size=%d\n", size);
106                 ret = msg_swap_inst_remove(&mb);
107                 break;
108         default:
109                 print_err("incorrect message ID [%u]. size=%d\n", msg_id, size);
110                 ret = -EINVAL;
111                 break;
112         }
113
114 uninit:
115         uninit_mb(&mb);
116         return ret;
117 }
118
119 static void register_msg_handler(void)
120 {
121         set_msg_handler(msg_handler);
122 }
123
124 static void unregister_msg_handler(void)
125 {
126         set_msg_handler(NULL);
127 }
128
129 static int __init swap_parser_init(void)
130 {
131         int ret;
132         register_msg_handler();
133
134         ret = init_cmd();
135
136         return ret;
137 }
138
139 static void __exit swap_parser_exit(void)
140 {
141         uninit_cmd();
142         unregister_msg_handler();
143 }
144
145 module_init(swap_parser_init);
146 module_exit(swap_parser_exit);
147
148 MODULE_LICENSE("GPL");