gamecon.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. */
  11. /*
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * Should you need to contact me, the author, you can do so either by
  27. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  28. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/delay.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/init.h>
  35. #include <linux/parport.h>
  36. #include <linux/input.h>
  37. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  38. MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
  39. MODULE_LICENSE("GPL");
  40. #define GC_MAX_PORTS 3
  41. #define GC_MAX_DEVICES 5
  42. struct gc_config {
  43. int args[GC_MAX_DEVICES + 1];
  44. int nargs;
  45. };
  46. static struct gc_config gc[GC_MAX_PORTS] __initdata;
  47. module_param_array_named(map, gc[0].args, int, &gc[0].nargs, 0);
  48. MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
  49. module_param_array_named(map2, gc[1].args, int, &gc[1].nargs, 0);
  50. MODULE_PARM_DESC(map2, "Describes second set of devices");
  51. module_param_array_named(map3, gc[2].args, int, &gc[2].nargs, 0);
  52. MODULE_PARM_DESC(map3, "Describes third set of devices");
  53. __obsolete_setup("gc=");
  54. __obsolete_setup("gc_2=");
  55. __obsolete_setup("gc_3=");
  56. /* see also gs_psx_delay parameter in PSX support section */
  57. #define GC_SNES 1
  58. #define GC_NES 2
  59. #define GC_NES4 3
  60. #define GC_MULTI 4
  61. #define GC_MULTI2 5
  62. #define GC_N64 6
  63. #define GC_PSX 7
  64. #define GC_DDR 8
  65. #define GC_MAX 8
  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 semaphore sem;
  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" };
  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 last 4 bits are unused */
  169. #define GC_NES_POWER 0xfc
  170. #define GC_NES_CLOCK 0x01
  171. #define GC_NES_LATCH 0x02
  172. static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
  173. static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
  174. static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
  175. /*
  176. * gc_nes_read_packet() reads a NES/SNES packet.
  177. * Each pad uses one bit per byte. So all pads connected to
  178. * this port are read in parallel.
  179. */
  180. static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
  181. {
  182. int i;
  183. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
  184. udelay(GC_NES_DELAY * 2);
  185. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
  186. for (i = 0; i < length; i++) {
  187. udelay(GC_NES_DELAY);
  188. parport_write_data(gc->pd->port, GC_NES_POWER);
  189. data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
  190. udelay(GC_NES_DELAY);
  191. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
  192. }
  193. }
  194. static void gc_nes_process_packet(struct gc *gc)
  195. {
  196. unsigned char data[GC_SNES_LENGTH];
  197. struct input_dev *dev;
  198. int i, j, s;
  199. gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
  200. for (i = 0; i < GC_MAX_DEVICES; i++) {
  201. dev = gc->dev[i];
  202. if (!dev)
  203. continue;
  204. s = gc_status_bit[i];
  205. if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
  206. input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
  207. input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
  208. }
  209. if (s & gc->pads[GC_NES])
  210. for (j = 0; j < 4; j++)
  211. input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
  212. if (s & gc->pads[GC_SNES])
  213. for (j = 0; j < 8; j++)
  214. input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
  215. input_sync(dev);
  216. }
  217. }
  218. /*
  219. * Multisystem joystick support
  220. */
  221. #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
  222. #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
  223. /*
  224. * gc_multi_read_packet() reads a Multisystem joystick packet.
  225. */
  226. static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
  227. {
  228. int i;
  229. for (i = 0; i < length; i++) {
  230. parport_write_data(gc->pd->port, ~(1 << i));
  231. data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
  232. }
  233. }
  234. static void gc_multi_process_packet(struct gc *gc)
  235. {
  236. unsigned char data[GC_MULTI2_LENGTH];
  237. struct input_dev *dev;
  238. int i, s;
  239. gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
  240. for (i = 0; i < GC_MAX_DEVICES; i++) {
  241. dev = gc->dev[i];
  242. if (!dev)
  243. continue;
  244. s = gc_status_bit[i];
  245. if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
  246. input_report_abs(dev, ABS_X, !(s & data[2]) - !(s & data[3]));
  247. input_report_abs(dev, ABS_Y, !(s & data[0]) - !(s & data[1]));
  248. input_report_key(dev, BTN_TRIGGER, s & data[4]);
  249. }
  250. if (s & gc->pads[GC_MULTI2])
  251. input_report_key(dev, BTN_THUMB, s & data[5]);
  252. input_sync(dev);
  253. }
  254. }
  255. /*
  256. * PSX support
  257. *
  258. * See documentation at:
  259. * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
  260. * http://www.gamesx.com/controldata/psxcont/psxcont.htm
  261. * ftp://milano.usal.es/pablo/
  262. *
  263. */
  264. #define GC_PSX_DELAY 25 /* 25 usec */
  265. #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
  266. #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
  267. #define GC_PSX_MOUSE 1 /* Mouse */
  268. #define GC_PSX_NEGCON 2 /* NegCon */
  269. #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
  270. #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
  271. #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
  272. #define GC_PSX_CLOCK 0x04 /* Pin 4 */
  273. #define GC_PSX_COMMAND 0x01 /* Pin 2 */
  274. #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
  275. #define GC_PSX_SELECT 0x02 /* Pin 3 */
  276. #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
  277. #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
  278. static int gc_psx_delay = GC_PSX_DELAY;
  279. module_param_named(psx_delay, gc_psx_delay, uint, 0);
  280. MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
  281. __obsolete_setup("gc_psx_delay=");
  282. static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
  283. static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
  284. BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
  285. static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
  286. /*
  287. * gc_psx_command() writes 8bit command and reads 8bit data from
  288. * the psx pad.
  289. */
  290. static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES])
  291. {
  292. int i, j, cmd, read;
  293. for (i = 0; i < GC_MAX_DEVICES; i++)
  294. data[i] = 0;
  295. for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
  296. cmd = (b & 1) ? GC_PSX_COMMAND : 0;
  297. parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
  298. udelay(gc_psx_delay);
  299. read = parport_read_status(gc->pd->port) ^ 0x80;
  300. for (j = 0; j < GC_MAX_DEVICES; j++)
  301. data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
  302. parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
  303. udelay(gc_psx_delay);
  304. }
  305. }
  306. /*
  307. * gc_psx_read_packet() reads a whole psx packet and returns
  308. * device identifier code.
  309. */
  310. static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
  311. unsigned char id[GC_MAX_DEVICES])
  312. {
  313. int i, j, max_len = 0;
  314. unsigned long flags;
  315. unsigned char data2[GC_MAX_DEVICES];
  316. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
  317. udelay(gc_psx_delay);
  318. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
  319. udelay(gc_psx_delay);
  320. local_irq_save(flags);
  321. gc_psx_command(gc, 0x01, data2); /* Access pad */
  322. gc_psx_command(gc, 0x42, id); /* Get device ids */
  323. gc_psx_command(gc, 0, data2); /* Dump status */
  324. for (i =0; i < GC_MAX_DEVICES; i++) /* Find the longest pad */
  325. if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
  326. && (GC_PSX_LEN(id[i]) > max_len)
  327. && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
  328. max_len = GC_PSX_LEN(id[i]);
  329. for (i = 0; i < max_len; i++) { /* Read in all the data */
  330. gc_psx_command(gc, 0, data2);
  331. for (j = 0; j < GC_MAX_DEVICES; j++)
  332. data[j][i] = data2[j];
  333. }
  334. local_irq_restore(flags);
  335. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
  336. for(i = 0; i < GC_MAX_DEVICES; i++) /* Set id's to the real value */
  337. id[i] = GC_PSX_ID(id[i]);
  338. }
  339. static void gc_psx_process_packet(struct gc *gc)
  340. {
  341. unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
  342. unsigned char id[GC_MAX_DEVICES];
  343. struct input_dev *dev;
  344. int i, j;
  345. gc_psx_read_packet(gc, data, id);
  346. for (i = 0; i < GC_MAX_DEVICES; i++) {
  347. dev = gc->dev[i];
  348. if (!dev)
  349. continue;
  350. switch (id[i]) {
  351. case GC_PSX_RUMBLE:
  352. input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04);
  353. input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02);
  354. case GC_PSX_NEGCON:
  355. case GC_PSX_ANALOG:
  356. if (gc->pads[GC_DDR] & gc_status_bit[i]) {
  357. for(j = 0; j < 4; j++)
  358. input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
  359. } else {
  360. for (j = 0; j < 4; j++)
  361. input_report_abs(dev, gc_psx_abs[j + 2], data[i][j + 2]);
  362. input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
  363. input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
  364. }
  365. for (j = 0; j < 8; j++)
  366. input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
  367. input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
  368. input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
  369. input_sync(dev);
  370. break;
  371. case GC_PSX_NORMAL:
  372. if (gc->pads[GC_DDR] & gc_status_bit[i]) {
  373. for(j = 0; j < 4; j++)
  374. input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
  375. } else {
  376. input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
  377. input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
  378. /* for some reason if the extra axes are left unset they drift */
  379. /* for (j = 0; j < 4; j++)
  380. input_report_abs(dev, gc_psx_abs[j + 2], 128);
  381. * This needs to be debugged properly,
  382. * maybe fuzz processing needs to be done in input_sync()
  383. * --vojtech
  384. */
  385. }
  386. for (j = 0; j < 8; j++)
  387. input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
  388. input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
  389. input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
  390. input_sync(dev);
  391. break;
  392. case 0: /* not a pad, ignore */
  393. break;
  394. }
  395. }
  396. }
  397. /*
  398. * gc_timer() initiates reads of console pads data.
  399. */
  400. static void gc_timer(unsigned long private)
  401. {
  402. struct gc *gc = (void *) private;
  403. /*
  404. * N64 pads - must be read first, any read confuses them for 200 us
  405. */
  406. if (gc->pads[GC_N64])
  407. gc_n64_process_packet(gc);
  408. /*
  409. * NES and SNES pads
  410. */
  411. if (gc->pads[GC_NES] || gc->pads[GC_SNES])
  412. gc_nes_process_packet(gc);
  413. /*
  414. * Multi and Multi2 joysticks
  415. */
  416. if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
  417. gc_multi_process_packet(gc);
  418. /*
  419. * PSX controllers
  420. */
  421. if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
  422. gc_psx_process_packet(gc);
  423. mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
  424. }
  425. static int gc_open(struct input_dev *dev)
  426. {
  427. struct gc *gc = dev->private;
  428. int err;
  429. err = down_interruptible(&gc->sem);
  430. if (err)
  431. return err;
  432. if (!gc->used++) {
  433. parport_claim(gc->pd);
  434. parport_write_control(gc->pd->port, 0x04);
  435. mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
  436. }
  437. up(&gc->sem);
  438. return 0;
  439. }
  440. static void gc_close(struct input_dev *dev)
  441. {
  442. struct gc *gc = dev->private;
  443. down(&gc->sem);
  444. if (!--gc->used) {
  445. del_timer_sync(&gc->timer);
  446. parport_write_control(gc->pd->port, 0x00);
  447. parport_release(gc->pd);
  448. }
  449. up(&gc->sem);
  450. }
  451. static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
  452. {
  453. struct input_dev *input_dev;
  454. int i;
  455. if (!pad_type)
  456. return 0;
  457. if (pad_type < 1 || pad_type > GC_MAX) {
  458. printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
  459. return -EINVAL;
  460. }
  461. gc->dev[idx] = input_dev = input_allocate_device();
  462. if (!input_dev) {
  463. printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
  464. return -ENOMEM;
  465. }
  466. input_dev->name = gc_names[pad_type];
  467. input_dev->phys = gc->phys[idx];
  468. input_dev->id.bustype = BUS_PARPORT;
  469. input_dev->id.vendor = 0x0001;
  470. input_dev->id.product = pad_type;
  471. input_dev->id.version = 0x0100;
  472. input_dev->private = gc;
  473. input_dev->open = gc_open;
  474. input_dev->close = gc_close;
  475. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  476. for (i = 0; i < 2; i++)
  477. input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
  478. gc->pads[0] |= gc_status_bit[idx];
  479. gc->pads[pad_type] |= gc_status_bit[idx];
  480. switch (pad_type) {
  481. case GC_N64:
  482. for (i = 0; i < 10; i++)
  483. set_bit(gc_n64_btn[i], input_dev->keybit);
  484. for (i = 0; i < 2; i++) {
  485. input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
  486. input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
  487. }
  488. break;
  489. case GC_SNES:
  490. for (i = 4; i < 8; i++)
  491. set_bit(gc_snes_btn[i], input_dev->keybit);
  492. case GC_NES:
  493. for (i = 0; i < 4; i++)
  494. set_bit(gc_snes_btn[i], input_dev->keybit);
  495. break;
  496. case GC_MULTI2:
  497. set_bit(BTN_THUMB, input_dev->keybit);
  498. case GC_MULTI:
  499. set_bit(BTN_TRIGGER, input_dev->keybit);
  500. break;
  501. case GC_PSX:
  502. for (i = 0; i < 6; i++)
  503. input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2);
  504. for (i = 0; i < 12; i++)
  505. set_bit(gc_psx_btn[i], input_dev->keybit);
  506. break;
  507. case GC_DDR:
  508. for (i = 0; i < 4; i++)
  509. set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
  510. for (i = 0; i < 12; i++)
  511. set_bit(gc_psx_btn[i], input_dev->keybit);
  512. break;
  513. }
  514. return 0;
  515. }
  516. static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
  517. {
  518. struct gc *gc;
  519. struct parport *pp;
  520. struct pardevice *pd;
  521. int i;
  522. int err;
  523. pp = parport_find_number(parport);
  524. if (!pp) {
  525. printk(KERN_ERR "gamecon.c: no such parport\n");
  526. err = -EINVAL;
  527. goto err_out;
  528. }
  529. pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
  530. if (!pd) {
  531. printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
  532. err = -EBUSY;
  533. goto err_put_pp;
  534. }
  535. gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
  536. if (!gc) {
  537. printk(KERN_ERR "gamecon.c: Not enough memory\n");
  538. err = -ENOMEM;
  539. goto err_unreg_pardev;
  540. }
  541. init_MUTEX(&gc->sem);
  542. gc->pd = pd;
  543. init_timer(&gc->timer);
  544. gc->timer.data = (long) gc;
  545. gc->timer.function = gc_timer;
  546. for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
  547. if (!pads[i])
  548. continue;
  549. sprintf(gc->phys[i], "%s/input%d", gc->pd->port->name, i);
  550. err = gc_setup_pad(gc, i, pads[i]);
  551. if (err)
  552. goto err_unreg_devs;
  553. err = input_register_device(gc->dev[i]);
  554. if (err)
  555. goto err_free_dev;
  556. }
  557. if (!gc->pads[0]) {
  558. printk(KERN_ERR "gamecon.c: No valid devices specified\n");
  559. err = -EINVAL;
  560. goto err_free_gc;
  561. }
  562. parport_put_port(pp);
  563. return gc;
  564. err_free_dev:
  565. input_free_device(gc->dev[i]);
  566. err_unreg_devs:
  567. while (--i >= 0)
  568. if (gc->dev[i])
  569. input_unregister_device(gc->dev[i]);
  570. err_free_gc:
  571. kfree(gc);
  572. err_unreg_pardev:
  573. parport_unregister_device(pd);
  574. err_put_pp:
  575. parport_put_port(pp);
  576. err_out:
  577. return ERR_PTR(err);
  578. }
  579. static void gc_remove(struct gc *gc)
  580. {
  581. int i;
  582. for (i = 0; i < GC_MAX_DEVICES; i++)
  583. if (gc->dev[i])
  584. input_unregister_device(gc->dev[i]);
  585. parport_unregister_device(gc->pd);
  586. kfree(gc);
  587. }
  588. static int __init gc_init(void)
  589. {
  590. int i;
  591. int have_dev = 0;
  592. int err = 0;
  593. for (i = 0; i < GC_MAX_PORTS; i++) {
  594. if (gc[i].nargs == 0 || gc[i].args[0] < 0)
  595. continue;
  596. if (gc[i].nargs < 2) {
  597. printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
  598. err = -EINVAL;
  599. break;
  600. }
  601. gc_base[i] = gc_probe(gc[i].args[0], gc[i].args + 1, gc[i].nargs - 1);
  602. if (IS_ERR(gc_base[i])) {
  603. err = PTR_ERR(gc_base[i]);
  604. break;
  605. }
  606. have_dev = 1;
  607. }
  608. if (err) {
  609. while (--i >= 0)
  610. if (gc_base[i])
  611. gc_remove(gc_base[i]);
  612. return err;
  613. }
  614. return have_dev ? 0 : -ENODEV;
  615. }
  616. static void __exit gc_exit(void)
  617. {
  618. int i;
  619. for (i = 0; i < GC_MAX_PORTS; i++)
  620. if (gc_base[i])
  621. gc_remove(gc_base[i]);
  622. }
  623. module_init(gc_init);
  624. module_exit(gc_exit);