2 * IPWireless 3G PCMCIA Network Driver
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>
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>
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)
49 struct ipw_hardware *hardware;
50 unsigned int channel_idx;
51 unsigned int secondary_channel_idx;
53 struct ipw_network *network;
54 unsigned int control_lines;
55 struct mutex ipw_tty_mutex;
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[] = {
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
80 * Number of minor starts with 16 (_RANGE * _RAS_RAW).
82 if (!ipwireless_loopback && index >=
83 IPWIRELESS_PCMCIA_MINOR_RANGE * TTYTYPE_RAS_RAW)
89 static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
91 struct ipw_tty *tty = get_tty(linux_tty->index);
96 mutex_lock(&tty->ipw_tty_mutex);
99 mutex_unlock(&tty->ipw_tty_mutex);
102 if (tty->port.count == 0)
103 tty->tx_bytes_queued = 0;
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);
119 static void do_ipw_close(struct ipw_tty *tty)
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;
143 mutex_lock(&tty->ipw_tty_mutex);
144 if (tty->port.count == 0) {
145 mutex_unlock(&tty->ipw_tty_mutex);
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,
165 mutex_lock(&tty->ipw_tty_mutex);
167 if (!tty->port.count) {
168 mutex_unlock(&tty->ipw_tty_mutex);
171 mutex_unlock(&tty->ipw_tty_mutex);
173 work = tty_insert_flip_string(&tty->port, data, length);
176 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
177 ": %d chars not inserted to flip buffer!\n",
181 * This may sleep if ->low_latency is set
184 tty_flip_buffer_push(&tty->port);
187 static void ipw_write_packet_sent_callback(void *callback_data,
188 unsigned int packet_length)
190 struct ipw_tty *tty = callback_data;
193 * Packet has been sent, so we subtract the number of bytes from our
194 * tally of outstanding TX bytes.
196 tty->tx_bytes_queued -= packet_length;
199 static int ipw_write(struct tty_struct *linux_tty,
200 const unsigned char *buf, int count)
202 struct ipw_tty *tty = linux_tty->driver_data;
208 mutex_lock(&tty->ipw_tty_mutex);
209 if (!tty->port.count) {
210 mutex_unlock(&tty->ipw_tty_mutex);
214 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
217 /* Don't allow caller to write any more than we have room for */
222 mutex_unlock(&tty->ipw_tty_mutex);
226 ret = ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS,
228 ipw_write_packet_sent_callback, tty);
230 mutex_unlock(&tty->ipw_tty_mutex);
234 tty->tx_bytes_queued += count;
235 mutex_unlock(&tty->ipw_tty_mutex);
240 static int ipw_write_room(struct tty_struct *linux_tty)
242 struct ipw_tty *tty = linux_tty->driver_data;
245 /* FIXME: Exactly how is the tty object locked here .. */
249 if (!tty->port.count)
252 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
259 static int ipwireless_get_serial_info(struct ipw_tty *tty,
260 struct serial_struct __user *retinfo)
262 struct serial_struct tmp;
267 memset(&tmp, 0, sizeof(tmp));
268 tmp.type = PORT_UNKNOWN;
269 tmp.line = tty->index;
273 tmp.baud_base = 115200;
275 tmp.closing_wait = 0;
276 tmp.custom_divisor = 0;
278 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
284 static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
286 struct ipw_tty *tty = linux_tty->driver_data;
291 if (!tty->port.count)
294 return tty->tx_bytes_queued;
297 static int get_control_lines(struct ipw_tty *tty)
299 unsigned int my = tty->control_lines;
300 unsigned int out = 0;
302 if (my & IPW_CONTROL_LINE_RTS)
304 if (my & IPW_CONTROL_LINE_DTR)
306 if (my & IPW_CONTROL_LINE_CTS)
308 if (my & IPW_CONTROL_LINE_DSR)
310 if (my & IPW_CONTROL_LINE_DCD)
316 static int set_control_lines(struct ipw_tty *tty, unsigned int set,
321 if (set & TIOCM_RTS) {
322 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1);
325 if (tty->secondary_channel_idx != -1) {
326 ret = ipwireless_set_RTS(tty->hardware,
327 tty->secondary_channel_idx, 1);
332 if (set & TIOCM_DTR) {
333 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1);
336 if (tty->secondary_channel_idx != -1) {
337 ret = ipwireless_set_DTR(tty->hardware,
338 tty->secondary_channel_idx, 1);
343 if (clear & TIOCM_RTS) {
344 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 0);
345 if (tty->secondary_channel_idx != -1) {
346 ret = ipwireless_set_RTS(tty->hardware,
347 tty->secondary_channel_idx, 0);
352 if (clear & TIOCM_DTR) {
353 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 0);
354 if (tty->secondary_channel_idx != -1) {
355 ret = ipwireless_set_DTR(tty->hardware,
356 tty->secondary_channel_idx, 0);
364 static int ipw_tiocmget(struct tty_struct *linux_tty)
366 struct ipw_tty *tty = linux_tty->driver_data;
367 /* FIXME: Exactly how is the tty object locked here .. */
372 if (!tty->port.count)
375 return get_control_lines(tty);
379 ipw_tiocmset(struct tty_struct *linux_tty,
380 unsigned int set, unsigned int clear)
382 struct ipw_tty *tty = linux_tty->driver_data;
383 /* FIXME: Exactly how is the tty object locked here .. */
388 if (!tty->port.count)
391 return set_control_lines(tty, set, clear);
394 static int ipw_ioctl(struct tty_struct *linux_tty,
395 unsigned int cmd, unsigned long arg)
397 struct ipw_tty *tty = linux_tty->driver_data;
402 if (!tty->port.count)
405 /* FIXME: Exactly how is the tty object locked here .. */
409 return ipwireless_get_serial_info(tty, (void __user *) arg);
412 return 0; /* Keeps the PCMCIA scripts happy. */
415 if (tty->tty_type == TTYTYPE_MODEM) {
419 int chan = ipwireless_ppp_channel_index(
424 if (put_user(chan, (int __user *) arg))
431 int unit = ipwireless_ppp_unit_number(
436 if (put_user(unit, (int __user *) arg))
445 if (put_user(val, (int __user *) arg))
450 return tty_perform_flush(linux_tty, arg);
456 static int add_tty(int j,
457 struct ipw_hardware *hardware,
458 struct ipw_network *network, int channel_idx,
459 int secondary_channel_idx, int tty_type)
461 ttys[j] = kzalloc(sizeof(struct ipw_tty), GFP_KERNEL);
465 ttys[j]->hardware = hardware;
466 ttys[j]->channel_idx = channel_idx;
467 ttys[j]->secondary_channel_idx = secondary_channel_idx;
468 ttys[j]->network = network;
469 ttys[j]->tty_type = tty_type;
470 mutex_init(&ttys[j]->ipw_tty_mutex);
471 tty_port_init(&ttys[j]->port);
473 tty_port_register_device(&ttys[j]->port, ipw_tty_driver, j, NULL);
474 ipwireless_associate_network_tty(network, channel_idx, ttys[j]);
476 if (secondary_channel_idx != -1)
477 ipwireless_associate_network_tty(network,
478 secondary_channel_idx,
480 /* check if we provide raw device (if loopback is enabled) */
482 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
483 ": registering %s device ttyIPWp%d\n",
484 tty_type_name(tty_type), j);
489 struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware,
490 struct ipw_network *network)
494 for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) {
497 for (j = i; j < IPWIRELESS_PCMCIA_MINORS;
498 j += IPWIRELESS_PCMCIA_MINOR_RANGE)
499 if (ttys[j] != NULL) {
507 if (add_tty(j, hardware, network,
508 IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS,
512 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
513 if (add_tty(j, hardware, network,
514 IPW_CHANNEL_DIALLER, -1,
518 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
519 if (add_tty(j, hardware, network,
531 * Must be called before ipwireless_network_free().
533 void ipwireless_tty_free(struct ipw_tty *tty)
536 struct ipw_network *network = ttys[tty->index]->network;
538 for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS;
539 j += IPWIRELESS_PCMCIA_MINOR_RANGE) {
540 struct ipw_tty *ttyj = ttys[j];
543 mutex_lock(&ttyj->ipw_tty_mutex);
545 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
546 ": deregistering %s device ttyIPWp%d\n",
547 tty_type_name(ttyj->tty_type), j);
549 if (ttyj->port.tty != NULL) {
550 mutex_unlock(&ttyj->ipw_tty_mutex);
551 tty_vhangup(ttyj->port.tty);
552 /* FIXME: Exactly how is the tty object locked here
553 against a parallel ioctl etc */
554 /* FIXME2: hangup does not mean all processes
556 mutex_lock(&ttyj->ipw_tty_mutex);
558 while (ttyj->port.count)
560 ipwireless_disassociate_network_ttys(network,
562 tty_unregister_device(ipw_tty_driver, j);
563 tty_port_destroy(&ttyj->port);
565 mutex_unlock(&ttyj->ipw_tty_mutex);
571 static const struct tty_operations tty_ops = {
574 .hangup = ipw_hangup,
576 .write_room = ipw_write_room,
578 .chars_in_buffer = ipw_chars_in_buffer,
579 .tiocmget = ipw_tiocmget,
580 .tiocmset = ipw_tiocmset,
583 int ipwireless_tty_init(void)
587 ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
591 ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME;
592 ipw_tty_driver->name = "ttyIPWp";
593 ipw_tty_driver->major = 0;
594 ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START;
595 ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
596 ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL;
597 ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
598 ipw_tty_driver->init_termios = tty_std_termios;
599 ipw_tty_driver->init_termios.c_cflag =
600 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
601 ipw_tty_driver->init_termios.c_ispeed = 9600;
602 ipw_tty_driver->init_termios.c_ospeed = 9600;
603 tty_set_operations(ipw_tty_driver, &tty_ops);
604 result = tty_register_driver(ipw_tty_driver);
606 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
607 ": failed to register tty driver\n");
608 put_tty_driver(ipw_tty_driver);
615 void ipwireless_tty_release(void)
619 ret = tty_unregister_driver(ipw_tty_driver);
620 put_tty_driver(ipw_tty_driver);
622 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
623 ": tty_unregister_driver failed with code %d\n", ret);
626 int ipwireless_tty_is_modem(struct ipw_tty *tty)
628 return tty->tty_type == TTYTYPE_MODEM;
632 ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
633 unsigned int channel_idx,
634 unsigned int control_lines,
635 unsigned int changed_mask)
637 unsigned int old_control_lines = tty->control_lines;
639 tty->control_lines = (tty->control_lines & ~changed_mask)
640 | (control_lines & changed_mask);
643 * If DCD is de-asserted, we close the tty so pppd can tell that we
646 if ((old_control_lines & IPW_CONTROL_LINE_DCD)
647 && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
649 tty_hangup(tty->port.tty);