analog.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1996-2001 Vojtech Pavlik
  4. */
  5. /*
  6. * Analog joystick and gamepad driver for Linux
  7. */
  8. /*
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/bitops.h>
  15. #include <linux/init.h>
  16. #include <linux/input.h>
  17. #include <linux/gameport.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/timex.h>
  20. #include <linux/timekeeping.h>
  21. #define DRIVER_DESC "Analog joystick and gamepad driver"
  22. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  23. MODULE_DESCRIPTION(DRIVER_DESC);
  24. MODULE_LICENSE("GPL");
  25. static bool use_ktime = true;
  26. module_param(use_ktime, bool, 0400);
  27. MODULE_PARM_DESC(use_ktime, "Use ktime for measuring I/O speed");
  28. /*
  29. * Option parsing.
  30. */
  31. #define ANALOG_PORTS 16
  32. static char *js[ANALOG_PORTS];
  33. static unsigned int js_nargs;
  34. static int analog_options[ANALOG_PORTS];
  35. module_param_array_named(map, js, charp, &js_nargs, 0);
  36. MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
  37. /*
  38. * Times, feature definitions.
  39. */
  40. #define ANALOG_RUDDER 0x00004
  41. #define ANALOG_THROTTLE 0x00008
  42. #define ANALOG_AXES_STD 0x0000f
  43. #define ANALOG_BTNS_STD 0x000f0
  44. #define ANALOG_BTNS_CHF 0x00100
  45. #define ANALOG_HAT1_CHF 0x00200
  46. #define ANALOG_HAT2_CHF 0x00400
  47. #define ANALOG_HAT_FCS 0x00800
  48. #define ANALOG_HATS_ALL 0x00e00
  49. #define ANALOG_BTN_TL 0x01000
  50. #define ANALOG_BTN_TR 0x02000
  51. #define ANALOG_BTN_TL2 0x04000
  52. #define ANALOG_BTN_TR2 0x08000
  53. #define ANALOG_BTNS_TLR 0x03000
  54. #define ANALOG_BTNS_TLR2 0x0c000
  55. #define ANALOG_BTNS_GAMEPAD 0x0f000
  56. #define ANALOG_HBTN_CHF 0x10000
  57. #define ANALOG_ANY_CHF 0x10700
  58. #define ANALOG_SAITEK 0x20000
  59. #define ANALOG_EXTENSIONS 0x7ff00
  60. #define ANALOG_GAMEPAD 0x80000
  61. #define ANALOG_MAX_TIME 3 /* 3 ms */
  62. #define ANALOG_LOOP_TIME 2000 /* 2 * loop */
  63. #define ANALOG_SAITEK_DELAY 200 /* 200 us */
  64. #define ANALOG_SAITEK_TIME 2000 /* 2000 us */
  65. #define ANALOG_AXIS_TIME 2 /* 2 * refresh */
  66. #define ANALOG_INIT_RETRIES 8 /* 8 times */
  67. #define ANALOG_FUZZ_BITS 2 /* 2 bit more */
  68. #define ANALOG_FUZZ_MAGIC 36 /* 36 u*ms/loop */
  69. #define ANALOG_MAX_NAME_LENGTH 128
  70. #define ANALOG_MAX_PHYS_LENGTH 32
  71. static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
  72. static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
  73. static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
  74. static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
  75. 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 };
  76. static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2,
  77. BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };
  78. static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
  79. struct analog {
  80. struct input_dev *dev;
  81. int mask;
  82. short *buttons;
  83. char name[ANALOG_MAX_NAME_LENGTH];
  84. char phys[ANALOG_MAX_PHYS_LENGTH];
  85. };
  86. struct analog_port {
  87. struct gameport *gameport;
  88. struct analog analog[2];
  89. unsigned char mask;
  90. char saitek;
  91. char cooked;
  92. int bads;
  93. int reads;
  94. int speed;
  95. int loop;
  96. int fuzz;
  97. int axes[4];
  98. int buttons;
  99. int initial[4];
  100. int axtime;
  101. };
  102. /*
  103. * Time macros.
  104. */
  105. #ifdef __i386__
  106. #include <linux/i8253.h>
  107. #define GET_TIME(x) do { if (boot_cpu_has(X86_FEATURE_TSC)) x = (unsigned int)rdtsc(); else x = get_time_pit(); } while (0)
  108. #define DELTA(x,y) (boot_cpu_has(X86_FEATURE_TSC) ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? PIT_TICK_RATE / HZ : 0)))
  109. #define TIME_NAME (boot_cpu_has(X86_FEATURE_TSC)?"TSC":"PIT")
  110. static unsigned int get_time_pit(void)
  111. {
  112. unsigned long flags;
  113. unsigned int count;
  114. raw_spin_lock_irqsave(&i8253_lock, flags);
  115. outb_p(0x00, 0x43);
  116. count = inb_p(0x40);
  117. count |= inb_p(0x40) << 8;
  118. raw_spin_unlock_irqrestore(&i8253_lock, flags);
  119. return count;
  120. }
  121. #elif defined(__x86_64__)
  122. #define GET_TIME(x) do { x = (unsigned int)rdtsc(); } while (0)
  123. #define DELTA(x,y) ((y)-(x))
  124. #define TIME_NAME "TSC"
  125. #elif defined(__alpha__) || defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_PPC) || defined(CONFIG_RISCV)
  126. #define GET_TIME(x) do { x = get_cycles(); } while (0)
  127. #define DELTA(x,y) ((y)-(x))
  128. #define TIME_NAME "get_cycles"
  129. #else
  130. #define FAKE_TIME
  131. static unsigned long analog_faketime = 0;
  132. #define GET_TIME(x) do { x = analog_faketime++; } while(0)
  133. #define DELTA(x,y) ((y)-(x))
  134. #define TIME_NAME "Unreliable"
  135. #warning Precise timer not defined for this architecture.
  136. #endif
  137. static inline u64 get_time(void)
  138. {
  139. if (use_ktime) {
  140. return ktime_get_ns();
  141. } else {
  142. unsigned int x;
  143. GET_TIME(x);
  144. return x;
  145. }
  146. }
  147. static inline unsigned int delta(u64 x, u64 y)
  148. {
  149. if (use_ktime)
  150. return y - x;
  151. else
  152. return DELTA((unsigned int)x, (unsigned int)y);
  153. }
  154. /*
  155. * analog_decode() decodes analog joystick data and reports input events.
  156. */
  157. static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
  158. {
  159. struct input_dev *dev = analog->dev;
  160. int i, j;
  161. if (analog->mask & ANALOG_HAT_FCS)
  162. for (i = 0; i < 4; i++)
  163. if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
  164. buttons |= 1 << (i + 14);
  165. break;
  166. }
  167. for (i = j = 0; i < 6; i++)
  168. if (analog->mask & (0x10 << i))
  169. input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
  170. if (analog->mask & ANALOG_HBTN_CHF)
  171. for (i = 0; i < 4; i++)
  172. input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
  173. if (analog->mask & ANALOG_BTN_TL)
  174. input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
  175. if (analog->mask & ANALOG_BTN_TR)
  176. input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
  177. if (analog->mask & ANALOG_BTN_TL2)
  178. input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
  179. if (analog->mask & ANALOG_BTN_TR2)
  180. input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
  181. for (i = j = 0; i < 4; i++)
  182. if (analog->mask & (1 << i))
  183. input_report_abs(dev, analog_axes[j++], axes[i]);
  184. for (i = j = 0; i < 3; i++)
  185. if (analog->mask & analog_exts[i]) {
  186. input_report_abs(dev, analog_hats[j++],
  187. ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
  188. input_report_abs(dev, analog_hats[j++],
  189. ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
  190. }
  191. input_sync(dev);
  192. }
  193. /*
  194. * analog_cooked_read() reads analog joystick data.
  195. */
  196. static int analog_cooked_read(struct analog_port *port)
  197. {
  198. struct gameport *gameport = port->gameport;
  199. u64 time[4], start, loop, now;
  200. unsigned int loopout, timeout;
  201. unsigned char data[4], this, last;
  202. unsigned long flags;
  203. int i, j;
  204. loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
  205. timeout = ANALOG_MAX_TIME * port->speed;
  206. local_irq_save(flags);
  207. gameport_trigger(gameport);
  208. now = get_time();
  209. local_irq_restore(flags);
  210. start = now;
  211. this = port->mask;
  212. i = 0;
  213. do {
  214. loop = now;
  215. last = this;
  216. local_irq_disable();
  217. this = gameport_read(gameport) & port->mask;
  218. now = get_time();
  219. local_irq_restore(flags);
  220. if ((last ^ this) && (delta(loop, now) < loopout)) {
  221. data[i] = last ^ this;
  222. time[i] = now;
  223. i++;
  224. }
  225. } while (this && (i < 4) && (delta(start, now) < timeout));
  226. this <<= 4;
  227. for (--i; i >= 0; i--) {
  228. this |= data[i];
  229. for (j = 0; j < 4; j++)
  230. if (data[i] & (1 << j))
  231. port->axes[j] = (delta(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop;
  232. }
  233. return -(this != port->mask);
  234. }
  235. static int analog_button_read(struct analog_port *port, char saitek, char chf)
  236. {
  237. unsigned char u;
  238. int t = 1, i = 0;
  239. int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
  240. u = gameport_read(port->gameport);
  241. if (!chf) {
  242. port->buttons = (~u >> 4) & 0xf;
  243. return 0;
  244. }
  245. port->buttons = 0;
  246. while ((~u & 0xf0) && (i < 16) && t) {
  247. port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
  248. if (!saitek) return 0;
  249. udelay(ANALOG_SAITEK_DELAY);
  250. t = strobe;
  251. gameport_trigger(port->gameport);
  252. while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
  253. i++;
  254. }
  255. return -(!t || (i == 16));
  256. }
  257. /*
  258. * analog_poll() repeatedly polls the Analog joysticks.
  259. */
  260. static void analog_poll(struct gameport *gameport)
  261. {
  262. struct analog_port *port = gameport_get_drvdata(gameport);
  263. int i;
  264. char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
  265. char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
  266. if (port->cooked) {
  267. port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
  268. if (chf)
  269. port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
  270. port->reads++;
  271. } else {
  272. if (!port->axtime--) {
  273. port->bads -= analog_cooked_read(port);
  274. port->bads -= analog_button_read(port, saitek, chf);
  275. port->reads++;
  276. port->axtime = ANALOG_AXIS_TIME - 1;
  277. } else {
  278. if (!saitek)
  279. analog_button_read(port, saitek, chf);
  280. }
  281. }
  282. for (i = 0; i < 2; i++)
  283. if (port->analog[i].mask)
  284. analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
  285. }
  286. /*
  287. * analog_open() is a callback from the input open routine.
  288. */
  289. static int analog_open(struct input_dev *dev)
  290. {
  291. struct analog_port *port = input_get_drvdata(dev);
  292. gameport_start_polling(port->gameport);
  293. return 0;
  294. }
  295. /*
  296. * analog_close() is a callback from the input close routine.
  297. */
  298. static void analog_close(struct input_dev *dev)
  299. {
  300. struct analog_port *port = input_get_drvdata(dev);
  301. gameport_stop_polling(port->gameport);
  302. }
  303. /*
  304. * analog_calibrate_timer() calibrates the timer and computes loop
  305. * and timeout values for a joystick port.
  306. */
  307. static void analog_calibrate_timer(struct analog_port *port)
  308. {
  309. struct gameport *gameport = port->gameport;
  310. unsigned int i, t, tx;
  311. u64 t1, t2, t3;
  312. unsigned long flags;
  313. if (use_ktime) {
  314. port->speed = 1000000;
  315. } else {
  316. local_irq_save(flags);
  317. t1 = get_time();
  318. #ifdef FAKE_TIME
  319. analog_faketime += 830;
  320. #endif
  321. mdelay(1);
  322. t2 = get_time();
  323. t3 = get_time();
  324. local_irq_restore(flags);
  325. port->speed = delta(t1, t2) - delta(t2, t3);
  326. }
  327. tx = ~0;
  328. for (i = 0; i < 50; i++) {
  329. local_irq_save(flags);
  330. t1 = get_time();
  331. for (t = 0; t < 50; t++) {
  332. gameport_read(gameport);
  333. t2 = get_time();
  334. }
  335. t3 = get_time();
  336. local_irq_restore(flags);
  337. udelay(i);
  338. t = delta(t1, t2) - delta(t2, t3);
  339. if (t < tx) tx = t;
  340. }
  341. port->loop = tx / 50;
  342. }
  343. /*
  344. * analog_name() constructs a name for an analog joystick.
  345. */
  346. static void analog_name(struct analog *analog)
  347. {
  348. snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
  349. hweight8(analog->mask & ANALOG_AXES_STD),
  350. hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
  351. hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
  352. if (analog->mask & ANALOG_HATS_ALL)
  353. snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
  354. analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
  355. if (analog->mask & ANALOG_HAT_FCS)
  356. strlcat(analog->name, " FCS", sizeof(analog->name));
  357. if (analog->mask & ANALOG_ANY_CHF)
  358. strlcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF",
  359. sizeof(analog->name));
  360. strlcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick",
  361. sizeof(analog->name));
  362. }
  363. /*
  364. * analog_init_device()
  365. */
  366. static int analog_init_device(struct analog_port *port, struct analog *analog, int index)
  367. {
  368. struct input_dev *input_dev;
  369. int i, j, t, v, w, x, y, z;
  370. int error;
  371. analog_name(analog);
  372. snprintf(analog->phys, sizeof(analog->phys),
  373. "%s/input%d", port->gameport->phys, index);
  374. analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
  375. analog->dev = input_dev = input_allocate_device();
  376. if (!input_dev)
  377. return -ENOMEM;
  378. input_dev->name = analog->name;
  379. input_dev->phys = analog->phys;
  380. input_dev->id.bustype = BUS_GAMEPORT;
  381. input_dev->id.vendor = GAMEPORT_ID_VENDOR_ANALOG;
  382. input_dev->id.product = analog->mask >> 4;
  383. input_dev->id.version = 0x0100;
  384. input_dev->dev.parent = &port->gameport->dev;
  385. input_set_drvdata(input_dev, port);
  386. input_dev->open = analog_open;
  387. input_dev->close = analog_close;
  388. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  389. for (i = j = 0; i < 4; i++)
  390. if (analog->mask & (1 << i)) {
  391. t = analog_axes[j];
  392. x = port->axes[i];
  393. y = (port->axes[0] + port->axes[1]) >> 1;
  394. z = y - port->axes[i];
  395. z = z > 0 ? z : -z;
  396. v = (x >> 3);
  397. w = (x >> 3);
  398. if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
  399. x = y;
  400. if (analog->mask & ANALOG_SAITEK) {
  401. if (i == 2) x = port->axes[i];
  402. v = x - (x >> 2);
  403. w = (x >> 4);
  404. }
  405. input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w);
  406. j++;
  407. }
  408. for (i = j = 0; i < 3; i++)
  409. if (analog->mask & analog_exts[i])
  410. for (x = 0; x < 2; x++) {
  411. t = analog_hats[j++];
  412. input_set_abs_params(input_dev, t, -1, 1, 0, 0);
  413. }
  414. for (i = j = 0; i < 4; i++)
  415. if (analog->mask & (0x10 << i))
  416. set_bit(analog->buttons[j++], input_dev->keybit);
  417. if (analog->mask & ANALOG_BTNS_CHF)
  418. for (i = 0; i < 2; i++)
  419. set_bit(analog->buttons[j++], input_dev->keybit);
  420. if (analog->mask & ANALOG_HBTN_CHF)
  421. for (i = 0; i < 4; i++)
  422. set_bit(analog->buttons[j++], input_dev->keybit);
  423. for (i = 0; i < 4; i++)
  424. if (analog->mask & (ANALOG_BTN_TL << i))
  425. set_bit(analog_pads[i], input_dev->keybit);
  426. analog_decode(analog, port->axes, port->initial, port->buttons);
  427. error = input_register_device(analog->dev);
  428. if (error) {
  429. input_free_device(analog->dev);
  430. return error;
  431. }
  432. return 0;
  433. }
  434. /*
  435. * analog_init_devices() sets up device-specific values and registers the input devices.
  436. */
  437. static int analog_init_masks(struct analog_port *port)
  438. {
  439. int i;
  440. struct analog *analog = port->analog;
  441. int max[4];
  442. if (!port->mask)
  443. return -1;
  444. if ((port->mask & 3) != 3 && port->mask != 0xc) {
  445. printk(KERN_WARNING "analog.c: Unknown joystick device found "
  446. "(data=%#x, %s), probably not analog joystick.\n",
  447. port->mask, port->gameport->phys);
  448. return -1;
  449. }
  450. i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
  451. analog[0].mask = i & 0xfffff;
  452. analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
  453. | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
  454. | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
  455. analog[0].mask &= ~(ANALOG_HAT2_CHF)
  456. | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
  457. analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
  458. | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
  459. | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
  460. | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
  461. analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
  462. | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
  463. & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
  464. analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
  465. analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
  466. : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
  467. if (port->cooked) {
  468. for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
  469. if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
  470. if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
  471. if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
  472. if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
  473. if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
  474. gameport_calibrate(port->gameport, port->axes, max);
  475. }
  476. for (i = 0; i < 4; i++)
  477. port->initial[i] = port->axes[i];
  478. return -!(analog[0].mask || analog[1].mask);
  479. }
  480. static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
  481. {
  482. int i, t, u, v;
  483. port->gameport = gameport;
  484. gameport_set_drvdata(gameport, port);
  485. if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
  486. analog_calibrate_timer(port);
  487. gameport_trigger(gameport);
  488. t = gameport_read(gameport);
  489. msleep(ANALOG_MAX_TIME);
  490. port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
  491. port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
  492. for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
  493. if (!analog_cooked_read(port))
  494. break;
  495. msleep(ANALOG_MAX_TIME);
  496. }
  497. u = v = 0;
  498. msleep(ANALOG_MAX_TIME);
  499. t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
  500. gameport_trigger(gameport);
  501. while ((gameport_read(port->gameport) & port->mask) && (u < t))
  502. u++;
  503. udelay(ANALOG_SAITEK_DELAY);
  504. t = gameport_time(gameport, ANALOG_SAITEK_TIME);
  505. gameport_trigger(gameport);
  506. while ((gameport_read(port->gameport) & port->mask) && (v < t))
  507. v++;
  508. if (v < (u >> 1)) { /* FIXME - more than one port */
  509. analog_options[0] |= /* FIXME - more than one port */
  510. ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
  511. return 0;
  512. }
  513. gameport_close(gameport);
  514. }
  515. if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
  516. for (i = 0; i < ANALOG_INIT_RETRIES; i++)
  517. if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
  518. break;
  519. for (i = 0; i < 4; i++)
  520. if (port->axes[i] != -1)
  521. port->mask |= 1 << i;
  522. port->fuzz = gameport->fuzz;
  523. port->cooked = 1;
  524. return 0;
  525. }
  526. return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  527. }
  528. static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
  529. {
  530. struct analog_port *port;
  531. int i;
  532. int err;
  533. if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
  534. return - ENOMEM;
  535. err = analog_init_port(gameport, drv, port);
  536. if (err)
  537. goto fail1;
  538. err = analog_init_masks(port);
  539. if (err)
  540. goto fail2;
  541. gameport_set_poll_handler(gameport, analog_poll);
  542. gameport_set_poll_interval(gameport, 10);
  543. for (i = 0; i < 2; i++)
  544. if (port->analog[i].mask) {
  545. err = analog_init_device(port, port->analog + i, i);
  546. if (err)
  547. goto fail3;
  548. }
  549. return 0;
  550. fail3: while (--i >= 0)
  551. if (port->analog[i].mask)
  552. input_unregister_device(port->analog[i].dev);
  553. fail2: gameport_close(gameport);
  554. fail1: gameport_set_drvdata(gameport, NULL);
  555. kfree(port);
  556. return err;
  557. }
  558. static void analog_disconnect(struct gameport *gameport)
  559. {
  560. struct analog_port *port = gameport_get_drvdata(gameport);
  561. int i;
  562. for (i = 0; i < 2; i++)
  563. if (port->analog[i].mask)
  564. input_unregister_device(port->analog[i].dev);
  565. gameport_close(gameport);
  566. gameport_set_drvdata(gameport, NULL);
  567. printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
  568. port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
  569. port->gameport->phys);
  570. kfree(port);
  571. }
  572. struct analog_types {
  573. char *name;
  574. int value;
  575. };
  576. static struct analog_types analog_types[] = {
  577. { "none", 0x00000000 },
  578. { "auto", 0x000000ff },
  579. { "2btn", 0x0000003f },
  580. { "y-joy", 0x0cc00033 },
  581. { "y-pad", 0x8cc80033 },
  582. { "fcs", 0x000008f7 },
  583. { "chf", 0x000002ff },
  584. { "fullchf", 0x000007ff },
  585. { "gamepad", 0x000830f3 },
  586. { "gamepad8", 0x0008f0f3 },
  587. { NULL, 0 }
  588. };
  589. static void analog_parse_options(void)
  590. {
  591. int i, j;
  592. char *end;
  593. for (i = 0; i < js_nargs; i++) {
  594. for (j = 0; analog_types[j].name; j++)
  595. if (!strcmp(analog_types[j].name, js[i])) {
  596. analog_options[i] = analog_types[j].value;
  597. break;
  598. }
  599. if (analog_types[j].name) continue;
  600. analog_options[i] = simple_strtoul(js[i], &end, 0);
  601. if (end != js[i]) continue;
  602. analog_options[i] = 0xff;
  603. if (!strlen(js[i])) continue;
  604. printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
  605. }
  606. for (; i < ANALOG_PORTS; i++)
  607. analog_options[i] = 0xff;
  608. }
  609. /*
  610. * The gameport device structure.
  611. */
  612. static struct gameport_driver analog_drv = {
  613. .driver = {
  614. .name = "analog",
  615. },
  616. .description = DRIVER_DESC,
  617. .connect = analog_connect,
  618. .disconnect = analog_disconnect,
  619. };
  620. static int __init analog_init(void)
  621. {
  622. analog_parse_options();
  623. return gameport_register_driver(&analog_drv);
  624. }
  625. static void __exit analog_exit(void)
  626. {
  627. gameport_unregister_driver(&analog_drv);
  628. }
  629. module_init(analog_init);
  630. module_exit(analog_exit);