Loading...
1/*
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>
24#include <linux/slab.h>
25#include <linux/if_ppp.h>
26#include <linux/skbuff.h>
27
28#include "network.h"
29#include "hardware.h"
30#include "main.h"
31#include "tty.h"
32
33#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 */
49 spinlock_t lock;
50 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
66static void notify_packet_sent(void *callback_data, unsigned int packet_length)
67{
68 struct ipw_network *network = callback_data;
69 unsigned long flags;
70
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);
78 if (ipwireless_debug)
79 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
80 ": ppp unblocked\n");
81 } else
82 spin_unlock_irqrestore(&network->lock, flags);
83 } else
84 spin_unlock_irqrestore(&network->lock, flags);
85}
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
96 spin_lock_irqsave(&network->lock, flags);
97 if (network->outgoing_packets_queued < ipwireless_out_queue) {
98 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++;
106 spin_unlock_irqrestore(&network->lock, flags);
107
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;
147 spin_unlock_irqrestore(&network->lock, flags);
148 if (ipwireless_debug)
149 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": ppp blocked\n");
150 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
242static const struct ppp_channel_ops ipwireless_ppp_channel_ops = {
243 .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
254 spin_lock_irqsave(&network->lock, flags);
255 if (!network->ppp_channel) {
256 struct ppp_channel *channel;
257
258 spin_unlock_irqrestore(&network->lock, flags);
259 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;
277 ppp_register_channel(channel);
278 spin_lock_irqsave(&network->lock, flags);
279 network->ppp_channel = channel;
280 }
281 spin_unlock_irqrestore(&network->lock, flags);
282}
283
284static void do_go_offline(struct work_struct *work_go_offline)
285{
286 struct ipw_network *network =
287 container_of(work_go_offline, struct ipw_network,
288 work_go_offline);
289 unsigned long flags;
290
291 mutex_lock(&network->close_lock);
292 spin_lock_irqsave(&network->lock, flags);
293 if (network->ppp_channel != NULL) {
294 struct ppp_channel *channel = network->ppp_channel;
295
296 network->ppp_channel = NULL;
297 spin_unlock_irqrestore(&network->lock, flags);
298 mutex_unlock(&network->close_lock);
299 ppp_unregister_channel(channel);
300 } else {
301 spin_unlock_irqrestore(&network->lock, flags);
302 mutex_unlock(&network->close_lock);
303 }
304}
305
306void ipwireless_network_notify_control_line_change(struct ipw_network *network,
307 unsigned int channel_idx,
308 unsigned int control_lines,
309 unsigned int changed_mask)
310{
311 int i;
312
313 if (channel_idx == IPW_CHANNEL_RAS)
314 network->ras_control_lines = control_lines;
315
316 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
317 struct ipw_tty *tty =
318 network->associated_ttys[channel_idx][i];
319
320 /*
321 * If it's associated with a tty (other than the RAS channel
322 * when we're online), then send the data to that tty. The RAS
323 * channel's data is handled above - it always goes through
324 * ppp_generic.
325 */
326 if (tty)
327 ipwireless_tty_notify_control_line_change(tty,
328 channel_idx,
329 control_lines,
330 changed_mask);
331 }
332}
333
334/*
335 * Some versions of firmware stuff packets with 0xff 0x03 (PPP: ALLSTATIONS, UI)
336 * bytes, which are required on sent packet, but not always present on received
337 * packets
338 */
339static struct sk_buff *ipw_packet_received_skb(unsigned char *data,
340 unsigned int length)
341{
342 struct sk_buff *skb;
343
344 if (length > 2 && data[0] == PPP_ALLSTATIONS && data[1] == PPP_UI) {
345 length -= 2;
346 data += 2;
347 }
348
349 skb = dev_alloc_skb(length + 4);
350 skb_reserve(skb, 2);
351 memcpy(skb_put(skb, length), data, length);
352
353 return skb;
354}
355
356void ipwireless_network_packet_received(struct ipw_network *network,
357 unsigned int channel_idx,
358 unsigned char *data,
359 unsigned int length)
360{
361 int i;
362 unsigned long flags;
363
364 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
365 struct ipw_tty *tty = network->associated_ttys[channel_idx][i];
366
367 if (!tty)
368 continue;
369
370 /*
371 * If it's associated with a tty (other than the RAS channel
372 * when we're online), then send the data to that tty. The RAS
373 * channel's data is handled above - it always goes through
374 * ppp_generic.
375 */
376 if (channel_idx == IPW_CHANNEL_RAS
377 && (network->ras_control_lines &
378 IPW_CONTROL_LINE_DCD) != 0
379 && ipwireless_tty_is_modem(tty)) {
380 /*
381 * If data came in on the RAS channel and this tty is
382 * the modem tty, and we are online, then we send it to
383 * the PPP layer.
384 */
385 mutex_lock(&network->close_lock);
386 spin_lock_irqsave(&network->lock, flags);
387 if (network->ppp_channel != NULL) {
388 struct sk_buff *skb;
389
390 spin_unlock_irqrestore(&network->lock,
391 flags);
392
393 /* Send the data to the ppp_generic module. */
394 skb = ipw_packet_received_skb(data, length);
395 ppp_input(network->ppp_channel, skb);
396 } else
397 spin_unlock_irqrestore(&network->lock,
398 flags);
399 mutex_unlock(&network->close_lock);
400 }
401 /* Otherwise we send it out the tty. */
402 else
403 ipwireless_tty_received(tty, data, length);
404 }
405}
406
407struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw)
408{
409 struct ipw_network *network =
410 kzalloc(sizeof(struct ipw_network), GFP_ATOMIC);
411
412 if (!network)
413 return NULL;
414
415 spin_lock_init(&network->lock);
416 mutex_init(&network->close_lock);
417
418 network->hardware = hw;
419
420 INIT_WORK(&network->work_go_online, do_go_online);
421 INIT_WORK(&network->work_go_offline, do_go_offline);
422
423 ipwireless_associate_network(hw, network);
424
425 return network;
426}
427
428void ipwireless_network_free(struct ipw_network *network)
429{
430 network->shutting_down = 1;
431
432 ipwireless_ppp_close(network);
433 flush_work_sync(&network->work_go_online);
434 flush_work_sync(&network->work_go_offline);
435
436 ipwireless_stop_interrupts(network->hardware);
437 ipwireless_associate_network(network->hardware, NULL);
438
439 kfree(network);
440}
441
442void ipwireless_associate_network_tty(struct ipw_network *network,
443 unsigned int channel_idx,
444 struct ipw_tty *tty)
445{
446 int i;
447
448 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
449 if (network->associated_ttys[channel_idx][i] == NULL) {
450 network->associated_ttys[channel_idx][i] = tty;
451 break;
452 }
453}
454
455void ipwireless_disassociate_network_ttys(struct ipw_network *network,
456 unsigned int channel_idx)
457{
458 int i;
459
460 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
461 network->associated_ttys[channel_idx][i] = NULL;
462}
463
464void ipwireless_ppp_open(struct ipw_network *network)
465{
466 if (ipwireless_debug)
467 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": online\n");
468 schedule_work(&network->work_go_online);
469}
470
471void ipwireless_ppp_close(struct ipw_network *network)
472{
473 /* Disconnect from the wireless network. */
474 if (ipwireless_debug)
475 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": offline\n");
476 schedule_work(&network->work_go_offline);
477}
478
479int ipwireless_ppp_channel_index(struct ipw_network *network)
480{
481 int ret = -1;
482 unsigned long flags;
483
484 spin_lock_irqsave(&network->lock, flags);
485 if (network->ppp_channel != NULL)
486 ret = ppp_channel_index(network->ppp_channel);
487 spin_unlock_irqrestore(&network->lock, flags);
488
489 return ret;
490}
491
492int ipwireless_ppp_unit_number(struct ipw_network *network)
493{
494 int ret = -1;
495 unsigned long flags;
496
497 spin_lock_irqsave(&network->lock, flags);
498 if (network->ppp_channel != NULL)
499 ret = ppp_unit_number(network->ppp_channel);
500 spin_unlock_irqrestore(&network->lock, flags);
501
502 return ret;
503}
504
505int ipwireless_ppp_mru(const struct ipw_network *network)
506{
507 return network->mru;
508}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * IPWireless 3G PCMCIA Network Driver
4 *
5 * Original code
6 * by Stephen Blackheath <stephen@blacksapphire.com>,
7 * Ben Martel <benm@symmetric.co.nz>
8 *
9 * Copyrighted as follows:
10 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
11 *
12 * Various driver changes and rewrites, port to new kernels
13 * Copyright (C) 2006-2007 Jiri Kosina
14 *
15 * Misc code cleanups and updates
16 * Copyright (C) 2007 David Sterba
17 */
18
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>
28
29#include "network.h"
30#include "hardware.h"
31#include "main.h"
32#include "tty.h"
33
34#define MAX_ASSOCIATED_TTYS 2
35
36#define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
37
38struct ipw_network {
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 */
46 int ppp_blocked;
47 /* Number of packets queued up in hardware module. */
48 int outgoing_packets_queued;
49 /* Spinlock to avoid interrupts during shutdown */
50 spinlock_t lock;
51 struct mutex close_lock;
52
53 /* PPP ioctl data, not actually used anywere */
54 unsigned int flags;
55 unsigned int rbits;
56 u32 xaccm[8];
57 u32 raccm;
58 int mru;
59
60 int shutting_down;
61 unsigned int ras_control_lines;
62
63 struct work_struct work_go_online;
64 struct work_struct work_go_offline;
65};
66
67static void notify_packet_sent(void *callback_data, unsigned int packet_length)
68{
69 struct ipw_network *network = callback_data;
70 unsigned long flags;
71
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);
79 if (ipwireless_debug)
80 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
81 ": ppp unblocked\n");
82 } else
83 spin_unlock_irqrestore(&network->lock, flags);
84 } else
85 spin_unlock_irqrestore(&network->lock, flags);
86}
87
88/*
89 * Called by the ppp system when it has a packet to send to the hardware.
90 */
91static int ipwireless_ppp_start_xmit(struct ppp_channel *ppp_channel,
92 struct sk_buff *skb)
93{
94 struct ipw_network *network = ppp_channel->private;
95 unsigned long flags;
96
97 spin_lock_irqsave(&network->lock, flags);
98 if (network->outgoing_packets_queued < ipwireless_out_queue) {
99 unsigned char *buf;
100 static unsigned char header[] = {
101 PPP_ALLSTATIONS, /* 0xff */
102 PPP_UI, /* 0x03 */
103 };
104 int ret;
105
106 network->outgoing_packets_queued++;
107 spin_unlock_irqrestore(&network->lock, flags);
108
109 /*
110 * If we have the requested amount of headroom in the skb we
111 * were handed, then we can add the header efficiently.
112 */
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,
117 skb->len,
118 notify_packet_sent,
119 network);
120 if (ret < 0) {
121 skb_pull(skb, 2);
122 return 0;
123 }
124 } else {
125 /* Otherwise (rarely) we do it inefficiently. */
126 buf = kmalloc(skb->len + 2, GFP_ATOMIC);
127 if (!buf)
128 return 0;
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,
133 skb->len + 2,
134 notify_packet_sent,
135 network);
136 kfree(buf);
137 if (ret < 0)
138 return 0;
139 }
140 kfree_skb(skb);
141 return 1;
142 } else {
143 /*
144 * Otherwise reject the packet, and flag that the ppp system
145 * needs to be unblocked once we are ready to send.
146 */
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");
151 return 0;
152 }
153}
154
155/* Handle an ioctl call that has come in via ppp. (copy of ppp_async_ioctl() */
156static int ipwireless_ppp_ioctl(struct ppp_channel *ppp_channel,
157 unsigned int cmd, unsigned long arg)
158{
159 struct ipw_network *network = ppp_channel->private;
160 int err, val;
161 u32 accm[8];
162 int __user *user_arg = (int __user *) arg;
163
164 err = -EFAULT;
165 switch (cmd) {
166 case PPPIOCGFLAGS:
167 val = network->flags | network->rbits;
168 if (put_user(val, user_arg))
169 break;
170 err = 0;
171 break;
172
173 case PPPIOCSFLAGS:
174 if (get_user(val, user_arg))
175 break;
176 network->flags = val & ~SC_RCV_BITS;
177 network->rbits = val & SC_RCV_BITS;
178 err = 0;
179 break;
180
181 case PPPIOCGASYNCMAP:
182 if (put_user(network->xaccm[0], user_arg))
183 break;
184 err = 0;
185 break;
186
187 case PPPIOCSASYNCMAP:
188 if (get_user(network->xaccm[0], user_arg))
189 break;
190 err = 0;
191 break;
192
193 case PPPIOCGRASYNCMAP:
194 if (put_user(network->raccm, user_arg))
195 break;
196 err = 0;
197 break;
198
199 case PPPIOCSRASYNCMAP:
200 if (get_user(network->raccm, user_arg))
201 break;
202 err = 0;
203 break;
204
205 case PPPIOCGXASYNCMAP:
206 if (copy_to_user((void __user *) arg, network->xaccm,
207 sizeof(network->xaccm)))
208 break;
209 err = 0;
210 break;
211
212 case PPPIOCSXASYNCMAP:
213 if (copy_from_user(accm, (void __user *) arg, sizeof(accm)))
214 break;
215 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
216 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
217 memcpy(network->xaccm, accm, sizeof(network->xaccm));
218 err = 0;
219 break;
220
221 case PPPIOCGMRU:
222 if (put_user(network->mru, user_arg))
223 break;
224 err = 0;
225 break;
226
227 case PPPIOCSMRU:
228 if (get_user(val, user_arg))
229 break;
230 if (val < PPP_MRU)
231 val = PPP_MRU;
232 network->mru = val;
233 err = 0;
234 break;
235
236 default:
237 err = -ENOTTY;
238 }
239
240 return err;
241}
242
243static const struct ppp_channel_ops ipwireless_ppp_channel_ops = {
244 .start_xmit = ipwireless_ppp_start_xmit,
245 .ioctl = ipwireless_ppp_ioctl
246};
247
248static void do_go_online(struct work_struct *work_go_online)
249{
250 struct ipw_network *network =
251 container_of(work_go_online, struct ipw_network,
252 work_go_online);
253 unsigned long flags;
254
255 spin_lock_irqsave(&network->lock, flags);
256 if (!network->ppp_channel) {
257 struct ppp_channel *channel;
258
259 spin_unlock_irqrestore(&network->lock, flags);
260 channel = kzalloc(sizeof(struct ppp_channel), GFP_KERNEL);
261 if (!channel) {
262 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
263 ": unable to allocate PPP channel\n");
264 return;
265 }
266 channel->private = network;
267 channel->mtu = 16384; /* Wild guess */
268 channel->hdrlen = 2;
269 channel->ops = &ipwireless_ppp_channel_ops;
270
271 network->flags = 0;
272 network->rbits = 0;
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");
281 kfree(channel);
282 return;
283 }
284 spin_lock_irqsave(&network->lock, flags);
285 network->ppp_channel = channel;
286 }
287 spin_unlock_irqrestore(&network->lock, flags);
288}
289
290static void do_go_offline(struct work_struct *work_go_offline)
291{
292 struct ipw_network *network =
293 container_of(work_go_offline, struct ipw_network,
294 work_go_offline);
295 unsigned long flags;
296
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;
301
302 network->ppp_channel = NULL;
303 spin_unlock_irqrestore(&network->lock, flags);
304 mutex_unlock(&network->close_lock);
305 ppp_unregister_channel(channel);
306 } else {
307 spin_unlock_irqrestore(&network->lock, flags);
308 mutex_unlock(&network->close_lock);
309 }
310}
311
312void 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)
316{
317 int i;
318
319 if (channel_idx == IPW_CHANNEL_RAS)
320 network->ras_control_lines = control_lines;
321
322 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
323 struct ipw_tty *tty =
324 network->associated_ttys[channel_idx][i];
325
326 /*
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
330 * ppp_generic.
331 */
332 if (tty)
333 ipwireless_tty_notify_control_line_change(tty,
334 channel_idx,
335 control_lines,
336 changed_mask);
337 }
338}
339
340/*
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
343 * packets
344 */
345static struct sk_buff *ipw_packet_received_skb(unsigned char *data,
346 unsigned int length)
347{
348 struct sk_buff *skb;
349
350 if (length > 2 && data[0] == PPP_ALLSTATIONS && data[1] == PPP_UI) {
351 length -= 2;
352 data += 2;
353 }
354
355 skb = dev_alloc_skb(length + 4);
356 if (skb == NULL)
357 return NULL;
358 skb_reserve(skb, 2);
359 skb_put_data(skb, data, length);
360
361 return skb;
362}
363
364void ipwireless_network_packet_received(struct ipw_network *network,
365 unsigned int channel_idx,
366 unsigned char *data,
367 unsigned int length)
368{
369 int i;
370 unsigned long flags;
371
372 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
373 struct ipw_tty *tty = network->associated_ttys[channel_idx][i];
374
375 if (!tty)
376 continue;
377
378 /*
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
382 * ppp_generic.
383 */
384 if (channel_idx == IPW_CHANNEL_RAS
385 && (network->ras_control_lines &
386 IPW_CONTROL_LINE_DCD) != 0
387 && ipwireless_tty_is_modem(tty)) {
388 /*
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
391 * the PPP layer.
392 */
393 mutex_lock(&network->close_lock);
394 spin_lock_irqsave(&network->lock, flags);
395 if (network->ppp_channel != NULL) {
396 struct sk_buff *skb;
397
398 spin_unlock_irqrestore(&network->lock,
399 flags);
400
401 /* Send the data to the ppp_generic module. */
402 skb = ipw_packet_received_skb(data, length);
403 if (skb)
404 ppp_input(network->ppp_channel, skb);
405 } else
406 spin_unlock_irqrestore(&network->lock,
407 flags);
408 mutex_unlock(&network->close_lock);
409 }
410 /* Otherwise we send it out the tty. */
411 else
412 ipwireless_tty_received(tty, data, length);
413 }
414}
415
416struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw)
417{
418 struct ipw_network *network =
419 kzalloc(sizeof(struct ipw_network), GFP_KERNEL);
420
421 if (!network)
422 return NULL;
423
424 spin_lock_init(&network->lock);
425 mutex_init(&network->close_lock);
426
427 network->hardware = hw;
428
429 INIT_WORK(&network->work_go_online, do_go_online);
430 INIT_WORK(&network->work_go_offline, do_go_offline);
431
432 ipwireless_associate_network(hw, network);
433
434 return network;
435}
436
437void ipwireless_network_free(struct ipw_network *network)
438{
439 network->shutting_down = 1;
440
441 ipwireless_ppp_close(network);
442 flush_work(&network->work_go_online);
443 flush_work(&network->work_go_offline);
444
445 ipwireless_stop_interrupts(network->hardware);
446 ipwireless_associate_network(network->hardware, NULL);
447
448 kfree(network);
449}
450
451void ipwireless_associate_network_tty(struct ipw_network *network,
452 unsigned int channel_idx,
453 struct ipw_tty *tty)
454{
455 int i;
456
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;
460 break;
461 }
462}
463
464void ipwireless_disassociate_network_ttys(struct ipw_network *network,
465 unsigned int channel_idx)
466{
467 int i;
468
469 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
470 network->associated_ttys[channel_idx][i] = NULL;
471}
472
473void ipwireless_ppp_open(struct ipw_network *network)
474{
475 if (ipwireless_debug)
476 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": online\n");
477 schedule_work(&network->work_go_online);
478}
479
480void ipwireless_ppp_close(struct ipw_network *network)
481{
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);
486}
487
488int ipwireless_ppp_channel_index(struct ipw_network *network)
489{
490 int ret = -1;
491 unsigned long flags;
492
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);
497
498 return ret;
499}
500
501int ipwireless_ppp_unit_number(struct ipw_network *network)
502{
503 int ret = -1;
504 unsigned long flags;
505
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);
510
511 return ret;
512}
513
514int ipwireless_ppp_mru(const struct ipw_network *network)
515{
516 return network->mru;
517}