db9.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * $Id: db9.c,v 1.13 2002/04/07 20:13:37 vojtech Exp $
  3. *
  4. * Copyright (c) 1999-2001 Vojtech Pavlik
  5. *
  6. * Based on the work of:
  7. * Andree Borrmann Mats Sjövall
  8. */
  9. /*
  10. * Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver for Linux
  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. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/delay.h>
  35. #include <linux/init.h>
  36. #include <linux/parport.h>
  37. #include <linux/input.h>
  38. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  39. MODULE_DESCRIPTION("Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver");
  40. MODULE_LICENSE("GPL");
  41. static int db9[] __initdata = { -1, 0 };
  42. static int db9_nargs __initdata = 0;
  43. module_param_array_named(dev, db9, int, &db9_nargs, 0);
  44. MODULE_PARM_DESC(dev, "Describes first attached device (<parport#>,<type>)");
  45. static int db9_2[] __initdata = { -1, 0 };
  46. static int db9_nargs_2 __initdata = 0;
  47. module_param_array_named(dev2, db9_2, int, &db9_nargs_2, 0);
  48. MODULE_PARM_DESC(dev2, "Describes second attached device (<parport#>,<type>)");
  49. static int db9_3[] __initdata = { -1, 0 };
  50. static int db9_nargs_3 __initdata = 0;
  51. module_param_array_named(dev3, db9_3, int, &db9_nargs_3, 0);
  52. MODULE_PARM_DESC(dev3, "Describes third attached device (<parport#>,<type>)");
  53. __obsolete_setup("db9=");
  54. __obsolete_setup("db9_2=");
  55. __obsolete_setup("db9_3=");
  56. #define DB9_MULTI_STICK 0x01
  57. #define DB9_MULTI2_STICK 0x02
  58. #define DB9_GENESIS_PAD 0x03
  59. #define DB9_GENESIS5_PAD 0x05
  60. #define DB9_GENESIS6_PAD 0x06
  61. #define DB9_SATURN_PAD 0x07
  62. #define DB9_MULTI_0802 0x08
  63. #define DB9_MULTI_0802_2 0x09
  64. #define DB9_CD32_PAD 0x0A
  65. #define DB9_SATURN_DPP 0x0B
  66. #define DB9_SATURN_DPP_2 0x0C
  67. #define DB9_MAX_PAD 0x0D
  68. #define DB9_UP 0x01
  69. #define DB9_DOWN 0x02
  70. #define DB9_LEFT 0x04
  71. #define DB9_RIGHT 0x08
  72. #define DB9_FIRE1 0x10
  73. #define DB9_FIRE2 0x20
  74. #define DB9_FIRE3 0x40
  75. #define DB9_FIRE4 0x80
  76. #define DB9_NORMAL 0x0a
  77. #define DB9_NOSELECT 0x08
  78. #define DB9_MAX_DEVICES 2
  79. #define DB9_GENESIS6_DELAY 14
  80. #define DB9_REFRESH_TIME HZ/100
  81. struct db9 {
  82. struct input_dev dev[DB9_MAX_DEVICES];
  83. struct timer_list timer;
  84. struct pardevice *pd;
  85. int mode;
  86. int used;
  87. char phys[2][32];
  88. };
  89. static struct db9 *db9_base[3];
  90. static short db9_multi_btn[] = { BTN_TRIGGER, BTN_THUMB };
  91. static short db9_genesis_btn[] = { BTN_START, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_MODE };
  92. static short db9_cd32_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START };
  93. static char db9_buttons[DB9_MAX_PAD] = { 0, 1, 2, 4, 0, 6, 8, 9, 1, 1, 7, 9, 9 };
  94. static short *db9_btn[DB9_MAX_PAD] = { NULL, db9_multi_btn, db9_multi_btn, db9_genesis_btn, NULL, db9_genesis_btn,
  95. db9_genesis_btn, db9_cd32_btn, db9_multi_btn, db9_multi_btn, db9_cd32_btn,
  96. db9_cd32_btn, db9_cd32_btn };
  97. static char *db9_name[DB9_MAX_PAD] = { NULL, "Multisystem joystick", "Multisystem joystick (2 fire)", "Genesis pad",
  98. NULL, "Genesis 5 pad", "Genesis 6 pad", "Saturn pad", "Multisystem (0.8.0.2) joystick",
  99. "Multisystem (0.8.0.2-dual) joystick", "Amiga CD-32 pad", "Saturn dpp", "Saturn dpp dual" };
  100. static const int db9_max_pads[DB9_MAX_PAD] = { 0, 1, 1, 1, 0, 1, 1, 6, 1, 2, 1, 6, 12 };
  101. static const int db9_num_axis[DB9_MAX_PAD] = { 0, 2, 2, 2, 0, 2, 2, 7, 2, 2, 2 ,7, 7 };
  102. static const short db9_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_RZ, ABS_Z, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
  103. static const int db9_bidirectional[DB9_MAX_PAD] = { 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0 };
  104. static const int db9_reverse[DB9_MAX_PAD] = { 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0 };
  105. /*
  106. * Saturn controllers
  107. */
  108. #define DB9_SATURN_DELAY 300
  109. static const int db9_saturn_byte[] = { 1, 1, 1, 2, 2, 2, 2, 2, 1 };
  110. static const unsigned char db9_saturn_mask[] = { 0x04, 0x01, 0x02, 0x40, 0x20, 0x10, 0x08, 0x80, 0x08 };
  111. /*
  112. * db9_saturn_write_sub() writes 2 bit data.
  113. */
  114. static void db9_saturn_write_sub(struct parport *port, int type, unsigned char data, int powered, int pwr_sub)
  115. {
  116. unsigned char c;
  117. switch (type) {
  118. case 1: /* DPP1 */
  119. c = 0x80 | 0x30 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | data;
  120. parport_write_data(port, c);
  121. break;
  122. case 2: /* DPP2 */
  123. c = 0x40 | data << 4 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | 0x03;
  124. parport_write_data(port, c);
  125. break;
  126. case 0: /* DB9 */
  127. c = ((((data & 2) ? 2 : 0) | ((data & 1) ? 4 : 0)) ^ 0x02) | !powered;
  128. parport_write_control(port, c);
  129. break;
  130. }
  131. }
  132. /*
  133. * gc_saturn_read_sub() reads 4 bit data.
  134. */
  135. static unsigned char db9_saturn_read_sub(struct parport *port, int type)
  136. {
  137. unsigned char data;
  138. if (type) {
  139. /* DPP */
  140. data = parport_read_status(port) ^ 0x80;
  141. return (data & 0x80 ? 1 : 0) | (data & 0x40 ? 2 : 0)
  142. | (data & 0x20 ? 4 : 0) | (data & 0x10 ? 8 : 0);
  143. } else {
  144. /* DB9 */
  145. data = parport_read_data(port) & 0x0f;
  146. return (data & 0x8 ? 1 : 0) | (data & 0x4 ? 2 : 0)
  147. | (data & 0x2 ? 4 : 0) | (data & 0x1 ? 8 : 0);
  148. }
  149. }
  150. /*
  151. * db9_saturn_read_analog() sends clock and reads 8 bit data.
  152. */
  153. static unsigned char db9_saturn_read_analog(struct parport *port, int type, int powered)
  154. {
  155. unsigned char data;
  156. db9_saturn_write_sub(port, type, 0, powered, 0);
  157. udelay(DB9_SATURN_DELAY);
  158. data = db9_saturn_read_sub(port, type) << 4;
  159. db9_saturn_write_sub(port, type, 2, powered, 0);
  160. udelay(DB9_SATURN_DELAY);
  161. data |= db9_saturn_read_sub(port, type);
  162. return data;
  163. }
  164. /*
  165. * db9_saturn_read_packet() reads whole saturn packet at connector
  166. * and returns device identifier code.
  167. */
  168. static unsigned char db9_saturn_read_packet(struct parport *port, unsigned char *data, int type, int powered)
  169. {
  170. int i, j;
  171. unsigned char tmp;
  172. db9_saturn_write_sub(port, type, 3, powered, 0);
  173. data[0] = db9_saturn_read_sub(port, type);
  174. switch (data[0] & 0x0f) {
  175. case 0xf:
  176. /* 1111 no pad */
  177. return data[0] = 0xff;
  178. case 0x4: case 0x4 | 0x8:
  179. /* ?100 : digital controller */
  180. db9_saturn_write_sub(port, type, 0, powered, 1);
  181. data[2] = db9_saturn_read_sub(port, type) << 4;
  182. db9_saturn_write_sub(port, type, 2, powered, 1);
  183. data[1] = db9_saturn_read_sub(port, type) << 4;
  184. db9_saturn_write_sub(port, type, 1, powered, 1);
  185. data[1] |= db9_saturn_read_sub(port, type);
  186. db9_saturn_write_sub(port, type, 3, powered, 1);
  187. /* data[2] |= db9_saturn_read_sub(port, type); */
  188. data[2] |= data[0];
  189. return data[0] = 0x02;
  190. case 0x1:
  191. /* 0001 : analog controller or multitap */
  192. db9_saturn_write_sub(port, type, 2, powered, 0);
  193. udelay(DB9_SATURN_DELAY);
  194. data[0] = db9_saturn_read_analog(port, type, powered);
  195. if (data[0] != 0x41) {
  196. /* read analog controller */
  197. for (i = 0; i < (data[0] & 0x0f); i++)
  198. data[i + 1] = db9_saturn_read_analog(port, type, powered);
  199. db9_saturn_write_sub(port, type, 3, powered, 0);
  200. return data[0];
  201. } else {
  202. /* read multitap */
  203. if (db9_saturn_read_analog(port, type, powered) != 0x60)
  204. return data[0] = 0xff;
  205. for (i = 0; i < 60; i += 10) {
  206. data[i] = db9_saturn_read_analog(port, type, powered);
  207. if (data[i] != 0xff)
  208. /* read each pad */
  209. for (j = 0; j < (data[i] & 0x0f); j++)
  210. data[i + j + 1] = db9_saturn_read_analog(port, type, powered);
  211. }
  212. db9_saturn_write_sub(port, type, 3, powered, 0);
  213. return 0x41;
  214. }
  215. case 0x0:
  216. /* 0000 : mouse */
  217. db9_saturn_write_sub(port, type, 2, powered, 0);
  218. udelay(DB9_SATURN_DELAY);
  219. tmp = db9_saturn_read_analog(port, type, powered);
  220. if (tmp == 0xff) {
  221. for (i = 0; i < 3; i++)
  222. data[i + 1] = db9_saturn_read_analog(port, type, powered);
  223. db9_saturn_write_sub(port, type, 3, powered, 0);
  224. return data[0] = 0xe3;
  225. }
  226. default:
  227. return data[0];
  228. }
  229. }
  230. /*
  231. * db9_saturn_report() analyzes packet and reports.
  232. */
  233. static int db9_saturn_report(unsigned char id, unsigned char data[60], struct input_dev *dev, int n, int max_pads)
  234. {
  235. int tmp, i, j;
  236. tmp = (id == 0x41) ? 60 : 10;
  237. for (j = 0; (j < tmp) && (n < max_pads); j += 10, n++) {
  238. switch (data[j]) {
  239. case 0x16: /* multi controller (analog 4 axis) */
  240. input_report_abs(dev + n, db9_abs[5], data[j + 6]);
  241. case 0x15: /* mission stick (analog 3 axis) */
  242. input_report_abs(dev + n, db9_abs[3], data[j + 4]);
  243. input_report_abs(dev + n, db9_abs[4], data[j + 5]);
  244. case 0x13: /* racing controller (analog 1 axis) */
  245. input_report_abs(dev + n, db9_abs[2], data[j + 3]);
  246. case 0x34: /* saturn keyboard (udlr ZXC ASD QE Esc) */
  247. case 0x02: /* digital pad (digital 2 axis + buttons) */
  248. input_report_abs(dev + n, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
  249. input_report_abs(dev + n, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
  250. for (i = 0; i < 9; i++)
  251. input_report_key(dev + n, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
  252. break;
  253. case 0x19: /* mission stick x2 (analog 6 axis + buttons) */
  254. input_report_abs(dev + n, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
  255. input_report_abs(dev + n, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
  256. for (i = 0; i < 9; i++)
  257. input_report_key(dev + n, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
  258. input_report_abs(dev + n, db9_abs[2], data[j + 3]);
  259. input_report_abs(dev + n, db9_abs[3], data[j + 4]);
  260. input_report_abs(dev + n, db9_abs[4], data[j + 5]);
  261. /*
  262. input_report_abs(dev + n, db9_abs[8], (data[j + 6] & 128 ? 0 : 1) - (data[j + 6] & 64 ? 0 : 1));
  263. input_report_abs(dev + n, db9_abs[9], (data[j + 6] & 32 ? 0 : 1) - (data[j + 6] & 16 ? 0 : 1));
  264. */
  265. input_report_abs(dev + n, db9_abs[6], data[j + 7]);
  266. input_report_abs(dev + n, db9_abs[7], data[j + 8]);
  267. input_report_abs(dev + n, db9_abs[5], data[j + 9]);
  268. break;
  269. case 0xd3: /* sankyo ff (analog 1 axis + stop btn) */
  270. input_report_key(dev + n, BTN_A, data[j + 3] & 0x80);
  271. input_report_abs(dev + n, db9_abs[2], data[j + 3] & 0x7f);
  272. break;
  273. case 0xe3: /* shuttle mouse (analog 2 axis + buttons. signed value) */
  274. input_report_key(dev + n, BTN_START, data[j + 1] & 0x08);
  275. input_report_key(dev + n, BTN_A, data[j + 1] & 0x04);
  276. input_report_key(dev + n, BTN_C, data[j + 1] & 0x02);
  277. input_report_key(dev + n, BTN_B, data[j + 1] & 0x01);
  278. input_report_abs(dev + n, db9_abs[2], data[j + 2] ^ 0x80);
  279. input_report_abs(dev + n, db9_abs[3], (0xff-(data[j + 3] ^ 0x80))+1); /* */
  280. break;
  281. case 0xff:
  282. default: /* no pad */
  283. input_report_abs(dev + n, db9_abs[0], 0);
  284. input_report_abs(dev + n, db9_abs[1], 0);
  285. for (i = 0; i < 9; i++)
  286. input_report_key(dev + n, db9_cd32_btn[i], 0);
  287. break;
  288. }
  289. }
  290. return n;
  291. }
  292. static int db9_saturn(int mode, struct parport *port, struct input_dev *dev)
  293. {
  294. unsigned char id, data[60];
  295. int type, n, max_pads;
  296. int tmp, i;
  297. switch (mode) {
  298. case DB9_SATURN_PAD:
  299. type = 0;
  300. n = 1;
  301. break;
  302. case DB9_SATURN_DPP:
  303. type = 1;
  304. n = 1;
  305. break;
  306. case DB9_SATURN_DPP_2:
  307. type = 1;
  308. n = 2;
  309. break;
  310. default:
  311. return -1;
  312. }
  313. max_pads = min(db9_max_pads[mode], DB9_MAX_DEVICES);
  314. for (tmp = 0, i = 0; i < n; i++) {
  315. id = db9_saturn_read_packet(port, data, type + i, 1);
  316. tmp = db9_saturn_report(id, data, dev, tmp, max_pads);
  317. }
  318. return 0;
  319. }
  320. static void db9_timer(unsigned long private)
  321. {
  322. struct db9 *db9 = (void *) private;
  323. struct parport *port = db9->pd->port;
  324. struct input_dev *dev = db9->dev;
  325. int data, i;
  326. switch(db9->mode) {
  327. case DB9_MULTI_0802_2:
  328. data = parport_read_data(port) >> 3;
  329. input_report_abs(dev + 1, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  330. input_report_abs(dev + 1, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  331. input_report_key(dev + 1, BTN_TRIGGER, ~data & DB9_FIRE1);
  332. case DB9_MULTI_0802:
  333. data = parport_read_status(port) >> 3;
  334. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  335. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  336. input_report_key(dev, BTN_TRIGGER, data & DB9_FIRE1);
  337. break;
  338. case DB9_MULTI_STICK:
  339. data = parport_read_data(port);
  340. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  341. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  342. input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
  343. break;
  344. case DB9_MULTI2_STICK:
  345. data = parport_read_data(port);
  346. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  347. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  348. input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
  349. input_report_key(dev, BTN_THUMB, ~data & DB9_FIRE2);
  350. break;
  351. case DB9_GENESIS_PAD:
  352. parport_write_control(port, DB9_NOSELECT);
  353. data = parport_read_data(port);
  354. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  355. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  356. input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
  357. input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
  358. parport_write_control(port, DB9_NORMAL);
  359. data=parport_read_data(port);
  360. input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
  361. input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
  362. break;
  363. case DB9_GENESIS5_PAD:
  364. parport_write_control(port, DB9_NOSELECT);
  365. data=parport_read_data(port);
  366. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  367. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  368. input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
  369. input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
  370. parport_write_control(port, DB9_NORMAL);
  371. data=parport_read_data(port);
  372. input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
  373. input_report_key(dev, BTN_X, ~data & DB9_FIRE2);
  374. input_report_key(dev, BTN_Y, ~data & DB9_LEFT);
  375. input_report_key(dev, BTN_START, ~data & DB9_RIGHT);
  376. break;
  377. case DB9_GENESIS6_PAD:
  378. parport_write_control(port, DB9_NOSELECT); /* 1 */
  379. udelay(DB9_GENESIS6_DELAY);
  380. data=parport_read_data(port);
  381. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  382. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  383. input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
  384. input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
  385. parport_write_control(port, DB9_NORMAL);
  386. udelay(DB9_GENESIS6_DELAY);
  387. data=parport_read_data(port);
  388. input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
  389. input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
  390. parport_write_control(port, DB9_NOSELECT); /* 2 */
  391. udelay(DB9_GENESIS6_DELAY);
  392. parport_write_control(port, DB9_NORMAL);
  393. udelay(DB9_GENESIS6_DELAY);
  394. parport_write_control(port, DB9_NOSELECT); /* 3 */
  395. udelay(DB9_GENESIS6_DELAY);
  396. data=parport_read_data(port);
  397. input_report_key(dev, BTN_X, ~data & DB9_LEFT);
  398. input_report_key(dev, BTN_Y, ~data & DB9_DOWN);
  399. input_report_key(dev, BTN_Z, ~data & DB9_UP);
  400. input_report_key(dev, BTN_MODE, ~data & DB9_RIGHT);
  401. parport_write_control(port, DB9_NORMAL);
  402. udelay(DB9_GENESIS6_DELAY);
  403. parport_write_control(port, DB9_NOSELECT); /* 4 */
  404. udelay(DB9_GENESIS6_DELAY);
  405. parport_write_control(port, DB9_NORMAL);
  406. break;
  407. case DB9_SATURN_PAD:
  408. case DB9_SATURN_DPP:
  409. case DB9_SATURN_DPP_2:
  410. db9_saturn(db9->mode, port, dev);
  411. break;
  412. case DB9_CD32_PAD:
  413. data=parport_read_data(port);
  414. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  415. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  416. parport_write_control(port, 0x0a);
  417. for (i = 0; i < 7; i++) {
  418. data = parport_read_data(port);
  419. parport_write_control(port, 0x02);
  420. parport_write_control(port, 0x0a);
  421. input_report_key(dev, db9_cd32_btn[i], ~data & DB9_FIRE2);
  422. }
  423. parport_write_control(port, 0x00);
  424. break;
  425. }
  426. input_sync(dev);
  427. mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
  428. }
  429. static int db9_open(struct input_dev *dev)
  430. {
  431. struct db9 *db9 = dev->private;
  432. struct parport *port = db9->pd->port;
  433. if (!db9->used++) {
  434. parport_claim(db9->pd);
  435. parport_write_data(port, 0xff);
  436. if (db9_reverse[db9->mode]) {
  437. parport_data_reverse(port);
  438. parport_write_control(port, DB9_NORMAL);
  439. }
  440. mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
  441. }
  442. return 0;
  443. }
  444. static void db9_close(struct input_dev *dev)
  445. {
  446. struct db9 *db9 = dev->private;
  447. struct parport *port = db9->pd->port;
  448. if (!--db9->used) {
  449. del_timer(&db9->timer);
  450. parport_write_control(port, 0x00);
  451. parport_data_forward(port);
  452. parport_release(db9->pd);
  453. }
  454. }
  455. static struct db9 __init *db9_probe(int *config, int nargs)
  456. {
  457. struct db9 *db9;
  458. struct parport *pp;
  459. int i, j;
  460. if (config[0] < 0)
  461. return NULL;
  462. if (nargs < 2) {
  463. printk(KERN_ERR "db9.c: Device type must be specified.\n");
  464. return NULL;
  465. }
  466. if (config[1] < 1 || config[1] >= DB9_MAX_PAD || !db9_buttons[config[1]]) {
  467. printk(KERN_ERR "db9.c: bad config\n");
  468. return NULL;
  469. }
  470. pp = parport_find_number(config[0]);
  471. if (!pp) {
  472. printk(KERN_ERR "db9.c: no such parport\n");
  473. return NULL;
  474. }
  475. if (db9_bidirectional[config[1]]) {
  476. if (!(pp->modes & PARPORT_MODE_TRISTATE)) {
  477. printk(KERN_ERR "db9.c: specified parport is not bidirectional\n");
  478. parport_put_port(pp);
  479. return NULL;
  480. }
  481. }
  482. if (!(db9 = kmalloc(sizeof(struct db9), GFP_KERNEL))) {
  483. parport_put_port(pp);
  484. return NULL;
  485. }
  486. memset(db9, 0, sizeof(struct db9));
  487. db9->mode = config[1];
  488. init_timer(&db9->timer);
  489. db9->timer.data = (long) db9;
  490. db9->timer.function = db9_timer;
  491. db9->pd = parport_register_device(pp, "db9", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
  492. parport_put_port(pp);
  493. if (!db9->pd) {
  494. printk(KERN_ERR "db9.c: parport busy already - lp.o loaded?\n");
  495. kfree(db9);
  496. return NULL;
  497. }
  498. for (i = 0; i < (min(db9_max_pads[db9->mode], DB9_MAX_DEVICES)); i++) {
  499. sprintf(db9->phys[i], "%s/input%d", db9->pd->port->name, i);
  500. db9->dev[i].private = db9;
  501. db9->dev[i].open = db9_open;
  502. db9->dev[i].close = db9_close;
  503. db9->dev[i].name = db9_name[db9->mode];
  504. db9->dev[i].phys = db9->phys[i];
  505. db9->dev[i].id.bustype = BUS_PARPORT;
  506. db9->dev[i].id.vendor = 0x0002;
  507. db9->dev[i].id.product = config[1];
  508. db9->dev[i].id.version = 0x0100;
  509. db9->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  510. for (j = 0; j < db9_buttons[db9->mode]; j++)
  511. set_bit(db9_btn[db9->mode][j], db9->dev[i].keybit);
  512. for (j = 0; j < db9_num_axis[db9->mode]; j++) {
  513. set_bit(db9_abs[j], db9->dev[i].absbit);
  514. if (j < 2) {
  515. db9->dev[i].absmin[db9_abs[j]] = -1;
  516. db9->dev[i].absmax[db9_abs[j]] = 1;
  517. } else {
  518. db9->dev[i].absmin[db9_abs[j]] = 1;
  519. db9->dev[i].absmax[db9_abs[j]] = 255;
  520. db9->dev[i].absflat[db9_abs[j]] = 0;
  521. }
  522. }
  523. input_register_device(db9->dev + i);
  524. printk(KERN_INFO "input: %s on %s\n", db9->dev[i].name, db9->pd->port->name);
  525. }
  526. return db9;
  527. }
  528. static int __init db9_init(void)
  529. {
  530. db9_base[0] = db9_probe(db9, db9_nargs);
  531. db9_base[1] = db9_probe(db9_2, db9_nargs_2);
  532. db9_base[2] = db9_probe(db9_3, db9_nargs_3);
  533. if (db9_base[0] || db9_base[1] || db9_base[2])
  534. return 0;
  535. return -ENODEV;
  536. }
  537. static void __exit db9_exit(void)
  538. {
  539. int i, j;
  540. for (i = 0; i < 3; i++)
  541. if (db9_base[i]) {
  542. for (j = 0; j < min(db9_max_pads[db9_base[i]->mode], DB9_MAX_DEVICES); j++)
  543. input_unregister_device(db9_base[i]->dev + j);
  544. parport_unregister_device(db9_base[i]->pd);
  545. }
  546. }
  547. module_init(db9_init);
  548. module_exit(db9_exit);