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
;
67 #ifdef IPWIRELESS_STATE_DEBUG
68 int ipwireless_dump_network_state(char *p
, size_t limit
,
69 struct ipw_network
*network
)
71 return snprintf(p
, limit
,
72 "debug: ppp_blocked=%d\n"
73 "debug: outgoing_packets_queued=%d\n"
74 "debug: network.shutting_down=%d\n",
76 network
->outgoing_packets_queued
,
77 network
->shutting_down
);
81 static void notify_packet_sent(void *callback_data
, unsigned int packet_length
)
83 struct ipw_network
*network
= callback_data
;
86 spin_lock_irqsave(&network
->spinlock
, flags
);
87 network
->outgoing_packets_queued
--;
88 if (network
->ppp_channel
!= NULL
) {
89 if (network
->ppp_blocked
) {
90 network
->ppp_blocked
= 0;
91 spin_unlock_irqrestore(&network
->spinlock
, flags
);
92 ppp_output_wakeup(network
->ppp_channel
);
94 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
97 spin_unlock_irqrestore(&network
->spinlock
, flags
);
99 spin_unlock_irqrestore(&network
->spinlock
, flags
);
103 * Called by the ppp system when it has a packet to send to the hardware.
105 static int ipwireless_ppp_start_xmit(struct ppp_channel
*ppp_channel
,
108 struct ipw_network
*network
= ppp_channel
->private;
111 spin_lock_irqsave(&network
->spinlock
, flags
);
112 if (network
->outgoing_packets_queued
< MAX_OUTGOING_PACKETS_QUEUED
) {
114 static unsigned char header
[] = {
115 PPP_ALLSTATIONS
, /* 0xff */
120 network
->outgoing_packets_queued
++;
121 spin_unlock_irqrestore(&network
->spinlock
, flags
);
124 * If we have the requested amount of headroom in the skb we
125 * were handed, then we can add the header efficiently.
127 if (skb_headroom(skb
) >= 2) {
128 memcpy(skb_push(skb
, 2), header
, 2);
129 ret
= ipwireless_send_packet(network
->hardware
,
130 IPW_CHANNEL_RAS
, skb
->data
,
139 /* Otherwise (rarely) we do it inefficiently. */
140 buf
= kmalloc(skb
->len
+ 2, GFP_ATOMIC
);
143 memcpy(buf
+ 2, skb
->data
, skb
->len
);
144 memcpy(buf
, header
, 2);
145 ret
= ipwireless_send_packet(network
->hardware
,
146 IPW_CHANNEL_RAS
, buf
,
158 * Otherwise reject the packet, and flag that the ppp system
159 * needs to be unblocked once we are ready to send.
161 network
->ppp_blocked
= 1;
162 spin_unlock_irqrestore(&network
->spinlock
, flags
);
167 /* Handle an ioctl call that has come in via ppp. (copy of ppp_async_ioctl() */
168 static int ipwireless_ppp_ioctl(struct ppp_channel
*ppp_channel
,
169 unsigned int cmd
, unsigned long arg
)
171 struct ipw_network
*network
= ppp_channel
->private;
174 int __user
*user_arg
= (int __user
*) arg
;
179 val
= network
->flags
| network
->rbits
;
180 if (put_user(val
, user_arg
))
186 if (get_user(val
, user_arg
))
188 network
->flags
= val
& ~SC_RCV_BITS
;
189 network
->rbits
= val
& SC_RCV_BITS
;
193 case PPPIOCGASYNCMAP
:
194 if (put_user(network
->xaccm
[0], user_arg
))
199 case PPPIOCSASYNCMAP
:
200 if (get_user(network
->xaccm
[0], user_arg
))
205 case PPPIOCGRASYNCMAP
:
206 if (put_user(network
->raccm
, user_arg
))
211 case PPPIOCSRASYNCMAP
:
212 if (get_user(network
->raccm
, user_arg
))
217 case PPPIOCGXASYNCMAP
:
218 if (copy_to_user((void __user
*) arg
, network
->xaccm
,
219 sizeof(network
->xaccm
)))
224 case PPPIOCSXASYNCMAP
:
225 if (copy_from_user(accm
, (void __user
*) arg
, sizeof(accm
)))
227 accm
[2] &= ~0x40000000U
; /* can't escape 0x5e */
228 accm
[3] |= 0x60000000U
; /* must escape 0x7d, 0x7e */
229 memcpy(network
->xaccm
, accm
, sizeof(network
->xaccm
));
234 if (put_user(network
->mru
, user_arg
))
240 if (get_user(val
, user_arg
))
255 static struct ppp_channel_ops ipwireless_ppp_channel_ops
= {
256 .start_xmit
= ipwireless_ppp_start_xmit
,
257 .ioctl
= ipwireless_ppp_ioctl
260 static void do_go_online(struct work_struct
*work_go_online
)
262 struct ipw_network
*network
=
263 container_of(work_go_online
, struct ipw_network
,
267 spin_lock_irqsave(&network
->spinlock
, flags
);
268 if (!network
->ppp_channel
) {
269 struct ppp_channel
*channel
;
271 spin_unlock_irqrestore(&network
->spinlock
, flags
);
272 channel
= kzalloc(sizeof(struct ppp_channel
), GFP_KERNEL
);
274 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
275 ": unable to allocate PPP channel\n");
278 channel
->private = network
;
279 channel
->mtu
= 16384; /* Wild guess */
281 channel
->ops
= &ipwireless_ppp_channel_ops
;
285 network
->mru
= PPP_MRU
;
286 memset(network
->xaccm
, 0, sizeof(network
->xaccm
));
287 network
->xaccm
[0] = ~0U;
288 network
->xaccm
[3] = 0x60000000U
;
289 network
->raccm
= ~0U;
290 ppp_register_channel(channel
);
291 spin_lock_irqsave(&network
->spinlock
, flags
);
292 network
->ppp_channel
= channel
;
294 spin_unlock_irqrestore(&network
->spinlock
, flags
);
297 static void do_go_offline(struct work_struct
*work_go_offline
)
299 struct ipw_network
*network
=
300 container_of(work_go_offline
, struct ipw_network
,
304 mutex_lock(&network
->close_lock
);
305 spin_lock_irqsave(&network
->spinlock
, flags
);
306 if (network
->ppp_channel
!= NULL
) {
307 struct ppp_channel
*channel
= network
->ppp_channel
;
309 network
->ppp_channel
= NULL
;
310 spin_unlock_irqrestore(&network
->spinlock
, flags
);
311 mutex_unlock(&network
->close_lock
);
312 ppp_unregister_channel(channel
);
314 spin_unlock_irqrestore(&network
->spinlock
, flags
);
315 mutex_unlock(&network
->close_lock
);
319 void ipwireless_network_notify_control_line_change(struct ipw_network
*network
,
320 unsigned int channel_idx
,
321 unsigned int control_lines
,
322 unsigned int changed_mask
)
326 if (channel_idx
== IPW_CHANNEL_RAS
)
327 network
->ras_control_lines
= control_lines
;
329 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++) {
330 struct ipw_tty
*tty
=
331 network
->associated_ttys
[channel_idx
][i
];
334 * If it's associated with a tty (other than the RAS channel
335 * when we're online), then send the data to that tty. The RAS
336 * channel's data is handled above - it always goes through
340 ipwireless_tty_notify_control_line_change(tty
,
348 * Some versions of firmware stuff packets with 0xff 0x03 (PPP: ALLSTATIONS, UI)
349 * bytes, which are required on sent packet, but not always present on received
352 static struct sk_buff
*ipw_packet_received_skb(unsigned char *data
,
357 if (length
> 2 && data
[0] == PPP_ALLSTATIONS
&& data
[1] == PPP_UI
) {
362 skb
= dev_alloc_skb(length
+ 4);
364 memcpy(skb_put(skb
, length
), data
, length
);
369 void ipwireless_network_packet_received(struct ipw_network
*network
,
370 unsigned int channel_idx
,
377 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++) {
378 struct ipw_tty
*tty
= network
->associated_ttys
[channel_idx
][i
];
384 * If it's associated with a tty (other than the RAS channel
385 * when we're online), then send the data to that tty. The RAS
386 * channel's data is handled above - it always goes through
389 if (channel_idx
== IPW_CHANNEL_RAS
390 && (network
->ras_control_lines
&
391 IPW_CONTROL_LINE_DCD
) != 0
392 && ipwireless_tty_is_modem(tty
)) {
394 * If data came in on the RAS channel and this tty is
395 * the modem tty, and we are online, then we send it to
398 mutex_lock(&network
->close_lock
);
399 spin_lock_irqsave(&network
->spinlock
, flags
);
400 if (network
->ppp_channel
!= NULL
) {
403 spin_unlock_irqrestore(&network
->spinlock
,
406 /* Send the data to the ppp_generic module. */
407 skb
= ipw_packet_received_skb(data
, length
);
408 ppp_input(network
->ppp_channel
, skb
);
410 spin_unlock_irqrestore(&network
->spinlock
,
412 mutex_unlock(&network
->close_lock
);
414 /* Otherwise we send it out the tty. */
416 ipwireless_tty_received(tty
, data
, length
);
420 struct ipw_network
*ipwireless_network_create(struct ipw_hardware
*hw
)
422 struct ipw_network
*network
=
423 kzalloc(sizeof(struct ipw_network
), GFP_ATOMIC
);
428 spin_lock_init(&network
->spinlock
);
429 mutex_init(&network
->close_lock
);
431 network
->hardware
= hw
;
433 INIT_WORK(&network
->work_go_online
, do_go_online
);
434 INIT_WORK(&network
->work_go_offline
, do_go_offline
);
436 ipwireless_associate_network(hw
, network
);
441 void ipwireless_network_free(struct ipw_network
*network
)
443 network
->shutting_down
= 1;
445 ipwireless_ppp_close(network
);
446 flush_scheduled_work();
448 ipwireless_stop_interrupts(network
->hardware
);
449 ipwireless_associate_network(network
->hardware
, NULL
);
454 void ipwireless_associate_network_tty(struct ipw_network
*network
,
455 unsigned int channel_idx
,
460 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++)
461 if (network
->associated_ttys
[channel_idx
][i
] == NULL
) {
462 network
->associated_ttys
[channel_idx
][i
] = tty
;
467 void ipwireless_disassociate_network_ttys(struct ipw_network
*network
,
468 unsigned int channel_idx
)
472 for (i
= 0; i
< MAX_ASSOCIATED_TTYS
; i
++)
473 network
->associated_ttys
[channel_idx
][i
] = NULL
;
476 void ipwireless_ppp_open(struct ipw_network
*network
)
478 if (ipwireless_debug
)
479 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
": online\n");
480 schedule_work(&network
->work_go_online
);
483 void ipwireless_ppp_close(struct ipw_network
*network
)
485 /* Disconnect from the wireless network. */
486 if (ipwireless_debug
)
487 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
": offline\n");
488 schedule_work(&network
->work_go_offline
);
491 int ipwireless_ppp_channel_index(struct ipw_network
*network
)
496 spin_lock_irqsave(&network
->spinlock
, flags
);
497 if (network
->ppp_channel
!= NULL
)
498 ret
= ppp_channel_index(network
->ppp_channel
);
499 spin_unlock_irqrestore(&network
->spinlock
, flags
);
504 int ipwireless_ppp_unit_number(struct ipw_network
*network
)
509 spin_lock_irqsave(&network
->spinlock
, flags
);
510 if (network
->ppp_channel
!= NULL
)
511 ret
= ppp_unit_number(network
->ppp_channel
);
512 spin_unlock_irqrestore(&network
->spinlock
, flags
);