Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / modules / debug / ps2link / net_fsys.c
blob56716858924901832a8d9e606ffe0dacd9035af0
1 /*********************************************************************
2 * Copyright (C) 2003 Tord Lindstrom (pukko@home.se)
3 * Copyright (C) 2004 adresd (adresd_ps2dev@yahoo.com)
4 * This file is subject to the terms and conditions of the PS2Link License.
5 * See the file LICENSE in the main directory of this distribution for more
6 * details.
7 */
9 #include <types.h>
10 #include <thbase.h>
11 #include <ioman.h>
12 #include <sysclib.h>
13 #include <stdio.h>
14 #include <intrman.h>
15 #include <loadcore.h>
16 #include <thsemap.h>
18 #include "net_fio.h"
20 #ifdef DEBUG
21 #define dbgprintf(args...) printf(args)
22 #else
23 #define dbgprintf(args...) do { } while(0)
24 #endif
26 static char fsname[] = "host";
28 ////////////////////////////////////////////////////////////////////////
29 //static iop_device_t fsys_driver;
31 /* File desc struct is probably larger than this, but we only
32 * need to access the word @ offset 0x0C (in which we put our identifier)
34 struct filedesc_info
36 int unkn0;
37 int unkn4;
38 int device_id; // the X in hostX
39 int own_fd;
42 ////////////////////////////////////////////////////////////////////////
43 /* We need(?) to protect the net access, so the server doesn't get
44 * confused if two processes calls a fsys func at the same time
46 static int fsys_sema;
47 static int fsys_pid = 0;
48 //static iop_device_t fsys_driver;
50 ////////////////////////////////////////////////////////////////////////
51 static int dummy5()
53 printf("dummy function called\n");
54 return -5;
57 ////////////////////////////////////////////////////////////////////////
58 static void fsysInit(iop_device_t *driver)
60 struct _iop_thread mythread;
61 int pid;
62 int i;
64 dbgprintf("initializing %s\n", driver->name);
66 // Start socket server thread
68 mythread.attr = 0x02000000; // attr
69 mythread.option = 0; // option
70 mythread.thread = (void *)pko_file_serv; // entry
71 mythread.stacksize = 0x800;
72 mythread.priority = 9; // We really should choose prio w more effort
74 pid = CreateThread(&mythread);
76 if (pid > 0) {
77 if ((i=StartThread(pid, NULL)) < 0) {
78 printf("StartThread failed (%d)\n", i);
81 else {
82 printf("CreateThread failed (%d)\n", pid);
85 fsys_pid = pid;
86 dbgprintf("Thread id: %x\n", pid);
89 ////////////////////////////////////////////////////////////////////////
90 static int fsysDestroy(void)
92 WaitSema(fsys_sema);
93 pko_close_fsys();
94 // ExitDeleteThread(fsys_pid);
95 SignalSema(fsys_sema);
96 DeleteSema(fsys_sema);
97 return 0;
101 ////////////////////////////////////////////////////////////////////////
102 static int fsysOpen( int fd, char *name, int mode)
104 struct filedesc_info *fd_info;
105 int fsys_fd;
107 dbgprintf("fsysOpen..\n");
108 dbgprintf(" fd: %x, name: %s, mode: %d\n\n", fd, name, mode);
110 fd_info = (struct filedesc_info *)fd;
112 WaitSema(fsys_sema);
113 fsys_fd = pko_open_file(name, mode);
114 SignalSema(fsys_sema);
115 fd_info->own_fd = fsys_fd;
117 return fsys_fd;
122 ////////////////////////////////////////////////////////////////////////
123 static int fsysClose( int fd)
125 struct filedesc_info *fd_info;
126 int ret;
128 dbgprintf("fsys_close..\n"
129 " fd: %x\n\n", fd);
131 fd_info = (struct filedesc_info *)fd;
132 WaitSema(fsys_sema);
133 ret = pko_close_file(fd_info->own_fd);
134 SignalSema(fsys_sema);
136 return ret;
141 ////////////////////////////////////////////////////////////////////////
142 static int fsysRead( int fd, char *buf, int size)
144 struct filedesc_info *fd_info;
145 int ret;
147 fd_info = (struct filedesc_info *)fd;
149 dbgprintf("fsysRead..."
150 " fd: %x\n"
151 " bf: %x\n"
152 " sz: %d\n"
153 " ow: %d\n\n", fd, (int)buf, size, fd_info->own_fd);
155 WaitSema(fsys_sema);
156 ret = pko_read_file(fd_info->own_fd, buf, size);
157 SignalSema(fsys_sema);
159 return ret;
165 ////////////////////////////////////////////////////////////////////////
166 static int fsysWrite( int fd, char *buf, int size)
168 struct filedesc_info *fd_info;
169 int ret;
171 dbgprintf("fsysWrite..."
172 " fd: %x\n", fd);
174 fd_info = (struct filedesc_info *)fd;
175 WaitSema(fsys_sema);
176 ret = pko_write_file(fd_info->own_fd, buf, size);
177 SignalSema(fsys_sema);
178 return ret;
183 ////////////////////////////////////////////////////////////////////////
184 static int fsysLseek( int fd, unsigned int offset, int whence)
186 struct filedesc_info *fd_info;
187 int ret;
189 dbgprintf("fsysLseek..\n"
190 " fd: %x\n"
191 " of: %x\n"
192 " wh: %x\n\n", fd, offset, whence);
194 fd_info = (struct filedesc_info *)fd;
195 WaitSema(fsys_sema);
196 ret = pko_lseek_file(fd_info->own_fd, offset, whence);
197 SignalSema(fsys_sema);
198 return ret;
201 ////////////////////////////////////////////////////////////////////////
202 static int fsysRemove(iop_file_t* file, char *name)
204 int ret;
205 dbgprintf("fsysRemove..\n");
206 dbgprintf(" name: %s\n\n", name);
208 WaitSema(fsys_sema);
209 ret = pko_remove(name);
210 SignalSema(fsys_sema);
212 return ret;
215 ////////////////////////////////////////////////////////////////////////
216 static int fsysMkdir(iop_file_t* file, char *name, int mode)
218 int ret;
219 dbgprintf("fsysMkdir..\n");
220 dbgprintf(" name: '%s'\n\n", name);
222 WaitSema(fsys_sema);
223 ret = pko_mkdir(name, mode);
224 SignalSema(fsys_sema);
226 return ret;
229 ////////////////////////////////////////////////////////////////////////
230 static int fsysRmdir(iop_file_t* file, char *name)
232 int ret;
233 dbgprintf("fsysRmdir..\n");
234 dbgprintf(" name: %s\n\n", name);
236 WaitSema(fsys_sema);
237 ret = pko_rmdir(name);
238 SignalSema(fsys_sema);
240 return ret;
244 ////////////////////////////////////////////////////////////////////////
245 static int fsysDopen(int fd, char *name)
247 struct filedesc_info *fd_info;
248 int fsys_fd;
250 dbgprintf("fsysDopen..\n");
251 dbgprintf(" fd: %x, name: %s\n\n", fd, name);
253 fd_info = (struct filedesc_info *)fd;
255 WaitSema(fsys_sema);
256 fsys_fd = pko_open_dir(name);
257 SignalSema(fsys_sema);
258 fd_info->own_fd = fsys_fd;
260 return fsys_fd;
263 ////////////////////////////////////////////////////////////////////////
264 static int fsysDread(int fd, void *buf)
266 struct filedesc_info *fd_info;
267 int ret;
269 fd_info = (struct filedesc_info *)fd;
271 dbgprintf("fsysDread..."
272 " fd: %x\n"
273 " bf: %x\n"
274 " ow: %d\n\n", fd, (int)buf, fd_info->own_fd);
276 WaitSema(fsys_sema);
277 ret = pko_read_dir(fd_info->own_fd, buf);
278 SignalSema(fsys_sema);
280 return ret;
284 ////////////////////////////////////////////////////////////////////////
285 static int fsysDclose(int fd)
287 struct filedesc_info *fd_info;
288 int ret;
290 dbgprintf("fsys_dclose..\n"
291 " fd: %x\n\n", fd);
293 fd_info = (struct filedesc_info *)fd;
294 WaitSema(fsys_sema);
295 ret = pko_close_dir(fd_info->own_fd);
296 SignalSema(fsys_sema);
298 return ret;
301 iop_device_ops_t fsys_functarray = { (void *)fsysInit, (void *)fsysDestroy, (void *)dummy5,
302 (void *)fsysOpen, (void *)fsysClose, (void *)fsysRead,
303 (void *)fsysWrite, (void *)fsysLseek, (void *)dummy5,
304 (void *)fsysRemove, (void *)fsysMkdir, (void *)fsysRmdir,
305 (void *)fsysDopen, (void *)fsysDclose, (void *)fsysDread,
306 (void *)dummy5, (void *)dummy5 };
308 iop_device_t fsys_driver = { fsname, 16, 1, "fsys driver",
309 &fsys_functarray };
310 ////////////////////////////////////////////////////////////////////////
311 // Entry point for mounting the file system
312 int fsysMount(void)
314 iop_sema_t sema_info;
316 sema_info.attr = 1;
317 sema_info.option = 0;
318 sema_info.initial = 1;
319 sema_info.max = 1;
320 fsys_sema = CreateSema(&sema_info);
322 DelDrv(fsname);
323 AddDrv(&fsys_driver);
325 return 0;
328 int fsysUnmount(void)
330 DelDrv(fsname);
331 return 0;