idifunc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* $Id: idifunc.c,v 1.14.4.4 2004/08/28 20:03:53 armin Exp $
  2. *
  3. * Driver for Eicon DIVA Server ISDN cards.
  4. * User Mode IDI Interface
  5. *
  6. * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
  7. * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. */
  12. #include "platform.h"
  13. #include "di_defs.h"
  14. #include "divasync.h"
  15. #include "um_xdi.h"
  16. #include "um_idi.h"
  17. #define DBG_MINIMUM (DL_LOG + DL_FTL + DL_ERR)
  18. #define DBG_DEFAULT (DBG_MINIMUM + DL_XLOG + DL_REG)
  19. extern char *DRIVERRELEASE_IDI;
  20. extern void DIVA_DIDD_Read(void *, int);
  21. extern int diva_user_mode_idi_create_adapter(const DESCRIPTOR *, int);
  22. extern void diva_user_mode_idi_remove_adapter(int);
  23. static dword notify_handle;
  24. static DESCRIPTOR DAdapter;
  25. static DESCRIPTOR MAdapter;
  26. static void no_printf(unsigned char *x, ...)
  27. {
  28. /* dummy debug function */
  29. }
  30. #include "debuglib.c"
  31. /*
  32. * stop debug
  33. */
  34. static void stop_dbg(void)
  35. {
  36. DbgDeregister();
  37. memset(&MAdapter, 0, sizeof(MAdapter));
  38. dprintf = no_printf;
  39. }
  40. typedef struct _udiva_card {
  41. struct list_head list;
  42. int Id;
  43. DESCRIPTOR d;
  44. } udiva_card;
  45. static LIST_HEAD(cards);
  46. static diva_os_spin_lock_t ll_lock;
  47. /*
  48. * find card in list
  49. */
  50. static udiva_card *find_card_in_list(DESCRIPTOR *d)
  51. {
  52. udiva_card *card;
  53. struct list_head *tmp;
  54. diva_os_spin_lock_magic_t old_irql;
  55. diva_os_enter_spin_lock(&ll_lock, &old_irql, "find card");
  56. list_for_each(tmp, &cards) {
  57. card = list_entry(tmp, udiva_card, list);
  58. if (card->d.request == d->request) {
  59. diva_os_leave_spin_lock(&ll_lock, &old_irql,
  60. "find card");
  61. return (card);
  62. }
  63. }
  64. diva_os_leave_spin_lock(&ll_lock, &old_irql, "find card");
  65. return ((udiva_card *) NULL);
  66. }
  67. /*
  68. * new card
  69. */
  70. static void um_new_card(DESCRIPTOR *d)
  71. {
  72. int adapter_nr = 0;
  73. udiva_card *card = NULL;
  74. IDI_SYNC_REQ sync_req;
  75. diva_os_spin_lock_magic_t old_irql;
  76. if (!(card = diva_os_malloc(0, sizeof(udiva_card)))) {
  77. DBG_ERR(("cannot get buffer for card"));
  78. return;
  79. }
  80. memcpy(&card->d, d, sizeof(DESCRIPTOR));
  81. sync_req.xdi_logical_adapter_number.Req = 0;
  82. sync_req.xdi_logical_adapter_number.Rc =
  83. IDI_SYNC_REQ_XDI_GET_LOGICAL_ADAPTER_NUMBER;
  84. card->d.request((ENTITY *)&sync_req);
  85. adapter_nr =
  86. sync_req.xdi_logical_adapter_number.info.logical_adapter_number;
  87. card->Id = adapter_nr;
  88. if (!(diva_user_mode_idi_create_adapter(d, adapter_nr))) {
  89. diva_os_enter_spin_lock(&ll_lock, &old_irql, "add card");
  90. list_add_tail(&card->list, &cards);
  91. diva_os_leave_spin_lock(&ll_lock, &old_irql, "add card");
  92. } else {
  93. DBG_ERR(("could not create user mode idi card %d",
  94. adapter_nr));
  95. diva_os_free(0, card);
  96. }
  97. }
  98. /*
  99. * remove card
  100. */
  101. static void um_remove_card(DESCRIPTOR *d)
  102. {
  103. diva_os_spin_lock_magic_t old_irql;
  104. udiva_card *card = NULL;
  105. if (!(card = find_card_in_list(d))) {
  106. DBG_ERR(("cannot find card to remove"));
  107. return;
  108. }
  109. diva_user_mode_idi_remove_adapter(card->Id);
  110. diva_os_enter_spin_lock(&ll_lock, &old_irql, "remove card");
  111. list_del(&card->list);
  112. diva_os_leave_spin_lock(&ll_lock, &old_irql, "remove card");
  113. DBG_LOG(("idi proc entry removed for card %d", card->Id));
  114. diva_os_free(0, card);
  115. }
  116. /*
  117. * remove all adapter
  118. */
  119. static void __exit remove_all_idi_proc(void)
  120. {
  121. udiva_card *card;
  122. diva_os_spin_lock_magic_t old_irql;
  123. rescan:
  124. diva_os_enter_spin_lock(&ll_lock, &old_irql, "remove all");
  125. if (!list_empty(&cards)) {
  126. card = list_entry(cards.next, udiva_card, list);
  127. list_del(&card->list);
  128. diva_os_leave_spin_lock(&ll_lock, &old_irql, "remove all");
  129. diva_user_mode_idi_remove_adapter(card->Id);
  130. diva_os_free(0, card);
  131. goto rescan;
  132. }
  133. diva_os_leave_spin_lock(&ll_lock, &old_irql, "remove all");
  134. }
  135. /*
  136. * DIDD notify callback
  137. */
  138. static void *didd_callback(void *context, DESCRIPTOR *adapter,
  139. int removal)
  140. {
  141. if (adapter->type == IDI_DADAPTER) {
  142. DBG_ERR(("Notification about IDI_DADAPTER change ! Oops."));
  143. return (NULL);
  144. } else if (adapter->type == IDI_DIMAINT) {
  145. if (removal) {
  146. stop_dbg();
  147. } else {
  148. memcpy(&MAdapter, adapter, sizeof(MAdapter));
  149. dprintf = (DIVA_DI_PRINTF) MAdapter.request;
  150. DbgRegister("User IDI", DRIVERRELEASE_IDI, DBG_DEFAULT);
  151. }
  152. } else if ((adapter->type > 0) && (adapter->type < 16)) { /* IDI Adapter */
  153. if (removal) {
  154. um_remove_card(adapter);
  155. } else {
  156. um_new_card(adapter);
  157. }
  158. }
  159. return (NULL);
  160. }
  161. /*
  162. * connect DIDD
  163. */
  164. static int __init connect_didd(void)
  165. {
  166. int x = 0;
  167. int dadapter = 0;
  168. IDI_SYNC_REQ req;
  169. DESCRIPTOR DIDD_Table[MAX_DESCRIPTORS];
  170. DIVA_DIDD_Read(DIDD_Table, sizeof(DIDD_Table));
  171. for (x = 0; x < MAX_DESCRIPTORS; x++) {
  172. if (DIDD_Table[x].type == IDI_DADAPTER) { /* DADAPTER found */
  173. dadapter = 1;
  174. memcpy(&DAdapter, &DIDD_Table[x], sizeof(DAdapter));
  175. req.didd_notify.e.Req = 0;
  176. req.didd_notify.e.Rc =
  177. IDI_SYNC_REQ_DIDD_REGISTER_ADAPTER_NOTIFY;
  178. req.didd_notify.info.callback = (void *)didd_callback;
  179. req.didd_notify.info.context = NULL;
  180. DAdapter.request((ENTITY *)&req);
  181. if (req.didd_notify.e.Rc != 0xff) {
  182. stop_dbg();
  183. return (0);
  184. }
  185. notify_handle = req.didd_notify.info.handle;
  186. } else if (DIDD_Table[x].type == IDI_DIMAINT) { /* MAINT found */
  187. memcpy(&MAdapter, &DIDD_Table[x], sizeof(DAdapter));
  188. dprintf = (DIVA_DI_PRINTF) MAdapter.request;
  189. DbgRegister("User IDI", DRIVERRELEASE_IDI, DBG_DEFAULT);
  190. } else if ((DIDD_Table[x].type > 0)
  191. && (DIDD_Table[x].type < 16)) { /* IDI Adapter found */
  192. um_new_card(&DIDD_Table[x]);
  193. }
  194. }
  195. if (!dadapter) {
  196. stop_dbg();
  197. }
  198. return (dadapter);
  199. }
  200. /*
  201. * Disconnect from DIDD
  202. */
  203. static void __exit disconnect_didd(void)
  204. {
  205. IDI_SYNC_REQ req;
  206. stop_dbg();
  207. req.didd_notify.e.Req = 0;
  208. req.didd_notify.e.Rc = IDI_SYNC_REQ_DIDD_REMOVE_ADAPTER_NOTIFY;
  209. req.didd_notify.info.handle = notify_handle;
  210. DAdapter.request((ENTITY *)&req);
  211. }
  212. /*
  213. * init
  214. */
  215. int __init idifunc_init(void)
  216. {
  217. diva_os_initialize_spin_lock(&ll_lock, "idifunc");
  218. if (diva_user_mode_idi_init()) {
  219. DBG_ERR(("init: init failed."));
  220. return (0);
  221. }
  222. if (!connect_didd()) {
  223. diva_user_mode_idi_finit();
  224. DBG_ERR(("init: failed to connect to DIDD."));
  225. return (0);
  226. }
  227. return (1);
  228. }
  229. /*
  230. * finit
  231. */
  232. void __exit idifunc_finit(void)
  233. {
  234. diva_user_mode_idi_finit();
  235. disconnect_didd();
  236. remove_all_idi_proc();
  237. }