analog.c 18 KB

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