lirc_dev.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. u8 fifo_initialized;
  28. };
  29. static inline void lirc_buffer_clear(struct lirc_buffer *buf)
  30. {
  31. unsigned long flags;
  32. if (buf->fifo_initialized) {
  33. spin_lock_irqsave(&buf->fifo_lock, flags);
  34. kfifo_reset(&buf->fifo);
  35. spin_unlock_irqrestore(&buf->fifo_lock, flags);
  36. } else
  37. WARN(1, "calling %s on an uninitialized lirc_buffer\n",
  38. __func__);
  39. }
  40. static inline int lirc_buffer_init(struct lirc_buffer *buf,
  41. unsigned int chunk_size,
  42. unsigned int size)
  43. {
  44. int ret;
  45. init_waitqueue_head(&buf->wait_poll);
  46. spin_lock_init(&buf->fifo_lock);
  47. buf->chunk_size = chunk_size;
  48. buf->size = size;
  49. ret = kfifo_alloc(&buf->fifo, size * chunk_size, GFP_KERNEL);
  50. if (ret == 0)
  51. buf->fifo_initialized = 1;
  52. return ret;
  53. }
  54. static inline void lirc_buffer_free(struct lirc_buffer *buf)
  55. {
  56. if (buf->fifo_initialized) {
  57. kfifo_free(&buf->fifo);
  58. buf->fifo_initialized = 0;
  59. } else
  60. WARN(1, "calling %s on an uninitialized lirc_buffer\n",
  61. __func__);
  62. }
  63. static inline int lirc_buffer_len(struct lirc_buffer *buf)
  64. {
  65. int len;
  66. unsigned long flags;
  67. spin_lock_irqsave(&buf->fifo_lock, flags);
  68. len = kfifo_len(&buf->fifo);
  69. spin_unlock_irqrestore(&buf->fifo_lock, flags);
  70. return len;
  71. }
  72. static inline int lirc_buffer_full(struct lirc_buffer *buf)
  73. {
  74. return lirc_buffer_len(buf) == buf->size * buf->chunk_size;
  75. }
  76. static inline int lirc_buffer_empty(struct lirc_buffer *buf)
  77. {
  78. return !lirc_buffer_len(buf);
  79. }
  80. static inline int lirc_buffer_available(struct lirc_buffer *buf)
  81. {
  82. return buf->size - (lirc_buffer_len(buf) / buf->chunk_size);
  83. }
  84. static inline unsigned int lirc_buffer_read(struct lirc_buffer *buf,
  85. unsigned char *dest)
  86. {
  87. unsigned int ret = 0;
  88. if (lirc_buffer_len(buf) >= buf->chunk_size)
  89. ret = kfifo_out_locked(&buf->fifo, dest, buf->chunk_size,
  90. &buf->fifo_lock);
  91. return ret;
  92. }
  93. static inline unsigned int lirc_buffer_write(struct lirc_buffer *buf,
  94. unsigned char *orig)
  95. {
  96. unsigned int ret;
  97. ret = kfifo_in_locked(&buf->fifo, orig, buf->chunk_size,
  98. &buf->fifo_lock);
  99. return ret;
  100. }
  101. struct lirc_driver {
  102. char name[40];
  103. int minor;
  104. __u32 code_length;
  105. unsigned int buffer_size; /* in chunks holding one code each */
  106. int sample_rate;
  107. __u32 features;
  108. unsigned int chunk_size;
  109. void *data;
  110. int min_timeout;
  111. int max_timeout;
  112. int (*add_to_buf) (void *data, struct lirc_buffer *buf);
  113. struct lirc_buffer *rbuf;
  114. int (*set_use_inc) (void *data);
  115. void (*set_use_dec) (void *data);
  116. const struct file_operations *fops;
  117. struct device *dev;
  118. struct module *owner;
  119. };
  120. /* name:
  121. * this string will be used for logs
  122. *
  123. * minor:
  124. * indicates minor device (/dev/lirc) number for registered driver
  125. * if caller fills it with negative value, then the first free minor
  126. * number will be used (if available)
  127. *
  128. * code_length:
  129. * length of the remote control key code expressed in bits
  130. *
  131. * sample_rate:
  132. *
  133. * data:
  134. * it may point to any driver data and this pointer will be passed to
  135. * all callback functions
  136. *
  137. * add_to_buf:
  138. * add_to_buf will be called after specified period of the time or
  139. * triggered by the external event, this behavior depends on value of
  140. * the sample_rate this function will be called in user context. This
  141. * routine should return 0 if data was added to the buffer and
  142. * -ENODATA if none was available. This should add some number of bits
  143. * evenly divisible by code_length to the buffer
  144. *
  145. * rbuf:
  146. * if not NULL, it will be used as a read buffer, you will have to
  147. * write to the buffer by other means, like irq's (see also
  148. * lirc_serial.c).
  149. *
  150. * set_use_inc:
  151. * set_use_inc will be called after device is opened
  152. *
  153. * set_use_dec:
  154. * set_use_dec will be called after device is closed
  155. *
  156. * fops:
  157. * file_operations for drivers which don't fit the current driver model.
  158. *
  159. * Some ioctl's can be directly handled by lirc_dev if the driver's
  160. * ioctl function is NULL or if it returns -ENOIOCTLCMD (see also
  161. * lirc_serial.c).
  162. *
  163. * owner:
  164. * the module owning this struct
  165. *
  166. */
  167. /* following functions can be called ONLY from user context
  168. *
  169. * returns negative value on error or minor number
  170. * of the registered device if success
  171. * contents of the structure pointed by p is copied
  172. */
  173. extern int lirc_register_driver(struct lirc_driver *d);
  174. /* returns negative value on error or 0 if success
  175. */
  176. extern int lirc_unregister_driver(int minor);
  177. /* Returns the private data stored in the lirc_driver
  178. * associated with the given device file pointer.
  179. */
  180. void *lirc_get_pdata(struct file *file);
  181. /* default file operations
  182. * used by drivers if they override only some operations
  183. */
  184. int lirc_dev_fop_open(struct inode *inode, struct file *file);
  185. int lirc_dev_fop_close(struct inode *inode, struct file *file);
  186. unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait);
  187. long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  188. ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,
  189. loff_t *ppos);
  190. ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
  191. size_t length, loff_t *ppos);
  192. #endif