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/slab.h>
25 #include <linux/ppp-ioctl.h>
26 #include <linux/skbuff.h>
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->lock, 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->lock, flags);
77 ppp_output_wakeup(network->ppp_channel);
79 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
82 spin_unlock_irqrestore(&network->lock, flags);
84 spin_unlock_irqrestore(&network->lock, 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->lock, flags);
97 if (network->outgoing_packets_queued < ipwireless_out_queue) {
99 static unsigned char header[] = {
100 PPP_ALLSTATIONS, /* 0xff */
105 network->outgoing_packets_queued++;
106 spin_unlock_irqrestore(&network->lock, 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->lock, flags);
148 if (ipwireless_debug)
149 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": ppp blocked\n");
154 /* Handle an ioctl call that has come in via ppp. (copy of ppp_async_ioctl() */
155 static int ipwireless_ppp_ioctl(struct ppp_channel *ppp_channel,
156 unsigned int cmd, unsigned long arg)
158 struct ipw_network *network = ppp_channel->private;
161 int __user *user_arg = (int __user *) arg;
166 val = network->flags | network->rbits;
167 if (put_user(val, user_arg))
173 if (get_user(val, user_arg))
175 network->flags = val & ~SC_RCV_BITS;
176 network->rbits = val & SC_RCV_BITS;
180 case PPPIOCGASYNCMAP:
181 if (put_user(network->xaccm[0], user_arg))
186 case PPPIOCSASYNCMAP:
187 if (get_user(network->xaccm[0], user_arg))
192 case PPPIOCGRASYNCMAP:
193 if (put_user(network->raccm, user_arg))
198 case PPPIOCSRASYNCMAP:
199 if (get_user(network->raccm, user_arg))
204 case PPPIOCGXASYNCMAP:
205 if (copy_to_user((void __user *) arg, network->xaccm,
206 sizeof(network->xaccm)))
211 case PPPIOCSXASYNCMAP:
212 if (copy_from_user(accm, (void __user *) arg, sizeof(accm)))
214 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
215 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
216 memcpy(network->xaccm, accm, sizeof(network->xaccm));
221 if (put_user(network->mru, user_arg))
227 if (get_user(val, user_arg))
242 static const struct ppp_channel_ops ipwireless_ppp_channel_ops = {
243 .start_xmit = ipwireless_ppp_start_xmit,
244 .ioctl = ipwireless_ppp_ioctl
247 static void do_go_online(struct work_struct *work_go_online)
249 struct ipw_network *network =
250 container_of(work_go_online, struct ipw_network,
254 spin_lock_irqsave(&network->lock, flags);
255 if (!network->ppp_channel) {
256 struct ppp_channel *channel;
258 spin_unlock_irqrestore(&network->lock, flags);
259 channel = kzalloc(sizeof(struct ppp_channel), GFP_KERNEL);
261 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
262 ": unable to allocate PPP channel\n");
265 channel->private = network;
266 channel->mtu = 16384; /* Wild guess */
268 channel->ops = &ipwireless_ppp_channel_ops;
272 network->mru = PPP_MRU;
273 memset(network->xaccm, 0, sizeof(network->xaccm));
274 network->xaccm[0] = ~0U;
275 network->xaccm[3] = 0x60000000U;
276 network->raccm = ~0U;
277 if (ppp_register_channel(channel) < 0) {
278 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
279 ": unable to register PPP channel\n");
283 spin_lock_irqsave(&network->lock, flags);
284 network->ppp_channel = channel;
286 spin_unlock_irqrestore(&network->lock, flags);
289 static void do_go_offline(struct work_struct *work_go_offline)
291 struct ipw_network *network =
292 container_of(work_go_offline, struct ipw_network,
296 mutex_lock(&network->close_lock);
297 spin_lock_irqsave(&network->lock, flags);
298 if (network->ppp_channel != NULL) {
299 struct ppp_channel *channel = network->ppp_channel;
301 network->ppp_channel = NULL;
302 spin_unlock_irqrestore(&network->lock, flags);
303 mutex_unlock(&network->close_lock);
304 ppp_unregister_channel(channel);
306 spin_unlock_irqrestore(&network->lock, flags);
307 mutex_unlock(&network->close_lock);
311 void ipwireless_network_notify_control_line_change(struct ipw_network *network,
312 unsigned int channel_idx,
313 unsigned int control_lines,
314 unsigned int changed_mask)
318 if (channel_idx == IPW_CHANNEL_RAS)
319 network->ras_control_lines = control_lines;
321 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
322 struct ipw_tty *tty =
323 network->associated_ttys[channel_idx][i];
326 * If it's associated with a tty (other than the RAS channel
327 * when we're online), then send the data to that tty. The RAS
328 * channel's data is handled above - it always goes through
332 ipwireless_tty_notify_control_line_change(tty,
340 * Some versions of firmware stuff packets with 0xff 0x03 (PPP: ALLSTATIONS, UI)
341 * bytes, which are required on sent packet, but not always present on received
344 static struct sk_buff *ipw_packet_received_skb(unsigned char *data,
349 if (length > 2 && data[0] == PPP_ALLSTATIONS && data[1] == PPP_UI) {
354 skb = dev_alloc_skb(length + 4);
358 memcpy(skb_put(skb, length), data, length);
363 void ipwireless_network_packet_received(struct ipw_network *network,
364 unsigned int channel_idx,
371 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
372 struct ipw_tty *tty = network->associated_ttys[channel_idx][i];
378 * If it's associated with a tty (other than the RAS channel
379 * when we're online), then send the data to that tty. The RAS
380 * channel's data is handled above - it always goes through
383 if (channel_idx == IPW_CHANNEL_RAS
384 && (network->ras_control_lines &
385 IPW_CONTROL_LINE_DCD) != 0
386 && ipwireless_tty_is_modem(tty)) {
388 * If data came in on the RAS channel and this tty is
389 * the modem tty, and we are online, then we send it to
392 mutex_lock(&network->close_lock);
393 spin_lock_irqsave(&network->lock, flags);
394 if (network->ppp_channel != NULL) {
397 spin_unlock_irqrestore(&network->lock,
400 /* Send the data to the ppp_generic module. */
401 skb = ipw_packet_received_skb(data, length);
403 ppp_input(network->ppp_channel, skb);
405 spin_unlock_irqrestore(&network->lock,
407 mutex_unlock(&network->close_lock);
409 /* Otherwise we send it out the tty. */
411 ipwireless_tty_received(tty, data, length);
415 struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw)
417 struct ipw_network *network =
418 kzalloc(sizeof(struct ipw_network), GFP_ATOMIC);
423 spin_lock_init(&network->lock);
424 mutex_init(&network->close_lock);
426 network->hardware = hw;
428 INIT_WORK(&network->work_go_online, do_go_online);
429 INIT_WORK(&network->work_go_offline, do_go_offline);
431 ipwireless_associate_network(hw, network);
436 void ipwireless_network_free(struct ipw_network *network)
438 network->shutting_down = 1;
440 ipwireless_ppp_close(network);
441 flush_work(&network->work_go_online);
442 flush_work(&network->work_go_offline);
444 ipwireless_stop_interrupts(network->hardware);
445 ipwireless_associate_network(network->hardware, NULL);
450 void ipwireless_associate_network_tty(struct ipw_network *network,
451 unsigned int channel_idx,
456 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
457 if (network->associated_ttys[channel_idx][i] == NULL) {
458 network->associated_ttys[channel_idx][i] = tty;
463 void ipwireless_disassociate_network_ttys(struct ipw_network *network,
464 unsigned int channel_idx)
468 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
469 network->associated_ttys[channel_idx][i] = NULL;
472 void ipwireless_ppp_open(struct ipw_network *network)
474 if (ipwireless_debug)
475 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": online\n");
476 schedule_work(&network->work_go_online);
479 void ipwireless_ppp_close(struct ipw_network *network)
481 /* Disconnect from the wireless network. */
482 if (ipwireless_debug)
483 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": offline\n");
484 schedule_work(&network->work_go_offline);
487 int ipwireless_ppp_channel_index(struct ipw_network *network)
492 spin_lock_irqsave(&network->lock, flags);
493 if (network->ppp_channel != NULL)
494 ret = ppp_channel_index(network->ppp_channel);
495 spin_unlock_irqrestore(&network->lock, flags);
500 int ipwireless_ppp_unit_number(struct ipw_network *network)
505 spin_lock_irqsave(&network->lock, flags);
506 if (network->ppp_channel != NULL)
507 ret = ppp_unit_number(network->ppp_channel);
508 spin_unlock_irqrestore(&network->lock, flags);
513 int ipwireless_ppp_mru(const struct ipw_network *network)