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