gamecon.c 25 KB

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