analog.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 <asm/timex.h>
  39. #define DRIVER_DESC "Analog joystick and gamepad driver"
  40. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  41. MODULE_DESCRIPTION(DRIVER_DESC);
  42. MODULE_LICENSE("GPL");
  43. /*
  44. * Option parsing.
  45. */
  46. #define ANALOG_PORTS 16
  47. static char *js[ANALOG_PORTS];
  48. static int js_nargs;
  49. static int analog_options[ANALOG_PORTS];
  50. module_param_array_named(map, js, charp, &js_nargs, 0);
  51. MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
  52. __obsolete_setup("js=");
  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 <asm/i8253.h>
  123. #define GET_TIME(x) do { if (cpu_has_tsc) rdtscl(x); else x = get_time_pit(); } while (0)
  124. #define DELTA(x,y) (cpu_has_tsc ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? CLOCK_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. spin_lock_irqsave(&i8253_lock, flags);
  131. outb_p(0x00, 0x43);
  132. count = inb_p(0x40);
  133. count |= inb_p(0x40) << 8;
  134. spin_unlock_irqrestore(&i8253_lock, flags);
  135. return count;
  136. }
  137. #elif defined(__x86_64__)
  138. #define GET_TIME(x) rdtscl(x)
  139. #define DELTA(x,y) ((y)-(x))
  140. #define TIME_NAME "TSC"
  141. #elif defined(__alpha__)
  142. #define GET_TIME(x) do { x = get_cycles(); } while (0)
  143. #define DELTA(x,y) ((y)-(x))
  144. #define TIME_NAME "PCC"
  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. /*
  154. * analog_decode() decodes analog joystick data and reports input events.
  155. */
  156. static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
  157. {
  158. struct input_dev *dev = &analog->dev;
  159. int i, j;
  160. if (analog->mask & ANALOG_HAT_FCS)
  161. for (i = 0; i < 4; i++)
  162. if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
  163. buttons |= 1 << (i + 14);
  164. break;
  165. }
  166. for (i = j = 0; i < 6; i++)
  167. if (analog->mask & (0x10 << i))
  168. input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
  169. if (analog->mask & ANALOG_HBTN_CHF)
  170. for (i = 0; i < 4; i++)
  171. input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
  172. if (analog->mask & ANALOG_BTN_TL)
  173. input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
  174. if (analog->mask & ANALOG_BTN_TR)
  175. input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
  176. if (analog->mask & ANALOG_BTN_TL2)
  177. input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
  178. if (analog->mask & ANALOG_BTN_TR2)
  179. input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
  180. for (i = j = 0; i < 4; i++)
  181. if (analog->mask & (1 << i))
  182. input_report_abs(dev, analog_axes[j++], axes[i]);
  183. for (i = j = 0; i < 3; i++)
  184. if (analog->mask & analog_exts[i]) {
  185. input_report_abs(dev, analog_hats[j++],
  186. ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
  187. input_report_abs(dev, analog_hats[j++],
  188. ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
  189. }
  190. input_sync(dev);
  191. }
  192. /*
  193. * analog_cooked_read() reads analog joystick data.
  194. */
  195. static int analog_cooked_read(struct analog_port *port)
  196. {
  197. struct gameport *gameport = port->gameport;
  198. unsigned int time[4], start, loop, now, loopout, timeout;
  199. unsigned char data[4], this, last;
  200. unsigned long flags;
  201. int i, j;
  202. loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
  203. timeout = ANALOG_MAX_TIME * port->speed;
  204. local_irq_save(flags);
  205. gameport_trigger(gameport);
  206. GET_TIME(now);
  207. local_irq_restore(flags);
  208. start = now;
  209. this = port->mask;
  210. i = 0;
  211. do {
  212. loop = now;
  213. last = this;
  214. local_irq_disable();
  215. this = gameport_read(gameport) & port->mask;
  216. GET_TIME(now);
  217. local_irq_restore(flags);
  218. if ((last ^ this) && (DELTA(loop, now) < loopout)) {
  219. data[i] = last ^ this;
  220. time[i] = now;
  221. i++;
  222. }
  223. } while (this && (i < 4) && (DELTA(start, now) < timeout));
  224. this <<= 4;
  225. for (--i; i >= 0; i--) {
  226. this |= data[i];
  227. for (j = 0; j < 4; j++)
  228. if (data[i] & (1 << j))
  229. port->axes[j] = (DELTA(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop;
  230. }
  231. return -(this != port->mask);
  232. }
  233. static int analog_button_read(struct analog_port *port, char saitek, char chf)
  234. {
  235. unsigned char u;
  236. int t = 1, i = 0;
  237. int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
  238. u = gameport_read(port->gameport);
  239. if (!chf) {
  240. port->buttons = (~u >> 4) & 0xf;
  241. return 0;
  242. }
  243. port->buttons = 0;
  244. while ((~u & 0xf0) && (i < 16) && t) {
  245. port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
  246. if (!saitek) return 0;
  247. udelay(ANALOG_SAITEK_DELAY);
  248. t = strobe;
  249. gameport_trigger(port->gameport);
  250. while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
  251. i++;
  252. }
  253. return -(!t || (i == 16));
  254. }
  255. /*
  256. * analog_poll() repeatedly polls the Analog joysticks.
  257. */
  258. static void analog_poll(struct gameport *gameport)
  259. {
  260. struct analog_port *port = gameport_get_drvdata(gameport);
  261. int i;
  262. char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
  263. char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
  264. if (port->cooked) {
  265. port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
  266. if (chf)
  267. port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
  268. port->reads++;
  269. } else {
  270. if (!port->axtime--) {
  271. port->bads -= analog_cooked_read(port);
  272. port->bads -= analog_button_read(port, saitek, chf);
  273. port->reads++;
  274. port->axtime = ANALOG_AXIS_TIME - 1;
  275. } else {
  276. if (!saitek)
  277. analog_button_read(port, saitek, chf);
  278. }
  279. }
  280. for (i = 0; i < 2; i++)
  281. if (port->analog[i].mask)
  282. analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
  283. }
  284. /*
  285. * analog_open() is a callback from the input open routine.
  286. */
  287. static int analog_open(struct input_dev *dev)
  288. {
  289. struct analog_port *port = dev->private;
  290. gameport_start_polling(port->gameport);
  291. return 0;
  292. }
  293. /*
  294. * analog_close() is a callback from the input close routine.
  295. */
  296. static void analog_close(struct input_dev *dev)
  297. {
  298. struct analog_port *port = dev->private;
  299. gameport_stop_polling(port->gameport);
  300. }
  301. /*
  302. * analog_calibrate_timer() calibrates the timer and computes loop
  303. * and timeout values for a joystick port.
  304. */
  305. static void analog_calibrate_timer(struct analog_port *port)
  306. {
  307. struct gameport *gameport = port->gameport;
  308. unsigned int i, t, tx, t1, t2, t3;
  309. unsigned long flags;
  310. local_irq_save(flags);
  311. GET_TIME(t1);
  312. #ifdef FAKE_TIME
  313. analog_faketime += 830;
  314. #endif
  315. mdelay(1);
  316. GET_TIME(t2);
  317. GET_TIME(t3);
  318. local_irq_restore(flags);
  319. port->speed = DELTA(t1, t2) - DELTA(t2, t3);
  320. tx = ~0;
  321. for (i = 0; i < 50; i++) {
  322. local_irq_save(flags);
  323. GET_TIME(t1);
  324. for (t = 0; t < 50; t++) { gameport_read(gameport); GET_TIME(t2); }
  325. GET_TIME(t3);
  326. local_irq_restore(flags);
  327. udelay(i);
  328. t = DELTA(t1, t2) - DELTA(t2, t3);
  329. if (t < tx) tx = t;
  330. }
  331. port->loop = tx / 50;
  332. }
  333. /*
  334. * analog_name() constructs a name for an analog joystick.
  335. */
  336. static void analog_name(struct analog *analog)
  337. {
  338. sprintf(analog->name, "Analog %d-axis %d-button",
  339. hweight8(analog->mask & ANALOG_AXES_STD),
  340. hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
  341. hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
  342. if (analog->mask & ANALOG_HATS_ALL)
  343. sprintf(analog->name, "%s %d-hat",
  344. analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
  345. if (analog->mask & ANALOG_HAT_FCS)
  346. strcat(analog->name, " FCS");
  347. if (analog->mask & ANALOG_ANY_CHF)
  348. strcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
  349. strcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick");
  350. }
  351. /*
  352. * analog_init_device()
  353. */
  354. static void analog_init_device(struct analog_port *port, struct analog *analog, int index)
  355. {
  356. int i, j, t, v, w, x, y, z;
  357. analog_name(analog);
  358. sprintf(analog->phys, "%s/input%d", port->gameport->phys, index);
  359. analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
  360. init_input_dev(&analog->dev);
  361. analog->dev.name = analog->name;
  362. analog->dev.phys = analog->phys;
  363. analog->dev.id.bustype = BUS_GAMEPORT;
  364. analog->dev.id.vendor = GAMEPORT_ID_VENDOR_ANALOG;
  365. analog->dev.id.product = analog->mask >> 4;
  366. analog->dev.id.version = 0x0100;
  367. analog->dev.open = analog_open;
  368. analog->dev.close = analog_close;
  369. analog->dev.private = port;
  370. analog->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  371. for (i = j = 0; i < 4; i++)
  372. if (analog->mask & (1 << i)) {
  373. t = analog_axes[j];
  374. x = port->axes[i];
  375. y = (port->axes[0] + port->axes[1]) >> 1;
  376. z = y - port->axes[i];
  377. z = z > 0 ? z : -z;
  378. v = (x >> 3);
  379. w = (x >> 3);
  380. set_bit(t, analog->dev.absbit);
  381. if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
  382. x = y;
  383. if (analog->mask & ANALOG_SAITEK) {
  384. if (i == 2) x = port->axes[i];
  385. v = x - (x >> 2);
  386. w = (x >> 4);
  387. }
  388. analog->dev.absmax[t] = (x << 1) - v;
  389. analog->dev.absmin[t] = v;
  390. analog->dev.absfuzz[t] = port->fuzz;
  391. analog->dev.absflat[t] = 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. set_bit(t, analog->dev.absbit);
  399. analog->dev.absmax[t] = 1;
  400. analog->dev.absmin[t] = -1;
  401. }
  402. for (i = j = 0; i < 4; i++)
  403. if (analog->mask & (0x10 << i))
  404. set_bit(analog->buttons[j++], analog->dev.keybit);
  405. if (analog->mask & ANALOG_BTNS_CHF)
  406. for (i = 0; i < 2; i++)
  407. set_bit(analog->buttons[j++], analog->dev.keybit);
  408. if (analog->mask & ANALOG_HBTN_CHF)
  409. for (i = 0; i < 4; i++)
  410. set_bit(analog->buttons[j++], analog->dev.keybit);
  411. for (i = 0; i < 4; i++)
  412. if (analog->mask & (ANALOG_BTN_TL << i))
  413. set_bit(analog_pads[i], analog->dev.keybit);
  414. analog_decode(analog, port->axes, port->initial, port->buttons);
  415. input_register_device(&analog->dev);
  416. printk(KERN_INFO "input: %s at %s", analog->name, port->gameport->phys);
  417. if (port->cooked)
  418. printk(" [ADC port]\n");
  419. else
  420. printk(" [%s timer, %d %sHz clock, %d ns res]\n", TIME_NAME,
  421. port->speed > 10000 ? (port->speed + 800) / 1000 : port->speed,
  422. port->speed > 10000 ? "M" : "k",
  423. port->speed > 10000 ? (port->loop * 1000) / (port->speed / 1000)
  424. : (port->loop * 1000000) / port->speed);
  425. }
  426. /*
  427. * analog_init_devices() sets up device-specific values and registers the input devices.
  428. */
  429. static int analog_init_masks(struct analog_port *port)
  430. {
  431. int i;
  432. struct analog *analog = port->analog;
  433. int max[4];
  434. if (!port->mask)
  435. return -1;
  436. if ((port->mask & 3) != 3 && port->mask != 0xc) {
  437. printk(KERN_WARNING "analog.c: Unknown joystick device found "
  438. "(data=%#x, %s), probably not analog joystick.\n",
  439. port->mask, port->gameport->phys);
  440. return -1;
  441. }
  442. i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
  443. analog[0].mask = i & 0xfffff;
  444. analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
  445. | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
  446. | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
  447. analog[0].mask &= ~(ANALOG_HAT2_CHF)
  448. | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
  449. analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
  450. | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
  451. | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
  452. | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
  453. analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
  454. | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
  455. & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
  456. analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
  457. analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
  458. : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
  459. if (port->cooked) {
  460. for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
  461. if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
  462. if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
  463. if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
  464. if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
  465. if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
  466. gameport_calibrate(port->gameport, port->axes, max);
  467. }
  468. for (i = 0; i < 4; i++)
  469. port->initial[i] = port->axes[i];
  470. return -!(analog[0].mask || analog[1].mask);
  471. }
  472. static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
  473. {
  474. int i, t, u, v;
  475. port->gameport = gameport;
  476. gameport_set_drvdata(gameport, port);
  477. if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
  478. analog_calibrate_timer(port);
  479. gameport_trigger(gameport);
  480. t = gameport_read(gameport);
  481. msleep(ANALOG_MAX_TIME);
  482. port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
  483. port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
  484. for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
  485. if (!analog_cooked_read(port))
  486. break;
  487. msleep(ANALOG_MAX_TIME);
  488. }
  489. u = v = 0;
  490. msleep(ANALOG_MAX_TIME);
  491. t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
  492. gameport_trigger(gameport);
  493. while ((gameport_read(port->gameport) & port->mask) && (u < t))
  494. u++;
  495. udelay(ANALOG_SAITEK_DELAY);
  496. t = gameport_time(gameport, ANALOG_SAITEK_TIME);
  497. gameport_trigger(gameport);
  498. while ((gameport_read(port->gameport) & port->mask) && (v < t))
  499. v++;
  500. if (v < (u >> 1)) { /* FIXME - more than one port */
  501. analog_options[0] |= /* FIXME - more than one port */
  502. ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
  503. return 0;
  504. }
  505. gameport_close(gameport);
  506. }
  507. if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
  508. for (i = 0; i < ANALOG_INIT_RETRIES; i++)
  509. if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
  510. break;
  511. for (i = 0; i < 4; i++)
  512. if (port->axes[i] != -1)
  513. port->mask |= 1 << i;
  514. port->fuzz = gameport->fuzz;
  515. port->cooked = 1;
  516. return 0;
  517. }
  518. return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  519. }
  520. static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
  521. {
  522. struct analog_port *port;
  523. int i;
  524. int err;
  525. if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
  526. return - ENOMEM;
  527. err = analog_init_port(gameport, drv, port);
  528. if (err) {
  529. kfree(port);
  530. return err;
  531. }
  532. err = analog_init_masks(port);
  533. if (err) {
  534. gameport_close(gameport);
  535. gameport_set_drvdata(gameport, NULL);
  536. kfree(port);
  537. return err;
  538. }
  539. gameport_set_poll_handler(gameport, analog_poll);
  540. gameport_set_poll_interval(gameport, 10);
  541. for (i = 0; i < 2; i++)
  542. if (port->analog[i].mask)
  543. analog_init_device(port, port->analog + i, i);
  544. return 0;
  545. }
  546. static void analog_disconnect(struct gameport *gameport)
  547. {
  548. int i;
  549. struct analog_port *port = gameport_get_drvdata(gameport);
  550. for (i = 0; i < 2; i++)
  551. if (port->analog[i].mask)
  552. input_unregister_device(&port->analog[i].dev);
  553. gameport_close(gameport);
  554. gameport_set_drvdata(gameport, NULL);
  555. printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
  556. port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
  557. port->gameport->phys);
  558. kfree(port);
  559. }
  560. struct analog_types {
  561. char *name;
  562. int value;
  563. };
  564. static struct analog_types analog_types[] = {
  565. { "none", 0x00000000 },
  566. { "auto", 0x000000ff },
  567. { "2btn", 0x0000003f },
  568. { "y-joy", 0x0cc00033 },
  569. { "y-pad", 0x8cc80033 },
  570. { "fcs", 0x000008f7 },
  571. { "chf", 0x000002ff },
  572. { "fullchf", 0x000007ff },
  573. { "gamepad", 0x000830f3 },
  574. { "gamepad8", 0x0008f0f3 },
  575. { NULL, 0 }
  576. };
  577. static void analog_parse_options(void)
  578. {
  579. int i, j;
  580. char *end;
  581. for (i = 0; i < js_nargs; i++) {
  582. for (j = 0; analog_types[j].name; j++)
  583. if (!strcmp(analog_types[j].name, js[i])) {
  584. analog_options[i] = analog_types[j].value;
  585. break;
  586. }
  587. if (analog_types[j].name) continue;
  588. analog_options[i] = simple_strtoul(js[i], &end, 0);
  589. if (end != js[i]) continue;
  590. analog_options[i] = 0xff;
  591. if (!strlen(js[i])) continue;
  592. printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
  593. }
  594. for (; i < ANALOG_PORTS; i++)
  595. analog_options[i] = 0xff;
  596. }
  597. /*
  598. * The gameport device structure.
  599. */
  600. static struct gameport_driver analog_drv = {
  601. .driver = {
  602. .name = "analog",
  603. },
  604. .description = DRIVER_DESC,
  605. .connect = analog_connect,
  606. .disconnect = analog_disconnect,
  607. };
  608. static int __init analog_init(void)
  609. {
  610. analog_parse_options();
  611. gameport_register_driver(&analog_drv);
  612. return 0;
  613. }
  614. static void __exit analog_exit(void)
  615. {
  616. gameport_unregister_driver(&analog_drv);
  617. }
  618. module_init(analog_init);
  619. module_exit(analog_exit);