x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / tty / ipwireless / tty.c
blobd917a34e94bba6a5fc38ea0f95650b55c53144d3
1 /*
2 * IPWireless 3G PCMCIA Network Driver
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
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>
24 #include <linux/ppp-ioctl.h>
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>
33 #include "tty.h"
34 #include "network.h"
35 #include "hardware.h"
36 #include "main.h"
38 #define IPWIRELESS_PCMCIA_START (0)
39 #define IPWIRELESS_PCMCIA_MINORS (24)
40 #define IPWIRELESS_PCMCIA_MINOR_RANGE (8)
42 #define TTYTYPE_MODEM (0)
43 #define TTYTYPE_MONITOR (1)
44 #define TTYTYPE_RAS_RAW (2)
46 struct ipw_tty {
47 struct tty_port port;
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 unsigned int control_lines;
55 struct mutex ipw_tty_mutex;
56 int tx_bytes_queued;
57 int closing;
60 static struct ipw_tty *ttys[IPWIRELESS_PCMCIA_MINORS];
62 static struct tty_driver *ipw_tty_driver;
64 static char *tty_type_name(int tty_type)
66 static char *channel_names[] = {
67 "modem",
68 "monitor",
69 "RAS-raw"
72 return channel_names[tty_type];
75 static struct ipw_tty *get_tty(int index)
78 * The 'ras_raw' channel is only available when 'loopback' mode
79 * is enabled.
80 * Number of minor starts with 16 (_RANGE * _RAS_RAW).
82 if (!ipwireless_loopback && index >=
83 IPWIRELESS_PCMCIA_MINOR_RANGE * TTYTYPE_RAS_RAW)
84 return NULL;
86 return ttys[index];
89 static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
91 struct ipw_tty *tty = get_tty(linux_tty->index);
93 if (!tty)
94 return -ENODEV;
96 mutex_lock(&tty->ipw_tty_mutex);
98 if (tty->closing) {
99 mutex_unlock(&tty->ipw_tty_mutex);
100 return -ENODEV;
102 if (tty->port.count == 0)
103 tty->tx_bytes_queued = 0;
105 tty->port.count++;
107 tty->port.tty = linux_tty;
108 linux_tty->driver_data = tty;
109 tty->port.low_latency = 1;
111 if (tty->tty_type == TTYTYPE_MODEM)
112 ipwireless_ppp_open(tty->network);
114 mutex_unlock(&tty->ipw_tty_mutex);
116 return 0;
119 static void do_ipw_close(struct ipw_tty *tty)
121 tty->port.count--;
123 if (tty->port.count == 0) {
124 struct tty_struct *linux_tty = tty->port.tty;
126 if (linux_tty != NULL) {
127 tty->port.tty = NULL;
128 linux_tty->driver_data = NULL;
130 if (tty->tty_type == TTYTYPE_MODEM)
131 ipwireless_ppp_close(tty->network);
136 static void ipw_hangup(struct tty_struct *linux_tty)
138 struct ipw_tty *tty = linux_tty->driver_data;
140 if (!tty)
141 return;
143 mutex_lock(&tty->ipw_tty_mutex);
144 if (tty->port.count == 0) {
145 mutex_unlock(&tty->ipw_tty_mutex);
146 return;
149 do_ipw_close(tty);
151 mutex_unlock(&tty->ipw_tty_mutex);
154 static void ipw_close(struct tty_struct *linux_tty, struct file *filp)
156 ipw_hangup(linux_tty);
159 /* Take data received from hardware, and send it out the tty */
160 void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
161 unsigned int length)
163 int work = 0;
165 mutex_lock(&tty->ipw_tty_mutex);
167 if (!tty->port.count) {
168 mutex_unlock(&tty->ipw_tty_mutex);
169 return;
171 mutex_unlock(&tty->ipw_tty_mutex);
173 work = tty_insert_flip_string(&tty->port, data, length);
175 if (work != length)
176 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
177 ": %d chars not inserted to flip buffer!\n",
178 length - work);
180 if (work)
181 tty_flip_buffer_push(&tty->port);
184 static void ipw_write_packet_sent_callback(void *callback_data,
185 unsigned int packet_length)
187 struct ipw_tty *tty = callback_data;
190 * Packet has been sent, so we subtract the number of bytes from our
191 * tally of outstanding TX bytes.
193 tty->tx_bytes_queued -= packet_length;
196 static int ipw_write(struct tty_struct *linux_tty,
197 const unsigned char *buf, int count)
199 struct ipw_tty *tty = linux_tty->driver_data;
200 int room, ret;
202 if (!tty)
203 return -ENODEV;
205 mutex_lock(&tty->ipw_tty_mutex);
206 if (!tty->port.count) {
207 mutex_unlock(&tty->ipw_tty_mutex);
208 return -EINVAL;
211 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
212 if (room < 0)
213 room = 0;
214 /* Don't allow caller to write any more than we have room for */
215 if (count > room)
216 count = room;
218 if (count == 0) {
219 mutex_unlock(&tty->ipw_tty_mutex);
220 return 0;
223 ret = ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS,
224 buf, count,
225 ipw_write_packet_sent_callback, tty);
226 if (ret == -1) {
227 mutex_unlock(&tty->ipw_tty_mutex);
228 return 0;
231 tty->tx_bytes_queued += count;
232 mutex_unlock(&tty->ipw_tty_mutex);
234 return count;
237 static int ipw_write_room(struct tty_struct *linux_tty)
239 struct ipw_tty *tty = linux_tty->driver_data;
240 int room;
242 /* FIXME: Exactly how is the tty object locked here .. */
243 if (!tty)
244 return -ENODEV;
246 if (!tty->port.count)
247 return -EINVAL;
249 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
250 if (room < 0)
251 room = 0;
253 return room;
256 static int ipwireless_get_serial_info(struct ipw_tty *tty,
257 struct serial_struct __user *retinfo)
259 struct serial_struct tmp;
261 if (!retinfo)
262 return (-EFAULT);
264 memset(&tmp, 0, sizeof(tmp));
265 tmp.type = PORT_UNKNOWN;
266 tmp.line = tty->index;
267 tmp.port = 0;
268 tmp.irq = 0;
269 tmp.flags = 0;
270 tmp.baud_base = 115200;
271 tmp.close_delay = 0;
272 tmp.closing_wait = 0;
273 tmp.custom_divisor = 0;
274 tmp.hub6 = 0;
275 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
276 return -EFAULT;
278 return 0;
281 static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
283 struct ipw_tty *tty = linux_tty->driver_data;
285 if (!tty)
286 return 0;
288 if (!tty->port.count)
289 return 0;
291 return tty->tx_bytes_queued;
294 static int get_control_lines(struct ipw_tty *tty)
296 unsigned int my = tty->control_lines;
297 unsigned int out = 0;
299 if (my & IPW_CONTROL_LINE_RTS)
300 out |= TIOCM_RTS;
301 if (my & IPW_CONTROL_LINE_DTR)
302 out |= TIOCM_DTR;
303 if (my & IPW_CONTROL_LINE_CTS)
304 out |= TIOCM_CTS;
305 if (my & IPW_CONTROL_LINE_DSR)
306 out |= TIOCM_DSR;
307 if (my & IPW_CONTROL_LINE_DCD)
308 out |= TIOCM_CD;
310 return out;
313 static int set_control_lines(struct ipw_tty *tty, unsigned int set,
314 unsigned int clear)
316 int ret;
318 if (set & TIOCM_RTS) {
319 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1);
320 if (ret)
321 return ret;
322 if (tty->secondary_channel_idx != -1) {
323 ret = ipwireless_set_RTS(tty->hardware,
324 tty->secondary_channel_idx, 1);
325 if (ret)
326 return ret;
329 if (set & TIOCM_DTR) {
330 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1);
331 if (ret)
332 return ret;
333 if (tty->secondary_channel_idx != -1) {
334 ret = ipwireless_set_DTR(tty->hardware,
335 tty->secondary_channel_idx, 1);
336 if (ret)
337 return ret;
340 if (clear & TIOCM_RTS) {
341 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 0);
342 if (tty->secondary_channel_idx != -1) {
343 ret = ipwireless_set_RTS(tty->hardware,
344 tty->secondary_channel_idx, 0);
345 if (ret)
346 return ret;
349 if (clear & TIOCM_DTR) {
350 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 0);
351 if (tty->secondary_channel_idx != -1) {
352 ret = ipwireless_set_DTR(tty->hardware,
353 tty->secondary_channel_idx, 0);
354 if (ret)
355 return ret;
358 return 0;
361 static int ipw_tiocmget(struct tty_struct *linux_tty)
363 struct ipw_tty *tty = linux_tty->driver_data;
364 /* FIXME: Exactly how is the tty object locked here .. */
366 if (!tty)
367 return -ENODEV;
369 if (!tty->port.count)
370 return -EINVAL;
372 return get_control_lines(tty);
375 static int
376 ipw_tiocmset(struct tty_struct *linux_tty,
377 unsigned int set, unsigned int clear)
379 struct ipw_tty *tty = linux_tty->driver_data;
380 /* FIXME: Exactly how is the tty object locked here .. */
382 if (!tty)
383 return -ENODEV;
385 if (!tty->port.count)
386 return -EINVAL;
388 return set_control_lines(tty, set, clear);
391 static int ipw_ioctl(struct tty_struct *linux_tty,
392 unsigned int cmd, unsigned long arg)
394 struct ipw_tty *tty = linux_tty->driver_data;
396 if (!tty)
397 return -ENODEV;
399 if (!tty->port.count)
400 return -EINVAL;
402 /* FIXME: Exactly how is the tty object locked here .. */
404 switch (cmd) {
405 case TIOCGSERIAL:
406 return ipwireless_get_serial_info(tty, (void __user *) arg);
408 case TIOCSSERIAL:
409 return 0; /* Keeps the PCMCIA scripts happy. */
412 if (tty->tty_type == TTYTYPE_MODEM) {
413 switch (cmd) {
414 case PPPIOCGCHAN:
416 int chan = ipwireless_ppp_channel_index(
417 tty->network);
419 if (chan < 0)
420 return -ENODEV;
421 if (put_user(chan, (int __user *) arg))
422 return -EFAULT;
424 return 0;
426 case PPPIOCGUNIT:
428 int unit = ipwireless_ppp_unit_number(
429 tty->network);
431 if (unit < 0)
432 return -ENODEV;
433 if (put_user(unit, (int __user *) arg))
434 return -EFAULT;
436 return 0;
438 case FIONREAD:
440 int val = 0;
442 if (put_user(val, (int __user *) arg))
443 return -EFAULT;
445 return 0;
446 case TCFLSH:
447 return tty_perform_flush(linux_tty, arg);
450 return -ENOIOCTLCMD;
453 static int add_tty(int j,
454 struct ipw_hardware *hardware,
455 struct ipw_network *network, int channel_idx,
456 int secondary_channel_idx, int tty_type)
458 ttys[j] = kzalloc(sizeof(struct ipw_tty), GFP_KERNEL);
459 if (!ttys[j])
460 return -ENOMEM;
461 ttys[j]->index = j;
462 ttys[j]->hardware = hardware;
463 ttys[j]->channel_idx = channel_idx;
464 ttys[j]->secondary_channel_idx = secondary_channel_idx;
465 ttys[j]->network = network;
466 ttys[j]->tty_type = tty_type;
467 mutex_init(&ttys[j]->ipw_tty_mutex);
468 tty_port_init(&ttys[j]->port);
470 tty_port_register_device(&ttys[j]->port, ipw_tty_driver, j, NULL);
471 ipwireless_associate_network_tty(network, channel_idx, ttys[j]);
473 if (secondary_channel_idx != -1)
474 ipwireless_associate_network_tty(network,
475 secondary_channel_idx,
476 ttys[j]);
477 /* check if we provide raw device (if loopback is enabled) */
478 if (get_tty(j))
479 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
480 ": registering %s device ttyIPWp%d\n",
481 tty_type_name(tty_type), j);
483 return 0;
486 struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware,
487 struct ipw_network *network)
489 int i, j;
491 for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) {
492 int allfree = 1;
494 for (j = i; j < IPWIRELESS_PCMCIA_MINORS;
495 j += IPWIRELESS_PCMCIA_MINOR_RANGE)
496 if (ttys[j] != NULL) {
497 allfree = 0;
498 break;
501 if (allfree) {
502 j = i;
504 if (add_tty(j, hardware, network,
505 IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS,
506 TTYTYPE_MODEM))
507 return NULL;
509 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
510 if (add_tty(j, hardware, network,
511 IPW_CHANNEL_DIALLER, -1,
512 TTYTYPE_MONITOR))
513 return NULL;
515 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
516 if (add_tty(j, hardware, network,
517 IPW_CHANNEL_RAS, -1,
518 TTYTYPE_RAS_RAW))
519 return NULL;
521 return ttys[i];
524 return NULL;
528 * Must be called before ipwireless_network_free().
530 void ipwireless_tty_free(struct ipw_tty *tty)
532 int j;
533 struct ipw_network *network = ttys[tty->index]->network;
535 for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS;
536 j += IPWIRELESS_PCMCIA_MINOR_RANGE) {
537 struct ipw_tty *ttyj = ttys[j];
539 if (ttyj) {
540 mutex_lock(&ttyj->ipw_tty_mutex);
541 if (get_tty(j))
542 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
543 ": deregistering %s device ttyIPWp%d\n",
544 tty_type_name(ttyj->tty_type), j);
545 ttyj->closing = 1;
546 if (ttyj->port.tty != NULL) {
547 mutex_unlock(&ttyj->ipw_tty_mutex);
548 tty_vhangup(ttyj->port.tty);
549 /* FIXME: Exactly how is the tty object locked here
550 against a parallel ioctl etc */
551 /* FIXME2: hangup does not mean all processes
552 * are gone */
553 mutex_lock(&ttyj->ipw_tty_mutex);
555 while (ttyj->port.count)
556 do_ipw_close(ttyj);
557 ipwireless_disassociate_network_ttys(network,
558 ttyj->channel_idx);
559 tty_unregister_device(ipw_tty_driver, j);
560 tty_port_destroy(&ttyj->port);
561 ttys[j] = NULL;
562 mutex_unlock(&ttyj->ipw_tty_mutex);
563 kfree(ttyj);
568 static const struct tty_operations tty_ops = {
569 .open = ipw_open,
570 .close = ipw_close,
571 .hangup = ipw_hangup,
572 .write = ipw_write,
573 .write_room = ipw_write_room,
574 .ioctl = ipw_ioctl,
575 .chars_in_buffer = ipw_chars_in_buffer,
576 .tiocmget = ipw_tiocmget,
577 .tiocmset = ipw_tiocmset,
580 int ipwireless_tty_init(void)
582 int result;
584 ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
585 if (!ipw_tty_driver)
586 return -ENOMEM;
588 ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME;
589 ipw_tty_driver->name = "ttyIPWp";
590 ipw_tty_driver->major = 0;
591 ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START;
592 ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
593 ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL;
594 ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
595 ipw_tty_driver->init_termios = tty_std_termios;
596 ipw_tty_driver->init_termios.c_cflag =
597 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
598 ipw_tty_driver->init_termios.c_ispeed = 9600;
599 ipw_tty_driver->init_termios.c_ospeed = 9600;
600 tty_set_operations(ipw_tty_driver, &tty_ops);
601 result = tty_register_driver(ipw_tty_driver);
602 if (result) {
603 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
604 ": failed to register tty driver\n");
605 put_tty_driver(ipw_tty_driver);
606 return result;
609 return 0;
612 void ipwireless_tty_release(void)
614 int ret;
616 ret = tty_unregister_driver(ipw_tty_driver);
617 put_tty_driver(ipw_tty_driver);
618 if (ret != 0)
619 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
620 ": tty_unregister_driver failed with code %d\n", ret);
623 int ipwireless_tty_is_modem(struct ipw_tty *tty)
625 return tty->tty_type == TTYTYPE_MODEM;
628 void
629 ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
630 unsigned int channel_idx,
631 unsigned int control_lines,
632 unsigned int changed_mask)
634 unsigned int old_control_lines = tty->control_lines;
636 tty->control_lines = (tty->control_lines & ~changed_mask)
637 | (control_lines & changed_mask);
640 * If DCD is de-asserted, we close the tty so pppd can tell that we
641 * have gone offline.
643 if ((old_control_lines & IPW_CONTROL_LINE_DCD)
644 && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
645 && tty->port.tty) {
646 tty_hangup(tty->port.tty);