David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * IPWireless 3G PCMCIA Network Driver |
| 3 | * |
| 4 | * Original code |
| 5 | * by Stephen Blackheath <stephen@blacksapphire.com>, |
| 6 | * Ben Martel <benm@symmetric.co.nz> |
| 7 | * |
| 8 | * Copyrighted as follows: |
| 9 | * Copyright (C) 2004 by Symmetric Systems Ltd (NZ) |
| 10 | * |
| 11 | * Various driver changes and rewrites, port to new kernels |
| 12 | * Copyright (C) 2006-2007 Jiri Kosina |
| 13 | * |
| 14 | * Misc code cleanups and updates |
| 15 | * Copyright (C) 2007 David Sterba |
| 16 | */ |
| 17 | |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/ppp_defs.h> |
| 23 | #include <linux/if.h> |
Paul Mackerras | 4b32da2b | 2012-03-04 12:56:55 +0000 | [diff] [blame] | 24 | #include <linux/ppp-ioctl.h> |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 25 | #include <linux/sched.h> |
| 26 | #include <linux/serial.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/tty.h> |
| 29 | #include <linux/tty_driver.h> |
| 30 | #include <linux/tty_flip.h> |
| 31 | #include <linux/uaccess.h> |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 32 | |
| 33 | #include "tty.h" |
| 34 | #include "network.h" |
| 35 | #include "hardware.h" |
| 36 | #include "main.h" |
| 37 | |
| 38 | #define IPWIRELESS_PCMCIA_START (0) |
| 39 | #define IPWIRELESS_PCMCIA_MINORS (24) |
| 40 | #define IPWIRELESS_PCMCIA_MINOR_RANGE (8) |
| 41 | |
| 42 | #define TTYTYPE_MODEM (0) |
| 43 | #define TTYTYPE_MONITOR (1) |
| 44 | #define TTYTYPE_RAS_RAW (2) |
| 45 | |
| 46 | struct ipw_tty { |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 47 | struct tty_port port; |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 48 | int index; |
| 49 | struct ipw_hardware *hardware; |
| 50 | unsigned int channel_idx; |
| 51 | unsigned int secondary_channel_idx; |
| 52 | int tty_type; |
| 53 | struct ipw_network *network; |
| 54 | struct tty_struct *linux_tty; |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 55 | unsigned int control_lines; |
| 56 | struct mutex ipw_tty_mutex; |
| 57 | int tx_bytes_queued; |
| 58 | int closing; |
| 59 | }; |
| 60 | |
| 61 | static struct ipw_tty *ttys[IPWIRELESS_PCMCIA_MINORS]; |
| 62 | |
| 63 | static struct tty_driver *ipw_tty_driver; |
| 64 | |
| 65 | static char *tty_type_name(int tty_type) |
| 66 | { |
| 67 | static char *channel_names[] = { |
| 68 | "modem", |
| 69 | "monitor", |
| 70 | "RAS-raw" |
| 71 | }; |
| 72 | |
| 73 | return channel_names[tty_type]; |
| 74 | } |
| 75 | |
Jiri Slaby | ecaa3bd | 2012-03-05 14:52:02 +0100 | [diff] [blame] | 76 | static struct ipw_tty *get_tty(int index) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 77 | { |
Jiri Slaby | ecaa3bd | 2012-03-05 14:52:02 +0100 | [diff] [blame] | 78 | /* |
| 79 | * The 'ras_raw' channel is only available when 'loopback' mode |
| 80 | * is enabled. |
| 81 | * Number of minor starts with 16 (_RANGE * _RAS_RAW). |
| 82 | */ |
| 83 | if (!ipwireless_loopback && index >= |
| 84 | IPWIRELESS_PCMCIA_MINOR_RANGE * TTYTYPE_RAS_RAW) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 85 | return NULL; |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 86 | |
Jiri Slaby | ecaa3bd | 2012-03-05 14:52:02 +0100 | [diff] [blame] | 87 | return ttys[index]; |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int ipw_open(struct tty_struct *linux_tty, struct file *filp) |
| 91 | { |
Jiri Slaby | ecaa3bd | 2012-03-05 14:52:02 +0100 | [diff] [blame] | 92 | struct ipw_tty *tty = get_tty(linux_tty->index); |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 93 | |
| 94 | if (!tty) |
| 95 | return -ENODEV; |
| 96 | |
| 97 | mutex_lock(&tty->ipw_tty_mutex); |
| 98 | |
| 99 | if (tty->closing) { |
| 100 | mutex_unlock(&tty->ipw_tty_mutex); |
| 101 | return -ENODEV; |
| 102 | } |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 103 | if (tty->port.count == 0) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 104 | tty->tx_bytes_queued = 0; |
| 105 | |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 106 | tty->port.count++; |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 107 | |
| 108 | tty->linux_tty = linux_tty; |
| 109 | linux_tty->driver_data = tty; |
| 110 | linux_tty->low_latency = 1; |
| 111 | |
| 112 | if (tty->tty_type == TTYTYPE_MODEM) |
| 113 | ipwireless_ppp_open(tty->network); |
| 114 | |
| 115 | mutex_unlock(&tty->ipw_tty_mutex); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static void do_ipw_close(struct ipw_tty *tty) |
| 121 | { |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 122 | tty->port.count--; |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 123 | |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 124 | if (tty->port.count == 0) { |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 125 | struct tty_struct *linux_tty = tty->linux_tty; |
| 126 | |
| 127 | if (linux_tty != NULL) { |
| 128 | tty->linux_tty = NULL; |
| 129 | linux_tty->driver_data = NULL; |
| 130 | |
| 131 | if (tty->tty_type == TTYTYPE_MODEM) |
| 132 | ipwireless_ppp_close(tty->network); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void ipw_hangup(struct tty_struct *linux_tty) |
| 138 | { |
| 139 | struct ipw_tty *tty = linux_tty->driver_data; |
| 140 | |
| 141 | if (!tty) |
| 142 | return; |
| 143 | |
| 144 | mutex_lock(&tty->ipw_tty_mutex); |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 145 | if (tty->port.count == 0) { |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 146 | mutex_unlock(&tty->ipw_tty_mutex); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | do_ipw_close(tty); |
| 151 | |
| 152 | mutex_unlock(&tty->ipw_tty_mutex); |
| 153 | } |
| 154 | |
| 155 | static void ipw_close(struct tty_struct *linux_tty, struct file *filp) |
| 156 | { |
| 157 | ipw_hangup(linux_tty); |
| 158 | } |
| 159 | |
| 160 | /* Take data received from hardware, and send it out the tty */ |
| 161 | void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data, |
| 162 | unsigned int length) |
| 163 | { |
| 164 | struct tty_struct *linux_tty; |
| 165 | int work = 0; |
| 166 | |
| 167 | mutex_lock(&tty->ipw_tty_mutex); |
| 168 | linux_tty = tty->linux_tty; |
| 169 | if (linux_tty == NULL) { |
| 170 | mutex_unlock(&tty->ipw_tty_mutex); |
| 171 | return; |
| 172 | } |
| 173 | |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 174 | if (!tty->port.count) { |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 175 | mutex_unlock(&tty->ipw_tty_mutex); |
| 176 | return; |
| 177 | } |
| 178 | mutex_unlock(&tty->ipw_tty_mutex); |
| 179 | |
| 180 | work = tty_insert_flip_string(linux_tty, data, length); |
| 181 | |
| 182 | if (work != length) |
| 183 | printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME |
| 184 | ": %d chars not inserted to flip buffer!\n", |
| 185 | length - work); |
| 186 | |
| 187 | /* |
| 188 | * This may sleep if ->low_latency is set |
| 189 | */ |
| 190 | if (work) |
| 191 | tty_flip_buffer_push(linux_tty); |
| 192 | } |
| 193 | |
| 194 | static void ipw_write_packet_sent_callback(void *callback_data, |
| 195 | unsigned int packet_length) |
| 196 | { |
| 197 | struct ipw_tty *tty = callback_data; |
| 198 | |
| 199 | /* |
| 200 | * Packet has been sent, so we subtract the number of bytes from our |
| 201 | * tally of outstanding TX bytes. |
| 202 | */ |
| 203 | tty->tx_bytes_queued -= packet_length; |
| 204 | } |
| 205 | |
| 206 | static int ipw_write(struct tty_struct *linux_tty, |
| 207 | const unsigned char *buf, int count) |
| 208 | { |
| 209 | struct ipw_tty *tty = linux_tty->driver_data; |
| 210 | int room, ret; |
| 211 | |
| 212 | if (!tty) |
| 213 | return -ENODEV; |
| 214 | |
| 215 | mutex_lock(&tty->ipw_tty_mutex); |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 216 | if (!tty->port.count) { |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 217 | mutex_unlock(&tty->ipw_tty_mutex); |
| 218 | return -EINVAL; |
| 219 | } |
| 220 | |
| 221 | room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued; |
| 222 | if (room < 0) |
| 223 | room = 0; |
| 224 | /* Don't allow caller to write any more than we have room for */ |
| 225 | if (count > room) |
| 226 | count = room; |
| 227 | |
| 228 | if (count == 0) { |
| 229 | mutex_unlock(&tty->ipw_tty_mutex); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | ret = ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS, |
David Sterba | ff3e990 | 2008-07-28 16:53:11 +0200 | [diff] [blame] | 234 | buf, count, |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 235 | ipw_write_packet_sent_callback, tty); |
| 236 | if (ret == -1) { |
| 237 | mutex_unlock(&tty->ipw_tty_mutex); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | tty->tx_bytes_queued += count; |
| 242 | mutex_unlock(&tty->ipw_tty_mutex); |
| 243 | |
| 244 | return count; |
| 245 | } |
| 246 | |
| 247 | static int ipw_write_room(struct tty_struct *linux_tty) |
| 248 | { |
| 249 | struct ipw_tty *tty = linux_tty->driver_data; |
| 250 | int room; |
| 251 | |
Alan Cox | 5aaa70a | 2008-10-13 10:38:07 +0100 | [diff] [blame] | 252 | /* FIXME: Exactly how is the tty object locked here .. */ |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 253 | if (!tty) |
| 254 | return -ENODEV; |
| 255 | |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 256 | if (!tty->port.count) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 257 | return -EINVAL; |
| 258 | |
| 259 | room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued; |
| 260 | if (room < 0) |
| 261 | room = 0; |
| 262 | |
| 263 | return room; |
| 264 | } |
| 265 | |
| 266 | static int ipwireless_get_serial_info(struct ipw_tty *tty, |
| 267 | struct serial_struct __user *retinfo) |
| 268 | { |
| 269 | struct serial_struct tmp; |
| 270 | |
| 271 | if (!retinfo) |
| 272 | return (-EFAULT); |
| 273 | |
| 274 | memset(&tmp, 0, sizeof(tmp)); |
| 275 | tmp.type = PORT_UNKNOWN; |
| 276 | tmp.line = tty->index; |
| 277 | tmp.port = 0; |
| 278 | tmp.irq = 0; |
| 279 | tmp.flags = 0; |
| 280 | tmp.baud_base = 115200; |
| 281 | tmp.close_delay = 0; |
| 282 | tmp.closing_wait = 0; |
| 283 | tmp.custom_divisor = 0; |
| 284 | tmp.hub6 = 0; |
| 285 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 286 | return -EFAULT; |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static int ipw_chars_in_buffer(struct tty_struct *linux_tty) |
| 292 | { |
| 293 | struct ipw_tty *tty = linux_tty->driver_data; |
| 294 | |
| 295 | if (!tty) |
Alan Cox | 23198fd | 2009-07-20 16:05:27 +0100 | [diff] [blame] | 296 | return 0; |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 297 | |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 298 | if (!tty->port.count) |
Alan Cox | 23198fd | 2009-07-20 16:05:27 +0100 | [diff] [blame] | 299 | return 0; |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 300 | |
| 301 | return tty->tx_bytes_queued; |
| 302 | } |
| 303 | |
| 304 | static int get_control_lines(struct ipw_tty *tty) |
| 305 | { |
| 306 | unsigned int my = tty->control_lines; |
| 307 | unsigned int out = 0; |
| 308 | |
| 309 | if (my & IPW_CONTROL_LINE_RTS) |
| 310 | out |= TIOCM_RTS; |
| 311 | if (my & IPW_CONTROL_LINE_DTR) |
| 312 | out |= TIOCM_DTR; |
| 313 | if (my & IPW_CONTROL_LINE_CTS) |
| 314 | out |= TIOCM_CTS; |
| 315 | if (my & IPW_CONTROL_LINE_DSR) |
| 316 | out |= TIOCM_DSR; |
| 317 | if (my & IPW_CONTROL_LINE_DCD) |
| 318 | out |= TIOCM_CD; |
| 319 | |
| 320 | return out; |
| 321 | } |
| 322 | |
| 323 | static int set_control_lines(struct ipw_tty *tty, unsigned int set, |
| 324 | unsigned int clear) |
| 325 | { |
| 326 | int ret; |
| 327 | |
| 328 | if (set & TIOCM_RTS) { |
| 329 | ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1); |
| 330 | if (ret) |
| 331 | return ret; |
| 332 | if (tty->secondary_channel_idx != -1) { |
| 333 | ret = ipwireless_set_RTS(tty->hardware, |
| 334 | tty->secondary_channel_idx, 1); |
| 335 | if (ret) |
| 336 | return ret; |
| 337 | } |
| 338 | } |
| 339 | if (set & TIOCM_DTR) { |
| 340 | ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | if (tty->secondary_channel_idx != -1) { |
| 344 | ret = ipwireless_set_DTR(tty->hardware, |
| 345 | tty->secondary_channel_idx, 1); |
| 346 | if (ret) |
| 347 | return ret; |
| 348 | } |
| 349 | } |
| 350 | if (clear & TIOCM_RTS) { |
| 351 | ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 0); |
| 352 | if (tty->secondary_channel_idx != -1) { |
| 353 | ret = ipwireless_set_RTS(tty->hardware, |
| 354 | tty->secondary_channel_idx, 0); |
| 355 | if (ret) |
| 356 | return ret; |
| 357 | } |
| 358 | } |
| 359 | if (clear & TIOCM_DTR) { |
| 360 | ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 0); |
| 361 | if (tty->secondary_channel_idx != -1) { |
| 362 | ret = ipwireless_set_DTR(tty->hardware, |
| 363 | tty->secondary_channel_idx, 0); |
| 364 | if (ret) |
| 365 | return ret; |
| 366 | } |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 371 | static int ipw_tiocmget(struct tty_struct *linux_tty) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 372 | { |
| 373 | struct ipw_tty *tty = linux_tty->driver_data; |
Alan Cox | 5aaa70a | 2008-10-13 10:38:07 +0100 | [diff] [blame] | 374 | /* FIXME: Exactly how is the tty object locked here .. */ |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 375 | |
| 376 | if (!tty) |
| 377 | return -ENODEV; |
| 378 | |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 379 | if (!tty->port.count) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | |
| 382 | return get_control_lines(tty); |
| 383 | } |
| 384 | |
| 385 | static int |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 386 | ipw_tiocmset(struct tty_struct *linux_tty, |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 387 | unsigned int set, unsigned int clear) |
| 388 | { |
| 389 | struct ipw_tty *tty = linux_tty->driver_data; |
Alan Cox | 5aaa70a | 2008-10-13 10:38:07 +0100 | [diff] [blame] | 390 | /* FIXME: Exactly how is the tty object locked here .. */ |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 391 | |
| 392 | if (!tty) |
| 393 | return -ENODEV; |
| 394 | |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 395 | if (!tty->port.count) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 396 | return -EINVAL; |
| 397 | |
| 398 | return set_control_lines(tty, set, clear); |
| 399 | } |
| 400 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 401 | static int ipw_ioctl(struct tty_struct *linux_tty, |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 402 | unsigned int cmd, unsigned long arg) |
| 403 | { |
| 404 | struct ipw_tty *tty = linux_tty->driver_data; |
| 405 | |
| 406 | if (!tty) |
| 407 | return -ENODEV; |
| 408 | |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 409 | if (!tty->port.count) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 410 | return -EINVAL; |
| 411 | |
Alan Cox | 5aaa70a | 2008-10-13 10:38:07 +0100 | [diff] [blame] | 412 | /* FIXME: Exactly how is the tty object locked here .. */ |
| 413 | |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 414 | switch (cmd) { |
| 415 | case TIOCGSERIAL: |
| 416 | return ipwireless_get_serial_info(tty, (void __user *) arg); |
| 417 | |
| 418 | case TIOCSSERIAL: |
| 419 | return 0; /* Keeps the PCMCIA scripts happy. */ |
| 420 | } |
| 421 | |
| 422 | if (tty->tty_type == TTYTYPE_MODEM) { |
| 423 | switch (cmd) { |
| 424 | case PPPIOCGCHAN: |
| 425 | { |
| 426 | int chan = ipwireless_ppp_channel_index( |
| 427 | tty->network); |
| 428 | |
| 429 | if (chan < 0) |
| 430 | return -ENODEV; |
| 431 | if (put_user(chan, (int __user *) arg)) |
| 432 | return -EFAULT; |
| 433 | } |
| 434 | return 0; |
| 435 | |
| 436 | case PPPIOCGUNIT: |
| 437 | { |
| 438 | int unit = ipwireless_ppp_unit_number( |
| 439 | tty->network); |
| 440 | |
| 441 | if (unit < 0) |
| 442 | return -ENODEV; |
| 443 | if (put_user(unit, (int __user *) arg)) |
| 444 | return -EFAULT; |
| 445 | } |
| 446 | return 0; |
| 447 | |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 448 | case FIONREAD: |
| 449 | { |
| 450 | int val = 0; |
| 451 | |
| 452 | if (put_user(val, (int __user *) arg)) |
| 453 | return -EFAULT; |
| 454 | } |
| 455 | return 0; |
Alan Cox | 5aaa70a | 2008-10-13 10:38:07 +0100 | [diff] [blame] | 456 | case TCFLSH: |
| 457 | return tty_perform_flush(linux_tty, arg); |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 458 | } |
| 459 | } |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 460 | return -ENOIOCTLCMD; |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Dominik Brodowski | b498ada | 2010-03-20 19:43:26 +0100 | [diff] [blame] | 463 | static int add_tty(int j, |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 464 | struct ipw_hardware *hardware, |
| 465 | struct ipw_network *network, int channel_idx, |
| 466 | int secondary_channel_idx, int tty_type) |
| 467 | { |
| 468 | ttys[j] = kzalloc(sizeof(struct ipw_tty), GFP_KERNEL); |
| 469 | if (!ttys[j]) |
| 470 | return -ENOMEM; |
| 471 | ttys[j]->index = j; |
| 472 | ttys[j]->hardware = hardware; |
| 473 | ttys[j]->channel_idx = channel_idx; |
| 474 | ttys[j]->secondary_channel_idx = secondary_channel_idx; |
| 475 | ttys[j]->network = network; |
| 476 | ttys[j]->tty_type = tty_type; |
| 477 | mutex_init(&ttys[j]->ipw_tty_mutex); |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 478 | tty_port_init(&ttys[j]->port); |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 479 | |
| 480 | tty_register_device(ipw_tty_driver, j, NULL); |
| 481 | ipwireless_associate_network_tty(network, channel_idx, ttys[j]); |
| 482 | |
| 483 | if (secondary_channel_idx != -1) |
| 484 | ipwireless_associate_network_tty(network, |
| 485 | secondary_channel_idx, |
| 486 | ttys[j]); |
Jiri Slaby | e6df3cc | 2012-04-02 13:54:32 +0200 | [diff] [blame] | 487 | /* check if we provide raw device (if loopback is enabled) */ |
| 488 | if (get_tty(j)) |
| 489 | printk(KERN_INFO IPWIRELESS_PCCARD_NAME |
| 490 | ": registering %s device ttyIPWp%d\n", |
| 491 | tty_type_name(tty_type), j); |
| 492 | |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware, |
Dominik Brodowski | b498ada | 2010-03-20 19:43:26 +0100 | [diff] [blame] | 497 | struct ipw_network *network) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 498 | { |
| 499 | int i, j; |
| 500 | |
| 501 | for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) { |
| 502 | int allfree = 1; |
| 503 | |
| 504 | for (j = i; j < IPWIRELESS_PCMCIA_MINORS; |
| 505 | j += IPWIRELESS_PCMCIA_MINOR_RANGE) |
| 506 | if (ttys[j] != NULL) { |
| 507 | allfree = 0; |
| 508 | break; |
| 509 | } |
| 510 | |
| 511 | if (allfree) { |
| 512 | j = i; |
| 513 | |
Dominik Brodowski | b498ada | 2010-03-20 19:43:26 +0100 | [diff] [blame] | 514 | if (add_tty(j, hardware, network, |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 515 | IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS, |
| 516 | TTYTYPE_MODEM)) |
| 517 | return NULL; |
| 518 | |
| 519 | j += IPWIRELESS_PCMCIA_MINOR_RANGE; |
Dominik Brodowski | b498ada | 2010-03-20 19:43:26 +0100 | [diff] [blame] | 520 | if (add_tty(j, hardware, network, |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 521 | IPW_CHANNEL_DIALLER, -1, |
| 522 | TTYTYPE_MONITOR)) |
| 523 | return NULL; |
| 524 | |
| 525 | j += IPWIRELESS_PCMCIA_MINOR_RANGE; |
Dominik Brodowski | b498ada | 2010-03-20 19:43:26 +0100 | [diff] [blame] | 526 | if (add_tty(j, hardware, network, |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 527 | IPW_CHANNEL_RAS, -1, |
| 528 | TTYTYPE_RAS_RAW)) |
| 529 | return NULL; |
| 530 | |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 531 | return ttys[i]; |
| 532 | } |
| 533 | } |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Must be called before ipwireless_network_free(). |
| 539 | */ |
| 540 | void ipwireless_tty_free(struct ipw_tty *tty) |
| 541 | { |
| 542 | int j; |
| 543 | struct ipw_network *network = ttys[tty->index]->network; |
| 544 | |
| 545 | for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS; |
| 546 | j += IPWIRELESS_PCMCIA_MINOR_RANGE) { |
| 547 | struct ipw_tty *ttyj = ttys[j]; |
| 548 | |
| 549 | if (ttyj) { |
| 550 | mutex_lock(&ttyj->ipw_tty_mutex); |
Jiri Slaby | e6df3cc | 2012-04-02 13:54:32 +0200 | [diff] [blame] | 551 | if (get_tty(j)) |
| 552 | printk(KERN_INFO IPWIRELESS_PCCARD_NAME |
| 553 | ": deregistering %s device ttyIPWp%d\n", |
| 554 | tty_type_name(ttyj->tty_type), j); |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 555 | ttyj->closing = 1; |
| 556 | if (ttyj->linux_tty != NULL) { |
| 557 | mutex_unlock(&ttyj->ipw_tty_mutex); |
Jiri Slaby | de3a60a | 2012-04-02 13:54:31 +0200 | [diff] [blame] | 558 | tty_vhangup(ttyj->linux_tty); |
Alan Cox | 5aaa70a | 2008-10-13 10:38:07 +0100 | [diff] [blame] | 559 | /* FIXME: Exactly how is the tty object locked here |
| 560 | against a parallel ioctl etc */ |
Jiri Slaby | de3a60a | 2012-04-02 13:54:31 +0200 | [diff] [blame] | 561 | /* FIXME2: hangup does not mean all processes |
| 562 | * are gone */ |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 563 | mutex_lock(&ttyj->ipw_tty_mutex); |
| 564 | } |
Jiri Slaby | 7393af8 | 2012-04-02 13:54:33 +0200 | [diff] [blame^] | 565 | while (ttyj->port.count) |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 566 | do_ipw_close(ttyj); |
| 567 | ipwireless_disassociate_network_ttys(network, |
| 568 | ttyj->channel_idx); |
| 569 | tty_unregister_device(ipw_tty_driver, j); |
| 570 | ttys[j] = NULL; |
| 571 | mutex_unlock(&ttyj->ipw_tty_mutex); |
| 572 | kfree(ttyj); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
Alexey Dobriyan | 1cceefd3 | 2009-10-03 00:12:06 +0400 | [diff] [blame] | 577 | static const struct tty_operations tty_ops = { |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 578 | .open = ipw_open, |
| 579 | .close = ipw_close, |
| 580 | .hangup = ipw_hangup, |
| 581 | .write = ipw_write, |
| 582 | .write_room = ipw_write_room, |
| 583 | .ioctl = ipw_ioctl, |
| 584 | .chars_in_buffer = ipw_chars_in_buffer, |
| 585 | .tiocmget = ipw_tiocmget, |
| 586 | .tiocmset = ipw_tiocmset, |
| 587 | }; |
| 588 | |
| 589 | int ipwireless_tty_init(void) |
| 590 | { |
| 591 | int result; |
| 592 | |
| 593 | ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS); |
| 594 | if (!ipw_tty_driver) |
| 595 | return -ENOMEM; |
| 596 | |
David Sterba | 099dc4f | 2008-02-07 10:57:12 +0100 | [diff] [blame] | 597 | ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME; |
| 598 | ipw_tty_driver->name = "ttyIPWp"; |
| 599 | ipw_tty_driver->major = 0; |
| 600 | ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START; |
| 601 | ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 602 | ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL; |
| 603 | ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
| 604 | ipw_tty_driver->init_termios = tty_std_termios; |
| 605 | ipw_tty_driver->init_termios.c_cflag = |
| 606 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 607 | ipw_tty_driver->init_termios.c_ispeed = 9600; |
| 608 | ipw_tty_driver->init_termios.c_ospeed = 9600; |
| 609 | tty_set_operations(ipw_tty_driver, &tty_ops); |
| 610 | result = tty_register_driver(ipw_tty_driver); |
| 611 | if (result) { |
| 612 | printk(KERN_ERR IPWIRELESS_PCCARD_NAME |
| 613 | ": failed to register tty driver\n"); |
| 614 | put_tty_driver(ipw_tty_driver); |
| 615 | return result; |
| 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | void ipwireless_tty_release(void) |
| 622 | { |
| 623 | int ret; |
| 624 | |
| 625 | ret = tty_unregister_driver(ipw_tty_driver); |
| 626 | put_tty_driver(ipw_tty_driver); |
| 627 | if (ret != 0) |
| 628 | printk(KERN_ERR IPWIRELESS_PCCARD_NAME |
| 629 | ": tty_unregister_driver failed with code %d\n", ret); |
| 630 | } |
| 631 | |
| 632 | int ipwireless_tty_is_modem(struct ipw_tty *tty) |
| 633 | { |
| 634 | return tty->tty_type == TTYTYPE_MODEM; |
| 635 | } |
| 636 | |
| 637 | void |
| 638 | ipwireless_tty_notify_control_line_change(struct ipw_tty *tty, |
| 639 | unsigned int channel_idx, |
| 640 | unsigned int control_lines, |
| 641 | unsigned int changed_mask) |
| 642 | { |
| 643 | unsigned int old_control_lines = tty->control_lines; |
| 644 | |
| 645 | tty->control_lines = (tty->control_lines & ~changed_mask) |
| 646 | | (control_lines & changed_mask); |
| 647 | |
| 648 | /* |
| 649 | * If DCD is de-asserted, we close the tty so pppd can tell that we |
| 650 | * have gone offline. |
| 651 | */ |
| 652 | if ((old_control_lines & IPW_CONTROL_LINE_DCD) |
| 653 | && !(tty->control_lines & IPW_CONTROL_LINE_DCD) |
| 654 | && tty->linux_tty) { |
| 655 | tty_hangup(tty->linux_tty); |
| 656 | } |
| 657 | } |
| 658 | |