3 * modules/driver/device_driver.c
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.
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.
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.
19 * Copyright (C) Samsung Electronics, 2013
21 * 2013 Alexander Aksenov <a.aksenov@samsung.com>: SWAP device driver implement
25 #include <linux/types.h>
27 #include <linux/cdev.h>
28 #include <linux/err.h>
29 #include <linux/device.h>
30 #include <linux/ioctl.h>
31 #include <linux/slab.h>
33 #include <linux/splice.h>
34 #include <linux/sched.h>
35 #include <linux/module.h>
36 #include <linux/wait.h>
37 #include <asm/uaccess.h>
39 #include <ksyms/ksyms.h>
41 #include "device_driver.h"
42 #include "swap_driver_errors.h"
43 #include "driver_to_buffer.h"
44 #include "swap_ioctl.h"
45 #include "driver_defs.h"
46 #include "device_driver_to_driver_to_buffer.h"
47 #include "driver_to_buffer.h"
48 #include "driver_to_msg.h"
50 #define SWAP_DEVICE_NAME "swap_device"
52 /* swap_device driver routines */
53 static int swap_device_open(struct inode *inode, struct file *filp);
54 static int swap_device_release(struct inode *inode, struct file *file);
55 static ssize_t swap_device_read(struct file *filp, char __user *buf,
56 size_t count, loff_t *f_pos);
57 static ssize_t swap_device_write(struct file *filp, const char __user *buf,
58 size_t count, loff_t *f_pos);
59 static long swap_device_ioctl(struct file *filp, unsigned int cmd,
61 static ssize_t swap_device_splice_read(struct file *filp, loff_t *ppos,
62 struct pipe_inode_info *pipe, size_t len,
65 /* File operations structure */
66 const struct file_operations swap_device_fops = {
67 .read = swap_device_read,
68 .write = swap_device_write,
69 .open = swap_device_open,
70 .release = swap_device_release,
71 .unlocked_ioctl = swap_device_ioctl,
72 .splice_read = swap_device_splice_read,
75 /* Typedefs for splice_* funcs. Prototypes are for linux-3.8.6 */
76 typedef ssize_t(*splice_to_pipe_p_t)(struct pipe_inode_info *pipe,
77 struct splice_pipe_desc *spd);
78 typedef int(*splice_grow_spd_p_t)(const struct pipe_inode_info *pipe,
79 struct splice_pipe_desc *spd);
81 static splice_to_pipe_p_t splice_to_pipe_p = NULL;
82 static splice_grow_spd_p_t splice_grow_spd_p = NULL;
84 static msg_handler_t msg_handler = NULL;
87 static dev_t swap_device_no = 0;
89 /* Device cdev struct */
90 static struct cdev *swap_device_cdev = NULL;
92 /* Device class struct */
93 static struct class *swap_device_class = NULL;
95 /* Device device struct */
96 static struct device *swap_device_device = NULL;
98 /* Reading tasks queue */
99 static DECLARE_WAIT_QUEUE_HEAD(swap_device_wait);
101 /* We need this realization of splice_shrink_spd() because of the its desing
102 * frequent changes that I have encountered in custom kernels */
103 void swap_device_splice_shrink_spd(struct pipe_inode_info *pipe,
104 struct splice_pipe_desc *spd)
106 if (pipe->buffers <= PIPE_DEF_BUFFERS)
114 /* Register device TODO Think of permanent major */
115 int swap_device_init(void)
119 /* Allocating device major and minor nums for swap_device */
120 result = alloc_chrdev_region(&swap_device_no, 0, 1, SWAP_DEVICE_NAME);
122 print_crit("Major number allocation has failed\n");
123 result = -E_SD_ALLOC_CHRDEV_FAIL;
127 /* Creating device class. Using IS_ERR, because class_create returns ERR_PTR
129 swap_device_class = class_create(THIS_MODULE, SWAP_DEVICE_NAME);
130 if (IS_ERR(swap_device_class)) {
131 print_crit("Class creation has failed\n");
132 result = -E_SD_CLASS_CREATE_FAIL;
136 /* Cdev allocation */
137 swap_device_cdev = cdev_alloc();
138 if (!swap_device_cdev) {
139 print_crit("Cdev structure allocation has failed\n");
140 result = -E_SD_CDEV_ALLOC_FAIL;
144 /* Cdev intialization and setting file operations */
145 cdev_init(swap_device_cdev, &swap_device_fops);
147 /* Adding cdev to system */
148 result = cdev_add(swap_device_cdev, swap_device_no, 1);
150 print_crit("Device adding has failed\n");
151 result = -E_SD_CDEV_ADD_FAIL;
155 /* Create device struct */
156 swap_device_device = device_create(swap_device_class, NULL, swap_device_no,
157 "%s", SWAP_DEVICE_NAME);
158 if (IS_ERR(swap_device_device)) {
159 print_crit("Device struct creating has failed\n");
160 result = -E_SD_DEVICE_CREATE_FAIL;
164 /* Find splice_* funcs addresses */
165 splice_to_pipe_p = (splice_to_pipe_p_t)swap_ksyms("splice_to_pipe");
166 if (!splice_to_pipe_p) {
167 print_err("splice_to_pipe() not found!\n");
168 result = -E_SD_NO_SPLICE_FUNCS;
172 splice_grow_spd_p = (splice_grow_spd_p_t)swap_ksyms("splice_grow_spd");
173 if (!splice_grow_spd_p) {
174 print_err("splice_grow_spd() not found!\n");
175 result = -E_SD_NO_SPLICE_FUNCS;
182 if (swap_device_cdev) {
183 cdev_del(swap_device_cdev);
185 if (swap_device_class) {
186 class_destroy(swap_device_class);
188 if (swap_device_no) {
189 unregister_chrdev_region(swap_device_no, 1);
194 /* Unregister device TODO Check wether driver is registered */
195 void swap_device_exit(void)
197 splice_to_pipe_p = NULL;
198 splice_grow_spd_p = NULL;
200 device_destroy(swap_device_class, swap_device_no);
201 cdev_del(swap_device_cdev);
202 class_destroy(swap_device_class);
203 unregister_chrdev_region(swap_device_no, 1);
206 static int swap_device_open(struct inode *inode, struct file *filp)
208 // TODO MOD_INC_USE_COUNT
212 static int swap_device_release(struct inode *inode, struct file *filp)
214 // TODO MOD_DEC_USE_COUNT
218 static ssize_t swap_device_read(struct file *filp, char __user *buf,
219 size_t count, loff_t *f_pos)
221 /* Wait queue item that consists current task. It is used to be added in
222 * swap_device_wait queue if there is no data to be read. */
226 //TODO : Think about spin_locks to prevent reading race condition.
227 while((result = driver_to_buffer_next_buffer_to_read()) != E_SD_SUCCESS) {
229 /* Add process to the swap_device_wait queue and set the current task
230 * state TASK_INTERRUPTIBLE. If there is any data to be read, then the
231 * current task is removed from the swap_device_wait queue and its state
232 * is changed to this. */
233 prepare_to_wait(&swap_device_wait, &wait, TASK_INTERRUPTIBLE);
237 goto swap_device_read_error;
238 } else if (result == E_SD_NO_DATA_TO_READ) {
239 /* Yes, E_SD_NO_DATA_TO_READ should be positive, cause it's not
241 if (filp->f_flags & O_NONBLOCK) {
243 goto swap_device_read_error;
245 if (signal_pending(current)) {
246 result = -ERESTARTSYS;
247 goto swap_device_read_error;
250 finish_wait(&swap_device_wait, &wait);
254 result = driver_to_buffer_read(buf, count);
255 /* If there is an error - return 0 */
262 swap_device_read_error:
263 finish_wait(&swap_device_wait, &wait);
268 static ssize_t swap_device_write(struct file *filp, const char __user *buf,
269 size_t count, loff_t *f_pos)
271 char *kern_buffer = NULL;
274 kern_buffer = kmalloc(count, GFP_KERNEL);
276 print_err("Error allocating memory for buffer\n");
277 goto swap_device_write_out;
280 result = copy_from_user(kern_buffer, buf, count);
282 result = count - result;
284 /* Return 0 if there was an error while writing */
285 result = driver_to_buffer_write(result, kern_buffer);
291 swap_device_write_out:
295 static long swap_device_ioctl(struct file *filp, unsigned int cmd,
301 case SWAP_DRIVER_BUFFER_INITIALIZE:
303 struct buffer_initialize initialize_struct;
305 result = copy_from_user(&initialize_struct, (void*)arg,
306 sizeof(struct buffer_initialize));
311 result = driver_to_buffer_initialize(initialize_struct.size,
312 initialize_struct.count);
314 print_err("Buffer initialization failed %d\n", result);
317 result = E_SD_SUCCESS;
321 case SWAP_DRIVER_BUFFER_UNINITIALIZE:
323 result = driver_to_buffer_uninitialize();
325 print_err("Buffer uninitialization failed %d\n", result);
329 case SWAP_DRIVER_NEXT_BUFFER_TO_READ:
331 /* Use this carefully */
332 result = driver_to_buffer_next_buffer_to_read();
333 if (result == E_SD_NO_DATA_TO_READ) {
334 /* TODO Do what we usually do when there are no subbuffers to
335 * read (make daemon sleep ?) */
339 case SWAP_DRIVER_FLUSH_BUFFER:
341 result = driver_to_buffer_flush();
344 case SWAP_DRIVER_MSG:
346 result = msg_handler((void __user *)arg);
348 print_warn("msg_handler() is not register\n");
353 print_warn("Unknown command %d\n", cmd);
361 static void swap_device_pipe_buf_release(struct pipe_inode_info *inode,
362 struct pipe_buffer *pipe)
364 __free_page(pipe->page);
367 static void swap_device_page_release(struct splice_pipe_desc *spd,
370 __free_page(spd->pages[i]);
373 static const struct pipe_buf_operations swap_device_pipe_buf_ops = {
375 .map = generic_pipe_buf_map,
376 .unmap = generic_pipe_buf_unmap,
377 .confirm = generic_pipe_buf_confirm,
378 .release = swap_device_pipe_buf_release,
379 .steal = generic_pipe_buf_steal,
380 .get = generic_pipe_buf_get
383 static ssize_t swap_device_splice_read(struct file *filp, loff_t *ppos,
384 struct pipe_inode_info *pipe,
385 size_t len, unsigned int flags)
387 /* Wait queue item that consists current task. It is used to be added in
388 * swap_device_wait queue if there is no data to be read. */
392 struct page *pages[PIPE_DEF_BUFFERS];
393 struct partial_page partial[PIPE_DEF_BUFFERS];
394 struct splice_pipe_desc spd = {
397 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 5))
398 .nr_pages_max = PIPE_DEF_BUFFERS,
402 .ops = &swap_device_pipe_buf_ops,
403 .spd_release = swap_device_page_release,
406 /* Get next buffer to read */
407 //TODO : Think about spin_locks to prevent reading race condition.
408 while((result = driver_to_buffer_next_buffer_to_read()) != E_SD_SUCCESS) {
410 /* Add process to the swap_device_wait queue and set the current task
411 * state TASK_INTERRUPTIBLE. If there is any data to be read, then the
412 * current task is removed from the swap_device_wait queue and its state
414 prepare_to_wait(&swap_device_wait, &wait, TASK_INTERRUPTIBLE);
416 print_err("driver_to_buffer_next_buffer_to_read error %d\n", result);
417 //TODO Error return to OS
419 goto swap_device_splice_read_error;
420 } else if (result == E_SD_NO_DATA_TO_READ) {
421 if (filp->f_flags & O_NONBLOCK) {
423 goto swap_device_splice_read_error;
425 if (signal_pending(current)) {
426 result = -ERESTARTSYS;
427 goto swap_device_splice_read_error;
430 finish_wait(&swap_device_wait, &wait);
434 if (splice_grow_spd_p(pipe, &spd)) {
436 goto swap_device_splice_read_out;
439 result = driver_to_buffer_fill_spd(&spd);
441 print_err("Cannot fill spd for splice\n");
442 goto swap_device_shrink_spd;
445 result = splice_to_pipe_p(pipe, &spd);
447 swap_device_shrink_spd:
448 swap_device_splice_shrink_spd(pipe, &spd);
450 swap_device_splice_read_out:
453 swap_device_splice_read_error:
454 finish_wait(&swap_device_wait, &wait);
459 void swap_device_wake_up_process(void)
461 wake_up_interruptible(&swap_device_wait);
464 void set_msg_handler(msg_handler_t mh)
468 EXPORT_SYMBOL_GPL(set_msg_handler);