1 // SPDX-License-Identifier: GPL-2.0
3 * IPWireless 3G PCMCIA Network Driver
6 * by Stephen Blackheath <stephen@blacksapphire.com>,
7 * Ben Martel <benm@symmetric.co.nz>
9 * Copyrighted as follows:
10 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
12 * Various driver changes and rewrites, port to new kernels
13 * Copyright (C) 2006-2007 Jiri Kosina
15 * Misc code cleanups and updates
16 * Copyright (C) 2007 David Sterba
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/mutex.h>
22 #include <linux/netdevice.h>
23 #include <linux/ppp_channel.h>
24 #include <linux/ppp_defs.h>
25 #include <linux/slab.h>
26 #include <linux/ppp-ioctl.h>
27 #include <linux/skbuff.h>
34 #define MAX_ASSOCIATED_TTYS 2
36 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
39 /* Hardware context, used for calls to hardware layer. */
40 struct ipw_hardware
*hardware
;
41 /* Context for kernel 'generic_ppp' functionality */
42 struct ppp_channel
*ppp_channel
;
43 /* tty context connected with IPW console */
44 struct ipw_tty
*associated_ttys
[NO_OF_IPW_CHANNELS
][MAX_ASSOCIATED_TTYS
];
45 /* True if ppp needs waking up once we're ready to xmit */
47 /* Number of packets queued up in hardware module. */
48 int outgoing_packets_queued
;
49 /* Spinlock to avoid interrupts during shutdown */
51 struct mutex close_lock
;
53 /* PPP ioctl data, not actually used anywere */
61 unsigned int ras_control_lines
;
63 struct work_struct work_go_online
;
64 struct work_struct work_go_offline
;
67 static void notify_packet_sent(void *callback_data
, unsigned int packet_length
)
69 struct ipw_network
*network
= callback_data
;
72 spin_lock_irqsave(&network
->lock
, flags
);
73 network
->outgoing_packets_queued
--;
74 if (network
->ppp_channel
!= NULL
) {
75 if (network
->ppp_blocked
) {
76 network
->ppp_blocked
= 0;
77 spin_unlock_irqrestore(&network
->lock
, flags
);
78 ppp_output_wakeup(network
->ppp_channel
);
80 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
83 spin_unlock_irqrestore(&network
->lock
, flags
);
85 spin_unlock_irqrestore(&network
->lock
, flags
);
89 * Called by the ppp system when it has a packet to send to the hardware.
91 static int ipwireless_ppp_start_xmit(struct ppp_channel
*ppp_channel
,
94 struct ipw_network
*network
= ppp_channel
->private;
97 spin_lock_irqsave(&network
->lock
, flags
);
98 if (network
->outgoing_packets_queued
< ipwireless_out_queue
) {
100 static unsigned char header
[] = {
101 PPP_ALLSTATIONS
, /* 0xff */
106 network
->outgoing_packets_queued
++;
107 spin_unlock_irqrestore(&network
->lock
, flags
);
110 * If we have the requested amount of headroom in the skb we
111 * were handed, then we can add the header efficiently.
113 if (skb_headroom(skb
) >= 2) {
114 memcpy(skb_push(skb
, 2), header
, 2);
115 ret
= ipwireless_send_packet(network
->hardware
,
116 IPW_CHANNEL_RAS
, skb
->data
,
125 /* Otherwise (rarely) we do it inefficiently. */
126 buf
= kmalloc(skb
->len
+ 2, GFP_ATOMIC
);
129 memcpy(buf
+ 2, skb
->data
, skb
->len
);
130 memcpy(buf
, header
, 2);
131 ret
= ipwireless_send_packet(network
->hardware
,
132 IPW_CHANNEL_RAS
, buf
,
144 * Otherwise reject the packet, and flag that the ppp system
145 * needs to be unblocked once we are ready to send.
147 network
->ppp_blocked
= 1;
148 spin_unlock_irqrestore(&network
->lock
, flags
);
149 if (ipwireless_debug
)
150 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
": ppp blocked\n");
155 /* Handle an ioctl call that has come in via ppp. (copy of ppp_async_ioctl() */
156 static int ipwireless_ppp_ioctl(struct ppp_channel
*ppp_channel
,
157 unsigned int cmd
, unsigned long arg
)
159 struct ipw_network
*network
= ppp_channel
->private;
162 int __user
*user_arg
= (int __user
*) arg
;
167 val
= network
->flags
| network
->rbits
;
168 if (put_user(val
, user_arg
))
174 if (get_user(val
, user_arg
))
176 network
->flags
= val
& ~SC_RCV_BITS
;
177 network
->rbits
= val
& SC_RCV_BITS
;
181 case PPPIOCGASYNCMAP
:
182 if (put_user(network
->xaccm
[0], user_arg
))
187 case PPPIOCSASYNCMAP
:
188 if (get_user(network
->xaccm
[0], user_arg
))
193 case PPPIOCGRASYNCMAP
:
194 if (put_user(network
->raccm
, user_arg
))
199 case PPPIOCSRASYNCMAP
:
200 if (get_user(network
->raccm
, user_arg
))
205 case PPPIOCGXASYNCMAP
:
206 if (copy_to_user((void __user
*) arg
, network
->xaccm
,
207 sizeof(network
->xaccm
)))
212 case PPPIOCSXASYNCMAP
:
213 if (copy_from_user(accm
, (void __user
*) arg
, sizeof(accm
)))
215 accm
[2] &= ~0x40000000U
; /* can't escape 0x5e */
216 accm
[3] |= 0x60000000U
; /* must escape 0x7d, 0x7e */
217 memcpy(network
->xaccm
, accm
, sizeof(network
->xaccm
));
222 if (put_user(network
->mru
, user_arg
))
228 if (get_user(val
, user_arg
))
243 static const struct ppp_channel_ops ipwireless_ppp_channel_ops
= {
244 .start_xmit
= ipwireless_ppp_start_xmit
,
245 .ioctl
= ipwireless_ppp_ioctl
248 static void do_go_online(struct work_struct
*work_go_online
)
250 struct ipw_network
*network
=
251 container_of(work_go_online
, struct ipw_network
,
255 spin_lock_irqsave(&network
->lock
, flags
);
256 if (!network
->ppp_channel
) {
257 struct ppp_channel
*channel
;
259 spin_unlock_irqrestore(&network
->lock
, flags
);
260 channel
= kzalloc(sizeof(struct ppp_channel
), GFP_KERNEL
);
262 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
263 ": unable to allocate PPP channel\n");
266 channel
->private = network
;
267 channel
->mtu
= 16384; /* Wild guess */
269 channel
->ops
= &ipwireless_ppp_channel_ops
;
273 network
->mru
= PPP_MRU
;
274 memset(network
->xaccm
, 0, sizeof(network
->xaccm
));
275 network
->xaccm
[0] = ~0U;
276 network
->xaccm
[3] = 0x60000000U
;
277 network
->raccm
= ~0U;
278 if (ppp_register_channel(channel
) < 0) {
279 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
280 ": unable to register PPP channel\n");
284 spin_lock_irqsave(&network
->lock
, flags
);
285 network
->ppp_channel
= channel
;
287 spin_unlock_irqrestore(&network
->lock
, flags
);
290 static void do_go_offline(struct work_struct
*work_go_offline
)
292 struct ipw_network
*network
=
293 container_of(work_go_offline
, struct ipw_network
,
297 mutex_lock(&network
->close_lock
);
298 spin_lock_irqsave(&network
->lock
, flags
);
299 if (network
->ppp_channel
!= NULL
) {
300 struct ppp_channel
*channel
= network
->ppp_channel
;
302 network
->ppp_channel
= NULL
;
303 spin_unlock_irqrestore(&network
->lock
, flags
);
304 mutex_unlock(&network
->close_lock
);
305 ppp_unregister_channel(channel
);
307 spin_unlock_irqrestore(&network
->lock
, flags
);
308 mutex_unlock(&network
->close_lock
);
312 void ipwireless_network_notify_control_line_change(struct ipw_network
*network
,
313 unsigned int channel_idx
,
314 unsigned int control_lines
,
315 unsigned int changed_mask
)
319 if (channel_idx
== IPW_CHANNEL_RAS
)
320 network
->ras_control_lines
= control_lines
;
322 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++) {
323 struct ipw_tty
*tty
=
324 network
->associated_ttys
[channel_idx
][i
];
327 * If it's associated with a tty (other than the RAS channel
328 * when we're online), then send the data to that tty. The RAS
329 * channel's data is handled above - it always goes through
333 ipwireless_tty_notify_control_line_change(tty
,
341 * Some versions of firmware stuff packets with 0xff 0x03 (PPP: ALLSTATIONS, UI)
342 * bytes, which are required on sent packet, but not always present on received
345 static struct sk_buff
*ipw_packet_received_skb(unsigned char *data
,
350 if (length
> 2 && data
[0] == PPP_ALLSTATIONS
&& data
[1] == PPP_UI
) {
355 skb
= dev_alloc_skb(length
+ 4);
359 skb_put_data(skb
, data
, length
);
364 void ipwireless_network_packet_received(struct ipw_network
*network
,
365 unsigned int channel_idx
,
372 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++) {
373 struct ipw_tty
*tty
= network
->associated_ttys
[channel_idx
][i
];
379 * If it's associated with a tty (other than the RAS channel
380 * when we're online), then send the data to that tty. The RAS
381 * channel's data is handled above - it always goes through
384 if (channel_idx
== IPW_CHANNEL_RAS
385 && (network
->ras_control_lines
&
386 IPW_CONTROL_LINE_DCD
) != 0
387 && ipwireless_tty_is_modem(tty
)) {
389 * If data came in on the RAS channel and this tty is
390 * the modem tty, and we are online, then we send it to
393 mutex_lock(&network
->close_lock
);
394 spin_lock_irqsave(&network
->lock
, flags
);
395 if (network
->ppp_channel
!= NULL
) {
398 spin_unlock_irqrestore(&network
->lock
,
401 /* Send the data to the ppp_generic module. */
402 skb
= ipw_packet_received_skb(data
, length
);
404 ppp_input(network
->ppp_channel
, skb
);
406 spin_unlock_irqrestore(&network
->lock
,
408 mutex_unlock(&network
->close_lock
);
410 /* Otherwise we send it out the tty. */
412 ipwireless_tty_received(tty
, data
, length
);
416 struct ipw_network
*ipwireless_network_create(struct ipw_hardware
*hw
)
418 struct ipw_network
*network
=
419 kzalloc(sizeof(struct ipw_network
), GFP_ATOMIC
);
424 spin_lock_init(&network
->lock
);
425 mutex_init(&network
->close_lock
);
427 network
->hardware
= hw
;
429 INIT_WORK(&network
->work_go_online
, do_go_online
);
430 INIT_WORK(&network
->work_go_offline
, do_go_offline
);
432 ipwireless_associate_network(hw
, network
);
437 void ipwireless_network_free(struct ipw_network
*network
)
439 network
->shutting_down
= 1;
441 ipwireless_ppp_close(network
);
442 flush_work(&network
->work_go_online
);
443 flush_work(&network
->work_go_offline
);
445 ipwireless_stop_interrupts(network
->hardware
);
446 ipwireless_associate_network(network
->hardware
, NULL
);
451 void ipwireless_associate_network_tty(struct ipw_network
*network
,
452 unsigned int channel_idx
,
457 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++)
458 if (network
->associated_ttys
[channel_idx
][i
] == NULL
) {
459 network
->associated_ttys
[channel_idx
][i
] = tty
;
464 void ipwireless_disassociate_network_ttys(struct ipw_network
*network
,
465 unsigned int channel_idx
)
469 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++)
470 network
->associated_ttys
[channel_idx
][i
] = NULL
;
473 void ipwireless_ppp_open(struct ipw_network
*network
)
475 if (ipwireless_debug
)
476 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
": online\n");
477 schedule_work(&network
->work_go_online
);
480 void ipwireless_ppp_close(struct ipw_network
*network
)
482 /* Disconnect from the wireless network. */
483 if (ipwireless_debug
)
484 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
": offline\n");
485 schedule_work(&network
->work_go_offline
);
488 int ipwireless_ppp_channel_index(struct ipw_network
*network
)
493 spin_lock_irqsave(&network
->lock
, flags
);
494 if (network
->ppp_channel
!= NULL
)
495 ret
= ppp_channel_index(network
->ppp_channel
);
496 spin_unlock_irqrestore(&network
->lock
, flags
);
501 int ipwireless_ppp_unit_number(struct ipw_network
*network
)
506 spin_lock_irqsave(&network
->lock
, flags
);
507 if (network
->ppp_channel
!= NULL
)
508 ret
= ppp_unit_number(network
->ppp_channel
);
509 spin_unlock_irqrestore(&network
->lock
, flags
);
514 int ipwireless_ppp_mru(const struct ipw_network
*network
)