lirc_dev.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. struct lirc_driver {
  98. char name[40];
  99. int minor;
  100. __u32 code_length;
  101. unsigned int buffer_size; /* in chunks holding one code each */
  102. int sample_rate;
  103. __u32 features;
  104. unsigned int chunk_size;
  105. void *data;
  106. int min_timeout;
  107. int max_timeout;
  108. int (*add_to_buf) (void *data, struct lirc_buffer *buf);
  109. struct lirc_buffer *rbuf;
  110. int (*set_use_inc) (void *data);
  111. void (*set_use_dec) (void *data);
  112. struct rc_dev *rdev;
  113. const struct file_operations *fops;
  114. struct device *dev;
  115. struct module *owner;
  116. };
  117. /* name:
  118. * this string will be used for logs
  119. *
  120. * minor:
  121. * indicates minor device (/dev/lirc) number for registered driver
  122. * if caller fills it with negative value, then the first free minor
  123. * number will be used (if available)
  124. *
  125. * code_length:
  126. * length of the remote control key code expressed in bits
  127. *
  128. * sample_rate:
  129. *
  130. * data:
  131. * it may point to any driver data and this pointer will be passed to
  132. * all callback functions
  133. *
  134. * add_to_buf:
  135. * add_to_buf will be called after specified period of the time or
  136. * triggered by the external event, this behavior depends on value of
  137. * the sample_rate this function will be called in user context. This
  138. * routine should return 0 if data was added to the buffer and
  139. * -ENODATA if none was available. This should add some number of bits
  140. * evenly divisible by code_length to the buffer
  141. *
  142. * rbuf:
  143. * if not NULL, it will be used as a read buffer, you will have to
  144. * write to the buffer by other means, like irq's (see also
  145. * lirc_serial.c).
  146. *
  147. * set_use_inc:
  148. * set_use_inc will be called after device is opened
  149. *
  150. * set_use_dec:
  151. * set_use_dec will be called after device is closed
  152. *
  153. * fops:
  154. * file_operations for drivers which don't fit the current driver model.
  155. *
  156. * Some ioctl's can be directly handled by lirc_dev if the driver's
  157. * ioctl function is NULL or if it returns -ENOIOCTLCMD (see also
  158. * lirc_serial.c).
  159. *
  160. * owner:
  161. * the module owning this struct
  162. *
  163. */
  164. /* following functions can be called ONLY from user context
  165. *
  166. * returns negative value on error or minor number
  167. * of the registered device if success
  168. * contents of the structure pointed by p is copied
  169. */
  170. extern int lirc_register_driver(struct lirc_driver *d);
  171. /* returns negative value on error or 0 if success
  172. */
  173. extern int lirc_unregister_driver(int minor);
  174. /* Returns the private data stored in the lirc_driver
  175. * associated with the given device file pointer.
  176. */
  177. void *lirc_get_pdata(struct file *file);
  178. /* default file operations
  179. * used by drivers if they override only some operations
  180. */
  181. int lirc_dev_fop_open(struct inode *inode, struct file *file);
  182. int lirc_dev_fop_close(struct inode *inode, struct file *file);
  183. unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait);
  184. long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  185. ssize_t lirc_dev_fop_read(struct file *file, char __user *buffer, size_t length,
  186. loff_t *ppos);
  187. ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
  188. size_t length, loff_t *ppos);
  189. #endif