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/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/mutex.h>
21 #include <linux/netdevice.h>
22 #include <linux/ppp_channel.h>
23 #include <linux/ppp_defs.h>
24 #include <linux/if_ppp.h>
25 #include <linux/skbuff.h>
32 #define MAX_OUTGOING_PACKETS_QUEUED ipwireless_out_queue
33 #define MAX_ASSOCIATED_TTYS 2
35 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
38 /* Hardware context, used for calls to hardware layer. */
39 struct ipw_hardware
*hardware
;
40 /* Context for kernel 'generic_ppp' functionality */
41 struct ppp_channel
*ppp_channel
;
42 /* tty context connected with IPW console */
43 struct ipw_tty
*associated_ttys
[NO_OF_IPW_CHANNELS
][MAX_ASSOCIATED_TTYS
];
44 /* True if ppp needs waking up once we're ready to xmit */
46 /* Number of packets queued up in hardware module. */
47 int outgoing_packets_queued
;
48 /* Spinlock to avoid interrupts during shutdown */
50 struct mutex close_lock
;
52 /* PPP ioctl data, not actually used anywere */
60 unsigned int ras_control_lines
;
62 struct work_struct work_go_online
;
63 struct work_struct work_go_offline
;
66 static void notify_packet_sent(void *callback_data
, unsigned int packet_length
)
68 struct ipw_network
*network
= callback_data
;
71 spin_lock_irqsave(&network
->spinlock
, flags
);
72 network
->outgoing_packets_queued
--;
73 if (network
->ppp_channel
!= NULL
) {
74 if (network
->ppp_blocked
) {
75 network
->ppp_blocked
= 0;
76 spin_unlock_irqrestore(&network
->spinlock
, flags
);
77 ppp_output_wakeup(network
->ppp_channel
);
79 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
82 spin_unlock_irqrestore(&network
->spinlock
, flags
);
84 spin_unlock_irqrestore(&network
->spinlock
, flags
);
88 * Called by the ppp system when it has a packet to send to the hardware.
90 static int ipwireless_ppp_start_xmit(struct ppp_channel
*ppp_channel
,
93 struct ipw_network
*network
= ppp_channel
->private;
96 spin_lock_irqsave(&network
->spinlock
, flags
);
97 if (network
->outgoing_packets_queued
< MAX_OUTGOING_PACKETS_QUEUED
) {
99 static unsigned char header
[] = {
100 PPP_ALLSTATIONS
, /* 0xff */
105 network
->outgoing_packets_queued
++;
106 spin_unlock_irqrestore(&network
->spinlock
, flags
);
109 * If we have the requested amount of headroom in the skb we
110 * were handed, then we can add the header efficiently.
112 if (skb_headroom(skb
) >= 2) {
113 memcpy(skb_push(skb
, 2), header
, 2);
114 ret
= ipwireless_send_packet(network
->hardware
,
115 IPW_CHANNEL_RAS
, skb
->data
,
124 /* Otherwise (rarely) we do it inefficiently. */
125 buf
= kmalloc(skb
->len
+ 2, GFP_ATOMIC
);
128 memcpy(buf
+ 2, skb
->data
, skb
->len
);
129 memcpy(buf
, header
, 2);
130 ret
= ipwireless_send_packet(network
->hardware
,
131 IPW_CHANNEL_RAS
, buf
,
143 * Otherwise reject the packet, and flag that the ppp system
144 * needs to be unblocked once we are ready to send.
146 network
->ppp_blocked
= 1;
147 spin_unlock_irqrestore(&network
->spinlock
, flags
);
152 /* Handle an ioctl call that has come in via ppp. (copy of ppp_async_ioctl() */
153 static int ipwireless_ppp_ioctl(struct ppp_channel
*ppp_channel
,
154 unsigned int cmd
, unsigned long arg
)
156 struct ipw_network
*network
= ppp_channel
->private;
159 int __user
*user_arg
= (int __user
*) arg
;
164 val
= network
->flags
| network
->rbits
;
165 if (put_user(val
, user_arg
))
171 if (get_user(val
, user_arg
))
173 network
->flags
= val
& ~SC_RCV_BITS
;
174 network
->rbits
= val
& SC_RCV_BITS
;
178 case PPPIOCGASYNCMAP
:
179 if (put_user(network
->xaccm
[0], user_arg
))
184 case PPPIOCSASYNCMAP
:
185 if (get_user(network
->xaccm
[0], user_arg
))
190 case PPPIOCGRASYNCMAP
:
191 if (put_user(network
->raccm
, user_arg
))
196 case PPPIOCSRASYNCMAP
:
197 if (get_user(network
->raccm
, user_arg
))
202 case PPPIOCGXASYNCMAP
:
203 if (copy_to_user((void __user
*) arg
, network
->xaccm
,
204 sizeof(network
->xaccm
)))
209 case PPPIOCSXASYNCMAP
:
210 if (copy_from_user(accm
, (void __user
*) arg
, sizeof(accm
)))
212 accm
[2] &= ~0x40000000U
; /* can't escape 0x5e */
213 accm
[3] |= 0x60000000U
; /* must escape 0x7d, 0x7e */
214 memcpy(network
->xaccm
, accm
, sizeof(network
->xaccm
));
219 if (put_user(network
->mru
, user_arg
))
225 if (get_user(val
, user_arg
))
240 static struct ppp_channel_ops ipwireless_ppp_channel_ops
= {
241 .start_xmit
= ipwireless_ppp_start_xmit
,
242 .ioctl
= ipwireless_ppp_ioctl
245 static void do_go_online(struct work_struct
*work_go_online
)
247 struct ipw_network
*network
=
248 container_of(work_go_online
, struct ipw_network
,
252 spin_lock_irqsave(&network
->spinlock
, flags
);
253 if (!network
->ppp_channel
) {
254 struct ppp_channel
*channel
;
256 spin_unlock_irqrestore(&network
->spinlock
, flags
);
257 channel
= kzalloc(sizeof(struct ppp_channel
), GFP_KERNEL
);
259 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
260 ": unable to allocate PPP channel\n");
263 channel
->private = network
;
264 channel
->mtu
= 16384; /* Wild guess */
266 channel
->ops
= &ipwireless_ppp_channel_ops
;
270 network
->mru
= PPP_MRU
;
271 memset(network
->xaccm
, 0, sizeof(network
->xaccm
));
272 network
->xaccm
[0] = ~0U;
273 network
->xaccm
[3] = 0x60000000U
;
274 network
->raccm
= ~0U;
275 ppp_register_channel(channel
);
276 spin_lock_irqsave(&network
->spinlock
, flags
);
277 network
->ppp_channel
= channel
;
279 spin_unlock_irqrestore(&network
->spinlock
, flags
);
282 static void do_go_offline(struct work_struct
*work_go_offline
)
284 struct ipw_network
*network
=
285 container_of(work_go_offline
, struct ipw_network
,
289 mutex_lock(&network
->close_lock
);
290 spin_lock_irqsave(&network
->spinlock
, flags
);
291 if (network
->ppp_channel
!= NULL
) {
292 struct ppp_channel
*channel
= network
->ppp_channel
;
294 network
->ppp_channel
= NULL
;
295 spin_unlock_irqrestore(&network
->spinlock
, flags
);
296 mutex_unlock(&network
->close_lock
);
297 ppp_unregister_channel(channel
);
299 spin_unlock_irqrestore(&network
->spinlock
, flags
);
300 mutex_unlock(&network
->close_lock
);
304 void ipwireless_network_notify_control_line_change(struct ipw_network
*network
,
305 unsigned int channel_idx
,
306 unsigned int control_lines
,
307 unsigned int changed_mask
)
311 if (channel_idx
== IPW_CHANNEL_RAS
)
312 network
->ras_control_lines
= control_lines
;
314 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++) {
315 struct ipw_tty
*tty
=
316 network
->associated_ttys
[channel_idx
][i
];
319 * If it's associated with a tty (other than the RAS channel
320 * when we're online), then send the data to that tty. The RAS
321 * channel's data is handled above - it always goes through
325 ipwireless_tty_notify_control_line_change(tty
,
333 * Some versions of firmware stuff packets with 0xff 0x03 (PPP: ALLSTATIONS, UI)
334 * bytes, which are required on sent packet, but not always present on received
337 static struct sk_buff
*ipw_packet_received_skb(unsigned char *data
,
342 if (length
> 2 && data
[0] == PPP_ALLSTATIONS
&& data
[1] == PPP_UI
) {
347 skb
= dev_alloc_skb(length
+ 4);
349 memcpy(skb_put(skb
, length
), data
, length
);
354 void ipwireless_network_packet_received(struct ipw_network
*network
,
355 unsigned int channel_idx
,
362 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++) {
363 struct ipw_tty
*tty
= network
->associated_ttys
[channel_idx
][i
];
369 * If it's associated with a tty (other than the RAS channel
370 * when we're online), then send the data to that tty. The RAS
371 * channel's data is handled above - it always goes through
374 if (channel_idx
== IPW_CHANNEL_RAS
375 && (network
->ras_control_lines
&
376 IPW_CONTROL_LINE_DCD
) != 0
377 && ipwireless_tty_is_modem(tty
)) {
379 * If data came in on the RAS channel and this tty is
380 * the modem tty, and we are online, then we send it to
383 mutex_lock(&network
->close_lock
);
384 spin_lock_irqsave(&network
->spinlock
, flags
);
385 if (network
->ppp_channel
!= NULL
) {
388 spin_unlock_irqrestore(&network
->spinlock
,
391 /* Send the data to the ppp_generic module. */
392 skb
= ipw_packet_received_skb(data
, length
);
393 ppp_input(network
->ppp_channel
, skb
);
395 spin_unlock_irqrestore(&network
->spinlock
,
397 mutex_unlock(&network
->close_lock
);
399 /* Otherwise we send it out the tty. */
401 ipwireless_tty_received(tty
, data
, length
);
405 struct ipw_network
*ipwireless_network_create(struct ipw_hardware
*hw
)
407 struct ipw_network
*network
=
408 kzalloc(sizeof(struct ipw_network
), GFP_ATOMIC
);
413 spin_lock_init(&network
->spinlock
);
414 mutex_init(&network
->close_lock
);
416 network
->hardware
= hw
;
418 INIT_WORK(&network
->work_go_online
, do_go_online
);
419 INIT_WORK(&network
->work_go_offline
, do_go_offline
);
421 ipwireless_associate_network(hw
, network
);
426 void ipwireless_network_free(struct ipw_network
*network
)
428 network
->shutting_down
= 1;
430 ipwireless_ppp_close(network
);
431 flush_scheduled_work();
433 ipwireless_stop_interrupts(network
->hardware
);
434 ipwireless_associate_network(network
->hardware
, NULL
);
439 void ipwireless_associate_network_tty(struct ipw_network
*network
,
440 unsigned int channel_idx
,
445 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++)
446 if (network
->associated_ttys
[channel_idx
][i
] == NULL
) {
447 network
->associated_ttys
[channel_idx
][i
] = tty
;
452 void ipwireless_disassociate_network_ttys(struct ipw_network
*network
,
453 unsigned int channel_idx
)
457 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++)
458 network
->associated_ttys
[channel_idx
][i
] = NULL
;
461 void ipwireless_ppp_open(struct ipw_network
*network
)
463 if (ipwireless_debug
)
464 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
": online\n");
465 schedule_work(&network
->work_go_online
);
468 void ipwireless_ppp_close(struct ipw_network
*network
)
470 /* Disconnect from the wireless network. */
471 if (ipwireless_debug
)
472 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
": offline\n");
473 schedule_work(&network
->work_go_offline
);
476 int ipwireless_ppp_channel_index(struct ipw_network
*network
)
481 spin_lock_irqsave(&network
->spinlock
, flags
);
482 if (network
->ppp_channel
!= NULL
)
483 ret
= ppp_channel_index(network
->ppp_channel
);
484 spin_unlock_irqrestore(&network
->spinlock
, flags
);
489 int ipwireless_ppp_unit_number(struct ipw_network
*network
)
494 spin_lock_irqsave(&network
->spinlock
, flags
);
495 if (network
->ppp_channel
!= NULL
)
496 ret
= ppp_unit_number(network
->ppp_channel
);
497 spin_unlock_irqrestore(&network
->spinlock
, flags
);