gamecon.c 22 KB

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