+ #include <rpc.h>
[meinos.git] / apps / cdrom / main.c
blob3166c4299af4d0c34892255dfce7d3d7a1256641
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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <dirent.h>
23 #include <devfs.h>
24 #include <llist.h>
25 #include <unistd.h>
26 #include <rpc.h>
28 #include "device.h"
30 static llist_t cdrom_devices;
32 static ssize_t cdrom_devfs_read(devfs_dev_t *devfs,void *buf,size_t count,off_t offset) {
33 struct cdrom_device *dev = devfs->user_data;
35 size_t lba = offset/dev->block_size;
36 size_t off = offset%dev->block_size;
37 size_t count_rem = count;
39 while (count_rem>0) {
40 size_t count_cur = off+count_rem>dev->block_size?dev->block_size-off:count_rem;
41 void *cdrom_buf = cdrom_read(dev,lba,1);
42 memcpy(buf,cdrom_buf+off,count_cur);
43 off = 0;
44 lba++;
45 count_rem -= count_cur;
46 buf += count_cur;
49 return count;
52 static int cdrom_device_init(char *name) {
53 struct cdrom_device *dev = cdrom_device_create(name);
54 if (dev!=NULL) {
55 if (cdrom_inquiry(dev)==0) {
56 if (cdrom_start(dev,1,1)==0) {
57 if (cdrom_read_capacity(dev)==0) {
58 char *devname;
59 asprintf(&devname,"cdrom%d",llist_size(cdrom_devices));
60 dev->devfs = devfs_createdev(devname);
61 free(devname);
62 if (dev->devfs!=NULL) {
63 devfs_onread(dev->devfs,cdrom_devfs_read);
64 dev->devfs->user_data = dev;
65 llist_push(cdrom_devices,dev);
66 return 0;
71 else {
72 cdrom_device_destroy(dev);
73 return 0;
76 cdrom_device_destroy(dev);
77 return -1;
80 static int cdrom_init() {
81 if (cdrom_buf_init()==-1) return -1;
82 devfs_init();
83 cdrom_devices = llist_create();
85 llist_t list = rpc_list();
86 char *name;
87 while ((name = llist_pop(list))) {
88 if (strncmp(name,"scsi_request_atapi",18)==0) {
89 char *dev = name+13; // dev = "atapiXX"
90 if (cdrom_device_init(dev)==-1) fprintf(stderr,"Could not initialize ATAPI device %s\n",dev);
92 free(name);
94 llist_destroy(list);
96 return 0;
99 static int cdrom_destroy() {
100 struct cdrom_device *dev;
101 while ((dev = llist_pop(cdrom_devices))) {
102 devfs_removedev(dev->devfs);
103 cdrom_device_destroy(dev);
105 llist_destroy(cdrom_devices);
106 return 0;
109 int main(int argc,char *argv[]) {
110 cdrom_init();
111 devfs_mainloop();
112 cdrom_destroy();
113 return 0;