dadapter.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. *
  3. Copyright (c) Eicon Networks, 2002.
  4. *
  5. This source file is supplied for the use with
  6. Eicon Networks range of DIVA Server Adapters.
  7. *
  8. Eicon File Revision : 2.1
  9. *
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2, or (at your option)
  13. any later version.
  14. *
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY OF ANY KIND WHATSOEVER INCLUDING ANY
  17. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. See the GNU General Public License for more details.
  19. *
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. */
  25. #include "platform.h"
  26. #include "pc.h"
  27. #include "debuglib.h"
  28. #include "di_defs.h"
  29. #include "divasync.h"
  30. #include "dadapter.h"
  31. /* --------------------------------------------------------------------------
  32. Adapter array change notification framework
  33. -------------------------------------------------------------------------- */
  34. typedef struct _didd_adapter_change_notification {
  35. didd_adapter_change_callback_t callback;
  36. void IDI_CALL_ENTITY_T *context;
  37. } didd_adapter_change_notification_t, \
  38. * IDI_CALL_ENTITY_T pdidd_adapter_change_notification_t;
  39. #define DIVA_DIDD_MAX_NOTIFICATIONS 256
  40. static didd_adapter_change_notification_t \
  41. NotificationTable[DIVA_DIDD_MAX_NOTIFICATIONS];
  42. /* --------------------------------------------------------------------------
  43. Array to held adapter information
  44. -------------------------------------------------------------------------- */
  45. static DESCRIPTOR HandleTable[NEW_MAX_DESCRIPTORS];
  46. static dword Adapters = 0; /* Number of adapters */
  47. /* --------------------------------------------------------------------------
  48. Shadow IDI_DIMAINT
  49. and 'shadow' debug stuff
  50. -------------------------------------------------------------------------- */
  51. static void no_printf(unsigned char *format, ...)
  52. {
  53. #ifdef EBUG
  54. va_list ap;
  55. va_start(ap, format);
  56. debug((format, ap));
  57. va_end(ap);
  58. #endif
  59. }
  60. /* -------------------------------------------------------------------------
  61. Portable debug Library
  62. ------------------------------------------------------------------------- */
  63. #include "debuglib.c"
  64. static DESCRIPTOR MAdapter = {IDI_DIMAINT, /* Adapter Type */
  65. 0x00, /* Channels */
  66. 0x0000, /* Features */
  67. (IDI_CALL)no_printf};
  68. /* --------------------------------------------------------------------------
  69. DAdapter. Only IDI clients with buffer, that is huge enough to
  70. get all descriptors will receive information about DAdapter
  71. { byte type, byte channels, word features, IDI_CALL request }
  72. -------------------------------------------------------------------------- */
  73. static void IDI_CALL_LINK_T diva_dadapter_request(ENTITY IDI_CALL_ENTITY_T *);
  74. static DESCRIPTOR DAdapter = {IDI_DADAPTER, /* Adapter Type */
  75. 0x00, /* Channels */
  76. 0x0000, /* Features */
  77. diva_dadapter_request };
  78. /* --------------------------------------------------------------------------
  79. LOCALS
  80. -------------------------------------------------------------------------- */
  81. static dword diva_register_adapter_callback(\
  82. didd_adapter_change_callback_t callback,
  83. void IDI_CALL_ENTITY_T *context);
  84. static void diva_remove_adapter_callback(dword handle);
  85. static void diva_notify_adapter_change(DESCRIPTOR *d, int removal);
  86. static diva_os_spin_lock_t didd_spin;
  87. /* --------------------------------------------------------------------------
  88. Should be called as first step, after driver init
  89. -------------------------------------------------------------------------- */
  90. void diva_didd_load_time_init(void) {
  91. memset(&HandleTable[0], 0x00, sizeof(HandleTable));
  92. memset(&NotificationTable[0], 0x00, sizeof(NotificationTable));
  93. diva_os_initialize_spin_lock(&didd_spin, "didd");
  94. }
  95. /* --------------------------------------------------------------------------
  96. Should be called as last step, if driver does unload
  97. -------------------------------------------------------------------------- */
  98. void diva_didd_load_time_finit(void) {
  99. diva_os_destroy_spin_lock(&didd_spin, "didd");
  100. }
  101. /* --------------------------------------------------------------------------
  102. Called in order to register new adapter in adapter array
  103. return adapter handle (> 0) on success
  104. return -1 adapter array overflow
  105. -------------------------------------------------------------------------- */
  106. static int diva_didd_add_descriptor(DESCRIPTOR *d) {
  107. diva_os_spin_lock_magic_t irql;
  108. int i;
  109. if (d->type == IDI_DIMAINT) {
  110. if (d->request) {
  111. MAdapter.request = d->request;
  112. dprintf = (DIVA_DI_PRINTF)d->request;
  113. diva_notify_adapter_change(&MAdapter, 0); /* Inserted */
  114. DBG_TRC(("DIMAINT registered, dprintf=%08x", d->request))
  115. } else {
  116. DBG_TRC(("DIMAINT removed"))
  117. diva_notify_adapter_change(&MAdapter, 1); /* About to remove */
  118. MAdapter.request = (IDI_CALL)no_printf;
  119. dprintf = no_printf;
  120. }
  121. return (NEW_MAX_DESCRIPTORS);
  122. }
  123. for (i = 0; i < NEW_MAX_DESCRIPTORS; i++) {
  124. diva_os_enter_spin_lock(&didd_spin, &irql, "didd_add");
  125. if (HandleTable[i].type == 0) {
  126. memcpy(&HandleTable[i], d, sizeof(*d));
  127. Adapters++;
  128. diva_os_leave_spin_lock(&didd_spin, &irql, "didd_add");
  129. diva_notify_adapter_change(d, 0); /* we have new adapter */
  130. DBG_TRC(("Add adapter[%d], request=%08x", (i + 1), d->request))
  131. return (i + 1);
  132. }
  133. diva_os_leave_spin_lock(&didd_spin, &irql, "didd_add");
  134. }
  135. DBG_ERR(("Can't add adapter, out of resources"))
  136. return (-1);
  137. }
  138. /* --------------------------------------------------------------------------
  139. Called in order to remove one registered adapter from array
  140. return adapter handle (> 0) on success
  141. return 0 on success
  142. -------------------------------------------------------------------------- */
  143. static int diva_didd_remove_descriptor(IDI_CALL request) {
  144. diva_os_spin_lock_magic_t irql;
  145. int i;
  146. if (request == MAdapter.request) {
  147. DBG_TRC(("DIMAINT removed"))
  148. dprintf = no_printf;
  149. diva_notify_adapter_change(&MAdapter, 1); /* About to remove */
  150. MAdapter.request = (IDI_CALL)no_printf;
  151. return (0);
  152. }
  153. for (i = 0; (Adapters && (i < NEW_MAX_DESCRIPTORS)); i++) {
  154. if (HandleTable[i].request == request) {
  155. diva_notify_adapter_change(&HandleTable[i], 1); /* About to remove */
  156. diva_os_enter_spin_lock(&didd_spin, &irql, "didd_rm");
  157. memset(&HandleTable[i], 0x00, sizeof(HandleTable[0]));
  158. Adapters--;
  159. diva_os_leave_spin_lock(&didd_spin, &irql, "didd_rm");
  160. DBG_TRC(("Remove adapter[%d], request=%08x", (i + 1), request))
  161. return (0);
  162. }
  163. }
  164. DBG_ERR(("Invalid request=%08x, can't remove adapter", request))
  165. return (-1);
  166. }
  167. /* --------------------------------------------------------------------------
  168. Read adapter array
  169. return 1 if not enough space to save all available adapters
  170. -------------------------------------------------------------------------- */
  171. static int diva_didd_read_adapter_array(DESCRIPTOR *buffer, int length) {
  172. diva_os_spin_lock_magic_t irql;
  173. int src, dst;
  174. memset(buffer, 0x00, length);
  175. length /= sizeof(DESCRIPTOR);
  176. DBG_TRC(("DIDD_Read, space = %d, Adapters = %d", length, Adapters + 2))
  177. diva_os_enter_spin_lock(&didd_spin, &irql, "didd_read");
  178. for (src = 0, dst = 0;
  179. (Adapters && (src < NEW_MAX_DESCRIPTORS) && (dst < length));
  180. src++) {
  181. if (HandleTable[src].type) {
  182. memcpy(&buffer[dst], &HandleTable[src], sizeof(DESCRIPTOR));
  183. dst++;
  184. }
  185. }
  186. diva_os_leave_spin_lock(&didd_spin, &irql, "didd_read");
  187. if (dst < length) {
  188. memcpy(&buffer[dst], &MAdapter, sizeof(DESCRIPTOR));
  189. dst++;
  190. } else {
  191. DBG_ERR(("Can't write DIMAINT. Array too small"))
  192. }
  193. if (dst < length) {
  194. memcpy(&buffer[dst], &DAdapter, sizeof(DESCRIPTOR));
  195. dst++;
  196. } else {
  197. DBG_ERR(("Can't write DADAPTER. Array too small"))
  198. }
  199. DBG_TRC(("Read %d adapters", dst))
  200. return (dst == length);
  201. }
  202. /* --------------------------------------------------------------------------
  203. DAdapter request function.
  204. This function does process only synchronous requests, and is used
  205. for reception/registration of new interfaces
  206. -------------------------------------------------------------------------- */
  207. static void IDI_CALL_LINK_T diva_dadapter_request( \
  208. ENTITY IDI_CALL_ENTITY_T *e) {
  209. IDI_SYNC_REQ *syncReq = (IDI_SYNC_REQ *)e;
  210. if (e->Req) { /* We do not process it, also return error */
  211. e->Rc = OUT_OF_RESOURCES;
  212. DBG_ERR(("Can't process async request, Req=%02x", e->Req))
  213. return;
  214. }
  215. /*
  216. So, we process sync request
  217. */
  218. switch (e->Rc) {
  219. case IDI_SYNC_REQ_DIDD_REGISTER_ADAPTER_NOTIFY: {
  220. diva_didd_adapter_notify_t *pinfo = &syncReq->didd_notify.info;
  221. pinfo->handle = diva_register_adapter_callback( \
  222. (didd_adapter_change_callback_t)pinfo->callback,
  223. (void IDI_CALL_ENTITY_T *)pinfo->context);
  224. e->Rc = 0xff;
  225. } break;
  226. case IDI_SYNC_REQ_DIDD_REMOVE_ADAPTER_NOTIFY: {
  227. diva_didd_adapter_notify_t *pinfo = &syncReq->didd_notify.info;
  228. diva_remove_adapter_callback(pinfo->handle);
  229. e->Rc = 0xff;
  230. } break;
  231. case IDI_SYNC_REQ_DIDD_ADD_ADAPTER: {
  232. diva_didd_add_adapter_t *pinfo = &syncReq->didd_add_adapter.info;
  233. if (diva_didd_add_descriptor((DESCRIPTOR *)pinfo->descriptor) < 0) {
  234. e->Rc = OUT_OF_RESOURCES;
  235. } else {
  236. e->Rc = 0xff;
  237. }
  238. } break;
  239. case IDI_SYNC_REQ_DIDD_REMOVE_ADAPTER: {
  240. diva_didd_remove_adapter_t *pinfo = &syncReq->didd_remove_adapter.info;
  241. if (diva_didd_remove_descriptor((IDI_CALL)pinfo->p_request) < 0) {
  242. e->Rc = OUT_OF_RESOURCES;
  243. } else {
  244. e->Rc = 0xff;
  245. }
  246. } break;
  247. case IDI_SYNC_REQ_DIDD_READ_ADAPTER_ARRAY: {
  248. diva_didd_read_adapter_array_t *pinfo =\
  249. &syncReq->didd_read_adapter_array.info;
  250. if (diva_didd_read_adapter_array((DESCRIPTOR *)pinfo->buffer,
  251. (int)pinfo->length)) {
  252. e->Rc = OUT_OF_RESOURCES;
  253. } else {
  254. e->Rc = 0xff;
  255. }
  256. } break;
  257. default:
  258. DBG_ERR(("Can't process sync request, Req=%02x", e->Rc))
  259. e->Rc = OUT_OF_RESOURCES;
  260. }
  261. }
  262. /* --------------------------------------------------------------------------
  263. IDI client does register his notification function
  264. -------------------------------------------------------------------------- */
  265. static dword diva_register_adapter_callback( \
  266. didd_adapter_change_callback_t callback,
  267. void IDI_CALL_ENTITY_T *context) {
  268. diva_os_spin_lock_magic_t irql;
  269. dword i;
  270. for (i = 0; i < DIVA_DIDD_MAX_NOTIFICATIONS; i++) {
  271. diva_os_enter_spin_lock(&didd_spin, &irql, "didd_nfy_add");
  272. if (!NotificationTable[i].callback) {
  273. NotificationTable[i].callback = callback;
  274. NotificationTable[i].context = context;
  275. diva_os_leave_spin_lock(&didd_spin, &irql, "didd_nfy_add");
  276. DBG_TRC(("Register adapter notification[%d]=%08x", i + 1, callback))
  277. return (i + 1);
  278. }
  279. diva_os_leave_spin_lock(&didd_spin, &irql, "didd_nfy_add");
  280. }
  281. DBG_ERR(("Can't register adapter notification, overflow"))
  282. return (0);
  283. }
  284. /* --------------------------------------------------------------------------
  285. IDI client does register his notification function
  286. -------------------------------------------------------------------------- */
  287. static void diva_remove_adapter_callback(dword handle) {
  288. diva_os_spin_lock_magic_t irql;
  289. if (handle && ((--handle) < DIVA_DIDD_MAX_NOTIFICATIONS)) {
  290. diva_os_enter_spin_lock(&didd_spin, &irql, "didd_nfy_rm");
  291. NotificationTable[handle].callback = NULL;
  292. NotificationTable[handle].context = NULL;
  293. diva_os_leave_spin_lock(&didd_spin, &irql, "didd_nfy_rm");
  294. DBG_TRC(("Remove adapter notification[%d]", (int)(handle + 1)))
  295. return;
  296. }
  297. DBG_ERR(("Can't remove adapter notification, handle=%d", handle))
  298. }
  299. /* --------------------------------------------------------------------------
  300. Notify all client about adapter array change
  301. Does suppose following behavior in the client side:
  302. Step 1: Redister Notification
  303. Step 2: Read Adapter Array
  304. -------------------------------------------------------------------------- */
  305. static void diva_notify_adapter_change(DESCRIPTOR *d, int removal) {
  306. int i, do_notify;
  307. didd_adapter_change_notification_t nfy;
  308. diva_os_spin_lock_magic_t irql;
  309. for (i = 0; i < DIVA_DIDD_MAX_NOTIFICATIONS; i++) {
  310. do_notify = 0;
  311. diva_os_enter_spin_lock(&didd_spin, &irql, "didd_nfy");
  312. if (NotificationTable[i].callback) {
  313. memcpy(&nfy, &NotificationTable[i], sizeof(nfy));
  314. do_notify = 1;
  315. }
  316. diva_os_leave_spin_lock(&didd_spin, &irql, "didd_nfy");
  317. if (do_notify) {
  318. (*(nfy.callback))(nfy.context, d, removal);
  319. }
  320. }
  321. }
  322. /* --------------------------------------------------------------------------
  323. For all systems, that are linked by Kernel Mode Linker this is ONLY one
  324. function thet should be exported by this device driver
  325. IDI clients should look for IDI_DADAPTER, and use request function
  326. of this adapter (sync request) in order to receive appropriate services:
  327. - add new adapter
  328. - remove existing adapter
  329. - add adapter array notification
  330. - remove adapter array notification
  331. (read adapter is redundant in this case)
  332. INPUT:
  333. buffer - pointer to buffer that will receive adapter array
  334. length - length (in bytes) of space in buffer
  335. OUTPUT:
  336. Adapter array will be written to memory described by 'buffer'
  337. If the last adapter seen in the returned adapter array is
  338. IDI_DADAPTER or if last adapter in array does have type '0', then
  339. it was enougth space in buffer to accommodate all available
  340. adapter descriptors
  341. *NOTE 1 (debug interface):
  342. The IDI adapter of type 'IDI_DIMAINT' does register as 'request'
  343. famous 'dprintf' function (of type DI_PRINTF, please look
  344. include/debuglib.c and include/debuglib.h) for details.
  345. So dprintf is not exported from module debug module directly,
  346. instead of this IDI_DIMAINT is registered.
  347. Module load order will receive in this case:
  348. 1. DIDD (this file)
  349. 2. DIMAINT does load and register 'IDI_DIMAINT', at this step
  350. DIDD should be able to get 'dprintf', save it, and
  351. register with DIDD by means of 'dprintf' function.
  352. 3. any other driver is loaded and is able to access adapter array
  353. and debug interface
  354. This approach does allow to load/unload debug interface on demand,
  355. and save memory, it it is necessary.
  356. -------------------------------------------------------------------------- */
  357. void IDI_CALL_LINK_T DIVA_DIDD_Read(void IDI_CALL_ENTITY_T *buffer,
  358. int length) {
  359. diva_didd_read_adapter_array(buffer, length);
  360. }