]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/net/wireless/strip.c
[SK_BUFF]: Introduce skb_reset_mac_header(skb)
[mirror_ubuntu-zesty-kernel.git] / drivers / net / wireless / strip.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright 1996 The Board of Trustees of The Leland Stanford
3 * Junior University. All Rights Reserved.
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies. Stanford University
9 * makes no representations about the suitability of this
10 * software for any purpose. It is provided "as is" without
11 * express or implied warranty.
12 *
13 * strip.c This module implements Starmode Radio IP (STRIP)
14 * for kernel-based devices like TTY. It interfaces between a
15 * raw TTY, and the kernel's INET protocol layers (via DDI).
16 *
17 * Version: @(#)strip.c 1.3 July 1997
18 *
19 * Author: Stuart Cheshire <cheshire@cs.stanford.edu>
20 *
21 * Fixes: v0.9 12th Feb 1996 (SC)
22 * New byte stuffing (2+6 run-length encoding)
23 * New watchdog timer task
24 * New Protocol key (SIP0)
25 *
26 * v0.9.1 3rd March 1996 (SC)
27 * Changed to dynamic device allocation -- no more compile
28 * time (or boot time) limit on the number of STRIP devices.
29 *
30 * v0.9.2 13th March 1996 (SC)
31 * Uses arp cache lookups (but doesn't send arp packets yet)
32 *
33 * v0.9.3 17th April 1996 (SC)
34 * Fixed bug where STR_ERROR flag was getting set unneccessarily
35 * (causing otherwise good packets to be unneccessarily dropped)
36 *
37 * v0.9.4 27th April 1996 (SC)
38 * First attempt at using "&COMMAND" Starmode AT commands
39 *
40 * v0.9.5 29th May 1996 (SC)
41 * First attempt at sending (unicast) ARP packets
42 *
43 * v0.9.6 5th June 1996 (Elliot)
44 * Put "message level" tags in every "printk" statement
45 *
46 * v0.9.7 13th June 1996 (laik)
47 * Added support for the /proc fs
48 *
49 * v0.9.8 July 1996 (Mema)
50 * Added packet logging
51 *
52 * v1.0 November 1996 (SC)
53 * Fixed (severe) memory leaks in the /proc fs code
54 * Fixed race conditions in the logging code
55 *
56 * v1.1 January 1997 (SC)
57 * Deleted packet logging (use tcpdump instead)
58 * Added support for Metricom Firmware v204 features
59 * (like message checksums)
60 *
61 * v1.2 January 1997 (SC)
62 * Put portables list back in
63 *
64 * v1.3 July 1997 (SC)
65 * Made STRIP driver set the radio's baud rate automatically.
66 * It is no longer necessarily to manually set the radio's
67 * rate permanently to 115200 -- the driver handles setting
68 * the rate automatically.
69 */
70
71#ifdef MODULE
72static const char StripVersion[] = "1.3A-STUART.CHESHIRE-MODULAR";
73#else
74static const char StripVersion[] = "1.3A-STUART.CHESHIRE";
75#endif
76
77#define TICKLE_TIMERS 0
78#define EXT_COUNTERS 1
79
80
81/************************************************************************/
82/* Header files */
83
1da177e4
LT
84#include <linux/kernel.h>
85#include <linux/module.h>
86#include <linux/init.h>
87#include <linux/bitops.h>
88#include <asm/system.h>
89#include <asm/uaccess.h>
90
91# include <linux/ctype.h>
92#include <linux/string.h>
93#include <linux/mm.h>
94#include <linux/interrupt.h>
95#include <linux/in.h>
96#include <linux/tty.h>
97#include <linux/errno.h>
98#include <linux/netdevice.h>
99#include <linux/inetdevice.h>
100#include <linux/etherdevice.h>
101#include <linux/skbuff.h>
102#include <linux/if_arp.h>
103#include <linux/if_strip.h>
104#include <linux/proc_fs.h>
105#include <linux/seq_file.h>
106#include <linux/serial.h>
107#include <linux/serialP.h>
108#include <linux/rcupdate.h>
109#include <net/arp.h>
110
111#include <linux/ip.h>
112#include <linux/tcp.h>
113#include <linux/time.h>
ff5688ae 114#include <linux/jiffies.h>
1da177e4
LT
115
116/************************************************************************/
117/* Useful structures and definitions */
118
119/*
120 * A MetricomKey identifies the protocol being carried inside a Metricom
121 * Starmode packet.
122 */
123
124typedef union {
125 __u8 c[4];
126 __u32 l;
127} MetricomKey;
128
129/*
130 * An IP address can be viewed as four bytes in memory (which is what it is) or as
131 * a single 32-bit long (which is convenient for assignment, equality testing etc.)
132 */
133
134typedef union {
135 __u8 b[4];
136 __u32 l;
137} IPaddr;
138
139/*
140 * A MetricomAddressString is used to hold a printable representation of
141 * a Metricom address.
142 */
143
144typedef struct {
145 __u8 c[24];
146} MetricomAddressString;
147
148/* Encapsulation can expand packet of size x to 65/64x + 1
149 * Sent packet looks like "<CR>*<address>*<key><encaps payload><CR>"
150 * 1 1 1-18 1 4 ? 1
151 * eg. <CR>*0000-1234*SIP0<encaps payload><CR>
152 * We allow 31 bytes for the stars, the key, the address and the <CR>s
153 */
154#define STRIP_ENCAP_SIZE(X) (32 + (X)*65L/64L)
155
156/*
157 * A STRIP_Header is never really sent over the radio, but making a dummy
158 * header for internal use within the kernel that looks like an Ethernet
159 * header makes certain other software happier. For example, tcpdump
160 * already understands Ethernet headers.
161 */
162
163typedef struct {
164 MetricomAddress dst_addr; /* Destination address, e.g. "0000-1234" */
165 MetricomAddress src_addr; /* Source address, e.g. "0000-5678" */
166 unsigned short protocol; /* The protocol type, using Ethernet codes */
167} STRIP_Header;
168
169typedef struct {
170 char c[60];
171} MetricomNode;
172
173#define NODE_TABLE_SIZE 32
174typedef struct {
175 struct timeval timestamp;
176 int num_nodes;
177 MetricomNode node[NODE_TABLE_SIZE];
178} MetricomNodeTable;
179
180enum { FALSE = 0, TRUE = 1 };
181
182/*
183 * Holds the radio's firmware version.
184 */
185typedef struct {
186 char c[50];
187} FirmwareVersion;
188
189/*
190 * Holds the radio's serial number.
191 */
192typedef struct {
193 char c[18];
194} SerialNumber;
195
196/*
197 * Holds the radio's battery voltage.
198 */
199typedef struct {
200 char c[11];
201} BatteryVoltage;
202
203typedef struct {
204 char c[8];
205} char8;
206
207enum {
208 NoStructure = 0, /* Really old firmware */
209 StructuredMessages = 1, /* Parsable AT response msgs */
210 ChecksummedMessages = 2 /* Parsable AT response msgs with checksums */
ff1d2767 211};
1da177e4
LT
212
213struct strip {
214 int magic;
215 /*
216 * These are pointers to the malloc()ed frame buffers.
217 */
218
219 unsigned char *rx_buff; /* buffer for received IP packet */
220 unsigned char *sx_buff; /* buffer for received serial data */
221 int sx_count; /* received serial data counter */
222 int sx_size; /* Serial buffer size */
223 unsigned char *tx_buff; /* transmitter buffer */
224 unsigned char *tx_head; /* pointer to next byte to XMIT */
225 int tx_left; /* bytes left in XMIT queue */
226 int tx_size; /* Serial buffer size */
227
228 /*
229 * STRIP interface statistics.
230 */
231
232 unsigned long rx_packets; /* inbound frames counter */
233 unsigned long tx_packets; /* outbound frames counter */
234 unsigned long rx_errors; /* Parity, etc. errors */
235 unsigned long tx_errors; /* Planned stuff */
236 unsigned long rx_dropped; /* No memory for skb */
237 unsigned long tx_dropped; /* When MTU change */
238 unsigned long rx_over_errors; /* Frame bigger then STRIP buf. */
239
240 unsigned long pps_timer; /* Timer to determine pps */
241 unsigned long rx_pps_count; /* Counter to determine pps */
242 unsigned long tx_pps_count; /* Counter to determine pps */
243 unsigned long sx_pps_count; /* Counter to determine pps */
244 unsigned long rx_average_pps; /* rx packets per second * 8 */
245 unsigned long tx_average_pps; /* tx packets per second * 8 */
246 unsigned long sx_average_pps; /* sent packets per second * 8 */
247
248#ifdef EXT_COUNTERS
249 unsigned long rx_bytes; /* total received bytes */
250 unsigned long tx_bytes; /* total received bytes */
251 unsigned long rx_rbytes; /* bytes thru radio i/f */
252 unsigned long tx_rbytes; /* bytes thru radio i/f */
253 unsigned long rx_sbytes; /* tot bytes thru serial i/f */
254 unsigned long tx_sbytes; /* tot bytes thru serial i/f */
255 unsigned long rx_ebytes; /* tot stat/err bytes */
256 unsigned long tx_ebytes; /* tot stat/err bytes */
257#endif
258
259 /*
260 * Internal variables.
261 */
262
263 struct list_head list; /* Linked list of devices */
264
265 int discard; /* Set if serial error */
266 int working; /* Is radio working correctly? */
267 int firmware_level; /* Message structuring level */
268 int next_command; /* Next periodic command */
269 unsigned int user_baud; /* The user-selected baud rate */
270 int mtu; /* Our mtu (to spot changes!) */
271 long watchdog_doprobe; /* Next time to test the radio */
272 long watchdog_doreset; /* Time to do next reset */
273 long gratuitous_arp; /* Time to send next ARP refresh */
274 long arp_interval; /* Next ARP interval */
275 struct timer_list idle_timer; /* For periodic wakeup calls */
276 MetricomAddress true_dev_addr; /* True address of radio */
277 int manual_dev_addr; /* Hack: See note below */
278
279 FirmwareVersion firmware_version; /* The radio's firmware version */
280 SerialNumber serial_number; /* The radio's serial number */
281 BatteryVoltage battery_voltage; /* The radio's battery voltage */
282
283 /*
284 * Other useful structures.
285 */
286
287 struct tty_struct *tty; /* ptr to TTY structure */
288 struct net_device *dev; /* Our device structure */
289
290 /*
291 * Neighbour radio records
292 */
293
294 MetricomNodeTable portables;
295 MetricomNodeTable poletops;
296};
297
298/*
299 * Note: manual_dev_addr hack
300 *
301 * It is not possible to change the hardware address of a Metricom radio,
302 * or to send packets with a user-specified hardware source address, thus
303 * trying to manually set a hardware source address is a questionable
304 * thing to do. However, if the user *does* manually set the hardware
305 * source address of a STRIP interface, then the kernel will believe it,
306 * and use it in certain places. For example, the hardware address listed
307 * by ifconfig will be the manual address, not the true one.
308 * (Both addresses are listed in /proc/net/strip.)
309 * Also, ARP packets will be sent out giving the user-specified address as
310 * the source address, not the real address. This is dangerous, because
311 * it means you won't receive any replies -- the ARP replies will go to
312 * the specified address, which will be some other radio. The case where
313 * this is useful is when that other radio is also connected to the same
314 * machine. This allows you to connect a pair of radios to one machine,
315 * and to use one exclusively for inbound traffic, and the other
316 * exclusively for outbound traffic. Pretty neat, huh?
317 *
318 * Here's the full procedure to set this up:
319 *
320 * 1. "slattach" two interfaces, e.g. st0 for outgoing packets,
321 * and st1 for incoming packets
322 *
323 * 2. "ifconfig" st0 (outbound radio) to have the hardware address
324 * which is the real hardware address of st1 (inbound radio).
325 * Now when it sends out packets, it will masquerade as st1, and
326 * replies will be sent to that radio, which is exactly what we want.
327 *
328 * 3. Set the route table entry ("route add default ..." or
329 * "route add -net ...", as appropriate) to send packets via the st0
330 * interface (outbound radio). Do not add any route which sends packets
331 * out via the st1 interface -- that radio is for inbound traffic only.
332 *
333 * 4. "ifconfig" st1 (inbound radio) to have hardware address zero.
334 * This tells the STRIP driver to "shut down" that interface and not
335 * send any packets through it. In particular, it stops sending the
336 * periodic gratuitous ARP packets that a STRIP interface normally sends.
337 * Also, when packets arrive on that interface, it will search the
338 * interface list to see if there is another interface who's manual
339 * hardware address matches its own real address (i.e. st0 in this
340 * example) and if so it will transfer ownership of the skbuff to
341 * that interface, so that it looks to the kernel as if the packet
342 * arrived on that interface. This is necessary because when the
343 * kernel sends an ARP packet on st0, it expects to get a reply on
344 * st0, and if it sees the reply come from st1 then it will ignore
345 * it (to be accurate, it puts the entry in the ARP table, but
346 * labelled in such a way that st0 can't use it).
347 *
348 * Thanks to Petros Maniatis for coming up with the idea of splitting
349 * inbound and outbound traffic between two interfaces, which turned
350 * out to be really easy to implement, even if it is a bit of a hack.
351 *
352 * Having set a manual address on an interface, you can restore it
353 * to automatic operation (where the address is automatically kept
354 * consistent with the real address of the radio) by setting a manual
355 * address of all ones, e.g. "ifconfig st0 hw strip FFFFFFFFFFFF"
356 * This 'turns off' manual override mode for the device address.
357 *
358 * Note: The IEEE 802 headers reported in tcpdump will show the *real*
359 * radio addresses the packets were sent and received from, so that you
360 * can see what is really going on with packets, and which interfaces
361 * they are really going through.
362 */
363
364
365/************************************************************************/
366/* Constants */
367
368/*
369 * CommandString1 works on all radios
370 * Other CommandStrings are only used with firmware that provides structured responses.
371 *
372 * ats319=1 Enables Info message for node additions and deletions
373 * ats319=2 Enables Info message for a new best node
374 * ats319=4 Enables checksums
375 * ats319=8 Enables ACK messages
376 */
377
378static const int MaxCommandStringLength = 32;
379static const int CompatibilityCommand = 1;
380
381static const char CommandString0[] = "*&COMMAND*ATS319=7"; /* Turn on checksums & info messages */
382static const char CommandString1[] = "*&COMMAND*ATS305?"; /* Query radio name */
383static const char CommandString2[] = "*&COMMAND*ATS325?"; /* Query battery voltage */
384static const char CommandString3[] = "*&COMMAND*ATS300?"; /* Query version information */
385static const char CommandString4[] = "*&COMMAND*ATS311?"; /* Query poletop list */
386static const char CommandString5[] = "*&COMMAND*AT~LA"; /* Query portables list */
387typedef struct {
388 const char *string;
389 long length;
390} StringDescriptor;
391
392static const StringDescriptor CommandString[] = {
393 {CommandString0, sizeof(CommandString0) - 1},
394 {CommandString1, sizeof(CommandString1) - 1},
395 {CommandString2, sizeof(CommandString2) - 1},
396 {CommandString3, sizeof(CommandString3) - 1},
397 {CommandString4, sizeof(CommandString4) - 1},
398 {CommandString5, sizeof(CommandString5) - 1}
399};
400
401#define GOT_ALL_RADIO_INFO(S) \
402 ((S)->firmware_version.c[0] && \
403 (S)->battery_voltage.c[0] && \
404 memcmp(&(S)->true_dev_addr, zero_address.c, sizeof(zero_address)))
405
406static const char hextable[16] = "0123456789ABCDEF";
407
408static const MetricomAddress zero_address;
409static const MetricomAddress broadcast_address =
410 { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} };
411
412static const MetricomKey SIP0Key = { "SIP0" };
413static const MetricomKey ARP0Key = { "ARP0" };
414static const MetricomKey ATR_Key = { "ATR " };
415static const MetricomKey ACK_Key = { "ACK_" };
416static const MetricomKey INF_Key = { "INF_" };
417static const MetricomKey ERR_Key = { "ERR_" };
418
419static const long MaxARPInterval = 60 * HZ; /* One minute */
420
421/*
422 * Maximum Starmode packet length is 1183 bytes. Allowing 4 bytes for
423 * protocol key, 4 bytes for checksum, one byte for CR, and 65/64 expansion
424 * for STRIP encoding, that translates to a maximum payload MTU of 1155.
425 * Note: A standard NFS 1K data packet is a total of 0x480 (1152) bytes
426 * long, including IP header, UDP header, and NFS header. Setting the STRIP
427 * MTU to 1152 allows us to send default sized NFS packets without fragmentation.
428 */
429static const unsigned short MAX_SEND_MTU = 1152;
430static const unsigned short MAX_RECV_MTU = 1500; /* Hoping for Ethernet sized packets in the future! */
431static const unsigned short DEFAULT_STRIP_MTU = 1152;
432static const int STRIP_MAGIC = 0x5303;
433static const long LongTime = 0x7FFFFFFF;
434
435/************************************************************************/
436/* Global variables */
437
438static LIST_HEAD(strip_list);
439static DEFINE_SPINLOCK(strip_lock);
440
441/************************************************************************/
442/* Macros */
443
444/* Returns TRUE if text T begins with prefix P */
445#define has_prefix(T,L,P) (((L) >= sizeof(P)-1) && !strncmp((T), (P), sizeof(P)-1))
446
447/* Returns TRUE if text T of length L is equal to string S */
448#define text_equal(T,L,S) (((L) == sizeof(S)-1) && !strncmp((T), (S), sizeof(S)-1))
449
450#define READHEX(X) ((X)>='0' && (X)<='9' ? (X)-'0' : \
451 (X)>='a' && (X)<='f' ? (X)-'a'+10 : \
452 (X)>='A' && (X)<='F' ? (X)-'A'+10 : 0 )
453
454#define READHEX16(X) ((__u16)(READHEX(X)))
455
456#define READDEC(X) ((X)>='0' && (X)<='9' ? (X)-'0' : 0)
457
458#define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
459
460#define JIFFIE_TO_SEC(X) ((X) / HZ)
461
462
463/************************************************************************/
464/* Utility routines */
465
466static int arp_query(unsigned char *haddr, u32 paddr,
467 struct net_device *dev)
468{
469 struct neighbour *neighbor_entry;
ee1377c3 470 int ret = 0;
1da177e4
LT
471
472 neighbor_entry = neigh_lookup(&arp_tbl, &paddr, dev);
473
474 if (neighbor_entry != NULL) {
475 neighbor_entry->used = jiffies;
476 if (neighbor_entry->nud_state & NUD_VALID) {
477 memcpy(haddr, neighbor_entry->ha, dev->addr_len);
ee1377c3 478 ret = 1;
1da177e4 479 }
ee1377c3 480 neigh_release(neighbor_entry);
1da177e4 481 }
ee1377c3 482 return ret;
1da177e4
LT
483}
484
485static void DumpData(char *msg, struct strip *strip_info, __u8 * ptr,
486 __u8 * end)
487{
488 static const int MAX_DumpData = 80;
489 __u8 pkt_text[MAX_DumpData], *p = pkt_text;
490
491 *p++ = '\"';
492
493 while (ptr < end && p < &pkt_text[MAX_DumpData - 4]) {
494 if (*ptr == '\\') {
495 *p++ = '\\';
496 *p++ = '\\';
497 } else {
498 if (*ptr >= 32 && *ptr <= 126) {
499 *p++ = *ptr;
500 } else {
501 sprintf(p, "\\%02X", *ptr);
502 p += 3;
503 }
504 }
505 ptr++;
506 }
507
508 if (ptr == end)
509 *p++ = '\"';
510 *p++ = 0;
511
512 printk(KERN_INFO "%s: %-13s%s\n", strip_info->dev->name, msg, pkt_text);
513}
514
515
516/************************************************************************/
517/* Byte stuffing/unstuffing routines */
518
519/* Stuffing scheme:
520 * 00 Unused (reserved character)
521 * 01-3F Run of 2-64 different characters
522 * 40-7F Run of 1-64 different characters plus a single zero at the end
523 * 80-BF Run of 1-64 of the same character
524 * C0-FF Run of 1-64 zeroes (ASCII 0)
525 */
526
527typedef enum {
528 Stuff_Diff = 0x00,
529 Stuff_DiffZero = 0x40,
530 Stuff_Same = 0x80,
531 Stuff_Zero = 0xC0,
532 Stuff_NoCode = 0xFF, /* Special code, meaning no code selected */
533
534 Stuff_CodeMask = 0xC0,
535 Stuff_CountMask = 0x3F,
536 Stuff_MaxCount = 0x3F,
537 Stuff_Magic = 0x0D /* The value we are eliminating */
538} StuffingCode;
539
540/* StuffData encodes the data starting at "src" for "length" bytes.
541 * It writes it to the buffer pointed to by "dst" (which must be at least
542 * as long as 1 + 65/64 of the input length). The output may be up to 1.6%
543 * larger than the input for pathological input, but will usually be smaller.
544 * StuffData returns the new value of the dst pointer as its result.
545 * "code_ptr_ptr" points to a "__u8 *" which is used to hold encoding state
546 * between calls, allowing an encoded packet to be incrementally built up
547 * from small parts. On the first call, the "__u8 *" pointed to should be
548 * initialized to NULL; between subsequent calls the calling routine should
549 * leave the value alone and simply pass it back unchanged so that the
550 * encoder can recover its current state.
551 */
552
553#define StuffData_FinishBlock(X) \
554(*code_ptr = (X) ^ Stuff_Magic, code = Stuff_NoCode)
555
556static __u8 *StuffData(__u8 * src, __u32 length, __u8 * dst,
557 __u8 ** code_ptr_ptr)
558{
559 __u8 *end = src + length;
560 __u8 *code_ptr = *code_ptr_ptr;
561 __u8 code = Stuff_NoCode, count = 0;
562
563 if (!length)
564 return (dst);
565
566 if (code_ptr) {
567 /*
568 * Recover state from last call, if applicable
569 */
570 code = (*code_ptr ^ Stuff_Magic) & Stuff_CodeMask;
571 count = (*code_ptr ^ Stuff_Magic) & Stuff_CountMask;
572 }
573
574 while (src < end) {
575 switch (code) {
576 /* Stuff_NoCode: If no current code, select one */
577 case Stuff_NoCode:
578 /* Record where we're going to put this code */
579 code_ptr = dst++;
580 count = 0; /* Reset the count (zero means one instance) */
581 /* Tentatively start a new block */
582 if (*src == 0) {
583 code = Stuff_Zero;
584 src++;
585 } else {
586 code = Stuff_Same;
587 *dst++ = *src++ ^ Stuff_Magic;
588 }
589 /* Note: We optimistically assume run of same -- */
590 /* which will be fixed later in Stuff_Same */
591 /* if it turns out not to be true. */
592 break;
593
594 /* Stuff_Zero: We already have at least one zero encoded */
595 case Stuff_Zero:
596 /* If another zero, count it, else finish this code block */
597 if (*src == 0) {
598 count++;
599 src++;
600 } else {
601 StuffData_FinishBlock(Stuff_Zero + count);
602 }
603 break;
604
605 /* Stuff_Same: We already have at least one byte encoded */
606 case Stuff_Same:
607 /* If another one the same, count it */
608 if ((*src ^ Stuff_Magic) == code_ptr[1]) {
609 count++;
610 src++;
611 break;
612 }
613 /* else, this byte does not match this block. */
614 /* If we already have two or more bytes encoded, finish this code block */
615 if (count) {
616 StuffData_FinishBlock(Stuff_Same + count);
617 break;
618 }
619 /* else, we only have one so far, so switch to Stuff_Diff code */
620 code = Stuff_Diff;
621 /* and fall through to Stuff_Diff case below
622 * Note cunning cleverness here: case Stuff_Diff compares
623 * the current character with the previous two to see if it
624 * has a run of three the same. Won't this be an error if
625 * there aren't two previous characters stored to compare with?
626 * No. Because we know the current character is *not* the same
627 * as the previous one, the first test below will necessarily
628 * fail and the send half of the "if" won't be executed.
629 */
630
631 /* Stuff_Diff: We have at least two *different* bytes encoded */
632 case Stuff_Diff:
633 /* If this is a zero, must encode a Stuff_DiffZero, and begin a new block */
634 if (*src == 0) {
635 StuffData_FinishBlock(Stuff_DiffZero +
636 count);
637 }
638 /* else, if we have three in a row, it is worth starting a Stuff_Same block */
639 else if ((*src ^ Stuff_Magic) == dst[-1]
640 && dst[-1] == dst[-2]) {
641 /* Back off the last two characters we encoded */
642 code += count - 2;
643 /* Note: "Stuff_Diff + 0" is an illegal code */
644 if (code == Stuff_Diff + 0) {
645 code = Stuff_Same + 0;
646 }
647 StuffData_FinishBlock(code);
648 code_ptr = dst - 2;
649 /* dst[-1] already holds the correct value */
650 count = 2; /* 2 means three bytes encoded */
651 code = Stuff_Same;
652 }
653 /* else, another different byte, so add it to the block */
654 else {
655 *dst++ = *src ^ Stuff_Magic;
656 count++;
657 }
658 src++; /* Consume the byte */
659 break;
660 }
661 if (count == Stuff_MaxCount) {
662 StuffData_FinishBlock(code + count);
663 }
664 }
665 if (code == Stuff_NoCode) {
666 *code_ptr_ptr = NULL;
667 } else {
668 *code_ptr_ptr = code_ptr;
669 StuffData_FinishBlock(code + count);
670 }
671 return (dst);
672}
673
674/*
675 * UnStuffData decodes the data at "src", up to (but not including) "end".
676 * It writes the decoded data into the buffer pointed to by "dst", up to a
677 * maximum of "dst_length", and returns the new value of "src" so that a
678 * follow-on call can read more data, continuing from where the first left off.
679 *
680 * There are three types of results:
681 * 1. The source data runs out before extracting "dst_length" bytes:
682 * UnStuffData returns NULL to indicate failure.
683 * 2. The source data produces exactly "dst_length" bytes:
684 * UnStuffData returns new_src = end to indicate that all bytes were consumed.
685 * 3. "dst_length" bytes are extracted, with more remaining.
686 * UnStuffData returns new_src < end to indicate that there are more bytes
687 * to be read.
688 *
689 * Note: The decoding may be destructive, in that it may alter the source
690 * data in the process of decoding it (this is necessary to allow a follow-on
691 * call to resume correctly).
692 */
693
694static __u8 *UnStuffData(__u8 * src, __u8 * end, __u8 * dst,
695 __u32 dst_length)
696{
697 __u8 *dst_end = dst + dst_length;
698 /* Sanity check */
699 if (!src || !end || !dst || !dst_length)
700 return (NULL);
701 while (src < end && dst < dst_end) {
702 int count = (*src ^ Stuff_Magic) & Stuff_CountMask;
703 switch ((*src ^ Stuff_Magic) & Stuff_CodeMask) {
704 case Stuff_Diff:
705 if (src + 1 + count >= end)
706 return (NULL);
707 do {
708 *dst++ = *++src ^ Stuff_Magic;
709 }
710 while (--count >= 0 && dst < dst_end);
711 if (count < 0)
712 src += 1;
713 else {
714 if (count == 0)
715 *src = Stuff_Same ^ Stuff_Magic;
716 else
717 *src =
718 (Stuff_Diff +
719 count) ^ Stuff_Magic;
720 }
721 break;
722 case Stuff_DiffZero:
723 if (src + 1 + count >= end)
724 return (NULL);
725 do {
726 *dst++ = *++src ^ Stuff_Magic;
727 }
728 while (--count >= 0 && dst < dst_end);
729 if (count < 0)
730 *src = Stuff_Zero ^ Stuff_Magic;
731 else
732 *src =
733 (Stuff_DiffZero + count) ^ Stuff_Magic;
734 break;
735 case Stuff_Same:
736 if (src + 1 >= end)
737 return (NULL);
738 do {
739 *dst++ = src[1] ^ Stuff_Magic;
740 }
741 while (--count >= 0 && dst < dst_end);
742 if (count < 0)
743 src += 2;
744 else
745 *src = (Stuff_Same + count) ^ Stuff_Magic;
746 break;
747 case Stuff_Zero:
748 do {
749 *dst++ = 0;
750 }
751 while (--count >= 0 && dst < dst_end);
752 if (count < 0)
753 src += 1;
754 else
755 *src = (Stuff_Zero + count) ^ Stuff_Magic;
756 break;
757 }
758 }
759 if (dst < dst_end)
760 return (NULL);
761 else
762 return (src);
763}
764
765
766/************************************************************************/
767/* General routines for STRIP */
768
769/*
770 * get_baud returns the current baud rate, as one of the constants defined in
771 * termbits.h
772 * If the user has issued a baud rate override using the 'setserial' command
773 * and the logical current rate is set to 38.4, then the true baud rate
774 * currently in effect (57.6 or 115.2) is returned.
775 */
776static unsigned int get_baud(struct tty_struct *tty)
777{
778 if (!tty || !tty->termios)
779 return (0);
780 if ((tty->termios->c_cflag & CBAUD) == B38400 && tty->driver_data) {
781 struct async_struct *info =
782 (struct async_struct *) tty->driver_data;
783 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
784 return (B57600);
785 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
786 return (B115200);
787 }
788 return (tty->termios->c_cflag & CBAUD);
789}
790
791/*
792 * set_baud sets the baud rate to the rate defined by baudcode
793 * Note: The rate B38400 should be avoided, because the user may have
794 * issued a 'setserial' speed override to map that to a different speed.
795 * We could achieve a true rate of 38400 if we needed to by cancelling
796 * any user speed override that is in place, but that might annoy the
797 * user, so it is simplest to just avoid using 38400.
798 */
799static void set_baud(struct tty_struct *tty, unsigned int baudcode)
800{
be90038a 801 struct ktermios old_termios = *(tty->termios);
1da177e4
LT
802 tty->termios->c_cflag &= ~CBAUD; /* Clear the old baud setting */
803 tty->termios->c_cflag |= baudcode; /* Set the new baud setting */
804 tty->driver->set_termios(tty, &old_termios);
805}
806
807/*
808 * Convert a string to a Metricom Address.
809 */
810
811#define IS_RADIO_ADDRESS(p) ( \
812 isdigit((p)[0]) && isdigit((p)[1]) && isdigit((p)[2]) && isdigit((p)[3]) && \
813 (p)[4] == '-' && \
814 isdigit((p)[5]) && isdigit((p)[6]) && isdigit((p)[7]) && isdigit((p)[8]) )
815
816static int string_to_radio_address(MetricomAddress * addr, __u8 * p)
817{
818 if (!IS_RADIO_ADDRESS(p))
819 return (1);
820 addr->c[0] = 0;
821 addr->c[1] = 0;
822 addr->c[2] = READHEX(p[0]) << 4 | READHEX(p[1]);
823 addr->c[3] = READHEX(p[2]) << 4 | READHEX(p[3]);
824 addr->c[4] = READHEX(p[5]) << 4 | READHEX(p[6]);
825 addr->c[5] = READHEX(p[7]) << 4 | READHEX(p[8]);
826 return (0);
827}
828
829/*
830 * Convert a Metricom Address to a string.
831 */
832
833static __u8 *radio_address_to_string(const MetricomAddress * addr,
834 MetricomAddressString * p)
835{
836 sprintf(p->c, "%02X%02X-%02X%02X", addr->c[2], addr->c[3],
837 addr->c[4], addr->c[5]);
838 return (p->c);
839}
840
841/*
842 * Note: Must make sure sx_size is big enough to receive a stuffed
843 * MAX_RECV_MTU packet. Additionally, we also want to ensure that it's
844 * big enough to receive a large radio neighbour list (currently 4K).
845 */
846
847static int allocate_buffers(struct strip *strip_info, int mtu)
848{
849 struct net_device *dev = strip_info->dev;
850 int sx_size = max_t(int, STRIP_ENCAP_SIZE(MAX_RECV_MTU), 4096);
851 int tx_size = STRIP_ENCAP_SIZE(mtu) + MaxCommandStringLength;
852 __u8 *r = kmalloc(MAX_RECV_MTU, GFP_ATOMIC);
853 __u8 *s = kmalloc(sx_size, GFP_ATOMIC);
854 __u8 *t = kmalloc(tx_size, GFP_ATOMIC);
855 if (r && s && t) {
856 strip_info->rx_buff = r;
857 strip_info->sx_buff = s;
858 strip_info->tx_buff = t;
859 strip_info->sx_size = sx_size;
860 strip_info->tx_size = tx_size;
861 strip_info->mtu = dev->mtu = mtu;
862 return (1);
863 }
b4558ea9
JJ
864 kfree(r);
865 kfree(s);
866 kfree(t);
1da177e4
LT
867 return (0);
868}
869
870/*
871 * MTU has been changed by the IP layer.
872 * We could be in
873 * an upcall from the tty driver, or in an ip packet queue.
874 */
875static int strip_change_mtu(struct net_device *dev, int new_mtu)
876{
877 struct strip *strip_info = netdev_priv(dev);
878 int old_mtu = strip_info->mtu;
879 unsigned char *orbuff = strip_info->rx_buff;
880 unsigned char *osbuff = strip_info->sx_buff;
881 unsigned char *otbuff = strip_info->tx_buff;
882
883 if (new_mtu > MAX_SEND_MTU) {
884 printk(KERN_ERR
885 "%s: MTU exceeds maximum allowable (%d), MTU change cancelled.\n",
886 strip_info->dev->name, MAX_SEND_MTU);
887 return -EINVAL;
888 }
889
890 spin_lock_bh(&strip_lock);
891 if (!allocate_buffers(strip_info, new_mtu)) {
892 printk(KERN_ERR "%s: unable to grow strip buffers, MTU change cancelled.\n",
893 strip_info->dev->name);
894 spin_unlock_bh(&strip_lock);
895 return -ENOMEM;
896 }
897
898 if (strip_info->sx_count) {
899 if (strip_info->sx_count <= strip_info->sx_size)
900 memcpy(strip_info->sx_buff, osbuff,
901 strip_info->sx_count);
902 else {
903 strip_info->discard = strip_info->sx_count;
904 strip_info->rx_over_errors++;
905 }
906 }
907
908 if (strip_info->tx_left) {
909 if (strip_info->tx_left <= strip_info->tx_size)
910 memcpy(strip_info->tx_buff, strip_info->tx_head,
911 strip_info->tx_left);
912 else {
913 strip_info->tx_left = 0;
914 strip_info->tx_dropped++;
915 }
916 }
917 strip_info->tx_head = strip_info->tx_buff;
918 spin_unlock_bh(&strip_lock);
919
920 printk(KERN_NOTICE "%s: strip MTU changed fom %d to %d.\n",
921 strip_info->dev->name, old_mtu, strip_info->mtu);
922
b4558ea9
JJ
923 kfree(orbuff);
924 kfree(osbuff);
925 kfree(otbuff);
1da177e4
LT
926 return 0;
927}
928
929static void strip_unlock(struct strip *strip_info)
930{
931 /*
932 * Set the timer to go off in one second.
933 */
934 strip_info->idle_timer.expires = jiffies + 1 * HZ;
935 add_timer(&strip_info->idle_timer);
936 netif_wake_queue(strip_info->dev);
937}
938
939
940
941/*
942 * If the time is in the near future, time_delta prints the number of
943 * seconds to go into the buffer and returns the address of the buffer.
944 * If the time is not in the near future, it returns the address of the
945 * string "Not scheduled" The buffer must be long enough to contain the
946 * ascii representation of the number plus 9 charactes for the " seconds"
947 * and the null character.
948 */
949#ifdef CONFIG_PROC_FS
950static char *time_delta(char buffer[], long time)
951{
952 time -= jiffies;
953 if (time > LongTime / 2)
954 return ("Not scheduled");
955 if (time < 0)
956 time = 0; /* Don't print negative times */
957 sprintf(buffer, "%ld seconds", time / HZ);
958 return (buffer);
959}
960
961/* get Nth element of the linked list */
962static struct strip *strip_get_idx(loff_t pos)
963{
964 struct list_head *l;
965 int i = 0;
966
967 list_for_each_rcu(l, &strip_list) {
968 if (pos == i)
969 return list_entry(l, struct strip, list);
970 ++i;
971 }
972 return NULL;
973}
974
975static void *strip_seq_start(struct seq_file *seq, loff_t *pos)
976{
977 rcu_read_lock();
978 return *pos ? strip_get_idx(*pos - 1) : SEQ_START_TOKEN;
979}
980
981static void *strip_seq_next(struct seq_file *seq, void *v, loff_t *pos)
982{
983 struct list_head *l;
984 struct strip *s;
985
986 ++*pos;
987 if (v == SEQ_START_TOKEN)
988 return strip_get_idx(1);
989
990 s = v;
991 l = &s->list;
992 list_for_each_continue_rcu(l, &strip_list) {
993 return list_entry(l, struct strip, list);
994 }
995 return NULL;
996}
997
998static void strip_seq_stop(struct seq_file *seq, void *v)
999{
1000 rcu_read_unlock();
1001}
1002
1003static void strip_seq_neighbours(struct seq_file *seq,
1004 const MetricomNodeTable * table,
1005 const char *title)
1006{
1007 /* We wrap this in a do/while loop, so if the table changes */
1008 /* while we're reading it, we just go around and try again. */
1009 struct timeval t;
1010
1011 do {
1012 int i;
1013 t = table->timestamp;
1014 if (table->num_nodes)
1015 seq_printf(seq, "\n %s\n", title);
1016 for (i = 0; i < table->num_nodes; i++) {
1017 MetricomNode node;
1018
1019 spin_lock_bh(&strip_lock);
1020 node = table->node[i];
1021 spin_unlock_bh(&strip_lock);
1022 seq_printf(seq, " %s\n", node.c);
1023 }
1024 } while (table->timestamp.tv_sec != t.tv_sec
1025 || table->timestamp.tv_usec != t.tv_usec);
1026}
1027
1028/*
1029 * This function prints radio status information via the seq_file
1030 * interface. The interface takes care of buffer size and over
1031 * run issues.
1032 *
1033 * The buffer in seq_file is PAGESIZE (4K)
1034 * so this routine should never print more or it will get truncated.
1035 * With the maximum of 32 portables and 32 poletops
1036 * reported, the routine outputs 3107 bytes into the buffer.
1037 */
1038static void strip_seq_status_info(struct seq_file *seq,
1039 const struct strip *strip_info)
1040{
1041 char temp[32];
1042 MetricomAddressString addr_string;
1043
1044 /* First, we must copy all of our data to a safe place, */
1045 /* in case a serial interrupt comes in and changes it. */
1046 int tx_left = strip_info->tx_left;
1047 unsigned long rx_average_pps = strip_info->rx_average_pps;
1048 unsigned long tx_average_pps = strip_info->tx_average_pps;
1049 unsigned long sx_average_pps = strip_info->sx_average_pps;
1050 int working = strip_info->working;
1051 int firmware_level = strip_info->firmware_level;
1052 long watchdog_doprobe = strip_info->watchdog_doprobe;
1053 long watchdog_doreset = strip_info->watchdog_doreset;
1054 long gratuitous_arp = strip_info->gratuitous_arp;
1055 long arp_interval = strip_info->arp_interval;
1056 FirmwareVersion firmware_version = strip_info->firmware_version;
1057 SerialNumber serial_number = strip_info->serial_number;
1058 BatteryVoltage battery_voltage = strip_info->battery_voltage;
1059 char *if_name = strip_info->dev->name;
1060 MetricomAddress true_dev_addr = strip_info->true_dev_addr;
1061 MetricomAddress dev_dev_addr =
1062 *(MetricomAddress *) strip_info->dev->dev_addr;
1063 int manual_dev_addr = strip_info->manual_dev_addr;
1064#ifdef EXT_COUNTERS
1065 unsigned long rx_bytes = strip_info->rx_bytes;
1066 unsigned long tx_bytes = strip_info->tx_bytes;
1067 unsigned long rx_rbytes = strip_info->rx_rbytes;
1068 unsigned long tx_rbytes = strip_info->tx_rbytes;
1069 unsigned long rx_sbytes = strip_info->rx_sbytes;
1070 unsigned long tx_sbytes = strip_info->tx_sbytes;
1071 unsigned long rx_ebytes = strip_info->rx_ebytes;
1072 unsigned long tx_ebytes = strip_info->tx_ebytes;
1073#endif
1074
1075 seq_printf(seq, "\nInterface name\t\t%s\n", if_name);
1076 seq_printf(seq, " Radio working:\t\t%s\n", working ? "Yes" : "No");
1077 radio_address_to_string(&true_dev_addr, &addr_string);
1078 seq_printf(seq, " Radio address:\t\t%s\n", addr_string.c);
1079 if (manual_dev_addr) {
1080 radio_address_to_string(&dev_dev_addr, &addr_string);
1081 seq_printf(seq, " Device address:\t%s\n", addr_string.c);
1082 }
1083 seq_printf(seq, " Firmware version:\t%s", !working ? "Unknown" :
1084 !firmware_level ? "Should be upgraded" :
1085 firmware_version.c);
1086 if (firmware_level >= ChecksummedMessages)
1087 seq_printf(seq, " (Checksums Enabled)");
1088 seq_printf(seq, "\n");
1089 seq_printf(seq, " Serial number:\t\t%s\n", serial_number.c);
1090 seq_printf(seq, " Battery voltage:\t%s\n", battery_voltage.c);
1091 seq_printf(seq, " Transmit queue (bytes):%d\n", tx_left);
1092 seq_printf(seq, " Receive packet rate: %ld packets per second\n",
1093 rx_average_pps / 8);
1094 seq_printf(seq, " Transmit packet rate: %ld packets per second\n",
1095 tx_average_pps / 8);
1096 seq_printf(seq, " Sent packet rate: %ld packets per second\n",
1097 sx_average_pps / 8);
1098 seq_printf(seq, " Next watchdog probe:\t%s\n",
1099 time_delta(temp, watchdog_doprobe));
1100 seq_printf(seq, " Next watchdog reset:\t%s\n",
1101 time_delta(temp, watchdog_doreset));
1102 seq_printf(seq, " Next gratuitous ARP:\t");
1103
1104 if (!memcmp
1105 (strip_info->dev->dev_addr, zero_address.c,
1106 sizeof(zero_address)))
1107 seq_printf(seq, "Disabled\n");
1108 else {
1109 seq_printf(seq, "%s\n", time_delta(temp, gratuitous_arp));
1110 seq_printf(seq, " Next ARP interval:\t%ld seconds\n",
1111 JIFFIE_TO_SEC(arp_interval));
1112 }
1113
1114 if (working) {
1115#ifdef EXT_COUNTERS
1116 seq_printf(seq, "\n");
1117 seq_printf(seq,
1118 " Total bytes: \trx:\t%lu\ttx:\t%lu\n",
1119 rx_bytes, tx_bytes);
1120 seq_printf(seq,
1121 " thru radio: \trx:\t%lu\ttx:\t%lu\n",
1122 rx_rbytes, tx_rbytes);
1123 seq_printf(seq,
1124 " thru serial port: \trx:\t%lu\ttx:\t%lu\n",
1125 rx_sbytes, tx_sbytes);
1126 seq_printf(seq,
1127 " Total stat/err bytes:\trx:\t%lu\ttx:\t%lu\n",
1128 rx_ebytes, tx_ebytes);
1129#endif
1130 strip_seq_neighbours(seq, &strip_info->poletops,
1131 "Poletops:");
1132 strip_seq_neighbours(seq, &strip_info->portables,
1133 "Portables:");
1134 }
1135}
1136
1137/*
1138 * This function is exports status information from the STRIP driver through
1139 * the /proc file system.
1140 */
1141static int strip_seq_show(struct seq_file *seq, void *v)
1142{
1143 if (v == SEQ_START_TOKEN)
1144 seq_printf(seq, "strip_version: %s\n", StripVersion);
1145 else
1146 strip_seq_status_info(seq, (const struct strip *)v);
1147 return 0;
1148}
1149
1150
1151static struct seq_operations strip_seq_ops = {
1152 .start = strip_seq_start,
1153 .next = strip_seq_next,
1154 .stop = strip_seq_stop,
1155 .show = strip_seq_show,
1156};
1157
1158static int strip_seq_open(struct inode *inode, struct file *file)
1159{
1160 return seq_open(file, &strip_seq_ops);
1161}
1162
d54b1fdb 1163static const struct file_operations strip_seq_fops = {
1da177e4
LT
1164 .owner = THIS_MODULE,
1165 .open = strip_seq_open,
1166 .read = seq_read,
1167 .llseek = seq_lseek,
1168 .release = seq_release,
1169};
1170#endif
1171
1172
1173
1174/************************************************************************/
1175/* Sending routines */
1176
1177static void ResetRadio(struct strip *strip_info)
1178{
1179 struct tty_struct *tty = strip_info->tty;
1180 static const char init[] = "ate0q1dt**starmode\r**";
1181 StringDescriptor s = { init, sizeof(init) - 1 };
1182
1183 /*
1184 * If the radio isn't working anymore,
1185 * we should clear the old status information.
1186 */
1187 if (strip_info->working) {
1188 printk(KERN_INFO "%s: No response: Resetting radio.\n",
1189 strip_info->dev->name);
1190 strip_info->firmware_version.c[0] = '\0';
1191 strip_info->serial_number.c[0] = '\0';
1192 strip_info->battery_voltage.c[0] = '\0';
1193 strip_info->portables.num_nodes = 0;
1194 do_gettimeofday(&strip_info->portables.timestamp);
1195 strip_info->poletops.num_nodes = 0;
1196 do_gettimeofday(&strip_info->poletops.timestamp);
1197 }
1198
1199 strip_info->pps_timer = jiffies;
1200 strip_info->rx_pps_count = 0;
1201 strip_info->tx_pps_count = 0;
1202 strip_info->sx_pps_count = 0;
1203 strip_info->rx_average_pps = 0;
1204 strip_info->tx_average_pps = 0;
1205 strip_info->sx_average_pps = 0;
1206
1207 /* Mark radio address as unknown */
1208 *(MetricomAddress *) & strip_info->true_dev_addr = zero_address;
1209 if (!strip_info->manual_dev_addr)
1210 *(MetricomAddress *) strip_info->dev->dev_addr =
1211 zero_address;
1212 strip_info->working = FALSE;
1213 strip_info->firmware_level = NoStructure;
1214 strip_info->next_command = CompatibilityCommand;
1215 strip_info->watchdog_doprobe = jiffies + 10 * HZ;
1216 strip_info->watchdog_doreset = jiffies + 1 * HZ;
1217
1218 /* If the user has selected a baud rate above 38.4 see what magic we have to do */
1219 if (strip_info->user_baud > B38400) {
1220 /*
1221 * Subtle stuff: Pay attention :-)
1222 * If the serial port is currently at the user's selected (>38.4) rate,
1223 * then we temporarily switch to 19.2 and issue the ATS304 command
1224 * to tell the radio to switch to the user's selected rate.
1225 * If the serial port is not currently at that rate, that means we just
1226 * issued the ATS304 command last time through, so this time we restore
1227 * the user's selected rate and issue the normal starmode reset string.
1228 */
1229 if (strip_info->user_baud == get_baud(tty)) {
1230 static const char b0[] = "ate0q1s304=57600\r";
1231 static const char b1[] = "ate0q1s304=115200\r";
1232 static const StringDescriptor baudstring[2] =
1233 { {b0, sizeof(b0) - 1}
1234 , {b1, sizeof(b1) - 1}
1235 };
1236 set_baud(tty, B19200);
1237 if (strip_info->user_baud == B57600)
1238 s = baudstring[0];
1239 else if (strip_info->user_baud == B115200)
1240 s = baudstring[1];
1241 else
1242 s = baudstring[1]; /* For now */
1243 } else
1244 set_baud(tty, strip_info->user_baud);
1245 }
1246
1247 tty->driver->write(tty, s.string, s.length);
1248#ifdef EXT_COUNTERS
1249 strip_info->tx_ebytes += s.length;
1250#endif
1251}
1252
1253/*
1254 * Called by the driver when there's room for more data. If we have
1255 * more packets to send, we send them here.
1256 */
1257
1258static void strip_write_some_more(struct tty_struct *tty)
1259{
1260 struct strip *strip_info = (struct strip *) tty->disc_data;
1261
1262 /* First make sure we're connected. */
1263 if (!strip_info || strip_info->magic != STRIP_MAGIC ||
1264 !netif_running(strip_info->dev))
1265 return;
1266
1267 if (strip_info->tx_left > 0) {
1268 int num_written =
1269 tty->driver->write(tty, strip_info->tx_head,
1270 strip_info->tx_left);
1271 strip_info->tx_left -= num_written;
1272 strip_info->tx_head += num_written;
1273#ifdef EXT_COUNTERS
1274 strip_info->tx_sbytes += num_written;
1275#endif
1276 } else { /* Else start transmission of another packet */
1277
1278 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1279 strip_unlock(strip_info);
1280 }
1281}
1282
1283static __u8 *add_checksum(__u8 * buffer, __u8 * end)
1284{
1285 __u16 sum = 0;
1286 __u8 *p = buffer;
1287 while (p < end)
1288 sum += *p++;
1289 end[3] = hextable[sum & 0xF];
1290 sum >>= 4;
1291 end[2] = hextable[sum & 0xF];
1292 sum >>= 4;
1293 end[1] = hextable[sum & 0xF];
1294 sum >>= 4;
1295 end[0] = hextable[sum & 0xF];
1296 return (end + 4);
1297}
1298
1299static unsigned char *strip_make_packet(unsigned char *buffer,
1300 struct strip *strip_info,
1301 struct sk_buff *skb)
1302{
1303 __u8 *ptr = buffer;
1304 __u8 *stuffstate = NULL;
1305 STRIP_Header *header = (STRIP_Header *) skb->data;
1306 MetricomAddress haddr = header->dst_addr;
1307 int len = skb->len - sizeof(STRIP_Header);
1308 MetricomKey key;
1309
1310 /*HexDump("strip_make_packet", strip_info, skb->data, skb->data + skb->len); */
1311
1312 if (header->protocol == htons(ETH_P_IP))
1313 key = SIP0Key;
1314 else if (header->protocol == htons(ETH_P_ARP))
1315 key = ARP0Key;
1316 else {
1317 printk(KERN_ERR
1318 "%s: strip_make_packet: Unknown packet type 0x%04X\n",
1319 strip_info->dev->name, ntohs(header->protocol));
1320 return (NULL);
1321 }
1322
1323 if (len > strip_info->mtu) {
1324 printk(KERN_ERR
1325 "%s: Dropping oversized transmit packet: %d bytes\n",
1326 strip_info->dev->name, len);
1327 return (NULL);
1328 }
1329
1330 /*
1331 * If we're sending to ourselves, discard the packet.
1332 * (Metricom radios choke if they try to send a packet to their own address.)
1333 */
1334 if (!memcmp(haddr.c, strip_info->true_dev_addr.c, sizeof(haddr))) {
1335 printk(KERN_ERR "%s: Dropping packet addressed to self\n",
1336 strip_info->dev->name);
1337 return (NULL);
1338 }
1339
1340 /*
1341 * If this is a broadcast packet, send it to our designated Metricom
1342 * 'broadcast hub' radio (First byte of address being 0xFF means broadcast)
1343 */
1344 if (haddr.c[0] == 0xFF) {
a144ea4b 1345 __be32 brd = 0;
1da177e4
LT
1346 struct in_device *in_dev;
1347
1348 rcu_read_lock();
e5ed6399 1349 in_dev = __in_dev_get_rcu(strip_info->dev);
1da177e4
LT
1350 if (in_dev == NULL) {
1351 rcu_read_unlock();
1352 return NULL;
1353 }
1354 if (in_dev->ifa_list)
1355 brd = in_dev->ifa_list->ifa_broadcast;
1356 rcu_read_unlock();
1357
1358 /* arp_query returns 1 if it succeeds in looking up the address, 0 if it fails */
1359 if (!arp_query(haddr.c, brd, strip_info->dev)) {
1360 printk(KERN_ERR
1361 "%s: Unable to send packet (no broadcast hub configured)\n",
1362 strip_info->dev->name);
1363 return (NULL);
1364 }
1365 /*
1366 * If we are the broadcast hub, don't bother sending to ourselves.
1367 * (Metricom radios choke if they try to send a packet to their own address.)
1368 */
1369 if (!memcmp
1370 (haddr.c, strip_info->true_dev_addr.c, sizeof(haddr)))
1371 return (NULL);
1372 }
1373
1374 *ptr++ = 0x0D;
1375 *ptr++ = '*';
1376 *ptr++ = hextable[haddr.c[2] >> 4];
1377 *ptr++ = hextable[haddr.c[2] & 0xF];
1378 *ptr++ = hextable[haddr.c[3] >> 4];
1379 *ptr++ = hextable[haddr.c[3] & 0xF];
1380 *ptr++ = '-';
1381 *ptr++ = hextable[haddr.c[4] >> 4];
1382 *ptr++ = hextable[haddr.c[4] & 0xF];
1383 *ptr++ = hextable[haddr.c[5] >> 4];
1384 *ptr++ = hextable[haddr.c[5] & 0xF];
1385 *ptr++ = '*';
1386 *ptr++ = key.c[0];
1387 *ptr++ = key.c[1];
1388 *ptr++ = key.c[2];
1389 *ptr++ = key.c[3];
1390
1391 ptr =
1392 StuffData(skb->data + sizeof(STRIP_Header), len, ptr,
1393 &stuffstate);
1394
1395 if (strip_info->firmware_level >= ChecksummedMessages)
1396 ptr = add_checksum(buffer + 1, ptr);
1397
1398 *ptr++ = 0x0D;
1399 return (ptr);
1400}
1401
1402static void strip_send(struct strip *strip_info, struct sk_buff *skb)
1403{
1404 MetricomAddress haddr;
1405 unsigned char *ptr = strip_info->tx_buff;
1406 int doreset = (long) jiffies - strip_info->watchdog_doreset >= 0;
1407 int doprobe = (long) jiffies - strip_info->watchdog_doprobe >= 0
1408 && !doreset;
a144ea4b 1409 __be32 addr, brd;
1da177e4
LT
1410
1411 /*
1412 * 1. If we have a packet, encapsulate it and put it in the buffer
1413 */
1414 if (skb) {
1415 char *newptr = strip_make_packet(ptr, strip_info, skb);
1416 strip_info->tx_pps_count++;
1417 if (!newptr)
1418 strip_info->tx_dropped++;
1419 else {
1420 ptr = newptr;
1421 strip_info->sx_pps_count++;
1422 strip_info->tx_packets++; /* Count another successful packet */
1423#ifdef EXT_COUNTERS
1424 strip_info->tx_bytes += skb->len;
1425 strip_info->tx_rbytes += ptr - strip_info->tx_buff;
1426#endif
1427 /*DumpData("Sending:", strip_info, strip_info->tx_buff, ptr); */
1428 /*HexDump("Sending", strip_info, strip_info->tx_buff, ptr); */
1429 }
1430 }
1431
1432 /*
1433 * 2. If it is time for another tickle, tack it on, after the packet
1434 */
1435 if (doprobe) {
1436 StringDescriptor ts = CommandString[strip_info->next_command];
1437#if TICKLE_TIMERS
1438 {
1439 struct timeval tv;
1440 do_gettimeofday(&tv);
1441 printk(KERN_INFO "**** Sending tickle string %d at %02d.%06d\n",
1442 strip_info->next_command, tv.tv_sec % 100,
1443 tv.tv_usec);
1444 }
1445#endif
1446 if (ptr == strip_info->tx_buff)
1447 *ptr++ = 0x0D;
1448
1449 *ptr++ = '*'; /* First send "**" to provoke an error message */
1450 *ptr++ = '*';
1451
1452 /* Then add the command */
1453 memcpy(ptr, ts.string, ts.length);
1454
1455 /* Add a checksum ? */
1456 if (strip_info->firmware_level < ChecksummedMessages)
1457 ptr += ts.length;
1458 else
1459 ptr = add_checksum(ptr, ptr + ts.length);
1460
1461 *ptr++ = 0x0D; /* Terminate the command with a <CR> */
1462
1463 /* Cycle to next periodic command? */
1464 if (strip_info->firmware_level >= StructuredMessages)
1465 if (++strip_info->next_command >=
1466 ARRAY_SIZE(CommandString))
1467 strip_info->next_command = 0;
1468#ifdef EXT_COUNTERS
1469 strip_info->tx_ebytes += ts.length;
1470#endif
1471 strip_info->watchdog_doprobe = jiffies + 10 * HZ;
1472 strip_info->watchdog_doreset = jiffies + 1 * HZ;
1473 /*printk(KERN_INFO "%s: Routine radio test.\n", strip_info->dev->name); */
1474 }
1475
1476 /*
1477 * 3. Set up the strip_info ready to send the data (if any).
1478 */
1479 strip_info->tx_head = strip_info->tx_buff;
1480 strip_info->tx_left = ptr - strip_info->tx_buff;
1481 strip_info->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1482
1483 /*
1484 * 4. Debugging check to make sure we're not overflowing the buffer.
1485 */
1486 if (strip_info->tx_size - strip_info->tx_left < 20)
1487 printk(KERN_ERR "%s: Sending%5d bytes;%5d bytes free.\n",
1488 strip_info->dev->name, strip_info->tx_left,
1489 strip_info->tx_size - strip_info->tx_left);
1490
1491 /*
1492 * 5. If watchdog has expired, reset the radio. Note: if there's data waiting in
1493 * the buffer, strip_write_some_more will send it after the reset has finished
1494 */
1495 if (doreset) {
1496 ResetRadio(strip_info);
1497 return;
1498 }
1499
1500 if (1) {
1501 struct in_device *in_dev;
1502
1503 brd = addr = 0;
1504 rcu_read_lock();
e5ed6399 1505 in_dev = __in_dev_get_rcu(strip_info->dev);
1da177e4
LT
1506 if (in_dev) {
1507 if (in_dev->ifa_list) {
1508 brd = in_dev->ifa_list->ifa_broadcast;
1509 addr = in_dev->ifa_list->ifa_local;
1510 }
1511 }
1512 rcu_read_unlock();
1513 }
1514
1515
1516 /*
1517 * 6. If it is time for a periodic ARP, queue one up to be sent.
1518 * We only do this if:
1519 * 1. The radio is working
1520 * 2. It's time to send another periodic ARP
1521 * 3. We really know what our address is (and it is not manually set to zero)
1522 * 4. We have a designated broadcast address configured
1523 * If we queue up an ARP packet when we don't have a designated broadcast
1524 * address configured, then the packet will just have to be discarded in
1525 * strip_make_packet. This is not fatal, but it causes misleading information
1526 * to be displayed in tcpdump. tcpdump will report that periodic APRs are
1527 * being sent, when in fact they are not, because they are all being dropped
1528 * in the strip_make_packet routine.
1529 */
1530 if (strip_info->working
1531 && (long) jiffies - strip_info->gratuitous_arp >= 0
1532 && memcmp(strip_info->dev->dev_addr, zero_address.c,
1533 sizeof(zero_address))
1534 && arp_query(haddr.c, brd, strip_info->dev)) {
1535 /*printk(KERN_INFO "%s: Sending gratuitous ARP with interval %ld\n",
1536 strip_info->dev->name, strip_info->arp_interval / HZ); */
1537 strip_info->gratuitous_arp =
1538 jiffies + strip_info->arp_interval;
1539 strip_info->arp_interval *= 2;
1540 if (strip_info->arp_interval > MaxARPInterval)
1541 strip_info->arp_interval = MaxARPInterval;
1542 if (addr)
1543 arp_send(ARPOP_REPLY, ETH_P_ARP, addr, /* Target address of ARP packet is our address */
1544 strip_info->dev, /* Device to send packet on */
1545 addr, /* Source IP address this ARP packet comes from */
1546 NULL, /* Destination HW address is NULL (broadcast it) */
1547 strip_info->dev->dev_addr, /* Source HW address is our HW address */
1548 strip_info->dev->dev_addr); /* Target HW address is our HW address (redundant) */
1549 }
1550
1551 /*
1552 * 7. All ready. Start the transmission
1553 */
1554 strip_write_some_more(strip_info->tty);
1555}
1556
1557/* Encapsulate a datagram and kick it into a TTY queue. */
1558static int strip_xmit(struct sk_buff *skb, struct net_device *dev)
1559{
1560 struct strip *strip_info = netdev_priv(dev);
1561
1562 if (!netif_running(dev)) {
1563 printk(KERN_ERR "%s: xmit call when iface is down\n",
1564 dev->name);
1565 return (1);
1566 }
1567
1568 netif_stop_queue(dev);
1569
1570 del_timer(&strip_info->idle_timer);
1571
1572
ff5688ae 1573 if (time_after(jiffies, strip_info->pps_timer + HZ)) {
1da177e4
LT
1574 unsigned long t = jiffies - strip_info->pps_timer;
1575 unsigned long rx_pps_count = (strip_info->rx_pps_count * HZ * 8 + t / 2) / t;
1576 unsigned long tx_pps_count = (strip_info->tx_pps_count * HZ * 8 + t / 2) / t;
1577 unsigned long sx_pps_count = (strip_info->sx_pps_count * HZ * 8 + t / 2) / t;
1578
1579 strip_info->pps_timer = jiffies;
1580 strip_info->rx_pps_count = 0;
1581 strip_info->tx_pps_count = 0;
1582 strip_info->sx_pps_count = 0;
1583
1584 strip_info->rx_average_pps = (strip_info->rx_average_pps + rx_pps_count + 1) / 2;
1585 strip_info->tx_average_pps = (strip_info->tx_average_pps + tx_pps_count + 1) / 2;
1586 strip_info->sx_average_pps = (strip_info->sx_average_pps + sx_pps_count + 1) / 2;
1587
1588 if (rx_pps_count / 8 >= 10)
1589 printk(KERN_INFO "%s: WARNING: Receiving %ld packets per second.\n",
1590 strip_info->dev->name, rx_pps_count / 8);
1591 if (tx_pps_count / 8 >= 10)
1592 printk(KERN_INFO "%s: WARNING: Tx %ld packets per second.\n",
1593 strip_info->dev->name, tx_pps_count / 8);
1594 if (sx_pps_count / 8 >= 10)
1595 printk(KERN_INFO "%s: WARNING: Sending %ld packets per second.\n",
1596 strip_info->dev->name, sx_pps_count / 8);
1597 }
1598
1599 spin_lock_bh(&strip_lock);
1600
1601 strip_send(strip_info, skb);
1602
1603 spin_unlock_bh(&strip_lock);
1604
1605 if (skb)
1606 dev_kfree_skb(skb);
1607 return 0;
1608}
1609
1610/*
1611 * IdleTask periodically calls strip_xmit, so even when we have no IP packets
1612 * to send for an extended period of time, the watchdog processing still gets
1613 * done to ensure that the radio stays in Starmode
1614 */
1615
1616static void strip_IdleTask(unsigned long parameter)
1617{
1618 strip_xmit(NULL, (struct net_device *) parameter);
1619}
1620
1621/*
1622 * Create the MAC header for an arbitrary protocol layer
1623 *
1624 * saddr!=NULL means use this specific address (n/a for Metricom)
1625 * saddr==NULL means use default device source address
1626 * daddr!=NULL means use this destination address
1627 * daddr==NULL means leave destination address alone
1628 * (e.g. unresolved arp -- kernel will call
1629 * rebuild_header later to fill in the address)
1630 */
1631
1632static int strip_header(struct sk_buff *skb, struct net_device *dev,
1633 unsigned short type, void *daddr, void *saddr,
1634 unsigned len)
1635{
1636 struct strip *strip_info = netdev_priv(dev);
1637 STRIP_Header *header = (STRIP_Header *) skb_push(skb, sizeof(STRIP_Header));
1638
1639 /*printk(KERN_INFO "%s: strip_header 0x%04X %s\n", dev->name, type,
1640 type == ETH_P_IP ? "IP" : type == ETH_P_ARP ? "ARP" : ""); */
1641
1642 header->src_addr = strip_info->true_dev_addr;
1643 header->protocol = htons(type);
1644
1645 /*HexDump("strip_header", netdev_priv(dev), skb->data, skb->data + skb->len); */
1646
1647 if (!daddr)
1648 return (-dev->hard_header_len);
1649
1650 header->dst_addr = *(MetricomAddress *) daddr;
1651 return (dev->hard_header_len);
1652}
1653
1654/*
1655 * Rebuild the MAC header. This is called after an ARP
1656 * (or in future other address resolution) has completed on this
1657 * sk_buff. We now let ARP fill in the other fields.
1658 * I think this should return zero if packet is ready to send,
1659 * or non-zero if it needs more time to do an address lookup
1660 */
1661
1662static int strip_rebuild_header(struct sk_buff *skb)
1663{
1664#ifdef CONFIG_INET
1665 STRIP_Header *header = (STRIP_Header *) skb->data;
1666
1667 /* Arp find returns zero if if knows the address, */
1668 /* or if it doesn't know the address it sends an ARP packet and returns non-zero */
1669 return arp_find(header->dst_addr.c, skb) ? 1 : 0;
1670#else
1671 return 0;
1672#endif
1673}
1674
1675
1676/************************************************************************/
1677/* Receiving routines */
1678
1da177e4
LT
1679/*
1680 * This function parses the response to the ATS300? command,
1681 * extracting the radio version and serial number.
1682 */
1683static void get_radio_version(struct strip *strip_info, __u8 * ptr, __u8 * end)
1684{
1685 __u8 *p, *value_begin, *value_end;
1686 int len;
1687
1688 /* Determine the beginning of the second line of the payload */
1689 p = ptr;
1690 while (p < end && *p != 10)
1691 p++;
1692 if (p >= end)
1693 return;
1694 p++;
1695 value_begin = p;
1696
1697 /* Determine the end of line */
1698 while (p < end && *p != 10)
1699 p++;
1700 if (p >= end)
1701 return;
1702 value_end = p;
1703 p++;
1704
1705 len = value_end - value_begin;
1706 len = min_t(int, len, sizeof(FirmwareVersion) - 1);
1707 if (strip_info->firmware_version.c[0] == 0)
1708 printk(KERN_INFO "%s: Radio Firmware: %.*s\n",
1709 strip_info->dev->name, len, value_begin);
1710 sprintf(strip_info->firmware_version.c, "%.*s", len, value_begin);
1711
1712 /* Look for the first colon */
1713 while (p < end && *p != ':')
1714 p++;
1715 if (p >= end)
1716 return;
1717 /* Skip over the space */
1718 p += 2;
1719 len = sizeof(SerialNumber) - 1;
1720 if (p + len <= end) {
1721 sprintf(strip_info->serial_number.c, "%.*s", len, p);
1722 } else {
1723 printk(KERN_DEBUG
1724 "STRIP: radio serial number shorter (%zd) than expected (%d)\n",
1725 end - p, len);
1726 }
1727}
1728
1729/*
1730 * This function parses the response to the ATS325? command,
1731 * extracting the radio battery voltage.
1732 */
1733static void get_radio_voltage(struct strip *strip_info, __u8 * ptr, __u8 * end)
1734{
1735 int len;
1736
1737 len = sizeof(BatteryVoltage) - 1;
1738 if (ptr + len <= end) {
1739 sprintf(strip_info->battery_voltage.c, "%.*s", len, ptr);
1740 } else {
1741 printk(KERN_DEBUG
1742 "STRIP: radio voltage string shorter (%zd) than expected (%d)\n",
1743 end - ptr, len);
1744 }
1745}
1746
1747/*
1748 * This function parses the responses to the AT~LA and ATS311 commands,
1749 * which list the radio's neighbours.
1750 */
1751static void get_radio_neighbours(MetricomNodeTable * table, __u8 * ptr, __u8 * end)
1752{
1753 table->num_nodes = 0;
1754 while (ptr < end && table->num_nodes < NODE_TABLE_SIZE) {
1755 MetricomNode *node = &table->node[table->num_nodes++];
1756 char *dst = node->c, *limit = dst + sizeof(*node) - 1;
1757 while (ptr < end && *ptr <= 32)
1758 ptr++;
1759 while (ptr < end && dst < limit && *ptr != 10)
1760 *dst++ = *ptr++;
1761 *dst++ = 0;
1762 while (ptr < end && ptr[-1] != 10)
1763 ptr++;
1764 }
1765 do_gettimeofday(&table->timestamp);
1766}
1767
1768static int get_radio_address(struct strip *strip_info, __u8 * p)
1769{
1770 MetricomAddress addr;
1771
1772 if (string_to_radio_address(&addr, p))
1773 return (1);
1774
1775 /* See if our radio address has changed */
1776 if (memcmp(strip_info->true_dev_addr.c, addr.c, sizeof(addr))) {
1777 MetricomAddressString addr_string;
1778 radio_address_to_string(&addr, &addr_string);
1779 printk(KERN_INFO "%s: Radio address = %s\n",
1780 strip_info->dev->name, addr_string.c);
1781 strip_info->true_dev_addr = addr;
1782 if (!strip_info->manual_dev_addr)
1783 *(MetricomAddress *) strip_info->dev->dev_addr =
1784 addr;
1785 /* Give the radio a few seconds to get its head straight, then send an arp */
1786 strip_info->gratuitous_arp = jiffies + 15 * HZ;
1787 strip_info->arp_interval = 1 * HZ;
1788 }
1789 return (0);
1790}
1791
1792static int verify_checksum(struct strip *strip_info)
1793{
1794 __u8 *p = strip_info->sx_buff;
1795 __u8 *end = strip_info->sx_buff + strip_info->sx_count - 4;
1796 u_short sum =
1797 (READHEX16(end[0]) << 12) | (READHEX16(end[1]) << 8) |
1798 (READHEX16(end[2]) << 4) | (READHEX16(end[3]));
1799 while (p < end)
1800 sum -= *p++;
1801 if (sum == 0 && strip_info->firmware_level == StructuredMessages) {
1802 strip_info->firmware_level = ChecksummedMessages;
1803 printk(KERN_INFO "%s: Radio provides message checksums\n",
1804 strip_info->dev->name);
1805 }
1806 return (sum == 0);
1807}
1808
1809static void RecvErr(char *msg, struct strip *strip_info)
1810{
1811 __u8 *ptr = strip_info->sx_buff;
1812 __u8 *end = strip_info->sx_buff + strip_info->sx_count;
1813 DumpData(msg, strip_info, ptr, end);
1814 strip_info->rx_errors++;
1815}
1816
1817static void RecvErr_Message(struct strip *strip_info, __u8 * sendername,
1818 const __u8 * msg, u_long len)
1819{
1820 if (has_prefix(msg, len, "001")) { /* Not in StarMode! */
1821 RecvErr("Error Msg:", strip_info);
1822 printk(KERN_INFO "%s: Radio %s is not in StarMode\n",
1823 strip_info->dev->name, sendername);
1824 }
1825
1826 else if (has_prefix(msg, len, "002")) { /* Remap handle */
1827 /* We ignore "Remap handle" messages for now */
1828 }
1829
1830 else if (has_prefix(msg, len, "003")) { /* Can't resolve name */
1831 RecvErr("Error Msg:", strip_info);
1832 printk(KERN_INFO "%s: Destination radio name is unknown\n",
1833 strip_info->dev->name);
1834 }
1835
1836 else if (has_prefix(msg, len, "004")) { /* Name too small or missing */
1837 strip_info->watchdog_doreset = jiffies + LongTime;
1838#if TICKLE_TIMERS
1839 {
1840 struct timeval tv;
1841 do_gettimeofday(&tv);
1842 printk(KERN_INFO
1843 "**** Got ERR_004 response at %02d.%06d\n",
1844 tv.tv_sec % 100, tv.tv_usec);
1845 }
1846#endif
1847 if (!strip_info->working) {
1848 strip_info->working = TRUE;
1849 printk(KERN_INFO "%s: Radio now in starmode\n",
1850 strip_info->dev->name);
1851 /*
1852 * If the radio has just entered a working state, we should do our first
1853 * probe ASAP, so that we find out our radio address etc. without delay.
1854 */
1855 strip_info->watchdog_doprobe = jiffies;
1856 }
1857 if (strip_info->firmware_level == NoStructure && sendername) {
1858 strip_info->firmware_level = StructuredMessages;
1859 strip_info->next_command = 0; /* Try to enable checksums ASAP */
1860 printk(KERN_INFO
1861 "%s: Radio provides structured messages\n",
1862 strip_info->dev->name);
1863 }
1864 if (strip_info->firmware_level >= StructuredMessages) {
1865 /*
1866 * If this message has a valid checksum on the end, then the call to verify_checksum
1867 * will elevate the firmware_level to ChecksummedMessages for us. (The actual return
1868 * code from verify_checksum is ignored here.)
1869 */
1870 verify_checksum(strip_info);
1871 /*
1872 * If the radio has structured messages but we don't yet have all our information about it,
1873 * we should do probes without delay, until we have gathered all the information
1874 */
1875 if (!GOT_ALL_RADIO_INFO(strip_info))
1876 strip_info->watchdog_doprobe = jiffies;
1877 }
1878 }
1879
1880 else if (has_prefix(msg, len, "005")) /* Bad count specification */
1881 RecvErr("Error Msg:", strip_info);
1882
1883 else if (has_prefix(msg, len, "006")) /* Header too big */
1884 RecvErr("Error Msg:", strip_info);
1885
1886 else if (has_prefix(msg, len, "007")) { /* Body too big */
1887 RecvErr("Error Msg:", strip_info);
1888 printk(KERN_ERR
1889 "%s: Error! Packet size too big for radio.\n",
1890 strip_info->dev->name);
1891 }
1892
1893 else if (has_prefix(msg, len, "008")) { /* Bad character in name */
1894 RecvErr("Error Msg:", strip_info);
1895 printk(KERN_ERR
1896 "%s: Radio name contains illegal character\n",
1897 strip_info->dev->name);
1898 }
1899
1900 else if (has_prefix(msg, len, "009")) /* No count or line terminator */
1901 RecvErr("Error Msg:", strip_info);
1902
1903 else if (has_prefix(msg, len, "010")) /* Invalid checksum */
1904 RecvErr("Error Msg:", strip_info);
1905
1906 else if (has_prefix(msg, len, "011")) /* Checksum didn't match */
1907 RecvErr("Error Msg:", strip_info);
1908
1909 else if (has_prefix(msg, len, "012")) /* Failed to transmit packet */
1910 RecvErr("Error Msg:", strip_info);
1911
1912 else
1913 RecvErr("Error Msg:", strip_info);
1914}
1915
1916static void process_AT_response(struct strip *strip_info, __u8 * ptr,
1917 __u8 * end)
1918{
1919 u_long len;
1920 __u8 *p = ptr;
1921 while (p < end && p[-1] != 10)
1922 p++; /* Skip past first newline character */
1923 /* Now ptr points to the AT command, and p points to the text of the response. */
1924 len = p - ptr;
1925
1926#if TICKLE_TIMERS
1927 {
1928 struct timeval tv;
1929 do_gettimeofday(&tv);
1930 printk(KERN_INFO "**** Got AT response %.7s at %02d.%06d\n",
1931 ptr, tv.tv_sec % 100, tv.tv_usec);
1932 }
1933#endif
1934
1935 if (has_prefix(ptr, len, "ATS300?"))
1936 get_radio_version(strip_info, p, end);
1937 else if (has_prefix(ptr, len, "ATS305?"))
1938 get_radio_address(strip_info, p);
1939 else if (has_prefix(ptr, len, "ATS311?"))
1940 get_radio_neighbours(&strip_info->poletops, p, end);
1941 else if (has_prefix(ptr, len, "ATS319=7"))
1942 verify_checksum(strip_info);
1943 else if (has_prefix(ptr, len, "ATS325?"))
1944 get_radio_voltage(strip_info, p, end);
1945 else if (has_prefix(ptr, len, "AT~LA"))
1946 get_radio_neighbours(&strip_info->portables, p, end);
1947 else
1948 RecvErr("Unknown AT Response:", strip_info);
1949}
1950
1951static void process_ACK(struct strip *strip_info, __u8 * ptr, __u8 * end)
1952{
1953 /* Currently we don't do anything with ACKs from the radio */
1954}
1955
1956static void process_Info(struct strip *strip_info, __u8 * ptr, __u8 * end)
1957{
1958 if (ptr + 16 > end)
1959 RecvErr("Bad Info Msg:", strip_info);
1960}
1961
1962static struct net_device *get_strip_dev(struct strip *strip_info)
1963{
1964 /* If our hardware address is *manually set* to zero, and we know our */
1965 /* real radio hardware address, try to find another strip device that has been */
1966 /* manually set to that address that we can 'transfer ownership' of this packet to */
1967 if (strip_info->manual_dev_addr &&
1968 !memcmp(strip_info->dev->dev_addr, zero_address.c,
1969 sizeof(zero_address))
1970 && memcmp(&strip_info->true_dev_addr, zero_address.c,
1971 sizeof(zero_address))) {
1972 struct net_device *dev;
1973 read_lock_bh(&dev_base_lock);
1974 dev = dev_base;
1975 while (dev) {
1976 if (dev->type == strip_info->dev->type &&
1977 !memcmp(dev->dev_addr,
1978 &strip_info->true_dev_addr,
1979 sizeof(MetricomAddress))) {
1980 printk(KERN_INFO
1981 "%s: Transferred packet ownership to %s.\n",
1982 strip_info->dev->name, dev->name);
1983 read_unlock_bh(&dev_base_lock);
1984 return (dev);
1985 }
1986 dev = dev->next;
1987 }
1988 read_unlock_bh(&dev_base_lock);
1989 }
1990 return (strip_info->dev);
1991}
1992
1993/*
1994 * Send one completely decapsulated datagram to the next layer.
1995 */
1996
1997static void deliver_packet(struct strip *strip_info, STRIP_Header * header,
1998 __u16 packetlen)
1999{
2000 struct sk_buff *skb = dev_alloc_skb(sizeof(STRIP_Header) + packetlen);
2001 if (!skb) {
2002 printk(KERN_ERR "%s: memory squeeze, dropping packet.\n",
2003 strip_info->dev->name);
2004 strip_info->rx_dropped++;
2005 } else {
2006 memcpy(skb_put(skb, sizeof(STRIP_Header)), header,
2007 sizeof(STRIP_Header));
2008 memcpy(skb_put(skb, packetlen), strip_info->rx_buff,
2009 packetlen);
2010 skb->dev = get_strip_dev(strip_info);
2011 skb->protocol = header->protocol;
459a98ed 2012 skb_reset_mac_header(skb);
1da177e4
LT
2013
2014 /* Having put a fake header on the front of the sk_buff for the */
2015 /* benefit of tools like tcpdump, skb_pull now 'consumes' that */
2016 /* fake header before we hand the packet up to the next layer. */
2017 skb_pull(skb, sizeof(STRIP_Header));
2018
2019 /* Finally, hand the packet up to the next layer (e.g. IP or ARP, etc.) */
2020 strip_info->rx_packets++;
2021 strip_info->rx_pps_count++;
2022#ifdef EXT_COUNTERS
2023 strip_info->rx_bytes += packetlen;
2024#endif
2025 skb->dev->last_rx = jiffies;
2026 netif_rx(skb);
2027 }
2028}
2029
2030static void process_IP_packet(struct strip *strip_info,
2031 STRIP_Header * header, __u8 * ptr,
2032 __u8 * end)
2033{
2034 __u16 packetlen;
2035
2036 /* Decode start of the IP packet header */
2037 ptr = UnStuffData(ptr, end, strip_info->rx_buff, 4);
2038 if (!ptr) {
2039 RecvErr("IP Packet too short", strip_info);
2040 return;
2041 }
2042
2043 packetlen = ((__u16) strip_info->rx_buff[2] << 8) | strip_info->rx_buff[3];
2044
2045 if (packetlen > MAX_RECV_MTU) {
2046 printk(KERN_INFO "%s: Dropping oversized received IP packet: %d bytes\n",
2047 strip_info->dev->name, packetlen);
2048 strip_info->rx_dropped++;
2049 return;
2050 }
2051
2052 /*printk(KERN_INFO "%s: Got %d byte IP packet\n", strip_info->dev->name, packetlen); */
2053
2054 /* Decode remainder of the IP packet */
2055 ptr =
2056 UnStuffData(ptr, end, strip_info->rx_buff + 4, packetlen - 4);
2057 if (!ptr) {
2058 RecvErr("IP Packet too short", strip_info);
2059 return;
2060 }
2061
2062 if (ptr < end) {
2063 RecvErr("IP Packet too long", strip_info);
2064 return;
2065 }
2066
2067 header->protocol = htons(ETH_P_IP);
2068
2069 deliver_packet(strip_info, header, packetlen);
2070}
2071
2072static void process_ARP_packet(struct strip *strip_info,
2073 STRIP_Header * header, __u8 * ptr,
2074 __u8 * end)
2075{
2076 __u16 packetlen;
2077 struct arphdr *arphdr = (struct arphdr *) strip_info->rx_buff;
2078
2079 /* Decode start of the ARP packet */
2080 ptr = UnStuffData(ptr, end, strip_info->rx_buff, 8);
2081 if (!ptr) {
2082 RecvErr("ARP Packet too short", strip_info);
2083 return;
2084 }
2085
2086 packetlen = 8 + (arphdr->ar_hln + arphdr->ar_pln) * 2;
2087
2088 if (packetlen > MAX_RECV_MTU) {
2089 printk(KERN_INFO
2090 "%s: Dropping oversized received ARP packet: %d bytes\n",
2091 strip_info->dev->name, packetlen);
2092 strip_info->rx_dropped++;
2093 return;
2094 }
2095
2096 /*printk(KERN_INFO "%s: Got %d byte ARP %s\n",
2097 strip_info->dev->name, packetlen,
2098 ntohs(arphdr->ar_op) == ARPOP_REQUEST ? "request" : "reply"); */
2099
2100 /* Decode remainder of the ARP packet */
2101 ptr =
2102 UnStuffData(ptr, end, strip_info->rx_buff + 8, packetlen - 8);
2103 if (!ptr) {
2104 RecvErr("ARP Packet too short", strip_info);
2105 return;
2106 }
2107
2108 if (ptr < end) {
2109 RecvErr("ARP Packet too long", strip_info);
2110 return;
2111 }
2112
2113 header->protocol = htons(ETH_P_ARP);
2114
2115 deliver_packet(strip_info, header, packetlen);
2116}
2117
2118/*
2119 * process_text_message processes a <CR>-terminated block of data received
2120 * from the radio that doesn't begin with a '*' character. All normal
2121 * Starmode communication messages with the radio begin with a '*',
2122 * so any text that does not indicates a serial port error, a radio that
2123 * is in Hayes command mode instead of Starmode, or a radio with really
2124 * old firmware that doesn't frame its Starmode responses properly.
2125 */
2126static void process_text_message(struct strip *strip_info)
2127{
2128 __u8 *msg = strip_info->sx_buff;
2129 int len = strip_info->sx_count;
2130
2131 /* Check for anything that looks like it might be our radio name */
2132 /* (This is here for backwards compatibility with old firmware) */
2133 if (len == 9 && get_radio_address(strip_info, msg) == 0)
2134 return;
2135
2136 if (text_equal(msg, len, "OK"))
2137 return; /* Ignore 'OK' responses from prior commands */
2138 if (text_equal(msg, len, "ERROR"))
2139 return; /* Ignore 'ERROR' messages */
2140 if (has_prefix(msg, len, "ate0q1"))
2141 return; /* Ignore character echo back from the radio */
2142
2143 /* Catch other error messages */
2144 /* (This is here for backwards compatibility with old firmware) */
2145 if (has_prefix(msg, len, "ERR_")) {
2146 RecvErr_Message(strip_info, NULL, &msg[4], len - 4);
2147 return;
2148 }
2149
2150 RecvErr("No initial *", strip_info);
2151}
2152
2153/*
2154 * process_message processes a <CR>-terminated block of data received
2155 * from the radio. If the radio is not in Starmode or has old firmware,
2156 * it may be a line of text in response to an AT command. Ideally, with
2157 * a current radio that's properly in Starmode, all data received should
2158 * be properly framed and checksummed radio message blocks, containing
2159 * either a starmode packet, or a other communication from the radio
2160 * firmware, like "INF_" Info messages and &COMMAND responses.
2161 */
2162static void process_message(struct strip *strip_info)
2163{
2164 STRIP_Header header = { zero_address, zero_address, 0 };
2165 __u8 *ptr = strip_info->sx_buff;
2166 __u8 *end = strip_info->sx_buff + strip_info->sx_count;
2167 __u8 sendername[32], *sptr = sendername;
2168 MetricomKey key;
2169
2170 /*HexDump("Receiving", strip_info, ptr, end); */
2171
2172 /* Check for start of address marker, and then skip over it */
2173 if (*ptr == '*')
2174 ptr++;
2175 else {
2176 process_text_message(strip_info);
2177 return;
2178 }
2179
2180 /* Copy out the return address */
2181 while (ptr < end && *ptr != '*'
2182 && sptr < ARRAY_END(sendername) - 1)
2183 *sptr++ = *ptr++;
2184 *sptr = 0; /* Null terminate the sender name */
2185
2186 /* Check for end of address marker, and skip over it */
2187 if (ptr >= end || *ptr != '*') {
2188 RecvErr("No second *", strip_info);
2189 return;
2190 }
2191 ptr++; /* Skip the second '*' */
2192
2193 /* If the sender name is "&COMMAND", ignore this 'packet' */
2194 /* (This is here for backwards compatibility with old firmware) */
2195 if (!strcmp(sendername, "&COMMAND")) {
2196 strip_info->firmware_level = NoStructure;
2197 strip_info->next_command = CompatibilityCommand;
2198 return;
2199 }
2200
2201 if (ptr + 4 > end) {
2202 RecvErr("No proto key", strip_info);
2203 return;
2204 }
2205
2206 /* Get the protocol key out of the buffer */
2207 key.c[0] = *ptr++;
2208 key.c[1] = *ptr++;
2209 key.c[2] = *ptr++;
2210 key.c[3] = *ptr++;
2211
2212 /* If we're using checksums, verify the checksum at the end of the packet */
2213 if (strip_info->firmware_level >= ChecksummedMessages) {
2214 end -= 4; /* Chop the last four bytes off the packet (they're the checksum) */
2215 if (ptr > end) {
2216 RecvErr("Missing Checksum", strip_info);
2217 return;
2218 }
2219 if (!verify_checksum(strip_info)) {
2220 RecvErr("Bad Checksum", strip_info);
2221 return;
2222 }
2223 }
2224
2225 /*printk(KERN_INFO "%s: Got packet from \"%s\".\n", strip_info->dev->name, sendername); */
2226
2227 /*
2228 * Fill in (pseudo) source and destination addresses in the packet.
2229 * We assume that the destination address was our address (the radio does not
2230 * tell us this). If the radio supplies a source address, then we use it.
2231 */
2232 header.dst_addr = strip_info->true_dev_addr;
2233 string_to_radio_address(&header.src_addr, sendername);
2234
2235#ifdef EXT_COUNTERS
2236 if (key.l == SIP0Key.l) {
2237 strip_info->rx_rbytes += (end - ptr);
2238 process_IP_packet(strip_info, &header, ptr, end);
2239 } else if (key.l == ARP0Key.l) {
2240 strip_info->rx_rbytes += (end - ptr);
2241 process_ARP_packet(strip_info, &header, ptr, end);
2242 } else if (key.l == ATR_Key.l) {
2243 strip_info->rx_ebytes += (end - ptr);
2244 process_AT_response(strip_info, ptr, end);
2245 } else if (key.l == ACK_Key.l) {
2246 strip_info->rx_ebytes += (end - ptr);
2247 process_ACK(strip_info, ptr, end);
2248 } else if (key.l == INF_Key.l) {
2249 strip_info->rx_ebytes += (end - ptr);
2250 process_Info(strip_info, ptr, end);
2251 } else if (key.l == ERR_Key.l) {
2252 strip_info->rx_ebytes += (end - ptr);
2253 RecvErr_Message(strip_info, sendername, ptr, end - ptr);
2254 } else
2255 RecvErr("Unrecognized protocol key", strip_info);
2256#else
2257 if (key.l == SIP0Key.l)
2258 process_IP_packet(strip_info, &header, ptr, end);
2259 else if (key.l == ARP0Key.l)
2260 process_ARP_packet(strip_info, &header, ptr, end);
2261 else if (key.l == ATR_Key.l)
2262 process_AT_response(strip_info, ptr, end);
2263 else if (key.l == ACK_Key.l)
2264 process_ACK(strip_info, ptr, end);
2265 else if (key.l == INF_Key.l)
2266 process_Info(strip_info, ptr, end);
2267 else if (key.l == ERR_Key.l)
2268 RecvErr_Message(strip_info, sendername, ptr, end - ptr);
2269 else
2270 RecvErr("Unrecognized protocol key", strip_info);
2271#endif
2272}
2273
2274#define TTYERROR(X) ((X) == TTY_BREAK ? "Break" : \
2275 (X) == TTY_FRAME ? "Framing Error" : \
2276 (X) == TTY_PARITY ? "Parity Error" : \
2277 (X) == TTY_OVERRUN ? "Hardware Overrun" : "Unknown Error")
2278
2279/*
2280 * Handle the 'receiver data ready' interrupt.
2281 * This function is called by the 'tty_io' module in the kernel when
2282 * a block of STRIP data has been received, which can now be decapsulated
2283 * and sent on to some IP layer for further processing.
2284 */
2285
2286static void strip_receive_buf(struct tty_struct *tty, const unsigned char *cp,
2287 char *fp, int count)
2288{
2289 struct strip *strip_info = (struct strip *) tty->disc_data;
2290 const unsigned char *end = cp + count;
2291
2292 if (!strip_info || strip_info->magic != STRIP_MAGIC
2293 || !netif_running(strip_info->dev))
2294 return;
2295
2296 spin_lock_bh(&strip_lock);
2297#if 0
2298 {
2299 struct timeval tv;
2300 do_gettimeofday(&tv);
2301 printk(KERN_INFO
2302 "**** strip_receive_buf: %3d bytes at %02d.%06d\n",
2303 count, tv.tv_sec % 100, tv.tv_usec);
2304 }
2305#endif
2306
2307#ifdef EXT_COUNTERS
2308 strip_info->rx_sbytes += count;
2309#endif
2310
2311 /* Read the characters out of the buffer */
2312 while (cp < end) {
2313 if (fp && *fp)
2314 printk(KERN_INFO "%s: %s on serial port\n",
2315 strip_info->dev->name, TTYERROR(*fp));
2316 if (fp && *fp++ && !strip_info->discard) { /* If there's a serial error, record it */
2317 /* If we have some characters in the buffer, discard them */
2318 strip_info->discard = strip_info->sx_count;
2319 strip_info->rx_errors++;
2320 }
2321
2322 /* Leading control characters (CR, NL, Tab, etc.) are ignored */
2323 if (strip_info->sx_count > 0 || *cp >= ' ') {
2324 if (*cp == 0x0D) { /* If end of packet, decide what to do with it */
2325 if (strip_info->sx_count > 3000)
2326 printk(KERN_INFO
2327 "%s: Cut a %d byte packet (%zd bytes remaining)%s\n",
2328 strip_info->dev->name,
2329 strip_info->sx_count,
2330 end - cp - 1,
2331 strip_info->
2332 discard ? " (discarded)" :
2333 "");
2334 if (strip_info->sx_count >
2335 strip_info->sx_size) {
2336 strip_info->rx_over_errors++;
2337 printk(KERN_INFO
2338 "%s: sx_buff overflow (%d bytes total)\n",
2339 strip_info->dev->name,
2340 strip_info->sx_count);
2341 } else if (strip_info->discard)
2342 printk(KERN_INFO
2343 "%s: Discarding bad packet (%d/%d)\n",
2344 strip_info->dev->name,
2345 strip_info->discard,
2346 strip_info->sx_count);
2347 else
2348 process_message(strip_info);
2349 strip_info->discard = 0;
2350 strip_info->sx_count = 0;
2351 } else {
2352 /* Make sure we have space in the buffer */
2353 if (strip_info->sx_count <
2354 strip_info->sx_size)
2355 strip_info->sx_buff[strip_info->
2356 sx_count] =
2357 *cp;
2358 strip_info->sx_count++;
2359 }
2360 }
2361 cp++;
2362 }
2363 spin_unlock_bh(&strip_lock);
2364}
2365
2366
2367/************************************************************************/
2368/* General control routines */
2369
2370static int set_mac_address(struct strip *strip_info,
2371 MetricomAddress * addr)
2372{
2373 /*
2374 * We're using a manually specified address if the address is set
2375 * to anything other than all ones. Setting the address to all ones
2376 * disables manual mode and goes back to automatic address determination
2377 * (tracking the true address that the radio has).
2378 */
2379 strip_info->manual_dev_addr =
2380 memcmp(addr->c, broadcast_address.c,
2381 sizeof(broadcast_address));
2382 if (strip_info->manual_dev_addr)
2383 *(MetricomAddress *) strip_info->dev->dev_addr = *addr;
2384 else
2385 *(MetricomAddress *) strip_info->dev->dev_addr =
2386 strip_info->true_dev_addr;
2387 return 0;
2388}
2389
2390static int strip_set_mac_address(struct net_device *dev, void *addr)
2391{
2392 struct strip *strip_info = netdev_priv(dev);
2393 struct sockaddr *sa = addr;
2394 printk(KERN_INFO "%s: strip_set_dev_mac_address called\n", dev->name);
2395 set_mac_address(strip_info, (MetricomAddress *) sa->sa_data);
2396 return 0;
2397}
2398
2399static struct net_device_stats *strip_get_stats(struct net_device *dev)
2400{
2401 struct strip *strip_info = netdev_priv(dev);
2402 static struct net_device_stats stats;
2403
2404 memset(&stats, 0, sizeof(struct net_device_stats));
2405
2406 stats.rx_packets = strip_info->rx_packets;
2407 stats.tx_packets = strip_info->tx_packets;
2408 stats.rx_dropped = strip_info->rx_dropped;
2409 stats.tx_dropped = strip_info->tx_dropped;
2410 stats.tx_errors = strip_info->tx_errors;
2411 stats.rx_errors = strip_info->rx_errors;
2412 stats.rx_over_errors = strip_info->rx_over_errors;
2413 return (&stats);
2414}
2415
2416
2417/************************************************************************/
2418/* Opening and closing */
2419
2420/*
2421 * Here's the order things happen:
2422 * When the user runs "slattach -p strip ..."
33f0f88f 2423 * 1. The TTY module calls strip_open;;
1da177e4
LT
2424 * 2. strip_open calls strip_alloc
2425 * 3. strip_alloc calls register_netdev
2426 * 4. register_netdev calls strip_dev_init
2427 * 5. then strip_open finishes setting up the strip_info
2428 *
2429 * When the user runs "ifconfig st<x> up address netmask ..."
2430 * 6. strip_open_low gets called
2431 *
2432 * When the user runs "ifconfig st<x> down"
2433 * 7. strip_close_low gets called
2434 *
2435 * When the user kills the slattach process
2436 * 8. strip_close gets called
2437 * 9. strip_close calls dev_close
2438 * 10. if the device is still up, then dev_close calls strip_close_low
2439 * 11. strip_close calls strip_free
2440 */
2441
2442/* Open the low-level part of the STRIP channel. Easy! */
2443
2444static int strip_open_low(struct net_device *dev)
2445{
2446 struct strip *strip_info = netdev_priv(dev);
2447
2448 if (strip_info->tty == NULL)
2449 return (-ENODEV);
2450
2451 if (!allocate_buffers(strip_info, dev->mtu))
2452 return (-ENOMEM);
2453
2454 strip_info->sx_count = 0;
2455 strip_info->tx_left = 0;
2456
2457 strip_info->discard = 0;
2458 strip_info->working = FALSE;
2459 strip_info->firmware_level = NoStructure;
2460 strip_info->next_command = CompatibilityCommand;
2461 strip_info->user_baud = get_baud(strip_info->tty);
2462
2463 printk(KERN_INFO "%s: Initializing Radio.\n",
2464 strip_info->dev->name);
2465 ResetRadio(strip_info);
2466 strip_info->idle_timer.expires = jiffies + 1 * HZ;
2467 add_timer(&strip_info->idle_timer);
2468 netif_wake_queue(dev);
2469 return (0);
2470}
2471
2472
2473/*
2474 * Close the low-level part of the STRIP channel. Easy!
2475 */
2476
2477static int strip_close_low(struct net_device *dev)
2478{
2479 struct strip *strip_info = netdev_priv(dev);
2480
2481 if (strip_info->tty == NULL)
2482 return -EBUSY;
2483 strip_info->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
2484
2485 netif_stop_queue(dev);
2486
2487 /*
2488 * Free all STRIP frame buffers.
2489 */
b4558ea9
JJ
2490 kfree(strip_info->rx_buff);
2491 strip_info->rx_buff = NULL;
2492 kfree(strip_info->sx_buff);
2493 strip_info->sx_buff = NULL;
2494 kfree(strip_info->tx_buff);
2495 strip_info->tx_buff = NULL;
2496
1da177e4
LT
2497 del_timer(&strip_info->idle_timer);
2498 return 0;
2499}
2500
2501/*
2502 * This routine is called by DDI when the
2503 * (dynamically assigned) device is registered
2504 */
2505
2506static void strip_dev_setup(struct net_device *dev)
2507{
2508 /*
2509 * Finish setting up the DEVICE info.
2510 */
2511
2512 SET_MODULE_OWNER(dev);
2513
2514 dev->trans_start = 0;
2515 dev->last_rx = 0;
2516 dev->tx_queue_len = 30; /* Drop after 30 frames queued */
2517
2518 dev->flags = 0;
2519 dev->mtu = DEFAULT_STRIP_MTU;
2520 dev->type = ARPHRD_METRICOM; /* dtang */
2521 dev->hard_header_len = sizeof(STRIP_Header);
2522 /*
2523 * dev->priv Already holds a pointer to our struct strip
2524 */
2525
2526 *(MetricomAddress *) & dev->broadcast = broadcast_address;
2527 dev->dev_addr[0] = 0;
2528 dev->addr_len = sizeof(MetricomAddress);
2529
2530 /*
2531 * Pointers to interface service routines.
2532 */
2533
2534 dev->open = strip_open_low;
2535 dev->stop = strip_close_low;
2536 dev->hard_start_xmit = strip_xmit;
2537 dev->hard_header = strip_header;
2538 dev->rebuild_header = strip_rebuild_header;
2539 dev->set_mac_address = strip_set_mac_address;
2540 dev->get_stats = strip_get_stats;
2541 dev->change_mtu = strip_change_mtu;
2542}
2543
2544/*
2545 * Free a STRIP channel.
2546 */
2547
2548static void strip_free(struct strip *strip_info)
2549{
2550 spin_lock_bh(&strip_lock);
2551 list_del_rcu(&strip_info->list);
2552 spin_unlock_bh(&strip_lock);
2553
2554 strip_info->magic = 0;
2555
2556 free_netdev(strip_info->dev);
2557}
2558
2559
2560/*
2561 * Allocate a new free STRIP channel
2562 */
2563static struct strip *strip_alloc(void)
2564{
2565 struct list_head *n;
2566 struct net_device *dev;
2567 struct strip *strip_info;
2568
2569 dev = alloc_netdev(sizeof(struct strip), "st%d",
2570 strip_dev_setup);
2571
2572 if (!dev)
2573 return NULL; /* If no more memory, return */
2574
2575
2576 strip_info = dev->priv;
2577 strip_info->dev = dev;
2578
2579 strip_info->magic = STRIP_MAGIC;
2580 strip_info->tty = NULL;
2581
2582 strip_info->gratuitous_arp = jiffies + LongTime;
2583 strip_info->arp_interval = 0;
2584 init_timer(&strip_info->idle_timer);
2585 strip_info->idle_timer.data = (long) dev;
2586 strip_info->idle_timer.function = strip_IdleTask;
2587
2588
2589 spin_lock_bh(&strip_lock);
2590 rescan:
2591 /*
2592 * Search the list to find where to put our new entry
2593 * (and in the process decide what channel number it is
2594 * going to be)
2595 */
2596 list_for_each(n, &strip_list) {
2597 struct strip *s = hlist_entry(n, struct strip, list);
2598
2599 if (s->dev->base_addr == dev->base_addr) {
2600 ++dev->base_addr;
2601 goto rescan;
2602 }
2603 }
2604
2605 sprintf(dev->name, "st%ld", dev->base_addr);
2606
2607 list_add_tail_rcu(&strip_info->list, &strip_list);
2608 spin_unlock_bh(&strip_lock);
2609
2610 return strip_info;
2611}
2612
2613/*
2614 * Open the high-level part of the STRIP channel.
2615 * This function is called by the TTY module when the
2616 * STRIP line discipline is called for. Because we are
2617 * sure the tty line exists, we only have to link it to
2618 * a free STRIP channel...
2619 */
2620
2621static int strip_open(struct tty_struct *tty)
2622{
2623 struct strip *strip_info = (struct strip *) tty->disc_data;
2624
2625 /*
2626 * First make sure we're not already connected.
2627 */
2628
2629 if (strip_info && strip_info->magic == STRIP_MAGIC)
2630 return -EEXIST;
2631
2632 /*
2633 * OK. Find a free STRIP channel to use.
2634 */
2635 if ((strip_info = strip_alloc()) == NULL)
2636 return -ENFILE;
2637
2638 /*
2639 * Register our newly created device so it can be ifconfig'd
2640 * strip_dev_init() will be called as a side-effect
2641 */
2642
2643 if (register_netdev(strip_info->dev) != 0) {
2644 printk(KERN_ERR "strip: register_netdev() failed.\n");
2645 strip_free(strip_info);
2646 return -ENFILE;
2647 }
2648
2649 strip_info->tty = tty;
2650 tty->disc_data = strip_info;
33f0f88f
AC
2651 tty->receive_room = 65536;
2652
1da177e4
LT
2653 if (tty->driver->flush_buffer)
2654 tty->driver->flush_buffer(tty);
2655
2656 /*
2657 * Restore default settings
2658 */
2659
2660 strip_info->dev->type = ARPHRD_METRICOM; /* dtang */
2661
2662 /*
2663 * Set tty options
2664 */
2665
2666 tty->termios->c_iflag |= IGNBRK | IGNPAR; /* Ignore breaks and parity errors. */
2667 tty->termios->c_cflag |= CLOCAL; /* Ignore modem control signals. */
2668 tty->termios->c_cflag &= ~HUPCL; /* Don't close on hup */
2669
2670 printk(KERN_INFO "STRIP: device \"%s\" activated\n",
2671 strip_info->dev->name);
2672
2673 /*
2674 * Done. We have linked the TTY line to a channel.
2675 */
2676 return (strip_info->dev->base_addr);
2677}
2678
2679/*
2680 * Close down a STRIP channel.
2681 * This means flushing out any pending queues, and then restoring the
2682 * TTY line discipline to what it was before it got hooked to STRIP
2683 * (which usually is TTY again).
2684 */
2685
2686static void strip_close(struct tty_struct *tty)
2687{
2688 struct strip *strip_info = (struct strip *) tty->disc_data;
2689
2690 /*
2691 * First make sure we're connected.
2692 */
2693
2694 if (!strip_info || strip_info->magic != STRIP_MAGIC)
2695 return;
2696
2697 unregister_netdev(strip_info->dev);
2698
2699 tty->disc_data = NULL;
2700 strip_info->tty = NULL;
2701 printk(KERN_INFO "STRIP: device \"%s\" closed down\n",
2702 strip_info->dev->name);
2703 strip_free(strip_info);
2704 tty->disc_data = NULL;
2705}
2706
2707
2708/************************************************************************/
2709/* Perform I/O control calls on an active STRIP channel. */
2710
2711static int strip_ioctl(struct tty_struct *tty, struct file *file,
2712 unsigned int cmd, unsigned long arg)
2713{
2714 struct strip *strip_info = (struct strip *) tty->disc_data;
2715
2716 /*
2717 * First make sure we're connected.
2718 */
2719
2720 if (!strip_info || strip_info->magic != STRIP_MAGIC)
2721 return -EINVAL;
2722
2723 switch (cmd) {
2724 case SIOCGIFNAME:
2725 if(copy_to_user((void __user *) arg, strip_info->dev->name, strlen(strip_info->dev->name) + 1))
2726 return -EFAULT;
2727 break;
2728 case SIOCSIFHWADDR:
2729 {
2730 MetricomAddress addr;
2731 //printk(KERN_INFO "%s: SIOCSIFHWADDR\n", strip_info->dev->name);
2732 if(copy_from_user(&addr, (void __user *) arg, sizeof(MetricomAddress)))
2733 return -EFAULT;
2734 return set_mac_address(strip_info, &addr);
2735 }
2736 /*
2737 * Allow stty to read, but not set, the serial port
2738 */
2739
2740 case TCGETS:
2741 case TCGETA:
2742 return n_tty_ioctl(tty, file, cmd, arg);
2743 break;
2744 default:
2745 return -ENOIOCTLCMD;
2746 break;
2747 }
2748 return 0;
2749}
2750
2751
2752/************************************************************************/
2753/* Initialization */
2754
2755static struct tty_ldisc strip_ldisc = {
2756 .magic = TTY_LDISC_MAGIC,
2757 .name = "strip",
2758 .owner = THIS_MODULE,
2759 .open = strip_open,
2760 .close = strip_close,
2761 .ioctl = strip_ioctl,
2762 .receive_buf = strip_receive_buf,
1da177e4
LT
2763 .write_wakeup = strip_write_some_more,
2764};
2765
2766/*
2767 * Initialize the STRIP driver.
2768 * This routine is called at boot time, to bootstrap the multi-channel
2769 * STRIP driver
2770 */
2771
2772static char signon[] __initdata =
2773 KERN_INFO "STRIP: Version %s (unlimited channels)\n";
2774
2775static int __init strip_init_driver(void)
2776{
2777 int status;
2778
2779 printk(signon, StripVersion);
2780
2781
2782 /*
2783 * Fill in our line protocol discipline, and register it
2784 */
2785 if ((status = tty_register_ldisc(N_STRIP, &strip_ldisc)))
2786 printk(KERN_ERR "STRIP: can't register line discipline (err = %d)\n",
2787 status);
2788
2789 /*
2790 * Register the status file with /proc
2791 */
2792 proc_net_fops_create("strip", S_IFREG | S_IRUGO, &strip_seq_fops);
2793
2794 return status;
2795}
2796
2797module_init(strip_init_driver);
2798
2799static const char signoff[] __exitdata =
2800 KERN_INFO "STRIP: Module Unloaded\n";
2801
2802static void __exit strip_exit_driver(void)
2803{
2804 int i;
2805 struct list_head *p,*n;
2806
2807 /* module ref count rules assure that all entries are unregistered */
2808 list_for_each_safe(p, n, &strip_list) {
2809 struct strip *s = list_entry(p, struct strip, list);
2810 strip_free(s);
2811 }
2812
2813 /* Unregister with the /proc/net file here. */
2814 proc_net_remove("strip");
2815
64ccd715 2816 if ((i = tty_unregister_ldisc(N_STRIP)))
1da177e4
LT
2817 printk(KERN_ERR "STRIP: can't unregister line discipline (err = %d)\n", i);
2818
2819 printk(signoff);
2820}
2821
2822module_exit(strip_exit_driver);
2823
2824MODULE_AUTHOR("Stuart Cheshire <cheshire@cs.stanford.edu>");
2825MODULE_DESCRIPTION("Starmode Radio IP (STRIP) Device Driver");
2826MODULE_LICENSE("Dual BSD/GPL");
2827
2828MODULE_SUPPORTED_DEVICE("Starmode Radio IP (STRIP) modem");