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)
48 struct ipw_hardware
*hardware
;
49 unsigned int channel_idx
;
50 unsigned int secondary_channel_idx
;
52 struct ipw_network
*network
;
53 struct tty_struct
*linux_tty
;
55 unsigned int control_lines
;
56 struct mutex ipw_tty_mutex
;
61 static struct ipw_tty
*ttys
[IPWIRELESS_PCMCIA_MINORS
];
63 static struct tty_driver
*ipw_tty_driver
;
65 static char *tty_type_name(int tty_type
)
67 static char *channel_names
[] = {
73 return channel_names
[tty_type
];
76 static void report_registering(struct ipw_tty
*tty
)
78 char *iftype
= tty_type_name(tty
->tty_type
);
80 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
81 ": registering %s device ttyIPWp%d\n", iftype
, tty
->index
);
84 static void report_deregistering(struct ipw_tty
*tty
)
86 char *iftype
= tty_type_name(tty
->tty_type
);
88 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
89 ": deregistering %s device ttyIPWp%d\n", iftype
,
93 static struct ipw_tty
*get_tty(int index
)
96 * The 'ras_raw' channel is only available when 'loopback' mode
98 * Number of minor starts with 16 (_RANGE * _RAS_RAW).
100 if (!ipwireless_loopback
&& index
>=
101 IPWIRELESS_PCMCIA_MINOR_RANGE
* TTYTYPE_RAS_RAW
)
107 static int ipw_open(struct tty_struct
*linux_tty
, struct file
*filp
)
109 struct ipw_tty
*tty
= get_tty(linux_tty
->index
);
114 mutex_lock(&tty
->ipw_tty_mutex
);
117 mutex_unlock(&tty
->ipw_tty_mutex
);
120 if (tty
->open_count
== 0)
121 tty
->tx_bytes_queued
= 0;
125 tty
->linux_tty
= linux_tty
;
126 linux_tty
->driver_data
= tty
;
127 linux_tty
->low_latency
= 1;
129 if (tty
->tty_type
== TTYTYPE_MODEM
)
130 ipwireless_ppp_open(tty
->network
);
132 mutex_unlock(&tty
->ipw_tty_mutex
);
137 static void do_ipw_close(struct ipw_tty
*tty
)
141 if (tty
->open_count
== 0) {
142 struct tty_struct
*linux_tty
= tty
->linux_tty
;
144 if (linux_tty
!= NULL
) {
145 tty
->linux_tty
= NULL
;
146 linux_tty
->driver_data
= NULL
;
148 if (tty
->tty_type
== TTYTYPE_MODEM
)
149 ipwireless_ppp_close(tty
->network
);
154 static void ipw_hangup(struct tty_struct
*linux_tty
)
156 struct ipw_tty
*tty
= linux_tty
->driver_data
;
161 mutex_lock(&tty
->ipw_tty_mutex
);
162 if (tty
->open_count
== 0) {
163 mutex_unlock(&tty
->ipw_tty_mutex
);
169 mutex_unlock(&tty
->ipw_tty_mutex
);
172 static void ipw_close(struct tty_struct
*linux_tty
, struct file
*filp
)
174 ipw_hangup(linux_tty
);
177 /* Take data received from hardware, and send it out the tty */
178 void ipwireless_tty_received(struct ipw_tty
*tty
, unsigned char *data
,
181 struct tty_struct
*linux_tty
;
184 mutex_lock(&tty
->ipw_tty_mutex
);
185 linux_tty
= tty
->linux_tty
;
186 if (linux_tty
== NULL
) {
187 mutex_unlock(&tty
->ipw_tty_mutex
);
191 if (!tty
->open_count
) {
192 mutex_unlock(&tty
->ipw_tty_mutex
);
195 mutex_unlock(&tty
->ipw_tty_mutex
);
197 work
= tty_insert_flip_string(linux_tty
, data
, length
);
200 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
201 ": %d chars not inserted to flip buffer!\n",
205 * This may sleep if ->low_latency is set
208 tty_flip_buffer_push(linux_tty
);
211 static void ipw_write_packet_sent_callback(void *callback_data
,
212 unsigned int packet_length
)
214 struct ipw_tty
*tty
= callback_data
;
217 * Packet has been sent, so we subtract the number of bytes from our
218 * tally of outstanding TX bytes.
220 tty
->tx_bytes_queued
-= packet_length
;
223 static int ipw_write(struct tty_struct
*linux_tty
,
224 const unsigned char *buf
, int count
)
226 struct ipw_tty
*tty
= linux_tty
->driver_data
;
232 mutex_lock(&tty
->ipw_tty_mutex
);
233 if (!tty
->open_count
) {
234 mutex_unlock(&tty
->ipw_tty_mutex
);
238 room
= IPWIRELESS_TX_QUEUE_SIZE
- tty
->tx_bytes_queued
;
241 /* Don't allow caller to write any more than we have room for */
246 mutex_unlock(&tty
->ipw_tty_mutex
);
250 ret
= ipwireless_send_packet(tty
->hardware
, IPW_CHANNEL_RAS
,
252 ipw_write_packet_sent_callback
, tty
);
254 mutex_unlock(&tty
->ipw_tty_mutex
);
258 tty
->tx_bytes_queued
+= count
;
259 mutex_unlock(&tty
->ipw_tty_mutex
);
264 static int ipw_write_room(struct tty_struct
*linux_tty
)
266 struct ipw_tty
*tty
= linux_tty
->driver_data
;
269 /* FIXME: Exactly how is the tty object locked here .. */
273 if (!tty
->open_count
)
276 room
= IPWIRELESS_TX_QUEUE_SIZE
- tty
->tx_bytes_queued
;
283 static int ipwireless_get_serial_info(struct ipw_tty
*tty
,
284 struct serial_struct __user
*retinfo
)
286 struct serial_struct tmp
;
291 memset(&tmp
, 0, sizeof(tmp
));
292 tmp
.type
= PORT_UNKNOWN
;
293 tmp
.line
= tty
->index
;
297 tmp
.baud_base
= 115200;
299 tmp
.closing_wait
= 0;
300 tmp
.custom_divisor
= 0;
302 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
308 static int ipw_chars_in_buffer(struct tty_struct
*linux_tty
)
310 struct ipw_tty
*tty
= linux_tty
->driver_data
;
315 if (!tty
->open_count
)
318 return tty
->tx_bytes_queued
;
321 static int get_control_lines(struct ipw_tty
*tty
)
323 unsigned int my
= tty
->control_lines
;
324 unsigned int out
= 0;
326 if (my
& IPW_CONTROL_LINE_RTS
)
328 if (my
& IPW_CONTROL_LINE_DTR
)
330 if (my
& IPW_CONTROL_LINE_CTS
)
332 if (my
& IPW_CONTROL_LINE_DSR
)
334 if (my
& IPW_CONTROL_LINE_DCD
)
340 static int set_control_lines(struct ipw_tty
*tty
, unsigned int set
,
345 if (set
& TIOCM_RTS
) {
346 ret
= ipwireless_set_RTS(tty
->hardware
, tty
->channel_idx
, 1);
349 if (tty
->secondary_channel_idx
!= -1) {
350 ret
= ipwireless_set_RTS(tty
->hardware
,
351 tty
->secondary_channel_idx
, 1);
356 if (set
& TIOCM_DTR
) {
357 ret
= ipwireless_set_DTR(tty
->hardware
, tty
->channel_idx
, 1);
360 if (tty
->secondary_channel_idx
!= -1) {
361 ret
= ipwireless_set_DTR(tty
->hardware
,
362 tty
->secondary_channel_idx
, 1);
367 if (clear
& TIOCM_RTS
) {
368 ret
= ipwireless_set_RTS(tty
->hardware
, tty
->channel_idx
, 0);
369 if (tty
->secondary_channel_idx
!= -1) {
370 ret
= ipwireless_set_RTS(tty
->hardware
,
371 tty
->secondary_channel_idx
, 0);
376 if (clear
& TIOCM_DTR
) {
377 ret
= ipwireless_set_DTR(tty
->hardware
, tty
->channel_idx
, 0);
378 if (tty
->secondary_channel_idx
!= -1) {
379 ret
= ipwireless_set_DTR(tty
->hardware
,
380 tty
->secondary_channel_idx
, 0);
388 static int ipw_tiocmget(struct tty_struct
*linux_tty
)
390 struct ipw_tty
*tty
= linux_tty
->driver_data
;
391 /* FIXME: Exactly how is the tty object locked here .. */
396 if (!tty
->open_count
)
399 return get_control_lines(tty
);
403 ipw_tiocmset(struct tty_struct
*linux_tty
,
404 unsigned int set
, unsigned int clear
)
406 struct ipw_tty
*tty
= linux_tty
->driver_data
;
407 /* FIXME: Exactly how is the tty object locked here .. */
412 if (!tty
->open_count
)
415 return set_control_lines(tty
, set
, clear
);
418 static int ipw_ioctl(struct tty_struct
*linux_tty
,
419 unsigned int cmd
, unsigned long arg
)
421 struct ipw_tty
*tty
= linux_tty
->driver_data
;
426 if (!tty
->open_count
)
429 /* FIXME: Exactly how is the tty object locked here .. */
433 return ipwireless_get_serial_info(tty
, (void __user
*) arg
);
436 return 0; /* Keeps the PCMCIA scripts happy. */
439 if (tty
->tty_type
== TTYTYPE_MODEM
) {
443 int chan
= ipwireless_ppp_channel_index(
448 if (put_user(chan
, (int __user
*) arg
))
455 int unit
= ipwireless_ppp_unit_number(
460 if (put_user(unit
, (int __user
*) arg
))
469 if (put_user(val
, (int __user
*) arg
))
474 return tty_perform_flush(linux_tty
, arg
);
480 static int add_tty(int j
,
481 struct ipw_hardware
*hardware
,
482 struct ipw_network
*network
, int channel_idx
,
483 int secondary_channel_idx
, int tty_type
)
485 ttys
[j
] = kzalloc(sizeof(struct ipw_tty
), GFP_KERNEL
);
489 ttys
[j
]->hardware
= hardware
;
490 ttys
[j
]->channel_idx
= channel_idx
;
491 ttys
[j
]->secondary_channel_idx
= secondary_channel_idx
;
492 ttys
[j
]->network
= network
;
493 ttys
[j
]->tty_type
= tty_type
;
494 mutex_init(&ttys
[j
]->ipw_tty_mutex
);
496 tty_register_device(ipw_tty_driver
, j
, NULL
);
497 ipwireless_associate_network_tty(network
, channel_idx
, ttys
[j
]);
499 if (secondary_channel_idx
!= -1)
500 ipwireless_associate_network_tty(network
,
501 secondary_channel_idx
,
503 if (get_tty(j
) == ttys
[j
])
504 report_registering(ttys
[j
]);
508 struct ipw_tty
*ipwireless_tty_create(struct ipw_hardware
*hardware
,
509 struct ipw_network
*network
)
513 for (i
= 0; i
< IPWIRELESS_PCMCIA_MINOR_RANGE
; i
++) {
516 for (j
= i
; j
< IPWIRELESS_PCMCIA_MINORS
;
517 j
+= IPWIRELESS_PCMCIA_MINOR_RANGE
)
518 if (ttys
[j
] != NULL
) {
526 if (add_tty(j
, hardware
, network
,
527 IPW_CHANNEL_DIALLER
, IPW_CHANNEL_RAS
,
531 j
+= IPWIRELESS_PCMCIA_MINOR_RANGE
;
532 if (add_tty(j
, hardware
, network
,
533 IPW_CHANNEL_DIALLER
, -1,
537 j
+= IPWIRELESS_PCMCIA_MINOR_RANGE
;
538 if (add_tty(j
, hardware
, network
,
550 * Must be called before ipwireless_network_free().
552 void ipwireless_tty_free(struct ipw_tty
*tty
)
555 struct ipw_network
*network
= ttys
[tty
->index
]->network
;
557 for (j
= tty
->index
; j
< IPWIRELESS_PCMCIA_MINORS
;
558 j
+= IPWIRELESS_PCMCIA_MINOR_RANGE
) {
559 struct ipw_tty
*ttyj
= ttys
[j
];
562 mutex_lock(&ttyj
->ipw_tty_mutex
);
563 if (get_tty(j
) == ttyj
)
564 report_deregistering(ttyj
);
566 if (ttyj
->linux_tty
!= NULL
) {
567 mutex_unlock(&ttyj
->ipw_tty_mutex
);
568 tty_hangup(ttyj
->linux_tty
);
569 /* Wait till the tty_hangup has completed */
570 flush_work_sync(&ttyj
->linux_tty
->hangup_work
);
571 /* FIXME: Exactly how is the tty object locked here
572 against a parallel ioctl etc */
573 mutex_lock(&ttyj
->ipw_tty_mutex
);
575 while (ttyj
->open_count
)
577 ipwireless_disassociate_network_ttys(network
,
579 tty_unregister_device(ipw_tty_driver
, j
);
581 mutex_unlock(&ttyj
->ipw_tty_mutex
);
587 static const struct tty_operations tty_ops
= {
590 .hangup
= ipw_hangup
,
592 .write_room
= ipw_write_room
,
594 .chars_in_buffer
= ipw_chars_in_buffer
,
595 .tiocmget
= ipw_tiocmget
,
596 .tiocmset
= ipw_tiocmset
,
599 int ipwireless_tty_init(void)
603 ipw_tty_driver
= alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS
);
607 ipw_tty_driver
->driver_name
= IPWIRELESS_PCCARD_NAME
;
608 ipw_tty_driver
->name
= "ttyIPWp";
609 ipw_tty_driver
->major
= 0;
610 ipw_tty_driver
->minor_start
= IPWIRELESS_PCMCIA_START
;
611 ipw_tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
612 ipw_tty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
613 ipw_tty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
614 ipw_tty_driver
->init_termios
= tty_std_termios
;
615 ipw_tty_driver
->init_termios
.c_cflag
=
616 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
617 ipw_tty_driver
->init_termios
.c_ispeed
= 9600;
618 ipw_tty_driver
->init_termios
.c_ospeed
= 9600;
619 tty_set_operations(ipw_tty_driver
, &tty_ops
);
620 result
= tty_register_driver(ipw_tty_driver
);
622 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
623 ": failed to register tty driver\n");
624 put_tty_driver(ipw_tty_driver
);
631 void ipwireless_tty_release(void)
635 ret
= tty_unregister_driver(ipw_tty_driver
);
636 put_tty_driver(ipw_tty_driver
);
638 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
639 ": tty_unregister_driver failed with code %d\n", ret
);
642 int ipwireless_tty_is_modem(struct ipw_tty
*tty
)
644 return tty
->tty_type
== TTYTYPE_MODEM
;
648 ipwireless_tty_notify_control_line_change(struct ipw_tty
*tty
,
649 unsigned int channel_idx
,
650 unsigned int control_lines
,
651 unsigned int changed_mask
)
653 unsigned int old_control_lines
= tty
->control_lines
;
655 tty
->control_lines
= (tty
->control_lines
& ~changed_mask
)
656 | (control_lines
& changed_mask
);
659 * If DCD is de-asserted, we close the tty so pppd can tell that we
662 if ((old_control_lines
& IPW_CONTROL_LINE_DCD
)
663 && !(tty
->control_lines
& IPW_CONTROL_LINE_DCD
)
665 tty_hangup(tty
->linux_tty
);