support jp keyboards. for some reason, they report non-standard keycodes. i'm a little unsure about some of the codes above 0x7f, as 0x80 == Keyup. Reference: /n/atom/patch/applied/usbkbjp Date: Thu Mar 20 02:28:23 CET 2014 Signed-off-by: quanstro@quanstro.net --- /sys/src/cmd/usb/kb/kb.c Thu Mar 20 02:27:33 2014 +++ /sys/src/cmd/usb/kb/kb.c Thu Mar 20 02:27:34 2014 @@ -28,6 +28,7 @@ Dwcidle = 8, }; +typedef ushort Scan; typedef struct KDev KDev; typedef struct Kbd Kbd; typedef struct Mouse Mouse; @@ -84,45 +85,85 @@ */ /* - * key code to scan code; for the page table used by + * usb key code to ps/2 scan code; for the page table used by * the logitech bluetooth keyboard. */ -static char sctab[256] = -{ -[0x00] 0x0, 0x0, 0x0, 0x0, 0x1e, 0x30, 0x2e, 0x20, +#define E SCesc1<<8 + +static Scan sctaben[256] ={ +[0x00] 0, 0, 0, 0, 0x1e, 0x30, 0x2e, 0x20, [0x08] 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, [0x10] 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, -[0x18] 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x2, 0x3, -[0x20] 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, -[0x28] 0x1c, 0x1, 0xe, 0xf, 0x39, 0xc, 0xd, 0x1a, +[0x18] 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, +[0x20] 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, +[0x28] 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, [0x30] 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, [0x38] 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, -[0x40] 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0x63, 0x46, -[0x48] 0x77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0x0, 0x0, 0x0, 0x0, 0x0, -[0xe0] 0x1d, 0x2a, 0x38, 0x7d, 0x61, 0x36, 0x64, 0x7e, -[0xe8] 0x0, 0x0, 0x0, 0x0, 0x0, 0x73, 0x72, 0x71, -[0xf0] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, -[0xf8] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, +[0x60] 0x48, 0x49, 0x52, 0x53, E|0x56, E|0x7f, E|0x74, E|0x75, +[0x68] E|0x55, E|0x59, E|0x5a, E|0x5b, E|0x5c, E|0x5d, E|0x5e, E|0x5f, +[0x70] E|0x78, E|0x79, E|0x7a, E|0x7b, 0, 0, 0, 0, +[0x78] 0, 0, 0, 0, 0, 0, 0, E|0x71, +[0x80] E|0x73, E|0x72, 0, 0, 0, E|0x7c, 0, 0, +[0x88] 0, 0, 0, 0, 0, 0, 0, 0, +[0x90] 0, 0, 0, 0, 0, 0, 0, 0, +[0x98] 0, 0, 0, 0, 0, 0, 0, 0, +[0xa0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xa8] 0, 0, 0, 0, 0, 0, 0, 0, +[0xb0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xb8] 0, 0, 0, 0, 0, 0, 0, 0, +[0xc0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xc8] 0, 0, 0, 0, 0, 0, 0, 0, +[0xd0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xd8] 0, 0, 0, 0, 0, 0, 0, 0, +[0xe0] 0x1d, 0x2a, E|0x38, E|0x7d, E|0x61, 0x36, E|0x64, E|0x7e, +[0xe8] 0, 0, 0, 0, 0, E|0x73, E|0x72, E|0x71, +[0xf0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xf8] 0, 0, 0, 0, 0, 0, 0, 0, }; +/* + * scan codes above 0x7f are keyup + sc&0x7f. i don't understand this. + */ +static Scan sctabjp[256] = { +[0x00] 0, 0, 0xfc, 0, 0x1e, 0x30, 0x2e, 0x20, +[0x08] 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, +[0x10] 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, +[0x18] 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, +[0x20] 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, +[0x28] 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, +[0x30] 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, +[0x38] 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, +[0x40] 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0x37, 0x46, +[0x48] 0xc6, E|0x52, E|0x47, E|0x49, E|0x53, E|0x4f, E|0x51, E|0x4d, +[0x50] E|0x4b, E|0x50, E|0x48, 0x45, 0x35, 0x37, E|0x4a, E|0x4e, +[0x58] 0x1c, E|0x4f, E|0x50, E|0x51, E|0x4b, E|0x4c, E|0x4d, E|0x47, +[0x60] E|0x48, E|0x49, E|0x52, E|0x53, 0x56, 0, 0x5e, 0x59, +[0x68] 0x64, 0x65, E|0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, +[0x70] 0x6c, 0x6d, 0x6e, 0x76, 0, 0, 0, 0, +[0x78] 0, 0, 0, 0, 0, 0, 0, 0, +[0x80] 0, 0, 0, 0, 0, 0x7e, 0, 0x73, +[0x88] 0x70, 0x7d, 0x79, 0x7b, 0x5c, 0, 0, 0, +[0x90] 0xf2, 0xf1, 0x78, 0x77, 0x76, 0, 0, 0, +[0x98] 0, 0, 0, 0, 0, 0, 0, 0, +[0xa0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xa8] 0, 0, 0, 0, 0, 0, 0, 0, +[0xb0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xb8] 0, 0, 0, 0, 0, 0, 0, 0, +[0xc0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xc8] 0, 0, 0, 0, 0, 0, 0, 0, +[0xd0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xd8] 0, 0, 0, 0, 0, 0, 0, 0, +[0xe0] 0x1d, 0x2a, 0x38, 0x5b, 0x1d, 0x36, 0x38, 0x5c, +[0xe8] 0, 0, 0, 0, 0, 0, 0, 0, +[0xf0] 0, 0, 0, 0, 0, 0, 0, 0, +[0xf8] 0, 0, 0, 0, 0, 0, 0, 0, +}; +static Scan *sctab = sctaben; + static QLock inlck; static Kin kbdin = { @@ -421,24 +462,16 @@ } static void -startrepeat(KDev *f, uchar esc1, uchar sc) +startrepeat(KDev *f, Scan sc) { - ulong c; - - if(esc1) - c = SCesc1 << 8 | (sc & 0xff); - else - c = sc; - sendul(f->repeatc, c); + sendul(f->repeatc, sc); } static void -putscan(KDev *f, uchar esc, uchar sc) +putscan(KDev *f, Scan sc) { - int kbinfd; - uchar s[2] = {SCesc1, 0}; + uchar s[2]; - kbinfd = f->in->fd; if(sc == 0x41){ f->debug += 2; return; @@ -448,12 +481,15 @@ return; } if(f->debug > 1) - fprint(2, "sc: %x %x\n", (esc? SCesc1: 0), sc); + fprint(2, "sc: %.4ux\n", sc); + if((uchar)sc == 0) + return; + s[0] = sc>>8; s[1] = sc; - if(esc && sc != 0) - write(kbinfd, s, 2); - else if(sc != 0) - write(kbinfd, s+1, 1); + if(sc >= 0x100) + write(f->in->fd, s, 2); + else + write(f->in->fd, s+1, 1); } static void @@ -506,23 +542,21 @@ case Tick: if(code == -1 || nsec() < timeout) continue; - putscan(f, (uchar)(code>>8), (uchar)code); + putscan(f, code); timeout = nsec() + 30*Msec; break; } } } -#define hasesc1(sc) (((sc) >= 0x47) || ((sc) == 0x38)) - static void -putmod(KDev *f, uchar mods, uchar omods, uchar mask, uchar esc, uchar sc) +putmod(KDev *f, uchar mods, uchar omods, uchar mask, Scan sc) { /* BUG: Should be a single write */ if((mods&mask) && !(omods&mask)) - putscan(f, esc, sc); + putscan(f, sc); if(!(mods&mask) && (omods&mask)) - putscan(f, esc, Keyup|sc); + putscan(f, sc | Keyup); } /* @@ -533,17 +567,17 @@ * The aim is to allow future addition of other keycode pages * for other keyboards. */ -static uchar -putkeys(KDev *f, uchar buf[], uchar obuf[], int n, uchar dk) +static Scan +putkeys(KDev *f, uchar buf[], uchar obuf[], int n, Scan dk) { int i, j; - uchar uk; + Scan uk; - putmod(f, buf[0], obuf[0], Mctrl, 0, SCctrl); - putmod(f, buf[0], obuf[0], (1<ep->dir); @@ -634,10 +669,7 @@ if(kbdbusy(buf + 2, c - 2)) continue; if(usbdebug > 2 || f->debug > 1){ - fprint(2, "kbd mod %x: ", buf[0]); - for(i = 2; i < c; i++) - fprint(2, "kc %x ", buf[i]); - fprint(2, "\n"); + fprint(2, "kbd %c mod %.2ux: %.*lH\n", sctab==sctabjp? 'j': 'e', buf[0], c-2, buf+2); } dk = putkeys(f, buf, lbuf, f->ep->maxpkt, dk); memmove(lbuf, buf, c); @@ -729,7 +761,7 @@ static int usage(void) { - werrstr("usage: usb/kb [-bdkm] [-a n] [-N nb]"); + werrstr("usage: usb/kb [-bdjkm] [-a n] [-N nb]"); return -1; } @@ -757,6 +789,9 @@ kena = 1; pena = 0; break; + case 'j': + sctab = sctabjp; + break; case 'm': kena = 0; pena = 1; @@ -806,3 +841,4 @@ } return 0; } +