pre-2.3.4..
[davej-history.git] / drivers / net / strip.c
blobc33421018c05f8180d550dffc5c60160e19ea4e1
1 /*
2 * Copyright 1996 The Board of Trustees of The Leland Stanford
3 * Junior University. All Rights Reserved.
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.
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).
17 * Version: @(#)strip.c 1.3 July 1997
19 * Author: Stuart Cheshire <cheshire@cs.stanford.edu>
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)
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.
30 * v0.9.2 13th March 1996 (SC)
31 * Uses arp cache lookups (but doesn't send arp packets yet)
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)
37 * v0.9.4 27th April 1996 (SC)
38 * First attempt at using "&COMMAND" Starmode AT commands
40 * v0.9.5 29th May 1996 (SC)
41 * First attempt at sending (unicast) ARP packets
43 * v0.9.6 5th June 1996 (Elliot)
44 * Put "message level" tags in every "printk" statement
46 * v0.9.7 13th June 1996 (laik)
47 * Added support for the /proc fs
49 * v0.9.8 July 1996 (Mema)
50 * Added packet logging
52 * v1.0 November 1996 (SC)
53 * Fixed (severe) memory leaks in the /proc fs code
54 * Fixed race conditions in the logging code
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)
61 * v1.2 January 1997 (SC)
62 * Put portables list back in
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.
71 #ifdef MODULE
72 static const char StripVersion[] = "1.3-STUART.CHESHIRE-MODULAR";
73 #else
74 static const char StripVersion[] = "1.3-STUART.CHESHIRE";
75 #endif
77 #define TICKLE_TIMERS 0
78 #define EXT_COUNTERS 1
81 /************************************************************************/
82 /* Header files */
84 #include <linux/config.h>
86 #ifdef MODULE
87 #include <linux/module.h>
88 #include <linux/version.h>
89 #endif
91 #include <asm/system.h>
92 #include <asm/uaccess.h>
93 #include <asm/segment.h>
94 #include <asm/bitops.h>
97 * isdigit() and isspace() use the ctype[] array, which is not available
98 * to kernel modules. If compiling as a module, use a local definition
99 * of isdigit() and isspace() until _ctype is added to ksyms.
101 #ifdef MODULE
102 # define isdigit(c) ('0' <= (c) && (c) <= '9')
103 # define isspace(c) ((c) == ' ' || (c) == '\t')
104 #else
105 # include <linux/ctype.h>
106 #endif
108 #include <linux/string.h>
109 #include <linux/mm.h>
110 #include <linux/interrupt.h>
111 #include <linux/in.h>
112 #include <linux/tty.h>
113 #include <linux/errno.h>
114 #include <linux/netdevice.h>
115 #include <linux/inetdevice.h>
116 #include <linux/etherdevice.h>
117 #include <linux/skbuff.h>
118 #include <linux/if_arp.h>
119 #include <linux/if_strip.h>
120 #include <linux/proc_fs.h>
121 #include <linux/serial.h>
122 #include <net/arp.h>
124 #include <linux/ip.h>
125 #include <linux/tcp.h>
126 #include <linux/time.h>
129 /************************************************************************/
130 /* Useful structures and definitions */
133 * A MetricomKey identifies the protocol being carried inside a Metricom
134 * Starmode packet.
137 typedef union
139 __u8 c[4];
140 __u32 l;
141 } MetricomKey;
144 * An IP address can be viewed as four bytes in memory (which is what it is) or as
145 * a single 32-bit long (which is convenient for assignment, equality testing etc.)
148 typedef union
150 __u8 b[4];
151 __u32 l;
152 } IPaddr;
155 * A MetricomAddressString is used to hold a printable representation of
156 * a Metricom address.
159 typedef struct
161 __u8 c[24];
162 } MetricomAddressString;
164 /* Encapsulation can expand packet of size x to 65/64x + 1
165 * Sent packet looks like "<CR>*<address>*<key><encaps payload><CR>"
166 * 1 1 1-18 1 4 ? 1
167 * eg. <CR>*0000-1234*SIP0<encaps payload><CR>
168 * We allow 31 bytes for the stars, the key, the address and the <CR>s
170 #define STRIP_ENCAP_SIZE(X) (32 + (X)*65L/64L)
173 * A STRIP_Header is never really sent over the radio, but making a dummy
174 * header for internal use within the kernel that looks like an Ethernet
175 * header makes certain other software happier. For example, tcpdump
176 * already understands Ethernet headers.
179 typedef struct
181 MetricomAddress dst_addr; /* Destination address, e.g. "0000-1234" */
182 MetricomAddress src_addr; /* Source address, e.g. "0000-5678" */
183 unsigned short protocol; /* The protocol type, using Ethernet codes */
184 } STRIP_Header;
186 typedef struct
188 char c[60];
189 } MetricomNode;
191 #define NODE_TABLE_SIZE 32
192 typedef struct
194 struct timeval timestamp;
195 int num_nodes;
196 MetricomNode node[NODE_TABLE_SIZE];
197 } MetricomNodeTable;
199 enum { FALSE = 0, TRUE = 1 };
202 * Holds the radio's firmware version.
204 typedef struct
206 char c[50];
207 } FirmwareVersion;
210 * Holds the radio's serial number.
212 typedef struct
214 char c[18];
215 } SerialNumber;
218 * Holds the radio's battery voltage.
220 typedef struct
222 char c[11];
223 } BatteryVoltage;
225 typedef struct
227 char c[8];
228 } char8;
230 enum
232 NoStructure = 0, /* Really old firmware */
233 StructuredMessages = 1, /* Parsable AT response msgs */
234 ChecksummedMessages = 2 /* Parsable AT response msgs with checksums */
235 } FirmwareLevel;
237 struct strip
239 int magic;
241 * These are pointers to the malloc()ed frame buffers.
244 unsigned char *rx_buff; /* buffer for received IP packet*/
245 unsigned char *sx_buff; /* buffer for received serial data*/
246 int sx_count; /* received serial data counter */
247 int sx_size; /* Serial buffer size */
248 unsigned char *tx_buff; /* transmitter buffer */
249 unsigned char *tx_head; /* pointer to next byte to XMIT */
250 int tx_left; /* bytes left in XMIT queue */
251 int tx_size; /* Serial buffer size */
254 * STRIP interface statistics.
257 unsigned long rx_packets; /* inbound frames counter */
258 unsigned long tx_packets; /* outbound frames counter */
259 unsigned long rx_errors; /* Parity, etc. errors */
260 unsigned long tx_errors; /* Planned stuff */
261 unsigned long rx_dropped; /* No memory for skb */
262 unsigned long tx_dropped; /* When MTU change */
263 unsigned long rx_over_errors; /* Frame bigger then STRIP buf. */
265 unsigned long pps_timer; /* Timer to determine pps */
266 unsigned long rx_pps_count; /* Counter to determine pps */
267 unsigned long tx_pps_count; /* Counter to determine pps */
268 unsigned long sx_pps_count; /* Counter to determine pps */
269 unsigned long rx_average_pps; /* rx packets per second * 8 */
270 unsigned long tx_average_pps; /* tx packets per second * 8 */
271 unsigned long sx_average_pps; /* sent packets per second * 8 */
273 #ifdef EXT_COUNTERS
274 unsigned long rx_bytes; /* total received bytes */
275 unsigned long tx_bytes; /* total received bytes */
276 unsigned long rx_rbytes; /* bytes thru radio i/f */
277 unsigned long tx_rbytes; /* bytes thru radio i/f */
278 unsigned long rx_sbytes; /* tot bytes thru serial i/f */
279 unsigned long tx_sbytes; /* tot bytes thru serial i/f */
280 unsigned long rx_ebytes; /* tot stat/err bytes */
281 unsigned long tx_ebytes; /* tot stat/err bytes */
282 #endif
285 * Internal variables.
288 struct strip *next; /* The next struct in the list */
289 struct strip **referrer; /* The pointer that points to us*/
290 int discard; /* Set if serial error */
291 int working; /* Is radio working correctly? */
292 int firmware_level; /* Message structuring level */
293 int next_command; /* Next periodic command */
294 unsigned int user_baud; /* The user-selected baud rate */
295 int mtu; /* Our mtu (to spot changes!) */
296 long watchdog_doprobe; /* Next time to test the radio */
297 long watchdog_doreset; /* Time to do next reset */
298 long gratuitous_arp; /* Time to send next ARP refresh*/
299 long arp_interval; /* Next ARP interval */
300 struct timer_list idle_timer; /* For periodic wakeup calls */
301 MetricomAddress true_dev_addr; /* True address of radio */
302 int manual_dev_addr; /* Hack: See note below */
304 FirmwareVersion firmware_version; /* The radio's firmware version */
305 SerialNumber serial_number; /* The radio's serial number */
306 BatteryVoltage battery_voltage; /* The radio's battery voltage */
309 * Other useful structures.
312 struct tty_struct *tty; /* ptr to TTY structure */
313 char8 if_name; /* Dynamically generated name */
314 struct device dev; /* Our device structure */
317 * Neighbour radio records
320 MetricomNodeTable portables;
321 MetricomNodeTable poletops;
325 * Note: manual_dev_addr hack
327 * It is not possible to change the hardware address of a Metricom radio,
328 * or to send packets with a user-specified hardware source address, thus
329 * trying to manually set a hardware source address is a questionable
330 * thing to do. However, if the user *does* manually set the hardware
331 * source address of a STRIP interface, then the kernel will believe it,
332 * and use it in certain places. For example, the hardware address listed
333 * by ifconfig will be the manual address, not the true one.
334 * (Both addresses are listed in /proc/net/strip.)
335 * Also, ARP packets will be sent out giving the user-specified address as
336 * the source address, not the real address. This is dangerous, because
337 * it means you won't receive any replies -- the ARP replies will go to
338 * the specified address, which will be some other radio. The case where
339 * this is useful is when that other radio is also connected to the same
340 * machine. This allows you to connect a pair of radios to one machine,
341 * and to use one exclusively for inbound traffic, and the other
342 * exclusively for outbound traffic. Pretty neat, huh?
344 * Here's the full procedure to set this up:
346 * 1. "slattach" two interfaces, e.g. st0 for outgoing packets,
347 * and st1 for incoming packets
349 * 2. "ifconfig" st0 (outbound radio) to have the hardware address
350 * which is the real hardware address of st1 (inbound radio).
351 * Now when it sends out packets, it will masquerade as st1, and
352 * replies will be sent to that radio, which is exactly what we want.
354 * 3. Set the route table entry ("route add default ..." or
355 * "route add -net ...", as appropriate) to send packets via the st0
356 * interface (outbound radio). Do not add any route which sends packets
357 * out via the st1 interface -- that radio is for inbound traffic only.
359 * 4. "ifconfig" st1 (inbound radio) to have hardware address zero.
360 * This tells the STRIP driver to "shut down" that interface and not
361 * send any packets through it. In particular, it stops sending the
362 * periodic gratuitous ARP packets that a STRIP interface normally sends.
363 * Also, when packets arrive on that interface, it will search the
364 * interface list to see if there is another interface who's manual
365 * hardware address matches its own real address (i.e. st0 in this
366 * example) and if so it will transfer ownership of the skbuff to
367 * that interface, so that it looks to the kernel as if the packet
368 * arrived on that interface. This is necessary because when the
369 * kernel sends an ARP packet on st0, it expects to get a reply on
370 * st0, and if it sees the reply come from st1 then it will ignore
371 * it (to be accurate, it puts the entry in the ARP table, but
372 * labelled in such a way that st0 can't use it).
374 * Thanks to Petros Maniatis for coming up with the idea of splitting
375 * inbound and outbound traffic between two interfaces, which turned
376 * out to be really easy to implement, even if it is a bit of a hack.
378 * Having set a manual address on an interface, you can restore it
379 * to automatic operation (where the address is automatically kept
380 * consistent with the real address of the radio) by setting a manual
381 * address of all ones, e.g. "ifconfig st0 hw strip FFFFFFFFFFFF"
382 * This 'turns off' manual override mode for the device address.
384 * Note: The IEEE 802 headers reported in tcpdump will show the *real*
385 * radio addresses the packets were sent and received from, so that you
386 * can see what is really going on with packets, and which interfaces
387 * they are really going through.
391 /************************************************************************/
392 /* Constants */
395 * CommandString1 works on all radios
396 * Other CommandStrings are only used with firmware that provides structured responses.
398 * ats319=1 Enables Info message for node additions and deletions
399 * ats319=2 Enables Info message for a new best node
400 * ats319=4 Enables checksums
401 * ats319=8 Enables ACK messages
404 static const int MaxCommandStringLength = 32;
405 static const int CompatibilityCommand = 1;
407 static const char CommandString0[] = "*&COMMAND*ATS319=7"; /* Turn on checksums & info messages */
408 static const char CommandString1[] = "*&COMMAND*ATS305?"; /* Query radio name */
409 static const char CommandString2[] = "*&COMMAND*ATS325?"; /* Query battery voltage */
410 static const char CommandString3[] = "*&COMMAND*ATS300?"; /* Query version information */
411 static const char CommandString4[] = "*&COMMAND*ATS311?"; /* Query poletop list */
412 static const char CommandString5[] = "*&COMMAND*AT~LA"; /* Query portables list */
413 typedef struct { const char *string; long length; } StringDescriptor;
415 static const StringDescriptor CommandString[] =
417 { CommandString0, sizeof(CommandString0)-1 },
418 { CommandString1, sizeof(CommandString1)-1 },
419 { CommandString2, sizeof(CommandString2)-1 },
420 { CommandString3, sizeof(CommandString3)-1 },
421 { CommandString4, sizeof(CommandString4)-1 },
422 { CommandString5, sizeof(CommandString5)-1 }
425 #define GOT_ALL_RADIO_INFO(S) \
426 ((S)->firmware_version.c[0] && \
427 (S)->battery_voltage.c[0] && \
428 memcmp(&(S)->true_dev_addr, zero_address.c, sizeof(zero_address)))
430 static const char hextable[16] = "0123456789ABCDEF";
432 static const MetricomAddress zero_address;
433 static const MetricomAddress broadcast_address = { { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF } };
435 static const MetricomKey SIP0Key = { { "SIP0" } };
436 static const MetricomKey ARP0Key = { { "ARP0" } };
437 static const MetricomKey ATR_Key = { { "ATR " } };
438 static const MetricomKey ACK_Key = { { "ACK_" } };
439 static const MetricomKey INF_Key = { { "INF_" } };
440 static const MetricomKey ERR_Key = { { "ERR_" } };
442 static const long MaxARPInterval = 60 * HZ; /* One minute */
445 * Maximum Starmode packet length is 1183 bytes. Allowing 4 bytes for
446 * protocol key, 4 bytes for checksum, one byte for CR, and 65/64 expansion
447 * for STRIP encoding, that translates to a maximum payload MTU of 1155.
448 * Note: A standard NFS 1K data packet is a total of 0x480 (1152) bytes
449 * long, including IP header, UDP header, and NFS header. Setting the STRIP
450 * MTU to 1152 allows us to send default sized NFS packets without fragmentation.
452 static const unsigned short MAX_SEND_MTU = 1152;
453 static const unsigned short MAX_RECV_MTU = 1500; /* Hoping for Ethernet sized packets in the future! */
454 static const unsigned short DEFAULT_STRIP_MTU = 1152;
455 static const int STRIP_MAGIC = 0x5303;
456 static const long LongTime = 0x7FFFFFFF;
459 /************************************************************************/
460 /* Global variables */
462 static struct strip *struct_strip_list = NULL;
465 /************************************************************************/
466 /* Macros */
468 /* Returns TRUE if text T begins with prefix P */
469 #define has_prefix(T,L,P) (((L) >= sizeof(P)-1) && !strncmp((T), (P), sizeof(P)-1))
471 /* Returns TRUE if text T of length L is equal to string S */
472 #define text_equal(T,L,S) (((L) == sizeof(S)-1) && !strncmp((T), (S), sizeof(S)-1))
474 #define READHEX(X) ((X)>='0' && (X)<='9' ? (X)-'0' : \
475 (X)>='a' && (X)<='f' ? (X)-'a'+10 : \
476 (X)>='A' && (X)<='F' ? (X)-'A'+10 : 0 )
478 #define READHEX16(X) ((__u16)(READHEX(X)))
480 #define READDEC(X) ((X)>='0' && (X)<='9' ? (X)-'0' : 0)
482 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
483 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
484 #define ELEMENTS_OF(X) (sizeof(X) / sizeof((X)[0]))
485 #define ARRAY_END(X) (&((X)[ELEMENTS_OF(X)]))
487 #define JIFFIE_TO_SEC(X) ((X) / HZ)
490 /************************************************************************/
491 /* Utility routines */
493 typedef unsigned long InterruptStatus;
495 extern __inline__ InterruptStatus DisableInterrupts(void)
497 InterruptStatus x;
498 save_flags(x);
499 cli();
500 return(x);
503 extern __inline__ void RestoreInterrupts(InterruptStatus x)
505 restore_flags(x);
508 static int arp_query(unsigned char *haddr, u32 paddr, struct device * dev)
510 struct neighbour *neighbor_entry;
512 neighbor_entry = neigh_lookup(&arp_tbl, &paddr, dev);
514 if (neighbor_entry != NULL)
516 neighbor_entry->used = jiffies;
517 if (neighbor_entry->nud_state & NUD_VALID)
519 memcpy(haddr, neighbor_entry->ha, dev->addr_len);
520 return 1;
523 return 0;
526 static void DumpData(char *msg, struct strip *strip_info, __u8 *ptr, __u8 *end)
528 static const int MAX_DumpData = 80;
529 __u8 pkt_text[MAX_DumpData], *p = pkt_text;
531 *p++ = '\"';
533 while (ptr<end && p < &pkt_text[MAX_DumpData-4])
535 if (*ptr == '\\')
537 *p++ = '\\';
538 *p++ = '\\';
540 else
542 if (*ptr >= 32 && *ptr <= 126)
544 *p++ = *ptr;
546 else
548 sprintf(p, "\\%02X", *ptr);
549 p+= 3;
552 ptr++;
555 if (ptr == end)
557 *p++ = '\"';
560 *p++ = 0;
562 printk(KERN_INFO "%s: %-13s%s\n", strip_info->dev.name, msg, pkt_text);
565 #if 0
566 static void HexDump(char *msg, struct strip *strip_info, __u8 *start, __u8 *end)
568 __u8 *ptr = start;
569 printk(KERN_INFO "%s: %s: %d bytes\n", strip_info->dev.name, msg, end-ptr);
571 while (ptr < end)
573 long offset = ptr - start;
574 __u8 text[80], *p = text;
575 while (ptr < end && p < &text[16*3])
577 *p++ = hextable[*ptr >> 4];
578 *p++ = hextable[*ptr++ & 0xF];
579 *p++ = ' ';
581 p[-1] = 0;
582 printk(KERN_INFO "%s: %4lX %s\n", strip_info->dev.name, offset, text);
585 #endif
588 /************************************************************************/
589 /* Byte stuffing/unstuffing routines */
591 /* Stuffing scheme:
592 * 00 Unused (reserved character)
593 * 01-3F Run of 2-64 different characters
594 * 40-7F Run of 1-64 different characters plus a single zero at the end
595 * 80-BF Run of 1-64 of the same character
596 * C0-FF Run of 1-64 zeroes (ASCII 0)
599 typedef enum
601 Stuff_Diff = 0x00,
602 Stuff_DiffZero = 0x40,
603 Stuff_Same = 0x80,
604 Stuff_Zero = 0xC0,
605 Stuff_NoCode = 0xFF, /* Special code, meaning no code selected */
607 Stuff_CodeMask = 0xC0,
608 Stuff_CountMask = 0x3F,
609 Stuff_MaxCount = 0x3F,
610 Stuff_Magic = 0x0D /* The value we are eliminating */
611 } StuffingCode;
613 /* StuffData encodes the data starting at "src" for "length" bytes.
614 * It writes it to the buffer pointed to by "dst" (which must be at least
615 * as long as 1 + 65/64 of the input length). The output may be up to 1.6%
616 * larger than the input for pathological input, but will usually be smaller.
617 * StuffData returns the new value of the dst pointer as its result.
618 * "code_ptr_ptr" points to a "__u8 *" which is used to hold encoding state
619 * between calls, allowing an encoded packet to be incrementally built up
620 * from small parts. On the first call, the "__u8 *" pointed to should be
621 * initialized to NULL; between subsequent calls the calling routine should
622 * leave the value alone and simply pass it back unchanged so that the
623 * encoder can recover its current state.
626 #define StuffData_FinishBlock(X) \
627 (*code_ptr = (X) ^ Stuff_Magic, code = Stuff_NoCode)
629 static __u8 *StuffData(__u8 *src, __u32 length, __u8 *dst, __u8 **code_ptr_ptr)
631 __u8 *end = src + length;
632 __u8 *code_ptr = *code_ptr_ptr;
633 __u8 code = Stuff_NoCode, count = 0;
635 if (!length)
636 return(dst);
638 if (code_ptr)
641 * Recover state from last call, if applicable
643 code = (*code_ptr ^ Stuff_Magic) & Stuff_CodeMask;
644 count = (*code_ptr ^ Stuff_Magic) & Stuff_CountMask;
647 while (src < end)
649 switch (code)
651 /* Stuff_NoCode: If no current code, select one */
652 case Stuff_NoCode:
653 /* Record where we're going to put this code */
654 code_ptr = dst++;
655 count = 0; /* Reset the count (zero means one instance) */
656 /* Tentatively start a new block */
657 if (*src == 0)
659 code = Stuff_Zero;
660 src++;
662 else
664 code = Stuff_Same;
665 *dst++ = *src++ ^ Stuff_Magic;
667 /* Note: We optimistically assume run of same -- */
668 /* which will be fixed later in Stuff_Same */
669 /* if it turns out not to be true. */
670 break;
672 /* Stuff_Zero: We already have at least one zero encoded */
673 case Stuff_Zero:
674 /* If another zero, count it, else finish this code block */
675 if (*src == 0)
677 count++;
678 src++;
680 else
682 StuffData_FinishBlock(Stuff_Zero + count);
684 break;
686 /* Stuff_Same: We already have at least one byte encoded */
687 case Stuff_Same:
688 /* If another one the same, count it */
689 if ((*src ^ Stuff_Magic) == code_ptr[1])
691 count++;
692 src++;
693 break;
695 /* else, this byte does not match this block. */
696 /* If we already have two or more bytes encoded, finish this code block */
697 if (count)
699 StuffData_FinishBlock(Stuff_Same + count);
700 break;
702 /* else, we only have one so far, so switch to Stuff_Diff code */
703 code = Stuff_Diff;
704 /* and fall through to Stuff_Diff case below
705 * Note cunning cleverness here: case Stuff_Diff compares
706 * the current character with the previous two to see if it
707 * has a run of three the same. Won't this be an error if
708 * there aren't two previous characters stored to compare with?
709 * No. Because we know the current character is *not* the same
710 * as the previous one, the first test below will necessarily
711 * fail and the send half of the "if" won't be executed.
714 /* Stuff_Diff: We have at least two *different* bytes encoded */
715 case Stuff_Diff:
716 /* If this is a zero, must encode a Stuff_DiffZero, and begin a new block */
717 if (*src == 0)
719 StuffData_FinishBlock(Stuff_DiffZero + count);
721 /* else, if we have three in a row, it is worth starting a Stuff_Same block */
722 else if ((*src ^ Stuff_Magic)==dst[-1] && dst[-1]==dst[-2])
724 /* Back off the last two characters we encoded */
725 code += count-2;
726 /* Note: "Stuff_Diff + 0" is an illegal code */
727 if (code == Stuff_Diff + 0)
729 code = Stuff_Same + 0;
731 StuffData_FinishBlock(code);
732 code_ptr = dst-2;
733 /* dst[-1] already holds the correct value */
734 count = 2; /* 2 means three bytes encoded */
735 code = Stuff_Same;
737 /* else, another different byte, so add it to the block */
738 else
740 *dst++ = *src ^ Stuff_Magic;
741 count++;
743 src++; /* Consume the byte */
744 break;
746 if (count == Stuff_MaxCount)
748 StuffData_FinishBlock(code + count);
751 if (code == Stuff_NoCode)
753 *code_ptr_ptr = NULL;
755 else
757 *code_ptr_ptr = code_ptr;
758 StuffData_FinishBlock(code + count);
760 return(dst);
764 * UnStuffData decodes the data at "src", up to (but not including) "end".
765 * It writes the decoded data into the buffer pointed to by "dst", up to a
766 * maximum of "dst_length", and returns the new value of "src" so that a
767 * follow-on call can read more data, continuing from where the first left off.
769 * There are three types of results:
770 * 1. The source data runs out before extracting "dst_length" bytes:
771 * UnStuffData returns NULL to indicate failure.
772 * 2. The source data produces exactly "dst_length" bytes:
773 * UnStuffData returns new_src = end to indicate that all bytes were consumed.
774 * 3. "dst_length" bytes are extracted, with more remaining.
775 * UnStuffData returns new_src < end to indicate that there are more bytes
776 * to be read.
778 * Note: The decoding may be destructive, in that it may alter the source
779 * data in the process of decoding it (this is necessary to allow a follow-on
780 * call to resume correctly).
783 static __u8 *UnStuffData(__u8 *src, __u8 *end, __u8 *dst, __u32 dst_length)
785 __u8 *dst_end = dst + dst_length;
786 /* Sanity check */
787 if (!src || !end || !dst || !dst_length)
788 return(NULL);
789 while (src < end && dst < dst_end)
791 int count = (*src ^ Stuff_Magic) & Stuff_CountMask;
792 switch ((*src ^ Stuff_Magic) & Stuff_CodeMask)
794 case Stuff_Diff:
795 if (src+1+count >= end)
796 return(NULL);
799 *dst++ = *++src ^ Stuff_Magic;
801 while(--count >= 0 && dst < dst_end);
802 if (count < 0)
803 src += 1;
804 else
806 if (count == 0)
807 *src = Stuff_Same ^ Stuff_Magic;
808 else
809 *src = (Stuff_Diff + count) ^ Stuff_Magic;
811 break;
812 case Stuff_DiffZero:
813 if (src+1+count >= end)
814 return(NULL);
817 *dst++ = *++src ^ Stuff_Magic;
819 while(--count >= 0 && dst < dst_end);
820 if (count < 0)
821 *src = Stuff_Zero ^ Stuff_Magic;
822 else
823 *src = (Stuff_DiffZero + count) ^ Stuff_Magic;
824 break;
825 case Stuff_Same:
826 if (src+1 >= end)
827 return(NULL);
830 *dst++ = src[1] ^ Stuff_Magic;
832 while(--count >= 0 && dst < dst_end);
833 if (count < 0)
834 src += 2;
835 else
836 *src = (Stuff_Same + count) ^ Stuff_Magic;
837 break;
838 case Stuff_Zero:
841 *dst++ = 0;
843 while(--count >= 0 && dst < dst_end);
844 if (count < 0)
845 src += 1;
846 else
847 *src = (Stuff_Zero + count) ^ Stuff_Magic;
848 break;
851 if (dst < dst_end)
852 return(NULL);
853 else
854 return(src);
858 /************************************************************************/
859 /* General routines for STRIP */
862 * get_baud returns the current baud rate, as one of the constants defined in
863 * termbits.h
864 * If the user has issued a baud rate override using the 'setserial' command
865 * and the logical current rate is set to 38.4, then the true baud rate
866 * currently in effect (57.6 or 115.2) is returned.
868 static unsigned int get_baud(struct tty_struct *tty)
870 if (!tty || !tty->termios) return(0);
871 if ((tty->termios->c_cflag & CBAUD) == B38400 && tty->driver_data)
873 struct async_struct *info = (struct async_struct *)tty->driver_data;
874 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI ) return(B57600);
875 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) return(B115200);
877 return(tty->termios->c_cflag & CBAUD);
881 * set_baud sets the baud rate to the rate defined by baudcode
882 * Note: The rate B38400 should be avoided, because the user may have
883 * issued a 'setserial' speed override to map that to a different speed.
884 * We could achieve a true rate of 38400 if we needed to by cancelling
885 * any user speed override that is in place, but that might annoy the
886 * user, so it is simplest to just avoid using 38400.
888 static void set_baud(struct tty_struct *tty, unsigned int baudcode)
890 struct termios old_termios = *(tty->termios);
891 tty->termios->c_cflag &= ~CBAUD; /* Clear the old baud setting */
892 tty->termios->c_cflag |= baudcode; /* Set the new baud setting */
893 tty->driver.set_termios(tty, &old_termios);
897 * Convert a string to a Metricom Address.
900 #define IS_RADIO_ADDRESS(p) ( \
901 isdigit((p)[0]) && isdigit((p)[1]) && isdigit((p)[2]) && isdigit((p)[3]) && \
902 (p)[4] == '-' && \
903 isdigit((p)[5]) && isdigit((p)[6]) && isdigit((p)[7]) && isdigit((p)[8]) )
905 static int string_to_radio_address(MetricomAddress *addr, __u8 *p)
907 if (!IS_RADIO_ADDRESS(p)) return(1);
908 addr->c[0] = 0;
909 addr->c[1] = 0;
910 addr->c[2] = READHEX(p[0]) << 4 | READHEX(p[1]);
911 addr->c[3] = READHEX(p[2]) << 4 | READHEX(p[3]);
912 addr->c[4] = READHEX(p[5]) << 4 | READHEX(p[6]);
913 addr->c[5] = READHEX(p[7]) << 4 | READHEX(p[8]);
914 return(0);
918 * Convert a Metricom Address to a string.
921 static __u8 *radio_address_to_string(const MetricomAddress *addr, MetricomAddressString *p)
923 sprintf(p->c, "%02X%02X-%02X%02X", addr->c[2], addr->c[3], addr->c[4], addr->c[5]);
924 return(p->c);
928 * Note: Must make sure sx_size is big enough to receive a stuffed
929 * MAX_RECV_MTU packet. Additionally, we also want to ensure that it's
930 * big enough to receive a large radio neighbour list (currently 4K).
933 static int allocate_buffers(struct strip *strip_info)
935 struct device *dev = &strip_info->dev;
936 int sx_size = MAX(STRIP_ENCAP_SIZE(MAX_RECV_MTU), 4096);
937 int tx_size = STRIP_ENCAP_SIZE(dev->mtu) + MaxCommandStringLength;
938 __u8 *r = kmalloc(MAX_RECV_MTU, GFP_ATOMIC);
939 __u8 *s = kmalloc(sx_size, GFP_ATOMIC);
940 __u8 *t = kmalloc(tx_size, GFP_ATOMIC);
941 if (r && s && t)
943 strip_info->rx_buff = r;
944 strip_info->sx_buff = s;
945 strip_info->tx_buff = t;
946 strip_info->sx_size = sx_size;
947 strip_info->tx_size = tx_size;
948 strip_info->mtu = dev->mtu;
949 return(1);
951 if (r) kfree(r);
952 if (s) kfree(s);
953 if (t) kfree(t);
954 return(0);
958 * MTU has been changed by the IP layer. Unfortunately we are not told
959 * about this, but we spot it ourselves and fix things up. We could be in
960 * an upcall from the tty driver, or in an ip packet queue.
963 static void strip_changedmtu(struct strip *strip_info)
965 int old_mtu = strip_info->mtu;
966 struct device *dev = &strip_info->dev;
967 unsigned char *orbuff = strip_info->rx_buff;
968 unsigned char *osbuff = strip_info->sx_buff;
969 unsigned char *otbuff = strip_info->tx_buff;
970 InterruptStatus intstat;
972 if (dev->mtu > MAX_SEND_MTU)
974 printk(KERN_ERR "%s: MTU exceeds maximum allowable (%d), MTU change cancelled.\n",
975 strip_info->dev.name, MAX_SEND_MTU);
976 dev->mtu = old_mtu;
977 return;
981 * Have to disable interrupts here because we're reallocating and resizing
982 * the serial buffers, and we can't have data arriving in them while we're
983 * moving them around in memory. This may cause data to be lost on the serial
984 * port, but hopefully people won't change MTU that often.
985 * Also note, this may not work on a symmetric multi-processor system.
987 intstat = DisableInterrupts();
989 if (!allocate_buffers(strip_info))
991 RestoreInterrupts(intstat);
992 printk(KERN_ERR "%s: unable to grow strip buffers, MTU change cancelled.\n",
993 strip_info->dev.name);
994 dev->mtu = old_mtu;
995 return;
998 if (strip_info->sx_count)
1000 if (strip_info->sx_count <= strip_info->sx_size)
1001 memcpy(strip_info->sx_buff, osbuff, strip_info->sx_count);
1002 else
1004 strip_info->discard = strip_info->sx_count;
1005 strip_info->rx_over_errors++;
1009 if (strip_info->tx_left)
1011 if (strip_info->tx_left <= strip_info->tx_size)
1012 memcpy(strip_info->tx_buff, strip_info->tx_head, strip_info->tx_left);
1013 else
1015 strip_info->tx_left = 0;
1016 strip_info->tx_dropped++;
1019 strip_info->tx_head = strip_info->tx_buff;
1021 RestoreInterrupts(intstat);
1023 printk(KERN_NOTICE "%s: strip MTU changed fom %d to %d.\n",
1024 strip_info->dev.name, old_mtu, strip_info->mtu);
1026 if (orbuff) kfree(orbuff);
1027 if (osbuff) kfree(osbuff);
1028 if (otbuff) kfree(otbuff);
1031 static void strip_unlock(struct strip *strip_info)
1034 * Set the timer to go off in one second.
1036 strip_info->idle_timer.expires = jiffies + 1*HZ;
1037 add_timer(&strip_info->idle_timer);
1038 if (!test_and_clear_bit(0, (void *)&strip_info->dev.tbusy))
1039 printk(KERN_ERR "%s: trying to unlock already unlocked device!\n",
1040 strip_info->dev.name);
1044 /************************************************************************/
1045 /* Callback routines for exporting information through /proc */
1048 * This function updates the total amount of data printed so far. It then
1049 * determines if the amount of data printed into a buffer has reached the
1050 * offset requested. If it hasn't, then the buffer is shifted over so that
1051 * the next bit of data can be printed over the old bit. If the total
1052 * amount printed so far exceeds the total amount requested, then this
1053 * function returns 1, otherwise 0.
1055 static int
1056 shift_buffer(char *buffer, int requested_offset, int requested_len,
1057 int *total, int *slop, char **buf)
1059 int printed;
1061 /* printk(KERN_DEBUG "shift: buffer: %d o: %d l: %d t: %d buf: %d\n",
1062 (int) buffer, requested_offset, requested_len, *total,
1063 (int) *buf); */
1064 printed = *buf - buffer;
1065 if (*total + printed <= requested_offset) {
1066 *total += printed;
1067 *buf = buffer;
1069 else {
1070 if (*total < requested_offset) {
1071 *slop = requested_offset - *total;
1073 *total = requested_offset + printed - *slop;
1075 if (*total > requested_offset + requested_len) {
1076 return 1;
1078 else {
1079 return 0;
1084 * This function calculates the actual start of the requested data
1085 * in the buffer. It also calculates actual length of data returned,
1086 * which could be less that the amount of data requested.
1088 static int
1089 calc_start_len(char *buffer, char **start, int requested_offset,
1090 int requested_len, int total, char *buf)
1092 int return_len, buffer_len;
1094 buffer_len = buf - buffer;
1095 if (buffer_len >= 4095) {
1096 printk(KERN_ERR "STRIP: exceeded /proc buffer size\n");
1100 * There may be bytes before and after the
1101 * chunk that was actually requested.
1103 return_len = total - requested_offset;
1104 if (return_len < 0) {
1105 return_len = 0;
1107 *start = buf - return_len;
1108 if (return_len > requested_len) {
1109 return_len = requested_len;
1111 /* printk(KERN_DEBUG "return_len: %d\n", return_len); */
1112 return return_len;
1116 * If the time is in the near future, time_delta prints the number of
1117 * seconds to go into the buffer and returns the address of the buffer.
1118 * If the time is not in the near future, it returns the address of the
1119 * string "Not scheduled" The buffer must be long enough to contain the
1120 * ascii representation of the number plus 9 charactes for the " seconds"
1121 * and the null character.
1123 static char *time_delta(char buffer[], long time)
1125 time -= jiffies;
1126 if (time > LongTime / 2) return("Not scheduled");
1127 if(time < 0) time = 0; /* Don't print negative times */
1128 sprintf(buffer, "%ld seconds", time / HZ);
1129 return(buffer);
1132 static int sprintf_neighbours(char *buffer, MetricomNodeTable *table, char *title)
1134 /* We wrap this in a do/while loop, so if the table changes */
1135 /* while we're reading it, we just go around and try again. */
1136 struct timeval t;
1137 char *ptr;
1140 int i;
1141 t = table->timestamp;
1142 ptr = buffer;
1143 if (table->num_nodes) ptr += sprintf(ptr, "\n %s\n", title);
1144 for (i=0; i<table->num_nodes; i++)
1146 InterruptStatus intstat = DisableInterrupts();
1147 MetricomNode node = table->node[i];
1148 RestoreInterrupts(intstat);
1149 ptr += sprintf(ptr, " %s\n", node.c);
1151 } while (table->timestamp.tv_sec != t.tv_sec || table->timestamp.tv_usec != t.tv_usec);
1152 return ptr - buffer;
1156 * This function prints radio status information into the specified buffer.
1157 * I think the buffer size is 4K, so this routine should never print more
1158 * than 4K of data into it. With the maximum of 32 portables and 32 poletops
1159 * reported, the routine outputs 3107 bytes into the buffer.
1161 static int
1162 sprintf_status_info(char *buffer, struct strip *strip_info)
1164 char temp[32];
1165 char *p = buffer;
1166 MetricomAddressString addr_string;
1168 /* First, we must copy all of our data to a safe place, */
1169 /* in case a serial interrupt comes in and changes it. */
1170 InterruptStatus intstat = DisableInterrupts();
1171 int tx_left = strip_info->tx_left;
1172 unsigned long rx_average_pps = strip_info->rx_average_pps;
1173 unsigned long tx_average_pps = strip_info->tx_average_pps;
1174 unsigned long sx_average_pps = strip_info->sx_average_pps;
1175 int working = strip_info->working;
1176 int firmware_level = strip_info->firmware_level;
1177 long watchdog_doprobe = strip_info->watchdog_doprobe;
1178 long watchdog_doreset = strip_info->watchdog_doreset;
1179 long gratuitous_arp = strip_info->gratuitous_arp;
1180 long arp_interval = strip_info->arp_interval;
1181 FirmwareVersion firmware_version = strip_info->firmware_version;
1182 SerialNumber serial_number = strip_info->serial_number;
1183 BatteryVoltage battery_voltage = strip_info->battery_voltage;
1184 char8 if_name = strip_info->if_name;
1185 MetricomAddress true_dev_addr = strip_info->true_dev_addr;
1186 MetricomAddress dev_dev_addr = *(MetricomAddress*)strip_info->dev.dev_addr;
1187 int manual_dev_addr = strip_info->manual_dev_addr;
1188 #ifdef EXT_COUNTERS
1189 unsigned long rx_bytes = strip_info->rx_bytes;
1190 unsigned long tx_bytes = strip_info->tx_bytes;
1191 unsigned long rx_rbytes = strip_info->rx_rbytes;
1192 unsigned long tx_rbytes = strip_info->tx_rbytes;
1193 unsigned long rx_sbytes = strip_info->rx_sbytes;
1194 unsigned long tx_sbytes = strip_info->tx_sbytes;
1195 unsigned long rx_ebytes = strip_info->rx_ebytes;
1196 unsigned long tx_ebytes = strip_info->tx_ebytes;
1197 #endif
1198 RestoreInterrupts(intstat);
1200 p += sprintf(p, "\nInterface name\t\t%s\n", if_name.c);
1201 p += sprintf(p, " Radio working:\t\t%s\n", working ? "Yes" : "No");
1202 radio_address_to_string(&true_dev_addr, &addr_string);
1203 p += sprintf(p, " Radio address:\t\t%s\n", addr_string.c);
1204 if (manual_dev_addr)
1206 radio_address_to_string(&dev_dev_addr, &addr_string);
1207 p += sprintf(p, " Device address:\t%s\n", addr_string.c);
1209 p += sprintf(p, " Firmware version:\t%s", !working ? "Unknown" :
1210 !firmware_level ? "Should be upgraded" :
1211 firmware_version.c);
1212 if (firmware_level >= ChecksummedMessages) p += sprintf(p, " (Checksums Enabled)");
1213 p += sprintf(p, "\n");
1214 p += sprintf(p, " Serial number:\t\t%s\n", serial_number.c);
1215 p += sprintf(p, " Battery voltage:\t%s\n", battery_voltage.c);
1216 p += sprintf(p, " Transmit queue (bytes):%d\n", tx_left);
1217 p += sprintf(p, " Receive packet rate: %ld packets per second\n", rx_average_pps / 8);
1218 p += sprintf(p, " Transmit packet rate: %ld packets per second\n", tx_average_pps / 8);
1219 p += sprintf(p, " Sent packet rate: %ld packets per second\n", sx_average_pps / 8);
1220 p += sprintf(p, " Next watchdog probe:\t%s\n", time_delta(temp, watchdog_doprobe));
1221 p += sprintf(p, " Next watchdog reset:\t%s\n", time_delta(temp, watchdog_doreset));
1222 p += sprintf(p, " Next gratuitous ARP:\t");
1224 if (!memcmp(strip_info->dev.dev_addr, zero_address.c, sizeof(zero_address)))
1225 p += sprintf(p, "Disabled\n");
1226 else
1228 p += sprintf(p, "%s\n", time_delta(temp, gratuitous_arp));
1229 p += sprintf(p, " Next ARP interval:\t%ld seconds\n", JIFFIE_TO_SEC(arp_interval));
1232 if (working)
1234 #ifdef EXT_COUNTERS
1235 p += sprintf(p, "\n");
1236 p += sprintf(p, " Total bytes: \trx:\t%lu\ttx:\t%lu\n", rx_bytes, tx_bytes);
1237 p += sprintf(p, " thru radio: \trx:\t%lu\ttx:\t%lu\n", rx_rbytes, tx_rbytes);
1238 p += sprintf(p, " thru serial port: \trx:\t%lu\ttx:\t%lu\n", rx_sbytes, tx_sbytes);
1239 p += sprintf(p, " Total stat/err bytes:\trx:\t%lu\ttx:\t%lu\n", rx_ebytes, tx_ebytes);
1240 #endif
1241 p += sprintf_neighbours(p, &strip_info->poletops, "Poletops:");
1242 p += sprintf_neighbours(p, &strip_info->portables, "Portables:");
1245 return p - buffer;
1249 * This function is exports status information from the STRIP driver through
1250 * the /proc file system.
1253 static int get_status_info(char *buffer, char **start, off_t req_offset, int req_len, int dummy)
1255 int total = 0, slop = 0;
1256 struct strip *strip_info = struct_strip_list;
1257 char *buf = buffer;
1259 buf += sprintf(buf, "strip_version: %s\n", StripVersion);
1260 if (shift_buffer(buffer, req_offset, req_len, &total, &slop, &buf)) goto exit;
1262 while (strip_info != NULL)
1264 buf += sprintf_status_info(buf, strip_info);
1265 if (shift_buffer(buffer, req_offset, req_len, &total, &slop, &buf)) break;
1266 strip_info = strip_info->next;
1268 exit:
1269 return(calc_start_len(buffer, start, req_offset, req_len, total, buf));
1272 static const char proc_strip_status_name[] = "strip";
1274 #ifdef CONFIG_PROC_FS
1275 static struct proc_dir_entry proc_strip_get_status_info =
1277 PROC_NET_STRIP_STATUS, /* unsigned short low_ino */
1278 sizeof(proc_strip_status_name)-1, /* unsigned short namelen */
1279 proc_strip_status_name, /* const char *name */
1280 S_IFREG | S_IRUGO, /* mode_t mode */
1281 1, /* nlink_t nlink */
1282 0, 0, 0, /* uid_t uid, gid_t gid, unsigned long size */
1283 &proc_net_inode_operations, /* struct inode_operations * ops */
1284 &get_status_info, /* int (*get_info)(...) */
1285 NULL, /* void (*fill_inode)(struct inode *); */
1286 NULL, NULL, NULL, /* struct proc_dir_entry *next, *parent, *subdir; */
1287 NULL /* void *data; */
1289 #endif /* CONFIG_PROC_FS */
1291 /************************************************************************/
1292 /* Sending routines */
1294 static void ResetRadio(struct strip *strip_info)
1296 struct tty_struct *tty = strip_info->tty;
1297 static const char init[] = "ate0q1dt**starmode\r**";
1298 StringDescriptor s = { init, sizeof(init)-1 };
1301 * If the radio isn't working anymore,
1302 * we should clear the old status information.
1304 if (strip_info->working)
1306 printk(KERN_INFO "%s: No response: Resetting radio.\n", strip_info->dev.name);
1307 strip_info->firmware_version.c[0] = '\0';
1308 strip_info->serial_number.c[0] = '\0';
1309 strip_info->battery_voltage.c[0] = '\0';
1310 strip_info->portables.num_nodes = 0;
1311 do_gettimeofday(&strip_info->portables.timestamp);
1312 strip_info->poletops.num_nodes = 0;
1313 do_gettimeofday(&strip_info->poletops.timestamp);
1316 strip_info->pps_timer = jiffies;
1317 strip_info->rx_pps_count = 0;
1318 strip_info->tx_pps_count = 0;
1319 strip_info->sx_pps_count = 0;
1320 strip_info->rx_average_pps = 0;
1321 strip_info->tx_average_pps = 0;
1322 strip_info->sx_average_pps = 0;
1324 /* Mark radio address as unknown */
1325 *(MetricomAddress*)&strip_info->true_dev_addr = zero_address;
1326 if (!strip_info->manual_dev_addr)
1327 *(MetricomAddress*)strip_info->dev.dev_addr = zero_address;
1328 strip_info->working = FALSE;
1329 strip_info->firmware_level = NoStructure;
1330 strip_info->next_command = CompatibilityCommand;
1331 strip_info->watchdog_doprobe = jiffies + 10 * HZ;
1332 strip_info->watchdog_doreset = jiffies + 1 * HZ;
1334 /* If the user has selected a baud rate above 38.4 see what magic we have to do */
1335 if (strip_info->user_baud > B38400)
1338 * Subtle stuff: Pay attention :-)
1339 * If the serial port is currently at the user's selected (>38.4) rate,
1340 * then we temporarily switch to 19.2 and issue the ATS304 command
1341 * to tell the radio to switch to the user's selected rate.
1342 * If the serial port is not currently at that rate, that means we just
1343 * issued the ATS304 command last time through, so this time we restore
1344 * the user's selected rate and issue the normal starmode reset string.
1346 if (strip_info->user_baud == get_baud(tty))
1348 static const char b0[] = "ate0q1s304=57600\r";
1349 static const char b1[] = "ate0q1s304=115200\r";
1350 static const StringDescriptor baudstring[2] =
1351 { { b0, sizeof(b0)-1 }, { b1, sizeof(b1)-1 } };
1352 set_baud(tty, B19200);
1353 if (strip_info->user_baud == B57600 ) s = baudstring[0];
1354 else if (strip_info->user_baud == B115200) s = baudstring[1];
1355 else s = baudstring[1]; /* For now */
1357 else set_baud(tty, strip_info->user_baud);
1360 tty->driver.write(tty, 0, s.string, s.length);
1361 #ifdef EXT_COUNTERS
1362 strip_info->tx_ebytes += s.length;
1363 #endif
1367 * Called by the driver when there's room for more data. If we have
1368 * more packets to send, we send them here.
1371 static void strip_write_some_more(struct tty_struct *tty)
1373 struct strip *strip_info = (struct strip *) tty->disc_data;
1375 /* First make sure we're connected. */
1376 if (!strip_info || strip_info->magic != STRIP_MAGIC || !strip_info->dev.start)
1377 return;
1379 if (strip_info->tx_left > 0)
1382 * If some data left, send it
1383 * Note: There's a kernel design bug here. The write_wakeup routine has to
1384 * know how many bytes were written in the previous call, but the number of
1385 * bytes written is returned as the result of the tty->driver.write call,
1386 * and there's no guarantee that the tty->driver.write routine will have
1387 * returned before the write_wakeup routine is invoked. If the PC has fast
1388 * Serial DMA hardware, then it's quite possible that the write could complete
1389 * almost instantaneously, meaning that my write_wakeup routine could be
1390 * called immediately, before tty->driver.write has had a chance to return
1391 * the number of bytes that it wrote. In an attempt to guard against this,
1392 * I disable interrupts around the call to tty->driver.write, although even
1393 * this might not work on a symmetric multi-processor system.
1395 InterruptStatus intstat = DisableInterrupts();
1396 int num_written = tty->driver.write(tty, 0, strip_info->tx_head, strip_info->tx_left);
1397 strip_info->tx_left -= num_written;
1398 strip_info->tx_head += num_written;
1399 #ifdef EXT_COUNTERS
1400 strip_info->tx_sbytes += num_written;
1401 #endif
1402 RestoreInterrupts(intstat);
1404 else /* Else start transmission of another packet */
1406 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1407 strip_unlock(strip_info);
1408 mark_bh(NET_BH);
1412 static __u8 *add_checksum(__u8 *buffer, __u8 *end)
1414 __u16 sum = 0;
1415 __u8 *p = buffer;
1416 while (p < end) sum += *p++;
1417 end[3] = hextable[sum & 0xF]; sum >>= 4;
1418 end[2] = hextable[sum & 0xF]; sum >>= 4;
1419 end[1] = hextable[sum & 0xF]; sum >>= 4;
1420 end[0] = hextable[sum & 0xF];
1421 return(end+4);
1424 static unsigned char *strip_make_packet(unsigned char *buffer, struct strip *strip_info, struct sk_buff *skb)
1426 __u8 *ptr = buffer;
1427 __u8 *stuffstate = NULL;
1428 STRIP_Header *header = (STRIP_Header *)skb->data;
1429 MetricomAddress haddr = header->dst_addr;
1430 int len = skb->len - sizeof(STRIP_Header);
1431 MetricomKey key;
1433 /*HexDump("strip_make_packet", strip_info, skb->data, skb->data + skb->len);*/
1435 if (header->protocol == htons(ETH_P_IP)) key = SIP0Key;
1436 else if (header->protocol == htons(ETH_P_ARP)) key = ARP0Key;
1437 else
1439 printk(KERN_ERR "%s: strip_make_packet: Unknown packet type 0x%04X\n",
1440 strip_info->dev.name, ntohs(header->protocol));
1441 return(NULL);
1444 if (len > strip_info->mtu)
1446 printk(KERN_ERR "%s: Dropping oversized transmit packet: %d bytes\n",
1447 strip_info->dev.name, len);
1448 return(NULL);
1452 * If we're sending to ourselves, discard the packet.
1453 * (Metricom radios choke if they try to send a packet to their own address.)
1455 if (!memcmp(haddr.c, strip_info->true_dev_addr.c, sizeof(haddr)))
1457 printk(KERN_ERR "%s: Dropping packet addressed to self\n", strip_info->dev.name);
1458 return(NULL);
1462 * If this is a broadcast packet, send it to our designated Metricom
1463 * 'broadcast hub' radio (First byte of address being 0xFF means broadcast)
1465 if (haddr.c[0] == 0xFF)
1467 struct in_device *in_dev = strip_info->dev.ip_ptr;
1468 /* arp_query returns 1 if it succeeds in looking up the address, 0 if it fails */
1469 if (!arp_query(haddr.c, in_dev->ifa_list->ifa_broadcast, &strip_info->dev))
1471 printk(KERN_ERR "%s: Unable to send packet (no broadcast hub configured)\n",
1472 strip_info->dev.name);
1473 return(NULL);
1476 * If we are the broadcast hub, don't bother sending to ourselves.
1477 * (Metricom radios choke if they try to send a packet to their own address.)
1479 if (!memcmp(haddr.c, strip_info->true_dev_addr.c, sizeof(haddr))) return(NULL);
1482 *ptr++ = 0x0D;
1483 *ptr++ = '*';
1484 *ptr++ = hextable[haddr.c[2] >> 4];
1485 *ptr++ = hextable[haddr.c[2] & 0xF];
1486 *ptr++ = hextable[haddr.c[3] >> 4];
1487 *ptr++ = hextable[haddr.c[3] & 0xF];
1488 *ptr++ = '-';
1489 *ptr++ = hextable[haddr.c[4] >> 4];
1490 *ptr++ = hextable[haddr.c[4] & 0xF];
1491 *ptr++ = hextable[haddr.c[5] >> 4];
1492 *ptr++ = hextable[haddr.c[5] & 0xF];
1493 *ptr++ = '*';
1494 *ptr++ = key.c[0];
1495 *ptr++ = key.c[1];
1496 *ptr++ = key.c[2];
1497 *ptr++ = key.c[3];
1499 ptr = StuffData(skb->data + sizeof(STRIP_Header), len, ptr, &stuffstate);
1501 if (strip_info->firmware_level >= ChecksummedMessages) ptr = add_checksum(buffer+1, ptr);
1503 *ptr++ = 0x0D;
1504 return(ptr);
1507 static void strip_send(struct strip *strip_info, struct sk_buff *skb)
1509 MetricomAddress haddr;
1510 unsigned char *ptr = strip_info->tx_buff;
1511 int doreset = (long)jiffies - strip_info->watchdog_doreset >= 0;
1512 int doprobe = (long)jiffies - strip_info->watchdog_doprobe >= 0 && !doreset;
1513 struct in_device *in_dev = strip_info->dev.ip_ptr;
1516 * 1. If we have a packet, encapsulate it and put it in the buffer
1518 if (skb)
1520 char *newptr = strip_make_packet(ptr, strip_info, skb);
1521 strip_info->tx_pps_count++;
1522 if (!newptr) strip_info->tx_dropped++;
1523 else
1525 ptr = newptr;
1526 strip_info->sx_pps_count++;
1527 strip_info->tx_packets++; /* Count another successful packet */
1528 #ifdef EXT_COUNTERS
1529 strip_info->tx_bytes += skb->len;
1530 strip_info->tx_rbytes += ptr - strip_info->tx_buff;
1531 #endif
1532 /*DumpData("Sending:", strip_info, strip_info->tx_buff, ptr);*/
1533 /*HexDump("Sending", strip_info, strip_info->tx_buff, ptr);*/
1538 * 2. If it is time for another tickle, tack it on, after the packet
1540 if (doprobe)
1542 StringDescriptor ts = CommandString[strip_info->next_command];
1543 #if TICKLE_TIMERS
1545 struct timeval tv;
1546 do_gettimeofday(&tv);
1547 printk(KERN_INFO "**** Sending tickle string %d at %02d.%06d\n",
1548 strip_info->next_command, tv.tv_sec % 100, tv.tv_usec);
1550 #endif
1551 if (ptr == strip_info->tx_buff) *ptr++ = 0x0D;
1553 *ptr++ = '*'; /* First send "**" to provoke an error message */
1554 *ptr++ = '*';
1556 /* Then add the command */
1557 memcpy(ptr, ts.string, ts.length);
1559 /* Add a checksum ? */
1560 if (strip_info->firmware_level < ChecksummedMessages) ptr += ts.length;
1561 else ptr = add_checksum(ptr, ptr + ts.length);
1563 *ptr++ = 0x0D; /* Terminate the command with a <CR> */
1565 /* Cycle to next periodic command? */
1566 if (strip_info->firmware_level >= StructuredMessages)
1567 if (++strip_info->next_command >= ELEMENTS_OF(CommandString))
1568 strip_info->next_command = 0;
1569 #ifdef EXT_COUNTERS
1570 strip_info->tx_ebytes += ts.length;
1571 #endif
1572 strip_info->watchdog_doprobe = jiffies + 10 * HZ;
1573 strip_info->watchdog_doreset = jiffies + 1 * HZ;
1574 /*printk(KERN_INFO "%s: Routine radio test.\n", strip_info->dev.name);*/
1578 * 3. Set up the strip_info ready to send the data (if any).
1580 strip_info->tx_head = strip_info->tx_buff;
1581 strip_info->tx_left = ptr - strip_info->tx_buff;
1582 strip_info->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1585 * 4. Debugging check to make sure we're not overflowing the buffer.
1587 if (strip_info->tx_size - strip_info->tx_left < 20)
1588 printk(KERN_ERR "%s: Sending%5d bytes;%5d bytes free.\n", strip_info->dev.name,
1589 strip_info->tx_left, strip_info->tx_size - strip_info->tx_left);
1592 * 5. If watchdog has expired, reset the radio. Note: if there's data waiting in
1593 * the buffer, strip_write_some_more will send it after the reset has finished
1595 if (doreset) { ResetRadio(strip_info); return; }
1598 * 6. If it is time for a periodic ARP, queue one up to be sent.
1599 * We only do this if:
1600 * 1. The radio is working
1601 * 2. It's time to send another periodic ARP
1602 * 3. We really know what our address is (and it is not manually set to zero)
1603 * 4. We have a designated broadcast address configured
1604 * If we queue up an ARP packet when we don't have a designated broadcast
1605 * address configured, then the packet will just have to be discarded in
1606 * strip_make_packet. This is not fatal, but it causes misleading information
1607 * to be displayed in tcpdump. tcpdump will report that periodic APRs are
1608 * being sent, when in fact they are not, because they are all being dropped
1609 * in the strip_make_packet routine.
1611 if (strip_info->working && (long)jiffies - strip_info->gratuitous_arp >= 0 &&
1612 memcmp(strip_info->dev.dev_addr, zero_address.c, sizeof(zero_address)) &&
1613 arp_query(haddr.c, in_dev->ifa_list->ifa_broadcast, &strip_info->dev))
1615 /*printk(KERN_INFO "%s: Sending gratuitous ARP with interval %ld\n",
1616 strip_info->dev.name, strip_info->arp_interval / HZ);*/
1617 strip_info->gratuitous_arp = jiffies + strip_info->arp_interval;
1618 strip_info->arp_interval *= 2;
1619 if (strip_info->arp_interval > MaxARPInterval)
1620 strip_info->arp_interval = MaxARPInterval;
1621 if (in_dev && in_dev->ifa_list)
1622 arp_send(
1623 ARPOP_REPLY, ETH_P_ARP,
1624 in_dev->ifa_list->ifa_address, /* Target address of ARP packet is our address */
1625 &strip_info->dev, /* Device to send packet on */
1626 in_dev->ifa_list->ifa_address, /* Source IP address this ARP packet comes from */
1627 NULL, /* Destination HW address is NULL (broadcast it) */
1628 strip_info->dev.dev_addr, /* Source HW address is our HW address */
1629 strip_info->dev.dev_addr); /* Target HW address is our HW address (redundant) */
1633 * 7. All ready. Start the transmission
1635 strip_write_some_more(strip_info->tty);
1638 /* Encapsulate a datagram and kick it into a TTY queue. */
1639 static int strip_xmit(struct sk_buff *skb, struct device *dev)
1641 struct strip *strip_info = (struct strip *)(dev->priv);
1643 if (!dev->start)
1645 printk(KERN_ERR "%s: xmit call when iface is down\n", dev->name);
1646 return(1);
1648 if (test_and_set_bit(0, (void *) &strip_info->dev.tbusy)) return(1);
1649 del_timer(&strip_info->idle_timer);
1651 /* See if someone has been ifconfigging */
1652 if (strip_info->mtu != strip_info->dev.mtu)
1653 strip_changedmtu(strip_info);
1655 if (jiffies - strip_info->pps_timer > HZ)
1657 unsigned long t = jiffies - strip_info->pps_timer;
1658 unsigned long rx_pps_count = (strip_info->rx_pps_count * HZ * 8 + t/2) / t;
1659 unsigned long tx_pps_count = (strip_info->tx_pps_count * HZ * 8 + t/2) / t;
1660 unsigned long sx_pps_count = (strip_info->sx_pps_count * HZ * 8 + t/2) / t;
1662 strip_info->pps_timer = jiffies;
1663 strip_info->rx_pps_count = 0;
1664 strip_info->tx_pps_count = 0;
1665 strip_info->sx_pps_count = 0;
1667 strip_info->rx_average_pps = (strip_info->rx_average_pps + rx_pps_count + 1) / 2;
1668 strip_info->tx_average_pps = (strip_info->tx_average_pps + tx_pps_count + 1) / 2;
1669 strip_info->sx_average_pps = (strip_info->sx_average_pps + sx_pps_count + 1) / 2;
1671 if (rx_pps_count / 8 >= 10)
1672 printk(KERN_INFO "%s: WARNING: Receiving %ld packets per second.\n",
1673 strip_info->dev.name, rx_pps_count / 8);
1674 if (tx_pps_count / 8 >= 10)
1675 printk(KERN_INFO "%s: WARNING: Tx %ld packets per second.\n",
1676 strip_info->dev.name, tx_pps_count / 8);
1677 if (sx_pps_count / 8 >= 10)
1678 printk(KERN_INFO "%s: WARNING: Sending %ld packets per second.\n",
1679 strip_info->dev.name, sx_pps_count / 8);
1682 strip_send(strip_info, skb);
1684 if (skb) dev_kfree_skb(skb);
1685 return(0);
1689 * IdleTask periodically calls strip_xmit, so even when we have no IP packets
1690 * to send for an extended period of time, the watchdog processing still gets
1691 * done to ensure that the radio stays in Starmode
1694 static void strip_IdleTask(unsigned long parameter)
1696 strip_xmit(NULL, (struct device *)parameter);
1700 * Create the MAC header for an arbitrary protocol layer
1702 * saddr!=NULL means use this specific address (n/a for Metricom)
1703 * saddr==NULL means use default device source address
1704 * daddr!=NULL means use this destination address
1705 * daddr==NULL means leave destination address alone
1706 * (e.g. unresolved arp -- kernel will call
1707 * rebuild_header later to fill in the address)
1710 static int strip_header(struct sk_buff *skb, struct device *dev,
1711 unsigned short type, void *daddr, void *saddr, unsigned len)
1713 struct strip *strip_info = (struct strip *)(dev->priv);
1714 STRIP_Header *header = (STRIP_Header *)skb_push(skb, sizeof(STRIP_Header));
1716 /*printk(KERN_INFO "%s: strip_header 0x%04X %s\n", dev->name, type,
1717 type == ETH_P_IP ? "IP" : type == ETH_P_ARP ? "ARP" : "");*/
1719 header->src_addr = strip_info->true_dev_addr;
1720 header->protocol = htons(type);
1722 /*HexDump("strip_header", (struct strip *)(dev->priv), skb->data, skb->data + skb->len);*/
1724 if (!daddr) return(-dev->hard_header_len);
1726 header->dst_addr = *(MetricomAddress*)daddr;
1727 return(dev->hard_header_len);
1731 * Rebuild the MAC header. This is called after an ARP
1732 * (or in future other address resolution) has completed on this
1733 * sk_buff. We now let ARP fill in the other fields.
1734 * I think this should return zero if packet is ready to send,
1735 * or non-zero if it needs more time to do an address lookup
1738 static int strip_rebuild_header(struct sk_buff *skb)
1740 #ifdef CONFIG_INET
1741 STRIP_Header *header = (STRIP_Header *) skb->data;
1743 /* Arp find returns zero if if knows the address, */
1744 /* or if it doesn't know the address it sends an ARP packet and returns non-zero */
1745 return arp_find(header->dst_addr.c, skb)? 1 : 0;
1746 #else
1747 return 0;
1748 #endif
1752 /************************************************************************/
1753 /* Receiving routines */
1755 static int strip_receive_room(struct tty_struct *tty)
1757 return 0x10000; /* We can handle an infinite amount of data. :-) */
1761 * This function parses the response to the ATS300? command,
1762 * extracting the radio version and serial number.
1764 static void get_radio_version(struct strip *strip_info, __u8 *ptr, __u8 *end)
1766 __u8 *p, *value_begin, *value_end;
1767 int len;
1769 /* Determine the beginning of the second line of the payload */
1770 p = ptr;
1771 while (p < end && *p != 10) p++;
1772 if (p >= end) return;
1773 p++;
1774 value_begin = p;
1776 /* Determine the end of line */
1777 while (p < end && *p != 10) p++;
1778 if (p >= end) return;
1779 value_end = p;
1780 p++;
1782 len = value_end - value_begin;
1783 len = MIN(len, sizeof(FirmwareVersion) - 1);
1784 if (strip_info->firmware_version.c[0] == 0)
1785 printk(KERN_INFO "%s: Radio Firmware: %.*s\n",
1786 strip_info->dev.name, len, value_begin);
1787 sprintf(strip_info->firmware_version.c, "%.*s", len, value_begin);
1789 /* Look for the first colon */
1790 while (p < end && *p != ':') p++;
1791 if (p >= end) return;
1792 /* Skip over the space */
1793 p += 2;
1794 len = sizeof(SerialNumber) - 1;
1795 if (p + len <= end) {
1796 sprintf(strip_info->serial_number.c, "%.*s", len, p);
1798 else {
1799 printk(KERN_DEBUG "STRIP: radio serial number shorter (%d) than expected (%d)\n",
1800 end - p, len);
1805 * This function parses the response to the ATS325? command,
1806 * extracting the radio battery voltage.
1808 static void get_radio_voltage(struct strip *strip_info, __u8 *ptr, __u8 *end)
1810 int len;
1812 len = sizeof(BatteryVoltage) - 1;
1813 if (ptr + len <= end) {
1814 sprintf(strip_info->battery_voltage.c, "%.*s", len, ptr);
1816 else {
1817 printk(KERN_DEBUG "STRIP: radio voltage string shorter (%d) than expected (%d)\n",
1818 end - ptr, len);
1823 * This function parses the responses to the AT~LA and ATS311 commands,
1824 * which list the radio's neighbours.
1826 static void get_radio_neighbours(MetricomNodeTable *table, __u8 *ptr, __u8 *end)
1828 table->num_nodes = 0;
1829 while (ptr < end && table->num_nodes < NODE_TABLE_SIZE)
1831 MetricomNode *node = &table->node[table->num_nodes++];
1832 char *dst = node->c, *limit = dst + sizeof(*node) - 1;
1833 while (ptr < end && *ptr <= 32) ptr++;
1834 while (ptr < end && dst < limit && *ptr != 10) *dst++ = *ptr++;
1835 *dst++ = 0;
1836 while (ptr < end && ptr[-1] != 10) ptr++;
1838 do_gettimeofday(&table->timestamp);
1841 static int get_radio_address(struct strip *strip_info, __u8 *p)
1843 MetricomAddress addr;
1845 if (string_to_radio_address(&addr, p)) return(1);
1847 /* See if our radio address has changed */
1848 if (memcmp(strip_info->true_dev_addr.c, addr.c, sizeof(addr)))
1850 MetricomAddressString addr_string;
1851 radio_address_to_string(&addr, &addr_string);
1852 printk(KERN_INFO "%s: Radio address = %s\n", strip_info->dev.name, addr_string.c);
1853 strip_info->true_dev_addr = addr;
1854 if (!strip_info->manual_dev_addr) *(MetricomAddress*)strip_info->dev.dev_addr = addr;
1855 /* Give the radio a few seconds to get its head straight, then send an arp */
1856 strip_info->gratuitous_arp = jiffies + 15 * HZ;
1857 strip_info->arp_interval = 1 * HZ;
1859 return(0);
1862 static int verify_checksum(struct strip *strip_info)
1864 __u8 *p = strip_info->sx_buff;
1865 __u8 *end = strip_info->sx_buff + strip_info->sx_count - 4;
1866 u_short sum = (READHEX16(end[0]) << 12) | (READHEX16(end[1]) << 8) |
1867 (READHEX16(end[2]) << 4) | (READHEX16(end[3]));
1868 while (p < end) sum -= *p++;
1869 if (sum == 0 && strip_info->firmware_level == StructuredMessages)
1871 strip_info->firmware_level = ChecksummedMessages;
1872 printk(KERN_INFO "%s: Radio provides message checksums\n", strip_info->dev.name);
1874 return(sum == 0);
1877 static void RecvErr(char *msg, struct strip *strip_info)
1879 __u8 *ptr = strip_info->sx_buff;
1880 __u8 *end = strip_info->sx_buff + strip_info->sx_count;
1881 DumpData(msg, strip_info, ptr, end);
1882 strip_info->rx_errors++;
1885 static void RecvErr_Message(struct strip *strip_info, __u8 *sendername, const __u8 *msg, u_long len)
1887 if (has_prefix(msg, len, "001")) /* Not in StarMode! */
1889 RecvErr("Error Msg:", strip_info);
1890 printk(KERN_INFO "%s: Radio %s is not in StarMode\n",
1891 strip_info->dev.name, sendername);
1894 else if (has_prefix(msg, len, "002")) /* Remap handle */
1896 /* We ignore "Remap handle" messages for now */
1899 else if (has_prefix(msg, len, "003")) /* Can't resolve name */
1901 RecvErr("Error Msg:", strip_info);
1902 printk(KERN_INFO "%s: Destination radio name is unknown\n",
1903 strip_info->dev.name);
1906 else if (has_prefix(msg, len, "004")) /* Name too small or missing */
1908 strip_info->watchdog_doreset = jiffies + LongTime;
1909 #if TICKLE_TIMERS
1911 struct timeval tv;
1912 do_gettimeofday(&tv);
1913 printk(KERN_INFO "**** Got ERR_004 response at %02d.%06d\n",
1914 tv.tv_sec % 100, tv.tv_usec);
1916 #endif
1917 if (!strip_info->working)
1919 strip_info->working = TRUE;
1920 printk(KERN_INFO "%s: Radio now in starmode\n", strip_info->dev.name);
1922 * If the radio has just entered a working state, we should do our first
1923 * probe ASAP, so that we find out our radio address etc. without delay.
1925 strip_info->watchdog_doprobe = jiffies;
1927 if (strip_info->firmware_level == NoStructure && sendername)
1929 strip_info->firmware_level = StructuredMessages;
1930 strip_info->next_command = 0; /* Try to enable checksums ASAP */
1931 printk(KERN_INFO "%s: Radio provides structured messages\n", strip_info->dev.name);
1933 if (strip_info->firmware_level >= StructuredMessages)
1936 * If this message has a valid checksum on the end, then the call to verify_checksum
1937 * will elevate the firmware_level to ChecksummedMessages for us. (The actual return
1938 * code from verify_checksum is ignored here.)
1940 verify_checksum(strip_info);
1942 * If the radio has structured messages but we don't yet have all our information about it,
1943 * we should do probes without delay, until we have gathered all the information
1945 if (!GOT_ALL_RADIO_INFO(strip_info)) strip_info->watchdog_doprobe = jiffies;
1949 else if (has_prefix(msg, len, "005")) /* Bad count specification */
1950 RecvErr("Error Msg:", strip_info);
1952 else if (has_prefix(msg, len, "006")) /* Header too big */
1953 RecvErr("Error Msg:", strip_info);
1955 else if (has_prefix(msg, len, "007")) /* Body too big */
1957 RecvErr("Error Msg:", strip_info);
1958 printk(KERN_ERR "%s: Error! Packet size too big for radio.\n",
1959 strip_info->dev.name);
1962 else if (has_prefix(msg, len, "008")) /* Bad character in name */
1964 RecvErr("Error Msg:", strip_info);
1965 printk(KERN_ERR "%s: Radio name contains illegal character\n",
1966 strip_info->dev.name);
1969 else if (has_prefix(msg, len, "009")) /* No count or line terminator */
1970 RecvErr("Error Msg:", strip_info);
1972 else if (has_prefix(msg, len, "010")) /* Invalid checksum */
1973 RecvErr("Error Msg:", strip_info);
1975 else if (has_prefix(msg, len, "011")) /* Checksum didn't match */
1976 RecvErr("Error Msg:", strip_info);
1978 else if (has_prefix(msg, len, "012")) /* Failed to transmit packet */
1979 RecvErr("Error Msg:", strip_info);
1981 else
1982 RecvErr("Error Msg:", strip_info);
1985 static void process_AT_response(struct strip *strip_info, __u8 *ptr, __u8 *end)
1987 u_long len;
1988 __u8 *p = ptr;
1989 while (p < end && p[-1] != 10) p++; /* Skip past first newline character */
1990 /* Now ptr points to the AT command, and p points to the text of the response. */
1991 len = p-ptr;
1993 #if TICKLE_TIMERS
1995 struct timeval tv;
1996 do_gettimeofday(&tv);
1997 printk(KERN_INFO "**** Got AT response %.7s at %02d.%06d\n",
1998 ptr, tv.tv_sec % 100, tv.tv_usec);
2000 #endif
2002 if (has_prefix(ptr, len, "ATS300?" )) get_radio_version(strip_info, p, end);
2003 else if (has_prefix(ptr, len, "ATS305?" )) get_radio_address(strip_info, p);
2004 else if (has_prefix(ptr, len, "ATS311?" )) get_radio_neighbours(&strip_info->poletops, p, end);
2005 else if (has_prefix(ptr, len, "ATS319=7")) verify_checksum(strip_info);
2006 else if (has_prefix(ptr, len, "ATS325?" )) get_radio_voltage(strip_info, p, end);
2007 else if (has_prefix(ptr, len, "AT~LA" )) get_radio_neighbours(&strip_info->portables, p, end);
2008 else RecvErr("Unknown AT Response:", strip_info);
2011 static void process_ACK(struct strip *strip_info, __u8 *ptr, __u8 *end)
2013 /* Currently we don't do anything with ACKs from the radio */
2016 static void process_Info(struct strip *strip_info, __u8 *ptr, __u8 *end)
2018 if (ptr+16 > end) RecvErr("Bad Info Msg:", strip_info);
2021 static struct device *get_strip_dev(struct strip *strip_info)
2023 /* If our hardware address is *manually set* to zero, and we know our */
2024 /* real radio hardware address, try to find another strip device that has been */
2025 /* manually set to that address that we can 'transfer ownership' of this packet to */
2026 if (strip_info->manual_dev_addr &&
2027 !memcmp(strip_info->dev.dev_addr, zero_address.c, sizeof(zero_address)) &&
2028 memcmp(&strip_info->true_dev_addr, zero_address.c, sizeof(zero_address)))
2030 struct device *dev;
2031 read_lock_bh(&dev_base_lock);
2032 dev = dev_base;
2033 while (dev)
2035 if (dev->type == strip_info->dev.type &&
2036 !memcmp(dev->dev_addr, &strip_info->true_dev_addr, sizeof(MetricomAddress)))
2038 printk(KERN_INFO "%s: Transferred packet ownership to %s.\n",
2039 strip_info->dev.name, dev->name);
2040 read_unlock_bh(&dev_base_lock);
2041 return(dev);
2043 dev = dev->next;
2045 read_unlock_bh(&dev_base_lock);
2047 return(&strip_info->dev);
2051 * Send one completely decapsulated datagram to the next layer.
2054 static void deliver_packet(struct strip *strip_info, STRIP_Header *header, __u16 packetlen)
2056 struct sk_buff *skb = dev_alloc_skb(sizeof(STRIP_Header) + packetlen);
2057 if (!skb)
2059 printk(KERN_ERR "%s: memory squeeze, dropping packet.\n", strip_info->dev.name);
2060 strip_info->rx_dropped++;
2062 else
2064 memcpy(skb_put(skb, sizeof(STRIP_Header)), header, sizeof(STRIP_Header));
2065 memcpy(skb_put(skb, packetlen), strip_info->rx_buff, packetlen);
2066 skb->dev = get_strip_dev(strip_info);
2067 skb->protocol = header->protocol;
2068 skb->mac.raw = skb->data;
2070 /* Having put a fake header on the front of the sk_buff for the */
2071 /* benefit of tools like tcpdump, skb_pull now 'consumes' that */
2072 /* fake header before we hand the packet up to the next layer. */
2073 skb_pull(skb, sizeof(STRIP_Header));
2075 /* Finally, hand the packet up to the next layer (e.g. IP or ARP, etc.) */
2076 strip_info->rx_packets++;
2077 strip_info->rx_pps_count++;
2078 #ifdef EXT_COUNTERS
2079 strip_info->rx_bytes += packetlen;
2080 #endif
2081 netif_rx(skb);
2085 static void process_IP_packet(struct strip *strip_info, STRIP_Header *header, __u8 *ptr, __u8 *end)
2087 __u16 packetlen;
2089 /* Decode start of the IP packet header */
2090 ptr = UnStuffData(ptr, end, strip_info->rx_buff, 4);
2091 if (!ptr)
2093 RecvErr("IP Packet too short", strip_info);
2094 return;
2097 packetlen = ((__u16)strip_info->rx_buff[2] << 8) | strip_info->rx_buff[3];
2099 if (packetlen > MAX_RECV_MTU)
2101 printk(KERN_INFO "%s: Dropping oversized received IP packet: %d bytes\n",
2102 strip_info->dev.name, packetlen);
2103 strip_info->rx_dropped++;
2104 return;
2107 /*printk(KERN_INFO "%s: Got %d byte IP packet\n", strip_info->dev.name, packetlen);*/
2109 /* Decode remainder of the IP packet */
2110 ptr = UnStuffData(ptr, end, strip_info->rx_buff+4, packetlen-4);
2111 if (!ptr)
2113 RecvErr("IP Packet too short", strip_info);
2114 return;
2117 if (ptr < end)
2119 RecvErr("IP Packet too long", strip_info);
2120 return;
2123 header->protocol = htons(ETH_P_IP);
2125 deliver_packet(strip_info, header, packetlen);
2128 static void process_ARP_packet(struct strip *strip_info, STRIP_Header *header, __u8 *ptr, __u8 *end)
2130 __u16 packetlen;
2131 struct arphdr *arphdr = (struct arphdr *)strip_info->rx_buff;
2133 /* Decode start of the ARP packet */
2134 ptr = UnStuffData(ptr, end, strip_info->rx_buff, 8);
2135 if (!ptr)
2137 RecvErr("ARP Packet too short", strip_info);
2138 return;
2141 packetlen = 8 + (arphdr->ar_hln + arphdr->ar_pln) * 2;
2143 if (packetlen > MAX_RECV_MTU)
2145 printk(KERN_INFO "%s: Dropping oversized received ARP packet: %d bytes\n",
2146 strip_info->dev.name, packetlen);
2147 strip_info->rx_dropped++;
2148 return;
2151 /*printk(KERN_INFO "%s: Got %d byte ARP %s\n",
2152 strip_info->dev.name, packetlen,
2153 ntohs(arphdr->ar_op) == ARPOP_REQUEST ? "request" : "reply");*/
2155 /* Decode remainder of the ARP packet */
2156 ptr = UnStuffData(ptr, end, strip_info->rx_buff+8, packetlen-8);
2157 if (!ptr)
2159 RecvErr("ARP Packet too short", strip_info);
2160 return;
2163 if (ptr < end)
2165 RecvErr("ARP Packet too long", strip_info);
2166 return;
2169 header->protocol = htons(ETH_P_ARP);
2171 deliver_packet(strip_info, header, packetlen);
2175 * process_text_message processes a <CR>-terminated block of data received
2176 * from the radio that doesn't begin with a '*' character. All normal
2177 * Starmode communication messages with the radio begin with a '*',
2178 * so any text that does not indicates a serial port error, a radio that
2179 * is in Hayes command mode instead of Starmode, or a radio with really
2180 * old firmware that doesn't frame its Starmode responses properly.
2182 static void process_text_message(struct strip *strip_info)
2184 __u8 *msg = strip_info->sx_buff;
2185 int len = strip_info->sx_count;
2187 /* Check for anything that looks like it might be our radio name */
2188 /* (This is here for backwards compatibility with old firmware) */
2189 if (len == 9 && get_radio_address(strip_info, msg) == 0) return;
2191 if (text_equal(msg, len, "OK" )) return; /* Ignore 'OK' responses from prior commands */
2192 if (text_equal(msg, len, "ERROR" )) return; /* Ignore 'ERROR' messages */
2193 if (has_prefix(msg, len, "ate0q1" )) return; /* Ignore character echo back from the radio */
2195 /* Catch other error messages */
2196 /* (This is here for backwards compatibility with old firmware) */
2197 if (has_prefix(msg, len, "ERR_")) { RecvErr_Message(strip_info, NULL, &msg[4], len-4); return; }
2199 RecvErr("No initial *", strip_info);
2203 * process_message processes a <CR>-terminated block of data received
2204 * from the radio. If the radio is not in Starmode or has old firmware,
2205 * it may be a line of text in response to an AT command. Ideally, with
2206 * a current radio that's properly in Starmode, all data received should
2207 * be properly framed and checksummed radio message blocks, containing
2208 * either a starmode packet, or a other communication from the radio
2209 * firmware, like "INF_" Info messages and &COMMAND responses.
2211 static void process_message(struct strip *strip_info)
2213 STRIP_Header header = { zero_address, zero_address, 0 };
2214 __u8 *ptr = strip_info->sx_buff;
2215 __u8 *end = strip_info->sx_buff + strip_info->sx_count;
2216 __u8 sendername[32], *sptr = sendername;
2217 MetricomKey key;
2219 /*HexDump("Receiving", strip_info, ptr, end);*/
2221 /* Check for start of address marker, and then skip over it */
2222 if (*ptr == '*') ptr++;
2223 else { process_text_message(strip_info); return; }
2225 /* Copy out the return address */
2226 while (ptr < end && *ptr != '*' && sptr < ARRAY_END(sendername)-1) *sptr++ = *ptr++;
2227 *sptr = 0; /* Null terminate the sender name */
2229 /* Check for end of address marker, and skip over it */
2230 if (ptr >= end || *ptr != '*')
2232 RecvErr("No second *", strip_info);
2233 return;
2235 ptr++; /* Skip the second '*' */
2237 /* If the sender name is "&COMMAND", ignore this 'packet' */
2238 /* (This is here for backwards compatibility with old firmware) */
2239 if (!strcmp(sendername, "&COMMAND"))
2241 strip_info->firmware_level = NoStructure;
2242 strip_info->next_command = CompatibilityCommand;
2243 return;
2246 if (ptr+4 > end)
2248 RecvErr("No proto key", strip_info);
2249 return;
2252 /* Get the protocol key out of the buffer */
2253 key.c[0] = *ptr++;
2254 key.c[1] = *ptr++;
2255 key.c[2] = *ptr++;
2256 key.c[3] = *ptr++;
2258 /* If we're using checksums, verify the checksum at the end of the packet */
2259 if (strip_info->firmware_level >= ChecksummedMessages)
2261 end -= 4; /* Chop the last four bytes off the packet (they're the checksum) */
2262 if (ptr > end)
2264 RecvErr("Missing Checksum", strip_info);
2265 return;
2267 if (!verify_checksum(strip_info))
2269 RecvErr("Bad Checksum", strip_info);
2270 return;
2274 /*printk(KERN_INFO "%s: Got packet from \"%s\".\n", strip_info->dev.name, sendername);*/
2277 * Fill in (pseudo) source and destination addresses in the packet.
2278 * We assume that the destination address was our address (the radio does not
2279 * tell us this). If the radio supplies a source address, then we use it.
2281 header.dst_addr = strip_info->true_dev_addr;
2282 string_to_radio_address(&header.src_addr, sendername);
2284 #ifdef EXT_COUNTERS
2285 if (key.l == SIP0Key.l) {
2286 strip_info->rx_rbytes += (end - ptr);
2287 process_IP_packet(strip_info, &header, ptr, end);
2288 } else if (key.l == ARP0Key.l) {
2289 strip_info->rx_rbytes += (end - ptr);
2290 process_ARP_packet(strip_info, &header, ptr, end);
2291 } else if (key.l == ATR_Key.l) {
2292 strip_info->rx_ebytes += (end - ptr);
2293 process_AT_response(strip_info, ptr, end);
2294 } else if (key.l == ACK_Key.l) {
2295 strip_info->rx_ebytes += (end - ptr);
2296 process_ACK(strip_info, ptr, end);
2297 } else if (key.l == INF_Key.l) {
2298 strip_info->rx_ebytes += (end - ptr);
2299 process_Info(strip_info, ptr, end);
2300 } else if (key.l == ERR_Key.l) {
2301 strip_info->rx_ebytes += (end - ptr);
2302 RecvErr_Message(strip_info, sendername, ptr, end-ptr);
2303 } else RecvErr("Unrecognized protocol key", strip_info);
2304 #else
2305 if (key.l == SIP0Key.l) process_IP_packet (strip_info, &header, ptr, end);
2306 else if (key.l == ARP0Key.l) process_ARP_packet (strip_info, &header, ptr, end);
2307 else if (key.l == ATR_Key.l) process_AT_response(strip_info, ptr, end);
2308 else if (key.l == ACK_Key.l) process_ACK (strip_info, ptr, end);
2309 else if (key.l == INF_Key.l) process_Info (strip_info, ptr, end);
2310 else if (key.l == ERR_Key.l) RecvErr_Message (strip_info, sendername, ptr, end-ptr);
2311 else RecvErr("Unrecognized protocol key", strip_info);
2312 #endif
2315 #define TTYERROR(X) ((X) == TTY_BREAK ? "Break" : \
2316 (X) == TTY_FRAME ? "Framing Error" : \
2317 (X) == TTY_PARITY ? "Parity Error" : \
2318 (X) == TTY_OVERRUN ? "Hardware Overrun" : "Unknown Error")
2321 * Handle the 'receiver data ready' interrupt.
2322 * This function is called by the 'tty_io' module in the kernel when
2323 * a block of STRIP data has been received, which can now be decapsulated
2324 * and sent on to some IP layer for further processing.
2327 static void
2328 strip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
2330 struct strip *strip_info = (struct strip *) tty->disc_data;
2331 const unsigned char *end = cp + count;
2333 if (!strip_info || strip_info->magic != STRIP_MAGIC || !strip_info->dev.start)
2334 return;
2336 /* Argh! mtu change time! - costs us the packet part received at the change */
2337 if (strip_info->mtu != strip_info->dev.mtu)
2338 strip_changedmtu(strip_info);
2340 #if 0
2342 struct timeval tv;
2343 do_gettimeofday(&tv);
2344 printk(KERN_INFO "**** strip_receive_buf: %3d bytes at %02d.%06d\n",
2345 count, tv.tv_sec % 100, tv.tv_usec);
2347 #endif
2349 #ifdef EXT_COUNTERS
2350 strip_info->rx_sbytes += count;
2351 #endif
2353 /* Read the characters out of the buffer */
2354 while (cp < end)
2356 if (fp && *fp) printk(KERN_INFO "%s: %s on serial port\n", strip_info->dev.name, TTYERROR(*fp));
2357 if (fp && *fp++ && !strip_info->discard) /* If there's a serial error, record it */
2359 /* If we have some characters in the buffer, discard them */
2360 strip_info->discard = strip_info->sx_count;
2361 strip_info->rx_errors++;
2364 /* Leading control characters (CR, NL, Tab, etc.) are ignored */
2365 if (strip_info->sx_count > 0 || *cp >= ' ')
2367 if (*cp == 0x0D) /* If end of packet, decide what to do with it */
2369 if (strip_info->sx_count > 3000)
2370 printk(KERN_INFO "%s: Cut a %d byte packet (%d bytes remaining)%s\n",
2371 strip_info->dev.name, strip_info->sx_count, end-cp-1,
2372 strip_info->discard ? " (discarded)" : "");
2373 if (strip_info->sx_count > strip_info->sx_size)
2375 strip_info->rx_over_errors++;
2376 printk(KERN_INFO "%s: sx_buff overflow (%d bytes total)\n",
2377 strip_info->dev.name, strip_info->sx_count);
2379 else if (strip_info->discard)
2380 printk(KERN_INFO "%s: Discarding bad packet (%d/%d)\n",
2381 strip_info->dev.name, strip_info->discard, strip_info->sx_count);
2382 else process_message(strip_info);
2383 strip_info->discard = 0;
2384 strip_info->sx_count = 0;
2386 else
2388 /* Make sure we have space in the buffer */
2389 if (strip_info->sx_count < strip_info->sx_size)
2390 strip_info->sx_buff[strip_info->sx_count] = *cp;
2391 strip_info->sx_count++;
2394 cp++;
2399 /************************************************************************/
2400 /* General control routines */
2402 static int set_mac_address(struct strip *strip_info, MetricomAddress *addr)
2405 * We're using a manually specified address if the address is set
2406 * to anything other than all ones. Setting the address to all ones
2407 * disables manual mode and goes back to automatic address determination
2408 * (tracking the true address that the radio has).
2410 strip_info->manual_dev_addr = memcmp(addr->c, broadcast_address.c, sizeof(broadcast_address));
2411 if (strip_info->manual_dev_addr)
2412 *(MetricomAddress*)strip_info->dev.dev_addr = *addr;
2413 else *(MetricomAddress*)strip_info->dev.dev_addr = strip_info->true_dev_addr;
2414 return 0;
2417 static int dev_set_mac_address(struct device *dev, void *addr)
2419 struct strip *strip_info = (struct strip *)(dev->priv);
2420 struct sockaddr *sa = addr;
2421 printk(KERN_INFO "%s: strip_set_dev_mac_address called\n", dev->name);
2422 set_mac_address(strip_info, (MetricomAddress *)sa->sa_data);
2423 return 0;
2426 static struct enet_statistics *strip_get_stats(struct device *dev)
2428 static struct enet_statistics stats;
2429 struct strip *strip_info = (struct strip *)(dev->priv);
2431 memset(&stats, 0, sizeof(struct enet_statistics));
2433 stats.rx_packets = strip_info->rx_packets;
2434 stats.tx_packets = strip_info->tx_packets;
2435 stats.rx_dropped = strip_info->rx_dropped;
2436 stats.tx_dropped = strip_info->tx_dropped;
2437 stats.tx_errors = strip_info->tx_errors;
2438 stats.rx_errors = strip_info->rx_errors;
2439 stats.rx_over_errors = strip_info->rx_over_errors;
2440 return(&stats);
2444 /************************************************************************/
2445 /* Opening and closing */
2448 * Here's the order things happen:
2449 * When the user runs "slattach -p strip ..."
2450 * 1. The TTY module calls strip_open
2451 * 2. strip_open calls strip_alloc
2452 * 3. strip_alloc calls register_netdev
2453 * 4. register_netdev calls strip_dev_init
2454 * 5. then strip_open finishes setting up the strip_info
2456 * When the user runs "ifconfig st<x> up address netmask ..."
2457 * 6. strip_open_low gets called
2459 * When the user runs "ifconfig st<x> down"
2460 * 7. strip_close_low gets called
2462 * When the user kills the slattach process
2463 * 8. strip_close gets called
2464 * 9. strip_close calls dev_close
2465 * 10. if the device is still up, then dev_close calls strip_close_low
2466 * 11. strip_close calls strip_free
2469 /* Open the low-level part of the STRIP channel. Easy! */
2471 static int strip_open_low(struct device *dev)
2473 struct strip *strip_info = (struct strip *)(dev->priv);
2474 struct in_device *in_dev = dev->ip_ptr;
2476 if (strip_info->tty == NULL)
2477 return(-ENODEV);
2479 if (!allocate_buffers(strip_info))
2480 return(-ENOMEM);
2482 strip_info->sx_count = 0;
2483 strip_info->tx_left = 0;
2485 strip_info->discard = 0;
2486 strip_info->working = FALSE;
2487 strip_info->firmware_level = NoStructure;
2488 strip_info->next_command = CompatibilityCommand;
2489 strip_info->user_baud = get_baud(strip_info->tty);
2492 * Needed because address '0' is special
2495 if (in_dev->ifa_list->ifa_address == 0)
2496 in_dev->ifa_list->ifa_address = ntohl(0xC0A80001);
2497 dev->tbusy = 0;
2498 dev->start = 1;
2500 printk(KERN_INFO "%s: Initializing Radio.\n", strip_info->dev.name);
2501 ResetRadio(strip_info);
2502 strip_info->idle_timer.expires = jiffies + 1*HZ;
2503 add_timer(&strip_info->idle_timer);
2504 return(0);
2509 * Close the low-level part of the STRIP channel. Easy!
2512 static int strip_close_low(struct device *dev)
2514 struct strip *strip_info = (struct strip *)(dev->priv);
2516 if (strip_info->tty == NULL)
2517 return -EBUSY;
2518 strip_info->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
2519 dev->tbusy = 1;
2520 dev->start = 0;
2523 * Free all STRIP frame buffers.
2525 if (strip_info->rx_buff)
2527 kfree(strip_info->rx_buff);
2528 strip_info->rx_buff = NULL;
2530 if (strip_info->sx_buff)
2532 kfree(strip_info->sx_buff);
2533 strip_info->sx_buff = NULL;
2535 if (strip_info->tx_buff)
2537 kfree(strip_info->tx_buff);
2538 strip_info->tx_buff = NULL;
2540 del_timer(&strip_info->idle_timer);
2541 return 0;
2545 * This routine is called by DDI when the
2546 * (dynamically assigned) device is registered
2549 static int strip_dev_init(struct device *dev)
2552 * Finish setting up the DEVICE info.
2555 dev->trans_start = 0;
2556 dev->last_rx = 0;
2557 dev->tx_queue_len = 30; /* Drop after 30 frames queued */
2559 dev->flags = 0;
2560 dev->mtu = DEFAULT_STRIP_MTU;
2561 dev->type = ARPHRD_METRICOM; /* dtang */
2562 dev->hard_header_len = sizeof(STRIP_Header);
2564 * dev->priv Already holds a pointer to our struct strip
2567 *(MetricomAddress*)&dev->broadcast = broadcast_address;
2568 dev->dev_addr[0] = 0;
2569 dev->addr_len = sizeof(MetricomAddress);
2572 * Pointers to interface service routines.
2575 dev->open = strip_open_low;
2576 dev->stop = strip_close_low;
2577 dev->hard_start_xmit = strip_xmit;
2578 dev->hard_header = strip_header;
2579 dev->rebuild_header = strip_rebuild_header;
2580 /* dev->type_trans unused */
2581 /* dev->set_multicast_list unused */
2582 dev->set_mac_address = dev_set_mac_address;
2583 /* dev->do_ioctl unused */
2584 /* dev->set_config unused */
2585 dev->get_stats = strip_get_stats;
2586 return 0;
2590 * Free a STRIP channel.
2593 static void strip_free(struct strip *strip_info)
2595 *(strip_info->referrer) = strip_info->next;
2596 if (strip_info->next)
2597 strip_info->next->referrer = strip_info->referrer;
2598 strip_info->magic = 0;
2599 kfree(strip_info);
2603 * Allocate a new free STRIP channel
2606 static struct strip *strip_alloc(void)
2608 int channel_id = 0;
2609 struct strip **s = &struct_strip_list;
2610 struct strip *strip_info = (struct strip *)
2611 kmalloc(sizeof(struct strip), GFP_KERNEL);
2613 if (!strip_info)
2614 return(NULL); /* If no more memory, return */
2617 * Clear the allocated memory
2620 memset(strip_info, 0, sizeof(struct strip));
2623 * Search the list to find where to put our new entry
2624 * (and in the process decide what channel number it is
2625 * going to be)
2628 while (*s && (*s)->dev.base_addr == channel_id)
2630 channel_id++;
2631 s = &(*s)->next;
2635 * Fill in the link pointers
2638 strip_info->next = *s;
2639 if (*s)
2640 (*s)->referrer = &strip_info->next;
2641 strip_info->referrer = s;
2642 *s = strip_info;
2644 strip_info->magic = STRIP_MAGIC;
2645 strip_info->tty = NULL;
2647 strip_info->gratuitous_arp = jiffies + LongTime;
2648 strip_info->arp_interval = 0;
2649 init_timer(&strip_info->idle_timer);
2650 strip_info->idle_timer.data = (long)&strip_info->dev;
2651 strip_info->idle_timer.function = strip_IdleTask;
2653 /* Note: strip_info->if_name is currently 8 characters long */
2654 sprintf(strip_info->if_name.c, "st%d", channel_id);
2655 strip_info->dev.name = strip_info->if_name.c;
2656 strip_info->dev.base_addr = channel_id;
2657 strip_info->dev.priv = (void*)strip_info;
2658 strip_info->dev.next = NULL;
2659 strip_info->dev.init = strip_dev_init;
2661 return(strip_info);
2665 * Open the high-level part of the STRIP channel.
2666 * This function is called by the TTY module when the
2667 * STRIP line discipline is called for. Because we are
2668 * sure the tty line exists, we only have to link it to
2669 * a free STRIP channel...
2672 static int strip_open(struct tty_struct *tty)
2674 struct strip *strip_info = (struct strip *) tty->disc_data;
2677 * First make sure we're not already connected.
2680 if (strip_info && strip_info->magic == STRIP_MAGIC)
2681 return -EEXIST;
2684 * OK. Find a free STRIP channel to use.
2686 if ((strip_info = strip_alloc()) == NULL)
2687 return -ENFILE;
2690 * Register our newly created device so it can be ifconfig'd
2691 * strip_dev_init() will be called as a side-effect
2694 if (register_netdev(&strip_info->dev) != 0)
2696 printk(KERN_ERR "strip: register_netdev() failed.\n");
2697 strip_free(strip_info);
2698 return -ENFILE;
2701 strip_info->tty = tty;
2702 tty->disc_data = strip_info;
2703 if (tty->driver.flush_buffer)
2704 tty->driver.flush_buffer(tty);
2705 if (tty->ldisc.flush_buffer)
2706 tty->ldisc.flush_buffer(tty);
2709 * Restore default settings
2712 strip_info->dev.type = ARPHRD_METRICOM; /* dtang */
2715 * Set tty options
2718 tty->termios->c_iflag |= IGNBRK |IGNPAR;/* Ignore breaks and parity errors. */
2719 tty->termios->c_cflag |= CLOCAL; /* Ignore modem control signals. */
2720 tty->termios->c_cflag &= ~HUPCL; /* Don't close on hup */
2722 #ifdef MODULE
2723 MOD_INC_USE_COUNT;
2724 #endif
2726 printk(KERN_INFO "STRIP: device \"%s\" activated\n", strip_info->if_name.c);
2729 * Done. We have linked the TTY line to a channel.
2731 return(strip_info->dev.base_addr);
2735 * Close down a STRIP channel.
2736 * This means flushing out any pending queues, and then restoring the
2737 * TTY line discipline to what it was before it got hooked to STRIP
2738 * (which usually is TTY again).
2741 static void strip_close(struct tty_struct *tty)
2743 struct strip *strip_info = (struct strip *) tty->disc_data;
2746 * First make sure we're connected.
2749 if (!strip_info || strip_info->magic != STRIP_MAGIC)
2750 return;
2752 dev_close(&strip_info->dev);
2753 unregister_netdev(&strip_info->dev);
2755 tty->disc_data = 0;
2756 strip_info->tty = NULL;
2757 printk(KERN_INFO "STRIP: device \"%s\" closed down\n", strip_info->if_name.c);
2758 strip_free(strip_info);
2759 tty->disc_data = NULL;
2760 #ifdef MODULE
2761 MOD_DEC_USE_COUNT;
2762 #endif
2766 /************************************************************************/
2767 /* Perform I/O control calls on an active STRIP channel. */
2769 static int strip_ioctl(struct tty_struct *tty, struct file *file,
2770 unsigned int cmd, unsigned long arg)
2772 struct strip *strip_info = (struct strip *) tty->disc_data;
2775 * First make sure we're connected.
2778 if (!strip_info || strip_info->magic != STRIP_MAGIC)
2779 return -EINVAL;
2781 switch(cmd)
2783 case SIOCGIFNAME:
2784 return copy_to_user((void*)arg, strip_info->dev.name,
2785 strlen(strip_info->dev.name) + 1) ?
2786 -EFAULT : 0;
2787 break;
2788 case SIOCSIFHWADDR:
2790 MetricomAddress addr;
2791 printk(KERN_INFO "%s: SIOCSIFHWADDR\n", strip_info->dev.name);
2792 return copy_from_user(&addr, (void*)arg, sizeof(MetricomAddress)) ?
2793 -EFAULT : set_mac_address(strip_info, &addr);
2794 break;
2797 * Allow stty to read, but not set, the serial port
2800 case TCGETS:
2801 case TCGETA:
2802 return n_tty_ioctl(tty, (struct file *) file, cmd,
2803 (unsigned long) arg);
2804 break;
2805 default:
2806 return -ENOIOCTLCMD;
2807 break;
2812 /************************************************************************/
2813 /* Initialization */
2816 * Initialize the STRIP driver.
2817 * This routine is called at boot time, to bootstrap the multi-channel
2818 * STRIP driver
2821 #ifdef MODULE
2822 static
2823 #endif
2824 int strip_init_ctrl_dev(struct device *dummy)
2826 static struct tty_ldisc strip_ldisc;
2827 int status;
2829 printk(KERN_INFO "STRIP: Version %s (unlimited channels)\n", StripVersion);
2832 * Fill in our line protocol discipline, and register it
2835 memset(&strip_ldisc, 0, sizeof(strip_ldisc));
2836 strip_ldisc.magic = TTY_LDISC_MAGIC;
2837 strip_ldisc.flags = 0;
2838 strip_ldisc.open = strip_open;
2839 strip_ldisc.close = strip_close;
2840 strip_ldisc.read = NULL;
2841 strip_ldisc.write = NULL;
2842 strip_ldisc.ioctl = strip_ioctl;
2843 strip_ldisc.poll = NULL;
2844 strip_ldisc.receive_buf = strip_receive_buf;
2845 strip_ldisc.receive_room = strip_receive_room;
2846 strip_ldisc.write_wakeup = strip_write_some_more;
2847 status = tty_register_ldisc(N_STRIP, &strip_ldisc);
2848 if (status != 0)
2850 printk(KERN_ERR "STRIP: can't register line discipline (err = %d)\n", status);
2854 * Register the status file with /proc
2856 #ifdef CONFIG_PROC_FS
2857 if (proc_net_register(&proc_strip_get_status_info) != 0)
2859 printk(KERN_ERR "strip: status proc_net_register() failed.\n");
2861 #endif
2863 #ifdef MODULE
2864 return status;
2865 #else
2867 /* Return "not found", so that dev_init() will unlink
2868 * the placeholder device entry for us.
2870 return ENODEV;
2871 #endif
2875 /************************************************************************/
2876 /* From here down is only used when compiled as an external module */
2878 #ifdef MODULE
2880 int init_module(void)
2882 return strip_init_ctrl_dev(0);
2885 void cleanup_module(void)
2887 int i;
2888 while (struct_strip_list)
2889 strip_free(struct_strip_list);
2891 /* Unregister with the /proc/net file here. */
2892 #ifdef CONFIG_PROC_FS
2893 proc_net_unregister(PROC_NET_STRIP_STATUS);
2894 #endif
2896 if ((i = tty_register_ldisc(N_STRIP, NULL)))
2897 printk(KERN_ERR "STRIP: can't unregister line discipline (err = %d)\n", i);
2899 printk(KERN_INFO "STRIP: Module Unloaded\n");
2901 #endif /* MODULE */