db9.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Copyright (c) 1999-2001 Vojtech Pavlik
  3. *
  4. * Based on the work of:
  5. * Andree Borrmann Mats Sjövall
  6. */
  7. /*
  8. * Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver for Linux
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * Should you need to contact me, the author, you can do so either by
  26. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  27. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/delay.h>
  32. #include <linux/init.h>
  33. #include <linux/parport.h>
  34. #include <linux/input.h>
  35. #include <linux/mutex.h>
  36. #include <linux/slab.h>
  37. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  38. MODULE_DESCRIPTION("Atari, Amstrad, Commodore, Amiga, Sega, etc. joystick driver");
  39. MODULE_LICENSE("GPL");
  40. struct db9_config {
  41. int args[2];
  42. unsigned int nargs;
  43. };
  44. #define DB9_MAX_PORTS 3
  45. static struct db9_config db9_cfg[DB9_MAX_PORTS] __initdata;
  46. module_param_array_named(dev, db9_cfg[0].args, int, &db9_cfg[0].nargs, 0);
  47. MODULE_PARM_DESC(dev, "Describes first attached device (<parport#>,<type>)");
  48. module_param_array_named(dev2, db9_cfg[1].args, int, &db9_cfg[1].nargs, 0);
  49. MODULE_PARM_DESC(dev2, "Describes second attached device (<parport#>,<type>)");
  50. module_param_array_named(dev3, db9_cfg[2].args, int, &db9_cfg[2].nargs, 0);
  51. MODULE_PARM_DESC(dev3, "Describes third attached device (<parport#>,<type>)");
  52. #define DB9_ARG_PARPORT 0
  53. #define DB9_ARG_MODE 1
  54. #define DB9_MULTI_STICK 0x01
  55. #define DB9_MULTI2_STICK 0x02
  56. #define DB9_GENESIS_PAD 0x03
  57. #define DB9_GENESIS5_PAD 0x05
  58. #define DB9_GENESIS6_PAD 0x06
  59. #define DB9_SATURN_PAD 0x07
  60. #define DB9_MULTI_0802 0x08
  61. #define DB9_MULTI_0802_2 0x09
  62. #define DB9_CD32_PAD 0x0A
  63. #define DB9_SATURN_DPP 0x0B
  64. #define DB9_SATURN_DPP_2 0x0C
  65. #define DB9_MAX_PAD 0x0D
  66. #define DB9_UP 0x01
  67. #define DB9_DOWN 0x02
  68. #define DB9_LEFT 0x04
  69. #define DB9_RIGHT 0x08
  70. #define DB9_FIRE1 0x10
  71. #define DB9_FIRE2 0x20
  72. #define DB9_FIRE3 0x40
  73. #define DB9_FIRE4 0x80
  74. #define DB9_NORMAL 0x0a
  75. #define DB9_NOSELECT 0x08
  76. #define DB9_GENESIS6_DELAY 14
  77. #define DB9_REFRESH_TIME HZ/100
  78. #define DB9_MAX_DEVICES 2
  79. struct db9_mode_data {
  80. const char *name;
  81. const short *buttons;
  82. int n_buttons;
  83. int n_pads;
  84. int n_axis;
  85. int bidirectional;
  86. int reverse;
  87. };
  88. struct db9 {
  89. struct input_dev *dev[DB9_MAX_DEVICES];
  90. struct timer_list timer;
  91. struct pardevice *pd;
  92. int mode;
  93. int used;
  94. struct mutex mutex;
  95. char phys[DB9_MAX_DEVICES][32];
  96. };
  97. static struct db9 *db9_base[3];
  98. static const short db9_multi_btn[] = { BTN_TRIGGER, BTN_THUMB };
  99. static const short db9_genesis_btn[] = { BTN_START, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_MODE };
  100. static const short db9_cd32_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START };
  101. 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 };
  102. static const struct db9_mode_data db9_modes[] = {
  103. { NULL, NULL, 0, 0, 0, 0, 0 },
  104. { "Multisystem joystick", db9_multi_btn, 1, 1, 2, 1, 1 },
  105. { "Multisystem joystick (2 fire)", db9_multi_btn, 2, 1, 2, 1, 1 },
  106. { "Genesis pad", db9_genesis_btn, 4, 1, 2, 1, 1 },
  107. { NULL, NULL, 0, 0, 0, 0, 0 },
  108. { "Genesis 5 pad", db9_genesis_btn, 6, 1, 2, 1, 1 },
  109. { "Genesis 6 pad", db9_genesis_btn, 8, 1, 2, 1, 1 },
  110. { "Saturn pad", db9_cd32_btn, 9, 6, 7, 0, 1 },
  111. { "Multisystem (0.8.0.2) joystick", db9_multi_btn, 1, 1, 2, 1, 1 },
  112. { "Multisystem (0.8.0.2-dual) joystick", db9_multi_btn, 1, 2, 2, 1, 1 },
  113. { "Amiga CD-32 pad", db9_cd32_btn, 7, 1, 2, 1, 1 },
  114. { "Saturn dpp", db9_cd32_btn, 9, 6, 7, 0, 0 },
  115. { "Saturn dpp dual", db9_cd32_btn, 9, 12, 7, 0, 0 },
  116. };
  117. /*
  118. * Saturn controllers
  119. */
  120. #define DB9_SATURN_DELAY 300
  121. static const int db9_saturn_byte[] = { 1, 1, 1, 2, 2, 2, 2, 2, 1 };
  122. static const unsigned char db9_saturn_mask[] = { 0x04, 0x01, 0x02, 0x40, 0x20, 0x10, 0x08, 0x80, 0x08 };
  123. /*
  124. * db9_saturn_write_sub() writes 2 bit data.
  125. */
  126. static void db9_saturn_write_sub(struct parport *port, int type, unsigned char data, int powered, int pwr_sub)
  127. {
  128. unsigned char c;
  129. switch (type) {
  130. case 1: /* DPP1 */
  131. c = 0x80 | 0x30 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | data;
  132. parport_write_data(port, c);
  133. break;
  134. case 2: /* DPP2 */
  135. c = 0x40 | data << 4 | (powered ? 0x08 : 0) | (pwr_sub ? 0x04 : 0) | 0x03;
  136. parport_write_data(port, c);
  137. break;
  138. case 0: /* DB9 */
  139. c = ((((data & 2) ? 2 : 0) | ((data & 1) ? 4 : 0)) ^ 0x02) | !powered;
  140. parport_write_control(port, c);
  141. break;
  142. }
  143. }
  144. /*
  145. * gc_saturn_read_sub() reads 4 bit data.
  146. */
  147. static unsigned char db9_saturn_read_sub(struct parport *port, int type)
  148. {
  149. unsigned char data;
  150. if (type) {
  151. /* DPP */
  152. data = parport_read_status(port) ^ 0x80;
  153. return (data & 0x80 ? 1 : 0) | (data & 0x40 ? 2 : 0)
  154. | (data & 0x20 ? 4 : 0) | (data & 0x10 ? 8 : 0);
  155. } else {
  156. /* DB9 */
  157. data = parport_read_data(port) & 0x0f;
  158. return (data & 0x8 ? 1 : 0) | (data & 0x4 ? 2 : 0)
  159. | (data & 0x2 ? 4 : 0) | (data & 0x1 ? 8 : 0);
  160. }
  161. }
  162. /*
  163. * db9_saturn_read_analog() sends clock and reads 8 bit data.
  164. */
  165. static unsigned char db9_saturn_read_analog(struct parport *port, int type, int powered)
  166. {
  167. unsigned char data;
  168. db9_saturn_write_sub(port, type, 0, powered, 0);
  169. udelay(DB9_SATURN_DELAY);
  170. data = db9_saturn_read_sub(port, type) << 4;
  171. db9_saturn_write_sub(port, type, 2, powered, 0);
  172. udelay(DB9_SATURN_DELAY);
  173. data |= db9_saturn_read_sub(port, type);
  174. return data;
  175. }
  176. /*
  177. * db9_saturn_read_packet() reads whole saturn packet at connector
  178. * and returns device identifier code.
  179. */
  180. static unsigned char db9_saturn_read_packet(struct parport *port, unsigned char *data, int type, int powered)
  181. {
  182. int i, j;
  183. unsigned char tmp;
  184. db9_saturn_write_sub(port, type, 3, powered, 0);
  185. data[0] = db9_saturn_read_sub(port, type);
  186. switch (data[0] & 0x0f) {
  187. case 0xf:
  188. /* 1111 no pad */
  189. return data[0] = 0xff;
  190. case 0x4: case 0x4 | 0x8:
  191. /* ?100 : digital controller */
  192. db9_saturn_write_sub(port, type, 0, powered, 1);
  193. data[2] = db9_saturn_read_sub(port, type) << 4;
  194. db9_saturn_write_sub(port, type, 2, powered, 1);
  195. data[1] = db9_saturn_read_sub(port, type) << 4;
  196. db9_saturn_write_sub(port, type, 1, powered, 1);
  197. data[1] |= db9_saturn_read_sub(port, type);
  198. db9_saturn_write_sub(port, type, 3, powered, 1);
  199. /* data[2] |= db9_saturn_read_sub(port, type); */
  200. data[2] |= data[0];
  201. return data[0] = 0x02;
  202. case 0x1:
  203. /* 0001 : analog controller or multitap */
  204. db9_saturn_write_sub(port, type, 2, powered, 0);
  205. udelay(DB9_SATURN_DELAY);
  206. data[0] = db9_saturn_read_analog(port, type, powered);
  207. if (data[0] != 0x41) {
  208. /* read analog controller */
  209. for (i = 0; i < (data[0] & 0x0f); i++)
  210. data[i + 1] = db9_saturn_read_analog(port, type, powered);
  211. db9_saturn_write_sub(port, type, 3, powered, 0);
  212. return data[0];
  213. } else {
  214. /* read multitap */
  215. if (db9_saturn_read_analog(port, type, powered) != 0x60)
  216. return data[0] = 0xff;
  217. for (i = 0; i < 60; i += 10) {
  218. data[i] = db9_saturn_read_analog(port, type, powered);
  219. if (data[i] != 0xff)
  220. /* read each pad */
  221. for (j = 0; j < (data[i] & 0x0f); j++)
  222. data[i + j + 1] = db9_saturn_read_analog(port, type, powered);
  223. }
  224. db9_saturn_write_sub(port, type, 3, powered, 0);
  225. return 0x41;
  226. }
  227. case 0x0:
  228. /* 0000 : mouse */
  229. db9_saturn_write_sub(port, type, 2, powered, 0);
  230. udelay(DB9_SATURN_DELAY);
  231. tmp = db9_saturn_read_analog(port, type, powered);
  232. if (tmp == 0xff) {
  233. for (i = 0; i < 3; i++)
  234. data[i + 1] = db9_saturn_read_analog(port, type, powered);
  235. db9_saturn_write_sub(port, type, 3, powered, 0);
  236. return data[0] = 0xe3;
  237. }
  238. default:
  239. return data[0];
  240. }
  241. }
  242. /*
  243. * db9_saturn_report() analyzes packet and reports.
  244. */
  245. static int db9_saturn_report(unsigned char id, unsigned char data[60], struct input_dev *devs[], int n, int max_pads)
  246. {
  247. struct input_dev *dev;
  248. int tmp, i, j;
  249. tmp = (id == 0x41) ? 60 : 10;
  250. for (j = 0; j < tmp && n < max_pads; j += 10, n++) {
  251. dev = devs[n];
  252. switch (data[j]) {
  253. case 0x16: /* multi controller (analog 4 axis) */
  254. input_report_abs(dev, db9_abs[5], data[j + 6]);
  255. case 0x15: /* mission stick (analog 3 axis) */
  256. input_report_abs(dev, db9_abs[3], data[j + 4]);
  257. input_report_abs(dev, db9_abs[4], data[j + 5]);
  258. case 0x13: /* racing controller (analog 1 axis) */
  259. input_report_abs(dev, db9_abs[2], data[j + 3]);
  260. case 0x34: /* saturn keyboard (udlr ZXC ASD QE Esc) */
  261. case 0x02: /* digital pad (digital 2 axis + buttons) */
  262. input_report_abs(dev, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
  263. input_report_abs(dev, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
  264. for (i = 0; i < 9; i++)
  265. input_report_key(dev, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
  266. break;
  267. case 0x19: /* mission stick x2 (analog 6 axis + buttons) */
  268. input_report_abs(dev, db9_abs[0], !(data[j + 1] & 128) - !(data[j + 1] & 64));
  269. input_report_abs(dev, db9_abs[1], !(data[j + 1] & 32) - !(data[j + 1] & 16));
  270. for (i = 0; i < 9; i++)
  271. input_report_key(dev, db9_cd32_btn[i], ~data[j + db9_saturn_byte[i]] & db9_saturn_mask[i]);
  272. input_report_abs(dev, db9_abs[2], data[j + 3]);
  273. input_report_abs(dev, db9_abs[3], data[j + 4]);
  274. input_report_abs(dev, db9_abs[4], data[j + 5]);
  275. /*
  276. input_report_abs(dev, db9_abs[8], (data[j + 6] & 128 ? 0 : 1) - (data[j + 6] & 64 ? 0 : 1));
  277. input_report_abs(dev, db9_abs[9], (data[j + 6] & 32 ? 0 : 1) - (data[j + 6] & 16 ? 0 : 1));
  278. */
  279. input_report_abs(dev, db9_abs[6], data[j + 7]);
  280. input_report_abs(dev, db9_abs[7], data[j + 8]);
  281. input_report_abs(dev, db9_abs[5], data[j + 9]);
  282. break;
  283. case 0xd3: /* sankyo ff (analog 1 axis + stop btn) */
  284. input_report_key(dev, BTN_A, data[j + 3] & 0x80);
  285. input_report_abs(dev, db9_abs[2], data[j + 3] & 0x7f);
  286. break;
  287. case 0xe3: /* shuttle mouse (analog 2 axis + buttons. signed value) */
  288. input_report_key(dev, BTN_START, data[j + 1] & 0x08);
  289. input_report_key(dev, BTN_A, data[j + 1] & 0x04);
  290. input_report_key(dev, BTN_C, data[j + 1] & 0x02);
  291. input_report_key(dev, BTN_B, data[j + 1] & 0x01);
  292. input_report_abs(dev, db9_abs[2], data[j + 2] ^ 0x80);
  293. input_report_abs(dev, db9_abs[3], (0xff-(data[j + 3] ^ 0x80))+1); /* */
  294. break;
  295. case 0xff:
  296. default: /* no pad */
  297. input_report_abs(dev, db9_abs[0], 0);
  298. input_report_abs(dev, db9_abs[1], 0);
  299. for (i = 0; i < 9; i++)
  300. input_report_key(dev, db9_cd32_btn[i], 0);
  301. break;
  302. }
  303. }
  304. return n;
  305. }
  306. static int db9_saturn(int mode, struct parport *port, struct input_dev *devs[])
  307. {
  308. unsigned char id, data[60];
  309. int type, n, max_pads;
  310. int tmp, i;
  311. switch (mode) {
  312. case DB9_SATURN_PAD:
  313. type = 0;
  314. n = 1;
  315. break;
  316. case DB9_SATURN_DPP:
  317. type = 1;
  318. n = 1;
  319. break;
  320. case DB9_SATURN_DPP_2:
  321. type = 1;
  322. n = 2;
  323. break;
  324. default:
  325. return -1;
  326. }
  327. max_pads = min(db9_modes[mode].n_pads, DB9_MAX_DEVICES);
  328. for (tmp = 0, i = 0; i < n; i++) {
  329. id = db9_saturn_read_packet(port, data, type + i, 1);
  330. tmp = db9_saturn_report(id, data, devs, tmp, max_pads);
  331. }
  332. return 0;
  333. }
  334. static void db9_timer(unsigned long private)
  335. {
  336. struct db9 *db9 = (void *) private;
  337. struct parport *port = db9->pd->port;
  338. struct input_dev *dev = db9->dev[0];
  339. struct input_dev *dev2 = db9->dev[1];
  340. int data, i;
  341. switch (db9->mode) {
  342. case DB9_MULTI_0802_2:
  343. data = parport_read_data(port) >> 3;
  344. input_report_abs(dev2, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  345. input_report_abs(dev2, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  346. input_report_key(dev2, BTN_TRIGGER, ~data & DB9_FIRE1);
  347. case DB9_MULTI_0802:
  348. data = parport_read_status(port) >> 3;
  349. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  350. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  351. input_report_key(dev, BTN_TRIGGER, data & DB9_FIRE1);
  352. break;
  353. case DB9_MULTI_STICK:
  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_TRIGGER, ~data & DB9_FIRE1);
  358. break;
  359. case DB9_MULTI2_STICK:
  360. data = parport_read_data(port);
  361. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  362. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  363. input_report_key(dev, BTN_TRIGGER, ~data & DB9_FIRE1);
  364. input_report_key(dev, BTN_THUMB, ~data & DB9_FIRE2);
  365. break;
  366. case DB9_GENESIS_PAD:
  367. parport_write_control(port, DB9_NOSELECT);
  368. data = parport_read_data(port);
  369. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  370. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  371. input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
  372. input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
  373. parport_write_control(port, DB9_NORMAL);
  374. data = parport_read_data(port);
  375. input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
  376. input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
  377. break;
  378. case DB9_GENESIS5_PAD:
  379. parport_write_control(port, DB9_NOSELECT);
  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. data = parport_read_data(port);
  387. input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
  388. input_report_key(dev, BTN_X, ~data & DB9_FIRE2);
  389. input_report_key(dev, BTN_Y, ~data & DB9_LEFT);
  390. input_report_key(dev, BTN_START, ~data & DB9_RIGHT);
  391. break;
  392. case DB9_GENESIS6_PAD:
  393. parport_write_control(port, DB9_NOSELECT); /* 1 */
  394. udelay(DB9_GENESIS6_DELAY);
  395. data = parport_read_data(port);
  396. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  397. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  398. input_report_key(dev, BTN_B, ~data & DB9_FIRE1);
  399. input_report_key(dev, BTN_C, ~data & DB9_FIRE2);
  400. parport_write_control(port, DB9_NORMAL);
  401. udelay(DB9_GENESIS6_DELAY);
  402. data = parport_read_data(port);
  403. input_report_key(dev, BTN_A, ~data & DB9_FIRE1);
  404. input_report_key(dev, BTN_START, ~data & DB9_FIRE2);
  405. parport_write_control(port, DB9_NOSELECT); /* 2 */
  406. udelay(DB9_GENESIS6_DELAY);
  407. parport_write_control(port, DB9_NORMAL);
  408. udelay(DB9_GENESIS6_DELAY);
  409. parport_write_control(port, DB9_NOSELECT); /* 3 */
  410. udelay(DB9_GENESIS6_DELAY);
  411. data=parport_read_data(port);
  412. input_report_key(dev, BTN_X, ~data & DB9_LEFT);
  413. input_report_key(dev, BTN_Y, ~data & DB9_DOWN);
  414. input_report_key(dev, BTN_Z, ~data & DB9_UP);
  415. input_report_key(dev, BTN_MODE, ~data & DB9_RIGHT);
  416. parport_write_control(port, DB9_NORMAL);
  417. udelay(DB9_GENESIS6_DELAY);
  418. parport_write_control(port, DB9_NOSELECT); /* 4 */
  419. udelay(DB9_GENESIS6_DELAY);
  420. parport_write_control(port, DB9_NORMAL);
  421. break;
  422. case DB9_SATURN_PAD:
  423. case DB9_SATURN_DPP:
  424. case DB9_SATURN_DPP_2:
  425. db9_saturn(db9->mode, port, db9->dev);
  426. break;
  427. case DB9_CD32_PAD:
  428. data = parport_read_data(port);
  429. input_report_abs(dev, ABS_X, (data & DB9_RIGHT ? 0 : 1) - (data & DB9_LEFT ? 0 : 1));
  430. input_report_abs(dev, ABS_Y, (data & DB9_DOWN ? 0 : 1) - (data & DB9_UP ? 0 : 1));
  431. parport_write_control(port, 0x0a);
  432. for (i = 0; i < 7; i++) {
  433. data = parport_read_data(port);
  434. parport_write_control(port, 0x02);
  435. parport_write_control(port, 0x0a);
  436. input_report_key(dev, db9_cd32_btn[i], ~data & DB9_FIRE2);
  437. }
  438. parport_write_control(port, 0x00);
  439. break;
  440. }
  441. input_sync(dev);
  442. mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
  443. }
  444. static int db9_open(struct input_dev *dev)
  445. {
  446. struct db9 *db9 = input_get_drvdata(dev);
  447. struct parport *port = db9->pd->port;
  448. int err;
  449. err = mutex_lock_interruptible(&db9->mutex);
  450. if (err)
  451. return err;
  452. if (!db9->used++) {
  453. parport_claim(db9->pd);
  454. parport_write_data(port, 0xff);
  455. if (db9_modes[db9->mode].reverse) {
  456. parport_data_reverse(port);
  457. parport_write_control(port, DB9_NORMAL);
  458. }
  459. mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME);
  460. }
  461. mutex_unlock(&db9->mutex);
  462. return 0;
  463. }
  464. static void db9_close(struct input_dev *dev)
  465. {
  466. struct db9 *db9 = input_get_drvdata(dev);
  467. struct parport *port = db9->pd->port;
  468. mutex_lock(&db9->mutex);
  469. if (!--db9->used) {
  470. del_timer_sync(&db9->timer);
  471. parport_write_control(port, 0x00);
  472. parport_data_forward(port);
  473. parport_release(db9->pd);
  474. }
  475. mutex_unlock(&db9->mutex);
  476. }
  477. static struct db9 __init *db9_probe(int parport, int mode)
  478. {
  479. struct db9 *db9;
  480. const struct db9_mode_data *db9_mode;
  481. struct parport *pp;
  482. struct pardevice *pd;
  483. struct input_dev *input_dev;
  484. int i, j;
  485. int err;
  486. if (mode < 1 || mode >= DB9_MAX_PAD || !db9_modes[mode].n_buttons) {
  487. printk(KERN_ERR "db9.c: Bad device type %d\n", mode);
  488. err = -EINVAL;
  489. goto err_out;
  490. }
  491. db9_mode = &db9_modes[mode];
  492. pp = parport_find_number(parport);
  493. if (!pp) {
  494. printk(KERN_ERR "db9.c: no such parport\n");
  495. err = -ENODEV;
  496. goto err_out;
  497. }
  498. if (db9_mode->bidirectional && !(pp->modes & PARPORT_MODE_TRISTATE)) {
  499. printk(KERN_ERR "db9.c: specified parport is not bidirectional\n");
  500. err = -EINVAL;
  501. goto err_put_pp;
  502. }
  503. pd = parport_register_device(pp, "db9", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
  504. if (!pd) {
  505. printk(KERN_ERR "db9.c: parport busy already - lp.o loaded?\n");
  506. err = -EBUSY;
  507. goto err_put_pp;
  508. }
  509. db9 = kzalloc(sizeof(struct db9), GFP_KERNEL);
  510. if (!db9) {
  511. printk(KERN_ERR "db9.c: Not enough memory\n");
  512. err = -ENOMEM;
  513. goto err_unreg_pardev;
  514. }
  515. mutex_init(&db9->mutex);
  516. db9->pd = pd;
  517. db9->mode = mode;
  518. init_timer(&db9->timer);
  519. db9->timer.data = (long) db9;
  520. db9->timer.function = db9_timer;
  521. for (i = 0; i < (min(db9_mode->n_pads, DB9_MAX_DEVICES)); i++) {
  522. db9->dev[i] = input_dev = input_allocate_device();
  523. if (!input_dev) {
  524. printk(KERN_ERR "db9.c: Not enough memory for input device\n");
  525. err = -ENOMEM;
  526. goto err_unreg_devs;
  527. }
  528. snprintf(db9->phys[i], sizeof(db9->phys[i]),
  529. "%s/input%d", db9->pd->port->name, i);
  530. input_dev->name = db9_mode->name;
  531. input_dev->phys = db9->phys[i];
  532. input_dev->id.bustype = BUS_PARPORT;
  533. input_dev->id.vendor = 0x0002;
  534. input_dev->id.product = mode;
  535. input_dev->id.version = 0x0100;
  536. input_set_drvdata(input_dev, db9);
  537. input_dev->open = db9_open;
  538. input_dev->close = db9_close;
  539. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  540. for (j = 0; j < db9_mode->n_buttons; j++)
  541. set_bit(db9_mode->buttons[j], input_dev->keybit);
  542. for (j = 0; j < db9_mode->n_axis; j++) {
  543. if (j < 2)
  544. input_set_abs_params(input_dev, db9_abs[j], -1, 1, 0, 0);
  545. else
  546. input_set_abs_params(input_dev, db9_abs[j], 1, 255, 0, 0);
  547. }
  548. err = input_register_device(input_dev);
  549. if (err)
  550. goto err_free_dev;
  551. }
  552. parport_put_port(pp);
  553. return db9;
  554. err_free_dev:
  555. input_free_device(db9->dev[i]);
  556. err_unreg_devs:
  557. while (--i >= 0)
  558. input_unregister_device(db9->dev[i]);
  559. kfree(db9);
  560. err_unreg_pardev:
  561. parport_unregister_device(pd);
  562. err_put_pp:
  563. parport_put_port(pp);
  564. err_out:
  565. return ERR_PTR(err);
  566. }
  567. static void db9_remove(struct db9 *db9)
  568. {
  569. int i;
  570. for (i = 0; i < min(db9_modes[db9->mode].n_pads, DB9_MAX_DEVICES); i++)
  571. input_unregister_device(db9->dev[i]);
  572. parport_unregister_device(db9->pd);
  573. kfree(db9);
  574. }
  575. static int __init db9_init(void)
  576. {
  577. int i;
  578. int have_dev = 0;
  579. int err = 0;
  580. for (i = 0; i < DB9_MAX_PORTS; i++) {
  581. if (db9_cfg[i].nargs == 0 || db9_cfg[i].args[DB9_ARG_PARPORT] < 0)
  582. continue;
  583. if (db9_cfg[i].nargs < 2) {
  584. printk(KERN_ERR "db9.c: Device type must be specified.\n");
  585. err = -EINVAL;
  586. break;
  587. }
  588. db9_base[i] = db9_probe(db9_cfg[i].args[DB9_ARG_PARPORT],
  589. db9_cfg[i].args[DB9_ARG_MODE]);
  590. if (IS_ERR(db9_base[i])) {
  591. err = PTR_ERR(db9_base[i]);
  592. break;
  593. }
  594. have_dev = 1;
  595. }
  596. if (err) {
  597. while (--i >= 0)
  598. if (db9_base[i])
  599. db9_remove(db9_base[i]);
  600. return err;
  601. }
  602. return have_dev ? 0 : -ENODEV;
  603. }
  604. static void __exit db9_exit(void)
  605. {
  606. int i;
  607. for (i = 0; i < DB9_MAX_PORTS; i++)
  608. if (db9_base[i])
  609. db9_remove(db9_base[i]);
  610. }
  611. module_init(db9_init);
  612. module_exit(db9_exit);