gamecon.c 19 KB

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