gamecon.c 25 KB

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