lirc_dev.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * LIRC base driver
  3. *
  4. * by Artur Lipowski <alipowski@interia.pl>
  5. * This code is licensed under GNU GPL
  6. *
  7. */
  8. #ifndef _LINUX_LIRC_DEV_H
  9. #define _LINUX_LIRC_DEV_H
  10. #define MAX_IRCTL_DEVICES 8
  11. #define BUFLEN 16
  12. #define mod(n, div) ((n) % (div))
  13. #include <linux/slab.h>
  14. #include <linux/fs.h>
  15. #include <linux/ioctl.h>
  16. #include <linux/poll.h>
  17. #include <linux/kfifo.h>
  18. #include <media/lirc.h>
  19. struct lirc_buffer {
  20. wait_queue_head_t wait_poll;
  21. spinlock_t fifo_lock;
  22. unsigned int chunk_size;
  23. unsigned int size; /* in chunks */
  24. /* Using chunks instead of bytes pretends to simplify boundary checking
  25. * And should allow for some performance fine tunning later */
  26. struct kfifo fifo;
  27. };
  28. static inline void lirc_buffer_clear(struct lirc_buffer *buf)
  29. {
  30. unsigned long flags;
  31. if (kfifo_initialized(&buf->fifo)) {
  32. spin_lock_irqsave(&buf->fifo_lock, flags);
  33. kfifo_reset(&buf->fifo);
  34. spin_unlock_irqrestore(&buf->fifo_lock, flags);
  35. } else
  36. WARN(1, "calling %s on an uninitialized lirc_buffer\n",
  37. __func__);
  38. }
  39. static inline int lirc_buffer_init(struct lirc_buffer *buf,
  40. unsigned int chunk_size,
  41. unsigned int size)
  42. {
  43. int ret;
  44. init_waitqueue_head(&buf->wait_poll);
  45. spin_lock_init(&buf->fifo_lock);
  46. buf->chunk_size = chunk_size;
  47. buf->size = size;
  48. ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL);
  49. return ret;
  50. }
  51. static inline void lirc_buffer_free(struct lirc_buffer *buf)
  52. {
  53. if (kfifo_initialized(&buf->fifo)) {
  54. kfifo_free(&buf->fifo);
  55. } else
  56. WARN(1, "calling %s on an uninitialized lirc_buffer\n",
  57. __func__);
  58. }
  59. static inline int lirc_buffer_len(struct lirc_buffer *buf)
  60. {
  61. int len;
  62. unsigned long flags;
  63. spin_lock_irqsave(&buf->fifo_lock, flags);
  64. len = kfifo_len(&buf->fifo);
  65. spin_unlock_irqrestore(&buf->fifo_lock, flags);
  66. return len;
  67. }
  68. static inline int lirc_buffer_full(struct lirc_buffer *buf)
  69. {
  70. return lirc_buffer_len(buf) == buf->size * buf->chunk_size;
  71. }
  72. static inline int lirc_buffer_empty(struct lirc_buffer *buf)
  73. {
  74. return !lirc_buffer_len(buf);
  75. }
  76. static inline int lirc_buffer_available(struct lirc_buffer *buf)
  77. {
  78. return buf->size - (lirc_buffer_len(buf) / buf->chunk_size);
  79. }
  80. static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf,
  81. unsigned char *dest)
  82. {
  83. unsigned int ret = 0;
  84. if (lirc_buffer_len(buf) >= buf->chunk_size)
  85. ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size,
  86. &buf->fifo_lock);
  87. return ret;
  88. }
  89. static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf,
  90. unsigned char *orig)
  91. {
  92. unsigned int ret;
  93. ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size,
  94. &buf->fifo_lock);
  95. return ret;
  96. }
  97. /**
  98. * struct lirc_driver - Defines the parameters on a LIRC driver
  99. *
  100. * @name: this string will be used for logs
  101. *
  102. * @minor: indicates minor device (/dev/lirc) number for
  103. * registered driver if caller fills it with negative
  104. * value, then the first free minor number will be used
  105. * (if available).
  106. *
  107. * @code_length: length of the remote control key code expressed in bits.
  108. *
  109. * @buffer_size: Number of FIFO buffers with @chunk_size size. If zero,
  110. * creates a buffer with BUFLEN size (16 bytes).
  111. *
  112. * @sample_rate: if zero, the device will wait for an event with a new
  113. * code to be parsed. Otherwise, specifies the sample
  114. * rate for polling. Value should be between 0
  115. * and HZ. If equal to HZ, it would mean one polling per
  116. * second.
  117. *
  118. * @features: lirc compatible hardware features, like LIRC_MODE_RAW,
  119. * LIRC_CAN_*, as defined at include/media/lirc.h.
  120. *
  121. * @chunk_size: Size of each FIFO buffer.
  122. *
  123. * @data: it may point to any driver data and this pointer will
  124. * be passed to all callback functions.
  125. *
  126. * @min_timeout: Minimum timeout for record. Valid only if
  127. * LIRC_CAN_SET_REC_TIMEOUT is defined.
  128. *
  129. * @max_timeout: Maximum timeout for record. Valid only if
  130. * LIRC_CAN_SET_REC_TIMEOUT is defined.
  131. *
  132. * @add_to_buf: add_to_buf will be called after specified period of the
  133. * time or triggered by the external event, this behavior
  134. * depends on value of the sample_rate this function will
  135. * be called in user context. This routine should return
  136. * 0 if data was added to the buffer and -ENODATA if none
  137. * was available. This should add some number of bits
  138. * evenly divisible by code_length to the buffer.
  139. *
  140. * @rbuf: if not NULL, it will be used as a read buffer, you will
  141. * have to write to the buffer by other means, like irq's
  142. * (see also lirc_serial.c).
  143. *
  144. * @set_use_inc: set_use_inc will be called after device is opened
  145. *
  146. * @set_use_dec: set_use_dec will be called after device is closed
  147. *
  148. * @rdev: Pointed to struct rc_dev associated with the LIRC
  149. * device.
  150. *
  151. * @fops: file_operations for drivers which don't fit the current
  152. * driver model.
  153. * Some ioctl's can be directly handled by lirc_dev if the
  154. * driver's ioctl function is NULL or if it returns
  155. * -ENOIOCTLCMD (see also lirc_serial.c).
  156. *
  157. * @dev: pointer to the struct device associated with the LIRC
  158. * device.
  159. *
  160. * @owner: the module owning this struct
  161. */
  162. struct lirc_driver {
  163. char name[40];
  164. int minor;
  165. __u32 code_length;
  166. unsigned int buffer_size; /* in chunks holding one code each */
  167. int sample_rate;
  168. __u32 features;
  169. unsigned int chunk_size;
  170. void *data;
  171. int min_timeout;
  172. int max_timeout;
  173. int (*add_to_buf)(void *data, struct lirc_buffer *buf);
  174. struct lirc_buffer *rbuf;
  175. int (*set_use_inc)(void *data);
  176. void (*set_use_dec)(void *data);
  177. struct rc_dev *rdev;
  178. const struct file_operations *fops;
  179. struct device *dev;
  180. struct module *owner;
  181. };
  182. /* following functions can be called ONLY from user context
  183. *
  184. * returns negative value on error or minor number
  185. * of the registered device if success
  186. * contents of the structure pointed by p is copied
  187. */
  188. extern int lirc_register_driver(struct lirc_driver *d);
  189. /* returns negative value on error or 0 if success
  190. */
  191. extern int lirc_unregister_driver(int minor);
  192. /* Returns the private data stored in the lirc_driver
  193. * associated with the given device file pointer.
  194. */
  195. void *lirc_get_pdata(struct file *file);
  196. /* default file operations
  197. * used by drivers if they override only some operations
  198. */
  199. int lirc_dev_fop_open(struct inode *inode, struct file *file);
  200. int lirc_dev_fop_close(struct inode *inode, struct file *file);
  201. unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait);
  202. long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  203. ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,
  204. loff_t *ppos);
  205. ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
  206. size_t length, loff_t *ppos);
  207. #endif