gamecon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. struct semaphore sem;
  73. char phys[5][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" };
  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. /*
  133. * NES/SNES support.
  134. */
  135. #define GC_NES_DELAY 6 /* Delay between bits - 6us */
  136. #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
  137. #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the last 4 bits are unused */
  138. #define GC_NES_POWER 0xfc
  139. #define GC_NES_CLOCK 0x01
  140. #define GC_NES_LATCH 0x02
  141. static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
  142. static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
  143. static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
  144. /*
  145. * gc_nes_read_packet() reads a NES/SNES packet.
  146. * Each pad uses one bit per byte. So all pads connected to
  147. * this port are read in parallel.
  148. */
  149. static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
  150. {
  151. int i;
  152. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
  153. udelay(GC_NES_DELAY * 2);
  154. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
  155. for (i = 0; i < length; i++) {
  156. udelay(GC_NES_DELAY);
  157. parport_write_data(gc->pd->port, GC_NES_POWER);
  158. data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
  159. udelay(GC_NES_DELAY);
  160. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
  161. }
  162. }
  163. /*
  164. * Multisystem joystick support
  165. */
  166. #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
  167. #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
  168. /*
  169. * gc_multi_read_packet() reads a Multisystem joystick packet.
  170. */
  171. static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
  172. {
  173. int i;
  174. for (i = 0; i < length; i++) {
  175. parport_write_data(gc->pd->port, ~(1 << i));
  176. data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
  177. }
  178. }
  179. /*
  180. * PSX support
  181. *
  182. * See documentation at:
  183. * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
  184. * http://www.gamesx.com/controldata/psxcont/psxcont.htm
  185. * ftp://milano.usal.es/pablo/
  186. *
  187. */
  188. #define GC_PSX_DELAY 25 /* 25 usec */
  189. #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
  190. #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
  191. #define GC_PSX_MOUSE 1 /* Mouse */
  192. #define GC_PSX_NEGCON 2 /* NegCon */
  193. #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
  194. #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
  195. #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
  196. #define GC_PSX_CLOCK 0x04 /* Pin 4 */
  197. #define GC_PSX_COMMAND 0x01 /* Pin 2 */
  198. #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
  199. #define GC_PSX_SELECT 0x02 /* Pin 3 */
  200. #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
  201. #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
  202. static int gc_psx_delay = GC_PSX_DELAY;
  203. module_param_named(psx_delay, gc_psx_delay, uint, 0);
  204. MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
  205. __obsolete_setup("gc_psx_delay=");
  206. static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
  207. static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
  208. BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
  209. static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
  210. /*
  211. * gc_psx_command() writes 8bit command and reads 8bit data from
  212. * the psx pad.
  213. */
  214. static void gc_psx_command(struct gc *gc, int b, unsigned char data[5])
  215. {
  216. int i, j, cmd, read;
  217. for (i = 0; i < 5; i++)
  218. data[i] = 0;
  219. for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
  220. cmd = (b & 1) ? GC_PSX_COMMAND : 0;
  221. parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
  222. udelay(gc_psx_delay);
  223. read = parport_read_status(gc->pd->port) ^ 0x80;
  224. for (j = 0; j < 5; j++)
  225. data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
  226. parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
  227. udelay(gc_psx_delay);
  228. }
  229. }
  230. /*
  231. * gc_psx_read_packet() reads a whole psx packet and returns
  232. * device identifier code.
  233. */
  234. static void gc_psx_read_packet(struct gc *gc, unsigned char data[5][GC_PSX_BYTES], unsigned char id[5])
  235. {
  236. int i, j, max_len = 0;
  237. unsigned long flags;
  238. unsigned char data2[5];
  239. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
  240. udelay(gc_psx_delay);
  241. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
  242. udelay(gc_psx_delay);
  243. local_irq_save(flags);
  244. gc_psx_command(gc, 0x01, data2); /* Access pad */
  245. gc_psx_command(gc, 0x42, id); /* Get device ids */
  246. gc_psx_command(gc, 0, data2); /* Dump status */
  247. for (i =0; i < 5; i++) /* Find the longest pad */
  248. if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
  249. && (GC_PSX_LEN(id[i]) > max_len)
  250. && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
  251. max_len = GC_PSX_LEN(id[i]);
  252. for (i = 0; i < max_len; i++) { /* Read in all the data */
  253. gc_psx_command(gc, 0, data2);
  254. for (j = 0; j < 5; j++)
  255. data[j][i] = data2[j];
  256. }
  257. local_irq_restore(flags);
  258. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
  259. for(i = 0; i < 5; i++) /* Set id's to the real value */
  260. id[i] = GC_PSX_ID(id[i]);
  261. }
  262. /*
  263. * gc_timer() reads and analyzes console pads data.
  264. */
  265. #define GC_MAX_LENGTH GC_N64_LENGTH
  266. static void gc_timer(unsigned long private)
  267. {
  268. struct gc *gc = (void *) private;
  269. struct input_dev *dev = gc->dev;
  270. unsigned char data[GC_MAX_LENGTH];
  271. unsigned char data_psx[5][GC_PSX_BYTES];
  272. int i, j, s;
  273. /*
  274. * N64 pads - must be read first, any read confuses them for 200 us
  275. */
  276. if (gc->pads[GC_N64]) {
  277. gc_n64_read_packet(gc, data);
  278. for (i = 0; i < 5; i++) {
  279. s = gc_status_bit[i];
  280. if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
  281. signed char axes[2];
  282. axes[0] = axes[1] = 0;
  283. for (j = 0; j < 8; j++) {
  284. if (data[23 - j] & s) axes[0] |= 1 << j;
  285. if (data[31 - j] & s) axes[1] |= 1 << j;
  286. }
  287. input_report_abs(dev + i, ABS_X, axes[0]);
  288. input_report_abs(dev + i, ABS_Y, -axes[1]);
  289. input_report_abs(dev + i, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
  290. input_report_abs(dev + i, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
  291. for (j = 0; j < 10; j++)
  292. input_report_key(dev + i, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
  293. input_sync(dev + i);
  294. }
  295. }
  296. }
  297. /*
  298. * NES and SNES pads
  299. */
  300. if (gc->pads[GC_NES] || gc->pads[GC_SNES]) {
  301. gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
  302. for (i = 0; i < 5; i++) {
  303. s = gc_status_bit[i];
  304. if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
  305. input_report_abs(dev + i, ABS_X, !(s & data[6]) - !(s & data[7]));
  306. input_report_abs(dev + i, ABS_Y, !(s & data[4]) - !(s & data[5]));
  307. }
  308. if (s & gc->pads[GC_NES])
  309. for (j = 0; j < 4; j++)
  310. input_report_key(dev + i, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
  311. if (s & gc->pads[GC_SNES])
  312. for (j = 0; j < 8; j++)
  313. input_report_key(dev + i, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
  314. input_sync(dev + i);
  315. }
  316. }
  317. /*
  318. * Multi and Multi2 joysticks
  319. */
  320. if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2]) {
  321. gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
  322. for (i = 0; i < 5; i++) {
  323. s = gc_status_bit[i];
  324. if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
  325. input_report_abs(dev + i, ABS_X, !(s & data[2]) - !(s & data[3]));
  326. input_report_abs(dev + i, ABS_Y, !(s & data[0]) - !(s & data[1]));
  327. input_report_key(dev + i, BTN_TRIGGER, s & data[4]);
  328. }
  329. if (s & gc->pads[GC_MULTI2])
  330. input_report_key(dev + i, BTN_THUMB, s & data[5]);
  331. input_sync(dev + i);
  332. }
  333. }
  334. /*
  335. * PSX controllers
  336. */
  337. if (gc->pads[GC_PSX] || gc->pads[GC_DDR]) {
  338. gc_psx_read_packet(gc, data_psx, data);
  339. for (i = 0; i < 5; i++) {
  340. switch (data[i]) {
  341. case GC_PSX_RUMBLE:
  342. input_report_key(dev + i, BTN_THUMBL, ~data_psx[i][0] & 0x04);
  343. input_report_key(dev + i, BTN_THUMBR, ~data_psx[i][0] & 0x02);
  344. case GC_PSX_NEGCON:
  345. case GC_PSX_ANALOG:
  346. if(gc->pads[GC_DDR] & gc_status_bit[i]) {
  347. for(j = 0; j < 4; j++)
  348. input_report_key(dev + i, gc_psx_ddr_btn[j], ~data_psx[i][0] & (0x10 << j));
  349. } else {
  350. for (j = 0; j < 4; j++)
  351. input_report_abs(dev + i, gc_psx_abs[j+2], data_psx[i][j + 2]);
  352. input_report_abs(dev + i, ABS_X, 128 + !(data_psx[i][0] & 0x20) * 127 - !(data_psx[i][0] & 0x80) * 128);
  353. input_report_abs(dev + i, ABS_Y, 128 + !(data_psx[i][0] & 0x40) * 127 - !(data_psx[i][0] & 0x10) * 128);
  354. }
  355. for (j = 0; j < 8; j++)
  356. input_report_key(dev + i, gc_psx_btn[j], ~data_psx[i][1] & (1 << j));
  357. input_report_key(dev + i, BTN_START, ~data_psx[i][0] & 0x08);
  358. input_report_key(dev + i, BTN_SELECT, ~data_psx[i][0] & 0x01);
  359. input_sync(dev + i);
  360. break;
  361. case GC_PSX_NORMAL:
  362. if(gc->pads[GC_DDR] & gc_status_bit[i]) {
  363. for(j = 0; j < 4; j++)
  364. input_report_key(dev + i, gc_psx_ddr_btn[j], ~data_psx[i][0] & (0x10 << j));
  365. } else {
  366. input_report_abs(dev + i, ABS_X, 128 + !(data_psx[i][0] & 0x20) * 127 - !(data_psx[i][0] & 0x80) * 128);
  367. input_report_abs(dev + i, ABS_Y, 128 + !(data_psx[i][0] & 0x40) * 127 - !(data_psx[i][0] & 0x10) * 128);
  368. /* for some reason if the extra axes are left unset they drift */
  369. /* for (j = 0; j < 4; j++)
  370. input_report_abs(dev + i, gc_psx_abs[j+2], 128);
  371. * This needs to be debugged properly,
  372. * maybe fuzz processing needs to be done in input_sync()
  373. * --vojtech
  374. */
  375. }
  376. for (j = 0; j < 8; j++)
  377. input_report_key(dev + i, gc_psx_btn[j], ~data_psx[i][1] & (1 << j));
  378. input_report_key(dev + i, BTN_START, ~data_psx[i][0] & 0x08);
  379. input_report_key(dev + i, BTN_SELECT, ~data_psx[i][0] & 0x01);
  380. input_sync(dev + i);
  381. break;
  382. case 0: /* not a pad, ignore */
  383. break;
  384. }
  385. }
  386. }
  387. mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
  388. }
  389. static int gc_open(struct input_dev *dev)
  390. {
  391. struct gc *gc = dev->private;
  392. int err;
  393. err = down_interruptible(&gc->sem);
  394. if (err)
  395. return err;
  396. if (!gc->used++) {
  397. parport_claim(gc->pd);
  398. parport_write_control(gc->pd->port, 0x04);
  399. mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
  400. }
  401. up(&gc->sem);
  402. return 0;
  403. }
  404. static void gc_close(struct input_dev *dev)
  405. {
  406. struct gc *gc = dev->private;
  407. down(&gc->sem);
  408. if (!--gc->used) {
  409. del_timer_sync(&gc->timer);
  410. parport_write_control(gc->pd->port, 0x00);
  411. parport_release(gc->pd);
  412. }
  413. up(&gc->sem);
  414. }
  415. static struct gc __init *gc_probe(int *config, int nargs)
  416. {
  417. struct gc *gc;
  418. struct parport *pp;
  419. int i, j;
  420. if (config[0] < 0)
  421. return NULL;
  422. if (nargs < 2) {
  423. printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
  424. return NULL;
  425. }
  426. pp = parport_find_number(config[0]);
  427. if (!pp) {
  428. printk(KERN_ERR "gamecon.c: no such parport\n");
  429. return NULL;
  430. }
  431. if (!(gc = kzalloc(sizeof(struct gc), GFP_KERNEL))) {
  432. parport_put_port(pp);
  433. return NULL;
  434. }
  435. init_MUTEX(&gc->sem);
  436. gc->pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
  437. parport_put_port(pp);
  438. if (!gc->pd) {
  439. printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
  440. kfree(gc);
  441. return NULL;
  442. }
  443. parport_claim(gc->pd);
  444. init_timer(&gc->timer);
  445. gc->timer.data = (long) gc;
  446. gc->timer.function = gc_timer;
  447. for (i = 0; i < nargs - 1; i++) {
  448. if (!config[i + 1])
  449. continue;
  450. if (config[i + 1] < 1 || config[i + 1] > GC_MAX) {
  451. printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", config[i + 1]);
  452. continue;
  453. }
  454. gc->dev[i].private = gc;
  455. gc->dev[i].open = gc_open;
  456. gc->dev[i].close = gc_close;
  457. gc->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  458. for (j = 0; j < 2; j++) {
  459. set_bit(ABS_X + j, gc->dev[i].absbit);
  460. gc->dev[i].absmin[ABS_X + j] = -1;
  461. gc->dev[i].absmax[ABS_X + j] = 1;
  462. }
  463. gc->pads[0] |= gc_status_bit[i];
  464. gc->pads[config[i + 1]] |= gc_status_bit[i];
  465. switch(config[i + 1]) {
  466. case GC_N64:
  467. for (j = 0; j < 10; j++)
  468. set_bit(gc_n64_btn[j], gc->dev[i].keybit);
  469. for (j = 0; j < 2; j++) {
  470. set_bit(ABS_X + j, gc->dev[i].absbit);
  471. gc->dev[i].absmin[ABS_X + j] = -127;
  472. gc->dev[i].absmax[ABS_X + j] = 126;
  473. gc->dev[i].absflat[ABS_X + j] = 2;
  474. set_bit(ABS_HAT0X + j, gc->dev[i].absbit);
  475. gc->dev[i].absmin[ABS_HAT0X + j] = -1;
  476. gc->dev[i].absmax[ABS_HAT0X + j] = 1;
  477. }
  478. break;
  479. case GC_SNES:
  480. for (j = 4; j < 8; j++)
  481. set_bit(gc_snes_btn[j], gc->dev[i].keybit);
  482. case GC_NES:
  483. for (j = 0; j < 4; j++)
  484. set_bit(gc_snes_btn[j], gc->dev[i].keybit);
  485. break;
  486. case GC_MULTI2:
  487. set_bit(BTN_THUMB, gc->dev[i].keybit);
  488. case GC_MULTI:
  489. set_bit(BTN_TRIGGER, gc->dev[i].keybit);
  490. break;
  491. case GC_PSX:
  492. case GC_DDR:
  493. if(config[i + 1] == GC_DDR) {
  494. for (j = 0; j < 4; j++)
  495. set_bit(gc_psx_ddr_btn[j], gc->dev[i].keybit);
  496. } else {
  497. for (j = 0; j < 6; j++) {
  498. set_bit(gc_psx_abs[j], gc->dev[i].absbit);
  499. gc->dev[i].absmin[gc_psx_abs[j]] = 4;
  500. gc->dev[i].absmax[gc_psx_abs[j]] = 252;
  501. gc->dev[i].absflat[gc_psx_abs[j]] = 2;
  502. }
  503. }
  504. for (j = 0; j < 12; j++)
  505. set_bit(gc_psx_btn[j], gc->dev[i].keybit);
  506. break;
  507. }
  508. sprintf(gc->phys[i], "%s/input%d", gc->pd->port->name, i);
  509. gc->dev[i].name = gc_names[config[i + 1]];
  510. gc->dev[i].phys = gc->phys[i];
  511. gc->dev[i].id.bustype = BUS_PARPORT;
  512. gc->dev[i].id.vendor = 0x0001;
  513. gc->dev[i].id.product = config[i + 1];
  514. gc->dev[i].id.version = 0x0100;
  515. }
  516. parport_release(gc->pd);
  517. if (!gc->pads[0]) {
  518. parport_unregister_device(gc->pd);
  519. kfree(gc);
  520. return NULL;
  521. }
  522. for (i = 0; i < 5; i++)
  523. if (gc->pads[0] & gc_status_bit[i]) {
  524. input_register_device(gc->dev + i);
  525. printk(KERN_INFO "input: %s on %s\n", gc->dev[i].name, gc->pd->port->name);
  526. }
  527. return gc;
  528. }
  529. static int __init gc_init(void)
  530. {
  531. gc_base[0] = gc_probe(gc, gc_nargs);
  532. gc_base[1] = gc_probe(gc_2, gc_nargs_2);
  533. gc_base[2] = gc_probe(gc_3, gc_nargs_3);
  534. if (gc_base[0] || gc_base[1] || gc_base[2])
  535. return 0;
  536. return -ENODEV;
  537. }
  538. static void __exit gc_exit(void)
  539. {
  540. int i, j;
  541. for (i = 0; i < 3; i++)
  542. if (gc_base[i]) {
  543. for (j = 0; j < 5; j++)
  544. if (gc_base[i]->pads[0] & gc_status_bit[j])
  545. input_unregister_device(gc_base[i]->dev + j);
  546. parport_unregister_device(gc_base[i]->pd);
  547. }
  548. }
  549. module_init(gc_init);
  550. module_exit(gc_exit);