dvb_ringbuffer.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. *
  3. * dvb_ringbuffer.h: ring buffer implementation for the dvb driver
  4. *
  5. * Copyright (C) 2003 Oliver Endriss
  6. * Copyright (C) 2004 Andrew de Quincey
  7. *
  8. * based on code originally found in av7110.c & dvb_ci.c:
  9. * Copyright (C) 1999-2003 Ralph Metzler & Marcus Metzler
  10. * for convergence integrated media GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public License
  14. * as published by the Free Software Foundation; either version 2.1
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. */
  22. #ifndef _DVB_RINGBUFFER_H_
  23. #define _DVB_RINGBUFFER_H_
  24. #include <linux/spinlock.h>
  25. #include <linux/wait.h>
  26. /**
  27. * struct dvb_ringbuffer - Describes a ring buffer used at DVB framework
  28. *
  29. * @data: Area were the ringbuffer data is written
  30. * @size: size of the ringbuffer
  31. * @pread: next position to read
  32. * @pwrite: next position to write
  33. * @error: used by ringbuffer clients to indicate that an error happened.
  34. * @queue: Wait queue used by ringbuffer clients to indicate when buffer
  35. * was filled
  36. * @lock: Spinlock used to protect the ringbuffer
  37. */
  38. struct dvb_ringbuffer {
  39. u8 *data;
  40. ssize_t size;
  41. ssize_t pread;
  42. ssize_t pwrite;
  43. int error;
  44. wait_queue_head_t queue;
  45. spinlock_t lock;
  46. };
  47. #define DVB_RINGBUFFER_PKTHDRSIZE 3
  48. /**
  49. * dvb_ringbuffer_init - initialize ring buffer, lock and queue
  50. *
  51. * @rbuf: pointer to struct dvb_ringbuffer
  52. * @data: pointer to the buffer where the data will be stored
  53. * @len: bytes from ring buffer into @buf
  54. */
  55. extern void dvb_ringbuffer_init(struct dvb_ringbuffer *rbuf, void *data,
  56. size_t len);
  57. /**
  58. * dvb_ringbuffer_empty - test whether buffer is empty
  59. *
  60. * @rbuf: pointer to struct dvb_ringbuffer
  61. */
  62. extern int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf);
  63. /**
  64. * dvb_ringbuffer_free - returns the number of free bytes in the buffer
  65. *
  66. * @rbuf: pointer to struct dvb_ringbuffer
  67. *
  68. * Return: number of free bytes in the buffer
  69. */
  70. extern ssize_t dvb_ringbuffer_free(struct dvb_ringbuffer *rbuf);
  71. /**
  72. * dvb_ringbuffer_avail - returns the number of bytes waiting in the buffer
  73. *
  74. * @rbuf: pointer to struct dvb_ringbuffer
  75. *
  76. * Return: number of bytes waiting in the buffer
  77. */
  78. extern ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf);
  79. /**
  80. * dvb_ringbuffer_reset - resets the ringbuffer to initial state
  81. *
  82. * @rbuf: pointer to struct dvb_ringbuffer
  83. *
  84. * Resets the read and write pointers to zero and flush the buffer.
  85. *
  86. * This counts as a read and write operation
  87. */
  88. extern void dvb_ringbuffer_reset(struct dvb_ringbuffer *rbuf);
  89. /*
  90. * read routines & macros
  91. */
  92. /**
  93. * dvb_ringbuffer_flush - flush buffer
  94. *
  95. * @rbuf: pointer to struct dvb_ringbuffer
  96. */
  97. extern void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf);
  98. /**
  99. * dvb_ringbuffer_flush_spinlock_wakeup- flush buffer protected by spinlock
  100. * and wake-up waiting task(s)
  101. *
  102. * @rbuf: pointer to struct dvb_ringbuffer
  103. */
  104. extern void dvb_ringbuffer_flush_spinlock_wakeup(struct dvb_ringbuffer *rbuf);
  105. /**
  106. * DVB_RINGBUFFER_PEEK - peek at byte @offs in the buffer
  107. *
  108. * @rbuf: pointer to struct dvb_ringbuffer
  109. * @offs: offset inside the ringbuffer
  110. */
  111. #define DVB_RINGBUFFER_PEEK(rbuf, offs) \
  112. ((rbuf)->data[((rbuf)->pread + (offs)) % (rbuf)->size])
  113. #define DVB_RINGBUFFER_PUSH(rbuf, num) \
  114. ((rbuf)->pwrite = (((rbuf)->pwrite+(num))%(rbuf)->size))
  115. /**
  116. * DVB_RINGBUFFER_SKIP - advance read ptr by @num bytes
  117. *
  118. * @rbuf: pointer to struct dvb_ringbuffer
  119. * @num: number of bytes to advance
  120. */
  121. #define DVB_RINGBUFFER_SKIP(rbuf, num) {\
  122. (rbuf)->pread = ((rbuf)->pread + (num)) % (rbuf)->size;\
  123. }
  124. /**
  125. * dvb_ringbuffer_read_user - Reads a buffer into an user pointer
  126. *
  127. * @rbuf: pointer to struct dvb_ringbuffer
  128. * @buf: pointer to the buffer where the data will be stored
  129. * @len: bytes from ring buffer into @buf
  130. *
  131. * This variant assumes that the buffer is a memory at the userspace. So,
  132. * it will internally call copy_to_user().
  133. *
  134. * Return: number of bytes transferred or -EFAULT
  135. */
  136. extern ssize_t dvb_ringbuffer_read_user(struct dvb_ringbuffer *rbuf,
  137. u8 __user *buf, size_t len);
  138. /**
  139. * dvb_ringbuffer_read - Reads a buffer into a pointer
  140. *
  141. * @rbuf: pointer to struct dvb_ringbuffer
  142. * @buf: pointer to the buffer where the data will be stored
  143. * @len: bytes from ring buffer into @buf
  144. *
  145. * This variant assumes that the buffer is a memory at the Kernel space
  146. *
  147. * Return: number of bytes transferred or -EFAULT
  148. */
  149. extern void dvb_ringbuffer_read(struct dvb_ringbuffer *rbuf,
  150. u8 *buf, size_t len);
  151. /*
  152. * write routines & macros
  153. */
  154. /**
  155. * DVB_RINGBUFFER_WRITE_BYTE - write single byte to ring buffer
  156. *
  157. * @rbuf: pointer to struct dvb_ringbuffer
  158. * @byte: byte to write
  159. */
  160. #define DVB_RINGBUFFER_WRITE_BYTE(rbuf, byte) \
  161. { (rbuf)->data[(rbuf)->pwrite] = (byte); \
  162. (rbuf)->pwrite = ((rbuf)->pwrite + 1) % (rbuf)->size; }
  163. /**
  164. * dvb_ringbuffer_write - Writes a buffer into the ringbuffer
  165. *
  166. * @rbuf: pointer to struct dvb_ringbuffer
  167. * @buf: pointer to the buffer where the data will be read
  168. * @len: bytes from ring buffer into @buf
  169. *
  170. * This variant assumes that the buffer is a memory at the Kernel space
  171. *
  172. * return: number of bytes transferred or -EFAULT
  173. */
  174. extern ssize_t dvb_ringbuffer_write(struct dvb_ringbuffer *rbuf, const u8 *buf,
  175. size_t len);
  176. /**
  177. * dvb_ringbuffer_write_user - Writes a buffer received via an user pointer
  178. *
  179. * @rbuf: pointer to struct dvb_ringbuffer
  180. * @buf: pointer to the buffer where the data will be read
  181. * @len: bytes from ring buffer into @buf
  182. *
  183. * This variant assumes that the buffer is a memory at the userspace. So,
  184. * it will internally call copy_from_user().
  185. *
  186. * Return: number of bytes transferred or -EFAULT
  187. */
  188. extern ssize_t dvb_ringbuffer_write_user(struct dvb_ringbuffer *rbuf,
  189. const u8 __user *buf, size_t len);
  190. /**
  191. * dvb_ringbuffer_pkt_write - Write a packet into the ringbuffer.
  192. *
  193. * @rbuf: Ringbuffer to write to.
  194. * @buf: Buffer to write.
  195. * @len: Length of buffer (currently limited to 65535 bytes max).
  196. *
  197. * Return: Number of bytes written, or -EFAULT, -ENOMEM, -EVINAL.
  198. */
  199. extern ssize_t dvb_ringbuffer_pkt_write(struct dvb_ringbuffer *rbuf, u8 *buf,
  200. size_t len);
  201. /**
  202. * dvb_ringbuffer_pkt_read_user - Read from a packet in the ringbuffer.
  203. *
  204. * @rbuf: Ringbuffer concerned.
  205. * @idx: Packet index as returned by dvb_ringbuffer_pkt_next().
  206. * @offset: Offset into packet to read from.
  207. * @buf: Destination buffer for data.
  208. * @len: Size of destination buffer.
  209. *
  210. * Return: Number of bytes read, or -EFAULT.
  211. *
  212. * .. note::
  213. *
  214. * unlike dvb_ringbuffer_read(), this does **NOT** update the read pointer
  215. * in the ringbuffer. You must use dvb_ringbuffer_pkt_dispose() to mark a
  216. * packet as no longer required.
  217. */
  218. extern ssize_t dvb_ringbuffer_pkt_read_user(struct dvb_ringbuffer *rbuf,
  219. size_t idx,
  220. int offset, u8 __user *buf,
  221. size_t len);
  222. /**
  223. * dvb_ringbuffer_pkt_read - Read from a packet in the ringbuffer.
  224. * Note: unlike dvb_ringbuffer_read_user(), this DOES update the read pointer
  225. * in the ringbuffer.
  226. *
  227. * @rbuf: Ringbuffer concerned.
  228. * @idx: Packet index as returned by dvb_ringbuffer_pkt_next().
  229. * @offset: Offset into packet to read from.
  230. * @buf: Destination buffer for data.
  231. * @len: Size of destination buffer.
  232. *
  233. * Return: Number of bytes read, or -EFAULT.
  234. */
  235. extern ssize_t dvb_ringbuffer_pkt_read(struct dvb_ringbuffer *rbuf, size_t idx,
  236. int offset, u8 *buf, size_t len);
  237. /**
  238. * dvb_ringbuffer_pkt_dispose - Dispose of a packet in the ring buffer.
  239. *
  240. * @rbuf: Ring buffer concerned.
  241. * @idx: Packet index as returned by dvb_ringbuffer_pkt_next().
  242. */
  243. extern void dvb_ringbuffer_pkt_dispose(struct dvb_ringbuffer *rbuf, size_t idx);
  244. /**
  245. * dvb_ringbuffer_pkt_next - Get the index of the next packet in a ringbuffer.
  246. *
  247. * @rbuf: Ringbuffer concerned.
  248. * @idx: Previous packet index, or -1 to return the first packet index.
  249. * @pktlen: On success, will be updated to contain the length of the packet
  250. * in bytes.
  251. * returns Packet index (if >=0), or -1 if no packets available.
  252. */
  253. extern ssize_t dvb_ringbuffer_pkt_next(struct dvb_ringbuffer *rbuf, size_t idx,
  254. size_t *pktlen);
  255. /**
  256. * Start a new packet that will be written directly by the user to the packet
  257. * buffer.
  258. * The function only writes the header of the packet into the packet buffer,
  259. * and the packet is in pending state (can't be read by the reader) until it is
  260. * closed using dvb_ringbuffer_pkt_close. You must write the data into the
  261. * packet buffer using dvb_ringbuffer_write followed by
  262. * dvb_ringbuffer_pkt_close.
  263. *
  264. * @rbuf: Ringbuffer concerned.
  265. * @len: Size of the packet's data
  266. * returns Index of the packet's header that was started.
  267. */
  268. extern ssize_t dvb_ringbuffer_pkt_start(struct dvb_ringbuffer *rbuf,
  269. size_t len);
  270. /**
  271. * Close a packet that was started using dvb_ringbuffer_pkt_start.
  272. * The packet will be marked as ready to be ready.
  273. *
  274. * @rbuf: Ringbuffer concerned.
  275. * @idx: Packet index that was returned by dvb_ringbuffer_pkt_start
  276. * returns error status, -EINVAL if the provided index is invalid
  277. */
  278. extern int dvb_ringbuffer_pkt_close(struct dvb_ringbuffer *rbuf, ssize_t idx);
  279. #endif /* _DVB_RINGBUFFER_H_ */