blob: c0dfb642383b29015a81c760a12b06b0452aaa65 [file] [log] [blame]
David Sterba099dc4f2008-02-07 10:57:12 +01001/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Paul Mackerras4b32da2b2012-03-04 12:56:55 +000025#include <linux/ppp-ioctl.h>
David Sterba099dc4f2008-02-07 10:57:12 +010026#include <linux/skbuff.h>
27
28#include "network.h"
29#include "hardware.h"
30#include "main.h"
31#include "tty.h"
32
David Sterba099dc4f2008-02-07 10:57:12 +010033#define MAX_ASSOCIATED_TTYS 2
34
35#define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
36
37struct ipw_network {
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 */
45 int ppp_blocked;
46 /* Number of packets queued up in hardware module. */
47 int outgoing_packets_queued;
48 /* Spinlock to avoid interrupts during shutdown */
David Sterba63c4dbd2008-07-28 16:52:44 +020049 spinlock_t lock;
David Sterba099dc4f2008-02-07 10:57:12 +010050 struct mutex close_lock;
51
52 /* PPP ioctl data, not actually used anywere */
53 unsigned int flags;
54 unsigned int rbits;
55 u32 xaccm[8];
56 u32 raccm;
57 int mru;
58
59 int shutting_down;
60 unsigned int ras_control_lines;
61
62 struct work_struct work_go_online;
63 struct work_struct work_go_offline;
64};
65
David Sterba099dc4f2008-02-07 10:57:12 +010066static void notify_packet_sent(void *callback_data, unsigned int packet_length)
67{
68 struct ipw_network *network = callback_data;
69 unsigned long flags;
70
David Sterba63c4dbd2008-07-28 16:52:44 +020071 spin_lock_irqsave(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +010072 network->outgoing_packets_queued--;
73 if (network->ppp_channel != NULL) {
74 if (network->ppp_blocked) {
75 network->ppp_blocked = 0;
David Sterba63c4dbd2008-07-28 16:52:44 +020076 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +010077 ppp_output_wakeup(network->ppp_channel);
78 if (ipwireless_debug)
David Sterbabee9c7c2008-07-28 16:53:21 +020079 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
David Sterba099dc4f2008-02-07 10:57:12 +010080 ": ppp unblocked\n");
81 } else
David Sterba63c4dbd2008-07-28 16:52:44 +020082 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +010083 } else
David Sterba63c4dbd2008-07-28 16:52:44 +020084 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +010085}
86
87/*
88 * Called by the ppp system when it has a packet to send to the hardware.
89 */
90static int ipwireless_ppp_start_xmit(struct ppp_channel *ppp_channel,
91 struct sk_buff *skb)
92{
93 struct ipw_network *network = ppp_channel->private;
94 unsigned long flags;
95
David Sterba63c4dbd2008-07-28 16:52:44 +020096 spin_lock_irqsave(&network->lock, flags);
David Sterba2e713162008-07-28 16:52:39 +020097 if (network->outgoing_packets_queued < ipwireless_out_queue) {
David Sterba099dc4f2008-02-07 10:57:12 +010098 unsigned char *buf;
99 static unsigned char header[] = {
100 PPP_ALLSTATIONS, /* 0xff */
101 PPP_UI, /* 0x03 */
102 };
103 int ret;
104
105 network->outgoing_packets_queued++;
David Sterba63c4dbd2008-07-28 16:52:44 +0200106 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100107
108 /*
109 * If we have the requested amount of headroom in the skb we
110 * were handed, then we can add the header efficiently.
111 */
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,
116 skb->len,
117 notify_packet_sent,
118 network);
119 if (ret == -1) {
120 skb_pull(skb, 2);
121 return 0;
122 }
123 } else {
124 /* Otherwise (rarely) we do it inefficiently. */
125 buf = kmalloc(skb->len + 2, GFP_ATOMIC);
126 if (!buf)
127 return 0;
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,
132 skb->len + 2,
133 notify_packet_sent,
134 network);
135 kfree(buf);
136 if (ret == -1)
137 return 0;
138 }
139 kfree_skb(skb);
140 return 1;
141 } else {
142 /*
143 * Otherwise reject the packet, and flag that the ppp system
144 * needs to be unblocked once we are ready to send.
145 */
146 network->ppp_blocked = 1;
David Sterba63c4dbd2008-07-28 16:52:44 +0200147 spin_unlock_irqrestore(&network->lock, flags);
David Sterbabee9c7c2008-07-28 16:53:21 +0200148 if (ipwireless_debug)
149 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": ppp blocked\n");
David Sterba099dc4f2008-02-07 10:57:12 +0100150 return 0;
151 }
152}
153
154/* Handle an ioctl call that has come in via ppp. (copy of ppp_async_ioctl() */
155static int ipwireless_ppp_ioctl(struct ppp_channel *ppp_channel,
156 unsigned int cmd, unsigned long arg)
157{
158 struct ipw_network *network = ppp_channel->private;
159 int err, val;
160 u32 accm[8];
161 int __user *user_arg = (int __user *) arg;
162
163 err = -EFAULT;
164 switch (cmd) {
165 case PPPIOCGFLAGS:
166 val = network->flags | network->rbits;
167 if (put_user(val, user_arg))
168 break;
169 err = 0;
170 break;
171
172 case PPPIOCSFLAGS:
173 if (get_user(val, user_arg))
174 break;
175 network->flags = val & ~SC_RCV_BITS;
176 network->rbits = val & SC_RCV_BITS;
177 err = 0;
178 break;
179
180 case PPPIOCGASYNCMAP:
181 if (put_user(network->xaccm[0], user_arg))
182 break;
183 err = 0;
184 break;
185
186 case PPPIOCSASYNCMAP:
187 if (get_user(network->xaccm[0], user_arg))
188 break;
189 err = 0;
190 break;
191
192 case PPPIOCGRASYNCMAP:
193 if (put_user(network->raccm, user_arg))
194 break;
195 err = 0;
196 break;
197
198 case PPPIOCSRASYNCMAP:
199 if (get_user(network->raccm, user_arg))
200 break;
201 err = 0;
202 break;
203
204 case PPPIOCGXASYNCMAP:
205 if (copy_to_user((void __user *) arg, network->xaccm,
206 sizeof(network->xaccm)))
207 break;
208 err = 0;
209 break;
210
211 case PPPIOCSXASYNCMAP:
212 if (copy_from_user(accm, (void __user *) arg, sizeof(accm)))
213 break;
214 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
215 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
216 memcpy(network->xaccm, accm, sizeof(network->xaccm));
217 err = 0;
218 break;
219
220 case PPPIOCGMRU:
221 if (put_user(network->mru, user_arg))
222 break;
223 err = 0;
224 break;
225
226 case PPPIOCSMRU:
227 if (get_user(val, user_arg))
228 break;
229 if (val < PPP_MRU)
230 val = PPP_MRU;
231 network->mru = val;
232 err = 0;
233 break;
234
235 default:
236 err = -ENOTTY;
237 }
238
239 return err;
240}
241
stephen hemmingerd7100da2010-08-04 07:34:36 +0000242static const struct ppp_channel_ops ipwireless_ppp_channel_ops = {
David Sterba099dc4f2008-02-07 10:57:12 +0100243 .start_xmit = ipwireless_ppp_start_xmit,
244 .ioctl = ipwireless_ppp_ioctl
245};
246
247static void do_go_online(struct work_struct *work_go_online)
248{
249 struct ipw_network *network =
250 container_of(work_go_online, struct ipw_network,
251 work_go_online);
252 unsigned long flags;
253
David Sterba63c4dbd2008-07-28 16:52:44 +0200254 spin_lock_irqsave(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100255 if (!network->ppp_channel) {
256 struct ppp_channel *channel;
257
David Sterba63c4dbd2008-07-28 16:52:44 +0200258 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100259 channel = kzalloc(sizeof(struct ppp_channel), GFP_KERNEL);
260 if (!channel) {
261 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
262 ": unable to allocate PPP channel\n");
263 return;
264 }
265 channel->private = network;
266 channel->mtu = 16384; /* Wild guess */
267 channel->hdrlen = 2;
268 channel->ops = &ipwireless_ppp_channel_ops;
269
270 network->flags = 0;
271 network->rbits = 0;
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;
Alan Coxb6abc902012-09-17 12:01:40 +0100277 if (ppp_register_channel(channel) < 0) {
278 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
279 ": unable to register PPP channel\n");
280 kfree(channel);
281 return;
282 }
David Sterba63c4dbd2008-07-28 16:52:44 +0200283 spin_lock_irqsave(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100284 network->ppp_channel = channel;
285 }
David Sterba63c4dbd2008-07-28 16:52:44 +0200286 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100287}
288
289static void do_go_offline(struct work_struct *work_go_offline)
290{
291 struct ipw_network *network =
292 container_of(work_go_offline, struct ipw_network,
293 work_go_offline);
294 unsigned long flags;
295
296 mutex_lock(&network->close_lock);
David Sterba63c4dbd2008-07-28 16:52:44 +0200297 spin_lock_irqsave(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100298 if (network->ppp_channel != NULL) {
299 struct ppp_channel *channel = network->ppp_channel;
300
301 network->ppp_channel = NULL;
David Sterba63c4dbd2008-07-28 16:52:44 +0200302 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100303 mutex_unlock(&network->close_lock);
304 ppp_unregister_channel(channel);
305 } else {
David Sterba63c4dbd2008-07-28 16:52:44 +0200306 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100307 mutex_unlock(&network->close_lock);
308 }
309}
310
311void 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)
315{
316 int i;
317
318 if (channel_idx == IPW_CHANNEL_RAS)
319 network->ras_control_lines = control_lines;
320
321 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
322 struct ipw_tty *tty =
323 network->associated_ttys[channel_idx][i];
324
325 /*
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
329 * ppp_generic.
330 */
331 if (tty)
332 ipwireless_tty_notify_control_line_change(tty,
333 channel_idx,
334 control_lines,
335 changed_mask);
336 }
337}
338
339/*
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
342 * packets
343 */
344static struct sk_buff *ipw_packet_received_skb(unsigned char *data,
345 unsigned int length)
346{
347 struct sk_buff *skb;
348
349 if (length > 2 && data[0] == PPP_ALLSTATIONS && data[1] == PPP_UI) {
350 length -= 2;
351 data += 2;
352 }
353
354 skb = dev_alloc_skb(length + 4);
Alan Coxd1519e22012-11-01 16:45:49 +0000355 if (skb == NULL)
356 return NULL;
David Sterba099dc4f2008-02-07 10:57:12 +0100357 skb_reserve(skb, 2);
358 memcpy(skb_put(skb, length), data, length);
359
360 return skb;
361}
362
363void ipwireless_network_packet_received(struct ipw_network *network,
364 unsigned int channel_idx,
365 unsigned char *data,
366 unsigned int length)
367{
368 int i;
369 unsigned long flags;
370
371 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
372 struct ipw_tty *tty = network->associated_ttys[channel_idx][i];
373
David Sterbaa51f4122008-03-04 14:28:50 -0800374 if (!tty)
375 continue;
376
David Sterba099dc4f2008-02-07 10:57:12 +0100377 /*
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
381 * ppp_generic.
382 */
David Sterbaa51f4122008-03-04 14:28:50 -0800383 if (channel_idx == IPW_CHANNEL_RAS
David Sterba099dc4f2008-02-07 10:57:12 +0100384 && (network->ras_control_lines &
385 IPW_CONTROL_LINE_DCD) != 0
386 && ipwireless_tty_is_modem(tty)) {
387 /*
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
390 * the PPP layer.
391 */
392 mutex_lock(&network->close_lock);
David Sterba63c4dbd2008-07-28 16:52:44 +0200393 spin_lock_irqsave(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100394 if (network->ppp_channel != NULL) {
395 struct sk_buff *skb;
396
David Sterba63c4dbd2008-07-28 16:52:44 +0200397 spin_unlock_irqrestore(&network->lock,
David Sterba099dc4f2008-02-07 10:57:12 +0100398 flags);
399
400 /* Send the data to the ppp_generic module. */
401 skb = ipw_packet_received_skb(data, length);
Alan Coxd1519e22012-11-01 16:45:49 +0000402 if (skb)
403 ppp_input(network->ppp_channel, skb);
David Sterba099dc4f2008-02-07 10:57:12 +0100404 } else
David Sterba63c4dbd2008-07-28 16:52:44 +0200405 spin_unlock_irqrestore(&network->lock,
David Sterba099dc4f2008-02-07 10:57:12 +0100406 flags);
407 mutex_unlock(&network->close_lock);
408 }
409 /* Otherwise we send it out the tty. */
410 else
411 ipwireless_tty_received(tty, data, length);
412 }
413}
414
415struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw)
416{
417 struct ipw_network *network =
418 kzalloc(sizeof(struct ipw_network), GFP_ATOMIC);
419
420 if (!network)
421 return NULL;
422
David Sterba63c4dbd2008-07-28 16:52:44 +0200423 spin_lock_init(&network->lock);
David Sterba099dc4f2008-02-07 10:57:12 +0100424 mutex_init(&network->close_lock);
425
426 network->hardware = hw;
427
428 INIT_WORK(&network->work_go_online, do_go_online);
429 INIT_WORK(&network->work_go_offline, do_go_offline);
430
431 ipwireless_associate_network(hw, network);
432
433 return network;
434}
435
436void ipwireless_network_free(struct ipw_network *network)
437{
438 network->shutting_down = 1;
439
440 ipwireless_ppp_close(network);
Tejun Heo43829732012-08-20 14:51:24 -0700441 flush_work(&network->work_go_online);
442 flush_work(&network->work_go_offline);
David Sterba099dc4f2008-02-07 10:57:12 +0100443
444 ipwireless_stop_interrupts(network->hardware);
445 ipwireless_associate_network(network->hardware, NULL);
446
447 kfree(network);
448}
449
450void ipwireless_associate_network_tty(struct ipw_network *network,
451 unsigned int channel_idx,
452 struct ipw_tty *tty)
453{
454 int i;
455
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;
459 break;
460 }
461}
462
463void ipwireless_disassociate_network_ttys(struct ipw_network *network,
464 unsigned int channel_idx)
465{
466 int i;
467
468 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
469 network->associated_ttys[channel_idx][i] = NULL;
470}
471
472void ipwireless_ppp_open(struct ipw_network *network)
473{
474 if (ipwireless_debug)
475 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": online\n");
476 schedule_work(&network->work_go_online);
477}
478
479void ipwireless_ppp_close(struct ipw_network *network)
480{
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);
485}
486
487int ipwireless_ppp_channel_index(struct ipw_network *network)
488{
489 int ret = -1;
490 unsigned long flags;
491
David Sterba63c4dbd2008-07-28 16:52:44 +0200492 spin_lock_irqsave(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100493 if (network->ppp_channel != NULL)
494 ret = ppp_channel_index(network->ppp_channel);
David Sterba63c4dbd2008-07-28 16:52:44 +0200495 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100496
497 return ret;
498}
499
500int ipwireless_ppp_unit_number(struct ipw_network *network)
501{
502 int ret = -1;
503 unsigned long flags;
504
David Sterba63c4dbd2008-07-28 16:52:44 +0200505 spin_lock_irqsave(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100506 if (network->ppp_channel != NULL)
507 ret = ppp_unit_number(network->ppp_channel);
David Sterba63c4dbd2008-07-28 16:52:44 +0200508 spin_unlock_irqrestore(&network->lock, flags);
David Sterba099dc4f2008-02-07 10:57:12 +0100509
510 return ret;
511}
David Sterbaa0138692008-07-28 16:53:32 +0200512
513int ipwireless_ppp_mru(const struct ipw_network *network)
514{
515 return network->mru;
516}