gamecon.c 22 KB

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