gamecon.c 20 KB

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