db9.c 20 KB

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