Rev 510 | Rev 517 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 510 | Rev 511 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2003 Josef Cejka |
2 | * Copyright (C) 2003 Josef Cejka |
3 | * Copyright (C) 2005 Jakub Jermar |
3 | * Copyright (C) 2005 Jakub Jermar |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
14 | * documentation and/or other materials provided with the distribution. |
15 | * - The name of the author may not be used to endorse or promote products |
15 | * - The name of the author may not be used to endorse or promote products |
16 | * derived from this software without specific prior written permission. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | #include <console/console.h> |
30 | #include <console/console.h> |
31 | #include <console/chardev.h> |
31 | #include <console/chardev.h> |
32 | #include <synch/waitq.h> |
32 | #include <synch/waitq.h> |
33 | #include <synch/spinlock.h> |
33 | #include <synch/spinlock.h> |
34 | #include <arch/types.h> |
34 | #include <arch/types.h> |
35 | #include <typedefs.h> |
35 | #include <typedefs.h> |
36 | #include <arch.h> |
36 | #include <arch.h> |
37 | 37 | ||
38 | /** Standard input character device. */ |
38 | /** Standard input character device. */ |
39 | chardev_t *stdin = NULL; |
39 | chardev_t *stdin = NULL; |
40 | 40 | ||
41 | /** Get string from character device. |
41 | /** Get string from character device. |
42 | * |
42 | * |
43 | * Read characters from character device until first occurrence |
43 | * Read characters from character device until first occurrence |
44 | * of newline character. |
44 | * of newline character. |
45 | * |
45 | * |
46 | * @param chardev Character device. |
46 | * @param chardev Character device. |
47 | * @param string Buffer where to store string terminated by '\0'. |
47 | * @param string Buffer where to store string terminated by '\0'. |
48 | * @param len Size of the buffer. |
48 | * @param len Size of the buffer. |
49 | */ |
49 | */ |
50 | void gets(chardev_t *chardev, __u8 *string, size_t buflen) |
50 | void gets(chardev_t *chardev, __u8 *string, size_t buflen) |
51 | { |
51 | { |
52 | index_t index = 0; |
52 | index_t index = 0; |
53 | char ch; |
53 | char ch; |
54 | 54 | ||
55 | while (index < buflen) { |
55 | while (index < buflen) { |
56 | ch = getc(chardev); |
56 | ch = getc(chardev); |
57 | if (ch == '\n') { /* end of string => write 0, return */ |
57 | if (ch == '\n') { /* end of string => write 0, return */ |
58 | string[index] = '\0'; |
58 | string[index] = '\0'; |
59 | return; |
59 | return; |
60 | } |
60 | } |
61 | string[index++] = ch; |
61 | string[index++] = ch; |
62 | } |
62 | } |
63 | return; |
63 | return; |
64 | } |
64 | } |
65 | 65 | ||
66 | /** Get character from character device. |
66 | /** Get character from character device. |
67 | * |
67 | * |
68 | * @param chardev Character device. |
68 | * @param chardev Character device. |
69 | * |
69 | * |
70 | * @return Character read. |
70 | * @return Character read. |
71 | */ |
71 | */ |
72 | __u8 getc(chardev_t *chardev) |
72 | __u8 getc(chardev_t *chardev) |
73 | { |
73 | { |
74 | __u8 ch; |
74 | __u8 ch; |
75 | ipl_t ipl; |
75 | ipl_t ipl; |
76 | 76 | ||
77 | waitq_sleep(&chardev->wq); |
77 | waitq_sleep(&chardev->wq); |
78 | ipl = interrupts_disable(); |
78 | ipl = interrupts_disable(); |
79 | spinlock_lock(&chardev->lock); |
79 | spinlock_lock(&chardev->lock); |
80 | ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN]; |
80 | ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN]; |
81 | chardev->counter--; |
81 | chardev->counter--; |
82 | spinlock_unlock(&chardev->lock); |
82 | spinlock_unlock(&chardev->lock); |
83 | interrupts_restore(ipl); |
83 | interrupts_restore(ipl); |
84 | 84 | ||
85 | chardev->ready_func(); |
85 | chardev->op->resume(); |
86 | 86 | ||
87 | return ch; |
87 | return ch; |
88 | } |
88 | } |
89 | 89 |