2 * driver/driver_to_buffer.c
3 * @author Alexander Aksenov <a.aksenov@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Copyright (C) Samsung Electronics, 2013
25 * @section DESCRIPTION
27 * Driver and buffer interaction interface implementation.
30 #include <linux/string.h>
31 #include <linux/slab.h>
32 #include <linux/splice.h>
33 #include <linux/uaccess.h>
34 #include <linux/spinlock.h>
37 #include <buffer/swap_buffer_module.h>
38 #include <buffer/swap_buffer_errors.h>
39 #include <buffer/buffer_description.h>
41 #include "driver_defs.h"
42 #include "swap_driver_errors.h"
43 #include "device_driver_to_driver_to_buffer.h"
44 #include "app_manage.h"
46 /* Current busy buffer */
47 static struct swap_subbuffer *busy_buffer;
49 /* Buffers count ready to be read */
50 static int buffers_to_read;
52 /* Pages count in one subbuffer */
53 static int pages_per_buffer;
55 /* Used to sync changes of the buffers_to_read var */
56 static spinlock_t buf_to_read;
59 static inline void init_buffers_to_read(void)
61 spin_lock_init(&buf_to_read);
65 static inline void inc_buffers_to_read(void)
69 spin_lock_irqsave(&buf_to_read, flags);
71 spin_unlock_irqrestore(&buf_to_read, flags);
74 static inline void dec_buffers_to_read(void)
78 spin_lock_irqsave(&buf_to_read, flags);
80 spin_unlock_irqrestore(&buf_to_read, flags);
83 static inline void set_buffers_to_read(int count)
87 spin_lock_irqsave(&buf_to_read, flags);
88 buffers_to_read = count;
89 spin_unlock_irqrestore(&buf_to_read, flags);
92 static inline int something_to_read(void)
97 spin_lock_irqsave(&buf_to_read, flags);
98 result = buffers_to_read;
99 spin_unlock_irqrestore(&buf_to_read, flags);
104 /* TODO Get subbuffer for reading */
105 static size_t driver_to_buffer_get(void)
109 /* If there is no readable buffers, return error */
110 result = swap_buffer_get(&busy_buffer);
111 if (result == -E_SB_NO_READABLE_BUFFERS) {
113 return -E_SD_NO_DATA_TO_READ;
114 } else if (result < 0) {
115 print_err("swap_buffer_get unhandle error %d\n", result);
116 return -E_SD_BUFFER_ERROR;
119 return busy_buffer->full_buffer_part;
122 /* TODO Release subbuffer */
123 static int driver_to_buffer_release(void)
128 return -E_SD_NO_BUSY_SUBBUFFER;
130 result = swap_buffer_release(&busy_buffer);
131 if (result == -E_SB_NO_SUBBUFFER_IN_BUSY) {
132 return -E_SD_WRONG_SUBBUFFER_PTR;
133 } else if (result < 0) {
134 print_err("swap_buffer_release unhandle error %d\n", result);
135 return -E_SD_BUFFER_ERROR;
143 static int driver_to_buffer_callback(bool wakeup)
145 /* Increment buffers_to_read counter */
146 inc_buffers_to_read();
148 swap_device_wake_up_process();
154 * @brief Copies data from subbuffer to userspace.
156 * @param[out] buf Pointer to userspace memory area whereto copy data from
158 * @param count Size of data to be read.
159 * @return Read data size on success, negative error code on error.
161 ssize_t driver_to_buffer_read(char __user *buf, size_t count)
163 size_t bytes_to_copy;
164 size_t bytes_to_read = 0;
165 int page_counter = 0;
167 /* Reading from swap_device means reading only current busy_buffer.
168 * So, if there is no busy_buffer, we don't get next to read, we just
169 * read nothing. In this case, or if there is nothing to read from
170 * busy_buffer - return -E_SD_NO_DATA_TO_READ. It should be correctly
171 * handled in device_driver */
172 if (!busy_buffer || !busy_buffer->full_buffer_part)
173 return -E_SD_NO_DATA_TO_READ;
175 /* Bytes count that we're going to copy to user buffer is equal to user
176 * buffer size or to subbuffer readable size whichever is less */
177 bytes_to_copy = (count > busy_buffer->full_buffer_part) ?
178 busy_buffer->full_buffer_part : count;
180 /* Copy data from each page to buffer */
181 while (bytes_to_copy > 0) {
182 /* Get size that should be copied from current page */
183 size_t read_from_this_page =
184 (bytes_to_copy > PAGE_SIZE) ? PAGE_SIZE
187 /* Copy and add size to copied bytes count */
189 /* TODO Check with more than one page */
190 bytes_to_read += read_from_this_page -
192 buf, page_address(busy_buffer->data_buffer) +
193 (sizeof(struct page *) *
195 read_from_this_page);
196 bytes_to_copy -= read_from_this_page;
200 return bytes_to_read;
204 * @brief Flushes SWAP buffer.
208 int driver_to_buffer_flush(void)
210 unsigned int flushed;
212 flushed = swap_buffer_flush();
213 set_buffers_to_read(flushed);
214 swap_device_wake_up_process();
220 * @brief Fills spd structure.
222 * @param[out] spd Pointer to the splice_pipe_desc struct that should be filled.
223 * @return 0 on success, negative error code on error.
225 int driver_to_buffer_fill_spd(struct splice_pipe_desc *spd)
227 size_t data_to_splice = busy_buffer->full_buffer_part;
228 struct page **pages = spd->pages;
229 struct partial_page *partial = spd->partial;
231 while (data_to_splice) {
232 size_t read_from_current_page = min(data_to_splice,
235 pages[spd->nr_pages] = alloc_page(GFP_KERNEL);
236 if (!pages[spd->nr_pages]) {
237 print_err("Cannot alloc page for splice\n");
241 /* FIXME: maybe there is more efficient way */
242 memcpy(page_address(pages[spd->nr_pages]),
243 page_address(&busy_buffer->data_buffer[spd->nr_pages]),
244 read_from_current_page);
246 /* Always beginning of the page */
247 partial[spd->nr_pages].offset = 0;
248 partial[spd->nr_pages].len = read_from_current_page;
250 /* Private is not used */
251 partial[spd->nr_pages].private = 0;
254 data_to_splice -= read_from_current_page;
256 /* TODO: add check for pipe->buffers exceeding */
257 /* if (spd->nr_pages == pipe->buffers) { */
265 * @brief Check for subbuffer ready to be read.
267 * @return 1 if there is subbuffer to be read, 0 - if there isn't.
269 int driver_to_buffer_buffer_to_read(void)
271 return busy_buffer ? 1 : 0;
275 * @brief Initializes SWAP buffer.
277 * @param size Size of one subbuffer.
278 * @param count Count of subbuffers.
279 * @return 0 on success, negative error code on error.
281 int driver_to_buffer_initialize(size_t size, unsigned int count)
284 struct buffer_init_t buf_init = {
285 .subbuffer_size = size,
286 .nr_subbuffers = count,
287 .subbuffer_full_cb = driver_to_buffer_callback,
288 .lower_threshold = 20,
289 .low_mem_cb = app_manage_pause_apps,
291 .enough_mem_cb = app_manage_cont_apps,
294 if (size == 0 && count == 0)
295 return -E_SD_WRONG_ARGS;
297 result = swap_buffer_init(&buf_init);
298 if (result == -E_SB_NO_MEM_QUEUE_BUSY
299 || result == -E_SB_NO_MEM_BUFFER_STRUCT) {
300 return -E_SD_NO_MEMORY;
303 /* TODO Race condition: buffer can be used in other thread till */
304 /* we're in this func */
305 /* Initialize driver_to_buffer variables */
306 pages_per_buffer = result;
308 init_buffers_to_read();
314 * @brief Uninitializes buffer.
316 * @return 0 on success, negative error code on error.
318 int driver_to_buffer_uninitialize(void)
322 /* Release occupied buffer */
324 result = driver_to_buffer_release();
325 /* TODO Maybe release anyway */
331 result = swap_buffer_uninit();
332 if (result == -E_SB_UNRELEASED_BUFFERS) {
333 print_err("Can't uninit buffer! There are busy subbuffers!\n");
334 result = -E_SD_BUFFER_ERROR;
335 } else if (result < 0) {
336 print_err("swap_buffer_uninit error %d\n", result);
337 result = -E_SD_BUFFER_ERROR;
339 result = E_SD_SUCCESS;
342 /* Reinit driver_to_buffer vars */
343 init_buffers_to_read();
344 pages_per_buffer = 0;
350 * @brief Get next buffer to read.
352 * @return 0 on success, negative error code on error, E_SD_NO_DATA_TO_READ if
353 * there is nothing to be read.
355 int driver_to_buffer_next_buffer_to_read(void)
359 /* If there is busy_buffer first release it */
361 result = driver_to_buffer_release();
366 /* If there is no buffers to read, return E_SD_NO_DATA_TO_READ.
367 * SHOULD BE POSITIVE, cause there is no real error. */
368 if (!something_to_read())
369 return E_SD_NO_DATA_TO_READ;
371 /* Get next buffer to read */
372 result = driver_to_buffer_get();
374 print_err("buffer_to_reads > 0, but there are no buffers to read\n");
378 /* Decrement buffers_to_read counter */
379 dec_buffers_to_read();