]> git.sesse.net Git - pistorm/blob - a314/a314device/device.c
Add missing copyright to A314 source files
[pistorm] / a314 / a314device / device.c
1 /*
2  * Copyright 2020-2021 Niklas Ekström
3  */
4
5 #include <exec/types.h>
6 #include <exec/execbase.h>
7 #include <exec/devices.h>
8 #include <exec/errors.h>
9 #include <exec/ports.h>
10 #include <libraries/dos.h>
11 #include <proto/exec.h>
12
13 #include "device.h"
14 #include "a314.h"
15 #include "startup.h"
16 #include "fix_mem_region.h"
17
18 char device_name[] = A314_NAME;
19 char id_string[] = A314_NAME " 1.1 (28 Nov 2020)";
20
21 struct ExecBase *SysBase;
22 BPTR saved_seg_list;
23 BOOL running = FALSE;
24
25 static struct Library *init_device(__reg("a6") struct ExecBase *sys_base, __reg("a0") BPTR seg_list, __reg("d0") struct Library *dev)
26 {
27         SysBase = *(struct ExecBase **)4;
28         saved_seg_list = seg_list;
29
30         // We are being called from InitResident() in initializers.asm.
31         // MakeLibrary() was executed before we got here.
32
33         dev->lib_Node.ln_Type = NT_DEVICE;
34         dev->lib_Node.ln_Name = device_name;
35         dev->lib_Flags = LIBF_SUMUSED | LIBF_CHANGED;
36         dev->lib_Version = 1;
37         dev->lib_Revision = 0;
38         dev->lib_IdString = (APTR)id_string;
39
40         // AddDevice() is executed after we return.
41         return dev;
42 }
43
44 static BPTR expunge(__reg("a6") struct Library *dev)
45 {
46         // There is currently no support for unloading a314.device.
47
48         if (TRUE) //dev->lib_OpenCnt != 0)
49         {
50                 dev->lib_Flags |= LIBF_DELEXP;
51                 return 0;
52         }
53
54         /*
55         BPTR seg_list = saved_seg_list;
56         Remove(&dev->lib_Node);
57         FreeMem((char *)dev - dev->lib_NegSize, dev->lib_NegSize + dev->lib_PosSize);
58         return seg_list;
59         */
60 }
61
62 static void open(__reg("a6") struct Library *dev, __reg("a1") struct A314_IORequest *ior, __reg("d0") ULONG unitnum, __reg("d1") ULONG flags)
63 {
64         dev->lib_OpenCnt++;
65
66         if (dev->lib_OpenCnt == 1 && !running)
67         {
68                 if (!task_start())
69                 {
70                         ior->a314_Request.io_Error = IOERR_OPENFAIL;
71                         ior->a314_Request.io_Message.mn_Node.ln_Type = NT_REPLYMSG;
72                         dev->lib_OpenCnt--;
73                         return;
74                 }
75                 running = TRUE;
76         }
77
78         ior->a314_Request.io_Error = 0;
79         ior->a314_Request.io_Message.mn_Node.ln_Type = NT_REPLYMSG;
80 }
81
82 static BPTR close(__reg("a6") struct Library *dev, __reg("a1") struct A314_IORequest *ior)
83 {
84         ior->a314_Request.io_Device = NULL;
85         ior->a314_Request.io_Unit = NULL;
86
87         dev->lib_OpenCnt--;
88
89         if (dev->lib_OpenCnt == 0 && (dev->lib_Flags & LIBF_DELEXP))
90                 return expunge(dev);
91
92         return 0;
93 }
94
95 static void begin_io(__reg("a6") struct Library *dev, __reg("a1") struct A314_IORequest *ior)
96 {
97         PutMsg(&task_mp, (struct Message *)ior);
98         ior->a314_Request.io_Flags &= ~IOF_QUICK;
99 }
100
101 static ULONG abort_io(__reg("a6") struct Library *dev, __reg("a1") struct A314_IORequest *ior)
102 {
103         // There is currently no support for aborting an IORequest.
104         return IOERR_NOCMD;
105 }
106
107 static ULONG device_vectors[] =
108 {
109         (ULONG)open,
110         (ULONG)close,
111         (ULONG)expunge,
112         0,
113         (ULONG)begin_io,
114         (ULONG)abort_io,
115         (ULONG)translate_address_a314,
116         -1,
117 };
118
119 ULONG auto_init_tables[] =
120 {
121         sizeof(struct Library),
122         (ULONG)device_vectors,
123         0,
124         (ULONG)init_device,
125 };