Path: blob/master/arch/powerpc/platforms/powermac/udbg_adb.c
26481 views
// SPDX-License-Identifier: GPL-2.01#include <linux/string.h>2#include <linux/kernel.h>3#include <linux/errno.h>4#include <linux/bitops.h>5#include <linux/ptrace.h>6#include <linux/adb.h>7#include <linux/pmu.h>8#include <linux/cuda.h>9#include <linux/of.h>10#include <asm/machdep.h>11#include <asm/io.h>12#include <asm/page.h>13#include <asm/xmon.h>14#include <asm/bootx.h>15#include <asm/errno.h>16#include <asm/pmac_feature.h>17#include <asm/processor.h>18#include <asm/delay.h>19#include <asm/btext.h>20#include <asm/time.h>21#include <asm/udbg.h>2223/*24* This implementation is "special", it can "patch" the current25* udbg implementation and work on top of it. It must thus be26* initialized last27*/2829static void (*udbg_adb_old_putc)(char c);30static int (*udbg_adb_old_getc)(void);31static int (*udbg_adb_old_getc_poll)(void);3233static enum {34input_adb_none,35input_adb_pmu,36input_adb_cuda,37} input_type = input_adb_none;3839int xmon_wants_key, xmon_adb_keycode;4041static inline void udbg_adb_poll(void)42{43#ifdef CONFIG_ADB_PMU44if (input_type == input_adb_pmu)45pmu_poll_adb();46#endif /* CONFIG_ADB_PMU */47#ifdef CONFIG_ADB_CUDA48if (input_type == input_adb_cuda)49cuda_poll();50#endif /* CONFIG_ADB_CUDA */51}5253#ifdef CONFIG_BOOTX_TEXT5455static int udbg_adb_use_btext;56static int xmon_adb_shiftstate;5758static unsigned char xmon_keytab[128] =59"asdfhgzxcv\000bqwer" /* 0x00 - 0x0f */60"yt123465=97-80]o" /* 0x10 - 0x1f */61"u[ip\rlj'k;\\,/nm." /* 0x20 - 0x2f */62"\t `\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */63"\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */64"\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */6566static unsigned char xmon_shift_keytab[128] =67"ASDFHGZXCV\000BQWER" /* 0x00 - 0x0f */68"YT!@#$^%+(&_*)}O" /* 0x10 - 0x1f */69"U{IP\rLJ\"K:|<?NM>" /* 0x20 - 0x2f */70"\t ~\177\0\033\0\0\0\0\0\0\0\0\0\0" /* 0x30 - 0x3f */71"\0.\0*\0+\0\0\0\0\0/\r\0-\0" /* 0x40 - 0x4f */72"\0\0000123456789\0\0\0"; /* 0x50 - 0x5f */7374static int udbg_adb_local_getc(void)75{76int k, t, on;7778xmon_wants_key = 1;79for (;;) {80xmon_adb_keycode = -1;81t = 0;82on = 0;83k = -1;84do {85if (--t < 0) {86on = 1 - on;87btext_drawchar(on? 0xdb: 0x20);88btext_drawchar('\b');89t = 200000;90}91udbg_adb_poll();92if (udbg_adb_old_getc_poll)93k = udbg_adb_old_getc_poll();94} while (k == -1 && xmon_adb_keycode == -1);95if (on)96btext_drawstring(" \b");97if (k != -1)98return k;99k = xmon_adb_keycode;100101/* test for shift keys */102if ((k & 0x7f) == 0x38 || (k & 0x7f) == 0x7b) {103xmon_adb_shiftstate = (k & 0x80) == 0;104continue;105}106if (k >= 0x80)107continue; /* ignore up transitions */108k = (xmon_adb_shiftstate? xmon_shift_keytab: xmon_keytab)[k];109if (k != 0)110break;111}112xmon_wants_key = 0;113return k;114}115#endif /* CONFIG_BOOTX_TEXT */116117static int udbg_adb_getc(void)118{119#ifdef CONFIG_BOOTX_TEXT120if (udbg_adb_use_btext && input_type != input_adb_none)121return udbg_adb_local_getc();122#endif123if (udbg_adb_old_getc)124return udbg_adb_old_getc();125return -1;126}127128/* getc_poll() is not really used, unless you have the xmon-over modem129* hack that doesn't quite concern us here, thus we just poll the low level130* ADB driver to prevent it from timing out and call back the original poll131* routine.132*/133static int udbg_adb_getc_poll(void)134{135udbg_adb_poll();136137if (udbg_adb_old_getc_poll)138return udbg_adb_old_getc_poll();139return -1;140}141142static void udbg_adb_putc(char c)143{144#ifdef CONFIG_BOOTX_TEXT145if (udbg_adb_use_btext)146btext_drawchar(c);147#endif148if (udbg_adb_old_putc)149return udbg_adb_old_putc(c);150}151152void __init udbg_adb_init_early(void)153{154#ifdef CONFIG_BOOTX_TEXT155if (btext_find_display(1) == 0) {156udbg_adb_use_btext = 1;157udbg_putc = udbg_adb_putc;158}159#endif160}161162int __init udbg_adb_init(int force_btext)163{164struct device_node *np;165166/* Capture existing callbacks */167udbg_adb_old_putc = udbg_putc;168udbg_adb_old_getc = udbg_getc;169udbg_adb_old_getc_poll = udbg_getc_poll;170171/* Check if our early init was already called */172if (udbg_adb_old_putc == udbg_adb_putc)173udbg_adb_old_putc = NULL;174#ifdef CONFIG_BOOTX_TEXT175if (udbg_adb_old_putc == btext_drawchar)176udbg_adb_old_putc = NULL;177#endif178179/* Set ours as output */180udbg_putc = udbg_adb_putc;181udbg_getc = udbg_adb_getc;182udbg_getc_poll = udbg_adb_getc_poll;183184#ifdef CONFIG_BOOTX_TEXT185/* Check if we should use btext output */186if (btext_find_display(force_btext) == 0)187udbg_adb_use_btext = 1;188#endif189190/* See if there is a keyboard in the device tree with a parent191* of type "adb". If not, we return a failure, but we keep the192* bext output set for now193*/194for_each_node_by_name(np, "keyboard") {195struct device_node *parent = of_get_parent(np);196int found = of_node_is_type(parent, "adb");197of_node_put(parent);198if (found)199break;200}201if (np == NULL)202return -ENODEV;203of_node_put(np);204205#ifdef CONFIG_ADB_PMU206if (find_via_pmu())207input_type = input_adb_pmu;208#endif209#ifdef CONFIG_ADB_CUDA210if (find_via_cuda())211input_type = input_adb_cuda;212#endif213214/* Same as above: nothing found, keep btext set for output */215if (input_type == input_adb_none)216return -ENODEV;217218return 0;219}220221222