1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: 17: 18: 19: 20: 21:
22:
23: #include "kbpd.h"
24:
25:
26: IMPORT W GetDevConf ( UB *name, W *val );
27:
28: 29: 30:
31: EXPORT MgrInfo kpMgrInfo;
32:
33: 34: 35:
36: LOCAL ER registDevice( BOOL regist )
37: {
38: ER er;
39: T_IDEV idev;
40: static const SDefDev ddev = {
41: NULL,
42: "kbpd",
43: 0,
44: 0,
45: 0,
46: 1,
47: NULL,
48: NULL,
49: kpReadFn,
50: kpWriteFn,
51: kpEventFn,
52: };
53:
54: if ( regist ) {
55:
56: er = SDefDevice(&ddev, &idev, &kpMgrInfo.Sdi);
57: if ( er < E_OK ) {
58: DEBUG_PRINT(("SDefDevice err= %d\n", er));
59: goto err_exit;
60: }
61:
62: kpMgrInfo.eventMbf = idev.evtmbfid;
63: } else {
64: er = SDelDevice(kpMgrInfo.Sdi);
65: }
66: err_exit:
67: return er;
68: }
69:
70: 71: 72:
73: LOCAL ER initialize( PRI mypri )
74: {
75: T_CMBX DataRcvMbx;
76: W val[L_DEVCONF_VAL];
77: ER err;
78:
79: 80: 81:
82: MEMSET(&kpMgrInfo, 0, sizeof(kpMgrInfo));
83:
84: 85: 86:
87: err = GetDevConf("KBMODE", val);
88: if ( err >= 1 ) {
89: kpMgrInfo.kpState.stat.mode = val[0] & 0x03;
90: kpMgrInfo.kpState.stat.han = (val[0] & 0x04) ? 1 : 0;
91: }
92:
93: 94: 95:
96: err = CreateLock(&kpMgrInfo.lock, "kbpd");
97: if ( err != E_OK ) {
98: DEBUG_PRINT(("initialize fast lock err = %d\n", err));
99: return err;
100: }
101:
102: 103: 104:
105: SetOBJNAME(DataRcvMbx.exinf, "kbpd");
106: DataRcvMbx.mbxatr = TA_TFIFO | TA_MFIFO;
107: err = tk_cre_mbx(&DataRcvMbx);
108: if ( err < E_OK ) {
109: DEBUG_PRINT(("initialize DataRcvMbx. er = %d\n", err));
110: return err;
111: }
112: kpMgrInfo.dataMbx = (ID)err;
113:
114: 115: 116:
117: err = kpStartDataReceiveTask(mypri-1);
118: if ( err != E_OK ) {
119: DEBUG_PRINT(("start DataReceiveTask. err = %d\n", err));
120: return err;
121: }
122:
123: return E_OK;
124: }
125:
126: 127: 128:
129: LOCAL void finish( void )
130: {
131: ID id;
132:
133:
134: registDevice(FALSE);
135:
136:
137: kpStopDataReceiveTask();
138:
139:
140: if ( (id = kpMgrInfo.dataMbx) != InvalidID ) tk_del_mbx(id);
141: if ( (id = kpMgrInfo.acceptPort) != InvalidID ) tk_del_por(id);
142: DeleteLock(&kpMgrInfo.lock);
143: }
144:
145: 146: 147:
148: LOCAL ER kbpdManager( PRI mypri )
149: {
150: ER err;
151:
152:
153: err = initialize(mypri);
154: if ( err != E_OK ) {
155: DEBUG_PRINT(("initialize err = %d\n", err));
156: goto err_exit;
157: }
158:
159:
160: err = registDevice(TRUE);
161: if ( err != E_OK ) {
162: DEBUG_PRINT(("registDevice err = %d\n", err));
163: goto err_exit;
164: }
165:
166: return E_OK;
167:
168: err_exit:
169:
170: finish();
171: return err;
172: }
173:
174: 175: 176:
177: EXPORT ER KbPdDrv( INT ac, UB *av[] )
178: {
179: PRI pri = DefaultPriority;
180: char *arg;
181: W v[L_DEVCONF_VAL];
182:
183:
184: if (GetDevConf("KbPdDrvEnable", v) > 0 && !v[0]) return E_NOSPT;
185:
186:
187: if ( ac < 0 ) return E_NOSPT;
188:
189:
190: if ( ac > 1 && (arg = av[1]) != NULL ) {
191: while ( *arg != NULL ) {
192: switch ( *arg ) {
193: case '!':
194: arg++;
195: pri = STRTOUL(arg, &arg, 10);
196: break;
197: default:
198: return E_PAR;
199: }
200: }
201: }
202:
203:
204: return kbpdManager(pri);
205: }