#include<string.h>
#include<console.h>
+#include<wait.h>
+
+cnsl_t cnsl;
+
+static bool empty(const cnsl_queue_t *q)
+{
+ return q->head == q->tail;
+}
+
+static bool full(const cnsl_queue_t *q)
+{
+ return (q->head + 1) % CNSL_QUEUE_SIZE == q->tail;
+}
+
+static void put(cnsl_queue_t *q, char c)
+{
+ if(!full(q))
+ {
+ q->data[q->head] = c;
+ q->head = (q->head + 1) % CNSL_QUEUE_SIZE;
+ }
+}
+
+static bool get(cnsl_queue_t *q, char *c)
+{
+ if(!empty(q))
+ {
+ *c = q->data[q->tail];
+ q->tail = (q->tail + 1) % CNSL_QUEUE_SIZE;
+ return true;
+ }
+
+ return false;
+}
+
+static void clear(cnsl_queue_t *q)
+{
+ q->head = q->tail = 0;
+}
+
+static void erase(cnsl_queue_t *q)
+{
+ if(empty(q))
+ return;
+
+ if(q->head == 0)
+ q->head = CNSL_QUEUE_SIZE -1;
+ else
+ q->head--;
+}
-cnsl_queue_t cnsl_rd_q;
-cnsl_queue_t cnsl_wr_q;
-cnsl_queue_t cnsl_sc_q;
static void cnsl_queue_init(cnsl_queue_t *cq)
{
memset((void *)cq, 0, sizeof(*cq));
-
cq->head = 0;
cq->tail = 0;
init_wait_queue(&cq->wait);
+}
- //cq->data = kmalloc(CNSL_QUEUE_SIZE, 0);
+wait_queue_head_t rdwq;
- printk("console queue data addr %08x\n", cq->data);
+void cnsl_init()
+{
+ cnsl_queue_init(&cnsl.rd_q);
+ cnsl_queue_init(&cnsl.wr_q);
+ cnsl_queue_init(&cnsl.sc_q);
+ init_wait_queue(&rdwq);
}
-void cnsl_init()
+
+int cnsl_kbd_write(char ch)
+{
+ if(ch == '\b')
+ {
+ if(!empty(&cnsl.wr_q))
+ vga_putc(0, '\b', 0x2);
+ erase(&cnsl.wr_q);
+ erase(&cnsl.sc_q);
+ }
+ else
+ {
+ put(&cnsl.wr_q, ch);
+ put(&cnsl.sc_q, ch);
+ vga_putc(0, ch, 0x2);
+ }
+
+
+ if(ch == '\n')
+ {
+ clear(&cnsl.wr_q);
+ while(get(&cnsl.sc_q, &ch))
+ put(&cnsl.rd_q, ch);
+ wake_up(&rdwq);
+ }
+
+#if 0
+ printl(23, "rd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
+ cnsl.rd_q.data[0],
+ cnsl.rd_q.data[1],
+ cnsl.rd_q.data[2],
+ cnsl.rd_q.data[3],
+ cnsl.rd_q.data[4],
+ cnsl.rd_q.data[5],
+ cnsl.rd_q.data[6],
+ cnsl.rd_q.data[7],
+ cnsl.rd_q.data[8],
+ cnsl.rd_q.data[9]);
+#endif
+}
+
+
+int cnsl_read(char *buf, size_t count)
{
- cnsl_queue_init(&cnsl_rd_q);
- cnsl_queue_init(&cnsl_wr_q);
- cnsl_queue_init(&cnsl_sc_q);
+ unsigned long flags;
+
+ assert(count > 0);
+ int cnt = 0;
+ for(cnt=0; cnt<count; )
+ {
+ char ch;
+ task_union * task = current;
+ DECLARE_WAIT_QUEUE(wait, task);
+ add_wait_queue(&rdwq, &wait);
+
+ while(true)
+ {
+ task->state = TASK_WAIT;
+ irq_save(flags);
+ bool r = get(&cnsl.rd_q, &ch);
+ irq_restore(flags);
+
+ if(r)
+ {
+ buf[cnt++] = ch;
+
+ task->state = TASK_RUNNING;
+ del_wait_queue(&rdwq, &wait);
+
+ if(ch == '\n')
+ goto end;
+
+ break;
+ }
+
+ schedule();
+ }
+
+ }
+
+end:
+ buf[cnt] = 0;
+ return cnt;
}
+
+chrdev_t cnsl_chrdev = {
+ .read = cnsl_read
+};
int debug_wait_queue_put(unsigned int v);
void ide_dma_pci_lba48();
-unsigned long kbd_cnt = 0;
+
+void kbd_debug(unsigned char scan_code);
+
+char kbd_char_tbl[]={0,0,
+'1','2','3','4','5','6','7','8','9','0','-','=','\b',0,
+'q','w','e','r','t','y','u','i','o','p','[',']','\n',0,
+'a','s','d','f','g','h','j','k','l',';','\'','`',0,'\\',
+'z','x','c','v','b','n','m',',','.','/',0,0,0,' ',
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+};
+
void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id)
{
unsigned char scan_code;
- scan_code = inb(0x60);
- printl(MPL_KEYBOARD, "keyboard:%d scan code %02x", kbd_cnt++, scan_code);
-
- if(scan_code == 0x01) // Esc
- reboot();
-
- printk("[%02x]", scan_code);
+ scan_code = inb(0x60);
- if(scan_code == 0x09) // 8
- ide_dma_pci_lba48();
+ kbd_debug(scan_code);
- if(scan_code == 0x0B) // 0
+ if(0x80 & scan_code) // break code
{
- asm("cli;");
- while(1);
+ return ;
}
- if(scan_code == 0x13) // r
- ide_debug();
+ unsigned int inx = scan_code & 0xFF;
+ char ch = kbd_char_tbl[inx];
+ cnsl_kbd_write(ch);
+}
+
+void kbd_debug(unsigned char scan_code)
+{
+ static unsigned long kbd_cnt = 0;
+ printl(MPL_KEYBOARD, "keyboard:%d scan code %02x", kbd_cnt++, scan_code);
- if(scan_code == 0x1F) // s
- ide_status();
+ if(scan_code == 0x01) // Esc
+ reboot();
+
+ printd("[%02x]", scan_code);
- if(scan_code == 0x14) // t
- debug_sched();
if(scan_code == 0x3B) // F1
vga_switch(0);
if(scan_code == 0x42) // F8
debug_wait_queue_put(7);
- if(scan_code == 0x43); // F9
- if(scan_code == 0x44); // F10
- if(scan_code == 0x57); // F11
+ if(scan_code == 0x43) // F9
+ ide_dma_pci_lba48();
+ if(scan_code == 0x44) // F10
+ ide_debug();
+ if(scan_code == 0x57) // F11
+ {
+ asm("cli;");
+ while(1);
+ }
if(scan_code == 0x58) // F12
vga_dbg_toggle();
-#if 1
- cnsl_rd_q.data[0] = (char) scan_code;
- wake_up(&cnsl_rd_q.wait);
-#endif
-}
-
-int sysc_read_kbd()
-{
- DECLARE_WAIT_QUEUE(wait, current);
- add_wait_queue(&cnsl_rd_q.wait, &wait);
- return 0;
+ //ide_status();
}
#include <io.h>
#include <printk.h>
#include <system.h>
-#define SECT_SIZE 512
-
-#if 0
-File file_table[NR_FILES] __attribute__ ((__aligned__(PAGE_SIZE))) = {{0,},};
-extern unsigned int ext2_start_sect;
-void hd_read(dev_t dev, u64 sect_nr, void *buf, u32 count);
-void init_file_table();
-void save_boot_part(int n, pPartition p, u32 base_sect);
-void read_ext_part(u32 base, u32 offset);
-void setup_fs()
-{
- int i, minor;
- pPartition p;
- unsigned char *buf;
-
-
- init_file_table();
-
-
- minor = DEV_MINOR(ROOT_DEV);
- buf = (unsigned char *) get_virt_pages(1);
- hd_read(ROOT_DEV, 0, buf, SECT_SIZE);
-
-
- for(i=0; i<4; i++)
- {
- p = (pPartition)(buf+PARTS_POS) + i;
- if(p->Type == 0)
- continue;
-
- //save_boot_part(i+1, p, p->AbsoluteSectNo);
- save_boot_part(i, p, p->AbsoluteSectNo);
-
- printk("hd%d\tbase: %08x size: %08x type:%02x",
- i, p->AbsoluteSectNo, p->PartitionSize, p->Type);
- if(p->Type == 0x05)
- {
- printk("\tExtend\n");
- //read_ext_part(p->AbsoluteSectNo, 0);
- }
- else
- printk("\n");
- }
-
-
- printk("ext2_start_sect: %x\n", ext2_start_sect);
+extern chrdev_t cnsl_chrdev;
- return ;
-}
-
-void init_file_table()
-{
- int i;
+chrdev_t *chrdev[CHRDEV_SIZE] = {
+ &cnsl_chrdev
+};
- for(i=0; i<NR_FILES; i++)
- {
- file_table[i].count = 0;
- file_table[i].ino_nr = 0;
- file_table[i].inode = NULL;
- }
-}
+void ext2_setup_fs();
+unsigned int ext2_search_inpath(const char *path);
-void save_boot_part(int n, pPartition p, u32 base_sect)
+void setup_fs()
{
- if(p->Type == 0x05)
- panic("partition should not be extended");
-
- int minor = DEV_MINOR(ROOT_DEV);
-
- if(minor-1 == n)
- ext2_start_sect = base_sect;
+ ext2_setup_fs();
}
-static unsigned int ext_part = 5;
-void read_ext_part(u32 base, u32 offset)
+unsigned int namei(const char *path)
{
- unsigned char *buf;
- pPartition p;
- buf = get_virt_pages(1);
-
- //printk("^^^^^^^^^:%08x\n", base+offset);
- hd_read(ROOT_DEV, base+offset, buf, SECT_SIZE);
- int i;
-
- for(i=0; i<4; i++)
- {
- p = (pPartition)(buf+PARTS_POS) + i;
- if(p->Type == 0x00)
- continue;
-
- if(p->Type != 0x05)
- {
-
- //save_boot_part(ext_part, p, 0);
- printk(" hd%d\tbase: %08x size: %08x type:%02x\n",
- ext_part++, base+p->AbsoluteSectNo,
- p->PartitionSize, p->Type);
- }
- else
- {
- read_ext_part(base, p->AbsoluteSectNo);
- }
- }
-
- printk("\n");
-
- free_virt_pages(buf);
+ return ext2_search_inpath(path);
}
-#endif