voicebus.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * VoiceBus(tm) Interface Library.
  3. *
  4. * Written by Shaun Ruffell <sruffell@digium.com>
  5. * and based on previous work by Mark Spencer <markster@digium.com>,
  6. * Matthew Fredrickson <creslin@digium.com>, and
  7. * Michael Spiceland <mspiceland@digium.com>
  8. *
  9. * Copyright (C) 2007-2010 Digium, Inc.
  10. *
  11. * All rights reserved.
  12. *
  13. */
  14. /*
  15. * See http://www.asterisk.org for more information about
  16. * the Asterisk project. Please do not directly contact
  17. * any of the maintainers of this project for assistance;
  18. * the project provides a web site, mailing lists and IRC
  19. * channels for your use.
  20. *
  21. * This program is free software, distributed under the terms of
  22. * the GNU General Public License Version 2 as published by the
  23. * Free Software Foundation. See the LICENSE file included with
  24. * this program for more details.
  25. */
  26. #ifndef __VOICEBUS_H__
  27. #define __VOICEBUS_H__
  28. #include <linux/interrupt.h>
  29. #ifdef VOICEBUS_NET_DEBUG
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #endif
  33. #define VOICEBUS_DEFAULT_LATENCY 3U
  34. #define VOICEBUS_DEFAULT_MAXLATENCY 25U
  35. #define VOICEBUS_MAXLATENCY_BUMP 6U
  36. #define VOICEBUS_SFRAME_SIZE 1004U
  37. /*! The number of descriptors in both the tx and rx descriptor ring. */
  38. #define DRING_SIZE (1 << 7) /* Must be a power of 2 */
  39. #define DRING_MASK (DRING_SIZE-1)
  40. /* Define CONFIG_VOICEBUS_SYSFS to create some attributes under the pci device.
  41. * This is disabled by default because it hasn't been tested on the full range
  42. * of supported kernels. */
  43. #undef CONFIG_VOICEBUS_SYSFS
  44. /* Do not generate interrupts on this interface, but instead just poll it */
  45. #undef CONFIG_VOICEBUS_TIMER
  46. /* Define this in order to create a debugging network interface. */
  47. #undef VOICEBUS_NET_DEBUG
  48. /* Define this to only run the processing in an interrupt handler
  49. * (and not tasklet). */
  50. #define CONFIG_VOICEBUS_INTERRUPT
  51. /* Define this to use a FIFO for the software echocan reference.
  52. * (experimental) */
  53. #undef CONFIG_VOICEBUS_ECREFERENCE
  54. #ifdef CONFIG_VOICEBUS_ECREFERENCE
  55. struct dahdi_fifo;
  56. unsigned int __dahdi_fifo_put(struct dahdi_fifo *fifo, u8 *data, size_t size);
  57. unsigned int __dahdi_fifo_get(struct dahdi_fifo *fifo, u8 *data, size_t size);
  58. void dahdi_fifo_free(struct dahdi_fifo *fifo);
  59. struct dahdi_fifo *dahdi_fifo_alloc(size_t maxsize, gfp_t alloc_flags);
  60. #endif
  61. struct voicebus;
  62. struct vbb {
  63. u8 data[VOICEBUS_SFRAME_SIZE];
  64. struct list_head entry;
  65. };
  66. struct voicebus_operations {
  67. void (*handle_receive)(struct voicebus *vb, struct list_head *buffers);
  68. void (*handle_transmit)(struct voicebus *vb, struct list_head *buffers);
  69. void (*handle_error)(struct voicebus *vb);
  70. };
  71. /**
  72. * struct voicebus_descriptor_list - A single descriptor list.
  73. */
  74. struct voicebus_descriptor_list {
  75. struct voicebus_descriptor *desc;
  76. unsigned int head;
  77. unsigned int tail;
  78. void *pending[DRING_SIZE];
  79. dma_addr_t desc_dma;
  80. atomic_t count;
  81. unsigned int padding;
  82. };
  83. /* Bit definitions for struct voicebus.flags */
  84. #define VOICEBUS_SHUTDOWN 0
  85. #define VOICEBUS_STOP 1
  86. #define VOICEBUS_STOPPED 2
  87. #define VOICEBUS_LATENCY_LOCKED 3
  88. #define VOICEBUS_HARD_UNDERRUN 4
  89. /**
  90. * voicebus_mode
  91. *
  92. * NORMAL: For non-hx8 boards. Uses idle_buffers.
  93. * BOOT: For hx8 boards. For sending single packets at a time.
  94. * RELAXED: Normal operating mode for Hx8 Boards. Does not use
  95. * idle_buffers.
  96. */
  97. enum voicebus_mode {
  98. NORMAL = 0,
  99. BOOT = 1,
  100. HX8 = 2,
  101. };
  102. /**
  103. * struct voicebus - Represents physical interface to voicebus card.
  104. *
  105. * @tx_complete: only used in the tasklet to temporarily hold complete tx
  106. * buffers.
  107. */
  108. struct voicebus {
  109. struct pci_dev *pdev;
  110. spinlock_t lock;
  111. struct voicebus_descriptor_list rxd;
  112. struct voicebus_descriptor_list txd;
  113. u8 *idle_vbb;
  114. dma_addr_t idle_vbb_dma_addr;
  115. const int *debug;
  116. void __iomem *iobase;
  117. struct tasklet_struct tasklet;
  118. enum voicebus_mode mode;
  119. #if defined(CONFIG_VOICEBUS_INTERRUPT)
  120. atomic_t deferred_disabled_count;
  121. #endif
  122. #if defined(CONFIG_VOICEBUS_TIMER)
  123. struct timer_list timer;
  124. #endif
  125. struct work_struct underrun_work;
  126. const struct voicebus_operations *ops;
  127. unsigned long flags;
  128. unsigned int min_tx_buffer_count;
  129. unsigned int max_latency;
  130. struct list_head tx_complete;
  131. struct list_head free_rx;
  132. #ifdef VOICEBUS_NET_DEBUG
  133. struct sk_buff_head captured_packets;
  134. struct net_device *netdev;
  135. struct net_device_stats net_stats;
  136. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
  137. struct napi_struct napi;
  138. #endif
  139. atomic_t tx_seqnum;
  140. atomic_t rx_seqnum;
  141. #endif
  142. };
  143. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  144. extern kmem_cache_t *voicebus_vbb_cache;
  145. #else
  146. extern struct kmem_cache *voicebus_vbb_cache;
  147. #endif
  148. int __voicebus_init(struct voicebus *vb, const char *board_name,
  149. enum voicebus_mode mode);
  150. void voicebus_release(struct voicebus *vb);
  151. int voicebus_start(struct voicebus *vb);
  152. void voicebus_stop(struct voicebus *vb);
  153. int voicebus_transmit(struct voicebus *vb, struct vbb *vbb);
  154. int voicebus_set_minlatency(struct voicebus *vb, unsigned int milliseconds);
  155. int voicebus_current_latency(struct voicebus *vb);
  156. static inline int voicebus_init(struct voicebus *vb, const char *board_name)
  157. {
  158. return __voicebus_init(vb, board_name, NORMAL);
  159. }
  160. static inline int
  161. voicebus_boot_init(struct voicebus *vb, const char *board_name)
  162. {
  163. return __voicebus_init(vb, board_name, BOOT);
  164. }
  165. /**
  166. * voicebus_lock_latency() - Do not increase the latency during underruns.
  167. *
  168. */
  169. static inline void voicebus_lock_latency(struct voicebus *vb)
  170. {
  171. set_bit(VOICEBUS_LATENCY_LOCKED, &vb->flags);
  172. }
  173. /**
  174. * voicebus_unlock_latency() - Bump up the latency during underruns.
  175. *
  176. */
  177. static inline void voicebus_unlock_latency(struct voicebus *vb)
  178. {
  179. clear_bit(VOICEBUS_LATENCY_LOCKED, &vb->flags);
  180. }
  181. /**
  182. * voicebus_is_latency_locked() - Return 1 if latency is currently locked.
  183. *
  184. */
  185. static inline int voicebus_is_latency_locked(const struct voicebus *vb)
  186. {
  187. return test_bit(VOICEBUS_LATENCY_LOCKED, &vb->flags);
  188. }
  189. static inline void voicebus_set_normal_mode(struct voicebus *vb)
  190. {
  191. vb->mode = NORMAL;
  192. }
  193. static inline void voicebus_set_hx8_mode(struct voicebus *vb)
  194. {
  195. vb->mode = HX8;
  196. }
  197. /**
  198. * voicebus_set_max_latency() - Set the maximum number of milliseconds the latency will be able to grow to.
  199. */
  200. static inline void
  201. voicebus_set_maxlatency(struct voicebus *vb, unsigned int max_latency)
  202. {
  203. unsigned long flags;
  204. spin_lock_irqsave(&vb->lock, flags);
  205. vb->max_latency = clamp(max_latency,
  206. vb->min_tx_buffer_count,
  207. VOICEBUS_DEFAULT_MAXLATENCY);
  208. spin_unlock_irqrestore(&vb->lock, flags);
  209. }
  210. #endif /* __VOICEBUS_H__ */