aboutsummaryrefslogtreecommitdiff
path: root/drivers/isdn/hardware/eicon/dadapter.c
blob: 89497890158d9fad084828551a0d3b1db57b8b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

/*
 *
  Copyright (c) Eicon Networks, 2002.
 *
  This source file is supplied for the use with
  Eicon Networks range of DIVA Server Adapters.
 *
  Eicon File Revision :    2.1
 *
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.
 *
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY OF ANY KIND WHATSOEVER INCLUDING ANY
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the GNU General Public License for more details.
 *
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
#include "platform.h"
#include "pc.h"
#include "debuglib.h"
#include "di_defs.h"
#include "divasync.h"
#include "dadapter.h"
/* --------------------------------------------------------------------------
  Adapter array change notification framework
   -------------------------------------------------------------------------- */
typedef struct _didd_adapter_change_notification {
 didd_adapter_change_callback_t callback;
 void IDI_CALL_ENTITY_T *    context;
} didd_adapter_change_notification_t, \
 * IDI_CALL_ENTITY_T pdidd_adapter_change_notification_t;
#define DIVA_DIDD_MAX_NOTIFICATIONS 256
static didd_adapter_change_notification_t\
              NotificationTable[DIVA_DIDD_MAX_NOTIFICATIONS];
/* --------------------------------------------------------------------------
  Array to held adapter information
   -------------------------------------------------------------------------- */
static DESCRIPTOR  HandleTable[NEW_MAX_DESCRIPTORS];
static dword Adapters = 0; /* Number of adapters */
/* --------------------------------------------------------------------------
  Shadow IDI_DIMAINT
  and 'shadow' debug stuff
   -------------------------------------------------------------------------- */
static void no_printf (unsigned char * format, ...)
{
#ifdef EBUG
	va_list ap;
	va_start (ap, format);
	debug((format, ap));
	va_end (ap);
#endif
}

/* -------------------------------------------------------------------------
  Portable debug Library
  ------------------------------------------------------------------------- */
#include "debuglib.c"
  
static DESCRIPTOR  MAdapter =  {IDI_DIMAINT, /* Adapter Type */
                0x00,     /* Channels */
                0x0000,    /* Features */
                (IDI_CALL)no_printf};
/* --------------------------------------------------------------------------
  DAdapter. Only IDI clients with buffer, that is huge enough to
  get all descriptors will receive information about DAdapter
  { byte type, byte channels, word features, IDI_CALL request }
   -------------------------------------------------------------------------- */
static void IDI_CALL_LINK_T diva_dadapter_request (ENTITY IDI_CALL_ENTITY_T *);
static DESCRIPTOR  DAdapter =  {IDI_DADAPTER, /* Adapter Type */
                0x00,     /* Channels */
                0x0000,    /* Features */
                diva_dadapter_request };
/* --------------------------------------------------------------------------
  LOCALS
   -------------------------------------------------------------------------- */
static dword diva_register_adapter_callback (\
                   didd_adapter_change_callback_t callback,
                   void IDI_CALL_ENTITY_T* context);
static void diva_remove_adapter_callback (dword handle);
static void diva_notify_adapter_change (DESCRIPTOR* d, int removal);
static diva_os_spin_lock_t didd_spin;
/* --------------------------------------------------------------------------
  Should be called as first step, after driver init
  -------------------------------------------------------------------------- */
void diva_didd_load_time_init (void) {
 memset (&HandleTable[0], 0x00, sizeof(HandleTable));
 memset (&NotificationTable[0], 0x00, sizeof(NotificationTable));
 diva_os_initialize_spin_lock (&didd_spin, "didd");
}
/* --------------------------------------------------------------------------
  Should be called as last step, if driver does unload
  -------------------------------------------------------------------------- */
void diva_didd_load_time_finit (void) {
 diva_os_destroy_spin_lock (&didd_spin, "didd");
}
/* --------------------------------------------------------------------------
  Called in order to register new adapter in adapter array
  return adapter handle (> 0) on success
  return -1 adapter array overflow
  -------------------------------------------------------------------------- */
static int diva_didd_add_descriptor (DESCRIPTOR* d) {
 diva_os_spin_lock_magic_t      irql;
 int i;
 if (d->type == IDI_DIMAINT) {
  if (d->request) {
   MAdapter.request = d->request;
   dprintf = (DIVA_DI_PRINTF)d->request;
   diva_notify_adapter_change (&MAdapter, 0); /* Inserted */
   DBG_TRC (("DIMAINT registered, dprintf=%08x", d->request))
  } else {
   DBG_TRC (("DIMAINT removed"))
   diva_notify_adapter_change (&MAdapter, 1); /* About to remove */
   MAdapter.request = (IDI_CALL)no_printf;
   dprintf = no_printf;
  }
  return (NEW_MAX_DESCRIPTORS);
 }
 for (i = 0; i < NEW_MAX_DESCRIPTORS; i++) {
  diva_os_enter_spin_lock (&didd_spin, &irql, "didd_add");
  if (HandleTable[i].type == 0) {
   memcpy (&HandleTable[i], d, sizeof(*d));
   Adapters++;
   diva_os_leave_spin_lock (&didd_spin, &irql, "didd_add");
   diva_notify_adapter_change (d, 0); /* we have new adapter */
   DBG_TRC (("Add adapter[%d], request=%08x", (i+1), d->request))
   return (i+1);
  }
  diva_os_leave_spin_lock (&didd_spin, &irql, "didd_add");
 }
 DBG_ERR (("Can't add adapter, out of resources"))
 return (-1);
}
/* --------------------------------------------------------------------------
  Called in order to remove one registered adapter from array
  return adapter handle (> 0) on success
  return 0 on success
  -------------------------------------------------------------------------- */
static int diva_didd_remove_descriptor (IDI_CALL request) {
 diva_os_spin_lock_magic_t      irql;
 int i;
 if (request == MAdapter.request) {
  DBG_TRC(("DIMAINT removed"))
  dprintf = no_printf;
  diva_notify_adapter_change (&MAdapter, 1); /* About to remove */
  MAdapter.request = (IDI_CALL)no_printf;
  return (0);
 }
 for (i = 0; (Adapters && (i < NEW_MAX_DESCRIPTORS)); i++) {
  if (HandleTable[i].request == request) {
   diva_notify_adapter_change (&HandleTable[i], 1); /* About to remove */
   diva_os_enter_spin_lock (&didd_spin, &irql, "didd_rm");
   memset (&HandleTable[i], 0x00, sizeof(HandleTable[0]));
   Adapters--;
   diva_os_leave_spin_lock (&didd_spin, &irql, "didd_rm");
   DBG_TRC (("Remove adapter[%d], request=%08x", (i+1), request))
   return (0);
  }
 }
 DBG_ERR (("Invalid request=%08x, can't remove adapter", request))
 return (-1);
}
/* --------------------------------------------------------------------------
  Read adapter array
  return 1 if not enough space to save all available adapters
   -------------------------------------------------------------------------- */
static int diva_didd_read_adapter_array (DESCRIPTOR* buffer, int length) {
 diva_os_spin_lock_magic_t      irql;
 int src, dst;
 memset (buffer, 0x00, length);
 length /= sizeof(DESCRIPTOR);
 DBG_TRC (("DIDD_Read, space = %d, Adapters = %d", length, Adapters+2))
 
 diva_os_enter_spin_lock (&didd_spin, &irql, "didd_read");
 for (src = 0, dst = 0;
    (Adapters && (src < NEW_MAX_DESCRIPTORS) && (dst < length));
    src++) {
  if (HandleTable[src].type) {
   memcpy (&buffer[dst], &HandleTable[src], sizeof(DESCRIPTOR));
   dst++;
  }
 }
 diva_os_leave_spin_lock (&didd_spin, &irql, "didd_read");
 if (dst < length) {
  memcpy (&buffer[dst], &MAdapter, sizeof(DESCRIPTOR));
  dst++;
 } else {
  DBG_ERR (("Can't write DIMAINT. Array too small"))
 }
 if (dst < length) {
  memcpy (&buffer[dst], &DAdapter, sizeof(DESCRIPTOR));
  dst++;
 } else {
  DBG_ERR (("Can't write DADAPTER. Array too small"))
 }
 DBG_TRC (("Read %d adapters", dst))
 return (dst == length);
}
/* --------------------------------------------------------------------------
  DAdapter request function.
  This function does process only synchronous requests, and is used
  for reception/registration of new interfaces
   -------------------------------------------------------------------------- */
static void IDI_CALL_LINK_T diva_dadapter_request (\
                      ENTITY IDI_CALL_ENTITY_T *e) {
 IDI_SYNC_REQ *syncReq = (IDI_SYNC_REQ *)e ;
 if (e->Req) { /* We do not process it, also return error */
  e->Rc = OUT_OF_RESOURCES;
  DBG_ERR (("Can't process async request, Req=%02x", e->Req))
  return;
 }
 /*
  So, we process sync request
  */
 switch (e->Rc) {
  case IDI_SYNC_REQ_DIDD_REGISTER_ADAPTER_NOTIFY: {
   diva_didd_adapter_notify_t* pinfo = &syncReq->didd_notify.info;
   pinfo->handle = diva_register_adapter_callback (\
             (didd_adapter_change_callback_t)pinfo->callback,
             (void IDI_CALL_ENTITY_T *)pinfo->context);
   e->Rc = 0xff;
  } break;
  case IDI_SYNC_REQ_DIDD_REMOVE_ADAPTER_NOTIFY: {
   diva_didd_adapter_notify_t* pinfo = &syncReq->didd_notify.info;
   diva_remove_adapter_callback (pinfo->handle);
   e->Rc = 0xff;
  } break;
  case IDI_SYNC_REQ_DIDD_ADD_ADAPTER: {
   diva_didd_add_adapter_t* pinfo = &syncReq->didd_add_adapter.info;
   if (diva_didd_add_descriptor ((DESCRIPTOR*)pinfo->descriptor) < 0) {
    e->Rc = OUT_OF_RESOURCES;
   } else {
    e->Rc = 0xff;
   }
  } break;
  case IDI_SYNC_REQ_DIDD_REMOVE_ADAPTER: {
   diva_didd_remove_adapter_t* pinfo = &syncReq->didd_remove_adapter.info;
   if (diva_didd_remove_descriptor ((IDI_CALL)pinfo->p_request) < 0) {
    e->Rc = OUT_OF_RESOURCES;
   } else {
    e->Rc = 0xff;
   }
  } break;
  case IDI_SYNC_REQ_DIDD_READ_ADAPTER_ARRAY: {
   diva_didd_read_adapter_array_t* pinfo =\
                &syncReq->didd_read_adapter_array.info;
   if (diva_didd_read_adapter_array ((DESCRIPTOR*)pinfo->buffer,
                  (int)pinfo->length)) {
    e->Rc = OUT_OF_RESOURCES;
   } else {
    e->Rc = 0xff;
   }
  } break;
  default:
   DBG_ERR (("Can't process sync request, Req=%02x", e->Rc))
   e->Rc = OUT_OF_RESOURCES;
 }
}
/* --------------------------------------------------------------------------
  IDI client does register his notification function
  -------------------------------------------------------------------------- */
static dword diva_register_adapter_callback (\
                   didd_adapter_change_callback_t callback,
                   void IDI_CALL_ENTITY_T* context) {
 diva_os_spin_lock_magic_t irql;
 dword i;
 
 for (i = 0; i < DIVA_DIDD_MAX_NOTIFICATIONS; i++) {
  diva_os_enter_spin_lock (&didd_spin, &irql, "didd_nfy_add");
  if (!NotificationTable[i].callback) {
   NotificationTable[i].callback = callback;
   NotificationTable[i].context = context;
   diva_os_leave_spin_lock (&didd_spin, &irql, "didd_nfy_add");
   DBG_TRC(("Register adapter notification[%d]=%08x", i+1, callback))
   return (i+1);
  }
  diva_os_leave_spin_lock (&didd_spin, &irql, "didd_nfy_add");
 }
 DBG_ERR (("Can't register adapter notification, overflow"))
 return (0);
}
/* --------------------------------------------------------------------------
  IDI client does register his notification function
  -------------------------------------------------------------------------- */
static void diva_remove_adapter_callback (dword handle) {
 diva_os_spin_lock_magic_t irql;
 if (handle && ((--handle) < DIVA_DIDD_MAX_NOTIFICATIONS)) {
  diva_os_enter_spin_lock (&didd_spin, &irql, "didd_nfy_rm");
  NotificationTable[handle].callback = NULL;
  NotificationTable[handle].context  = NULL;
  diva_os_leave_spin_lock (&didd_spin, &irql, "didd_nfy_rm");
  DBG_TRC(("Remove adapter notification[%d]", (int)(handle+1)))
  return;
 }
 DBG_ERR(("Can't remove adapter notification, handle=%d", handle))
}
/* --------------------------------------------------------------------------
  Notify all client about adapter array change
  Does suppose following behavior in the client side:
  Step 1: Redister Notification
  Step 2: Read Adapter Array
  -------------------------------------------------------------------------- */
static void diva_notify_adapter_change (DESCRIPTOR* d, int removal) {
 int i, do_notify;
 didd_adapter_change_notification_t nfy;
 diva_os_spin_lock_magic_t irql;
 for (i = 0; i < DIVA_DIDD_MAX_NOTIFICATIONS; i++) {
  do_notify = 0;
  diva_os_enter_spin_lock (&didd_spin, &irql, "didd_nfy");
  if (NotificationTable[i].callback) {
   memcpy (&nfy, &NotificationTable[i], sizeof(nfy));
   do_notify = 1;
  }
  diva_os_leave_spin_lock (&didd_spin, &irql, "didd_nfy");
  if (do_notify) {
   (*(nfy.callback))(nfy.context, d, removal);
  }
 }
}
/* --------------------------------------------------------------------------
  For all systems, that are linked by Kernel Mode Linker this is ONLY one
  function thet should be exported by this device driver
  IDI clients should look for IDI_DADAPTER, and use request function
  of this adapter (sync request) in order to receive appropriate services:
  - add new adapter
  - remove existing adapter
  - add adapter array notification
  - remove adapter array notification
  (read adapter is redundant in this case)
  INPUT:
   buffer - pointer to buffer that will receive adapter array
   length  - length (in bytes) of space in buffer
  OUTPUT:
   Adapter array will be written to memory described by 'buffer'
   If the last adapter seen in the returned adapter array is
   IDI_DADAPTER or if last adapter in array does have type '0', then
   it was enougth space in buffer to accommodate all available
   adapter descriptors
  *NOTE 1 (debug interface):
   The IDI adapter of type 'IDI_DIMAINT' does register as 'request'
   famous 'dprintf' function (of type DI_PRINTF, please look
   include/debuglib.c and include/debuglib.h) for details.
   So dprintf is not exported from module debug module directly,
   instead of this IDI_DIMAINT is registered.
   Module load order will receive in this case:
    1. DIDD (this file)
    2. DIMAINT does load and register 'IDI_DIMAINT', at this step
      DIDD should be able to get 'dprintf', save it, and
      register with DIDD by means of 'dprintf' function.
    3. any other driver is loaded and is able to access adapter array
      and debug interface
   This approach does allow to load/unload debug interface on demand,
   and save memory, it it is necessary.
  -------------------------------------------------------------------------- */
void IDI_CALL_LINK_T DIVA_DIDD_Read (void IDI_CALL_ENTITY_T * buffer,
                   int length) {
 diva_didd_read_adapter_array (buffer, length);
}