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