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/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/ppp_defs.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>
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)
48 struct ipw_hardware *hardware;
49 unsigned int channel_idx;
50 unsigned int secondary_channel_idx;
52 struct ipw_network *network;
53 unsigned int control_lines;
54 struct mutex ipw_tty_mutex;
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[] = {
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
79 * Number of minor starts with 16 (_RANGE * _RAS_RAW).
81 if (!ipwireless_loopback && index >=
82 IPWIRELESS_PCMCIA_MINOR_RANGE * TTYTYPE_RAS_RAW)
88 static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
90 struct ipw_tty *tty = get_tty(linux_tty->index);
95 mutex_lock(&tty->ipw_tty_mutex);
98 mutex_unlock(&tty->ipw_tty_mutex);
101 if (tty->port.count == 0)
102 tty->tx_bytes_queued = 0;
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);
118 static void do_ipw_close(struct ipw_tty *tty)
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;
142 mutex_lock(&tty->ipw_tty_mutex);
143 if (tty->port.count == 0) {
144 mutex_unlock(&tty->ipw_tty_mutex);
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,
164 mutex_lock(&tty->ipw_tty_mutex);
166 if (!tty->port.count) {
167 mutex_unlock(&tty->ipw_tty_mutex);
170 mutex_unlock(&tty->ipw_tty_mutex);
172 work = tty_insert_flip_string(&tty->port, data, length);
175 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
176 ": %d chars not inserted to flip buffer!\n",
180 * This may sleep if ->low_latency is set
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;
207 mutex_lock(&tty->ipw_tty_mutex);
208 if (!tty->port.count) {
209 mutex_unlock(&tty->ipw_tty_mutex);
213 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
216 /* Don't allow caller to write any more than we have room for */
221 mutex_unlock(&tty->ipw_tty_mutex);
225 ret = ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS,
227 ipw_write_packet_sent_callback, tty);
229 mutex_unlock(&tty->ipw_tty_mutex);
233 tty->tx_bytes_queued += count;
234 mutex_unlock(&tty->ipw_tty_mutex);
239 static int ipw_write_room(struct tty_struct *linux_tty)
241 struct ipw_tty *tty = linux_tty->driver_data;
244 /* FIXME: Exactly how is the tty object locked here .. */
248 if (!tty->port.count)
251 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
258 static int ipwireless_get_serial_info(struct ipw_tty *tty,
259 struct serial_struct __user *retinfo)
261 struct serial_struct tmp;
266 memset(&tmp, 0, sizeof(tmp));
267 tmp.type = PORT_UNKNOWN;
268 tmp.line = tty->index;
272 tmp.baud_base = 115200;
274 tmp.closing_wait = 0;
275 tmp.custom_divisor = 0;
277 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
283 static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
285 struct ipw_tty *tty = linux_tty->driver_data;
290 if (!tty->port.count)
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)
303 if (my & IPW_CONTROL_LINE_DTR)
305 if (my & IPW_CONTROL_LINE_CTS)
307 if (my & IPW_CONTROL_LINE_DSR)
309 if (my & IPW_CONTROL_LINE_DCD)
315 static int set_control_lines(struct ipw_tty *tty, unsigned int set,
320 if (set & TIOCM_RTS) {
321 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1);
324 if (tty->secondary_channel_idx != -1) {
325 ret = ipwireless_set_RTS(tty->hardware,
326 tty->secondary_channel_idx, 1);
331 if (set & TIOCM_DTR) {
332 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1);
335 if (tty->secondary_channel_idx != -1) {
336 ret = ipwireless_set_DTR(tty->hardware,
337 tty->secondary_channel_idx, 1);
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);
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);
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 .. */
371 if (!tty->port.count)
374 return get_control_lines(tty);
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 .. */
387 if (!tty->port.count)
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;
401 if (!tty->port.count)
404 /* FIXME: Exactly how is the tty object locked here .. */
408 return ipwireless_get_serial_info(tty, (void __user *) arg);
411 return 0; /* Keeps the PCMCIA scripts happy. */
414 if (tty->tty_type == TTYTYPE_MODEM) {
418 int chan = ipwireless_ppp_channel_index(
423 if (put_user(chan, (int __user *) arg))
430 int unit = ipwireless_ppp_unit_number(
435 if (put_user(unit, (int __user *) arg))
444 if (put_user(val, (int __user *) arg))
449 return tty_perform_flush(linux_tty, arg);
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);
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,
479 /* check if we provide raw device (if loopback is enabled) */
481 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
482 ": registering %s device ttyIPWp%d\n",
483 tty_type_name(tty_type), j);
488 struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware,
489 struct ipw_network *network)
493 for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) {
496 for (j = i; j < IPWIRELESS_PCMCIA_MINORS;
497 j += IPWIRELESS_PCMCIA_MINOR_RANGE)
498 if (ttys[j] != NULL) {
506 if (add_tty(j, hardware, network,
507 IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS,
511 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
512 if (add_tty(j, hardware, network,
513 IPW_CHANNEL_DIALLER, -1,
517 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
518 if (add_tty(j, hardware, network,
530 * Must be called before ipwireless_network_free().
532 void ipwireless_tty_free(struct ipw_tty *tty)
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];
542 mutex_lock(&ttyj->ipw_tty_mutex);
544 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
545 ": deregistering %s device ttyIPWp%d\n",
546 tty_type_name(ttyj->tty_type), j);
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
555 mutex_lock(&ttyj->ipw_tty_mutex);
557 while (ttyj->port.count)
559 ipwireless_disassociate_network_ttys(network,
561 tty_unregister_device(ipw_tty_driver, j);
562 tty_port_destroy(&ttyj->port);
564 mutex_unlock(&ttyj->ipw_tty_mutex);
570 static const struct tty_operations tty_ops = {
573 .hangup = ipw_hangup,
575 .write_room = ipw_write_room,
577 .chars_in_buffer = ipw_chars_in_buffer,
578 .tiocmget = ipw_tiocmget,
579 .tiocmset = ipw_tiocmset,
582 int ipwireless_tty_init(void)
586 ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
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);
605 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
606 ": failed to register tty driver\n");
607 put_tty_driver(ipw_tty_driver);
614 void ipwireless_tty_release(void)
618 ret = tty_unregister_driver(ipw_tty_driver);
619 put_tty_driver(ipw_tty_driver);
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;
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
645 if ((old_control_lines & IPW_CONTROL_LINE_DCD)
646 && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
648 tty_hangup(tty->port.tty);