2 * (C) Copyright 2004 Intracom S.A.
3 * Pantelis Antoniou <panto@intracom.gr>
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * A phone based console
29 * Virtual display of 80x24 characters.
30 * The actual display is much smaller and panned to show the virtual one.
31 * Input is made by a numeric keypad utilizing the input method of
32 * mobile phones. Sorry no T9 lexicons...
39 #include <linux/types.h>
40 #include <stdio_dev.h>
44 /*************************************************************************************************/
49 #define REFRESH_HZ (CONFIG_SYS_HZ/50) /* refresh every 20ms */
50 #define BLINK_HZ (CONFIG_SYS_HZ/2) /* cursor blink every 500ms */
52 /*************************************************************************************************/
54 #define DISPLAY_BACKLIT_PORT ((volatile immap_t *)CONFIG_SYS_IMMR)->im_ioport.iop_pcdat
55 #define DISPLAY_BACKLIT_MASK 0x0010
57 /*************************************************************************************************/
59 #define KP_STABLE_HZ (CONFIG_SYS_HZ/100) /* stable for 10ms */
60 #define KP_REPEAT_DELAY_HZ (CONFIG_SYS_HZ/4) /* delay before repeat 250ms */
61 #define KP_REPEAT_HZ (CONFIG_SYS_HZ/20) /* repeat every 50ms */
62 #define KP_FORCE_DELAY_HZ (CONFIG_SYS_HZ/2) /* key was force pressed */
63 #define KP_IDLE_DELAY_HZ (CONFIG_SYS_HZ/2) /* key was released and idle */
65 #if CONFIG_NETPHONE_VERSION == 1
66 #define KP_SPI_RXD_PORT (((volatile immap_t *)CONFIG_SYS_IMMR)->im_ioport.iop_pcdat)
67 #define KP_SPI_RXD_MASK 0x0008
69 #define KP_SPI_TXD_PORT (((volatile immap_t *)CONFIG_SYS_IMMR)->im_ioport.iop_pcdat)
70 #define KP_SPI_TXD_MASK 0x0004
72 #define KP_SPI_CLK_PORT (((volatile immap_t *)CONFIG_SYS_IMMR)->im_ioport.iop_pcdat)
73 #define KP_SPI_CLK_MASK 0x0001
74 #elif CONFIG_NETPHONE_VERSION == 2
75 #define KP_SPI_RXD_PORT (((volatile immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_pbdat)
76 #define KP_SPI_RXD_MASK 0x00000008
78 #define KP_SPI_TXD_PORT (((volatile immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_pbdat)
79 #define KP_SPI_TXD_MASK 0x00000004
81 #define KP_SPI_CLK_PORT (((volatile immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_pbdat)
82 #define KP_SPI_CLK_MASK 0x00000002
85 #define KP_CS_PORT (((volatile immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_pedat)
86 #define KP_CS_MASK 0x00000010
88 #define KP_SPI_RXD() (KP_SPI_RXD_PORT & KP_SPI_RXD_MASK)
90 #define KP_SPI_TXD(x) \
93 KP_SPI_TXD_PORT |= KP_SPI_TXD_MASK; \
95 KP_SPI_TXD_PORT &= ~KP_SPI_TXD_MASK; \
98 #define KP_SPI_CLK(x) \
101 KP_SPI_CLK_PORT |= KP_SPI_CLK_MASK; \
103 KP_SPI_CLK_PORT &= ~KP_SPI_CLK_MASK; \
106 #define KP_SPI_CLK_TOGGLE() (KP_SPI_CLK_PORT ^= KP_SPI_CLK_MASK)
108 #define KP_SPI_BIT_DELAY() /* no delay */
113 KP_CS_PORT |= KP_CS_MASK; \
115 KP_CS_PORT &= ~KP_CS_MASK; \
121 #define KP_ROWS_MASK ((1 << KP_ROWS) - 1)
122 #define KP_COLS_MASK ((1 << KP_COLS) - 1)
125 #define SCAN_FILTER 1
127 #define SCAN_COL_FILTER 3
130 #define KP_F1 0 /* leftmost dot (tab) */
131 #define KP_F2 1 /* middle left dot */
132 #define KP_F3 2 /* up */
133 #define KP_F4 3 /* middle right dot */
134 #define KP_F5 4 /* rightmost dot */
135 #define KP_F6 5 /* C */
136 #define KP_F7 6 /* left */
137 #define KP_F8 7 /* down */
138 #define KP_F9 8 /* right */
139 #define KP_F10 9 /* enter */
140 #define KP_F11 10 /* R */
141 #define KP_F12 11 /* save */
142 #define KP_F13 12 /* redial */
143 #define KP_F14 13 /* speaker */
144 #define KP_F15 14 /* unused */
145 #define KP_F16 15 /* unused */
147 #define KP_RELEASE -1 /* key depressed */
148 #define KP_FORCE -2 /* key was pressed for more than force hz */
149 #define KP_IDLE -3 /* key was released and idle */
164 /*************************************************************************************************/
166 static int curs_disabled;
167 static int curs_col, curs_row;
168 static int disp_col, disp_row;
170 static int width, height;
172 /* the simulated vty buffer */
173 static char vty_buf[ROWS * COLS];
174 static char last_visible_buf[ROWS * COLS]; /* worst case */
175 static char *last_visible_curs_ptr;
176 static int last_visible_curs_rev;
177 static int blinked_state;
178 static int last_input_mode;
179 static int refresh_time;
180 static int blink_time;
181 static char last_fast_punct;
183 /*************************************************************************************************/
189 static int input_mode;
190 static char fast_punct;
191 static int tab_indicator;
192 static const char *fast_punct_list = ",.:;*";
194 static const char *input_mode_txt[] = { "abc", "ABC", "123" };
196 static const char *punct = ".,!;?'\"-()@/:_+&%*=<>$[]{}\\~^#|";
197 static const char *whspace = " 0\n";
198 /* per mode character select (for 2-9) */
199 static const char *digits_sel[2][8] = {
221 /*****************************************************************************/
223 static void update(void);
224 static void ensure_visible(int col, int row, int dx, int dy);
226 static void console_init(void)
235 input_mode = IM_SMALL;
237 last_fast_punct = '\0';
238 refresh_time = REFRESH_HZ;
239 blink_time = BLINK_HZ;
241 memset(vty_buf, ' ', sizeof(vty_buf));
243 memset(last_visible_buf, ' ', sizeof(last_visible_buf));
244 last_visible_curs_ptr = NULL;
245 last_input_mode = -1;
246 last_visible_curs_rev = 0;
251 width = sed156x_text_width;
252 height = sed156x_text_height - 1;
257 /*****************************************************************************/
259 void phone_putc(const char c);
261 /*****************************************************************************/
263 static int queued_char = -1;
264 static int enabled = 0;
266 /*****************************************************************************/
269 int phone_start(void)
277 queued_char = 'U' - '@';
280 DISPLAY_BACKLIT_PORT &= ~DISPLAY_BACKLIT_MASK;
293 DISPLAY_BACKLIT_PORT |= DISPLAY_BACKLIT_MASK;
298 void phone_puts(const char *s)
300 int count = strlen(s);
308 return queued_char >= 0 ? 1 : 0;
324 /*****************************************************************************/
326 int drv_phone_init(void)
328 struct stdio_dev console_dev;
332 memset(&console_dev, 0, sizeof(console_dev));
333 strcpy(console_dev.name, "phone");
334 console_dev.ext = DEV_EXT_VIDEO; /* Video extensions */
335 console_dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
336 console_dev.start = phone_start;
337 console_dev.stop = phone_stop;
338 console_dev.putc = phone_putc; /* 'putc' function */
339 console_dev.puts = phone_puts; /* 'puts' function */
340 console_dev.tstc = phone_tstc; /* 'tstc' function */
341 console_dev.getc = phone_getc; /* 'getc' function */
343 if (stdio_register(&console_dev) == 0)
351 int drv_phone_use_me(void)
356 static void kp_do_poll(void);
358 void phone_console_do_poll(void)
366 blink_time -= PHONE_CONSOLE_POLL_HZ;
367 if (blink_time <= 0) {
368 blink_time += BLINK_HZ;
369 if (last_visible_curs_ptr) {
370 i = last_visible_curs_ptr - last_visible_buf;
371 x = i % width; y = i / width;
372 sed156x_reverse_at(x, y, 1);
373 last_visible_curs_rev ^= 1;
378 refresh_time -= PHONE_CONSOLE_POLL_HZ;
379 if (refresh_time <= 0) {
380 refresh_time += REFRESH_HZ;
387 static int last_scancode = -1;
388 static int forced_scancode = 0;
389 static int input_state = -1;
390 static int input_scancode = -1;
391 static int input_selected_char = -1;
392 static char input_covered_char;
394 static void putchar_at_cursor(char c)
396 vty_buf[curs_row * COLS + curs_col] = c;
397 ensure_visible(curs_col, curs_row, 1, 1);
400 static char getchar_at_cursor(void)
402 return vty_buf[curs_row * COLS + curs_col];
405 static void queue_input_char(char c)
413 static void terminate_input(void)
418 if (input_selected_char >= 0)
419 queue_input_char(input_selected_char);
422 input_selected_char = -1;
423 putchar_at_cursor(input_covered_char);
426 blink_time = BLINK_HZ;
430 static void handle_enabled_scancode(int scancode)
433 int new_disp_col, new_disp_row;
439 /* key was released */
447 switch (last_scancode) {
449 if (input_mode == IM_NUMBER) {
450 input_mode = IM_CAPITAL;
451 /* queue backspace to erase # */
452 queue_input_char('\b');
454 input_mode = IM_NUMBER;
461 case '2': case '3': case '4': case '5':
462 case '6': case '7': case '8': case '9':
467 input_selected_char = last_scancode;
468 putchar_at_cursor((char)input_selected_char);
479 /* release and idle */
487 /* change input mode */
489 if (last_scancode == '#') /* no repeat */
492 if (input_mode == IM_NUMBER) {
493 input_scancode = scancode;
495 input_selected_char = scancode;
496 input_covered_char = getchar_at_cursor();
497 putchar_at_cursor((char)input_selected_char);
502 if (input_mode == IM_SMALL)
503 input_mode = IM_CAPITAL;
505 input_mode = IM_SMALL;
512 if (last_scancode == scancode)
515 if (input_state >= 0)
518 input_scancode = fast_punct;
520 input_selected_char = input_scancode;
521 input_covered_char = getchar_at_cursor();
522 putchar_at_cursor((char)input_selected_char);
528 case '2': case '3': case '4': case '5':
529 case '6': case '7': case '8': case '9':
532 if (last_scancode == scancode)
535 if (input_mode == IM_NUMBER) {
536 input_scancode = scancode;
538 input_selected_char = scancode;
539 input_covered_char = getchar_at_cursor();
540 putchar_at_cursor((char)input_selected_char);
545 if (input_state >= 0 && input_scancode != scancode)
548 if (input_state < 0) {
550 input_scancode = scancode;
552 input_covered_char = getchar_at_cursor();
558 else if (scancode == '1')
561 sel = digits_sel[input_mode][scancode - '2'];
562 c = *(sel + input_state);
568 input_selected_char = (int)c;
569 putchar_at_cursor((char)input_selected_char);
574 /* move visible display */
575 case KP_F3: case KP_F8: case KP_F7: case KP_F9:
577 new_disp_col = disp_col;
578 new_disp_row = disp_row;
583 if (new_disp_row <= 0)
590 if (new_disp_row >= ROWS - height)
597 if (new_disp_col <= 0)
604 if (new_disp_col >= COLS - width)
611 if (disp_col == new_disp_col && disp_row == new_disp_row)
614 disp_col = new_disp_col;
615 disp_row = new_disp_row;
620 case KP_F6: /* backspace */
621 /* inputing something; no backspace sent, just cancel input */
622 if (input_state >= 0) {
623 input_selected_char = -1; /* cancel */
627 queue_input_char('\b');
630 case KP_F10: /* enter */
631 /* inputing something; first cancel input */
632 if (input_state >= 0)
634 queue_input_char('\r');
637 case KP_F11: /* R -> Ctrl-C (abort) */
638 if (input_state >= 0)
640 queue_input_char('C' - 'Q'); /* ctrl-c */
643 case KP_F5: /* F% -> Ctrl-U (clear line) */
644 if (input_state >= 0)
646 queue_input_char('U' - 'Q'); /* ctrl-c */
650 case KP_F1: /* tab */
651 /* inputing something; first cancel input */
652 if (input_state >= 0)
654 queue_input_char('\t');
657 case KP_F2: /* change fast punct */
658 sel = strchr(fast_punct_list, fast_punct);
660 sel = &fast_punct_list[0];
663 sel = &fast_punct_list[0];
671 if (scancode != KP_FORCE && scancode != KP_IDLE) /* don't record forced or idle scancode */
672 last_scancode = scancode;
675 static void scancode_action(int scancode)
678 if (scancode == KP_RELEASE)
679 printf(" RELEASE\n");
680 else if (scancode == KP_FORCE)
682 else if (scancode == KP_IDLE)
684 else if (scancode < 32)
685 printf(" F%d", scancode + 1);
687 printf(" %c", (char)scancode);
692 handle_enabled_scancode(scancode);
696 if (scancode == KP_FORCE && last_scancode == '*')
699 last_scancode = scancode;
702 /**************************************************************************************/
704 /* update the display; make sure to update only the differences */
705 static void update(void)
708 char *s, *e, *t, *r, *b, *cp;
710 if (input_mode != last_input_mode)
711 sed156x_output_at(sed156x_text_width - 3, sed156x_text_height - 1, input_mode_txt[input_mode], 3);
713 if (tab_indicator == 0) {
714 sed156x_output_at(0, sed156x_text_height - 1, "\\t", 2);
718 if (fast_punct != last_fast_punct)
719 sed156x_output_at(4, sed156x_text_height - 1, &fast_punct, 1);
722 curs_col < disp_col || curs_col >= (disp_col + width) ||
723 curs_row < disp_row || curs_row >= (disp_row + height)) {
726 cp = last_visible_buf + (curs_row - disp_row) * width + (curs_col - disp_col);
729 /* printf("(%d,%d) (%d,%d) %s\n", curs_col, curs_row, disp_col, disp_row, cp ? "YES" : "no"); */
731 /* clear previous cursor */
732 if (last_visible_curs_ptr && last_visible_curs_rev == 0) {
733 i = last_visible_curs_ptr - last_visible_buf;
734 sed156x_reverse_at(i % width, i / width, 1);
737 b = vty_buf + disp_row * COLS + disp_col;
738 t = last_visible_buf;
739 for (i = 0; i < height; i++) {
742 /* update only the differences */
744 while (s < e && *s == *t) {
748 if (s == e) /* no more */
753 while (s < e && *s != *t)
757 sed156x_output_at(r - b, i, r, s - r);
766 last_visible_curs_ptr = cp;
767 i = last_visible_curs_ptr - last_visible_buf;
768 sed156x_reverse_at(i % width, i / width, 1);
769 last_visible_curs_rev = 0;
771 last_visible_curs_ptr = NULL;
774 last_input_mode = input_mode;
775 last_fast_punct = fast_punct;
778 /* ensure visibility; the trick is to minimize the screen movement */
779 static void ensure_visible(int col, int row, int dx, int dy)
781 int x1, y1, x2, y2, a1, b1, a2, b2;
783 /* clamp visible region */
805 /* move to easier to use vars */
806 x1 = disp_col; y1 = disp_row;
807 x2 = x1 + width; y2 = y1 + height;
809 a2 = a1 + dx; b2 = b1 + dy;
811 /* printf("(%d,%d) - (%d,%d) : (%d, %d) - (%d, %d)\n", x1, y1, x2, y2, a1, b1, a2, b2); */
814 /* move to the right */
821 } else if (a1 < x1) {
822 /* move to the left */
839 } else if (b1 < y1) {
849 /* printf("(%d,%d) - (%d,%d) : (%d, %d) - (%d, %d)\n", x1, y1, x2, y2, a1, b1, a2, b2); */
852 if (disp_col == x1 && disp_row == y1)
859 /**************************************************************************************/
861 static void newline(void)
864 if (curs_row + 1 < ROWS)
867 memmove(vty_buf, vty_buf + COLS, COLS * (ROWS - 1));
868 memset(vty_buf + (ROWS - 1) * COLS, ' ', COLS);
872 void phone_putc(const char c)
876 if (input_mode != -1) {
877 input_selected_char = -1;
884 blink_time = BLINK_HZ;
887 case '\a': /* ignore bell */
888 case '\r': /* ignore carriage return */
891 case '\n': /* next line */
893 ensure_visible(curs_col, curs_row, 1, 1);
907 ensure_visible(curs_col, curs_row, 1, 1);
910 case 8: /* backspace */
915 /* make sure that we see a couple of characters before */
917 ensure_visible(curs_col - 4, curs_row, 4, 1);
919 ensure_visible(curs_col, curs_row, 1, 1);
923 default: /* draw the char */
924 putchar_at_cursor(c);
929 if (curs_col + 1 < COLS)
934 ensure_visible(curs_col, curs_row, 1, 1);
940 blink_time = BLINK_HZ;
944 /**************************************************************************************/
946 static inline unsigned int kp_transfer(unsigned int val)
953 KP_SPI_TXD(val & 0x80);
967 unsigned int kp_data_transfer(unsigned int val)
971 val = kp_transfer(val);
977 unsigned int kp_get_col_mask(unsigned int row_mask)
979 unsigned int val, col_mask;
981 val = 0x80 | (row_mask & 0x7F);
982 (void)kp_data_transfer(val);
983 #if CONFIG_NETPHONE_VERSION == 1
984 col_mask = kp_data_transfer(val) & 0x0F;
985 #elif CONFIG_NETPHONE_VERSION == 2
986 col_mask = ((volatile immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_pedat & 0x0f;
987 /* XXX FUCK FUCK FUCK FUCK FUCK!!!! */
988 col_mask = ((col_mask & 0x08) >> 3) | /* BKBR1 */
989 ((col_mask & 0x04) << 1) | /* BKBR2 */
990 (col_mask & 0x02) | /* BKBR3 */
991 ((col_mask & 0x01) << 2); /* BKBR4 */
994 /* printf("col_mask(row_mask = 0x%x) -> col_mask = 0x%x\n", row_mask, col_mask); */
999 /**************************************************************************************/
1001 static const int kp_scancodes[KP_ROWS * KP_COLS] = {
1002 KP_F1, KP_F3, KP_F4, KP_F2,
1003 KP_F6, KP_F8, KP_F9, KP_F7,
1004 KP_1, KP_3, KP_F11, KP_2,
1005 KP_4, KP_6, KP_F12, KP_5,
1006 KP_7, KP_9, KP_F13, KP_8,
1007 KP_STAR, KP_HASH, KP_F14, KP_0,
1008 KP_F5, KP_F15, KP_F16, KP_F10,
1011 static const int kp_repeats[KP_ROWS * KP_COLS] = {
1021 static int kp_state = SCAN;
1022 static int kp_last_col_mask;
1023 static int kp_cur_row, kp_cur_col;
1024 static int kp_scancode;
1025 static int kp_stable;
1026 static int kp_repeat;
1027 static int kp_repeat_time;
1028 static int kp_force_time;
1029 static int kp_idle_time;
1031 static void kp_do_poll(void)
1033 unsigned int col_mask;
1038 if (kp_idle_time > 0) {
1039 kp_idle_time -= PHONE_CONSOLE_POLL_HZ;
1040 if (kp_idle_time <= 0)
1041 scancode_action(KP_IDLE);
1044 col_mask = kp_get_col_mask(KP_ROWS_MASK);
1045 if (col_mask == KP_COLS_MASK)
1046 break; /* nothing */
1047 kp_last_col_mask = col_mask;
1049 kp_state = SCAN_FILTER;
1053 col_mask = kp_get_col_mask(KP_ROWS_MASK);
1054 if (col_mask != kp_last_col_mask) {
1059 kp_stable += PHONE_CONSOLE_POLL_HZ;
1060 if (kp_stable < KP_STABLE_HZ)
1065 kp_state = SCAN_COL;
1067 (void)kp_get_col_mask(1 << kp_cur_row);
1071 col_mask = kp_get_col_mask(1 << kp_cur_row);
1072 if (col_mask == KP_COLS_MASK) {
1073 if (++kp_cur_row >= KP_ROWS) {
1077 kp_get_col_mask(1 << kp_cur_row);
1080 kp_last_col_mask = col_mask;
1082 kp_state = SCAN_COL_FILTER;
1085 case SCAN_COL_FILTER:
1086 col_mask = kp_get_col_mask(1 << kp_cur_row);
1087 if (col_mask != kp_last_col_mask || col_mask == KP_COLS_MASK) {
1092 kp_stable += PHONE_CONSOLE_POLL_HZ;
1093 if (kp_stable < KP_STABLE_HZ)
1096 for (col = 0; col < KP_COLS; col++)
1097 if ((col_mask & (1 << col)) == 0)
1101 kp_scancode = kp_scancodes[kp_cur_row * KP_COLS + kp_cur_col];
1102 kp_repeat = kp_repeats[kp_cur_row * KP_COLS + kp_cur_col];
1105 kp_repeat_time = KP_REPEAT_DELAY_HZ;
1106 kp_force_time = KP_FORCE_DELAY_HZ;
1108 scancode_action(kp_scancode);
1113 col_mask = kp_get_col_mask(1 << kp_cur_row);
1114 if (col_mask != kp_last_col_mask) {
1116 scancode_action(KP_RELEASE);
1117 kp_idle_time = KP_IDLE_DELAY_HZ;
1122 kp_repeat_time -= PHONE_CONSOLE_POLL_HZ;
1123 if (kp_repeat_time <= 0) {
1124 kp_repeat_time += KP_REPEAT_HZ;
1125 scancode_action(kp_scancode);
1129 if (kp_force_time > 0) {
1130 kp_force_time -= PHONE_CONSOLE_POLL_HZ;
1131 if (kp_force_time <= 0)
1132 scancode_action(KP_FORCE);
1139 /**************************************************************************************/
1141 int drv_phone_is_idle(void)
1143 return kp_state == SCAN;