Exec locks provides means to protect exec's lists from beeing accessed. The lock...
[AROS.git] / arch / all-unix / hidd / parallel / ParallelUnitClass.c
blob99d4c1098df862acffbf007b4e6537dfed9c33b8
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Parallel Unit hidd class implementation.
6 Lang: english
7 */
9 #define __OOP_NOATTRBASES__
11 /* Some POSIX includes */
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <termios.h>
15 #include <unistd.h>
17 /* the rest are Amiga includes */
18 #define timeval aros_timeval
20 #include <proto/exec.h>
21 #include <proto/utility.h>
22 #include <proto/oop.h>
23 #include <proto/alib.h>
24 #include <exec/libraries.h>
25 #include <exec/ports.h>
26 #include <exec/memory.h>
27 #include <exec/interrupts.h>
28 #include <exec/lists.h>
29 #include <utility/tagitem.h>
30 #include <hidd/parallel.h>
31 #include <hidd/unixio.h>
33 #include <aros/symbolsets.h>
35 #include "parallel_intern.h"
36 #undef timeval
38 #include LC_LIBDEFS_FILE
40 #undef SDEBUG
41 #undef DEBUG
42 #define SDEBUG 1
43 #define DEBUG 1
44 #include <aros/debug.h>
46 void parallelunit_io(int fd, int mode, void *ptr);
48 static char * unitname[] =
50 "/dev/lp0",
51 "/dev/lp1",
52 "/dev/lp2"
55 /*************************** Classes *****************************/
57 static OOP_AttrBase HiddParallelUnitAB;
58 static OOP_AttrBase HiddUnixIOAttrBase;
60 static struct OOP_ABDescr attrbases[] =
62 { IID_Hidd_ParallelUnit, &HiddParallelUnitAB },
63 { IID_Hidd_UnixIO , &HiddUnixIOAttrBase },
64 { NULL, NULL }
67 /******* ParallelUnit::New() ***********************************/
68 OOP_Object *UXParUnit__Root__New(OOP_Class *cl, OOP_Object *obj, struct pRoot_New *msg)
70 struct HIDDParallelUnitData * data;
71 const struct TagItem tags[] = {
72 {aHidd_UnixIO_Opener , (IPTR)"parallel.hidd"},
73 {aHidd_UnixIO_Architecture, (IPTR)AROS_ARCHITECTURE},
74 {TAG_END, 0}
76 struct TagItem *tag, *tstate;
77 ULONG unitnum = 0;
79 EnterFunc(bug("ParallelUnit::New()\n"));
81 tstate = msg->attrList;
82 while ((tag = NextTagItem(&tstate))) {
83 ULONG idx;
85 if (IS_HIDDPARALLELUNIT_ATTR(tag->ti_Tag, idx)) {
86 switch (idx) {
87 case aoHidd_ParallelUnit_Unit:
88 unitnum = (ULONG)tag->ti_Data;
89 break;
93 } /* while (tags to process) */
95 D(bug("!!!!Request for unit number %d\n",unitnum));
97 obj = (OOP_Object *)OOP_DoSuperMethod(cl, obj, (OOP_Msg)msg);
99 if (obj)
101 data = OOP_INST_DATA(cl, obj);
103 data->unitnum = unitnum;
105 data->unixio = OOP_NewObject(NULL, CLID_Hidd_UnixIO, (struct TagItem *)tags);
106 if (NULL != data->unixio)
108 D(bug("Opening %s.\n",unitname[data->unitnum]));
110 data->unixio_int.fd = Hidd_UnixIO_OpenFile(data->unixio, unitname[data->unitnum], O_NONBLOCK|O_RDWR, 0, NULL);
112 D(bug("Opened %s on handle %d\n",unitname[data->unitnum], data->unixio_int.fd));
114 if (-1 != data->unixio_int.fd)
116 int error;
118 data->unixio_int.mode = vHidd_UnixIO_RW;
119 data->unixio_int.handler = parallelunit_io;
120 data->unixio_int.handlerData = data;
122 D(bug("Adding UnixIO AsyncIO interrupt!\n"));
123 error = Hidd_UnixIO_AddInterrupt(data->unixio, &data->unixio_int);
125 if (!error)
126 ReturnPtr("ParallelUnit::New()", OOP_Object *, obj);
128 Hidd_UnixIO_CloseFile(data->unixio, data->unixio_int.fd, NULL);
130 /* There's no need to dispose UnixIO object */
133 OOP_DisposeObject(obj);
134 } /* if (obj) */
136 ReturnPtr("ParallelUnit::New()", OOP_Object *, NULL);
139 /******* ParallelUnit::Dispose() ***********************************/
140 OOP_Object *UXParUnit__Root__Dispose(OOP_Class *cl, OOP_Object *obj, OOP_Msg msg)
142 struct HIDDParallelUnitData * data;
143 EnterFunc(bug("ParallelUnit::Dispose()\n"));
145 data = OOP_INST_DATA(cl, obj);
146 D(bug("Freeing filedescriptor (%d)!\n",data->unixio_int.fd));
148 if (-1 != data->unixio_int.fd)
150 Hidd_UnixIO_RemInterrupt(data->unixio, &data->unixio_int);
151 Hidd_UnixIO_CloseFile(data->unixio, data->unixio_int.fd, NULL);
153 OOP_DoSuperMethod(cl, obj, (OOP_Msg)msg);
154 ReturnPtr("ParallelUnit::Dispose()", OOP_Object *, obj);
157 /******* ParallelUnit::Init() **********************************/
158 BOOL UXParUnit__Hidd_ParallelUnit__Init(OOP_Class *cl, OOP_Object *o, struct pHidd_ParallelUnit_Init *msg)
160 struct HIDDParallelUnitData * data = OOP_INST_DATA(cl, o);
162 EnterFunc(bug("ParallelUnit::Init()\n"));
163 data->DataReceivedCallBack = msg->DataReceived;
164 data->DataReceivedUserData = msg->DataReceivedUserData;
165 data->DataWriteCallBack = msg->WriteData;
166 data->DataWriteUserData = msg->WriteDataUserData;
168 ReturnBool("ParallelUnit::Init()", TRUE);
171 /******* ParallelUnit::Write() **********************************/
172 ULONG UXParUnit__Hidd_ParallelUnit__Write(OOP_Class *cl, OOP_Object *o, struct pHidd_ParallelUnit_Write *msg)
174 struct HIDDParallelUnitData * data = OOP_INST_DATA(cl, o);
175 ULONG len = 0;
177 EnterFunc(bug("ParallelUnit::Write()\n"));
179 if (TRUE == data->stopped)
180 return 0;
182 D(bug("Writing %d bytes to fd %d (stream: %s)\n",
183 msg->Length,
184 data->unixio_int.fd,
185 msg->Outbuffer));
187 len = Hidd_UnixIO_WriteFile(data->unixio, data->unixio_int.fd, msg->Outbuffer, msg->Length, NULL);
189 ReturnInt("ParallelUnit::Write()",ULONG, len);
192 /******* ParallelUnit::Start() **********************************/
193 VOID UXParUnit__Hidd_ParallelUnit__Start(OOP_Class *cl, OOP_Object *o, struct pHidd_ParallelUnit_Start *msg)
195 struct HIDDParallelUnitData * data = OOP_INST_DATA(cl, o);
198 * Allow or start feeding the UART with data. Get the data
199 * from upper layer.
201 if (TRUE == data->stopped) {
202 if (NULL != data->DataWriteCallBack)
203 data->DataWriteCallBack(data->unitnum, data->DataWriteUserData);
205 * Also mark the stopped flag as FALSE.
207 data->stopped = FALSE;
211 /******* ParallelUnit::Stop() **********************************/
212 VOID UXParUnit__Hidd_ParallelUnit__Stop(OOP_Class *cl, OOP_Object *o, struct pHidd_ParallelUnit_Stop *msg)
214 struct HIDDParallelUnitData * data = OOP_INST_DATA(cl, o);
217 * The next time the interrupt comes along and asks for
218 * more data we just don't do anything...
220 data->stopped = TRUE;
223 /****** ParallelUnit::GetStatus ********************************/
224 UWORD UXParUnit__Hidd_ParallelUnit__GetStatus(OOP_Class *cl, OOP_Object *o, struct pHidd_ParallelUnit_GetStatus *msg)
226 #if 0
227 struct HIDDParallelUnitData * data = OOP_INST_DATA(cl, o);
228 #endif
230 return 0;
234 /************* The software interrupt handler that gets data from PORT *****/
237 #define READBUFFER_SIZE 513
239 void parallelunit_io(int fd, int mode, void *ptr)
241 struct HIDDParallelUnitData *data = ptr;
243 if (mode & vHidd_UnixIO_Read)
245 ssize_t len;
246 UBYTE buffer[READBUFFER_SIZE];
249 ** Read the data from the port ...
251 len = Hidd_UnixIO_ReadFile(data->unixio, fd, buffer, READBUFFER_SIZE, NULL);
254 ** ... and deliver them to whoever is interested.
256 if (NULL != data->DataReceivedCallBack)
257 data->DataReceivedCallBack(buffer, len, data->unitnum, data->DataReceivedUserData);
261 if (mode & vHidd_UnixIO_Write)
263 if (NULL != data->DataWriteCallBack)
264 data->DataWriteCallBack(data->unitnum, data->DataWriteUserData);
268 /******* init_parallelunitclass ********************************/
270 static int UXParUnit_InitAttrBases(LIBBASETYPEPTR LIBBASE)
272 return OOP_ObtainAttrBases(attrbases);
276 static int UXParUnit_ExpungeAttrBases(LIBBASETYPEPTR LIBBASE)
278 OOP_ReleaseAttrBases(attrbases);
279 return TRUE;
282 ADD2INITLIB(UXParUnit_InitAttrBases, 0)
283 ADD2EXPUNGELIB(UXParUnit_ExpungeAttrBases, 0)