gamecon.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*
  2. * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
  3. *
  4. * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
  5. * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
  6. *
  7. * Based on the work of:
  8. * Andree Borrmann John Dahlstrom
  9. * David Kuder Nathan Hand
  10. * Raphael Assenat
  11. */
  12. /*
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * Should you need to contact me, the author, you can do so either by
  28. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  29. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/delay.h>
  33. #include <linux/module.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/init.h>
  36. #include <linux/parport.h>
  37. #include <linux/input.h>
  38. #include <linux/mutex.h>
  39. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  40. MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
  41. MODULE_LICENSE("GPL");
  42. #define GC_MAX_PORTS 3
  43. #define GC_MAX_DEVICES 5
  44. struct gc_config {
  45. int args[GC_MAX_DEVICES + 1];
  46. unsigned int nargs;
  47. };
  48. static struct gc_config gc_cfg[GC_MAX_PORTS] __initdata;
  49. module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
  50. MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
  51. module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
  52. MODULE_PARM_DESC(map2, "Describes second set of devices");
  53. module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
  54. MODULE_PARM_DESC(map3, "Describes third set of devices");
  55. /* see also gs_psx_delay parameter in PSX support section */
  56. #define GC_SNES 1
  57. #define GC_NES 2
  58. #define GC_NES4 3
  59. #define GC_MULTI 4
  60. #define GC_MULTI2 5
  61. #define GC_N64 6
  62. #define GC_PSX 7
  63. #define GC_DDR 8
  64. #define GC_SNESMOUSE 9
  65. #define GC_MAX 9
  66. #define GC_REFRESH_TIME HZ/100
  67. struct gc {
  68. struct pardevice *pd;
  69. struct input_dev *dev[GC_MAX_DEVICES];
  70. struct timer_list timer;
  71. unsigned char pads[GC_MAX + 1];
  72. int used;
  73. struct mutex mutex;
  74. char phys[GC_MAX_DEVICES][32];
  75. };
  76. static struct gc *gc_base[3];
  77. static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
  78. static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
  79. "Multisystem 2-button joystick", "N64 controller", "PSX controller",
  80. "PSX DDR controller", "SNES mouse" };
  81. /*
  82. * N64 support.
  83. */
  84. static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
  85. static short gc_n64_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START };
  86. #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
  87. #define GC_N64_REQUEST_LENGTH 37 /* transmit request sequence is 9 bits long */
  88. #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
  89. #define GC_N64_REQUEST 0x1dd1111111ULL /* the request data command (encoded for 000000011) */
  90. #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
  91. /* GC_N64_DWS > 24 is known to fail */
  92. #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
  93. #define GC_N64_POWER_R 0xfd /* power during read */
  94. #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
  95. /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
  96. /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
  97. /* than 123 us */
  98. #define GC_N64_CLOCK 0x02 /* clock bits for read */
  99. /*
  100. * gc_n64_read_packet() reads an N64 packet.
  101. * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
  102. */
  103. static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
  104. {
  105. int i;
  106. unsigned long flags;
  107. /*
  108. * Request the pad to transmit data
  109. */
  110. local_irq_save(flags);
  111. for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) {
  112. parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0));
  113. udelay(GC_N64_DWS);
  114. }
  115. local_irq_restore(flags);
  116. /*
  117. * Wait for the pad response to be loaded into the 33-bit register of the adapter
  118. */
  119. udelay(GC_N64_DELAY);
  120. /*
  121. * Grab data (ignoring the last bit, which is a stop bit)
  122. */
  123. for (i = 0; i < GC_N64_LENGTH; i++) {
  124. parport_write_data(gc->pd->port, GC_N64_POWER_R);
  125. data[i] = parport_read_status(gc->pd->port);
  126. parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
  127. }
  128. /*
  129. * We must wait 200 ms here for the controller to reinitialize before the next read request.
  130. * No worries as long as gc_read is polled less frequently than this.
  131. */
  132. }
  133. static void gc_n64_process_packet(struct gc *gc)
  134. {
  135. unsigned char data[GC_N64_LENGTH];
  136. signed char axes[2];
  137. struct input_dev *dev;
  138. int i, j, s;
  139. gc_n64_read_packet(gc, data);
  140. for (i = 0; i < GC_MAX_DEVICES; i++) {
  141. dev = gc->dev[i];
  142. if (!dev)
  143. continue;
  144. s = gc_status_bit[i];
  145. if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
  146. axes[0] = axes[1] = 0;
  147. for (j = 0; j < 8; j++) {
  148. if (data[23 - j] & s)
  149. axes[0] |= 1 << j;
  150. if (data[31 - j] & s)
  151. axes[1] |= 1 << j;
  152. }
  153. input_report_abs(dev, ABS_X, axes[0]);
  154. input_report_abs(dev, ABS_Y, -axes[1]);
  155. input_report_abs(dev, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
  156. input_report_abs(dev, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
  157. for (j = 0; j < 10; j++)
  158. input_report_key(dev, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
  159. input_sync(dev);
  160. }
  161. }
  162. }
  163. /*
  164. * NES/SNES support.
  165. */
  166. #define GC_NES_DELAY 6 /* Delay between bits - 6us */
  167. #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
  168. #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
  169. last 4 bits are unused */
  170. #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
  171. 16 bits are equivalent to a gamepad */
  172. #define GC_NES_POWER 0xfc
  173. #define GC_NES_CLOCK 0x01
  174. #define GC_NES_LATCH 0x02
  175. static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
  176. static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
  177. static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
  178. /*
  179. * gc_nes_read_packet() reads a NES/SNES packet.
  180. * Each pad uses one bit per byte. So all pads connected to
  181. * this port are read in parallel.
  182. */
  183. static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
  184. {
  185. int i;
  186. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
  187. udelay(GC_NES_DELAY * 2);
  188. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
  189. for (i = 0; i < length; i++) {
  190. udelay(GC_NES_DELAY);
  191. parport_write_data(gc->pd->port, GC_NES_POWER);
  192. data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
  193. udelay(GC_NES_DELAY);
  194. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
  195. }
  196. }
  197. static void gc_nes_process_packet(struct gc *gc)
  198. {
  199. unsigned char data[GC_SNESMOUSE_LENGTH];
  200. struct input_dev *dev;
  201. int i, j, s, len;
  202. char x_rel, y_rel;
  203. len = gc->pads[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
  204. (gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
  205. gc_nes_read_packet(gc, len, data);
  206. for (i = 0; i < GC_MAX_DEVICES; i++) {
  207. dev = gc->dev[i];
  208. if (!dev)
  209. continue;
  210. s = gc_status_bit[i];
  211. if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
  212. input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
  213. input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
  214. }
  215. if (s & gc->pads[GC_NES])
  216. for (j = 0; j < 4; j++)
  217. input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
  218. if (s & gc->pads[GC_SNES])
  219. for (j = 0; j < 8; j++)
  220. input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
  221. if (s & gc->pads[GC_SNESMOUSE]) {
  222. /*
  223. * The 4 unused bits from SNES controllers appear to be ID bits
  224. * so use them to make sure iwe are dealing with a mouse.
  225. * gamepad is connected. This is important since
  226. * my SNES gamepad sends 1's for bits 16-31, which
  227. * cause the mouse pointer to quickly move to the
  228. * upper left corner of the screen.
  229. */
  230. if (!(s & data[12]) && !(s & data[13]) &&
  231. !(s & data[14]) && (s & data[15])) {
  232. input_report_key(dev, BTN_LEFT, s & data[9]);
  233. input_report_key(dev, BTN_RIGHT, s & data[8]);
  234. x_rel = y_rel = 0;
  235. for (j = 0; j < 7; j++) {
  236. x_rel <<= 1;
  237. if (data[25 + j] & s)
  238. x_rel |= 1;
  239. y_rel <<= 1;
  240. if (data[17 + j] & s)
  241. y_rel |= 1;
  242. }
  243. if (x_rel) {
  244. if (data[24] & s)
  245. x_rel = -x_rel;
  246. input_report_rel(dev, REL_X, x_rel);
  247. }
  248. if (y_rel) {
  249. if (data[16] & s)
  250. y_rel = -y_rel;
  251. input_report_rel(dev, REL_Y, y_rel);
  252. }
  253. }
  254. }
  255. input_sync(dev);
  256. }
  257. }
  258. /*
  259. * Multisystem joystick support
  260. */
  261. #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
  262. #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
  263. /*
  264. * gc_multi_read_packet() reads a Multisystem joystick packet.
  265. */
  266. static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
  267. {
  268. int i;
  269. for (i = 0; i < length; i++) {
  270. parport_write_data(gc->pd->port, ~(1 << i));
  271. data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
  272. }
  273. }
  274. static void gc_multi_process_packet(struct gc *gc)
  275. {
  276. unsigned char data[GC_MULTI2_LENGTH];
  277. struct input_dev *dev;
  278. int i, s;
  279. gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
  280. for (i = 0; i < GC_MAX_DEVICES; i++) {
  281. dev = gc->dev[i];
  282. if (!dev)
  283. continue;
  284. s = gc_status_bit[i];
  285. if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
  286. input_report_abs(dev, ABS_X, !(s & data[2]) - !(s & data[3]));
  287. input_report_abs(dev, ABS_Y, !(s & data[0]) - !(s & data[1]));
  288. input_report_key(dev, BTN_TRIGGER, s & data[4]);
  289. }
  290. if (s & gc->pads[GC_MULTI2])
  291. input_report_key(dev, BTN_THUMB, s & data[5]);
  292. input_sync(dev);
  293. }
  294. }
  295. /*
  296. * PSX support
  297. *
  298. * See documentation at:
  299. * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
  300. * http://www.gamesx.com/controldata/psxcont/psxcont.htm
  301. * ftp://milano.usal.es/pablo/
  302. *
  303. */
  304. #define GC_PSX_DELAY 25 /* 25 usec */
  305. #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
  306. #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
  307. #define GC_PSX_MOUSE 1 /* Mouse */
  308. #define GC_PSX_NEGCON 2 /* NegCon */
  309. #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
  310. #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
  311. #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
  312. #define GC_PSX_CLOCK 0x04 /* Pin 4 */
  313. #define GC_PSX_COMMAND 0x01 /* Pin 2 */
  314. #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
  315. #define GC_PSX_SELECT 0x02 /* Pin 3 */
  316. #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
  317. #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
  318. static int gc_psx_delay = GC_PSX_DELAY;
  319. module_param_named(psx_delay, gc_psx_delay, uint, 0);
  320. MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
  321. static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
  322. static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
  323. BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
  324. static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
  325. /*
  326. * gc_psx_command() writes 8bit command and reads 8bit data from
  327. * the psx pad.
  328. */
  329. static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES])
  330. {
  331. int i, j, cmd, read;
  332. for (i = 0; i < GC_MAX_DEVICES; i++)
  333. data[i] = 0;
  334. for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
  335. cmd = (b & 1) ? GC_PSX_COMMAND : 0;
  336. parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
  337. udelay(gc_psx_delay);
  338. read = parport_read_status(gc->pd->port) ^ 0x80;
  339. for (j = 0; j < GC_MAX_DEVICES; j++)
  340. data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
  341. parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
  342. udelay(gc_psx_delay);
  343. }
  344. }
  345. /*
  346. * gc_psx_read_packet() reads a whole psx packet and returns
  347. * device identifier code.
  348. */
  349. static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
  350. unsigned char id[GC_MAX_DEVICES])
  351. {
  352. int i, j, max_len = 0;
  353. unsigned long flags;
  354. unsigned char data2[GC_MAX_DEVICES];
  355. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
  356. udelay(gc_psx_delay);
  357. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
  358. udelay(gc_psx_delay);
  359. local_irq_save(flags);
  360. gc_psx_command(gc, 0x01, data2); /* Access pad */
  361. gc_psx_command(gc, 0x42, id); /* Get device ids */
  362. gc_psx_command(gc, 0, data2); /* Dump status */
  363. for (i =0; i < GC_MAX_DEVICES; i++) /* Find the longest pad */
  364. if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
  365. && (GC_PSX_LEN(id[i]) > max_len)
  366. && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
  367. max_len = GC_PSX_LEN(id[i]);
  368. for (i = 0; i < max_len; i++) { /* Read in all the data */
  369. gc_psx_command(gc, 0, data2);
  370. for (j = 0; j < GC_MAX_DEVICES; j++)
  371. data[j][i] = data2[j];
  372. }
  373. local_irq_restore(flags);
  374. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
  375. for(i = 0; i < GC_MAX_DEVICES; i++) /* Set id's to the real value */
  376. id[i] = GC_PSX_ID(id[i]);
  377. }
  378. static void gc_psx_process_packet(struct gc *gc)
  379. {
  380. unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
  381. unsigned char id[GC_MAX_DEVICES];
  382. struct input_dev *dev;
  383. int i, j;
  384. gc_psx_read_packet(gc, data, id);
  385. for (i = 0; i < GC_MAX_DEVICES; i++) {
  386. dev = gc->dev[i];
  387. if (!dev)
  388. continue;
  389. switch (id[i]) {
  390. case GC_PSX_RUMBLE:
  391. input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04);
  392. input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02);
  393. case GC_PSX_NEGCON:
  394. case GC_PSX_ANALOG:
  395. if (gc->pads[GC_DDR] & gc_status_bit[i]) {
  396. for(j = 0; j < 4; j++)
  397. input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
  398. } else {
  399. for (j = 0; j < 4; j++)
  400. input_report_abs(dev, gc_psx_abs[j + 2], data[i][j + 2]);
  401. input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
  402. input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
  403. }
  404. for (j = 0; j < 8; j++)
  405. input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
  406. input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
  407. input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
  408. input_sync(dev);
  409. break;
  410. case GC_PSX_NORMAL:
  411. if (gc->pads[GC_DDR] & gc_status_bit[i]) {
  412. for(j = 0; j < 4; j++)
  413. input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
  414. } else {
  415. input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
  416. input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
  417. /* for some reason if the extra axes are left unset they drift */
  418. /* for (j = 0; j < 4; j++)
  419. input_report_abs(dev, gc_psx_abs[j + 2], 128);
  420. * This needs to be debugged properly,
  421. * maybe fuzz processing needs to be done in input_sync()
  422. * --vojtech
  423. */
  424. }
  425. for (j = 0; j < 8; j++)
  426. input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
  427. input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
  428. input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
  429. input_sync(dev);
  430. break;
  431. case 0: /* not a pad, ignore */
  432. break;
  433. }
  434. }
  435. }
  436. /*
  437. * gc_timer() initiates reads of console pads data.
  438. */
  439. static void gc_timer(unsigned long private)
  440. {
  441. struct gc *gc = (void *) private;
  442. /*
  443. * N64 pads - must be read first, any read confuses them for 200 us
  444. */
  445. if (gc->pads[GC_N64])
  446. gc_n64_process_packet(gc);
  447. /*
  448. * NES and SNES pads or mouse
  449. */
  450. if (gc->pads[GC_NES] || gc->pads[GC_SNES] || gc->pads[GC_SNESMOUSE])
  451. gc_nes_process_packet(gc);
  452. /*
  453. * Multi and Multi2 joysticks
  454. */
  455. if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
  456. gc_multi_process_packet(gc);
  457. /*
  458. * PSX controllers
  459. */
  460. if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
  461. gc_psx_process_packet(gc);
  462. mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
  463. }
  464. static int gc_open(struct input_dev *dev)
  465. {
  466. struct gc *gc = input_get_drvdata(dev);
  467. int err;
  468. err = mutex_lock_interruptible(&gc->mutex);
  469. if (err)
  470. return err;
  471. if (!gc->used++) {
  472. parport_claim(gc->pd);
  473. parport_write_control(gc->pd->port, 0x04);
  474. mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
  475. }
  476. mutex_unlock(&gc->mutex);
  477. return 0;
  478. }
  479. static void gc_close(struct input_dev *dev)
  480. {
  481. struct gc *gc = input_get_drvdata(dev);
  482. mutex_lock(&gc->mutex);
  483. if (!--gc->used) {
  484. del_timer_sync(&gc->timer);
  485. parport_write_control(gc->pd->port, 0x00);
  486. parport_release(gc->pd);
  487. }
  488. mutex_unlock(&gc->mutex);
  489. }
  490. static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
  491. {
  492. struct input_dev *input_dev;
  493. int i;
  494. if (!pad_type)
  495. return 0;
  496. if (pad_type < 1 || pad_type > GC_MAX) {
  497. printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
  498. return -EINVAL;
  499. }
  500. gc->dev[idx] = input_dev = input_allocate_device();
  501. if (!input_dev) {
  502. printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
  503. return -ENOMEM;
  504. }
  505. input_dev->name = gc_names[pad_type];
  506. input_dev->phys = gc->phys[idx];
  507. input_dev->id.bustype = BUS_PARPORT;
  508. input_dev->id.vendor = 0x0001;
  509. input_dev->id.product = pad_type;
  510. input_dev->id.version = 0x0100;
  511. input_set_drvdata(input_dev, gc);
  512. input_dev->open = gc_open;
  513. input_dev->close = gc_close;
  514. if (pad_type != GC_SNESMOUSE) {
  515. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  516. for (i = 0; i < 2; i++)
  517. input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
  518. } else
  519. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  520. gc->pads[0] |= gc_status_bit[idx];
  521. gc->pads[pad_type] |= gc_status_bit[idx];
  522. switch (pad_type) {
  523. case GC_N64:
  524. for (i = 0; i < 10; i++)
  525. set_bit(gc_n64_btn[i], input_dev->keybit);
  526. for (i = 0; i < 2; i++) {
  527. input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
  528. input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
  529. }
  530. break;
  531. case GC_SNESMOUSE:
  532. set_bit(BTN_LEFT, input_dev->keybit);
  533. set_bit(BTN_RIGHT, input_dev->keybit);
  534. set_bit(REL_X, input_dev->relbit);
  535. set_bit(REL_Y, input_dev->relbit);
  536. break;
  537. case GC_SNES:
  538. for (i = 4; i < 8; i++)
  539. set_bit(gc_snes_btn[i], input_dev->keybit);
  540. case GC_NES:
  541. for (i = 0; i < 4; i++)
  542. set_bit(gc_snes_btn[i], input_dev->keybit);
  543. break;
  544. case GC_MULTI2:
  545. set_bit(BTN_THUMB, input_dev->keybit);
  546. case GC_MULTI:
  547. set_bit(BTN_TRIGGER, input_dev->keybit);
  548. break;
  549. case GC_PSX:
  550. for (i = 0; i < 6; i++)
  551. input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2);
  552. for (i = 0; i < 12; i++)
  553. set_bit(gc_psx_btn[i], input_dev->keybit);
  554. break;
  555. case GC_DDR:
  556. for (i = 0; i < 4; i++)
  557. set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
  558. for (i = 0; i < 12; i++)
  559. set_bit(gc_psx_btn[i], input_dev->keybit);
  560. break;
  561. }
  562. return 0;
  563. }
  564. static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
  565. {
  566. struct gc *gc;
  567. struct parport *pp;
  568. struct pardevice *pd;
  569. int i;
  570. int err;
  571. pp = parport_find_number(parport);
  572. if (!pp) {
  573. printk(KERN_ERR "gamecon.c: no such parport\n");
  574. err = -EINVAL;
  575. goto err_out;
  576. }
  577. pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
  578. if (!pd) {
  579. printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
  580. err = -EBUSY;
  581. goto err_put_pp;
  582. }
  583. gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
  584. if (!gc) {
  585. printk(KERN_ERR "gamecon.c: Not enough memory\n");
  586. err = -ENOMEM;
  587. goto err_unreg_pardev;
  588. }
  589. mutex_init(&gc->mutex);
  590. gc->pd = pd;
  591. init_timer(&gc->timer);
  592. gc->timer.data = (long) gc;
  593. gc->timer.function = gc_timer;
  594. for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
  595. if (!pads[i])
  596. continue;
  597. snprintf(gc->phys[i], sizeof(gc->phys[i]),
  598. "%s/input%d", gc->pd->port->name, i);
  599. err = gc_setup_pad(gc, i, pads[i]);
  600. if (err)
  601. goto err_unreg_devs;
  602. err = input_register_device(gc->dev[i]);
  603. if (err)
  604. goto err_free_dev;
  605. }
  606. if (!gc->pads[0]) {
  607. printk(KERN_ERR "gamecon.c: No valid devices specified\n");
  608. err = -EINVAL;
  609. goto err_free_gc;
  610. }
  611. parport_put_port(pp);
  612. return gc;
  613. err_free_dev:
  614. input_free_device(gc->dev[i]);
  615. err_unreg_devs:
  616. while (--i >= 0)
  617. if (gc->dev[i])
  618. input_unregister_device(gc->dev[i]);
  619. err_free_gc:
  620. kfree(gc);
  621. err_unreg_pardev:
  622. parport_unregister_device(pd);
  623. err_put_pp:
  624. parport_put_port(pp);
  625. err_out:
  626. return ERR_PTR(err);
  627. }
  628. static void gc_remove(struct gc *gc)
  629. {
  630. int i;
  631. for (i = 0; i < GC_MAX_DEVICES; i++)
  632. if (gc->dev[i])
  633. input_unregister_device(gc->dev[i]);
  634. parport_unregister_device(gc->pd);
  635. kfree(gc);
  636. }
  637. static int __init gc_init(void)
  638. {
  639. int i;
  640. int have_dev = 0;
  641. int err = 0;
  642. for (i = 0; i < GC_MAX_PORTS; i++) {
  643. if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
  644. continue;
  645. if (gc_cfg[i].nargs < 2) {
  646. printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
  647. err = -EINVAL;
  648. break;
  649. }
  650. gc_base[i] = gc_probe(gc_cfg[i].args[0],
  651. gc_cfg[i].args + 1, gc_cfg[i].nargs - 1);
  652. if (IS_ERR(gc_base[i])) {
  653. err = PTR_ERR(gc_base[i]);
  654. break;
  655. }
  656. have_dev = 1;
  657. }
  658. if (err) {
  659. while (--i >= 0)
  660. if (gc_base[i])
  661. gc_remove(gc_base[i]);
  662. return err;
  663. }
  664. return have_dev ? 0 : -ENODEV;
  665. }
  666. static void __exit gc_exit(void)
  667. {
  668. int i;
  669. for (i = 0; i < GC_MAX_PORTS; i++)
  670. if (gc_base[i])
  671. gc_remove(gc_base[i]);
  672. }
  673. module_init(gc_init);
  674. module_exit(gc_exit);