analog.c 20 KB

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