gamecon.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
  3. *
  4. * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
  5. * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
  6. *
  7. * Based on the work of:
  8. * Andree Borrmann John Dahlstrom
  9. * David Kuder Nathan Hand
  10. * Raphael Assenat
  11. */
  12. /*
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/kernel.h>
  29. #include <linux/delay.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/parport.h>
  33. #include <linux/input.h>
  34. #include <linux/mutex.h>
  35. #include <linux/slab.h>
  36. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  37. MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
  38. MODULE_LICENSE("GPL");
  39. #define GC_MAX_PORTS 3
  40. #define GC_MAX_DEVICES 5
  41. struct gc_config {
  42. int args[GC_MAX_DEVICES + 1];
  43. unsigned int nargs;
  44. };
  45. static struct gc_config gc_cfg[GC_MAX_PORTS];
  46. module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
  47. MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
  48. module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
  49. MODULE_PARM_DESC(map2, "Describes second set of devices");
  50. module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
  51. MODULE_PARM_DESC(map3, "Describes third set of devices");
  52. /* see also gs_psx_delay parameter in PSX support section */
  53. enum gc_type {
  54. GC_NONE = 0,
  55. GC_SNES,
  56. GC_NES,
  57. GC_NES4,
  58. GC_MULTI,
  59. GC_MULTI2,
  60. GC_N64,
  61. GC_PSX,
  62. GC_DDR,
  63. GC_SNESMOUSE,
  64. GC_MAX
  65. };
  66. #define GC_REFRESH_TIME HZ/100
  67. struct gc_pad {
  68. struct input_dev *dev;
  69. enum gc_type type;
  70. char phys[32];
  71. };
  72. struct gc {
  73. struct pardevice *pd;
  74. struct gc_pad pads[GC_MAX_DEVICES];
  75. struct timer_list timer;
  76. int pad_count[GC_MAX];
  77. int used;
  78. int parportno;
  79. struct mutex mutex;
  80. };
  81. struct gc_subdev {
  82. unsigned int idx;
  83. };
  84. static struct gc *gc_base[3];
  85. static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
  86. static const char *gc_names[] = {
  87. NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
  88. "Multisystem 2-button joystick", "N64 controller", "PSX controller",
  89. "PSX DDR controller", "SNES mouse"
  90. };
  91. /*
  92. * N64 support.
  93. */
  94. static const unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
  95. static const short gc_n64_btn[] = {
  96. BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
  97. BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
  98. };
  99. #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
  100. #define GC_N64_STOP_LENGTH 5 /* Length of encoded stop bit */
  101. #define GC_N64_CMD_00 0x11111111UL
  102. #define GC_N64_CMD_01 0xd1111111UL
  103. #define GC_N64_CMD_03 0xdd111111UL
  104. #define GC_N64_CMD_1b 0xdd1dd111UL
  105. #define GC_N64_CMD_c0 0x111111ddUL
  106. #define GC_N64_CMD_80 0x1111111dUL
  107. #define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
  108. #define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
  109. #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
  110. #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
  111. /* GC_N64_DWS > 24 is known to fail */
  112. #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
  113. #define GC_N64_POWER_R 0xfd /* power during read */
  114. #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
  115. /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
  116. /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
  117. /* than 123 us */
  118. #define GC_N64_CLOCK 0x02 /* clock bits for read */
  119. /*
  120. * Used for rumble code.
  121. */
  122. /* Send encoded command */
  123. static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
  124. unsigned char target)
  125. {
  126. struct parport *port = gc->pd->port;
  127. int i;
  128. for (i = 0; i < GC_N64_LENGTH; i++) {
  129. unsigned char data = (cmd >> i) & 1 ? target : 0;
  130. parport_write_data(port, GC_N64_POWER_W | data);
  131. udelay(GC_N64_DWS);
  132. }
  133. }
  134. /* Send stop bit */
  135. static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
  136. {
  137. struct parport *port = gc->pd->port;
  138. int i;
  139. for (i = 0; i < GC_N64_STOP_LENGTH; i++) {
  140. unsigned char data = (GC_N64_STOP_BIT >> i) & 1 ? target : 0;
  141. parport_write_data(port, GC_N64_POWER_W | data);
  142. udelay(GC_N64_DWS);
  143. }
  144. }
  145. /*
  146. * gc_n64_read_packet() reads an N64 packet.
  147. * Each pad uses one bit per byte. So all pads connected to this port
  148. * are read in parallel.
  149. */
  150. static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
  151. {
  152. int i;
  153. unsigned long flags;
  154. /*
  155. * Request the pad to transmit data
  156. */
  157. local_irq_save(flags);
  158. gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
  159. gc_n64_send_stop_bit(gc, GC_N64_OUT);
  160. local_irq_restore(flags);
  161. /*
  162. * Wait for the pad response to be loaded into the 33-bit register
  163. * of the adapter.
  164. */
  165. udelay(GC_N64_DELAY);
  166. /*
  167. * Grab data (ignoring the last bit, which is a stop bit)
  168. */
  169. for (i = 0; i < GC_N64_LENGTH; i++) {
  170. parport_write_data(gc->pd->port, GC_N64_POWER_R);
  171. udelay(2);
  172. data[i] = parport_read_status(gc->pd->port);
  173. parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
  174. }
  175. /*
  176. * We must wait 200 ms here for the controller to reinitialize before
  177. * the next read request. No worries as long as gc_read is polled less
  178. * frequently than this.
  179. */
  180. }
  181. static void gc_n64_process_packet(struct gc *gc)
  182. {
  183. unsigned char data[GC_N64_LENGTH];
  184. struct input_dev *dev;
  185. int i, j, s;
  186. signed char x, y;
  187. gc_n64_read_packet(gc, data);
  188. for (i = 0; i < GC_MAX_DEVICES; i++) {
  189. if (gc->pads[i].type != GC_N64)
  190. continue;
  191. dev = gc->pads[i].dev;
  192. s = gc_status_bit[i];
  193. if (s & ~(data[8] | data[9])) {
  194. x = y = 0;
  195. for (j = 0; j < 8; j++) {
  196. if (data[23 - j] & s)
  197. x |= 1 << j;
  198. if (data[31 - j] & s)
  199. y |= 1 << j;
  200. }
  201. input_report_abs(dev, ABS_X, x);
  202. input_report_abs(dev, ABS_Y, -y);
  203. input_report_abs(dev, ABS_HAT0X,
  204. !(s & data[6]) - !(s & data[7]));
  205. input_report_abs(dev, ABS_HAT0Y,
  206. !(s & data[4]) - !(s & data[5]));
  207. for (j = 0; j < 10; j++)
  208. input_report_key(dev, gc_n64_btn[j],
  209. s & data[gc_n64_bytes[j]]);
  210. input_sync(dev);
  211. }
  212. }
  213. }
  214. static int gc_n64_play_effect(struct input_dev *dev, void *data,
  215. struct ff_effect *effect)
  216. {
  217. int i;
  218. unsigned long flags;
  219. struct gc *gc = input_get_drvdata(dev);
  220. struct gc_subdev *sdev = data;
  221. unsigned char target = 1 << sdev->idx; /* select desired pin */
  222. if (effect->type == FF_RUMBLE) {
  223. struct ff_rumble_effect *rumble = &effect->u.rumble;
  224. unsigned int cmd =
  225. rumble->strong_magnitude || rumble->weak_magnitude ?
  226. GC_N64_CMD_01 : GC_N64_CMD_00;
  227. local_irq_save(flags);
  228. /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
  229. gc_n64_send_command(gc, GC_N64_CMD_03, target);
  230. gc_n64_send_command(gc, GC_N64_CMD_80, target);
  231. gc_n64_send_command(gc, GC_N64_CMD_01, target);
  232. for (i = 0; i < 32; i++)
  233. gc_n64_send_command(gc, GC_N64_CMD_80, target);
  234. gc_n64_send_stop_bit(gc, target);
  235. udelay(GC_N64_DELAY);
  236. /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
  237. gc_n64_send_command(gc, GC_N64_CMD_03, target);
  238. gc_n64_send_command(gc, GC_N64_CMD_c0, target);
  239. gc_n64_send_command(gc, GC_N64_CMD_1b, target);
  240. for (i = 0; i < 32; i++)
  241. gc_n64_send_command(gc, cmd, target);
  242. gc_n64_send_stop_bit(gc, target);
  243. local_irq_restore(flags);
  244. }
  245. return 0;
  246. }
  247. static int gc_n64_init_ff(struct input_dev *dev, int i)
  248. {
  249. struct gc_subdev *sdev;
  250. int err;
  251. sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
  252. if (!sdev)
  253. return -ENOMEM;
  254. sdev->idx = i;
  255. input_set_capability(dev, EV_FF, FF_RUMBLE);
  256. err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
  257. if (err) {
  258. kfree(sdev);
  259. return err;
  260. }
  261. return 0;
  262. }
  263. /*
  264. * NES/SNES support.
  265. */
  266. #define GC_NES_DELAY 6 /* Delay between bits - 6us */
  267. #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
  268. #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
  269. last 4 bits are unused */
  270. #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
  271. 16 bits are equivalent to a gamepad */
  272. #define GC_NES_POWER 0xfc
  273. #define GC_NES_CLOCK 0x01
  274. #define GC_NES_LATCH 0x02
  275. static const unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
  276. static const unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
  277. static const short gc_snes_btn[] = {
  278. BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
  279. };
  280. /*
  281. * gc_nes_read_packet() reads a NES/SNES packet.
  282. * Each pad uses one bit per byte. So all pads connected to
  283. * this port are read in parallel.
  284. */
  285. static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
  286. {
  287. int i;
  288. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
  289. udelay(GC_NES_DELAY * 2);
  290. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
  291. for (i = 0; i < length; i++) {
  292. udelay(GC_NES_DELAY);
  293. parport_write_data(gc->pd->port, GC_NES_POWER);
  294. data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
  295. udelay(GC_NES_DELAY);
  296. parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
  297. }
  298. }
  299. static void gc_nes_process_packet(struct gc *gc)
  300. {
  301. unsigned char data[GC_SNESMOUSE_LENGTH];
  302. struct gc_pad *pad;
  303. struct input_dev *dev;
  304. int i, j, s, len;
  305. char x_rel, y_rel;
  306. len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
  307. (gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
  308. gc_nes_read_packet(gc, len, data);
  309. for (i = 0; i < GC_MAX_DEVICES; i++) {
  310. pad = &gc->pads[i];
  311. dev = pad->dev;
  312. s = gc_status_bit[i];
  313. switch (pad->type) {
  314. case GC_NES:
  315. input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
  316. input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
  317. for (j = 0; j < 4; j++)
  318. input_report_key(dev, gc_snes_btn[j],
  319. s & data[gc_nes_bytes[j]]);
  320. input_sync(dev);
  321. break;
  322. case GC_SNES:
  323. input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
  324. input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
  325. for (j = 0; j < 8; j++)
  326. input_report_key(dev, gc_snes_btn[j],
  327. s & data[gc_snes_bytes[j]]);
  328. input_sync(dev);
  329. break;
  330. case GC_SNESMOUSE:
  331. /*
  332. * The 4 unused bits from SNES controllers appear
  333. * to be ID bits so use them to make sure we are
  334. * dealing with a mouse.
  335. * gamepad is connected. This is important since
  336. * my SNES gamepad sends 1's for bits 16-31, which
  337. * cause the mouse pointer to quickly move to the
  338. * upper left corner of the screen.
  339. */
  340. if (!(s & data[12]) && !(s & data[13]) &&
  341. !(s & data[14]) && (s & data[15])) {
  342. input_report_key(dev, BTN_LEFT, s & data[9]);
  343. input_report_key(dev, BTN_RIGHT, s & data[8]);
  344. x_rel = y_rel = 0;
  345. for (j = 0; j < 7; j++) {
  346. x_rel <<= 1;
  347. if (data[25 + j] & s)
  348. x_rel |= 1;
  349. y_rel <<= 1;
  350. if (data[17 + j] & s)
  351. y_rel |= 1;
  352. }
  353. if (x_rel) {
  354. if (data[24] & s)
  355. x_rel = -x_rel;
  356. input_report_rel(dev, REL_X, x_rel);
  357. }
  358. if (y_rel) {
  359. if (data[16] & s)
  360. y_rel = -y_rel;
  361. input_report_rel(dev, REL_Y, y_rel);
  362. }
  363. input_sync(dev);
  364. }
  365. break;
  366. default:
  367. break;
  368. }
  369. }
  370. }
  371. /*
  372. * Multisystem joystick support
  373. */
  374. #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
  375. #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
  376. /*
  377. * gc_multi_read_packet() reads a Multisystem joystick packet.
  378. */
  379. static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
  380. {
  381. int i;
  382. for (i = 0; i < length; i++) {
  383. parport_write_data(gc->pd->port, ~(1 << i));
  384. data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
  385. }
  386. }
  387. static void gc_multi_process_packet(struct gc *gc)
  388. {
  389. unsigned char data[GC_MULTI2_LENGTH];
  390. int data_len = gc->pad_count[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
  391. struct gc_pad *pad;
  392. struct input_dev *dev;
  393. int i, s;
  394. gc_multi_read_packet(gc, data_len, data);
  395. for (i = 0; i < GC_MAX_DEVICES; i++) {
  396. pad = &gc->pads[i];
  397. dev = pad->dev;
  398. s = gc_status_bit[i];
  399. switch (pad->type) {
  400. case GC_MULTI2:
  401. input_report_key(dev, BTN_THUMB, s & data[5]);
  402. /* fall through */
  403. case GC_MULTI:
  404. input_report_abs(dev, ABS_X,
  405. !(s & data[2]) - !(s & data[3]));
  406. input_report_abs(dev, ABS_Y,
  407. !(s & data[0]) - !(s & data[1]));
  408. input_report_key(dev, BTN_TRIGGER, s & data[4]);
  409. input_sync(dev);
  410. break;
  411. default:
  412. break;
  413. }
  414. }
  415. }
  416. /*
  417. * PSX support
  418. *
  419. * See documentation at:
  420. * http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt
  421. * http://www.gamesx.com/controldata/psxcont/psxcont.htm
  422. *
  423. */
  424. #define GC_PSX_DELAY 25 /* 25 usec */
  425. #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
  426. #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
  427. #define GC_PSX_MOUSE 1 /* Mouse */
  428. #define GC_PSX_NEGCON 2 /* NegCon */
  429. #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
  430. #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
  431. #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
  432. #define GC_PSX_CLOCK 0x04 /* Pin 4 */
  433. #define GC_PSX_COMMAND 0x01 /* Pin 2 */
  434. #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
  435. #define GC_PSX_SELECT 0x02 /* Pin 3 */
  436. #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
  437. #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
  438. static int gc_psx_delay = GC_PSX_DELAY;
  439. module_param_named(psx_delay, gc_psx_delay, uint, 0);
  440. MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
  441. static const short gc_psx_abs[] = {
  442. ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
  443. };
  444. static const short gc_psx_btn[] = {
  445. BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
  446. BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR
  447. };
  448. static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
  449. /*
  450. * gc_psx_command() writes 8bit command and reads 8bit data from
  451. * the psx pad.
  452. */
  453. static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
  454. {
  455. struct parport *port = gc->pd->port;
  456. int i, j, cmd, read;
  457. memset(data, 0, GC_MAX_DEVICES);
  458. for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
  459. cmd = (b & 1) ? GC_PSX_COMMAND : 0;
  460. parport_write_data(port, cmd | GC_PSX_POWER);
  461. udelay(gc_psx_delay);
  462. read = parport_read_status(port) ^ 0x80;
  463. for (j = 0; j < GC_MAX_DEVICES; j++) {
  464. struct gc_pad *pad = &gc->pads[j];
  465. if (pad->type == GC_PSX || pad->type == GC_DDR)
  466. data[j] |= (read & gc_status_bit[j]) ? (1 << i) : 0;
  467. }
  468. parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
  469. udelay(gc_psx_delay);
  470. }
  471. }
  472. /*
  473. * gc_psx_read_packet() reads a whole psx packet and returns
  474. * device identifier code.
  475. */
  476. static void gc_psx_read_packet(struct gc *gc,
  477. unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
  478. unsigned char id[GC_MAX_DEVICES])
  479. {
  480. int i, j, max_len = 0;
  481. unsigned long flags;
  482. unsigned char data2[GC_MAX_DEVICES];
  483. /* Select pad */
  484. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
  485. udelay(gc_psx_delay);
  486. /* Deselect, begin command */
  487. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
  488. udelay(gc_psx_delay);
  489. local_irq_save(flags);
  490. gc_psx_command(gc, 0x01, data2); /* Access pad */
  491. gc_psx_command(gc, 0x42, id); /* Get device ids */
  492. gc_psx_command(gc, 0, data2); /* Dump status */
  493. /* Find the longest pad */
  494. for (i = 0; i < GC_MAX_DEVICES; i++) {
  495. struct gc_pad *pad = &gc->pads[i];
  496. if ((pad->type == GC_PSX || pad->type == GC_DDR) &&
  497. GC_PSX_LEN(id[i]) > max_len &&
  498. GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
  499. max_len = GC_PSX_LEN(id[i]);
  500. }
  501. }
  502. /* Read in all the data */
  503. for (i = 0; i < max_len; i++) {
  504. gc_psx_command(gc, 0, data2);
  505. for (j = 0; j < GC_MAX_DEVICES; j++)
  506. data[j][i] = data2[j];
  507. }
  508. local_irq_restore(flags);
  509. parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
  510. /* Set id's to the real value */
  511. for (i = 0; i < GC_MAX_DEVICES; i++)
  512. id[i] = GC_PSX_ID(id[i]);
  513. }
  514. static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
  515. unsigned char *data)
  516. {
  517. struct input_dev *dev = pad->dev;
  518. int i;
  519. switch (psx_type) {
  520. case GC_PSX_RUMBLE:
  521. input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
  522. input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
  523. /* fall through */
  524. case GC_PSX_NEGCON:
  525. case GC_PSX_ANALOG:
  526. if (pad->type == GC_DDR) {
  527. for (i = 0; i < 4; i++)
  528. input_report_key(dev, gc_psx_ddr_btn[i],
  529. ~data[0] & (0x10 << i));
  530. } else {
  531. for (i = 0; i < 4; i++)
  532. input_report_abs(dev, gc_psx_abs[i + 2],
  533. data[i + 2]);
  534. input_report_abs(dev, ABS_X,
  535. !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
  536. input_report_abs(dev, ABS_Y,
  537. !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
  538. }
  539. for (i = 0; i < 8; i++)
  540. input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
  541. input_report_key(dev, BTN_START, ~data[0] & 0x08);
  542. input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
  543. input_sync(dev);
  544. break;
  545. case GC_PSX_NORMAL:
  546. if (pad->type == GC_DDR) {
  547. for (i = 0; i < 4; i++)
  548. input_report_key(dev, gc_psx_ddr_btn[i],
  549. ~data[0] & (0x10 << i));
  550. } else {
  551. input_report_abs(dev, ABS_X,
  552. !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
  553. input_report_abs(dev, ABS_Y,
  554. !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
  555. /*
  556. * For some reason if the extra axes are left unset
  557. * they drift.
  558. * for (i = 0; i < 4; i++)
  559. input_report_abs(dev, gc_psx_abs[i + 2], 128);
  560. * This needs to be debugged properly,
  561. * maybe fuzz processing needs to be done
  562. * in input_sync()
  563. * --vojtech
  564. */
  565. }
  566. for (i = 0; i < 8; i++)
  567. input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
  568. input_report_key(dev, BTN_START, ~data[0] & 0x08);
  569. input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
  570. input_sync(dev);
  571. break;
  572. default: /* not a pad, ignore */
  573. break;
  574. }
  575. }
  576. static void gc_psx_process_packet(struct gc *gc)
  577. {
  578. unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
  579. unsigned char id[GC_MAX_DEVICES];
  580. struct gc_pad *pad;
  581. int i;
  582. gc_psx_read_packet(gc, data, id);
  583. for (i = 0; i < GC_MAX_DEVICES; i++) {
  584. pad = &gc->pads[i];
  585. if (pad->type == GC_PSX || pad->type == GC_DDR)
  586. gc_psx_report_one(pad, id[i], data[i]);
  587. }
  588. }
  589. /*
  590. * gc_timer() initiates reads of console pads data.
  591. */
  592. static void gc_timer(struct timer_list *t)
  593. {
  594. struct gc *gc = from_timer(gc, t, timer);
  595. /*
  596. * N64 pads - must be read first, any read confuses them for 200 us
  597. */
  598. if (gc->pad_count[GC_N64])
  599. gc_n64_process_packet(gc);
  600. /*
  601. * NES and SNES pads or mouse
  602. */
  603. if (gc->pad_count[GC_NES] ||
  604. gc->pad_count[GC_SNES] ||
  605. gc->pad_count[GC_SNESMOUSE]) {
  606. gc_nes_process_packet(gc);
  607. }
  608. /*
  609. * Multi and Multi2 joysticks
  610. */
  611. if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
  612. gc_multi_process_packet(gc);
  613. /*
  614. * PSX controllers
  615. */
  616. if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
  617. gc_psx_process_packet(gc);
  618. mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
  619. }
  620. static int gc_open(struct input_dev *dev)
  621. {
  622. struct gc *gc = input_get_drvdata(dev);
  623. int err;
  624. err = mutex_lock_interruptible(&gc->mutex);
  625. if (err)
  626. return err;
  627. if (!gc->used++) {
  628. parport_claim(gc->pd);
  629. parport_write_control(gc->pd->port, 0x04);
  630. mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
  631. }
  632. mutex_unlock(&gc->mutex);
  633. return 0;
  634. }
  635. static void gc_close(struct input_dev *dev)
  636. {
  637. struct gc *gc = input_get_drvdata(dev);
  638. mutex_lock(&gc->mutex);
  639. if (!--gc->used) {
  640. del_timer_sync(&gc->timer);
  641. parport_write_control(gc->pd->port, 0x00);
  642. parport_release(gc->pd);
  643. }
  644. mutex_unlock(&gc->mutex);
  645. }
  646. static int gc_setup_pad(struct gc *gc, int idx, int pad_type)
  647. {
  648. struct gc_pad *pad = &gc->pads[idx];
  649. struct input_dev *input_dev;
  650. int i;
  651. int err;
  652. if (pad_type < 1 || pad_type >= GC_MAX) {
  653. pr_err("Pad type %d unknown\n", pad_type);
  654. return -EINVAL;
  655. }
  656. pad->dev = input_dev = input_allocate_device();
  657. if (!input_dev) {
  658. pr_err("Not enough memory for input device\n");
  659. return -ENOMEM;
  660. }
  661. pad->type = pad_type;
  662. snprintf(pad->phys, sizeof(pad->phys),
  663. "%s/input%d", gc->pd->port->name, idx);
  664. input_dev->name = gc_names[pad_type];
  665. input_dev->phys = pad->phys;
  666. input_dev->id.bustype = BUS_PARPORT;
  667. input_dev->id.vendor = 0x0001;
  668. input_dev->id.product = pad_type;
  669. input_dev->id.version = 0x0100;
  670. input_set_drvdata(input_dev, gc);
  671. input_dev->open = gc_open;
  672. input_dev->close = gc_close;
  673. if (pad_type != GC_SNESMOUSE) {
  674. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  675. for (i = 0; i < 2; i++)
  676. input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
  677. } else
  678. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  679. gc->pad_count[pad_type]++;
  680. switch (pad_type) {
  681. case GC_N64:
  682. for (i = 0; i < 10; i++)
  683. input_set_capability(input_dev, EV_KEY, gc_n64_btn[i]);
  684. for (i = 0; i < 2; i++) {
  685. input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
  686. input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
  687. }
  688. err = gc_n64_init_ff(input_dev, idx);
  689. if (err) {
  690. pr_warn("Failed to initiate rumble for N64 device %d\n",
  691. idx);
  692. goto err_free_dev;
  693. }
  694. break;
  695. case GC_SNESMOUSE:
  696. input_set_capability(input_dev, EV_KEY, BTN_LEFT);
  697. input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
  698. input_set_capability(input_dev, EV_REL, REL_X);
  699. input_set_capability(input_dev, EV_REL, REL_Y);
  700. break;
  701. case GC_SNES:
  702. for (i = 4; i < 8; i++)
  703. input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
  704. /* fall through */
  705. case GC_NES:
  706. for (i = 0; i < 4; i++)
  707. input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
  708. break;
  709. case GC_MULTI2:
  710. input_set_capability(input_dev, EV_KEY, BTN_THUMB);
  711. /* fall through */
  712. case GC_MULTI:
  713. input_set_capability(input_dev, EV_KEY, BTN_TRIGGER);
  714. /* fall through */
  715. break;
  716. case GC_PSX:
  717. for (i = 0; i < 6; i++)
  718. input_set_abs_params(input_dev,
  719. gc_psx_abs[i], 4, 252, 0, 2);
  720. for (i = 0; i < 12; i++)
  721. input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
  722. break;
  723. break;
  724. case GC_DDR:
  725. for (i = 0; i < 4; i++)
  726. input_set_capability(input_dev, EV_KEY,
  727. gc_psx_ddr_btn[i]);
  728. for (i = 0; i < 12; i++)
  729. input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
  730. break;
  731. }
  732. err = input_register_device(pad->dev);
  733. if (err)
  734. goto err_free_dev;
  735. return 0;
  736. err_free_dev:
  737. input_free_device(pad->dev);
  738. pad->dev = NULL;
  739. return err;
  740. }
  741. static void gc_attach(struct parport *pp)
  742. {
  743. struct gc *gc;
  744. struct pardevice *pd;
  745. int i, port_idx;
  746. int count = 0;
  747. int *pads, n_pads;
  748. struct pardev_cb gc_parport_cb;
  749. for (port_idx = 0; port_idx < GC_MAX_PORTS; port_idx++) {
  750. if (gc_cfg[port_idx].nargs == 0 || gc_cfg[port_idx].args[0] < 0)
  751. continue;
  752. if (gc_cfg[port_idx].args[0] == pp->number)
  753. break;
  754. }
  755. if (port_idx == GC_MAX_PORTS) {
  756. pr_debug("Not using parport%d.\n", pp->number);
  757. return;
  758. }
  759. pads = gc_cfg[port_idx].args + 1;
  760. n_pads = gc_cfg[port_idx].nargs - 1;
  761. memset(&gc_parport_cb, 0, sizeof(gc_parport_cb));
  762. gc_parport_cb.flags = PARPORT_FLAG_EXCL;
  763. pd = parport_register_dev_model(pp, "gamecon", &gc_parport_cb,
  764. port_idx);
  765. if (!pd) {
  766. pr_err("parport busy already - lp.o loaded?\n");
  767. return;
  768. }
  769. gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
  770. if (!gc) {
  771. pr_err("Not enough memory\n");
  772. goto err_unreg_pardev;
  773. }
  774. mutex_init(&gc->mutex);
  775. gc->pd = pd;
  776. gc->parportno = pp->number;
  777. timer_setup(&gc->timer, gc_timer, 0);
  778. for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
  779. if (!pads[i])
  780. continue;
  781. if (gc_setup_pad(gc, i, pads[i]))
  782. goto err_unreg_devs;
  783. count++;
  784. }
  785. if (count == 0) {
  786. pr_err("No valid devices specified\n");
  787. goto err_free_gc;
  788. }
  789. gc_base[port_idx] = gc;
  790. return;
  791. err_unreg_devs:
  792. while (--i >= 0)
  793. if (gc->pads[i].dev)
  794. input_unregister_device(gc->pads[i].dev);
  795. err_free_gc:
  796. kfree(gc);
  797. err_unreg_pardev:
  798. parport_unregister_device(pd);
  799. }
  800. static void gc_detach(struct parport *port)
  801. {
  802. int i;
  803. struct gc *gc;
  804. for (i = 0; i < GC_MAX_PORTS; i++) {
  805. if (gc_base[i] && gc_base[i]->parportno == port->number)
  806. break;
  807. }
  808. if (i == GC_MAX_PORTS)
  809. return;
  810. gc = gc_base[i];
  811. gc_base[i] = NULL;
  812. for (i = 0; i < GC_MAX_DEVICES; i++)
  813. if (gc->pads[i].dev)
  814. input_unregister_device(gc->pads[i].dev);
  815. parport_unregister_device(gc->pd);
  816. kfree(gc);
  817. }
  818. static struct parport_driver gc_parport_driver = {
  819. .name = "gamecon",
  820. .match_port = gc_attach,
  821. .detach = gc_detach,
  822. .devmodel = true,
  823. };
  824. static int __init gc_init(void)
  825. {
  826. int i;
  827. int have_dev = 0;
  828. for (i = 0; i < GC_MAX_PORTS; i++) {
  829. if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
  830. continue;
  831. if (gc_cfg[i].nargs < 2) {
  832. pr_err("at least one device must be specified\n");
  833. return -EINVAL;
  834. }
  835. have_dev = 1;
  836. }
  837. if (!have_dev)
  838. return -ENODEV;
  839. return parport_register_driver(&gc_parport_driver);
  840. }
  841. static void __exit gc_exit(void)
  842. {
  843. parport_unregister_driver(&gc_parport_driver);
  844. }
  845. module_init(gc_init);
  846. module_exit(gc_exit);