Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / tty / ipwireless / tty.c
blobebd5bff0f5c1668b70bdefead34bc60fca8a81ef
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/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/ppp_defs.h>
22 #include <linux/if.h>
23 #include <linux/ppp-ioctl.h>
24 #include <linux/sched.h>
25 #include <linux/serial.h>
26 #include <linux/slab.h>
27 #include <linux/tty.h>
28 #include <linux/tty_driver.h>
29 #include <linux/tty_flip.h>
30 #include <linux/uaccess.h>
32 #include "tty.h"
33 #include "network.h"
34 #include "hardware.h"
35 #include "main.h"
37 #define IPWIRELESS_PCMCIA_START (0)
38 #define IPWIRELESS_PCMCIA_MINORS (24)
39 #define IPWIRELESS_PCMCIA_MINOR_RANGE (8)
41 #define TTYTYPE_MODEM (0)
42 #define TTYTYPE_MONITOR (1)
43 #define TTYTYPE_RAS_RAW (2)
45 struct ipw_tty {
46 struct tty_port port;
47 int index;
48 struct ipw_hardware *hardware;
49 unsigned int channel_idx;
50 unsigned int secondary_channel_idx;
51 int tty_type;
52 struct ipw_network *network;
53 unsigned int control_lines;
54 struct mutex ipw_tty_mutex;
55 int tx_bytes_queued;
56 int closing;
59 static struct ipw_tty *ttys[IPWIRELESS_PCMCIA_MINORS];
61 static struct tty_driver *ipw_tty_driver;
63 static char *tty_type_name(int tty_type)
65 static char *channel_names[] = {
66 "modem",
67 "monitor",
68 "RAS-raw"
71 return channel_names[tty_type];
74 static struct ipw_tty *get_tty(int index)
77 * The 'ras_raw' channel is only available when 'loopback' mode
78 * is enabled.
79 * Number of minor starts with 16 (_RANGE * _RAS_RAW).
81 if (!ipwireless_loopback && index >=
82 IPWIRELESS_PCMCIA_MINOR_RANGE * TTYTYPE_RAS_RAW)
83 return NULL;
85 return ttys[index];
88 static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
90 struct ipw_tty *tty = get_tty(linux_tty->index);
92 if (!tty)
93 return -ENODEV;
95 mutex_lock(&tty->ipw_tty_mutex);
97 if (tty->closing) {
98 mutex_unlock(&tty->ipw_tty_mutex);
99 return -ENODEV;
101 if (tty->port.count == 0)
102 tty->tx_bytes_queued = 0;
104 tty->port.count++;
106 tty->port.tty = linux_tty;
107 linux_tty->driver_data = tty;
108 tty->port.low_latency = 1;
110 if (tty->tty_type == TTYTYPE_MODEM)
111 ipwireless_ppp_open(tty->network);
113 mutex_unlock(&tty->ipw_tty_mutex);
115 return 0;
118 static void do_ipw_close(struct ipw_tty *tty)
120 tty->port.count--;
122 if (tty->port.count == 0) {
123 struct tty_struct *linux_tty = tty->port.tty;
125 if (linux_tty != NULL) {
126 tty->port.tty = NULL;
127 linux_tty->driver_data = NULL;
129 if (tty->tty_type == TTYTYPE_MODEM)
130 ipwireless_ppp_close(tty->network);
135 static void ipw_hangup(struct tty_struct *linux_tty)
137 struct ipw_tty *tty = linux_tty->driver_data;
139 if (!tty)
140 return;
142 mutex_lock(&tty->ipw_tty_mutex);
143 if (tty->port.count == 0) {
144 mutex_unlock(&tty->ipw_tty_mutex);
145 return;
148 do_ipw_close(tty);
150 mutex_unlock(&tty->ipw_tty_mutex);
153 static void ipw_close(struct tty_struct *linux_tty, struct file *filp)
155 ipw_hangup(linux_tty);
158 /* Take data received from hardware, and send it out the tty */
159 void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
160 unsigned int length)
162 int work = 0;
164 mutex_lock(&tty->ipw_tty_mutex);
166 if (!tty->port.count) {
167 mutex_unlock(&tty->ipw_tty_mutex);
168 return;
170 mutex_unlock(&tty->ipw_tty_mutex);
172 work = tty_insert_flip_string(&tty->port, data, length);
174 if (work != length)
175 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
176 ": %d chars not inserted to flip buffer!\n",
177 length - work);
180 * This may sleep if ->low_latency is set
182 if (work)
183 tty_flip_buffer_push(&tty->port);
186 static void ipw_write_packet_sent_callback(void *callback_data,
187 unsigned int packet_length)
189 struct ipw_tty *tty = callback_data;
192 * Packet has been sent, so we subtract the number of bytes from our
193 * tally of outstanding TX bytes.
195 tty->tx_bytes_queued -= packet_length;
198 static int ipw_write(struct tty_struct *linux_tty,
199 const unsigned char *buf, int count)
201 struct ipw_tty *tty = linux_tty->driver_data;
202 int room, ret;
204 if (!tty)
205 return -ENODEV;
207 mutex_lock(&tty->ipw_tty_mutex);
208 if (!tty->port.count) {
209 mutex_unlock(&tty->ipw_tty_mutex);
210 return -EINVAL;
213 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
214 if (room < 0)
215 room = 0;
216 /* Don't allow caller to write any more than we have room for */
217 if (count > room)
218 count = room;
220 if (count == 0) {
221 mutex_unlock(&tty->ipw_tty_mutex);
222 return 0;
225 ret = ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS,
226 buf, count,
227 ipw_write_packet_sent_callback, tty);
228 if (ret == -1) {
229 mutex_unlock(&tty->ipw_tty_mutex);
230 return 0;
233 tty->tx_bytes_queued += count;
234 mutex_unlock(&tty->ipw_tty_mutex);
236 return count;
239 static int ipw_write_room(struct tty_struct *linux_tty)
241 struct ipw_tty *tty = linux_tty->driver_data;
242 int room;
244 /* FIXME: Exactly how is the tty object locked here .. */
245 if (!tty)
246 return -ENODEV;
248 if (!tty->port.count)
249 return -EINVAL;
251 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
252 if (room < 0)
253 room = 0;
255 return room;
258 static int ipwireless_get_serial_info(struct ipw_tty *tty,
259 struct serial_struct __user *retinfo)
261 struct serial_struct tmp;
263 if (!retinfo)
264 return (-EFAULT);
266 memset(&tmp, 0, sizeof(tmp));
267 tmp.type = PORT_UNKNOWN;
268 tmp.line = tty->index;
269 tmp.port = 0;
270 tmp.irq = 0;
271 tmp.flags = 0;
272 tmp.baud_base = 115200;
273 tmp.close_delay = 0;
274 tmp.closing_wait = 0;
275 tmp.custom_divisor = 0;
276 tmp.hub6 = 0;
277 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
278 return -EFAULT;
280 return 0;
283 static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
285 struct ipw_tty *tty = linux_tty->driver_data;
287 if (!tty)
288 return 0;
290 if (!tty->port.count)
291 return 0;
293 return tty->tx_bytes_queued;
296 static int get_control_lines(struct ipw_tty *tty)
298 unsigned int my = tty->control_lines;
299 unsigned int out = 0;
301 if (my & IPW_CONTROL_LINE_RTS)
302 out |= TIOCM_RTS;
303 if (my & IPW_CONTROL_LINE_DTR)
304 out |= TIOCM_DTR;
305 if (my & IPW_CONTROL_LINE_CTS)
306 out |= TIOCM_CTS;
307 if (my & IPW_CONTROL_LINE_DSR)
308 out |= TIOCM_DSR;
309 if (my & IPW_CONTROL_LINE_DCD)
310 out |= TIOCM_CD;
312 return out;
315 static int set_control_lines(struct ipw_tty *tty, unsigned int set,
316 unsigned int clear)
318 int ret;
320 if (set & TIOCM_RTS) {
321 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1);
322 if (ret)
323 return ret;
324 if (tty->secondary_channel_idx != -1) {
325 ret = ipwireless_set_RTS(tty->hardware,
326 tty->secondary_channel_idx, 1);
327 if (ret)
328 return ret;
331 if (set & TIOCM_DTR) {
332 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1);
333 if (ret)
334 return ret;
335 if (tty->secondary_channel_idx != -1) {
336 ret = ipwireless_set_DTR(tty->hardware,
337 tty->secondary_channel_idx, 1);
338 if (ret)
339 return ret;
342 if (clear & TIOCM_RTS) {
343 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 0);
344 if (tty->secondary_channel_idx != -1) {
345 ret = ipwireless_set_RTS(tty->hardware,
346 tty->secondary_channel_idx, 0);
347 if (ret)
348 return ret;
351 if (clear & TIOCM_DTR) {
352 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 0);
353 if (tty->secondary_channel_idx != -1) {
354 ret = ipwireless_set_DTR(tty->hardware,
355 tty->secondary_channel_idx, 0);
356 if (ret)
357 return ret;
360 return 0;
363 static int ipw_tiocmget(struct tty_struct *linux_tty)
365 struct ipw_tty *tty = linux_tty->driver_data;
366 /* FIXME: Exactly how is the tty object locked here .. */
368 if (!tty)
369 return -ENODEV;
371 if (!tty->port.count)
372 return -EINVAL;
374 return get_control_lines(tty);
377 static int
378 ipw_tiocmset(struct tty_struct *linux_tty,
379 unsigned int set, unsigned int clear)
381 struct ipw_tty *tty = linux_tty->driver_data;
382 /* FIXME: Exactly how is the tty object locked here .. */
384 if (!tty)
385 return -ENODEV;
387 if (!tty->port.count)
388 return -EINVAL;
390 return set_control_lines(tty, set, clear);
393 static int ipw_ioctl(struct tty_struct *linux_tty,
394 unsigned int cmd, unsigned long arg)
396 struct ipw_tty *tty = linux_tty->driver_data;
398 if (!tty)
399 return -ENODEV;
401 if (!tty->port.count)
402 return -EINVAL;
404 /* FIXME: Exactly how is the tty object locked here .. */
406 switch (cmd) {
407 case TIOCGSERIAL:
408 return ipwireless_get_serial_info(tty, (void __user *) arg);
410 case TIOCSSERIAL:
411 return 0; /* Keeps the PCMCIA scripts happy. */
414 if (tty->tty_type == TTYTYPE_MODEM) {
415 switch (cmd) {
416 case PPPIOCGCHAN:
418 int chan = ipwireless_ppp_channel_index(
419 tty->network);
421 if (chan < 0)
422 return -ENODEV;
423 if (put_user(chan, (int __user *) arg))
424 return -EFAULT;
426 return 0;
428 case PPPIOCGUNIT:
430 int unit = ipwireless_ppp_unit_number(
431 tty->network);
433 if (unit < 0)
434 return -ENODEV;
435 if (put_user(unit, (int __user *) arg))
436 return -EFAULT;
438 return 0;
440 case FIONREAD:
442 int val = 0;
444 if (put_user(val, (int __user *) arg))
445 return -EFAULT;
447 return 0;
448 case TCFLSH:
449 return tty_perform_flush(linux_tty, arg);
452 return -ENOIOCTLCMD;
455 static int add_tty(int j,
456 struct ipw_hardware *hardware,
457 struct ipw_network *network, int channel_idx,
458 int secondary_channel_idx, int tty_type)
460 ttys[j] = kzalloc(sizeof(struct ipw_tty), GFP_KERNEL);
461 if (!ttys[j])
462 return -ENOMEM;
463 ttys[j]->index = j;
464 ttys[j]->hardware = hardware;
465 ttys[j]->channel_idx = channel_idx;
466 ttys[j]->secondary_channel_idx = secondary_channel_idx;
467 ttys[j]->network = network;
468 ttys[j]->tty_type = tty_type;
469 mutex_init(&ttys[j]->ipw_tty_mutex);
470 tty_port_init(&ttys[j]->port);
472 tty_port_register_device(&ttys[j]->port, ipw_tty_driver, j, NULL);
473 ipwireless_associate_network_tty(network, channel_idx, ttys[j]);
475 if (secondary_channel_idx != -1)
476 ipwireless_associate_network_tty(network,
477 secondary_channel_idx,
478 ttys[j]);
479 /* check if we provide raw device (if loopback is enabled) */
480 if (get_tty(j))
481 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
482 ": registering %s device ttyIPWp%d\n",
483 tty_type_name(tty_type), j);
485 return 0;
488 struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware,
489 struct ipw_network *network)
491 int i, j;
493 for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) {
494 int allfree = 1;
496 for (j = i; j < IPWIRELESS_PCMCIA_MINORS;
497 j += IPWIRELESS_PCMCIA_MINOR_RANGE)
498 if (ttys[j] != NULL) {
499 allfree = 0;
500 break;
503 if (allfree) {
504 j = i;
506 if (add_tty(j, hardware, network,
507 IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS,
508 TTYTYPE_MODEM))
509 return NULL;
511 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
512 if (add_tty(j, hardware, network,
513 IPW_CHANNEL_DIALLER, -1,
514 TTYTYPE_MONITOR))
515 return NULL;
517 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
518 if (add_tty(j, hardware, network,
519 IPW_CHANNEL_RAS, -1,
520 TTYTYPE_RAS_RAW))
521 return NULL;
523 return ttys[i];
526 return NULL;
530 * Must be called before ipwireless_network_free().
532 void ipwireless_tty_free(struct ipw_tty *tty)
534 int j;
535 struct ipw_network *network = ttys[tty->index]->network;
537 for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS;
538 j += IPWIRELESS_PCMCIA_MINOR_RANGE) {
539 struct ipw_tty *ttyj = ttys[j];
541 if (ttyj) {
542 mutex_lock(&ttyj->ipw_tty_mutex);
543 if (get_tty(j))
544 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
545 ": deregistering %s device ttyIPWp%d\n",
546 tty_type_name(ttyj->tty_type), j);
547 ttyj->closing = 1;
548 if (ttyj->port.tty != NULL) {
549 mutex_unlock(&ttyj->ipw_tty_mutex);
550 tty_vhangup(ttyj->port.tty);
551 /* FIXME: Exactly how is the tty object locked here
552 against a parallel ioctl etc */
553 /* FIXME2: hangup does not mean all processes
554 * are gone */
555 mutex_lock(&ttyj->ipw_tty_mutex);
557 while (ttyj->port.count)
558 do_ipw_close(ttyj);
559 ipwireless_disassociate_network_ttys(network,
560 ttyj->channel_idx);
561 tty_unregister_device(ipw_tty_driver, j);
562 tty_port_destroy(&ttyj->port);
563 ttys[j] = NULL;
564 mutex_unlock(&ttyj->ipw_tty_mutex);
565 kfree(ttyj);
570 static const struct tty_operations tty_ops = {
571 .open = ipw_open,
572 .close = ipw_close,
573 .hangup = ipw_hangup,
574 .write = ipw_write,
575 .write_room = ipw_write_room,
576 .ioctl = ipw_ioctl,
577 .chars_in_buffer = ipw_chars_in_buffer,
578 .tiocmget = ipw_tiocmget,
579 .tiocmset = ipw_tiocmset,
582 int ipwireless_tty_init(void)
584 int result;
586 ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
587 if (!ipw_tty_driver)
588 return -ENOMEM;
590 ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME;
591 ipw_tty_driver->name = "ttyIPWp";
592 ipw_tty_driver->major = 0;
593 ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START;
594 ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
595 ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL;
596 ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
597 ipw_tty_driver->init_termios = tty_std_termios;
598 ipw_tty_driver->init_termios.c_cflag =
599 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
600 ipw_tty_driver->init_termios.c_ispeed = 9600;
601 ipw_tty_driver->init_termios.c_ospeed = 9600;
602 tty_set_operations(ipw_tty_driver, &tty_ops);
603 result = tty_register_driver(ipw_tty_driver);
604 if (result) {
605 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
606 ": failed to register tty driver\n");
607 put_tty_driver(ipw_tty_driver);
608 return result;
611 return 0;
614 void ipwireless_tty_release(void)
616 int ret;
618 ret = tty_unregister_driver(ipw_tty_driver);
619 put_tty_driver(ipw_tty_driver);
620 if (ret != 0)
621 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
622 ": tty_unregister_driver failed with code %d\n", ret);
625 int ipwireless_tty_is_modem(struct ipw_tty *tty)
627 return tty->tty_type == TTYTYPE_MODEM;
630 void
631 ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
632 unsigned int channel_idx,
633 unsigned int control_lines,
634 unsigned int changed_mask)
636 unsigned int old_control_lines = tty->control_lines;
638 tty->control_lines = (tty->control_lines & ~changed_mask)
639 | (control_lines & changed_mask);
642 * If DCD is de-asserted, we close the tty so pppd can tell that we
643 * have gone offline.
645 if ((old_control_lines & IPW_CONTROL_LINE_DCD)
646 && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
647 && tty->port.tty) {
648 tty_hangup(tty->port.tty);