diff mbox series

[qemu,v3,1/1] Emulating sun keyboard language layout dip switches, taking the value for the dip switches from the "-k" option to qemu.

Message ID 167305907804.10652.2746409341903316717-1@git.sr.ht (mailing list archive)
State New, archived
Headers show
Series Emulate dip switch language layout settings on SUN keyboard | expand

Commit Message

~henca Jan. 6, 2023, 9:33 p.m. UTC
From: Henrik Carlqvist <hc1245@poolhem.se>

SUN Type 4, 5 and 5c keyboards have dip switches to choose the language
layout of the keyboard. Solaris makes an ioctl to query the value of the
dipswitches and uses that value to select keyboard layout. Also the SUN
bios like the one in the file ss5.bin uses this value to support at least
some keyboard layouts. However, the OpenBIOS provided with qemu is
hardcoded to always use an US keyboard layout.

Before this patch, qemu allways gave dip switch value 0x21 (US keyboard),
this patch uses the command line switch "-k" (keyboard layout) to select
dip switch value. A table is used to lookup values from arguments like:

-k fr
-k es

But the patch also accepts numeric dip switch values directly to the -k
switch:

-k 0x2b
-k 43

Both values above are the same and select swedish keyboard as explained in
table 3-15 at
https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html

Unless you want to do a full Solaris installation but happen to have
access to a bios file, the easiest way to test that the patch works is to:

qemu-system-sparc -k sv -bios /path/to/ss5.bin

If you already happen to have a Solaris installation in a qemu disk image
file you can easily try different keyboard layouts after this patch is
applied.
---
 hw/char/escc.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 73 insertions(+), 1 deletion(-)

Comments

Mark Cave-Ayland Jan. 10, 2023, 11:08 p.m. UTC | #1
On 06/01/2023 21:33, ~henca wrote:

> From: Henrik Carlqvist <hc1245@poolhem.se>
> 
> SUN Type 4, 5 and 5c keyboards have dip switches to choose the language
> layout of the keyboard. Solaris makes an ioctl to query the value of the
> dipswitches and uses that value to select keyboard layout. Also the SUN
> bios like the one in the file ss5.bin uses this value to support at least
> some keyboard layouts. However, the OpenBIOS provided with qemu is
> hardcoded to always use an US keyboard layout.
> 
> Before this patch, qemu allways gave dip switch value 0x21 (US keyboard),
> this patch uses the command line switch "-k" (keyboard layout) to select
> dip switch value. A table is used to lookup values from arguments like:
> 
> -k fr
> -k es
> 
> But the patch also accepts numeric dip switch values directly to the -k
> switch:
> 
> -k 0x2b
> -k 43
> 
> Both values above are the same and select swedish keyboard as explained in
> table 3-15 at
> https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html
> 
> Unless you want to do a full Solaris installation but happen to have
> access to a bios file, the easiest way to test that the patch works is to:
> 
> qemu-system-sparc -k sv -bios /path/to/ss5.bin
> 
> If you already happen to have a Solaris installation in a qemu disk image
> file you can easily try different keyboard layouts after this patch is
> applied.
> ---
>   hw/char/escc.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++-
>   1 file changed, 73 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/char/escc.c b/hw/char/escc.c
> index 17a908c59b..53022ccf39 100644
> --- a/hw/char/escc.c
> +++ b/hw/char/escc.c
> @@ -31,6 +31,8 @@
>   #include "qemu/module.h"
>   #include "hw/char/escc.h"
>   #include "ui/console.h"
> +#include "sysemu/sysemu.h"
> +#include "qemu/cutils.h"
>   #include "trace.h"
>   
>   /*
> @@ -190,6 +192,7 @@
>   #define R_MISC1I 14
>   #define R_EXTINT 15
>   
> +static unsigned char sun_keyboard_layout_dip_switch(void);
>   static void handle_kbd_command(ESCCChannelState *s, int val);
>   static int serial_can_receive(void *opaque);
>   static void serial_receive_byte(ESCCChannelState *s, int ch);
> @@ -846,6 +849,75 @@ static QemuInputHandler sunkbd_handler = {
>       .event = sunkbd_handle_event,
>   };
>   
> +static unsigned char sun_keyboard_layout_dip_switch(void)
> +{
> +    /* Return the value of the dip-switches in a SUN Type 5 keyboard */
> +    static unsigned char ret = 0xff;
> +
> +    if ((ret == 0xff) && keyboard_layout) {
> +        int i;
> +        struct layout_values {
> +            const char *lang;
> +            unsigned char dip;
> +        } languages[] =
> +    /* Dip values from table 3-16 Layouts for Type 4, 5, and 5c Keyboards */
> +            {
> +                {"en-us", 0x21}, /* U.S.A. (US5.kt) */
> +                                 /* 0x22 is some other US (US_UNIX5.kt)*/
> +                {"fr",    0x23}, /* France (France5.kt) */
> +                {"da",    0x24}, /* Denmark (Denmark5.kt) */
> +                {"de",    0x25}, /* Germany (Germany5.kt) */
> +                {"it",    0x26}, /* Italy (Italy5.kt) */
> +                {"nl",    0x27}, /* The Netherlands (Netherland5.kt) */
> +                {"no",    0x28}, /* Norway (Norway.kt) */
> +                {"pt",    0x29}, /* Portugal (Portugal5.kt) */
> +                {"es",    0x2a}, /* Spain (Spain5.kt) */
> +                {"sv",    0x2b}, /* Sweden (Sweden5.kt) */
> +                {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */
> +                {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */
> +                {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */
> +                {"ko",    0x2f}, /* Korea (Korea5.kt) */
> +                {"tw",    0x30}, /* Taiwan (Taiwan5.kt) */
> +                {"ja",    0x31}, /* Japan (Japan5.kt) */
> +                {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */
> +                {"hu",    0x33}, /* Hungary (Hungary5.kt) */
> +                {"pl",    0x34}, /* Poland (Poland5.kt) */
> +                {"cz",    0x35}, /* Czech (Czech5.kt) */
> +                {"ru",    0x36}, /* Russia (Russia5.kt) */
> +                {"lv",    0x37}, /* Latvia (Latvia5.kt) */
> +                {"tr",    0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */
> +                {"gr",    0x39}, /* Greece (Greece5.kt) */
> +                {"ar",    0x3a}, /* Arabic (Arabic5.kt) */
> +                {"lt",    0x3b}, /* Lithuania (Lithuania5.kt) */
> +                {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */
> +                {"be",    0x3c}, /* Belgium (Belgian5.kt) */
> +            };
> +
> +        for (i = 0;
> +             i < sizeof(languages) / sizeof(struct layout_values);
> +             i++) {
> +            if (!strcmp(keyboard_layout, languages[i].lang)) {
> +                ret = languages[i].dip;
> +                return ret;
> +            }
> +        }
> +        /* Found no known language code */
> +
> +        if ((keyboard_layout[0] >= '0') && (keyboard_layout[0] <= '9')) {
> +            unsigned int tmp;
> +            /* As a fallback we also accept numeric dip switch value */
> +            if (!qemu_strtoui(keyboard_layout, NULL, 0, &tmp)) {
> +                ret = (unsigned char)tmp;
> +            }
> +        }
> +    }
> +    if (ret == 0xff) {
> +        /* Final fallback if keyboard_layout was not set or recognized */
> +        ret = 0x21; /* en-us layout */
> +    }
> +    return ret;
> +}
> +
>   static void handle_kbd_command(ESCCChannelState *s, int val)
>   {
>       trace_escc_kbd_command(val);
> @@ -867,7 +939,7 @@ static void handle_kbd_command(ESCCChannelState *s, int val)
>       case 0xf:
>           clear_queue(s);
>           put_queue(s, 0xfe);
> -        put_queue(s, 0x21); /*  en-us layout */
> +        put_queue(s, sun_keyboard_layout_dip_switch());
>           break;
>       default:
>           break;

Thanks for the patch. I think what you're effectively doing here is equivalent to 
creating a qdev property that is used to set the keyboard layout, which is a nicer 
approach because it enables better control from the command line and also handles the 
default value.

Does changing the keyboard layout work with other OSs? It may also be that OpenBIOS 
will be difficult to use if the layout defaults to anything other than en-us since I 
suspect it will be hardcoded there too.


ATB,

Mark.
Henrik Carlqvist Jan. 23, 2023, 7:09 p.m. UTC | #2
On Sun, 22 Jan 2023 18:07:47 +0000
Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> wrote:
> Did you see my comments re: OpenBIOS for the earlier version of this patch?

Thanks for your replies! Sorry, I missed that reply... To make sure that I
wouldn't miss any reply I subscribed to the mailing list, but that made many
mails to me drown in mails from the list...

On Tue, 10 Jan 2023 23:08:27 +0000
Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> wrote:
> Thanks for the patch. I think what you're effectively doing here is
> equivalent to creating a qdev property that is used to set the keyboard
> layout, which is a nicer approach because it enables better control from the
> command line and also handles the default value.

I must admit that I am not very familiar with "qdev properties", to me it
seemed like a rather intuitive solution to use the existing -k switch for this
purpose as it is intended to use for keyboard layouts. Would you prefer some
other implementation?

> Does changing the keyboard layout work with other OSs?

To my knowledge only Solaris and the SUN sparc openboot bios cares about those
dip switch settings in the 32 bit SUN sparc machine keyboards.

This post on a forum makes me think that it doesn't work with NetBSD:
https://www.linuxquestions.org/questions/%2Absd-17/netbsd-sparc-non-us-keyboard-layout-issue-367601/

It does seem as if the Linux kernel supports those layout dip switches,
however I don't know if it is used in any Linux distribution. I have never run
any Linux distribution on sparc myself. From the Linux kernel source file
sunkbd.c:

-8<--------------------
	if (sunkbd->type == 4) {	/* Type 4 keyboard */
		sunkbd->layout = -2;
		serio_write(sunkbd->serio, SUNKBD_CMD_LAYOUT);
		wait_event_interruptible_timeout(sunkbd->wait,
						 sunkbd->layout >= 0, HZ / 4);
		if (sunkbd->layout < 0)
			return -1;
		if (sunkbd->layout & SUNKBD_LAYOUT_5_MASK)
			sunkbd->type = 5;
	}
-8<--------------------

> It may also be that OpenBIOS will be difficult to use if the layout
> defaults to anything other than en-us since I suspect it will be hardcoded
> there too.

Today qemu is hardcoded to emulate dip switch setting 0x21 (en-us layout). My
patch keeps 0x21 (en-us layout) as default value, but also makes it possible
to give other values with the -k option. From may experience it seems as if
OpenBIOS does not care about the settings of the dip switch and allways uses
en-us layout. Suns openboot bios (which is possible to find for download and
use with qemu) supports some, but not all layout dip switch settings which are
supported by Solaris.

regards Henrik
Henrik Carlqvist March 4, 2023, 9:07 p.m. UTC | #3
On Wed, 1 Mar 2023 11:52:42 +0000
Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> wrote:
> I've done a quick grep for similar examples for serial devices that use
> keyboard_layout but it looks like this would be the first. 

Thanks again for your reply!

It is probably not a very common solution with hardware in the keyboard
telling the computer about its language layout. Maybe it is even less common
with such hardware possible to adjust with a dip switch to choose language
layout. But that was the solution Sun selected for their keybords back in the
days and back then (and even still today) it was common for keyboards to
communicate with the computer by some kind of serial interface.

> My first instinct is that you'd want to make this a device property that is
> configured during machine init using keyboard_layout (rather than using
> keyboard_layout directly),

If it does matter in any way, my patch will only at first call to the
sun_keyboard_layout_dip_switch function look at the keyboard_layout variable.
Once the static "ret" variable has gotten a valid value (the initial value
0xff is not valid for a 6-bit dip swithc) the ret value assigned at the
initial call will be immediately returned without any more evaluations of the
keyboard_layout string.

> but I'd be interested to hear what Paolo and Marc-André think about what the
> best approach should be.

As I found your latest reply only on
https://lists.nongnu.org/archive/html/qemu-devel/2023-03/msg00097.html and
can't see the email addresses of any receiver of that post this reply does not
go to Paolo or Marc-André, only to you and the mailing list.

I must admit that I am not very failiar with qemu programming and device
properties. 

> Another aspect to consider is whether keyboard_layout should just use
> standard strings, in which case it may not make sense to accept numeric hex
> values. 

I agree that those standard strings will make most sense to most people.

However, as the choices of valid keyboard layouts are limited by the 64 values
allowed by the 6 bits on the dip switch I initially did choose to also truly
emulate the dip switch value as decimal or hexadecimal number to the -k
option. It might also be worth noting that the sun keyboard layouts have
multiple dip switch settings for a single language, probably with some minor
differences in keyboard layout or keyboard type. So both value 8 and 40 (0x28)
will give some norwegian keyboard layout. if someone, for some reason, would
want to emulate for example one of the four possible US keyboard layouts (0x0,
0x1, 0x21 or 0x22) it would be harder to do without being able to give those
numerical values to the -k switch.

best regards Henrik
Daniel P. Berrangé March 28, 2023, 2:01 p.m. UTC | #4
On Sat, Mar 04, 2023 at 10:07:54PM +0100, Henrik Carlqvist wrote:
> On Wed, 1 Mar 2023 11:52:42 +0000
> Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> wrote:
> > Another aspect to consider is whether keyboard_layout should just use
> > standard strings, in which case it may not make sense to accept numeric hex
> > values. 
> 
> I agree that those standard strings will make most sense to most people.
> 
> However, as the choices of valid keyboard layouts are limited by the 64 values
> allowed by the 6 bits on the dip switch I initially did choose to also truly
> emulate the dip switch value as decimal or hexadecimal number to the -k
> option. It might also be worth noting that the sun keyboard layouts have
> multiple dip switch settings for a single language, probably with some minor
> differences in keyboard layout or keyboard type. So both value 8 and 40 (0x28)
> will give some norwegian keyboard layout. if someone, for some reason, would
> want to emulate for example one of the four possible US keyboard layouts (0x0,
> 0x1, 0x21 or 0x22) it would be harder to do without being able to give those
> numerical values to the -k switch.

This is another reason why use of the '-k' switch is a bad idea. Its
range of permissible values / vocabulary does not match the range of
values / vocabulary needed for this hardware device.

In https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html
the keyboard layouts have distinct names

"Norway4" vs "Norway5" and "US4" vs  "US5" vs "US_UNIX5"

I'd suggest a property to the escc device should take the names
given by that reference page above. eg

  -global escc.sunkbd_layout=Norway4

the only ambguity I see is that 0x0 and 0x1 both have the same
name (US4), which could be resolved by handling 0x0 as the default
with an empty string perhaps.

With regards,
Daniel
Henrik Carlqvist March 28, 2023, 5:19 p.m. UTC | #5
Thanks for your feedback!

On Tue, 28 Mar 2023 15:01:55 +0100
Daniel P. Berrangé <berrange@redhat.com> wrote:

> This is another reason why use of the '-k' switch is a bad idea. Its
> range of permissible values / vocabulary does not match the range of
> values / vocabulary needed for this hardware device.
> 
> In https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html
> the keyboard layouts have distinct names
> 
> "Norway4" vs "Norway5" and "US4" vs  "US5" vs "US_UNIX5"

Those distinct names are names of files in the OS filesystem. This is a link
to a description of a patch which gives those keyboard layouts support for
the euro sign:

http://download.nust.na/pub3/solaris/patches/106839.readme

> I'd suggest a property to the escc device should take the names
> given by that reference page above. eg
> 
>   -global escc.sunkbd_layout=Norway4

Would you mind if such an assignment could also be given in multiple ways,
that is:

-global escc.sunkbd_layout=33
-global escc.sunkbd_layout=0x21
-global escc.sunkbd_layout=US5
-global escc.sunkbd_layout=en_us

would all result in the same dip switch setting 0x21? 

The nice thing with being able to assign keyboard layouts with a string like
"en_us" is that it does not require the user to read reference documentation
from Oracle to see which odd named layouts to choose from.

The nice thing to also being able to give numerical values like 33 or 0x21 is
that some possible dip switch settings (like 0x20) are not mentioned in the
Oracle reference documentation, but of course they would be possible to set
with physical dip switches even though they might not be supported by the OS.

> the only ambguity I see is that 0x0 and 0x1 both have the same
> name (US4), which could be resolved by handling 0x0 as the default
> with an empty string perhaps.

With multiple ways to give the values as I suggest it would be possible to
give 0x0 and 0x1 as values but "US4" would allways result in one of them,
probably 0x0. 

The default value when no value is given or when some invalid value is given
to escc.sunkbd_layout would preferably be 0x21 for backwards compability as
that is the only value you can get from the dip switch in qemu today.

Once we find a method we all agree on I am willing to rewrite my patch, but
maybe I will not be able to do it before summer when I get my vacation.

best regards Henrik
Daniel P. Berrangé March 28, 2023, 5:59 p.m. UTC | #6
On Tue, Mar 28, 2023 at 07:19:58PM +0200, Henrik Carlqvist wrote:
> Thanks for your feedback!
> 
> On Tue, 28 Mar 2023 15:01:55 +0100
> Daniel P. Berrangé <berrange@redhat.com> wrote:
> 
> > This is another reason why use of the '-k' switch is a bad idea. Its
> > range of permissible values / vocabulary does not match the range of
> > values / vocabulary needed for this hardware device.
> > 
> > In https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html
> > the keyboard layouts have distinct names
> > 
> > "Norway4" vs "Norway5" and "US4" vs  "US5" vs "US_UNIX5"
> 
> Those distinct names are names of files in the OS filesystem. This is a link
> to a description of a patch which gives those keyboard layouts support for
> the euro sign:
> 
> http://download.nust.na/pub3/solaris/patches/106839.readme
> 
> > I'd suggest a property to the escc device should take the names
> > given by that reference page above. eg
> > 
> >   -global escc.sunkbd_layout=Norway4
> 
> Would you mind if such an assignment could also be given in multiple ways,
> that is:
> 
> -global escc.sunkbd_layout=33
> -global escc.sunkbd_layout=0x21
> -global escc.sunkbd_layout=US5
> -global escc.sunkbd_layout=en_us
> 
> would all result in the same dip switch setting 0x21?

I'm generally not in favour of creating many different ways to set
the same thing, especially not multiple string based names, but if
a single vocabulary for strings is insufficient, then having support
for numbers feels reasonable.

> The nice thing with being able to assign keyboard layouts with a string like
> "en_us" is that it does not require the user to read reference documentation
> from Oracle to see which odd named layouts to choose from.
> 
> The nice thing to also being able to give numerical values like 33 or 0x21 is
> that some possible dip switch settings (like 0x20) are not mentioned in the
> Oracle reference documentation, but of course they would be possible to set
> with physical dip switches even though they might not be supported by the OS.

If you care about documentation then I think there ought to be docs
added in QEMU for it, as out of the 33 currrent keymap names supported
by QEMU, only 28 seem to be supported by this device.


With regards,
Daniel
Henrik Carlqvist March 28, 2023, 8:16 p.m. UTC | #7
On Tue, 28 Mar 2023 18:59:26 +0100
Daniel P. Berrangé <berrange@redhat.com> wrote:
> I'm generally not in favour of creating many different ways to set
> the same thing, especially not multiple string based names, but if
> a single vocabulary for strings is insufficient, then having support
> for numbers feels reasonable.

If only a single way to set them would be allowed, I think that the
hexadecimal number would make most sense as we are emulating dip-switches.
However, it would be nice to also allow more easy to remember strings where
applicable.

> If you care about documentation then I think there ought to be docs
> added in QEMU for it, as out of the 33 currrent keymap names supported
> by QEMU, only 28 seem to be supported by this device.

Maybe it could be documented by printing some text if given

-global escc.sunkbd_layout=help

or

-global escc.sunkbd_layout=list

Best regards Henrik
Henrik Carlqvist April 30, 2023, 8:55 p.m. UTC | #8
I have now changed the patch to instead of using the -k switch use 

-global escc.sunkbd_layout=

to select keyboard layout. It would be nice to somehow document this. Yes, in 
the monitor, you can type "info qtree" and see something like:

  dev: escc, id ""
    gpio-out "sysbus-irq" 2
    frequency = 4915200 (0x4b0000)
    it_shift = 1 (0x1)
    bit_swap = false
    disabled = 0 (0x0)
    chnBtype = 2 (0x2)
    chnAtype = 1 (0x1)
    chrB = ""
    chrA = ""
    sunkbd_layout = "43"
    mmio 0000000071000000/0000000000000008
 
but that information is not easy to find and it does not say anything about 
possible values. What is the best way to document this kind of global 
parameters?

Please cc me any reply as I am no longer subscribed to the mailing list.

best regards Henrik

SUN Type 4, 5 and 5c keyboards have dip switches to choose the language layout 
of the keyboard. Solaris makes an ioctl to query the value of the dipswitches 
and uses that value to select keyboard layout. Also the SUN bios like the one 
in the file ss5.bin uses this value to support at least some keyboard layouts. 
However, the OpenBIOS provided with qemu is hardcoded to always use an 
US keyboard layout.

Before this patch, qemu allways gave dip switch value 0x21 (US keyboard),
this patch uses a command line switch like "-global escc.sunkbd_layout=de" to
select dip switch value. A table is used to lookup values from arguments like:

-global escc.sunkbd_layout=fr
-global escc.sunkbd_layout=es

But the patch also accepts numeric dip switch values directly to the -k
switch:

-global escc.sunkbd_layout=0x2b
-global escc.sunkbd_layout=43

Both values above are the same and select swedish keyboard as explained in
table 3-15 at
https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html

Unless you want to do a full Solaris installation but happen to have
access to a bios file, the easiest way to test that the patch works is to:

qemu-system-sparc -global escc.sunkbd_layout=sv -bios /path/to/ss5.bin

If you already happen to have a Solaris installation in a qemu disk image
file you can easily try different keyboard layouts after this patch is
applied.

Signed-off-by: Henrik Carlqvist <hc1245@poolhem.se>
---
 hw/char/escc.c         | 81 +++++++++++++++++++++++++++++++++++++++---
 include/hw/char/escc.h |  1 +
 2 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/hw/char/escc.c b/hw/char/escc.c
index 17a908c59b..0aac4f0f92 100644
--- a/hw/char/escc.c
+++ b/hw/char/escc.c
@@ -31,6 +31,8 @@
 #include "qemu/module.h"
 #include "hw/char/escc.h"
 #include "ui/console.h"
+
+#include "qemu/cutils.h"
 #include "trace.h"
 
 /*
@@ -190,7 +192,8 @@
 #define R_MISC1I 14
 #define R_EXTINT 15
 
-static void handle_kbd_command(ESCCChannelState *s, int val);
+static unsigned char sun_keyboard_layout_dip_switch(const char *keyboard_layout);
+static void handle_kbd_command(ESCCChannelState *s, int val, ESCCState *k);
 static int serial_can_receive(void *opaque);
 static void serial_receive_byte(ESCCChannelState *s, int ch);
 
@@ -657,7 +660,7 @@ static void escc_mem_write(void *opaque, hwaddr addr,
                  */
                 qemu_chr_fe_write_all(&s->chr, &s->tx, 1);
             } else if (s->type == escc_kbd && !s->disabled) {
-                handle_kbd_command(s, val);
+                handle_kbd_command(s, val, serial);
             }
         }
         s->rregs[R_STATUS] |= STATUS_TXEMPTY; /* Tx buffer empty */
@@ -846,7 +849,76 @@ static QemuInputHandler sunkbd_handler = {
     .event = sunkbd_handle_event,
 };
 
-static void handle_kbd_command(ESCCChannelState *s, int val)
+static unsigned char sun_keyboard_layout_dip_switch(const char *keyboard_layout)
+{
+    /* Return the value of the dip-switches in a SUN Type 5 keyboard */
+    static unsigned char ret = 0xff;
+
+    if ((ret == 0xff) && keyboard_layout) {
+        int i;
+        struct layout_values {
+            const char *lang;
+            unsigned char dip;
+        } languages[] =
+    /* Dip values from table 3-16 Layouts for Type 4, 5, and 5c Keyboards */
+            {
+                {"en-us", 0x21}, /* U.S.A. (US5.kt) */
+                                 /* 0x22 is some other US (US_UNIX5.kt)*/
+                {"fr",    0x23}, /* France (France5.kt) */
+                {"da",    0x24}, /* Denmark (Denmark5.kt) */
+                {"de",    0x25}, /* Germany (Germany5.kt) */
+                {"it",    0x26}, /* Italy (Italy5.kt) */
+                {"nl",    0x27}, /* The Netherlands (Netherland5.kt) */
+                {"no",    0x28}, /* Norway (Norway.kt) */
+                {"pt",    0x29}, /* Portugal (Portugal5.kt) */
+                {"es",    0x2a}, /* Spain (Spain5.kt) */
+                {"sv",    0x2b}, /* Sweden (Sweden5.kt) */
+                {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */
+                {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */
+                {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */
+                {"ko",    0x2f}, /* Korea (Korea5.kt) */
+                {"tw",    0x30}, /* Taiwan (Taiwan5.kt) */
+                {"ja",    0x31}, /* Japan (Japan5.kt) */
+                {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */
+                {"hu",    0x33}, /* Hungary (Hungary5.kt) */
+                {"pl",    0x34}, /* Poland (Poland5.kt) */
+                {"cz",    0x35}, /* Czech (Czech5.kt) */
+                {"ru",    0x36}, /* Russia (Russia5.kt) */
+                {"lv",    0x37}, /* Latvia (Latvia5.kt) */
+                {"tr",    0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */
+                {"gr",    0x39}, /* Greece (Greece5.kt) */
+                {"ar",    0x3a}, /* Arabic (Arabic5.kt) */
+                {"lt",    0x3b}, /* Lithuania (Lithuania5.kt) */
+                {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */
+                {"be",    0x3c}, /* Belgium (Belgian5.kt) */
+            };
+
+        for (i = 0;
+             i < sizeof(languages) / sizeof(struct layout_values);
+             i++) {
+            if (!strcmp(keyboard_layout, languages[i].lang)) {
+                ret = languages[i].dip;
+                return ret;
+            }
+        }
+        /* Found no known language code */
+
+        if ((keyboard_layout[0] >= '0') && (keyboard_layout[0] <= '9')) {
+            unsigned int tmp;
+            /* As a fallback we also accept numeric dip switch value */
+            if (!qemu_strtoui(keyboard_layout, NULL, 0, &tmp)) {
+                ret = (unsigned char)tmp;
+            }
+        }
+    }
+    if (ret == 0xff) {
+        /* Final fallback if keyboard_layout was not set or recognized */
+        ret = 0x21; /* en-us layout */
+    }
+    return ret;
+}
+
+static void handle_kbd_command(ESCCChannelState *s, int val, ESCCState *k)
 {
     trace_escc_kbd_command(val);
     if (s->led_mode) { /* Ignore led byte */
@@ -867,7 +939,7 @@ static void handle_kbd_command(ESCCChannelState *s, int val)
     case 0xf:
         clear_queue(s);
         put_queue(s, 0xfe);
-        put_queue(s, 0x21); /*  en-us layout */
+        put_queue(s, sun_keyboard_layout_dip_switch(k->keyboard_layout));
         break;
     default:
         break;
@@ -976,6 +1048,7 @@ static Property escc_properties[] = {
     DEFINE_PROP_UINT32("chnAtype",  ESCCState, chn[1].type, 0),
     DEFINE_PROP_CHR("chrB", ESCCState, chn[0].chr),
     DEFINE_PROP_CHR("chrA", ESCCState, chn[1].chr),
+    DEFINE_PROP_STRING("sunkbd_layout", ESCCState, keyboard_layout),
     DEFINE_PROP_END_OF_LIST(),
 };
 
diff --git a/include/hw/char/escc.h b/include/hw/char/escc.h
index 7e9482dee2..2830876a17 100644
--- a/include/hw/char/escc.h
+++ b/include/hw/char/escc.h
@@ -56,6 +56,7 @@ struct ESCCState {
     MemoryRegion mmio;
     uint32_t disabled;
     uint32_t frequency;
+    char *keyboard_layout;
 };
 
 #endif
Henrik Carlqvist June 8, 2023, 4:14 p.m. UTC | #9
I didn't get much response to my last version of the patch to implement
emulation of language selection dip switch on SUN keyboards. By request, I
changed the patch to listen for -global escc.sunkbd_layout= instead of using
the -k switch to select keyboard layout.

What do you think about this patch? Would you like to apply it as is, or do
you want any more changes?

Best regards Henrik

On Sun, 30 Apr 2023 22:55:33 +0200
Henrik Carlqvist <hc981@poolhem.se> wrote:

> I have now changed the patch to instead of using the -k switch use 
> 
> -global escc.sunkbd_layout=
> 
> to select keyboard layout. It would be nice to somehow document this. Yes,
> in the monitor, you can type "info qtree" and see something like:
> 
>   dev: escc, id ""
>     gpio-out "sysbus-irq" 2
>     frequency = 4915200 (0x4b0000)
>     it_shift = 1 (0x1)
>     bit_swap = false
>     disabled = 0 (0x0)
>     chnBtype = 2 (0x2)
>     chnAtype = 1 (0x1)
>     chrB = ""
>     chrA = ""
>     sunkbd_layout = "43"
>     mmio 0000000071000000/0000000000000008
>  
> but that information is not easy to find and it does not say anything about 
> possible values. What is the best way to document this kind of global 
> parameters?
> 
> Please cc me any reply as I am no longer subscribed to the mailing list.
> 
> best regards Henrik
> 
> SUN Type 4, 5 and 5c keyboards have dip switches to choose the language
> layout of the keyboard. Solaris makes an ioctl to query the value of the
> dipswitches and uses that value to select keyboard layout. Also the SUN bios
> like the one in the file ss5.bin uses this value to support at least some
> keyboard layouts. However, the OpenBIOS provided with qemu is hardcoded to
> always use an US keyboard layout.
> 
> Before this patch, qemu allways gave dip switch value 0x21 (US keyboard),
> this patch uses a command line switch like "-global escc.sunkbd_layout=de"
> to select dip switch value. A table is used to lookup values from arguments
> like:
> 
> -global escc.sunkbd_layout=fr
> -global escc.sunkbd_layout=es
> 
> But the patch also accepts numeric dip switch values directly to the -k
> switch:
> 
> -global escc.sunkbd_layout=0x2b
> -global escc.sunkbd_layout=43
> 
> Both values above are the same and select swedish keyboard as explained in
> table 3-15 at
> https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html
> 
> Unless you want to do a full Solaris installation but happen to have
> access to a bios file, the easiest way to test that the patch works is to:
> 
> qemu-system-sparc -global escc.sunkbd_layout=sv -bios /path/to/ss5.bin
> 
> If you already happen to have a Solaris installation in a qemu disk image
> file you can easily try different keyboard layouts after this patch is
> applied.
> 
> Signed-off-by: Henrik Carlqvist <hc1245@poolhem.se>
> ---
>  hw/char/escc.c         | 81 +++++++++++++++++++++++++++++++++++++++---
>  include/hw/char/escc.h |  1 +
>  2 files changed, 78 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/char/escc.c b/hw/char/escc.c
> index 17a908c59b..0aac4f0f92 100644
> --- a/hw/char/escc.c
> +++ b/hw/char/escc.c
> @@ -31,6 +31,8 @@
>  #include "qemu/module.h"
>  #include "hw/char/escc.h"
>  #include "ui/console.h"
> +
> +#include "qemu/cutils.h"
>  #include "trace.h"
>  
>  /*
> @@ -190,7 +192,8 @@
>  #define R_MISC1I 14
>  #define R_EXTINT 15
>  
> -static void handle_kbd_command(ESCCChannelState *s, int val);
> +static unsigned char sun_keyboard_layout_dip_switch(const char
> *keyboard_layout);+static void handle_kbd_command(ESCCChannelState *s, int
> val, ESCCState *k);
>  static int serial_can_receive(void *opaque);
>  static void serial_receive_byte(ESCCChannelState *s, int ch);
>  
> @@ -657,7 +660,7 @@ static void escc_mem_write(void *opaque, hwaddr addr,
>                   */
>                  qemu_chr_fe_write_all(&s->chr, &s->tx, 1);
>              } else if (s->type == escc_kbd && !s->disabled) {
> -                handle_kbd_command(s, val);
> +                handle_kbd_command(s, val, serial);
>              }
>          }
>          s->rregs[R_STATUS] |= STATUS_TXEMPTY; /* Tx buffer empty */
> @@ -846,7 +849,76 @@ static QemuInputHandler sunkbd_handler = {
>      .event = sunkbd_handle_event,
>  };
>  
> -static void handle_kbd_command(ESCCChannelState *s, int val)
> +static unsigned char sun_keyboard_layout_dip_switch(const char
> *keyboard_layout)+{
> +    /* Return the value of the dip-switches in a SUN Type 5 keyboard */
> +    static unsigned char ret = 0xff;
> +
> +    if ((ret == 0xff) && keyboard_layout) {
> +        int i;
> +        struct layout_values {
> +            const char *lang;
> +            unsigned char dip;
> +        } languages[] =
> +    /* Dip values from table 3-16 Layouts for Type 4, 5, and 5c Keyboards
> */+            {
> +                {"en-us", 0x21}, /* U.S.A. (US5.kt) */
> +                                 /* 0x22 is some other US (US_UNIX5.kt)*/
> +                {"fr",    0x23}, /* France (France5.kt) */
> +                {"da",    0x24}, /* Denmark (Denmark5.kt) */
> +                {"de",    0x25}, /* Germany (Germany5.kt) */
> +                {"it",    0x26}, /* Italy (Italy5.kt) */
> +                {"nl",    0x27}, /* The Netherlands (Netherland5.kt) */
> +                {"no",    0x28}, /* Norway (Norway.kt) */
> +                {"pt",    0x29}, /* Portugal (Portugal5.kt) */
> +                {"es",    0x2a}, /* Spain (Spain5.kt) */
> +                {"sv",    0x2b}, /* Sweden (Sweden5.kt) */
> +                {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */
> +                {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */
> +                {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */
> +                {"ko",    0x2f}, /* Korea (Korea5.kt) */
> +                {"tw",    0x30}, /* Taiwan (Taiwan5.kt) */
> +                {"ja",    0x31}, /* Japan (Japan5.kt) */
> +                {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */
> +                {"hu",    0x33}, /* Hungary (Hungary5.kt) */
> +                {"pl",    0x34}, /* Poland (Poland5.kt) */
> +                {"cz",    0x35}, /* Czech (Czech5.kt) */
> +                {"ru",    0x36}, /* Russia (Russia5.kt) */
> +                {"lv",    0x37}, /* Latvia (Latvia5.kt) */
> +                {"tr",    0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */
> +                {"gr",    0x39}, /* Greece (Greece5.kt) */
> +                {"ar",    0x3a}, /* Arabic (Arabic5.kt) */
> +                {"lt",    0x3b}, /* Lithuania (Lithuania5.kt) */
> +                {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */
> +                {"be",    0x3c}, /* Belgium (Belgian5.kt) */
> +            };
> +
> +        for (i = 0;
> +             i < sizeof(languages) / sizeof(struct layout_values);
> +             i++) {
> +            if (!strcmp(keyboard_layout, languages[i].lang)) {
> +                ret = languages[i].dip;
> +                return ret;
> +            }
> +        }
> +        /* Found no known language code */
> +
> +        if ((keyboard_layout[0] >= '0') && (keyboard_layout[0] <= '9')) {
> +            unsigned int tmp;
> +            /* As a fallback we also accept numeric dip switch value */
> +            if (!qemu_strtoui(keyboard_layout, NULL, 0, &tmp)) {
> +                ret = (unsigned char)tmp;
> +            }
> +        }
> +    }
> +    if (ret == 0xff) {
> +        /* Final fallback if keyboard_layout was not set or recognized */
> +        ret = 0x21; /* en-us layout */
> +    }
> +    return ret;
> +}
> +
> +static void handle_kbd_command(ESCCChannelState *s, int val, ESCCState *k)
>  {
>      trace_escc_kbd_command(val);
>      if (s->led_mode) { /* Ignore led byte */
> @@ -867,7 +939,7 @@ static void handle_kbd_command(ESCCChannelState *s, int
> val)
>      case 0xf:
>          clear_queue(s);
>          put_queue(s, 0xfe);
> -        put_queue(s, 0x21); /*  en-us layout */
> +        put_queue(s, sun_keyboard_layout_dip_switch(k->keyboard_layout));
>          break;
>      default:
>          break;
> @@ -976,6 +1048,7 @@ static Property escc_properties[] = {
>      DEFINE_PROP_UINT32("chnAtype",  ESCCState, chn[1].type, 0),
>      DEFINE_PROP_CHR("chrB", ESCCState, chn[0].chr),
>      DEFINE_PROP_CHR("chrA", ESCCState, chn[1].chr),
> +    DEFINE_PROP_STRING("sunkbd_layout", ESCCState, keyboard_layout),
>      DEFINE_PROP_END_OF_LIST(),
>  };
>  
> diff --git a/include/hw/char/escc.h b/include/hw/char/escc.h
> index 7e9482dee2..2830876a17 100644
> --- a/include/hw/char/escc.h
> +++ b/include/hw/char/escc.h
> @@ -56,6 +56,7 @@ struct ESCCState {
>      MemoryRegion mmio;
>      uint32_t disabled;
>      uint32_t frequency;
> +    char *keyboard_layout;
>  };
>  
>  #endif
> -- 
> 2.30.4
>
Daniel P. Berrangé June 8, 2023, 4:22 p.m. UTC | #10
On Sun, Apr 30, 2023 at 10:55:33PM +0200, Henrik Carlqvist wrote:
> I have now changed the patch to instead of using the -k switch use 
> 
> -global escc.sunkbd_layout=
> 
> to select keyboard layout. It would be nice to somehow document this. Yes, in 
> the monitor, you can type "info qtree" and see something like:
> 
>   dev: escc, id ""
>     gpio-out "sysbus-irq" 2
>     frequency = 4915200 (0x4b0000)
>     it_shift = 1 (0x1)
>     bit_swap = false
>     disabled = 0 (0x0)
>     chnBtype = 2 (0x2)
>     chnAtype = 1 (0x1)
>     chrB = ""
>     chrA = ""
>     sunkbd_layout = "43"
>     mmio 0000000071000000/0000000000000008
>  
> but that information is not easy to find and it does not say anything about 
> possible values. What is the best way to document this kind of global 
> parameters?

You can put it in docs/system/sparc.rst (or sparc64.rst whichever is best?)

Alternatively create a docs/system/devices/escc.rst and link it from
device-emulation.rst

> 
> Please cc me any reply as I am no longer subscribed to the mailing list.
> 
> best regards Henrik
> 
> SUN Type 4, 5 and 5c keyboards have dip switches to choose the language layout 
> of the keyboard. Solaris makes an ioctl to query the value of the dipswitches 
> and uses that value to select keyboard layout. Also the SUN bios like the one 
> in the file ss5.bin uses this value to support at least some keyboard layouts. 
> However, the OpenBIOS provided with qemu is hardcoded to always use an 
> US keyboard layout.
> 
> Before this patch, qemu allways gave dip switch value 0x21 (US keyboard),
> this patch uses a command line switch like "-global escc.sunkbd_layout=de" to
> select dip switch value. A table is used to lookup values from arguments like:
> 
> -global escc.sunkbd_layout=fr
> -global escc.sunkbd_layout=es
> 
> But the patch also accepts numeric dip switch values directly to the -k
> switch:
> 
> -global escc.sunkbd_layout=0x2b
> -global escc.sunkbd_layout=43
> 
> Both values above are the same and select swedish keyboard as explained in
> table 3-15 at
> https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html
> 
> Unless you want to do a full Solaris installation but happen to have
> access to a bios file, the easiest way to test that the patch works is to:
> 
> qemu-system-sparc -global escc.sunkbd_layout=sv -bios /path/to/ss5.bin
> 
> If you already happen to have a Solaris installation in a qemu disk image
> file you can easily try different keyboard layouts after this patch is
> applied.
> 
> Signed-off-by: Henrik Carlqvist <hc1245@poolhem.se>
> ---
>  hw/char/escc.c         | 81 +++++++++++++++++++++++++++++++++++++++---
>  include/hw/char/escc.h |  1 +
>  2 files changed, 78 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/char/escc.c b/hw/char/escc.c
> index 17a908c59b..0aac4f0f92 100644
> --- a/hw/char/escc.c
> +++ b/hw/char/escc.c
> @@ -31,6 +31,8 @@
> -static void handle_kbd_command(ESCCChannelState *s, int val)
> +static unsigned char sun_keyboard_layout_dip_switch(const char *keyboard_layout)
> +{
> +    /* Return the value of the dip-switches in a SUN Type 5 keyboard */
> +    static unsigned char ret = 0xff;
> +
> +    if ((ret == 0xff) && keyboard_layout) {
> +        int i;
> +        struct layout_values {
> +            const char *lang;
> +            unsigned char dip;
> +        } languages[] =
> +    /* Dip values from table 3-16 Layouts for Type 4, 5, and 5c Keyboards */
> +            {
> +                {"en-us", 0x21}, /* U.S.A. (US5.kt) */
> +                                 /* 0x22 is some other US (US_UNIX5.kt)*/
> +                {"fr",    0x23}, /* France (France5.kt) */
> +                {"da",    0x24}, /* Denmark (Denmark5.kt) */
> +                {"de",    0x25}, /* Germany (Germany5.kt) */
> +                {"it",    0x26}, /* Italy (Italy5.kt) */
> +                {"nl",    0x27}, /* The Netherlands (Netherland5.kt) */
> +                {"no",    0x28}, /* Norway (Norway.kt) */
> +                {"pt",    0x29}, /* Portugal (Portugal5.kt) */
> +                {"es",    0x2a}, /* Spain (Spain5.kt) */
> +                {"sv",    0x2b}, /* Sweden (Sweden5.kt) */
> +                {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */
> +                {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */
> +                {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */
> +                {"ko",    0x2f}, /* Korea (Korea5.kt) */
> +                {"tw",    0x30}, /* Taiwan (Taiwan5.kt) */
> +                {"ja",    0x31}, /* Japan (Japan5.kt) */
> +                {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */
> +                {"hu",    0x33}, /* Hungary (Hungary5.kt) */
> +                {"pl",    0x34}, /* Poland (Poland5.kt) */
> +                {"cz",    0x35}, /* Czech (Czech5.kt) */
> +                {"ru",    0x36}, /* Russia (Russia5.kt) */
> +                {"lv",    0x37}, /* Latvia (Latvia5.kt) */
> +                {"tr",    0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */
> +                {"gr",    0x39}, /* Greece (Greece5.kt) */
> +                {"ar",    0x3a}, /* Arabic (Arabic5.kt) */
> +                {"lt",    0x3b}, /* Lithuania (Lithuania5.kt) */
> +                {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */
> +                {"be",    0x3c}, /* Belgium (Belgian5.kt) */
> +            };
> +
> +        for (i = 0;
> +             i < sizeof(languages) / sizeof(struct layout_values);

Simpify to

      i < ARRAY_SIZE(languages);

at which point you can fit the for(;;) on one line.

> +             i++) {
> +            if (!strcmp(keyboard_layout, languages[i].lang)) {
> +                ret = languages[i].dip;
> +                return ret;
> +            }
> +        }
> +        /* Found no known language code */
> +
> +        if ((keyboard_layout[0] >= '0') && (keyboard_layout[0] <= '9')) {
> +            unsigned int tmp;
> +            /* As a fallback we also accept numeric dip switch value */
> +            if (!qemu_strtoui(keyboard_layout, NULL, 0, &tmp)) {
> +                ret = (unsigned char)tmp;
> +            }
> +        }
> +    }
> +    if (ret == 0xff) {
> +        /* Final fallback if keyboard_layout was not set or recognized */
> +        ret = 0x21; /* en-us layout */
> +    }
> +    return ret;
> +}


With regards,
Daniel
Henrik Carlqvist June 8, 2023, 6:12 p.m. UTC | #11
On Thu, 8 Jun 2023 17:22:23 +0100
Daniel P. Berrangé <berrange@redhat.com> wrote:

> On Sun, Apr 30, 2023 at 10:55:33PM +0200, Henrik Carlqvist wrote:
> > What is the best way to document this kind of global parameters?
> 
> You can put it in docs/system/sparc.rst (or sparc64.rst whichever is best?)

Thanks for your reply! As far as I know those dip switches are only in the
keyboards of the old 32 bit sparc machines, ultrasparc used USB keyboards
instead without any dip switches. I will add the text to
docs/system/target-sparc.rst.

Would you like the documentation as a separate patch or added to the patch
which adds the functionality to hw/char/escc.c?

I hope to be able to create the patch in the upcoming weekend.

Best regards Henrik
Daniel P. Berrangé June 9, 2023, 7:50 a.m. UTC | #12
On Thu, Jun 08, 2023 at 08:12:21PM +0200, Henrik Carlqvist wrote:
> On Thu, 8 Jun 2023 17:22:23 +0100
> Daniel P. Berrangé <berrange@redhat.com> wrote:
> 
> > On Sun, Apr 30, 2023 at 10:55:33PM +0200, Henrik Carlqvist wrote:
> > > What is the best way to document this kind of global parameters?
> > 
> > You can put it in docs/system/sparc.rst (or sparc64.rst whichever is best?)
> 
> Thanks for your reply! As far as I know those dip switches are only in the
> keyboards of the old 32 bit sparc machines, ultrasparc used USB keyboards
> instead without any dip switches. I will add the text to
> docs/system/target-sparc.rst.
> 
> Would you like the documentation as a separate patch or added to the patch
> which adds the functionality to hw/char/escc.c?

Either way is fine imho, mild perference to have it in the same patch if
the docs are reasonably short.

With regards,
Daniel
Mark Cave-Ayland June 10, 2023, 7:06 a.m. UTC | #13
On 08/06/2023 17:14, Henrik Carlqvist wrote:

> I didn't get much response to my last version of the patch to implement
> emulation of language selection dip switch on SUN keyboards. By request, I
> changed the patch to listen for -global escc.sunkbd_layout= instead of using
> the -k switch to select keyboard layout.
> 
> What do you think about this patch? Would you like to apply it as is, or do
> you want any more changes?

Apologies for the delay in getting to this, let me add a few comments below.

> Best regards Henrik
> 
> On Sun, 30 Apr 2023 22:55:33 +0200
> Henrik Carlqvist <hc981@poolhem.se> wrote:
> 
>> I have now changed the patch to instead of using the -k switch use
>>
>> -global escc.sunkbd_layout=
>>
>> to select keyboard layout. It would be nice to somehow document this. Yes,
>> in the monitor, you can type "info qtree" and see something like:
>>
>>    dev: escc, id ""
>>      gpio-out "sysbus-irq" 2
>>      frequency = 4915200 (0x4b0000)
>>      it_shift = 1 (0x1)
>>      bit_swap = false
>>      disabled = 0 (0x0)
>>      chnBtype = 2 (0x2)
>>      chnAtype = 1 (0x1)
>>      chrB = ""
>>      chrA = ""
>>      sunkbd_layout = "43"
>>      mmio 0000000071000000/0000000000000008
>>   
>> but that information is not easy to find and it does not say anything about
>> possible values. What is the best way to document this kind of global
>> parameters?
>>
>> Please cc me any reply as I am no longer subscribed to the mailing list.
>>
>> best regards Henrik
>>
>> SUN Type 4, 5 and 5c keyboards have dip switches to choose the language
>> layout of the keyboard. Solaris makes an ioctl to query the value of the
>> dipswitches and uses that value to select keyboard layout. Also the SUN bios
>> like the one in the file ss5.bin uses this value to support at least some
>> keyboard layouts. However, the OpenBIOS provided with qemu is hardcoded to
>> always use an US keyboard layout.
>>
>> Before this patch, qemu allways gave dip switch value 0x21 (US keyboard),
>> this patch uses a command line switch like "-global escc.sunkbd_layout=de"
>> to select dip switch value. A table is used to lookup values from arguments
>> like:
>>
>> -global escc.sunkbd_layout=fr
>> -global escc.sunkbd_layout=es
>>
>> But the patch also accepts numeric dip switch values directly to the -k
>> switch:
>>
>> -global escc.sunkbd_layout=0x2b
>> -global escc.sunkbd_layout=43
>>
>> Both values above are the same and select swedish keyboard as explained in
>> table 3-15 at
>> https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html
>>
>> Unless you want to do a full Solaris installation but happen to have
>> access to a bios file, the easiest way to test that the patch works is to:
>>
>> qemu-system-sparc -global escc.sunkbd_layout=sv -bios /path/to/ss5.bin
>>
>> If you already happen to have a Solaris installation in a qemu disk image
>> file you can easily try different keyboard layouts after this patch is
>> applied.
>>
>> Signed-off-by: Henrik Carlqvist <hc1245@poolhem.se>
>> ---
>>   hw/char/escc.c         | 81 +++++++++++++++++++++++++++++++++++++++---
>>   include/hw/char/escc.h |  1 +
>>   2 files changed, 78 insertions(+), 4 deletions(-)
>>
>> diff --git a/hw/char/escc.c b/hw/char/escc.c
>> index 17a908c59b..0aac4f0f92 100644
>> --- a/hw/char/escc.c
>> +++ b/hw/char/escc.c
>> @@ -31,6 +31,8 @@
>>   #include "qemu/module.h"
>>   #include "hw/char/escc.h"
>>   #include "ui/console.h"
>> +
>> +#include "qemu/cutils.h"
>>   #include "trace.h"
>>   
>>   /*
>> @@ -190,7 +192,8 @@
>>   #define R_MISC1I 14
>>   #define R_EXTINT 15
>>   
>> -static void handle_kbd_command(ESCCChannelState *s, int val);
>> +static unsigned char sun_keyboard_layout_dip_switch(const char
>> *keyboard_layout);+static void handle_kbd_command(ESCCChannelState *s, int
>> val, ESCCState *k);
>>   static int serial_can_receive(void *opaque);
>>   static void serial_receive_byte(ESCCChannelState *s, int ch);
>>   
>> @@ -657,7 +660,7 @@ static void escc_mem_write(void *opaque, hwaddr addr,
>>                    */
>>                   qemu_chr_fe_write_all(&s->chr, &s->tx, 1);
>>               } else if (s->type == escc_kbd && !s->disabled) {
>> -                handle_kbd_command(s, val);
>> +                handle_kbd_command(s, val, serial);
>>               }
>>           }
>>           s->rregs[R_STATUS] |= STATUS_TXEMPTY; /* Tx buffer empty */
>> @@ -846,7 +849,76 @@ static QemuInputHandler sunkbd_handler = {
>>       .event = sunkbd_handle_event,
>>   };
>>   
>> -static void handle_kbd_command(ESCCChannelState *s, int val)
>> +static unsigned char sun_keyboard_layout_dip_switch(const char
>> *keyboard_layout)+{
>> +    /* Return the value of the dip-switches in a SUN Type 5 keyboard */
>> +    static unsigned char ret = 0xff;
>> +
>> +    if ((ret == 0xff) && keyboard_layout) {
>> +        int i;
>> +        struct layout_values {
>> +            const char *lang;
>> +            unsigned char dip;
>> +        } languages[] =
>> +    /* Dip values from table 3-16 Layouts for Type 4, 5, and 5c Keyboards
>> */+            {
>> +                {"en-us", 0x21}, /* U.S.A. (US5.kt) */
>> +                                 /* 0x22 is some other US (US_UNIX5.kt)*/
>> +                {"fr",    0x23}, /* France (France5.kt) */
>> +                {"da",    0x24}, /* Denmark (Denmark5.kt) */
>> +                {"de",    0x25}, /* Germany (Germany5.kt) */
>> +                {"it",    0x26}, /* Italy (Italy5.kt) */
>> +                {"nl",    0x27}, /* The Netherlands (Netherland5.kt) */
>> +                {"no",    0x28}, /* Norway (Norway.kt) */
>> +                {"pt",    0x29}, /* Portugal (Portugal5.kt) */
>> +                {"es",    0x2a}, /* Spain (Spain5.kt) */
>> +                {"sv",    0x2b}, /* Sweden (Sweden5.kt) */
>> +                {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */
>> +                {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */
>> +                {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */
>> +                {"ko",    0x2f}, /* Korea (Korea5.kt) */
>> +                {"tw",    0x30}, /* Taiwan (Taiwan5.kt) */
>> +                {"ja",    0x31}, /* Japan (Japan5.kt) */
>> +                {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */
>> +                {"hu",    0x33}, /* Hungary (Hungary5.kt) */
>> +                {"pl",    0x34}, /* Poland (Poland5.kt) */
>> +                {"cz",    0x35}, /* Czech (Czech5.kt) */
>> +                {"ru",    0x36}, /* Russia (Russia5.kt) */
>> +                {"lv",    0x37}, /* Latvia (Latvia5.kt) */
>> +                {"tr",    0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */
>> +                {"gr",    0x39}, /* Greece (Greece5.kt) */
>> +                {"ar",    0x3a}, /* Arabic (Arabic5.kt) */
>> +                {"lt",    0x3b}, /* Lithuania (Lithuania5.kt) */
>> +                {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */
>> +                {"be",    0x3c}, /* Belgium (Belgian5.kt) */
>> +            };
>> +
>> +        for (i = 0;
>> +             i < sizeof(languages) / sizeof(struct layout_values);
>> +             i++) {
>> +            if (!strcmp(keyboard_layout, languages[i].lang)) {
>> +                ret = languages[i].dip;
>> +                return ret;
>> +            }
>> +        }
>> +        /* Found no known language code */
>> +
>> +        if ((keyboard_layout[0] >= '0') && (keyboard_layout[0] <= '9')) {
>> +            unsigned int tmp;
>> +            /* As a fallback we also accept numeric dip switch value */
>> +            if (!qemu_strtoui(keyboard_layout, NULL, 0, &tmp)) {
>> +                ret = (unsigned char)tmp;
>> +            }
>> +        }
>> +    }
>> +    if (ret == 0xff) {
>> +        /* Final fallback if keyboard_layout was not set or recognized */
>> +        ret = 0x21; /* en-us layout */
>> +    }
>> +    return ret;
>> +}
>> +
>> +static void handle_kbd_command(ESCCChannelState *s, int val, ESCCState *k)

This feels like your keyboard_layout variable should be in ESCCChannelState rather 
than ESCCState, which makes sense since the keyboard is a device plugged into one of 
the serial ports.

I see that there are 2 different naming conventions here: in some places you use 
keyboard, others kbd. I think you should standardise on sunkbd_layout since it 
emphasises that this is a Sun-specific feature.

>>   {
>>       trace_escc_kbd_command(val);
>>       if (s->led_mode) { /* Ignore led byte */
>> @@ -867,7 +939,7 @@ static void handle_kbd_command(ESCCChannelState *s, int
>> val)
>>       case 0xf:
>>           clear_queue(s);
>>           put_queue(s, 0xfe);
>> -        put_queue(s, 0x21); /*  en-us layout */
>> +        put_queue(s, sun_keyboard_layout_dip_switch(k->keyboard_layout));

sunkbd_layout_dip_switch()

>>           break;
>>       default:
>>           break;
>> @@ -976,6 +1048,7 @@ static Property escc_properties[] = {
>>       DEFINE_PROP_UINT32("chnAtype",  ESCCState, chn[1].type, 0),
>>       DEFINE_PROP_CHR("chrB", ESCCState, chn[0].chr),
>>       DEFINE_PROP_CHR("chrA", ESCCState, chn[1].chr),
>> +    DEFINE_PROP_STRING("sunkbd_layout", ESCCState, keyboard_layout),

DEFINE_PROP_STRING("chrA-sunkbd-layout", ESCCState, chn[1].sunkbd_layout),

Our qdev property naming guidelines did state that hyphens should be used in 
preference to underscores.

>>       DEFINE_PROP_END_OF_LIST(),
>>   };
>>   
>> diff --git a/include/hw/char/escc.h b/include/hw/char/escc.h
>> index 7e9482dee2..2830876a17 100644
>> --- a/include/hw/char/escc.h
>> +++ b/include/hw/char/escc.h
>> @@ -56,6 +56,7 @@ struct ESCCState {
>>       MemoryRegion mmio;
>>       uint32_t disabled;
>>       uint32_t frequency;
>> +    char *keyboard_layout;
>>   };
>>   
>>   #endif
>> -- 
>> 2.30.4


ATB,

Mark.
Henrik Carlqvist June 10, 2023, 10:29 a.m. UTC | #14
Thanks for your thoughts on this! Getting the variable in ESCCChannelState
instead of ESCCState is nice as it means that I don't have to add the
ESCCState variable to the handle_kbd_command. 

So I will rewrite the patch to use argumetns like "-global
escc.chrA-sunkbd-layout=sv" (or do we want it to be called "-global
escc.chnA-sunkbd-layout=sv" with chn for channel A instead of chr?) 

I will also rename functions and variables to sunkbd and add documentation to
docs/system/target-sparc.rst (possibly as a link to a file of its own as
describing all the keyboards to choose from might create a bigger page than
the current sparc32 page).

Best regards Henrik

On Sat, 10 Jun 2023 08:06:47 +0100
Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> wrote:

> On 08/06/2023 17:14, Henrik Carlqvist wrote:
> 
> > I didn't get much response to my last version of the patch to implement
> > emulation of language selection dip switch on SUN keyboards. By request, I
> > changed the patch to listen for -global escc.sunkbd_layout= instead of
> > using the -k switch to select keyboard layout.
> > 
> > What do you think about this patch? Would you like to apply it as is, or
> > do you want any more changes?
> 
> Apologies for the delay in getting to this, let me add a few comments below.
> 
> > Best regards Henrik
> > 
> > On Sun, 30 Apr 2023 22:55:33 +0200
> > Henrik Carlqvist <hc981@poolhem.se> wrote:
> > 
> >> I have now changed the patch to instead of using the -k switch use
> >>
> >> -global escc.sunkbd_layout=
> >>
> >> to select keyboard layout. It would be nice to somehow document this.
> >Yes,> in the monitor, you can type "info qtree" and see something like:
> >>
> >>    dev: escc, id ""
> >>      gpio-out "sysbus-irq" 2
> >>      frequency = 4915200 (0x4b0000)
> >>      it_shift = 1 (0x1)
> >>      bit_swap = false
> >>      disabled = 0 (0x0)
> >>      chnBtype = 2 (0x2)
> >>      chnAtype = 1 (0x1)
> >>      chrB = ""
> >>      chrA = ""
> >>      sunkbd_layout = "43"
> >>      mmio 0000000071000000/0000000000000008
> >>   
> >> but that information is not easy to find and it does not say anything
> >about> possible values. What is the best way to document this kind of
> >global> parameters?
> >>
> >> Please cc me any reply as I am no longer subscribed to the mailing list.
> >>
> >> best regards Henrik
> >>
> >> SUN Type 4, 5 and 5c keyboards have dip switches to choose the language
> >> layout of the keyboard. Solaris makes an ioctl to query the value of the
> >> dipswitches and uses that value to select keyboard layout. Also the SUN
> >bios> like the one in the file ss5.bin uses this value to support at least
> >some> keyboard layouts. However, the OpenBIOS provided with qemu is
> >hardcoded to> always use an US keyboard layout.
> >>
> >> Before this patch, qemu allways gave dip switch value 0x21 (US keyboard),
> >> this patch uses a command line switch like "-global
> >escc.sunkbd_layout=de"> to select dip switch value. A table is used to
> >lookup values from arguments> like:
> >>
> >> -global escc.sunkbd_layout=fr
> >> -global escc.sunkbd_layout=es
> >>
> >> But the patch also accepts numeric dip switch values directly to the -k
> >> switch:
> >>
> >> -global escc.sunkbd_layout=0x2b
> >> -global escc.sunkbd_layout=43
> >>
> >> Both values above are the same and select swedish keyboard as explained
> >in> table 3-15 at
> >> https://docs.oracle.com/cd/E19683-01/806-6642/new-43/index.html
> >>
> >> Unless you want to do a full Solaris installation but happen to have
> >> access to a bios file, the easiest way to test that the patch works is
> >to:>
> >> qemu-system-sparc -global escc.sunkbd_layout=sv -bios /path/to/ss5.bin
> >>
> >> If you already happen to have a Solaris installation in a qemu disk image
> >> file you can easily try different keyboard layouts after this patch is
> >> applied.
> >>
> >> Signed-off-by: Henrik Carlqvist <hc1245@poolhem.se>
> >> ---
> >>   hw/char/escc.c         | 81 +++++++++++++++++++++++++++++++++++++++---
> >>   include/hw/char/escc.h |  1 +
> >>   2 files changed, 78 insertions(+), 4 deletions(-)
> >>
> >> diff --git a/hw/char/escc.c b/hw/char/escc.c
> >> index 17a908c59b..0aac4f0f92 100644
> >> --- a/hw/char/escc.c
> >> +++ b/hw/char/escc.c
> >> @@ -31,6 +31,8 @@
> >>   #include "qemu/module.h"
> >>   #include "hw/char/escc.h"
> >>   #include "ui/console.h"
> >> +
> >> +#include "qemu/cutils.h"
> >>   #include "trace.h"
> >>   
> >>   /*
> >> @@ -190,7 +192,8 @@
> >>   #define R_MISC1I 14
> >>   #define R_EXTINT 15
> >>   
> >> -static void handle_kbd_command(ESCCChannelState *s, int val);
> >> +static unsigned char sun_keyboard_layout_dip_switch(const char
> >> *keyboard_layout);+static void handle_kbd_command(ESCCChannelState *s,
> >int> val, ESCCState *k);
> >>   static int serial_can_receive(void *opaque);
> >>   static void serial_receive_byte(ESCCChannelState *s, int ch);
> >>   
> >> @@ -657,7 +660,7 @@ static void escc_mem_write(void *opaque, hwaddr addr,
> >>                    */
> >>                   qemu_chr_fe_write_all(&s->chr, &s->tx, 1);
> >>               } else if (s->type == escc_kbd && !s->disabled) {
> >> -                handle_kbd_command(s, val);
> >> +                handle_kbd_command(s, val, serial);
> >>               }
> >>           }
> >>           s->rregs[R_STATUS] |= STATUS_TXEMPTY; /* Tx buffer empty */
> >> @@ -846,7 +849,76 @@ static QemuInputHandler sunkbd_handler = {
> >>       .event = sunkbd_handle_event,
> >>   };
> >>   
> >> -static void handle_kbd_command(ESCCChannelState *s, int val)
> >> +static unsigned char sun_keyboard_layout_dip_switch(const char
> >> *keyboard_layout)+{
> >> +    /* Return the value of the dip-switches in a SUN Type 5 keyboard */
> >> +    static unsigned char ret = 0xff;
> >> +
> >> +    if ((ret == 0xff) && keyboard_layout) {
> >> +        int i;
> >> +        struct layout_values {
> >> +            const char *lang;
> >> +            unsigned char dip;
> >> +        } languages[] =
> >> +    /* Dip values from table 3-16 Layouts for Type 4, 5, and 5c
> >Keyboards> */+            {
> >> +                {"en-us", 0x21}, /* U.S.A. (US5.kt) */
> >> +                                 /* 0x22 is some other US
> >(US_UNIX5.kt)*/> +                {"fr",    0x23}, /* France (France5.kt)
> >*/> +                {"da",    0x24}, /* Denmark (Denmark5.kt) */
> >> +                {"de",    0x25}, /* Germany (Germany5.kt) */
> >> +                {"it",    0x26}, /* Italy (Italy5.kt) */
> >> +                {"nl",    0x27}, /* The Netherlands (Netherland5.kt) */
> >> +                {"no",    0x28}, /* Norway (Norway.kt) */
> >> +                {"pt",    0x29}, /* Portugal (Portugal5.kt) */
> >> +                {"es",    0x2a}, /* Spain (Spain5.kt) */
> >> +                {"sv",    0x2b}, /* Sweden (Sweden5.kt) */
> >> +                {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt)
> >*/> +                {"de-ch", 0x2d}, /* Switzerland/German
> >(Switzer_Ge5.kt) */> +                {"en-gb", 0x2e}, /* Great Britain
> >(UK5.kt) */> +                {"ko",    0x2f}, /* Korea (Korea5.kt) */
> >> +                {"tw",    0x30}, /* Taiwan (Taiwan5.kt) */
> >> +                {"ja",    0x31}, /* Japan (Japan5.kt) */
> >> +                {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */
> >> +                {"hu",    0x33}, /* Hungary (Hungary5.kt) */
> >> +                {"pl",    0x34}, /* Poland (Poland5.kt) */
> >> +                {"cz",    0x35}, /* Czech (Czech5.kt) */
> >> +                {"ru",    0x36}, /* Russia (Russia5.kt) */
> >> +                {"lv",    0x37}, /* Latvia (Latvia5.kt) */
> >> +                {"tr",    0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */
> >> +                {"gr",    0x39}, /* Greece (Greece5.kt) */
> >> +                {"ar",    0x3a}, /* Arabic (Arabic5.kt) */
> >> +                {"lt",    0x3b}, /* Lithuania (Lithuania5.kt) */
> >> +                {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */
> >> +                {"be",    0x3c}, /* Belgium (Belgian5.kt) */
> >> +            };
> >> +
> >> +        for (i = 0;
> >> +             i < sizeof(languages) / sizeof(struct layout_values);
> >> +             i++) {
> >> +            if (!strcmp(keyboard_layout, languages[i].lang)) {
> >> +                ret = languages[i].dip;
> >> +                return ret;
> >> +            }
> >> +        }
> >> +        /* Found no known language code */
> >> +
> >> +        if ((keyboard_layout[0] >= '0') && (keyboard_layout[0] <= '9'))
> >{> +            unsigned int tmp;
> >> +            /* As a fallback we also accept numeric dip switch value */
> >> +            if (!qemu_strtoui(keyboard_layout, NULL, 0, &tmp)) {
> >> +                ret = (unsigned char)tmp;
> >> +            }
> >> +        }
> >> +    }
> >> +    if (ret == 0xff) {
> >> +        /* Final fallback if keyboard_layout was not set or recognized
> >*/> +        ret = 0x21; /* en-us layout */
> >> +    }
> >> +    return ret;
> >> +}
> >> +
> >> +static void handle_kbd_command(ESCCChannelState *s, int val, ESCCState
> >*k)
> 
> This feels like your keyboard_layout variable should be in ESCCChannelState
> rather than ESCCState, which makes sense since the keyboard is a device
> plugged into one of the serial ports.
> 
> I see that there are 2 different naming conventions here: in some places you
> use keyboard, others kbd. I think you should standardise on sunkbd_layout
> since it emphasises that this is a Sun-specific feature.
> 
> >>   {
> >>       trace_escc_kbd_command(val);
> >>       if (s->led_mode) { /* Ignore led byte */
> >> @@ -867,7 +939,7 @@ static void handle_kbd_command(ESCCChannelState *s,
> >int> val)
> >>       case 0xf:
> >>           clear_queue(s);
> >>           put_queue(s, 0xfe);
> >> -        put_queue(s, 0x21); /*  en-us layout */
> >> +        put_queue(s,
> >sun_keyboard_layout_dip_switch(k->keyboard_layout));
> 
> sunkbd_layout_dip_switch()
> 
> >>           break;
> >>       default:
> >>           break;
> >> @@ -976,6 +1048,7 @@ static Property escc_properties[] = {
> >>       DEFINE_PROP_UINT32("chnAtype",  ESCCState, chn[1].type, 0),
> >>       DEFINE_PROP_CHR("chrB", ESCCState, chn[0].chr),
> >>       DEFINE_PROP_CHR("chrA", ESCCState, chn[1].chr),
> >> +    DEFINE_PROP_STRING("sunkbd_layout", ESCCState, keyboard_layout),
> 
> DEFINE_PROP_STRING("chrA-sunkbd-layout", ESCCState, chn[1].sunkbd_layout),
> 
> Our qdev property naming guidelines did state that hyphens should be used in
> 
> preference to underscores.
> 
> >>       DEFINE_PROP_END_OF_LIST(),
> >>   };
> >>   
> >> diff --git a/include/hw/char/escc.h b/include/hw/char/escc.h
> >> index 7e9482dee2..2830876a17 100644
> >> --- a/include/hw/char/escc.h
> >> +++ b/include/hw/char/escc.h
> >> @@ -56,6 +56,7 @@ struct ESCCState {
> >>       MemoryRegion mmio;
> >>       uint32_t disabled;
> >>       uint32_t frequency;
> >> +    char *keyboard_layout;
> >>   };
> >>   
> >>   #endif
> >> -- 
> >> 2.30.4
> 
> 
> ATB,
> 
> Mark.
diff mbox series

Patch

diff --git a/hw/char/escc.c b/hw/char/escc.c
index 17a908c59b..53022ccf39 100644
--- a/hw/char/escc.c
+++ b/hw/char/escc.c
@@ -31,6 +31,8 @@ 
 #include "qemu/module.h"
 #include "hw/char/escc.h"
 #include "ui/console.h"
+#include "sysemu/sysemu.h"
+#include "qemu/cutils.h"
 #include "trace.h"
 
 /*
@@ -190,6 +192,7 @@ 
 #define R_MISC1I 14
 #define R_EXTINT 15
 
+static unsigned char sun_keyboard_layout_dip_switch(void);
 static void handle_kbd_command(ESCCChannelState *s, int val);
 static int serial_can_receive(void *opaque);
 static void serial_receive_byte(ESCCChannelState *s, int ch);
@@ -846,6 +849,75 @@  static QemuInputHandler sunkbd_handler = {
     .event = sunkbd_handle_event,
 };
 
+static unsigned char sun_keyboard_layout_dip_switch(void)
+{
+    /* Return the value of the dip-switches in a SUN Type 5 keyboard */
+    static unsigned char ret = 0xff;
+
+    if ((ret == 0xff) && keyboard_layout) {
+        int i;
+        struct layout_values {
+            const char *lang;
+            unsigned char dip;
+        } languages[] =
+    /* Dip values from table 3-16 Layouts for Type 4, 5, and 5c Keyboards */
+            {
+                {"en-us", 0x21}, /* U.S.A. (US5.kt) */
+                                 /* 0x22 is some other US (US_UNIX5.kt)*/
+                {"fr",    0x23}, /* France (France5.kt) */
+                {"da",    0x24}, /* Denmark (Denmark5.kt) */
+                {"de",    0x25}, /* Germany (Germany5.kt) */
+                {"it",    0x26}, /* Italy (Italy5.kt) */
+                {"nl",    0x27}, /* The Netherlands (Netherland5.kt) */
+                {"no",    0x28}, /* Norway (Norway.kt) */
+                {"pt",    0x29}, /* Portugal (Portugal5.kt) */
+                {"es",    0x2a}, /* Spain (Spain5.kt) */
+                {"sv",    0x2b}, /* Sweden (Sweden5.kt) */
+                {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */
+                {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */
+                {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */
+                {"ko",    0x2f}, /* Korea (Korea5.kt) */
+                {"tw",    0x30}, /* Taiwan (Taiwan5.kt) */
+                {"ja",    0x31}, /* Japan (Japan5.kt) */
+                {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */
+                {"hu",    0x33}, /* Hungary (Hungary5.kt) */
+                {"pl",    0x34}, /* Poland (Poland5.kt) */
+                {"cz",    0x35}, /* Czech (Czech5.kt) */
+                {"ru",    0x36}, /* Russia (Russia5.kt) */
+                {"lv",    0x37}, /* Latvia (Latvia5.kt) */
+                {"tr",    0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */
+                {"gr",    0x39}, /* Greece (Greece5.kt) */
+                {"ar",    0x3a}, /* Arabic (Arabic5.kt) */
+                {"lt",    0x3b}, /* Lithuania (Lithuania5.kt) */
+                {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */
+                {"be",    0x3c}, /* Belgium (Belgian5.kt) */
+            };
+
+        for (i = 0;
+             i < sizeof(languages) / sizeof(struct layout_values);
+             i++) {
+            if (!strcmp(keyboard_layout, languages[i].lang)) {
+                ret = languages[i].dip;
+                return ret;
+            }
+        }
+        /* Found no known language code */
+
+        if ((keyboard_layout[0] >= '0') && (keyboard_layout[0] <= '9')) {
+            unsigned int tmp;
+            /* As a fallback we also accept numeric dip switch value */
+            if (!qemu_strtoui(keyboard_layout, NULL, 0, &tmp)) {
+                ret = (unsigned char)tmp;
+            }
+        }
+    }
+    if (ret == 0xff) {
+        /* Final fallback if keyboard_layout was not set or recognized */
+        ret = 0x21; /* en-us layout */
+    }
+    return ret;
+}
+
 static void handle_kbd_command(ESCCChannelState *s, int val)
 {
     trace_escc_kbd_command(val);
@@ -867,7 +939,7 @@  static void handle_kbd_command(ESCCChannelState *s, int val)
     case 0xf:
         clear_queue(s);
         put_queue(s, 0xfe);
-        put_queue(s, 0x21); /*  en-us layout */
+        put_queue(s, sun_keyboard_layout_dip_switch());
         break;
     default:
         break;