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 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
;
205 mutex_lock(&tty
->ipw_tty_mutex
);
206 if (!tty
->port
.count
) {
207 mutex_unlock(&tty
->ipw_tty_mutex
);
211 room
= IPWIRELESS_TX_QUEUE_SIZE
- tty
->tx_bytes_queued
;
214 /* Don't allow caller to write any more than we have room for */
219 mutex_unlock(&tty
->ipw_tty_mutex
);
223 ret
= ipwireless_send_packet(tty
->hardware
, IPW_CHANNEL_RAS
,
225 ipw_write_packet_sent_callback
, tty
);
227 mutex_unlock(&tty
->ipw_tty_mutex
);
231 tty
->tx_bytes_queued
+= count
;
232 mutex_unlock(&tty
->ipw_tty_mutex
);
237 static int ipw_write_room(struct tty_struct
*linux_tty
)
239 struct ipw_tty
*tty
= linux_tty
->driver_data
;
242 /* FIXME: Exactly how is the tty object locked here .. */
246 if (!tty
->port
.count
)
249 room
= IPWIRELESS_TX_QUEUE_SIZE
- tty
->tx_bytes_queued
;
256 static int ipwireless_get_serial_info(struct ipw_tty
*tty
,
257 struct serial_struct __user
*retinfo
)
259 struct serial_struct tmp
;
264 memset(&tmp
, 0, sizeof(tmp
));
265 tmp
.type
= PORT_UNKNOWN
;
266 tmp
.line
= tty
->index
;
270 tmp
.baud_base
= 115200;
272 tmp
.closing_wait
= 0;
273 tmp
.custom_divisor
= 0;
275 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
281 static int ipw_chars_in_buffer(struct tty_struct
*linux_tty
)
283 struct ipw_tty
*tty
= linux_tty
->driver_data
;
288 if (!tty
->port
.count
)
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
)
301 if (my
& IPW_CONTROL_LINE_DTR
)
303 if (my
& IPW_CONTROL_LINE_CTS
)
305 if (my
& IPW_CONTROL_LINE_DSR
)
307 if (my
& IPW_CONTROL_LINE_DCD
)
313 static int set_control_lines(struct ipw_tty
*tty
, unsigned int set
,
318 if (set
& TIOCM_RTS
) {
319 ret
= ipwireless_set_RTS(tty
->hardware
, tty
->channel_idx
, 1);
322 if (tty
->secondary_channel_idx
!= -1) {
323 ret
= ipwireless_set_RTS(tty
->hardware
,
324 tty
->secondary_channel_idx
, 1);
329 if (set
& TIOCM_DTR
) {
330 ret
= ipwireless_set_DTR(tty
->hardware
, tty
->channel_idx
, 1);
333 if (tty
->secondary_channel_idx
!= -1) {
334 ret
= ipwireless_set_DTR(tty
->hardware
,
335 tty
->secondary_channel_idx
, 1);
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);
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);
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 .. */
369 if (!tty
->port
.count
)
372 return get_control_lines(tty
);
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 .. */
385 if (!tty
->port
.count
)
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
;
399 if (!tty
->port
.count
)
402 /* FIXME: Exactly how is the tty object locked here .. */
406 return ipwireless_get_serial_info(tty
, (void __user
*) arg
);
409 return 0; /* Keeps the PCMCIA scripts happy. */
412 if (tty
->tty_type
== TTYTYPE_MODEM
) {
416 int chan
= ipwireless_ppp_channel_index(
421 if (put_user(chan
, (int __user
*) arg
))
428 int unit
= ipwireless_ppp_unit_number(
433 if (put_user(unit
, (int __user
*) arg
))
442 if (put_user(val
, (int __user
*) arg
))
447 return tty_perform_flush(linux_tty
, arg
);
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
);
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
,
477 /* check if we provide raw device (if loopback is enabled) */
479 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
480 ": registering %s device ttyIPWp%d\n",
481 tty_type_name(tty_type
), j
);
486 struct ipw_tty
*ipwireless_tty_create(struct ipw_hardware
*hardware
,
487 struct ipw_network
*network
)
491 for (i
= 0; i
< IPWIRELESS_PCMCIA_MINOR_RANGE
; i
++) {
494 for (j
= i
; j
< IPWIRELESS_PCMCIA_MINORS
;
495 j
+= IPWIRELESS_PCMCIA_MINOR_RANGE
)
496 if (ttys
[j
] != NULL
) {
504 if (add_tty(j
, hardware
, network
,
505 IPW_CHANNEL_DIALLER
, IPW_CHANNEL_RAS
,
509 j
+= IPWIRELESS_PCMCIA_MINOR_RANGE
;
510 if (add_tty(j
, hardware
, network
,
511 IPW_CHANNEL_DIALLER
, -1,
515 j
+= IPWIRELESS_PCMCIA_MINOR_RANGE
;
516 if (add_tty(j
, hardware
, network
,
528 * Must be called before ipwireless_network_free().
530 void ipwireless_tty_free(struct ipw_tty
*tty
)
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
];
540 mutex_lock(&ttyj
->ipw_tty_mutex
);
542 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
543 ": deregistering %s device ttyIPWp%d\n",
544 tty_type_name(ttyj
->tty_type
), j
);
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
553 mutex_lock(&ttyj
->ipw_tty_mutex
);
555 while (ttyj
->port
.count
)
557 ipwireless_disassociate_network_ttys(network
,
559 tty_unregister_device(ipw_tty_driver
, j
);
560 tty_port_destroy(&ttyj
->port
);
562 mutex_unlock(&ttyj
->ipw_tty_mutex
);
568 static const struct tty_operations tty_ops
= {
571 .hangup
= ipw_hangup
,
573 .write_room
= ipw_write_room
,
575 .chars_in_buffer
= ipw_chars_in_buffer
,
576 .tiocmget
= ipw_tiocmget
,
577 .tiocmset
= ipw_tiocmset
,
580 int ipwireless_tty_init(void)
584 ipw_tty_driver
= alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS
);
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
);
603 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
604 ": failed to register tty driver\n");
605 put_tty_driver(ipw_tty_driver
);
612 void ipwireless_tty_release(void)
616 ret
= tty_unregister_driver(ipw_tty_driver
);
617 put_tty_driver(ipw_tty_driver
);
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
;
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
643 if ((old_control_lines
& IPW_CONTROL_LINE_DCD
)
644 && !(tty
->control_lines
& IPW_CONTROL_LINE_DCD
)
646 tty_hangup(tty
->port
.tty
);