+ exec* functions
[meinos.git] / apps / com / main.c
blob62cdd658904b98521bd08ea97f7124dc2c501e58
1 /*
2 meinOS - A unix-like x86 microkernel operating system
3 Copyright (C) 2008 Janosch Gräf <janosch.graef@gmx.net>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <sys/types.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <devfs.h>
23 #include <stdio.h>
25 #include "com.h"
27 static void quit() {
28 size_t i;
30 for (i=0;i<4;i++) {
31 if (devices[i]!=NULL) {
32 devfs_removedev(devices[i]->dev);
33 free(devices[i]);
38 static struct com_device *com_dev_find(devfs_dev_t *dev) {
39 size_t i;
41 for (i=0;i<4;i++) {
42 if (dev==devices[i]->dev) return devices[i];
44 return NULL;
47 static ssize_t com_onread(devfs_dev_t *dev,void *buf,size_t count,off_t offset) {
48 struct com_device *com_dev = com_dev_find(dev);
49 if (dev!=NULL) return com_dev_recv(com_dev,buf,count);
50 else return -1;
53 static ssize_t com_onwrite(devfs_dev_t *dev,void *buf,size_t count,off_t offset) {
54 struct com_device *com_dev = com_dev_find(dev);
55 if (dev!=NULL) return com_dev_send(com_dev,buf,count);
56 else return -1;
59 int main() {
60 uint16_t io_ports[] = {0x3F8,0x2F8,0x3E8,0x2E8};
61 size_t i;
63 devfs_init();
64 atexit(quit);
66 for (i=0;i<4;i++) {
67 if (io_ports[i]!=0) {
68 char devname[5];
69 snprintf(devname,5,"com%d",i);
70 devfs_dev_t *dev = devfs_createdev(devname);
71 if (dev!=NULL) {
72 devfs_onread(dev,com_onread);
73 devfs_onwrite(dev,com_onwrite);
74 devices[i] = malloc(sizeof(struct com_device));
75 devices[i]->base = io_ports[i];
76 devices[i]->dev = dev;
78 else devices[i] = NULL;
80 else devices[i] = NULL;
83 devfs_mainloop();
85 return 0;