?? analog.c
字號:
/* * $Id: analog.c,v 1.52 2000/06/07 13:07:06 vojtech Exp $ * * Copyright (c) 1996-2000 Vojtech Pavlik * * Sponsored by SuSE *//* * Analog joystick and gamepad driver for Linux *//* * 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, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Should you need to contact me, the author, you can do so either by * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail: * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic */#include <linux/config.h>#include <linux/delay.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/bitops.h>#include <linux/init.h>#include <linux/input.h>#include <linux/gameport.h>#include <asm/timex.h>MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");MODULE_DESCRIPTION("Analog joystick and gamepad driver for Linux");MODULE_LICENSE("GPL");/* * Option parsing. */#define ANALOG_PORTS 16static char *js[ANALOG_PORTS];static int analog_options[ANALOG_PORTS];MODULE_PARM(js, "1-" __MODULE_STRING(ANALOG_PORTS) "s");MODULE_PARM_DESC(js, "Analog joystick options");/* * Times, feature definitions. */#define ANALOG_RUDDER 0x00004#define ANALOG_THROTTLE 0x00008#define ANALOG_AXES_STD 0x0000f#define ANALOG_BTNS_STD 0x000f0#define ANALOG_BTNS_CHF 0x00100#define ANALOG_HAT1_CHF 0x00200#define ANALOG_HAT2_CHF 0x00400#define ANALOG_HAT_FCS 0x00800#define ANALOG_HATS_ALL 0x00e00#define ANALOG_BTN_TL 0x01000#define ANALOG_BTN_TR 0x02000#define ANALOG_BTN_TL2 0x04000#define ANALOG_BTN_TR2 0x08000#define ANALOG_BTNS_TLR 0x03000#define ANALOG_BTNS_TLR2 0x0c000#define ANALOG_BTNS_GAMEPAD 0x0f000#define ANALOG_HBTN_CHF 0x10000#define ANALOG_ANY_CHF 0x10700#define ANALOG_SAITEK 0x20000#define ANALOG_EXTENSIONS 0x7ff00#define ANALOG_GAMEPAD 0x80000#define ANALOG_MAX_TIME 3 /* 3 ms */#define ANALOG_LOOP_TIME 2000 /* 2 * loop */#define ANALOG_REFRESH_TIME HZ/100 /* 10 ms */#define ANALOG_SAITEK_DELAY 200 /* 200 us */#define ANALOG_SAITEK_TIME 2000 /* 2000 us */#define ANALOG_AXIS_TIME 2 /* 2 * refresh */#define ANALOG_INIT_RETRIES 8 /* 8 times */#define ANALOG_FUZZ_BITS 2 /* 2 bit more */#define ANALOG_FUZZ_MAGIC 36 /* 36 u*ms/loop */#define ANALOG_MAX_NAME_LENGTH 128static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };static short analog_pad_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_TL2, BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_BASE };static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };struct analog { struct input_dev dev; int mask; short *buttons; char name[ANALOG_MAX_NAME_LENGTH];};struct analog_port { struct gameport *gameport; struct timer_list timer; struct analog analog[2]; unsigned char mask; char saitek; char cooked; int bads; int reads; int speed; int loop; int fuzz; int axes[4]; int buttons; int initial[4]; int used; int axtime;};/* * Time macros. */#ifdef __i386__#define GET_TIME(x) do { if (cpu_has_tsc) rdtscl(x); else { outb(0, 0x43); x = inb(0x40); x |= inb(0x40) << 8; } } while (0)#define DELTA(x,y) (cpu_has_tsc?((y)-(x)):((x)-(y)+((x)<(y)?1193180L/HZ:0)))#define TIME_NAME (cpu_has_tsc?"TSC":"PIT")#elif __x86_64__#define GET_TIME(x) rdtscl(x)#define DELTA(x,y) ((y)-(x))#define TIME_NAME "TSC"#elif __alpha__#define GET_TIME(x) ((x) = get_cycles())#define DELTA(x,y) ((y)-(x))#define TIME_NAME "PCC"#else#define FAKE_TIMEstatic unsigned long analog_faketime = 0;#define GET_TIME(x) do { x = analog_faketime++; } while(0)#define DELTA(x,y) ((y)-(x))#define TIME_NAME "Unreliable"#warning Precise timer not defined for this architecture.#endif/* * analog_decode() decodes analog joystick data and reports input events. */static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons){ struct input_dev *dev = &analog->dev; int i, j; if (analog->mask & ANALOG_HAT_FCS) for (i = 0; i < 4; i++) if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) { buttons |= 1 << (i + 14); break; } for (i = j = 0; i < 6; i++) if (analog->mask & (0x10 << i)) input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1); if (analog->mask & ANALOG_HBTN_CHF) for (i = 0; i < 4; i++) input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1); if (analog->mask & ANALOG_BTN_TL) input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1)); if (analog->mask & ANALOG_BTN_TR) input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1)); if (analog->mask & ANALOG_BTN_TL2) input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1))); if (analog->mask & ANALOG_BTN_TR2) input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1))); for (i = j = 0; i < 4; i++) if (analog->mask & (1 << i)) input_report_abs(dev, analog_axes[j++], axes[i]); for (i = j = 0; i < 3; i++) if (analog->mask & analog_exts[i]) { input_report_abs(dev, analog_hats[j++], ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1)); input_report_abs(dev, analog_hats[j++], ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1)); }}/* * analog_cooked_read() reads analog joystick data. */static int analog_cooked_read(struct analog_port *port){ struct gameport *gameport = port->gameport; unsigned int time[4], start, loop, now, loopout, timeout; unsigned char data[4], this, last; unsigned long flags; int i, j; loopout = (ANALOG_LOOP_TIME * port->loop) / 1000; timeout = ANALOG_MAX_TIME * port->speed; __save_flags(flags); __cli(); gameport_trigger(gameport); GET_TIME(now); __restore_flags(flags); start = now; this = port->mask; i = 0; do { loop = now; last = this; __cli(); this = gameport_read(gameport) & port->mask; GET_TIME(now); __restore_flags(flags); if ((last ^ this) && (DELTA(loop, now) < loopout)) { data[i] = last ^ this; time[i] = now; i++; } } while (this && (i < 4) && (DELTA(start, now) < timeout)); this <<= 4; for (--i; i >= 0; i--) { this |= data[i]; for (j = 0; j < 4; j++) if (data[i] & (1 << j)) port->axes[j] = (DELTA(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop; } return -(this != port->mask);}static int analog_button_read(struct analog_port *port, char saitek, char chf){ unsigned char u; int t = 1, i = 0; int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME); u = gameport_read(port->gameport); if (!chf) { port->buttons = (~u >> 4) & 0xf; return 0; } port->buttons = 0; while ((~u & 0xf0) && (i < 16) && t) { port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf]; if (!saitek) return 0; udelay(ANALOG_SAITEK_DELAY); t = strobe; gameport_trigger(port->gameport); while (((u = gameport_read(port->gameport)) & port->mask) && t) t--; i++; } return -(!t || (i == 16));}/* * analog_timer() repeatedly polls the Analog joysticks. */static void analog_timer(unsigned long data){ struct analog_port *port = (void *) data; int i; char saitek = !!(port->analog[0].mask & ANALOG_SAITEK); char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF); if (port->cooked) { port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons); if (chf) port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0; port->reads++; } else { if (!port->axtime--) { port->bads -= analog_cooked_read(port); port->bads -= analog_button_read(port, saitek, chf); port->reads++; port->axtime = ANALOG_AXIS_TIME - 1; } else { if (!saitek) analog_button_read(port, saitek, chf); } } for (i = 0; i < 2; i++) if (port->analog[i].mask) analog_decode(port->analog + i, port->axes, port->initial, port->buttons); mod_timer(&port->timer, jiffies + ANALOG_REFRESH_TIME);}/* * analog_open() is a callback from the input open routine. */static int analog_open(struct input_dev *dev){ struct analog_port *port = dev->private; if (!port->used++) mod_timer(&port->timer, jiffies + ANALOG_REFRESH_TIME); return 0;}/* * analog_close() is a callback from the input close routine. */static void analog_close(struct input_dev *dev){ struct analog_port *port = dev->private; if (!--port->used) del_timer(&port->timer);}/* * analog_calibrate_timer() calibrates the timer and computes loop * and timeout values for a joystick port. */static void analog_calibrate_timer(struct analog_port *port){ struct gameport *gameport = port->gameport; unsigned int i, t, tx, t1, t2, t3; unsigned long flags; save_flags(flags); cli(); GET_TIME(t1);#ifdef FAKE_TIME analog_faketime += 830;#endif udelay(1000); GET_TIME(t2); GET_TIME(t3); restore_flags(flags); port->speed = DELTA(t1, t2) - DELTA(t2, t3); tx = ~0; for (i = 0; i < 50; i++) { save_flags(flags); cli(); GET_TIME(t1); for (t = 0; t < 50; t++) { gameport_read(gameport); GET_TIME(t2); } GET_TIME(t3); restore_flags(flags); udelay(i); t = DELTA(t1, t2) - DELTA(t2, t3);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -