Path: blob/master/drivers/isdn/hardware/eicon/dadapter.c
15115 views
1/*2*3Copyright (c) Eicon Networks, 2002.4*5This source file is supplied for the use with6Eicon Networks range of DIVA Server Adapters.7*8Eicon File Revision : 2.19*10This program is free software; you can redistribute it and/or modify11it under the terms of the GNU General Public License as published by12the Free Software Foundation; either version 2, or (at your option)13any later version.14*15This program is distributed in the hope that it will be useful,16but WITHOUT ANY WARRANTY OF ANY KIND WHATSOEVER INCLUDING ANY17implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.18See the GNU General Public License for more details.19*20You should have received a copy of the GNU General Public License21along with this program; if not, write to the Free Software22Foundation, 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/* --------------------------------------------------------------------------32Adapter array change notification framework33-------------------------------------------------------------------------- */34typedef struct _didd_adapter_change_notification {35didd_adapter_change_callback_t callback;36void 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 25640static didd_adapter_change_notification_t\41NotificationTable[DIVA_DIDD_MAX_NOTIFICATIONS];42/* --------------------------------------------------------------------------43Array to held adapter information44-------------------------------------------------------------------------- */45static DESCRIPTOR HandleTable[NEW_MAX_DESCRIPTORS];46static dword Adapters = 0; /* Number of adapters */47/* --------------------------------------------------------------------------48Shadow IDI_DIMAINT49and 'shadow' debug stuff50-------------------------------------------------------------------------- */51static void no_printf (unsigned char * format, ...)52{53#ifdef EBUG54va_list ap;55va_start (ap, format);56debug((format, ap));57va_end (ap);58#endif59}6061/* -------------------------------------------------------------------------62Portable debug Library63------------------------------------------------------------------------- */64#include "debuglib.c"6566static DESCRIPTOR MAdapter = {IDI_DIMAINT, /* Adapter Type */670x00, /* Channels */680x0000, /* Features */69(IDI_CALL)no_printf};70/* --------------------------------------------------------------------------71DAdapter. Only IDI clients with buffer, that is huge enough to72get all descriptors will receive information about DAdapter73{ byte type, byte channels, word features, IDI_CALL request }74-------------------------------------------------------------------------- */75static void IDI_CALL_LINK_T diva_dadapter_request (ENTITY IDI_CALL_ENTITY_T *);76static DESCRIPTOR DAdapter = {IDI_DADAPTER, /* Adapter Type */770x00, /* Channels */780x0000, /* Features */79diva_dadapter_request };80/* --------------------------------------------------------------------------81LOCALS82-------------------------------------------------------------------------- */83static dword diva_register_adapter_callback (\84didd_adapter_change_callback_t callback,85void IDI_CALL_ENTITY_T* context);86static void diva_remove_adapter_callback (dword handle);87static void diva_notify_adapter_change (DESCRIPTOR* d, int removal);88static diva_os_spin_lock_t didd_spin;89/* --------------------------------------------------------------------------90Should be called as first step, after driver init91-------------------------------------------------------------------------- */92void diva_didd_load_time_init (void) {93memset (&HandleTable[0], 0x00, sizeof(HandleTable));94memset (&NotificationTable[0], 0x00, sizeof(NotificationTable));95diva_os_initialize_spin_lock (&didd_spin, "didd");96}97/* --------------------------------------------------------------------------98Should be called as last step, if driver does unload99-------------------------------------------------------------------------- */100void diva_didd_load_time_finit (void) {101diva_os_destroy_spin_lock (&didd_spin, "didd");102}103/* --------------------------------------------------------------------------104Called in order to register new adapter in adapter array105return adapter handle (> 0) on success106return -1 adapter array overflow107-------------------------------------------------------------------------- */108static int diva_didd_add_descriptor (DESCRIPTOR* d) {109diva_os_spin_lock_magic_t irql;110int i;111if (d->type == IDI_DIMAINT) {112if (d->request) {113MAdapter.request = d->request;114dprintf = (DIVA_DI_PRINTF)d->request;115diva_notify_adapter_change (&MAdapter, 0); /* Inserted */116DBG_TRC (("DIMAINT registered, dprintf=%08x", d->request))117} else {118DBG_TRC (("DIMAINT removed"))119diva_notify_adapter_change (&MAdapter, 1); /* About to remove */120MAdapter.request = (IDI_CALL)no_printf;121dprintf = no_printf;122}123return (NEW_MAX_DESCRIPTORS);124}125for (i = 0; i < NEW_MAX_DESCRIPTORS; i++) {126diva_os_enter_spin_lock (&didd_spin, &irql, "didd_add");127if (HandleTable[i].type == 0) {128memcpy (&HandleTable[i], d, sizeof(*d));129Adapters++;130diva_os_leave_spin_lock (&didd_spin, &irql, "didd_add");131diva_notify_adapter_change (d, 0); /* we have new adapter */132DBG_TRC (("Add adapter[%d], request=%08x", (i+1), d->request))133return (i+1);134}135diva_os_leave_spin_lock (&didd_spin, &irql, "didd_add");136}137DBG_ERR (("Can't add adapter, out of resources"))138return (-1);139}140/* --------------------------------------------------------------------------141Called in order to remove one registered adapter from array142return adapter handle (> 0) on success143return 0 on success144-------------------------------------------------------------------------- */145static int diva_didd_remove_descriptor (IDI_CALL request) {146diva_os_spin_lock_magic_t irql;147int i;148if (request == MAdapter.request) {149DBG_TRC(("DIMAINT removed"))150dprintf = no_printf;151diva_notify_adapter_change (&MAdapter, 1); /* About to remove */152MAdapter.request = (IDI_CALL)no_printf;153return (0);154}155for (i = 0; (Adapters && (i < NEW_MAX_DESCRIPTORS)); i++) {156if (HandleTable[i].request == request) {157diva_notify_adapter_change (&HandleTable[i], 1); /* About to remove */158diva_os_enter_spin_lock (&didd_spin, &irql, "didd_rm");159memset (&HandleTable[i], 0x00, sizeof(HandleTable[0]));160Adapters--;161diva_os_leave_spin_lock (&didd_spin, &irql, "didd_rm");162DBG_TRC (("Remove adapter[%d], request=%08x", (i+1), request))163return (0);164}165}166DBG_ERR (("Invalid request=%08x, can't remove adapter", request))167return (-1);168}169/* --------------------------------------------------------------------------170Read adapter array171return 1 if not enough space to save all available adapters172-------------------------------------------------------------------------- */173static int diva_didd_read_adapter_array (DESCRIPTOR* buffer, int length) {174diva_os_spin_lock_magic_t irql;175int src, dst;176memset (buffer, 0x00, length);177length /= sizeof(DESCRIPTOR);178DBG_TRC (("DIDD_Read, space = %d, Adapters = %d", length, Adapters+2))179180diva_os_enter_spin_lock (&didd_spin, &irql, "didd_read");181for (src = 0, dst = 0;182(Adapters && (src < NEW_MAX_DESCRIPTORS) && (dst < length));183src++) {184if (HandleTable[src].type) {185memcpy (&buffer[dst], &HandleTable[src], sizeof(DESCRIPTOR));186dst++;187}188}189diva_os_leave_spin_lock (&didd_spin, &irql, "didd_read");190if (dst < length) {191memcpy (&buffer[dst], &MAdapter, sizeof(DESCRIPTOR));192dst++;193} else {194DBG_ERR (("Can't write DIMAINT. Array too small"))195}196if (dst < length) {197memcpy (&buffer[dst], &DAdapter, sizeof(DESCRIPTOR));198dst++;199} else {200DBG_ERR (("Can't write DADAPTER. Array too small"))201}202DBG_TRC (("Read %d adapters", dst))203return (dst == length);204}205/* --------------------------------------------------------------------------206DAdapter request function.207This function does process only synchronous requests, and is used208for reception/registration of new interfaces209-------------------------------------------------------------------------- */210static void IDI_CALL_LINK_T diva_dadapter_request (\211ENTITY IDI_CALL_ENTITY_T *e) {212IDI_SYNC_REQ *syncReq = (IDI_SYNC_REQ *)e ;213if (e->Req) { /* We do not process it, also return error */214e->Rc = OUT_OF_RESOURCES;215DBG_ERR (("Can't process async request, Req=%02x", e->Req))216return;217}218/*219So, we process sync request220*/221switch (e->Rc) {222case IDI_SYNC_REQ_DIDD_REGISTER_ADAPTER_NOTIFY: {223diva_didd_adapter_notify_t* pinfo = &syncReq->didd_notify.info;224pinfo->handle = diva_register_adapter_callback (\225(didd_adapter_change_callback_t)pinfo->callback,226(void IDI_CALL_ENTITY_T *)pinfo->context);227e->Rc = 0xff;228} break;229case IDI_SYNC_REQ_DIDD_REMOVE_ADAPTER_NOTIFY: {230diva_didd_adapter_notify_t* pinfo = &syncReq->didd_notify.info;231diva_remove_adapter_callback (pinfo->handle);232e->Rc = 0xff;233} break;234case IDI_SYNC_REQ_DIDD_ADD_ADAPTER: {235diva_didd_add_adapter_t* pinfo = &syncReq->didd_add_adapter.info;236if (diva_didd_add_descriptor ((DESCRIPTOR*)pinfo->descriptor) < 0) {237e->Rc = OUT_OF_RESOURCES;238} else {239e->Rc = 0xff;240}241} break;242case IDI_SYNC_REQ_DIDD_REMOVE_ADAPTER: {243diva_didd_remove_adapter_t* pinfo = &syncReq->didd_remove_adapter.info;244if (diva_didd_remove_descriptor ((IDI_CALL)pinfo->p_request) < 0) {245e->Rc = OUT_OF_RESOURCES;246} else {247e->Rc = 0xff;248}249} break;250case IDI_SYNC_REQ_DIDD_READ_ADAPTER_ARRAY: {251diva_didd_read_adapter_array_t* pinfo =\252&syncReq->didd_read_adapter_array.info;253if (diva_didd_read_adapter_array ((DESCRIPTOR*)pinfo->buffer,254(int)pinfo->length)) {255e->Rc = OUT_OF_RESOURCES;256} else {257e->Rc = 0xff;258}259} break;260default:261DBG_ERR (("Can't process sync request, Req=%02x", e->Rc))262e->Rc = OUT_OF_RESOURCES;263}264}265/* --------------------------------------------------------------------------266IDI client does register his notification function267-------------------------------------------------------------------------- */268static dword diva_register_adapter_callback (\269didd_adapter_change_callback_t callback,270void IDI_CALL_ENTITY_T* context) {271diva_os_spin_lock_magic_t irql;272dword i;273274for (i = 0; i < DIVA_DIDD_MAX_NOTIFICATIONS; i++) {275diva_os_enter_spin_lock (&didd_spin, &irql, "didd_nfy_add");276if (!NotificationTable[i].callback) {277NotificationTable[i].callback = callback;278NotificationTable[i].context = context;279diva_os_leave_spin_lock (&didd_spin, &irql, "didd_nfy_add");280DBG_TRC(("Register adapter notification[%d]=%08x", i+1, callback))281return (i+1);282}283diva_os_leave_spin_lock (&didd_spin, &irql, "didd_nfy_add");284}285DBG_ERR (("Can't register adapter notification, overflow"))286return (0);287}288/* --------------------------------------------------------------------------289IDI client does register his notification function290-------------------------------------------------------------------------- */291static void diva_remove_adapter_callback (dword handle) {292diva_os_spin_lock_magic_t irql;293if (handle && ((--handle) < DIVA_DIDD_MAX_NOTIFICATIONS)) {294diva_os_enter_spin_lock (&didd_spin, &irql, "didd_nfy_rm");295NotificationTable[handle].callback = NULL;296NotificationTable[handle].context = NULL;297diva_os_leave_spin_lock (&didd_spin, &irql, "didd_nfy_rm");298DBG_TRC(("Remove adapter notification[%d]", (int)(handle+1)))299return;300}301DBG_ERR(("Can't remove adapter notification, handle=%d", handle))302}303/* --------------------------------------------------------------------------304Notify all client about adapter array change305Does suppose following behavior in the client side:306Step 1: Redister Notification307Step 2: Read Adapter Array308-------------------------------------------------------------------------- */309static void diva_notify_adapter_change (DESCRIPTOR* d, int removal) {310int i, do_notify;311didd_adapter_change_notification_t nfy;312diva_os_spin_lock_magic_t irql;313for (i = 0; i < DIVA_DIDD_MAX_NOTIFICATIONS; i++) {314do_notify = 0;315diva_os_enter_spin_lock (&didd_spin, &irql, "didd_nfy");316if (NotificationTable[i].callback) {317memcpy (&nfy, &NotificationTable[i], sizeof(nfy));318do_notify = 1;319}320diva_os_leave_spin_lock (&didd_spin, &irql, "didd_nfy");321if (do_notify) {322(*(nfy.callback))(nfy.context, d, removal);323}324}325}326/* --------------------------------------------------------------------------327For all systems, that are linked by Kernel Mode Linker this is ONLY one328function thet should be exported by this device driver329IDI clients should look for IDI_DADAPTER, and use request function330of this adapter (sync request) in order to receive appropriate services:331- add new adapter332- remove existing adapter333- add adapter array notification334- remove adapter array notification335(read adapter is redundant in this case)336INPUT:337buffer - pointer to buffer that will receive adapter array338length - length (in bytes) of space in buffer339OUTPUT:340Adapter array will be written to memory described by 'buffer'341If the last adapter seen in the returned adapter array is342IDI_DADAPTER or if last adapter in array does have type '0', then343it was enougth space in buffer to accommodate all available344adapter descriptors345*NOTE 1 (debug interface):346The IDI adapter of type 'IDI_DIMAINT' does register as 'request'347famous 'dprintf' function (of type DI_PRINTF, please look348include/debuglib.c and include/debuglib.h) for details.349So dprintf is not exported from module debug module directly,350instead of this IDI_DIMAINT is registered.351Module load order will receive in this case:3521. DIDD (this file)3532. DIMAINT does load and register 'IDI_DIMAINT', at this step354DIDD should be able to get 'dprintf', save it, and355register with DIDD by means of 'dprintf' function.3563. any other driver is loaded and is able to access adapter array357and debug interface358This approach does allow to load/unload debug interface on demand,359and save memory, it it is necessary.360-------------------------------------------------------------------------- */361void IDI_CALL_LINK_T DIVA_DIDD_Read (void IDI_CALL_ENTITY_T * buffer,362int length) {363diva_didd_read_adapter_array (buffer, length);364}365366367368