grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / devs / networks / ppp / serial.c
blob51325e65e5e70e86917fe9516828600ca68ca65d
1 /*
2 * $Id$
3 */
5 #include <exec/types.h>
6 #include <exec/resident.h>
7 #include <exec/io.h>
8 #include <exec/ports.h>
9 #include <exec/errors.h>
10 #include <exec/lists.h>
12 #include <aros/io.h>
13 #include <aros/libcall.h>
14 #include <aros/symbolsets.h>
16 #include <devices/sana2.h>
17 #include <devices/sana2specialstats.h>
18 #include <devices/newstyle.h>
19 #include <devices/timer.h>
20 #include <devices/serial.h>
21 #include <devices/timer.h>
23 #include <utility/utility.h>
24 #include <utility/tagitem.h>
25 #include <utility/hooks.h>
27 #include <proto/exec.h>
28 #include <proto/dos.h>
29 #include <proto/oop.h>
30 #include <proto/timer.h>
31 #include <proto/utility.h>
33 #include <stdlib.h>
34 #include <stdio.h>
37 #include <oop/oop.h>
38 #include <hidd/pci.h>
40 #include <ctype.h>
41 #include <ctype.h>
42 #include <devices/serial.h>
43 #include <devices/timer.h>
45 #include "ppp.h"
46 #include "device_protos.h"
47 #include LC_LIBDEFS_FILE
50 void SetTimer(struct EasyTimer* t,const ULONG s){
51 if( t ){
52 //bug("setTimer\n");
53 if( t->TimeReq ){
54 AbortIO((struct IORequest *)t->TimeReq);
55 WaitIO((struct IORequest *)t->TimeReq);
56 while(GetMsg(t->TimeMsg));
58 if( s <= 0 ) return;
60 t->TimeReq->tr_time.tv_secs = s;
61 t->TimeReq->tr_time.tv_micro = 0;
62 ((struct IORequest *)t->TimeReq)->io_Command = TR_ADDREQUEST;
63 SendIO( (struct IORequest *)t->TimeReq );
64 //bug("setTimer ok\n");
70 void CloseTimer(struct EasyTimer* t){
71 if( t ){
72 //bug("closeTimer\n");
73 if( t->TimeReq ){
74 AbortIO((struct IORequest *)t->TimeReq);
75 WaitIO((struct IORequest *)t->TimeReq);
76 while(GetMsg(t->TimeMsg));
77 CloseDevice((struct IORequest *)t->TimeReq);
78 DeleteIORequest(t->TimeReq);
80 if(t->TimeMsg) DeleteMsgPort(t->TimeMsg);
81 FreeMem( t , sizeof(struct EasyTimer) );
82 //bug("closeTimer ok\n");
86 struct EasyTimer* OpenTimer(){
87 struct EasyTimer* t;
88 //bug("OpenTimer\n");
89 if( t = AllocMem( sizeof(struct EasyTimer),MEMF_CLEAR|MEMF_PUBLIC) ){
90 if( t->TimeMsg = CreateMsgPort() ){
91 if( t->TimeReq = CreateIORequest((struct MsgPort *)t->TimeMsg, sizeof(struct timerequest))){
92 if(!OpenDevice("timer.device", UNIT_VBLANK,(struct IORequest *)t->TimeReq, 0)){
93 t->TimeReq->tr_time.tv_secs = 0;
94 t->TimeReq->tr_time.tv_micro = 1;
95 ((struct IORequest *)t->TimeReq)->io_Command = TR_ADDREQUEST;
96 DoIO( (struct IORequest *)t->TimeReq );
97 SetTimer(t,0);
98 //bug("OpenTimer ok!\n");
99 return t;
104 //bug("OpenTimer FAIL!\n");
105 CloseTimer(t);
106 return NULL;
110 VOID DoStr(struct EasySerial *s,const STRPTR str){
111 DoBYTES(s,str,strlen((char*) str));
114 void DoBYTES(struct EasySerial *s, BYTE *p,ULONG len){
116 if( ! s ) return;
117 if( ! s->Ok ) return;
119 WaitIO((struct IORequest*)s->SerTx);
121 s->SerTx->IOSer.io_Length = len;
122 s->SerTx->IOSer.io_Data = p;
123 s->SerTx->IOSer.io_Command = CMD_WRITE;
124 DoIO((struct IORequest*)s->SerTx);
127 void SendBYTES(struct EasySerial *s, BYTE *p, ULONG len){
129 if( ! s ) return;
130 if( ! s->Ok ) return;
132 WaitIO((struct IORequest*)s->SerTx);
134 s->SerTx->IOSer.io_Length = len;
135 s->SerTx->IOSer.io_Data = p;
136 s->SerTx->IOSer.io_Command = CMD_WRITE;
137 SendIO((struct IORequest*)s->SerTx);
141 struct EasySerial *OpenSerial(BYTE *name,ULONG unit){
142 struct EasySerial *s = NULL;
143 D(bug("OpenSerial\n"));
147 if( ! (s = AllocMem( sizeof(struct EasySerial),MEMF_CLEAR|MEMF_PUBLIC))) break;
148 if( ! (s->TxBuff = AllocMem( SERIAL_BUFSIZE ,MEMF_CLEAR|MEMF_PUBLIC))) break;
149 if( ! (s->RxBuff = AllocMem( SERIAL_BUFSIZE ,MEMF_CLEAR|MEMF_PUBLIC))) break;
150 if( ! (s->TxPort = CreateMsgPort())) break;
151 if( ! (s->SerTx = CreateIORequest(s->TxPort,sizeof(struct IOExtSer)))) break;
154 D(bug("OpenDevice: \"%s\" unit %d\n",name,unit));
155 if( OpenDevice( name , unit , (struct IORequest *)s->SerTx,0)){
156 DeleteIORequest(s->SerTx);
157 s->SerTx = NULL;
158 break;
161 bug("Test CMD_WRITE\n");
162 s->SerTx->IOSer.io_Length = 0;
163 s->SerTx->IOSer.io_Data = " ";
164 s->SerTx->IOSer.io_Command = CMD_WRITE;
165 if( DoIO((struct IORequest *)s->SerTx)){
166 break;
169 if( ! (s->RxPort = CreateMsgPort()) ) break;
170 if( ! (s->SerRx = CreateIORequest(s->RxPort,sizeof(struct IOExtSer)))) break;
172 bug("Test SDCMD_QUERY\n");
173 s->SerRx->IOSer.io_Device = s->SerTx->IOSer.io_Device;
174 s->SerRx->IOSer.io_Unit = s->SerTx->IOSer.io_Unit;
175 s->SerRx->IOSer.io_Command = SDCMD_QUERY;
176 if( DoIO((struct IORequest *)s->SerRx)){
177 break;
180 D(bug("OpenSerial OK\n"));
181 s->Ok = TRUE;
182 return s;
184 }while(0);
185 // FAIL:
186 _CloseSerial(s);
187 return NULL;
190 VOID _CloseSerial(struct EasySerial *s){
192 if( ! s ) return ;
193 s->Ok = FALSE;
195 if( s->SerRx ){
196 AbortIO((struct IORequest *)s->SerRx);
197 WaitIO((struct IORequest *)s->SerRx);
198 while(GetMsg(s->RxPort));
201 if( s->SerTx ){
202 AbortIO((struct IORequest *)s->SerTx);
203 WaitIO((struct IORequest *)s->SerTx);
204 while(GetMsg(s->TxPort));
207 if(s->SerTx) CloseDevice((struct IORequest *)s->SerTx);
209 if(s->SerTx) DeleteIORequest(s->SerTx);
210 if(s->TxPort) DeleteMsgPort(s->TxPort);
212 if(s->SerRx) DeleteIORequest(s->SerRx);
213 if(s->RxPort) DeleteMsgPort(s->RxPort);
215 if( s->TxBuff ) FreeMem( s->TxBuff , SERIAL_BUFSIZE );
216 if( s->RxBuff ) FreeMem( s->RxBuff , SERIAL_BUFSIZE );
217 FreeMem( s , sizeof(struct EasySerial) );
220 VOID QueueSerRequest(struct EasySerial *s , LONG maxlength){
222 if( ! s ) return;
223 if( ! s->Ok ) return;
224 if( maxlength < 1 ) return;
226 if( maxlength > SERIAL_BUFSIZE ) maxlength = SERIAL_BUFSIZE;
227 s->SerRx->IOSer.io_Command = SDCMD_QUERY;
228 DoIO((struct IORequest *)s->SerRx);
230 if( s->SerRx->IOSer.io_Error ){
231 bug("QueueSerRequest() OOOOPS lost device!\n");
232 s->Ok = FALSE;
233 return;
236 s->SerRx->IOSer.io_Command = CMD_READ;
237 s->SerRx->IOSer.io_Data = s->RxBuff;
238 s->SerRx->IOSer.io_Length = s->SerRx->IOSer.io_Actual ;
240 if( s->SerRx->IOSer.io_Length > maxlength )
241 s->SerRx->IOSer.io_Length = maxlength;
242 /* If the number of bytes available is zero, queue a request
243 for one byte. */
244 if(!s->SerRx->IOSer.io_Length)
245 s->SerRx->IOSer.io_Length = 1;
247 SendIO((struct IORequest *)s->SerRx);
248 if( s->SerRx->IOSer.io_Error ){
249 bug("QueueSerRequest() SendIO -> CMD_READ error!");