xbus-core.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Written by Oron Peled <oron@actcom.co.il>
  3. * Copyright (C) 2004-2006, Xorcom
  4. *
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #ifndef XBUS_CORE_H
  23. #define XBUS_CORE_H
  24. #include <linux/wait.h>
  25. #include <linux/interrupt.h> /* for tasklets */
  26. #include <linux/kref.h>
  27. #include "xpd.h"
  28. #include "xframe_queue.h"
  29. #include "xbus-pcm.h"
  30. #define MAX_BUSES 128
  31. #define XFRAME_DATASIZE 512
  32. #define MAX_ENV_STR 40
  33. /* forward declarations */
  34. struct xbus_workqueue;
  35. #ifdef __KERNEL__
  36. struct xbus_ops {
  37. int (*xframe_send_pcm) (xbus_t *xbus, xframe_t *xframe);
  38. int (*xframe_send_cmd) (xbus_t *xbus, xframe_t *xframe);
  39. xframe_t *(*alloc_xframe) (xbus_t *xbus, gfp_t gfp_flags);
  40. void (*free_xframe) (xbus_t *xbus, xframe_t *xframe);
  41. };
  42. /*
  43. * XBUS statistics counters
  44. */
  45. enum {
  46. XBUS_N_UNITS,
  47. XBUS_N_TX_XFRAME_PCM,
  48. XBUS_N_RX_XFRAME_PCM,
  49. XBUS_N_TX_PACK_PCM,
  50. XBUS_N_RX_PACK_PCM,
  51. XBUS_N_TX_BYTES,
  52. XBUS_N_RX_BYTES,
  53. XBUS_N_TX_PCM_FRAG,
  54. XBUS_N_RX_CMD,
  55. XBUS_N_TX_CMD,
  56. };
  57. #define XBUS_COUNTER(xbus, counter) ((xbus)->counters[XBUS_N_ ## counter])
  58. #define C_(x) [ XBUS_N_ ## x ] = { #x }
  59. /* yucky, make an instance so we can size it... */
  60. static struct xbus_counters {
  61. char *name;
  62. } xbus_counters[] = {
  63. C_(UNITS), C_(TX_XFRAME_PCM), C_(RX_XFRAME_PCM), C_(TX_PACK_PCM),
  64. C_(RX_PACK_PCM), C_(TX_BYTES), C_(RX_BYTES),
  65. C_(TX_PCM_FRAG), C_(RX_CMD), C_(TX_CMD),};
  66. #undef C_
  67. #define XBUS_COUNTER_MAX ARRAY_SIZE(xbus_counters)
  68. enum xbus_state {
  69. XBUS_STATE_START,
  70. XBUS_STATE_IDLE,
  71. XBUS_STATE_SENT_REQUEST,
  72. XBUS_STATE_RECVD_DESC,
  73. XBUS_STATE_READY,
  74. XBUS_STATE_DEACTIVATING,
  75. XBUS_STATE_DEACTIVATED,
  76. XBUS_STATE_FAIL,
  77. };
  78. const char *xbus_statename(enum xbus_state st);
  79. struct xbus_transport {
  80. struct xbus_ops *ops;
  81. void *priv;
  82. struct device *transport_device;
  83. ushort max_send_size;
  84. enum xbus_state xbus_state;
  85. unsigned long transport_flags;
  86. spinlock_t state_lock;
  87. atomic_t transport_refcount;
  88. wait_queue_head_t transport_unused;
  89. spinlock_t lock;
  90. char model_string[MAX_ENV_STR];
  91. };
  92. #define MAX_SEND_SIZE(xbus) ((xbus)->transport.max_send_size)
  93. #define XBUS_STATE(xbus) ((xbus)->transport.xbus_state)
  94. #define XBUS_IS(xbus, st) (XBUS_STATE(xbus) == XBUS_STATE_ ## st)
  95. #define TRANSPORT_EXIST(xbus) ((xbus)->transport.ops != NULL)
  96. #define XBUS_FLAG_CONNECTED 0
  97. #define XBUS_FLAGS(xbus, flg) \
  98. test_bit(XBUS_FLAG_ ## flg, &((xbus)->transport.transport_flags))
  99. struct xbus_ops *transportops_get(xbus_t *xbus);
  100. void transportops_put(xbus_t *xbus);
  101. /*
  102. * Encapsulate all poll related data of a single xbus.
  103. */
  104. struct xbus_workqueue {
  105. struct workqueue_struct *wq;
  106. struct work_struct xpds_init_work;
  107. bool xpds_init_done;
  108. struct list_head card_list;
  109. int num_units;
  110. int num_units_initialized;
  111. wait_queue_head_t wait_for_xpd_initialization;
  112. spinlock_t worker_lock;
  113. struct semaphore running_initialization;
  114. };
  115. /*
  116. * Allocate/Free an xframe from pools of empty xframes.
  117. * Calls to {get, put}_xframe are wrapped in
  118. * the macros bellow, so we take/return it
  119. * to the correct pool.
  120. */
  121. xframe_t *get_xframe(struct xframe_queue *q);
  122. void put_xframe(struct xframe_queue *q, xframe_t *xframe);
  123. #define ALLOC_SEND_XFRAME(xbus) \
  124. get_xframe(&(xbus)->send_pool)
  125. #define ALLOC_RECV_XFRAME(xbus) \
  126. get_xframe(&(xbus)->receive_pool)
  127. #define FREE_SEND_XFRAME(xbus, xframe) \
  128. put_xframe(&(xbus)->send_pool, (xframe))
  129. #define FREE_RECV_XFRAME(xbus, xframe) \
  130. put_xframe(&(xbus)->receive_pool, (xframe))
  131. xbus_t *xbus_num(uint num);
  132. xbus_t *get_xbus(const char *msg, uint num);
  133. void put_xbus(const char *msg, xbus_t *xbus);
  134. int refcount_xbus(xbus_t *xbus);
  135. /*
  136. * Echo canceller related data
  137. */
  138. #define ECHO_TIMESLOTS 128
  139. struct echoops {
  140. int (*ec_set) (xpd_t *xpd, int pos, bool on);
  141. int (*ec_get) (xpd_t *xpd, int pos);
  142. int (*ec_update) (xbus_t *xbus);
  143. void (*ec_dump) (xbus_t *xbus);
  144. };
  145. struct xbus_echo_state {
  146. const struct echoops *echoops;
  147. __u8 timeslots[ECHO_TIMESLOTS];
  148. int xpd_idx;
  149. struct device_attribute *da[MAX_XPDS];
  150. };
  151. #define ECHOOPS(xbus) ((xbus)->echo_state.echoops)
  152. #define EC_METHOD(name, xbus) (ECHOOPS(xbus)->name)
  153. #define CALL_EC_METHOD(name, xbus, ...) (EC_METHOD(name, (xbus))(__VA_ARGS__))
  154. /*
  155. * An xbus is a transport layer for Xorcom Protocol commands
  156. */
  157. struct xbus {
  158. char busname[XBUS_NAMELEN]; /* set by xbus_new() */
  159. /* low-level bus drivers set these 2 fields */
  160. char connector[XBUS_DESCLEN];
  161. char label[LABEL_SIZE];
  162. __u8 revision; /* Protocol revision */
  163. struct xbus_transport transport;
  164. struct dahdi_device *ddev;
  165. int num;
  166. struct xpd *xpds[MAX_XPDS];
  167. struct xbus_echo_state echo_state;
  168. int command_tick_counter;
  169. int usec_nosend; /* Firmware flow control */
  170. struct xframe_queue command_queue;
  171. wait_queue_head_t command_queue_empty;
  172. struct xframe_queue send_pool; /* empty xframes for send */
  173. struct xframe_queue receive_pool; /* empty xframes for receive */
  174. /* tasklet processing */
  175. struct xframe_queue receive_queue;
  176. struct tasklet_struct receive_tasklet;
  177. int cpu_rcv_intr[NR_CPUS];
  178. int cpu_rcv_tasklet[NR_CPUS];
  179. struct quirks {
  180. unsigned int has_fxo:1;
  181. unsigned int has_digital_span:1;
  182. } quirks;
  183. bool self_ticking;
  184. enum sync_mode sync_mode;
  185. /* Managed by low-level drivers: */
  186. enum sync_mode sync_mode_default;
  187. struct timer_list command_timer;
  188. unsigned int xbus_frag_count;
  189. struct xframe_queue pcm_tospan;
  190. struct xpp_ticker ticker; /* for tick rate */
  191. struct xpp_drift drift; /* for tick offset */
  192. atomic_t pcm_rx_counter;
  193. unsigned int global_counter;
  194. /* Device-Model */
  195. struct device astribank;
  196. #define dev_to_xbus(dev) container_of(dev, struct xbus, astribank)
  197. struct kref kref;
  198. #define kref_to_xbus(k) container_of(k, struct xbus, kref)
  199. spinlock_t lock;
  200. /* PCM metrics */
  201. struct timeval last_tx_sync;
  202. struct timeval last_rx_sync;
  203. unsigned long max_tx_sync;
  204. unsigned long min_tx_sync;
  205. unsigned long max_rx_sync;
  206. unsigned long min_rx_sync;
  207. unsigned long max_rx_process; /* packet processing time (usec) */
  208. #ifdef SAMPLE_TICKS
  209. #define SAMPLE_SIZE 1000
  210. int sample_ticks[SAMPLE_SIZE];
  211. bool sample_running;
  212. int sample_pos;
  213. #endif
  214. struct xbus_workqueue worker;
  215. /*
  216. * Sync adjustment
  217. */
  218. int sync_adjustment;
  219. int sync_adjustment_offset;
  220. long pll_updated_at;
  221. atomic_t num_xpds;
  222. #ifdef CONFIG_PROC_FS
  223. struct proc_dir_entry *proc_xbus_dir;
  224. struct proc_dir_entry *proc_xbus_summary;
  225. #ifdef PROTOCOL_DEBUG
  226. struct proc_dir_entry *proc_xbus_command;
  227. #endif
  228. #endif
  229. /* statistics */
  230. int counters[XBUS_COUNTER_MAX];
  231. };
  232. #endif
  233. #define XFRAME_MAGIC 123456L
  234. struct xframe {
  235. unsigned long xframe_magic;
  236. struct list_head frame_list;
  237. atomic_t frame_len;
  238. xbus_t *xbus;
  239. struct timeval tv_created;
  240. struct timeval tv_queued;
  241. struct timeval tv_submitted;
  242. struct timeval tv_received;
  243. /* filled by transport layer */
  244. size_t frame_maxlen;
  245. __u8 *packets; /* max XFRAME_DATASIZE */
  246. __u8 *first_free;
  247. int usec_towait; /* prevent overflowing AB */
  248. void *priv;
  249. };
  250. void xframe_init(xbus_t *xbus, xframe_t *xframe, void *buf, size_t maxsize,
  251. void *priv);
  252. #define XFRAME_LEN(frame) atomic_read(&(frame)->frame_len)
  253. int xbus_core_init(void); /* Initializer */
  254. void xbus_core_shutdown(void); /* Terminator */
  255. /* Frame handling */
  256. void dump_xframe(const char msg[], const xbus_t *xbus, const xframe_t *xframe,
  257. int debug);
  258. int send_cmd_frame(xbus_t *xbus, xframe_t *xframe);
  259. /*
  260. * Return pointer to next packet slot in the frame
  261. * or NULL if the frame is full.
  262. */
  263. xpacket_t *xframe_next_packet(xframe_t *xframe, int len);
  264. /* XBUS handling */
  265. /*
  266. * Map: unit+subunit <--> index in xbus->xpds[]
  267. */
  268. #define XPD_IDX(unit, subunit) ((unit) * MAX_SUBUNIT + (subunit))
  269. #define XBUS_UNIT(idx) ((idx) / MAX_SUBUNIT)
  270. #define XBUS_SUBUNIT(idx) ((idx) % MAX_SUBUNIT)
  271. xpd_t *xpd_of(const xbus_t *xbus, int xpd_num);
  272. xpd_t *xpd_byaddr(const xbus_t *xbus, uint unit, uint subunit);
  273. int xbus_check_unique(xbus_t *xbus);
  274. bool xbus_setstate(xbus_t *xbus, enum xbus_state newstate);
  275. bool xbus_setflags(xbus_t *xbus, int flagbit, bool on);
  276. xbus_t *xbus_new(struct xbus_ops *ops, ushort max_send_size,
  277. struct device *transport_device, void *priv);
  278. void xbus_free(xbus_t *xbus);
  279. int xbus_connect(xbus_t *xbus);
  280. int xbus_activate(xbus_t *xbus);
  281. void xbus_deactivate(xbus_t *xbus);
  282. void xbus_disconnect(xbus_t *xbus);
  283. void xbus_receive_xframe(xbus_t *xbus, xframe_t *xframe);
  284. int xbus_process_worker(xbus_t *xbus);
  285. int waitfor_xpds(xbus_t *xbus, char *buf);
  286. int xbus_xpd_bind(xbus_t *xbus, xpd_t *xpd, int unit, int subunit);
  287. int xbus_xpd_unbind(xbus_t *xbus, xpd_t *xpd);
  288. /* sysfs */
  289. int xpd_device_register(xbus_t *xbus, xpd_t *xpd);
  290. void xpd_device_unregister(xpd_t *xpd);
  291. int echocancel_xpd(xpd_t *xpd, int on);
  292. int xbus_is_registered(xbus_t *xbus);
  293. int xbus_register_dahdi_device(xbus_t *xbus);
  294. void xbus_unregister_dahdi_device(xbus_t *xbus);
  295. int xpp_driver_init(void);
  296. void xpp_driver_exit(void);
  297. int xbus_sysfs_transport_create(xbus_t *xbus);
  298. void xbus_sysfs_transport_remove(xbus_t *xbus);
  299. int xbus_sysfs_create(xbus_t *xbus);
  300. void xbus_sysfs_remove(xbus_t *xbus);
  301. void astribank_uevent_send(xbus_t *xbus, enum kobject_action act);
  302. #endif /* XBUS_CORE_H */