dadapter.c 14.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *
3
 Copyright (c) Eicon Networks, 2002.
L
Linus Torvalds 已提交
4
 *
5 6
 This source file is supplied for the use with
 Eicon Networks range of DIVA Server Adapters.
L
Linus Torvalds 已提交
7
 *
8
 Eicon File Revision :    2.1
L
Linus Torvalds 已提交
9
 *
10 11 12 13
 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.
L
Linus Torvalds 已提交
14
 *
15 16 17 18
 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.
L
Linus Torvalds 已提交
19
 *
20 21 22
 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.
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31
 *
 */
#include "platform.h"
#include "pc.h"
#include "debuglib.h"
#include "di_defs.h"
#include "divasync.h"
#include "dadapter.h"
/* --------------------------------------------------------------------------
32
   Adapter array change notification framework
L
Linus Torvalds 已提交
33 34
   -------------------------------------------------------------------------- */
typedef struct _didd_adapter_change_notification {
35 36 37 38
	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;
L
Linus Torvalds 已提交
39
#define DIVA_DIDD_MAX_NOTIFICATIONS 256
40 41
static didd_adapter_change_notification_t	\
NotificationTable[DIVA_DIDD_MAX_NOTIFICATIONS];
L
Linus Torvalds 已提交
42
/* --------------------------------------------------------------------------
43
   Array to held adapter information
L
Linus Torvalds 已提交
44 45
   -------------------------------------------------------------------------- */
static DESCRIPTOR  HandleTable[NEW_MAX_DESCRIPTORS];
46
static dword Adapters = 0; /* Number of adapters */
L
Linus Torvalds 已提交
47
/* --------------------------------------------------------------------------
48 49
   Shadow IDI_DIMAINT
   and 'shadow' debug stuff
L
Linus Torvalds 已提交
50
   -------------------------------------------------------------------------- */
51
static void no_printf(unsigned char *format, ...)
L
Linus Torvalds 已提交
52 53 54
{
#ifdef EBUG
	va_list ap;
55
	va_start(ap, format);
L
Linus Torvalds 已提交
56
	debug((format, ap));
57
	va_end(ap);
L
Linus Torvalds 已提交
58 59 60 61
#endif
}

/* -------------------------------------------------------------------------
62 63
   Portable debug Library
   ------------------------------------------------------------------------- */
L
Linus Torvalds 已提交
64
#include "debuglib.c"
65

L
Linus Torvalds 已提交
66
static DESCRIPTOR  MAdapter =  {IDI_DIMAINT, /* Adapter Type */
67 68 69
				0x00,     /* Channels */
				0x0000,    /* Features */
				(IDI_CALL)no_printf};
L
Linus Torvalds 已提交
70
/* --------------------------------------------------------------------------
71 72 73
   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 }
L
Linus Torvalds 已提交
74
   -------------------------------------------------------------------------- */
75
static void IDI_CALL_LINK_T diva_dadapter_request(ENTITY IDI_CALL_ENTITY_T *);
L
Linus Torvalds 已提交
76
static DESCRIPTOR  DAdapter =  {IDI_DADAPTER, /* Adapter Type */
77 78 79
				0x00,     /* Channels */
				0x0000,    /* Features */
				diva_dadapter_request };
L
Linus Torvalds 已提交
80
/* --------------------------------------------------------------------------
81
   LOCALS
L
Linus Torvalds 已提交
82
   -------------------------------------------------------------------------- */
83 84 85 86 87
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);
L
Linus Torvalds 已提交
88 89
static diva_os_spin_lock_t didd_spin;
/* --------------------------------------------------------------------------
90 91 92 93 94 95
   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");
L
Linus Torvalds 已提交
96 97
}
/* --------------------------------------------------------------------------
98 99 100 101
   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");
L
Linus Torvalds 已提交
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
   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);
L
Linus Torvalds 已提交
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
   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);
L
Linus Torvalds 已提交
168 169
}
/* --------------------------------------------------------------------------
170 171
   Read adapter array
   return 1 if not enough space to save all available adapters
L
Linus Torvalds 已提交
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
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);
L
Linus Torvalds 已提交
204 205
}
/* --------------------------------------------------------------------------
206 207 208
   DAdapter request function.
   This function does process only synchronous requests, and is used
   for reception/registration of new interfaces
L
Linus Torvalds 已提交
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
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;
	}
L
Linus Torvalds 已提交
264 265
}
/* --------------------------------------------------------------------------
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
   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);
L
Linus Torvalds 已提交
287 288
}
/* --------------------------------------------------------------------------
289 290 291 292 293 294 295 296 297 298 299 300 301 302
   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))
		}
L
Linus Torvalds 已提交
303
/* --------------------------------------------------------------------------
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
   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);
		}
	}
L
Linus Torvalds 已提交
325 326
}
/* --------------------------------------------------------------------------
327 328 329 330 331 332 333 334 335 336
   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:
L
Linus Torvalds 已提交
337 338
   buffer - pointer to buffer that will receive adapter array
   length  - length (in bytes) of space in buffer
339
   OUTPUT:
L
Linus Torvalds 已提交
340 341 342 343 344
   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
345
   *NOTE 1 (debug interface):
L
Linus Torvalds 已提交
346 347 348 349 350 351
   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:
352 353 354 355 356 357
   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
L
Linus Torvalds 已提交
358 359
   This approach does allow to load/unload debug interface on demand,
   and save memory, it it is necessary.
360 361 362 363
   -------------------------------------------------------------------------- */
void IDI_CALL_LINK_T DIVA_DIDD_Read(void IDI_CALL_ENTITY_T *buffer,
				    int length) {
	diva_didd_read_adapter_array(buffer, length);
L
Linus Torvalds 已提交
364
}