analog.c 20 KB

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