aboutsummaryrefslogtreecommitdiffstats
path: root/quantum/process_keycode/process_terminal.c
blob: 7d1eefa9edc3e84816b65827163e64efae23591b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* Copyright 2017 Jack Humbert
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "process_terminal.h"
#include <string.h>
#include "version.h"
#include <stdio.h>
#include <math.h>

#ifndef CMD_BUFF_SIZE
#    define CMD_BUFF_SIZE 5
#endif

bool terminal_enabled = false;
char buffer[80]       = "";
char cmd_buffer[CMD_BUFF_SIZE][80];
bool cmd_buffer_enabled = true;  // replace with ifdef?
char newline[2]         = "\n";
char arguments[6][20];
bool firstTime = true;

short int current_cmd_buffer_pos = 0;  // used for up/down arrows - keeps track of where you are in the command buffer

__attribute__((weak)) const char terminal_prompt[8] = "> ";

#ifdef AUDIO_ENABLE
#    ifndef TERMINAL_SONG
#        define TERMINAL_SONG SONG(TERMINAL_SOUND)
#    endif
float terminal_song[][2] = TERMINAL_SONG;
#    define TERMINAL_BELL() PLAY_SONG(terminal_song)
#else
#    define TERMINAL_BELL()
#endif

__attribute__((weak)) const char keycode_to_ascii_lut[58] = {0, 0, 0, 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0, 0, 0, '\t', ' ', '-', '=', '[', ']', '\\', 0, ';', '\'', '`', ',', '.', '/'};

__attribute__((weak)) const char shifted_keycode_to_ascii_lut[58] = {0, 0, 0, 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', 0, 0, 0, '\t', ' ', '_', '+', '{', '}', '|', 0, ':', '\'', '~', '<', '>', '?'};

struct stringcase {
    char *string;
    void (*func)(void);
} typedef stringcase;

void enable_terminal(void) {
    terminal_enabled = true;
    strcpy(buffer, "");
    memset(cmd_buffer, 0, CMD_BUFF_SIZE * 80);
    for (int i = 0; i < 6; i++) strcpy(arguments[i], "");
    // select all text to start over
    // SEND_STRING(SS_LCTL("a"));
    send_string(terminal_prompt);
}

void disable_terminal(void) {
    terminal_enabled = false;
    SEND_STRING("\n");
}

void push_to_cmd_buffer(void) {
    if (cmd_buffer_enabled) {
        if (cmd_buffer == NULL) {
            return;
        } else {
            if (firstTime) {
                firstTime = false;
                strcpy(cmd_buffer[0], buffer);
                return;
            }

            for (int i = CMD_BUFF_SIZE - 1; i > 0; --i) {
                strncpy(cmd_buffer[i], cmd_buffer[i - 1], 80);
            }

            strcpy(cmd_buffer[0], buffer);

            return;
        }
    }
}

void terminal_about(void) {
    SEND_STRING("QMK Firmware\n");
    SEND_STRING("  v");
    SEND_STRING(QMK_VERSION);
    SEND_STRING("\n" SS_TAP(X_HOME) "  Built: ");
    SEND_STRING(QMK_BUILDDATE);
    send_string(newline);
#ifdef TERMINAL_HELP
    if (strlen(arguments[1]) != 0) {
        SEND_STRING("You entered: ");
        send_string(arguments[1]);
        send_string(newline);
    }
#endif
}

void terminal_help(void);

extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];

void terminal_keycode(void) {
    if (strlen(arguments[1]) != 0 && strlen(arguments[2]) != 0 && strlen(arguments[3]) != 0) {
        char     keycode_dec[5];
        char     keycode_hex[5];
        uint16_t layer   = strtol(arguments[1], (char **)NULL, 10);
        uint16_t row     = strtol(arguments[2], (char **)NULL, 10);
        uint16_t col     = strtol(arguments[3], (char **)NULL, 10);
        uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
        itoa(keycode, keycode_dec, 10);
        itoa(keycode, keycode_hex, 16);
        SEND_STRING("0x");
        send_string(keycode_hex);
        SEND_STRING(" (");
        send_string(keycode_dec);
        SEND_STRING(")\n");
    } else {
#ifdef TERMINAL_HELP
        SEND_STRING("usage: keycode <layer> <row> <col>\n");
#endif
    }
}

void terminal_keymap(void) {
    if (strlen(arguments[1]) != 0) {
        uint16_t layer = strtol(arguments[1], (char **)NULL, 10);
        for (int r = 0; r < MATRIX_ROWS; r++) {
            for (int c = 0; c < MATRIX_COLS; c++) {
                uint16_t keycode = pgm_read_word(&keymaps[layer][r][c]);
                char     keycode_s[8];
                sprintf(keycode_s, "0x%04x,", keycode);
                send_string(keycode_s);
            }
            send_string(newline);
        }
    } else {
#ifdef TERMINAL_HELP
        SEND_STRING("usage: keymap <layer>\n");
#endif
    }
}

void print_cmd_buff(void) {
    /* without the below wait, a race condition can occur wherein the
     buffer can be printed before it has been fully moved */
    wait_ms(250);
    for (int i = 0; i < CMD_BUFF_SIZE; i++) {
        char tmpChar = ' ';
        itoa(i, &tmpChar, 10);
        const char *tmpCnstCharStr = &tmpChar;  // because sned_string wont take a normal char *
        send_string(tmpCnstCharStr);
        SEND_STRING(". ");
        send_string(cmd_buffer[i]);
        SEND_STRING("\n");
    }
}

void flush_cmd_buffer(void) {
    memset(cmd_buffer, 0, CMD_BUFF_SIZE * 80);
    SEND_STRING("Buffer Cleared!\n");
}

stringcase terminal_cases[] = {{"about", terminal_about}, {"help", terminal_help}, {"keycode", terminal_keycode}, {"keymap", terminal_keymap}, {"flush-buffer", flush_cmd_buffer}, {"print-buffer", print_cmd_buff}, {"exit", disable_terminal}};

void terminal_help(void) {
    SEND_STRING("commands available:\n  ");
    for (stringcase *case_p = terminal_cases; case_p != terminal_cases + sizeof(terminal_cases) / sizeof(terminal_cases[0]); case_p++) {
        send_string(case_p->string);
        SEND_STRING(" ");
    }
    send_string(newline);
}

void command_not_found(void) {
    wait_ms(50);  // sometimes buffer isnt grabbed quick enough
    SEND_STRING("command \"");
    send_string(buffer);
    SEND_STRING("\" not found\n");
}

void process_terminal_command(void) {
    // we capture return bc of the order of events, so we need to manually send a newline
    send_string(newline);

    char *  pch;
    uint8_t i = 0;
    pch       = strtok(buffer, " ");
    while (pch != NULL) {
        strcpy(arguments[i], pch);
        pch = strtok(NULL, " ");
        i++;
    }

    bool command_found = false;
    for (stringcase *case_p = terminal_cases; case_p != terminal_cases + sizeof(terminal_cases) / sizeof(terminal_cases[0]); case_p++) {
        if (0 == strcmp(case_p->string, buffer)) {
            command_found = true;
            (*case_p->func)();
            break;
        }
    }

    if (!command_found) command_not_found();

    if (terminal_enabled) {
        strcpy(buffer, "");
        for (int i = 0; i < 6; i++) strcpy(arguments[i], "");
        SEND_STRING(SS_TAP(X_HOME));
        send_string(terminal_prompt);
    }
}
void check_pos(void) {
    if (current_cmd_buffer_pos >= CMD_BUFF_SIZE) {  // if over the top, move it back down to the top of the buffer so you can climb back down...
        current_cmd_buffer_pos = CMD_BUFF_SIZE - 1;
    } else if (current_cmd_buffer_pos < 0) {  //...and if you fall under the bottom of the buffer, reset back to 0 so you can climb back up
        current_cmd_buffer_pos = 0;
    }
}

bool process_terminal(uint16_t keycode, keyrecord_t *record) {
    if (keycode == TERM_ON && record->event.pressed) {
        enable_terminal();
        return false;
    }

    if (terminal_enabled && record->event.pressed) {
        if (keycode == TERM_OFF && record->event.pressed) {
            disable_terminal();
            return false;
        }

        if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
            keycode = keycode & 0xFF;
        }

        if (keycode < 256) {
            uint8_t str_len;
            char    char_to_add;
            switch (keycode) {
                case KC_ENTER:
                case KC_KP_ENTER:
                    push_to_cmd_buffer();
                    current_cmd_buffer_pos = 0;
                    process_terminal_command();
                    return false;
                    break;
                case KC_ESC:
                    SEND_STRING("\n");
                    enable_terminal();
                    return false;
                    break;
                case KC_BSPC:
                    str_len = strlen(buffer);
                    if (str_len > 0) {
                        buffer[str_len - 1] = 0;
                        return true;
                    } else {
                        TERMINAL_BELL();
                        return false;
                    }
                    break;
                case KC_LEFT:
                    return false;
                    break;
                case KC_RIGHT:
                    return false;
                    break;
                case KC_UP:                                             // 0 = recent
                    check_pos();                                        // check our current buffer position is valid
                    if (current_cmd_buffer_pos <= CMD_BUFF_SIZE - 1) {  // once we get to the top, dont do anything
                        str_len = strlen(buffer);
                        for (int i = 0; i < str_len; ++i) {
                            send_string(SS_TAP(X_BSPACE));  // clear w/e is on the line already
                            // process_terminal(KC_BSPC,record);
                        }
                        strncpy(buffer, cmd_buffer[current_cmd_buffer_pos], 80);

                        send_string(buffer);
                        ++current_cmd_buffer_pos;  // get ready to access the above cmd if up/down is pressed again
                    }
                    return false;
                    break;
                case KC_DOWN:
                    check_pos();
                    if (current_cmd_buffer_pos >= 0) {  // once we get to the bottom, dont do anything
                        str_len = strlen(buffer);
                        for (int i = 0; i < str_len; ++i) {
                            send_string(SS_TAP(X_BSPACE));  // clear w/e is on the line already
                            // process_terminal(KC_BSPC,record);
                        }
                        strncpy(buffer, cmd_buffer[current_cmd_buffer_pos], 79);

                        send_string(buffer);
                        --current_cmd_buffer_pos;  // get ready to access the above cmd if down/up is pressed again
                    }
                    return false;
                    break;
                default:
                    if (keycode <= 58) {
                        char_to_add = 0;
                        if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
                            char_to_add = shifted_keycode_to_ascii_lut[keycode];
                        } else if (get_mods() == 0) {
                            char_to_add = keycode_to_ascii_lut[keycode];
                        }
                        if (char_to_add != 0) {
                            strncat(buffer, &char_to_add, 1);
                        }
                    }
                    break;
            }
        }
    }
    return true;
}
in reasonable speed. + * @max_bus_speed: The maximum speed of the SPI bus, in Hertz. + */ +struct gpiommc_platform_data { + char name[GPIOMMC_MAX_NAMELEN + 1]; + struct gpiommc_pins pins; + u8 mode; + bool no_spi_delay; + unsigned int max_bus_speed; +}; + +/** + * GPIOMMC_PLATDEV_NAME - The platform device name string. + * + * The name string that has to be used for platform_device_alloc + * when allocating a gpiommc device. + */ +#define GPIOMMC_PLATDEV_NAME "gpiommc" + +/** + * gpiommc_next_id - Get another platform device ID number. + * + * This returns the next platform device ID number that has to be used + * for platform_device_alloc. The ID is opaque and should not be used for + * anything else. + */ +int gpiommc_next_id(void); + +#endif /* LINUX_GPIOMMC_H_ */ --- /dev/null +++ b/Documentation/gpiommc.txt @@ -0,0 +1,97 @@ +GPIOMMC - Driver for an MMC/SD card on a bitbanging GPIO SPI bus +================================================================ + +The gpiommc module hooks up the mmc_spi and spi_gpio modules for running an +MMC or SD card on GPIO pins. + +Two interfaces for registering a new MMC/SD card device are provided: +A static platform-device based mechanism and a dynamic configfs based interface. + + +Registering devices via platform-device +======================================= + +The platform-device interface is used for registering MMC/SD devices that are +part of the hardware platform. This is most useful only for embedded machines +with MMC/SD devices statically connected to the platform GPIO bus. + +The data structures are declared in <linux/mmc/gpiommc.h>. + +To register a new device, define an instance of struct gpiommc_platform_data. +This structure holds any information about how the device is hooked up to the +GPIO pins and what hardware modes the device supports. See the docbook-style +documentation in the header file for more information on the struct fields. + +Then allocate a new instance of a platform device by doing: + + pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME, gpiommc_next_id()); + +This will allocate the platform device data structures and hook it up to the +gpiommc driver. +Then add the gpiommc_platform_data to the platform device. + + err = platform_device_add_data(pdev, pdata, sizeof(struct gpiommc_platform_data)); + +You may free the local instance of struct gpiommc_platform_data now. (So the +struct may be allocated on the stack, too). +Now simply register the platform device. + + err = platform_device_add(pdev); + +Done. The gpiommc probe routine will be invoked now and you should see a kernel +log message for the added device. + + +Registering devices via configfs +================================ + +MMC/SD cards connected via GPIO often are a pretty dynamic thing, as for example +selfmade hacks for soldering an MMC/SD card to standard GPIO pins on embedded +hardware are a common situation. +So we provide a dynamic interface to conveniently handle adding and removing +devices from userspace, without the need to recompile the kernel. + +The "gpiommc" subdirectory at the configfs mountpoint is used for handling +the dynamic configuration. + +To create a new device, it must first be allocated with mkdir. +The following command will allocate a device named "my_mmc": + mkdir /config/gpiommc/my_mmc + +There are several configuration files available in the new +/config/gpiommc/my_mmc/ directory: + +gpio_data_in = The SPI data-IN GPIO pin number. +gpio_data_out = The SPI data-OUT GPIO pin number. +gpio_clock = The SPI Clock GPIO pin number. +gpio_chipselect = The SPI Chipselect GPIO pin number. +gpio_chipselect_activelow = Boolean. If 0, Chipselect is active-HIGH. + If 1, Chipselect is active-LOW. +spi_mode = The SPI data mode. Can be 0-3. +spi_delay = Enable all delays in the lowlevel bitbanging. +max_bus_speed = The maximum SPI bus speed. In Hertz. + +register = Not a configuration parameter. + Used to register the configured card + with the kernel. + +The device must first get configured and then registered by writing "1" to +the "register" file. +The configuration parameters "gpio_data_in", "gpio_data_out", "gpio_clock" +and "gpio_chipselect" are essential and _must_ be configured before writing +"1" to the "register" file. The registration will fail, otherwise. + +The default values for the other parameters are: +gpio_chipselect_activelow = 1 (CS active-LOW) +spi_mode = 0 (SPI_MODE_0) +spi_delay = 1 (enabled) +max_bus_speed = 5000000 (5 Mhz) + +Configuration values can not be changed after registration. To unregister +the device, write a "0" to the "register" file. The configuration can be +changed again after unregistering. + +To completely remove the device, simply rmdir the directory +(/config/gpiommc/my_mmc in this example). +There's no need to first unregister the device before removing it. That will +be done automatically. --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3507,6 +3507,11 @@ L: linuxppc-dev@lists.ozlabs.org S: Odd Fixes F: drivers/tty/hvc/ +GPIOMMC DRIVER +P: Michael Buesch +M: mb@bu3sch.de +S: Maintained + HARDWARE MONITORING M: Jean Delvare <khali@linux-fr.org> M: Guenter Roeck <linux@roeck-us.net>