diff options
Diffstat (limited to 'include/linux/kfifo.h')
-rw-r--r-- | include/linux/kfifo.h | 555 |
1 files changed, 507 insertions, 48 deletions
diff --git a/include/linux/kfifo.h b/include/linux/kfifo.h index ad6bdf5a5970..486e8ad3bb50 100644 --- a/include/linux/kfifo.h +++ b/include/linux/kfifo.h | |||
@@ -1,6 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | * A simple kernel FIFO implementation. | 2 | * A generic kernel FIFO implementation. |
3 | * | 3 | * |
4 | * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net> | ||
4 | * Copyright (C) 2004 Stelian Pop <stelian@popies.net> | 5 | * Copyright (C) 2004 Stelian Pop <stelian@popies.net> |
5 | * | 6 | * |
6 | * This program is free software; you can redistribute it and/or modify | 7 | * This program is free software; you can redistribute it and/or modify |
@@ -18,6 +19,25 @@ | |||
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | * | 20 | * |
20 | */ | 21 | */ |
22 | |||
23 | /* | ||
24 | * Howto porting drivers to the new generic fifo API: | ||
25 | * | ||
26 | * - Modify the declaration of the "struct kfifo *" object into a | ||
27 | * in-place "struct kfifo" object | ||
28 | * - Init the in-place object with kfifo_alloc() or kfifo_init() | ||
29 | * Note: The address of the in-place "struct kfifo" object must be | ||
30 | * passed as the first argument to this functions | ||
31 | * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get | ||
32 | * into kfifo_out | ||
33 | * - Replace the use of kfifo_put into kfifo_in_locked and kfifo_get | ||
34 | * into kfifo_out_locked | ||
35 | * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc | ||
36 | * must be passed now to the kfifo_in_locked and kfifo_out_locked | ||
37 | * as the last parameter. | ||
38 | * - All formerly name __kfifo_* functions has been renamed into kfifo_* | ||
39 | */ | ||
40 | |||
21 | #ifndef _LINUX_KFIFO_H | 41 | #ifndef _LINUX_KFIFO_H |
22 | #define _LINUX_KFIFO_H | 42 | #define _LINUX_KFIFO_H |
23 | 43 | ||
@@ -29,124 +49,563 @@ struct kfifo { | |||
29 | unsigned int size; /* the size of the allocated buffer */ | 49 | unsigned int size; /* the size of the allocated buffer */ |
30 | unsigned int in; /* data is added at offset (in % size) */ | 50 | unsigned int in; /* data is added at offset (in % size) */ |
31 | unsigned int out; /* data is extracted from off. (out % size) */ | 51 | unsigned int out; /* data is extracted from off. (out % size) */ |
32 | spinlock_t *lock; /* protects concurrent modifications */ | ||
33 | }; | 52 | }; |
34 | 53 | ||
35 | extern struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size, | 54 | /* |
36 | gfp_t gfp_mask, spinlock_t *lock); | 55 | * Macros for declaration and initialization of the kfifo datatype |
37 | extern struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask, | 56 | */ |
38 | spinlock_t *lock); | 57 | |
58 | /* helper macro */ | ||
59 | #define __kfifo_initializer(s, b) \ | ||
60 | (struct kfifo) { \ | ||
61 | .size = s, \ | ||
62 | .in = 0, \ | ||
63 | .out = 0, \ | ||
64 | .buffer = b \ | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer | ||
69 | * @name: name of the declared kfifo datatype | ||
70 | * @size: size of the fifo buffer | ||
71 | * | ||
72 | * Note: the macro can be used inside struct or union declaration | ||
73 | * Note: the macro creates two objects: | ||
74 | * A kfifo object with the given name and a buffer for the kfifo | ||
75 | * object named name##kfifo_buffer | ||
76 | */ | ||
77 | #define DECLARE_KFIFO(name, size) \ | ||
78 | union { \ | ||
79 | struct kfifo name; \ | ||
80 | unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \ | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * INIT_KFIFO - Initialize a kfifo declared by DECLARED_KFIFO | ||
85 | * @name: name of the declared kfifo datatype | ||
86 | * @size: size of the fifo buffer | ||
87 | */ | ||
88 | #define INIT_KFIFO(name) \ | ||
89 | name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \ | ||
90 | sizeof(struct kfifo), name##kfifo_buffer) | ||
91 | |||
92 | /** | ||
93 | * DEFINE_KFIFO - macro to define and initialize a kfifo | ||
94 | * @name: name of the declared kfifo datatype | ||
95 | * @size: size of the fifo buffer | ||
96 | * | ||
97 | * Note: the macro can be used for global and local kfifo data type variables | ||
98 | * Note: the macro creates two objects: | ||
99 | * A kfifo object with the given name and a buffer for the kfifo | ||
100 | * object named name##kfifo_buffer | ||
101 | */ | ||
102 | #define DEFINE_KFIFO(name, size) \ | ||
103 | unsigned char name##kfifo_buffer[size]; \ | ||
104 | struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer) | ||
105 | |||
106 | #undef __kfifo_initializer | ||
107 | |||
108 | extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer, | ||
109 | unsigned int size); | ||
110 | extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size, | ||
111 | gfp_t gfp_mask); | ||
39 | extern void kfifo_free(struct kfifo *fifo); | 112 | extern void kfifo_free(struct kfifo *fifo); |
40 | extern unsigned int __kfifo_put(struct kfifo *fifo, | 113 | extern unsigned int kfifo_in(struct kfifo *fifo, |
41 | const unsigned char *buffer, unsigned int len); | 114 | const unsigned char *from, unsigned int len); |
42 | extern unsigned int __kfifo_get(struct kfifo *fifo, | 115 | extern __must_check unsigned int kfifo_out(struct kfifo *fifo, |
43 | unsigned char *buffer, unsigned int len); | 116 | unsigned char *to, unsigned int len); |
44 | 117 | ||
45 | /** | 118 | /** |
46 | * __kfifo_reset - removes the entire FIFO contents, no locking version | 119 | * kfifo_reset - removes the entire FIFO contents |
47 | * @fifo: the fifo to be emptied. | 120 | * @fifo: the fifo to be emptied. |
48 | */ | 121 | */ |
49 | static inline void __kfifo_reset(struct kfifo *fifo) | 122 | static inline void kfifo_reset(struct kfifo *fifo) |
50 | { | 123 | { |
51 | fifo->in = fifo->out = 0; | 124 | fifo->in = fifo->out = 0; |
52 | } | 125 | } |
53 | 126 | ||
54 | /** | 127 | /** |
55 | * kfifo_reset - removes the entire FIFO contents | 128 | * kfifo_reset_out - skip FIFO contents |
56 | * @fifo: the fifo to be emptied. | 129 | * @fifo: the fifo to be emptied. |
57 | */ | 130 | */ |
58 | static inline void kfifo_reset(struct kfifo *fifo) | 131 | static inline void kfifo_reset_out(struct kfifo *fifo) |
59 | { | 132 | { |
60 | unsigned long flags; | 133 | smp_mb(); |
134 | fifo->out = fifo->in; | ||
135 | } | ||
61 | 136 | ||
62 | spin_lock_irqsave(fifo->lock, flags); | 137 | /** |
138 | * kfifo_size - returns the size of the fifo in bytes | ||
139 | * @fifo: the fifo to be used. | ||
140 | */ | ||
141 | static inline __must_check unsigned int kfifo_size(struct kfifo *fifo) | ||
142 | { | ||
143 | return fifo->size; | ||
144 | } | ||
63 | 145 | ||
64 | __kfifo_reset(fifo); | 146 | /** |
147 | * kfifo_len - returns the number of used bytes in the FIFO | ||
148 | * @fifo: the fifo to be used. | ||
149 | */ | ||
150 | static inline unsigned int kfifo_len(struct kfifo *fifo) | ||
151 | { | ||
152 | register unsigned int out; | ||
65 | 153 | ||
66 | spin_unlock_irqrestore(fifo->lock, flags); | 154 | out = fifo->out; |
155 | smp_rmb(); | ||
156 | return fifo->in - out; | ||
67 | } | 157 | } |
68 | 158 | ||
69 | /** | 159 | /** |
70 | * kfifo_put - puts some data into the FIFO | 160 | * kfifo_is_empty - returns true if the fifo is empty |
71 | * @fifo: the fifo to be used. | 161 | * @fifo: the fifo to be used. |
72 | * @buffer: the data to be added. | 162 | */ |
73 | * @len: the length of the data to be added. | 163 | static inline __must_check int kfifo_is_empty(struct kfifo *fifo) |
164 | { | ||
165 | return fifo->in == fifo->out; | ||
166 | } | ||
167 | |||
168 | /** | ||
169 | * kfifo_is_full - returns true if the fifo is full | ||
170 | * @fifo: the fifo to be used. | ||
171 | */ | ||
172 | static inline __must_check int kfifo_is_full(struct kfifo *fifo) | ||
173 | { | ||
174 | return kfifo_len(fifo) == kfifo_size(fifo); | ||
175 | } | ||
176 | |||
177 | /** | ||
178 | * kfifo_avail - returns the number of bytes available in the FIFO | ||
179 | * @fifo: the fifo to be used. | ||
180 | */ | ||
181 | static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo) | ||
182 | { | ||
183 | return kfifo_size(fifo) - kfifo_len(fifo); | ||
184 | } | ||
185 | |||
186 | /** | ||
187 | * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking | ||
188 | * @fifo: the fifo to be used. | ||
189 | * @from: the data to be added. | ||
190 | * @n: the length of the data to be added. | ||
191 | * @lock: pointer to the spinlock to use for locking. | ||
74 | * | 192 | * |
75 | * This function copies at most @len bytes from the @buffer into | 193 | * This function copies at most @len bytes from the @from buffer into |
76 | * the FIFO depending on the free space, and returns the number of | 194 | * the FIFO depending on the free space, and returns the number of |
77 | * bytes copied. | 195 | * bytes copied. |
78 | */ | 196 | */ |
79 | static inline unsigned int kfifo_put(struct kfifo *fifo, | 197 | static inline unsigned int kfifo_in_locked(struct kfifo *fifo, |
80 | const unsigned char *buffer, unsigned int len) | 198 | const unsigned char *from, unsigned int n, spinlock_t *lock) |
81 | { | 199 | { |
82 | unsigned long flags; | 200 | unsigned long flags; |
83 | unsigned int ret; | 201 | unsigned int ret; |
84 | 202 | ||
85 | spin_lock_irqsave(fifo->lock, flags); | 203 | spin_lock_irqsave(lock, flags); |
86 | 204 | ||
87 | ret = __kfifo_put(fifo, buffer, len); | 205 | ret = kfifo_in(fifo, from, n); |
88 | 206 | ||
89 | spin_unlock_irqrestore(fifo->lock, flags); | 207 | spin_unlock_irqrestore(lock, flags); |
90 | 208 | ||
91 | return ret; | 209 | return ret; |
92 | } | 210 | } |
93 | 211 | ||
94 | /** | 212 | /** |
95 | * kfifo_get - gets some data from the FIFO | 213 | * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking |
96 | * @fifo: the fifo to be used. | 214 | * @fifo: the fifo to be used. |
97 | * @buffer: where the data must be copied. | 215 | * @to: where the data must be copied. |
98 | * @len: the size of the destination buffer. | 216 | * @n: the size of the destination buffer. |
217 | * @lock: pointer to the spinlock to use for locking. | ||
99 | * | 218 | * |
100 | * This function copies at most @len bytes from the FIFO into the | 219 | * This function copies at most @len bytes from the FIFO into the |
101 | * @buffer and returns the number of copied bytes. | 220 | * @to buffer and returns the number of copied bytes. |
102 | */ | 221 | */ |
103 | static inline unsigned int kfifo_get(struct kfifo *fifo, | 222 | static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo, |
104 | unsigned char *buffer, unsigned int len) | 223 | unsigned char *to, unsigned int n, spinlock_t *lock) |
105 | { | 224 | { |
106 | unsigned long flags; | 225 | unsigned long flags; |
107 | unsigned int ret; | 226 | unsigned int ret; |
108 | 227 | ||
109 | spin_lock_irqsave(fifo->lock, flags); | 228 | spin_lock_irqsave(lock, flags); |
110 | 229 | ||
111 | ret = __kfifo_get(fifo, buffer, len); | 230 | ret = kfifo_out(fifo, to, n); |
112 | 231 | ||
113 | /* | 232 | /* |
114 | * optimization: if the FIFO is empty, set the indices to 0 | 233 | * optimization: if the FIFO is empty, set the indices to 0 |
115 | * so we don't wrap the next time | 234 | * so we don't wrap the next time |
116 | */ | 235 | */ |
117 | if (fifo->in == fifo->out) | 236 | if (kfifo_is_empty(fifo)) |
118 | fifo->in = fifo->out = 0; | 237 | kfifo_reset(fifo); |
238 | |||
239 | spin_unlock_irqrestore(lock, flags); | ||
240 | |||
241 | return ret; | ||
242 | } | ||
243 | |||
244 | extern void kfifo_skip(struct kfifo *fifo, unsigned int len); | ||
245 | |||
246 | extern __must_check unsigned int kfifo_from_user(struct kfifo *fifo, | ||
247 | const void __user *from, unsigned int n); | ||
248 | |||
249 | extern __must_check unsigned int kfifo_to_user(struct kfifo *fifo, | ||
250 | void __user *to, unsigned int n); | ||
251 | |||
252 | /** | ||
253 | * __kfifo_add_out internal helper function for updating the out offset | ||
254 | */ | ||
255 | static inline void __kfifo_add_out(struct kfifo *fifo, | ||
256 | unsigned int off) | ||
257 | { | ||
258 | smp_mb(); | ||
259 | fifo->out += off; | ||
260 | } | ||
261 | |||
262 | /** | ||
263 | * __kfifo_add_in internal helper function for updating the in offset | ||
264 | */ | ||
265 | static inline void __kfifo_add_in(struct kfifo *fifo, | ||
266 | unsigned int off) | ||
267 | { | ||
268 | smp_wmb(); | ||
269 | fifo->in += off; | ||
270 | } | ||
271 | |||
272 | /** | ||
273 | * __kfifo_off internal helper function for calculating the index of a | ||
274 | * given offeset | ||
275 | */ | ||
276 | static inline unsigned int __kfifo_off(struct kfifo *fifo, unsigned int off) | ||
277 | { | ||
278 | return off & (fifo->size - 1); | ||
279 | } | ||
280 | |||
281 | /** | ||
282 | * __kfifo_peek_n internal helper function for determinate the length of | ||
283 | * the next record in the fifo | ||
284 | */ | ||
285 | static inline unsigned int __kfifo_peek_n(struct kfifo *fifo, | ||
286 | unsigned int recsize) | ||
287 | { | ||
288 | #define __KFIFO_GET(fifo, off, shift) \ | ||
289 | ((fifo)->buffer[__kfifo_off((fifo), (fifo)->out+(off))] << (shift)) | ||
290 | |||
291 | unsigned int l; | ||
292 | |||
293 | l = __KFIFO_GET(fifo, 0, 0); | ||
294 | |||
295 | if (--recsize) | ||
296 | l |= __KFIFO_GET(fifo, 1, 8); | ||
297 | |||
298 | return l; | ||
299 | #undef __KFIFO_GET | ||
300 | } | ||
301 | |||
302 | /** | ||
303 | * __kfifo_poke_n internal helper function for storing the length of | ||
304 | * the next record into the fifo | ||
305 | */ | ||
306 | static inline void __kfifo_poke_n(struct kfifo *fifo, | ||
307 | unsigned int recsize, unsigned int n) | ||
308 | { | ||
309 | #define __KFIFO_PUT(fifo, off, val, shift) \ | ||
310 | ( \ | ||
311 | (fifo)->buffer[__kfifo_off((fifo), (fifo)->in+(off))] = \ | ||
312 | (unsigned char)((val) >> (shift)) \ | ||
313 | ) | ||
119 | 314 | ||
120 | spin_unlock_irqrestore(fifo->lock, flags); | 315 | __KFIFO_PUT(fifo, 0, n, 0); |
121 | 316 | ||
317 | if (--recsize) | ||
318 | __KFIFO_PUT(fifo, 1, n, 8); | ||
319 | #undef __KFIFO_PUT | ||
320 | } | ||
321 | |||
322 | /** | ||
323 | * __kfifo_in_... internal functions for put date into the fifo | ||
324 | * do not call it directly, use kfifo_in_rec() instead | ||
325 | */ | ||
326 | extern unsigned int __kfifo_in_n(struct kfifo *fifo, | ||
327 | const void *from, unsigned int n, unsigned int recsize); | ||
328 | |||
329 | extern unsigned int __kfifo_in_generic(struct kfifo *fifo, | ||
330 | const void *from, unsigned int n, unsigned int recsize); | ||
331 | |||
332 | static inline unsigned int __kfifo_in_rec(struct kfifo *fifo, | ||
333 | const void *from, unsigned int n, unsigned int recsize) | ||
334 | { | ||
335 | unsigned int ret; | ||
336 | |||
337 | ret = __kfifo_in_n(fifo, from, n, recsize); | ||
338 | |||
339 | if (likely(ret == 0)) { | ||
340 | if (recsize) | ||
341 | __kfifo_poke_n(fifo, recsize, n); | ||
342 | __kfifo_add_in(fifo, n + recsize); | ||
343 | } | ||
122 | return ret; | 344 | return ret; |
123 | } | 345 | } |
124 | 346 | ||
125 | /** | 347 | /** |
126 | * __kfifo_len - returns the number of bytes available in the FIFO, no locking version | 348 | * kfifo_in_rec - puts some record data into the FIFO |
127 | * @fifo: the fifo to be used. | 349 | * @fifo: the fifo to be used. |
350 | * @from: the data to be added. | ||
351 | * @n: the length of the data to be added. | ||
352 | * @recsize: size of record field | ||
353 | * | ||
354 | * This function copies @n bytes from the @from into the FIFO and returns | ||
355 | * the number of bytes which cannot be copied. | ||
356 | * A returned value greater than the @n value means that the record doesn't | ||
357 | * fit into the buffer. | ||
358 | * | ||
359 | * Note that with only one concurrent reader and one concurrent | ||
360 | * writer, you don't need extra locking to use these functions. | ||
128 | */ | 361 | */ |
129 | static inline unsigned int __kfifo_len(struct kfifo *fifo) | 362 | static inline __must_check unsigned int kfifo_in_rec(struct kfifo *fifo, |
363 | void *from, unsigned int n, unsigned int recsize) | ||
130 | { | 364 | { |
131 | return fifo->in - fifo->out; | 365 | if (!__builtin_constant_p(recsize)) |
366 | return __kfifo_in_generic(fifo, from, n, recsize); | ||
367 | return __kfifo_in_rec(fifo, from, n, recsize); | ||
132 | } | 368 | } |
133 | 369 | ||
134 | /** | 370 | /** |
135 | * kfifo_len - returns the number of bytes available in the FIFO | 371 | * __kfifo_out_... internal functions for get date from the fifo |
372 | * do not call it directly, use kfifo_out_rec() instead | ||
373 | */ | ||
374 | extern unsigned int __kfifo_out_n(struct kfifo *fifo, | ||
375 | void *to, unsigned int reclen, unsigned int recsize); | ||
376 | |||
377 | extern unsigned int __kfifo_out_generic(struct kfifo *fifo, | ||
378 | void *to, unsigned int n, | ||
379 | unsigned int recsize, unsigned int *total); | ||
380 | |||
381 | static inline unsigned int __kfifo_out_rec(struct kfifo *fifo, | ||
382 | void *to, unsigned int n, unsigned int recsize, | ||
383 | unsigned int *total) | ||
384 | { | ||
385 | unsigned int l; | ||
386 | |||
387 | if (!recsize) { | ||
388 | l = n; | ||
389 | if (total) | ||
390 | *total = l; | ||
391 | } else { | ||
392 | l = __kfifo_peek_n(fifo, recsize); | ||
393 | if (total) | ||
394 | *total = l; | ||
395 | if (n < l) | ||
396 | return l; | ||
397 | } | ||
398 | |||
399 | return __kfifo_out_n(fifo, to, l, recsize); | ||
400 | } | ||
401 | |||
402 | /** | ||
403 | * kfifo_out_rec - gets some record data from the FIFO | ||
136 | * @fifo: the fifo to be used. | 404 | * @fifo: the fifo to be used. |
405 | * @to: where the data must be copied. | ||
406 | * @n: the size of the destination buffer. | ||
407 | * @recsize: size of record field | ||
408 | * @total: pointer where the total number of to copied bytes should stored | ||
409 | * | ||
410 | * This function copies at most @n bytes from the FIFO to @to and returns the | ||
411 | * number of bytes which cannot be copied. | ||
412 | * A returned value greater than the @n value means that the record doesn't | ||
413 | * fit into the @to buffer. | ||
414 | * | ||
415 | * Note that with only one concurrent reader and one concurrent | ||
416 | * writer, you don't need extra locking to use these functions. | ||
137 | */ | 417 | */ |
138 | static inline unsigned int kfifo_len(struct kfifo *fifo) | 418 | static inline __must_check unsigned int kfifo_out_rec(struct kfifo *fifo, |
419 | void *to, unsigned int n, unsigned int recsize, | ||
420 | unsigned int *total) | ||
421 | |||
139 | { | 422 | { |
140 | unsigned long flags; | 423 | if (!__builtin_constant_p(recsize)) |
141 | unsigned int ret; | 424 | return __kfifo_out_generic(fifo, to, n, recsize, total); |
425 | return __kfifo_out_rec(fifo, to, n, recsize, total); | ||
426 | } | ||
427 | |||
428 | /** | ||
429 | * __kfifo_from_user_... internal functions for transfer from user space into | ||
430 | * the fifo. do not call it directly, use kfifo_from_user_rec() instead | ||
431 | */ | ||
432 | extern unsigned int __kfifo_from_user_n(struct kfifo *fifo, | ||
433 | const void __user *from, unsigned int n, unsigned int recsize); | ||
142 | 434 | ||
143 | spin_lock_irqsave(fifo->lock, flags); | 435 | extern unsigned int __kfifo_from_user_generic(struct kfifo *fifo, |
436 | const void __user *from, unsigned int n, unsigned int recsize); | ||
144 | 437 | ||
145 | ret = __kfifo_len(fifo); | 438 | static inline unsigned int __kfifo_from_user_rec(struct kfifo *fifo, |
439 | const void __user *from, unsigned int n, unsigned int recsize) | ||
440 | { | ||
441 | unsigned int ret; | ||
146 | 442 | ||
147 | spin_unlock_irqrestore(fifo->lock, flags); | 443 | ret = __kfifo_from_user_n(fifo, from, n, recsize); |
148 | 444 | ||
445 | if (likely(ret == 0)) { | ||
446 | if (recsize) | ||
447 | __kfifo_poke_n(fifo, recsize, n); | ||
448 | __kfifo_add_in(fifo, n + recsize); | ||
449 | } | ||
149 | return ret; | 450 | return ret; |
150 | } | 451 | } |
151 | 452 | ||
453 | /** | ||
454 | * kfifo_from_user_rec - puts some data from user space into the FIFO | ||
455 | * @fifo: the fifo to be used. | ||
456 | * @from: pointer to the data to be added. | ||
457 | * @n: the length of the data to be added. | ||
458 | * @recsize: size of record field | ||
459 | * | ||
460 | * This function copies @n bytes from the @from into the | ||
461 | * FIFO and returns the number of bytes which cannot be copied. | ||
462 | * | ||
463 | * If the returned value is equal or less the @n value, the copy_from_user() | ||
464 | * functions has failed. Otherwise the record doesn't fit into the buffer. | ||
465 | * | ||
466 | * Note that with only one concurrent reader and one concurrent | ||
467 | * writer, you don't need extra locking to use these functions. | ||
468 | */ | ||
469 | static inline __must_check unsigned int kfifo_from_user_rec(struct kfifo *fifo, | ||
470 | const void __user *from, unsigned int n, unsigned int recsize) | ||
471 | { | ||
472 | if (!__builtin_constant_p(recsize)) | ||
473 | return __kfifo_from_user_generic(fifo, from, n, recsize); | ||
474 | return __kfifo_from_user_rec(fifo, from, n, recsize); | ||
475 | } | ||
476 | |||
477 | /** | ||
478 | * __kfifo_to_user_... internal functions for transfer fifo data into user space | ||
479 | * do not call it directly, use kfifo_to_user_rec() instead | ||
480 | */ | ||
481 | extern unsigned int __kfifo_to_user_n(struct kfifo *fifo, | ||
482 | void __user *to, unsigned int n, unsigned int reclen, | ||
483 | unsigned int recsize); | ||
484 | |||
485 | extern unsigned int __kfifo_to_user_generic(struct kfifo *fifo, | ||
486 | void __user *to, unsigned int n, unsigned int recsize, | ||
487 | unsigned int *total); | ||
488 | |||
489 | static inline unsigned int __kfifo_to_user_rec(struct kfifo *fifo, | ||
490 | void __user *to, unsigned int n, | ||
491 | unsigned int recsize, unsigned int *total) | ||
492 | { | ||
493 | unsigned int l; | ||
494 | |||
495 | if (!recsize) { | ||
496 | l = n; | ||
497 | if (total) | ||
498 | *total = l; | ||
499 | } else { | ||
500 | l = __kfifo_peek_n(fifo, recsize); | ||
501 | if (total) | ||
502 | *total = l; | ||
503 | if (n < l) | ||
504 | return l; | ||
505 | } | ||
506 | |||
507 | return __kfifo_to_user_n(fifo, to, n, l, recsize); | ||
508 | } | ||
509 | |||
510 | /** | ||
511 | * kfifo_to_user_rec - gets data from the FIFO and write it to user space | ||
512 | * @fifo: the fifo to be used. | ||
513 | * @to: where the data must be copied. | ||
514 | * @n: the size of the destination buffer. | ||
515 | * @recsize: size of record field | ||
516 | * @total: pointer where the total number of to copied bytes should stored | ||
517 | * | ||
518 | * This function copies at most @n bytes from the FIFO to the @to. | ||
519 | * In case of an error, the function returns the number of bytes which cannot | ||
520 | * be copied. | ||
521 | * If the returned value is equal or less the @n value, the copy_to_user() | ||
522 | * functions has failed. Otherwise the record doesn't fit into the @to buffer. | ||
523 | * | ||
524 | * Note that with only one concurrent reader and one concurrent | ||
525 | * writer, you don't need extra locking to use these functions. | ||
526 | */ | ||
527 | static inline __must_check unsigned int kfifo_to_user_rec(struct kfifo *fifo, | ||
528 | void __user *to, unsigned int n, unsigned int recsize, | ||
529 | unsigned int *total) | ||
530 | { | ||
531 | if (!__builtin_constant_p(recsize)) | ||
532 | return __kfifo_to_user_generic(fifo, to, n, recsize, total); | ||
533 | return __kfifo_to_user_rec(fifo, to, n, recsize, total); | ||
534 | } | ||
535 | |||
536 | /** | ||
537 | * __kfifo_peek_... internal functions for peek into the next fifo record | ||
538 | * do not call it directly, use kfifo_peek_rec() instead | ||
539 | */ | ||
540 | extern unsigned int __kfifo_peek_generic(struct kfifo *fifo, | ||
541 | unsigned int recsize); | ||
542 | |||
543 | /** | ||
544 | * kfifo_peek_rec - gets the size of the next FIFO record data | ||
545 | * @fifo: the fifo to be used. | ||
546 | * @recsize: size of record field | ||
547 | * | ||
548 | * This function returns the size of the next FIFO record in number of bytes | ||
549 | */ | ||
550 | static inline __must_check unsigned int kfifo_peek_rec(struct kfifo *fifo, | ||
551 | unsigned int recsize) | ||
552 | { | ||
553 | if (!__builtin_constant_p(recsize)) | ||
554 | return __kfifo_peek_generic(fifo, recsize); | ||
555 | if (!recsize) | ||
556 | return kfifo_len(fifo); | ||
557 | return __kfifo_peek_n(fifo, recsize); | ||
558 | } | ||
559 | |||
560 | /** | ||
561 | * __kfifo_skip_... internal functions for skip the next fifo record | ||
562 | * do not call it directly, use kfifo_skip_rec() instead | ||
563 | */ | ||
564 | extern void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize); | ||
565 | |||
566 | static inline void __kfifo_skip_rec(struct kfifo *fifo, | ||
567 | unsigned int recsize) | ||
568 | { | ||
569 | unsigned int l; | ||
570 | |||
571 | if (recsize) { | ||
572 | l = __kfifo_peek_n(fifo, recsize); | ||
573 | |||
574 | if (l + recsize <= kfifo_len(fifo)) { | ||
575 | __kfifo_add_out(fifo, l + recsize); | ||
576 | return; | ||
577 | } | ||
578 | } | ||
579 | kfifo_reset_out(fifo); | ||
580 | } | ||
581 | |||
582 | /** | ||
583 | * kfifo_skip_rec - skip the next fifo out record | ||
584 | * @fifo: the fifo to be used. | ||
585 | * @recsize: size of record field | ||
586 | * | ||
587 | * This function skips the next FIFO record | ||
588 | */ | ||
589 | static inline void kfifo_skip_rec(struct kfifo *fifo, | ||
590 | unsigned int recsize) | ||
591 | { | ||
592 | if (!__builtin_constant_p(recsize)) | ||
593 | __kfifo_skip_generic(fifo, recsize); | ||
594 | else | ||
595 | __kfifo_skip_rec(fifo, recsize); | ||
596 | } | ||
597 | |||
598 | /** | ||
599 | * kfifo_avail_rec - returns the number of bytes available in a record FIFO | ||
600 | * @fifo: the fifo to be used. | ||
601 | * @recsize: size of record field | ||
602 | */ | ||
603 | static inline __must_check unsigned int kfifo_avail_rec(struct kfifo *fifo, | ||
604 | unsigned int recsize) | ||
605 | { | ||
606 | unsigned int l = kfifo_size(fifo) - kfifo_len(fifo); | ||
607 | |||
608 | return (l > recsize) ? l - recsize : 0; | ||
609 | } | ||
610 | |||
152 | #endif | 611 | #endif |