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
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.
72 static const char StripVersion[] = "1.3A-STUART.CHESHIRE-MODULAR";
74 static const char StripVersion[] = "1.3A-STUART.CHESHIRE";
77 #define TICKLE_TIMERS 0
78 #define EXT_COUNTERS 1
81 /************************************************************************/
84 #include <linux/config.h>
85 #include <linux/kernel.h>
86 #include <linux/module.h>
87 #include <linux/init.h>
88 #include <asm/system.h>
89 #include <asm/uaccess.h>
90 #include <asm/bitops.h>
92 # include <linux/ctype.h>
93 #include <linux/string.h>
95 #include <linux/interrupt.h>
97 #include <linux/tty.h>
98 #include <linux/errno.h>
99 #include <linux/netdevice.h>
100 #include <linux/inetdevice.h>
101 #include <linux/etherdevice.h>
102 #include <linux/skbuff.h>
103 #include <linux/if_arp.h>
104 #include <linux/if_strip.h>
105 #include <linux/proc_fs.h>
106 #include <linux/seq_file.h>
107 #include <linux/serial.h>
108 #include <linux/serialP.h>
111 #include <linux/ip.h>
112 #include <linux/tcp.h>
113 #include <linux/time.h>
116 /************************************************************************/
117 /* Useful structures and definitions */
120 * A MetricomKey identifies the protocol being carried inside a Metricom
130 * An IP address can be viewed as four bytes in memory (which is what it is) or as
131 * a single 32-bit long (which is convenient for assignment, equality testing etc.)
140 * A MetricomAddressString is used to hold a printable representation of
141 * a Metricom address.
146 } MetricomAddressString;
148 /* Encapsulation can expand packet of size x to 65/64x + 1
149 * Sent packet looks like "<CR>*<address>*<key><encaps payload><CR>"
151 * eg. <CR>*0000-1234*SIP0<encaps payload><CR>
152 * We allow 31 bytes for the stars, the key, the address and the <CR>s
154 #define STRIP_ENCAP_SIZE(X) (32 + (X)*65L/64L)
157 * A STRIP_Header is never really sent over the radio, but making a dummy
158 * header for internal use within the kernel that looks like an Ethernet
159 * header makes certain other software happier. For example, tcpdump
160 * already understands Ethernet headers.
164 MetricomAddress dst_addr; /* Destination address, e.g. "0000-1234" */
165 MetricomAddress src_addr; /* Source address, e.g. "0000-5678" */
166 unsigned short protocol; /* The protocol type, using Ethernet codes */
173 #define NODE_TABLE_SIZE 32
175 struct timeval timestamp;
177 MetricomNode node[NODE_TABLE_SIZE];
180 enum { FALSE = 0, TRUE = 1 };
183 * Holds the radio's firmware version.
190 * Holds the radio's serial number.
197 * Holds the radio's battery voltage.
208 NoStructure = 0, /* Really old firmware */
209 StructuredMessages = 1, /* Parsable AT response msgs */
210 ChecksummedMessages = 2 /* Parsable AT response msgs with checksums */
216 * These are pointers to the malloc()ed frame buffers.
219 unsigned char *rx_buff; /* buffer for received IP packet */
220 unsigned char *sx_buff; /* buffer for received serial data */
221 int sx_count; /* received serial data counter */
222 int sx_size; /* Serial buffer size */
223 unsigned char *tx_buff; /* transmitter buffer */
224 unsigned char *tx_head; /* pointer to next byte to XMIT */
225 int tx_left; /* bytes left in XMIT queue */
226 int tx_size; /* Serial buffer size */
229 * STRIP interface statistics.
232 unsigned long rx_packets; /* inbound frames counter */
233 unsigned long tx_packets; /* outbound frames counter */
234 unsigned long rx_errors; /* Parity, etc. errors */
235 unsigned long tx_errors; /* Planned stuff */
236 unsigned long rx_dropped; /* No memory for skb */
237 unsigned long tx_dropped; /* When MTU change */
238 unsigned long rx_over_errors; /* Frame bigger then STRIP buf. */
240 unsigned long pps_timer; /* Timer to determine pps */
241 unsigned long rx_pps_count; /* Counter to determine pps */
242 unsigned long tx_pps_count; /* Counter to determine pps */
243 unsigned long sx_pps_count; /* Counter to determine pps */
244 unsigned long rx_average_pps; /* rx packets per second * 8 */
245 unsigned long tx_average_pps; /* tx packets per second * 8 */
246 unsigned long sx_average_pps; /* sent packets per second * 8 */
249 unsigned long rx_bytes; /* total received bytes */
250 unsigned long tx_bytes; /* total received bytes */
251 unsigned long rx_rbytes; /* bytes thru radio i/f */
252 unsigned long tx_rbytes; /* bytes thru radio i/f */
253 unsigned long rx_sbytes; /* tot bytes thru serial i/f */
254 unsigned long tx_sbytes; /* tot bytes thru serial i/f */
255 unsigned long rx_ebytes; /* tot stat/err bytes */
256 unsigned long tx_ebytes; /* tot stat/err bytes */
260 * Internal variables.
263 struct list_head list; /* Linked list of devices */
265 int discard; /* Set if serial error */
266 int working; /* Is radio working correctly? */
267 int firmware_level; /* Message structuring level */
268 int next_command; /* Next periodic command */
269 unsigned int user_baud; /* The user-selected baud rate */
270 int mtu; /* Our mtu (to spot changes!) */
271 long watchdog_doprobe; /* Next time to test the radio */
272 long watchdog_doreset; /* Time to do next reset */
273 long gratuitous_arp; /* Time to send next ARP refresh */
274 long arp_interval; /* Next ARP interval */
275 struct timer_list idle_timer; /* For periodic wakeup calls */
276 MetricomAddress true_dev_addr; /* True address of radio */
277 int manual_dev_addr; /* Hack: See note below */
279 FirmwareVersion firmware_version; /* The radio's firmware version */
280 SerialNumber serial_number; /* The radio's serial number */
281 BatteryVoltage battery_voltage; /* The radio's battery voltage */
284 * Other useful structures.
287 struct tty_struct *tty; /* ptr to TTY structure */
288 struct net_device *dev; /* Our device structure */
291 * Neighbour radio records
294 MetricomNodeTable portables;
295 MetricomNodeTable poletops;
299 * Note: manual_dev_addr hack
301 * It is not possible to change the hardware address of a Metricom radio,
302 * or to send packets with a user-specified hardware source address, thus
303 * trying to manually set a hardware source address is a questionable
304 * thing to do. However, if the user *does* manually set the hardware
305 * source address of a STRIP interface, then the kernel will believe it,
306 * and use it in certain places. For example, the hardware address listed
307 * by ifconfig will be the manual address, not the true one.
308 * (Both addresses are listed in /proc/net/strip.)
309 * Also, ARP packets will be sent out giving the user-specified address as
310 * the source address, not the real address. This is dangerous, because
311 * it means you won't receive any replies -- the ARP replies will go to
312 * the specified address, which will be some other radio. The case where
313 * this is useful is when that other radio is also connected to the same
314 * machine. This allows you to connect a pair of radios to one machine,
315 * and to use one exclusively for inbound traffic, and the other
316 * exclusively for outbound traffic. Pretty neat, huh?
318 * Here's the full procedure to set this up:
320 * 1. "slattach" two interfaces, e.g. st0 for outgoing packets,
321 * and st1 for incoming packets
323 * 2. "ifconfig" st0 (outbound radio) to have the hardware address
324 * which is the real hardware address of st1 (inbound radio).
325 * Now when it sends out packets, it will masquerade as st1, and
326 * replies will be sent to that radio, which is exactly what we want.
328 * 3. Set the route table entry ("route add default ..." or
329 * "route add -net ...", as appropriate) to send packets via the st0
330 * interface (outbound radio). Do not add any route which sends packets
331 * out via the st1 interface -- that radio is for inbound traffic only.
333 * 4. "ifconfig" st1 (inbound radio) to have hardware address zero.
334 * This tells the STRIP driver to "shut down" that interface and not
335 * send any packets through it. In particular, it stops sending the
336 * periodic gratuitous ARP packets that a STRIP interface normally sends.
337 * Also, when packets arrive on that interface, it will search the
338 * interface list to see if there is another interface who's manual
339 * hardware address matches its own real address (i.e. st0 in this
340 * example) and if so it will transfer ownership of the skbuff to
341 * that interface, so that it looks to the kernel as if the packet
342 * arrived on that interface. This is necessary because when the
343 * kernel sends an ARP packet on st0, it expects to get a reply on
344 * st0, and if it sees the reply come from st1 then it will ignore
345 * it (to be accurate, it puts the entry in the ARP table, but
346 * labelled in such a way that st0 can't use it).
348 * Thanks to Petros Maniatis for coming up with the idea of splitting
349 * inbound and outbound traffic between two interfaces, which turned
350 * out to be really easy to implement, even if it is a bit of a hack.
352 * Having set a manual address on an interface, you can restore it
353 * to automatic operation (where the address is automatically kept
354 * consistent with the real address of the radio) by setting a manual
355 * address of all ones, e.g. "ifconfig st0 hw strip FFFFFFFFFFFF"
356 * This 'turns off' manual override mode for the device address.
358 * Note: The IEEE 802 headers reported in tcpdump will show the *real*
359 * radio addresses the packets were sent and received from, so that you
360 * can see what is really going on with packets, and which interfaces
361 * they are really going through.
365 /************************************************************************/
369 * CommandString1 works on all radios
370 * Other CommandStrings are only used with firmware that provides structured responses.
372 * ats319=1 Enables Info message for node additions and deletions
373 * ats319=2 Enables Info message for a new best node
374 * ats319=4 Enables checksums
375 * ats319=8 Enables ACK messages
378 static const int MaxCommandStringLength = 32;
379 static const int CompatibilityCommand = 1;
381 static const char CommandString0[] = "*&COMMAND*ATS319=7"; /* Turn on checksums & info messages */
382 static const char CommandString1[] = "*&COMMAND*ATS305?"; /* Query radio name */
383 static const char CommandString2[] = "*&COMMAND*ATS325?"; /* Query battery voltage */
384 static const char CommandString3[] = "*&COMMAND*ATS300?"; /* Query version information */
385 static const char CommandString4[] = "*&COMMAND*ATS311?"; /* Query poletop list */
386 static const char CommandString5[] = "*&COMMAND*AT~LA"; /* Query portables list */
392 static const StringDescriptor CommandString[] = {
393 {CommandString0, sizeof(CommandString0) - 1},
394 {CommandString1, sizeof(CommandString1) - 1},
395 {CommandString2, sizeof(CommandString2) - 1},
396 {CommandString3, sizeof(CommandString3) - 1},
397 {CommandString4, sizeof(CommandString4) - 1},
398 {CommandString5, sizeof(CommandString5) - 1}
401 #define GOT_ALL_RADIO_INFO(S) \
402 ((S)->firmware_version.c[0] && \
403 (S)->battery_voltage.c[0] && \
404 memcmp(&(S)->true_dev_addr, zero_address.c, sizeof(zero_address)))
406 static const char hextable[16] = "0123456789ABCDEF";
408 static const MetricomAddress zero_address;
409 static const MetricomAddress broadcast_address =
410 { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} };
412 static const MetricomKey SIP0Key = { {"SIP0"} };
413 static const MetricomKey ARP0Key = { {"ARP0"} };
414 static const MetricomKey ATR_Key = { {"ATR "} };
415 static const MetricomKey ACK_Key = { {"ACK_"} };
416 static const MetricomKey INF_Key = { {"INF_"} };
417 static const MetricomKey ERR_Key = { {"ERR_"} };
419 static const long MaxARPInterval = 60 * HZ; /* One minute */
422 * Maximum Starmode packet length is 1183 bytes. Allowing 4 bytes for
423 * protocol key, 4 bytes for checksum, one byte for CR, and 65/64 expansion
424 * for STRIP encoding, that translates to a maximum payload MTU of 1155.
425 * Note: A standard NFS 1K data packet is a total of 0x480 (1152) bytes
426 * long, including IP header, UDP header, and NFS header. Setting the STRIP
427 * MTU to 1152 allows us to send default sized NFS packets without fragmentation.
429 static const unsigned short MAX_SEND_MTU = 1152;
430 static const unsigned short MAX_RECV_MTU = 1500; /* Hoping for Ethernet sized packets in the future! */
431 static const unsigned short DEFAULT_STRIP_MTU = 1152;
432 static const int STRIP_MAGIC = 0x5303;
433 static const long LongTime = 0x7FFFFFFF;
435 /************************************************************************/
436 /* Global variables */
438 static LIST_HEAD(strip_list);
439 static spinlock_t strip_lock = SPIN_LOCK_UNLOCKED;
441 /************************************************************************/
444 /* Returns TRUE if text T begins with prefix P */
445 #define has_prefix(T,L,P) (((L) >= sizeof(P)-1) && !strncmp((T), (P), sizeof(P)-1))
447 /* Returns TRUE if text T of length L is equal to string S */
448 #define text_equal(T,L,S) (((L) == sizeof(S)-1) && !strncmp((T), (S), sizeof(S)-1))
450 #define READHEX(X) ((X)>='0' && (X)<='9' ? (X)-'0' : \
451 (X)>='a' && (X)<='f' ? (X)-'a'+10 : \
452 (X)>='A' && (X)<='F' ? (X)-'A'+10 : 0 )
454 #define READHEX16(X) ((__u16)(READHEX(X)))
456 #define READDEC(X) ((X)>='0' && (X)<='9' ? (X)-'0' : 0)
458 #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
460 #define JIFFIE_TO_SEC(X) ((X) / HZ)
463 /************************************************************************/
464 /* Utility routines */
466 static int arp_query(unsigned char *haddr, u32 paddr,
467 struct net_device *dev)
469 struct neighbour *neighbor_entry;
471 neighbor_entry = neigh_lookup(&arp_tbl, &paddr, dev);
473 if (neighbor_entry != NULL) {
474 neighbor_entry->used = jiffies;
475 if (neighbor_entry->nud_state & NUD_VALID) {
476 memcpy(haddr, neighbor_entry->ha, dev->addr_len);
483 static void DumpData(char *msg, struct strip *strip_info, __u8 * ptr,
486 static const int MAX_DumpData = 80;
487 __u8 pkt_text[MAX_DumpData], *p = pkt_text;
491 while (ptr < end && p < &pkt_text[MAX_DumpData - 4]) {
496 if (*ptr >= 32 && *ptr <= 126) {
499 sprintf(p, "\\%02X", *ptr);
510 printk(KERN_INFO "%s: %-13s%s\n", strip_info->dev->name, msg, pkt_text);
514 /************************************************************************/
515 /* Byte stuffing/unstuffing routines */
518 * 00 Unused (reserved character)
519 * 01-3F Run of 2-64 different characters
520 * 40-7F Run of 1-64 different characters plus a single zero at the end
521 * 80-BF Run of 1-64 of the same character
522 * C0-FF Run of 1-64 zeroes (ASCII 0)
527 Stuff_DiffZero = 0x40,
530 Stuff_NoCode = 0xFF, /* Special code, meaning no code selected */
532 Stuff_CodeMask = 0xC0,
533 Stuff_CountMask = 0x3F,
534 Stuff_MaxCount = 0x3F,
535 Stuff_Magic = 0x0D /* The value we are eliminating */
538 /* StuffData encodes the data starting at "src" for "length" bytes.
539 * It writes it to the buffer pointed to by "dst" (which must be at least
540 * as long as 1 + 65/64 of the input length). The output may be up to 1.6%
541 * larger than the input for pathological input, but will usually be smaller.
542 * StuffData returns the new value of the dst pointer as its result.
543 * "code_ptr_ptr" points to a "__u8 *" which is used to hold encoding state
544 * between calls, allowing an encoded packet to be incrementally built up
545 * from small parts. On the first call, the "__u8 *" pointed to should be
546 * initialized to NULL; between subsequent calls the calling routine should
547 * leave the value alone and simply pass it back unchanged so that the
548 * encoder can recover its current state.
551 #define StuffData_FinishBlock(X) \
552 (*code_ptr = (X) ^ Stuff_Magic, code = Stuff_NoCode)
554 static __u8 *StuffData(__u8 * src, __u32 length, __u8 * dst,
555 __u8 ** code_ptr_ptr)
557 __u8 *end = src + length;
558 __u8 *code_ptr = *code_ptr_ptr;
559 __u8 code = Stuff_NoCode, count = 0;
566 * Recover state from last call, if applicable
568 code = (*code_ptr ^ Stuff_Magic) & Stuff_CodeMask;
569 count = (*code_ptr ^ Stuff_Magic) & Stuff_CountMask;
574 /* Stuff_NoCode: If no current code, select one */
576 /* Record where we're going to put this code */
578 count = 0; /* Reset the count (zero means one instance) */
579 /* Tentatively start a new block */
585 *dst++ = *src++ ^ Stuff_Magic;
587 /* Note: We optimistically assume run of same -- */
588 /* which will be fixed later in Stuff_Same */
589 /* if it turns out not to be true. */
592 /* Stuff_Zero: We already have at least one zero encoded */
594 /* If another zero, count it, else finish this code block */
599 StuffData_FinishBlock(Stuff_Zero + count);
603 /* Stuff_Same: We already have at least one byte encoded */
605 /* If another one the same, count it */
606 if ((*src ^ Stuff_Magic) == code_ptr[1]) {
611 /* else, this byte does not match this block. */
612 /* If we already have two or more bytes encoded, finish this code block */
614 StuffData_FinishBlock(Stuff_Same + count);
617 /* else, we only have one so far, so switch to Stuff_Diff code */
619 /* and fall through to Stuff_Diff case below
620 * Note cunning cleverness here: case Stuff_Diff compares
621 * the current character with the previous two to see if it
622 * has a run of three the same. Won't this be an error if
623 * there aren't two previous characters stored to compare with?
624 * No. Because we know the current character is *not* the same
625 * as the previous one, the first test below will necessarily
626 * fail and the send half of the "if" won't be executed.
629 /* Stuff_Diff: We have at least two *different* bytes encoded */
631 /* If this is a zero, must encode a Stuff_DiffZero, and begin a new block */
633 StuffData_FinishBlock(Stuff_DiffZero +
636 /* else, if we have three in a row, it is worth starting a Stuff_Same block */
637 else if ((*src ^ Stuff_Magic) == dst[-1]
638 && dst[-1] == dst[-2]) {
639 /* Back off the last two characters we encoded */
641 /* Note: "Stuff_Diff + 0" is an illegal code */
642 if (code == Stuff_Diff + 0) {
643 code = Stuff_Same + 0;
645 StuffData_FinishBlock(code);
647 /* dst[-1] already holds the correct value */
648 count = 2; /* 2 means three bytes encoded */
651 /* else, another different byte, so add it to the block */
653 *dst++ = *src ^ Stuff_Magic;
656 src++; /* Consume the byte */
659 if (count == Stuff_MaxCount) {
660 StuffData_FinishBlock(code + count);
663 if (code == Stuff_NoCode) {
664 *code_ptr_ptr = NULL;
666 *code_ptr_ptr = code_ptr;
667 StuffData_FinishBlock(code + count);
673 * UnStuffData decodes the data at "src", up to (but not including) "end".
674 * It writes the decoded data into the buffer pointed to by "dst", up to a
675 * maximum of "dst_length", and returns the new value of "src" so that a
676 * follow-on call can read more data, continuing from where the first left off.
678 * There are three types of results:
679 * 1. The source data runs out before extracting "dst_length" bytes:
680 * UnStuffData returns NULL to indicate failure.
681 * 2. The source data produces exactly "dst_length" bytes:
682 * UnStuffData returns new_src = end to indicate that all bytes were consumed.
683 * 3. "dst_length" bytes are extracted, with more remaining.
684 * UnStuffData returns new_src < end to indicate that there are more bytes
687 * Note: The decoding may be destructive, in that it may alter the source
688 * data in the process of decoding it (this is necessary to allow a follow-on
689 * call to resume correctly).
692 static __u8 *UnStuffData(__u8 * src, __u8 * end, __u8 * dst,
695 __u8 *dst_end = dst + dst_length;
697 if (!src || !end || !dst || !dst_length)
699 while (src < end && dst < dst_end) {
700 int count = (*src ^ Stuff_Magic) & Stuff_CountMask;
701 switch ((*src ^ Stuff_Magic) & Stuff_CodeMask) {
703 if (src + 1 + count >= end)
706 *dst++ = *++src ^ Stuff_Magic;
708 while (--count >= 0 && dst < dst_end);
713 *src = Stuff_Same ^ Stuff_Magic;
717 count) ^ Stuff_Magic;
721 if (src + 1 + count >= end)
724 *dst++ = *++src ^ Stuff_Magic;
726 while (--count >= 0 && dst < dst_end);
728 *src = Stuff_Zero ^ Stuff_Magic;
731 (Stuff_DiffZero + count) ^ Stuff_Magic;
737 *dst++ = src[1] ^ Stuff_Magic;
739 while (--count >= 0 && dst < dst_end);
743 *src = (Stuff_Same + count) ^ Stuff_Magic;
749 while (--count >= 0 && dst < dst_end);
753 *src = (Stuff_Zero + count) ^ Stuff_Magic;
764 /************************************************************************/
765 /* General routines for STRIP */
768 * get_baud returns the current baud rate, as one of the constants defined in
770 * If the user has issued a baud rate override using the 'setserial' command
771 * and the logical current rate is set to 38.4, then the true baud rate
772 * currently in effect (57.6 or 115.2) is returned.
774 static unsigned int get_baud(struct tty_struct *tty)
776 if (!tty || !tty->termios)
778 if ((tty->termios->c_cflag & CBAUD) == B38400 && tty->driver_data) {
779 struct async_struct *info =
780 (struct async_struct *) tty->driver_data;
781 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
783 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
786 return (tty->termios->c_cflag & CBAUD);
790 * set_baud sets the baud rate to the rate defined by baudcode
791 * Note: The rate B38400 should be avoided, because the user may have
792 * issued a 'setserial' speed override to map that to a different speed.
793 * We could achieve a true rate of 38400 if we needed to by cancelling
794 * any user speed override that is in place, but that might annoy the
795 * user, so it is simplest to just avoid using 38400.
797 static void set_baud(struct tty_struct *tty, unsigned int baudcode)
799 struct termios old_termios = *(tty->termios);
800 tty->termios->c_cflag &= ~CBAUD; /* Clear the old baud setting */
801 tty->termios->c_cflag |= baudcode; /* Set the new baud setting */
802 tty->driver->set_termios(tty, &old_termios);
806 * Convert a string to a Metricom Address.
809 #define IS_RADIO_ADDRESS(p) ( \
810 isdigit((p)[0]) && isdigit((p)[1]) && isdigit((p)[2]) && isdigit((p)[3]) && \
812 isdigit((p)[5]) && isdigit((p)[6]) && isdigit((p)[7]) && isdigit((p)[8]) )
814 static int string_to_radio_address(MetricomAddress * addr, __u8 * p)
816 if (!IS_RADIO_ADDRESS(p))
820 addr->c[2] = READHEX(p[0]) << 4 | READHEX(p[1]);
821 addr->c[3] = READHEX(p[2]) << 4 | READHEX(p[3]);
822 addr->c[4] = READHEX(p[5]) << 4 | READHEX(p[6]);
823 addr->c[5] = READHEX(p[7]) << 4 | READHEX(p[8]);
828 * Convert a Metricom Address to a string.
831 static __u8 *radio_address_to_string(const MetricomAddress * addr,
832 MetricomAddressString * p)
834 sprintf(p->c, "%02X%02X-%02X%02X", addr->c[2], addr->c[3],
835 addr->c[4], addr->c[5]);
840 * Note: Must make sure sx_size is big enough to receive a stuffed
841 * MAX_RECV_MTU packet. Additionally, we also want to ensure that it's
842 * big enough to receive a large radio neighbour list (currently 4K).
845 static int allocate_buffers(struct strip *strip_info, int mtu)
847 struct net_device *dev = strip_info->dev;
848 int sx_size = max_t(int, STRIP_ENCAP_SIZE(MAX_RECV_MTU), 4096);
849 int tx_size = STRIP_ENCAP_SIZE(mtu) + MaxCommandStringLength;
850 __u8 *r = kmalloc(MAX_RECV_MTU, GFP_ATOMIC);
851 __u8 *s = kmalloc(sx_size, GFP_ATOMIC);
852 __u8 *t = kmalloc(tx_size, GFP_ATOMIC);
854 strip_info->rx_buff = r;
855 strip_info->sx_buff = s;
856 strip_info->tx_buff = t;
857 strip_info->sx_size = sx_size;
858 strip_info->tx_size = tx_size;
859 strip_info->mtu = dev->mtu = mtu;
872 * MTU has been changed by the IP layer.
874 * an upcall from the tty driver, or in an ip packet queue.
876 static int strip_change_mtu(struct net_device *dev, int new_mtu)
878 struct strip *strip_info = dev->priv;
879 int old_mtu = strip_info->mtu;
880 unsigned char *orbuff = strip_info->rx_buff;
881 unsigned char *osbuff = strip_info->sx_buff;
882 unsigned char *otbuff = strip_info->tx_buff;
884 if (new_mtu > MAX_SEND_MTU) {
886 "%s: MTU exceeds maximum allowable (%d), MTU change cancelled.\n",
887 strip_info->dev->name, MAX_SEND_MTU);
891 spin_lock_bh(&strip_lock);
892 if (!allocate_buffers(strip_info, new_mtu)) {
893 printk(KERN_ERR "%s: unable to grow strip buffers, MTU change cancelled.\n",
894 strip_info->dev->name);
895 spin_unlock_bh(&strip_lock);
899 if (strip_info->sx_count) {
900 if (strip_info->sx_count <= strip_info->sx_size)
901 memcpy(strip_info->sx_buff, osbuff,
902 strip_info->sx_count);
904 strip_info->discard = strip_info->sx_count;
905 strip_info->rx_over_errors++;
909 if (strip_info->tx_left) {
910 if (strip_info->tx_left <= strip_info->tx_size)
911 memcpy(strip_info->tx_buff, strip_info->tx_head,
912 strip_info->tx_left);
914 strip_info->tx_left = 0;
915 strip_info->tx_dropped++;
918 strip_info->tx_head = strip_info->tx_buff;
919 spin_unlock_bh(&strip_lock);
921 printk(KERN_NOTICE "%s: strip MTU changed fom %d to %d.\n",
922 strip_info->dev->name, old_mtu, strip_info->mtu);
934 static void strip_unlock(struct strip *strip_info)
937 * Set the timer to go off in one second.
939 strip_info->idle_timer.expires = jiffies + 1 * HZ;
940 add_timer(&strip_info->idle_timer);
941 netif_wake_queue(strip_info->dev);
947 * If the time is in the near future, time_delta prints the number of
948 * seconds to go into the buffer and returns the address of the buffer.
949 * If the time is not in the near future, it returns the address of the
950 * string "Not scheduled" The buffer must be long enough to contain the
951 * ascii representation of the number plus 9 charactes for the " seconds"
952 * and the null character.
954 #ifdef CONFIG_PROC_FS
955 static char *time_delta(char buffer[], long time)
958 if (time > LongTime / 2)
959 return ("Not scheduled");
961 time = 0; /* Don't print negative times */
962 sprintf(buffer, "%ld seconds", time / HZ);
966 /* get Nth element of the linked list */
967 static struct strip *strip_get_idx(loff_t pos)
972 list_for_each_rcu(l, &strip_list) {
974 return list_entry(l, struct strip, list);
980 static void *strip_seq_start(struct seq_file *seq, loff_t *pos)
983 return *pos ? strip_get_idx(*pos - 1) : SEQ_START_TOKEN;
986 static void *strip_seq_next(struct seq_file *seq, void *v, loff_t *pos)
992 if (v == SEQ_START_TOKEN)
993 return strip_get_idx(1);
997 list_for_each_continue_rcu(l, &strip_list) {
998 return list_entry(l, struct strip, list);
1003 static void strip_seq_stop(struct seq_file *seq, void *v)
1008 static void strip_seq_neighbours(struct seq_file *seq,
1009 const MetricomNodeTable * table,
1012 /* We wrap this in a do/while loop, so if the table changes */
1013 /* while we're reading it, we just go around and try again. */
1018 t = table->timestamp;
1019 if (table->num_nodes)
1020 seq_printf(seq, "\n %s\n", title);
1021 for (i = 0; i < table->num_nodes; i++) {
1024 spin_lock_bh(&strip_lock);
1025 node = table->node[i];
1026 spin_unlock_bh(&strip_lock);
1027 seq_printf(seq, " %s\n", node.c);
1029 } while (table->timestamp.tv_sec != t.tv_sec
1030 || table->timestamp.tv_usec != t.tv_usec);
1034 * This function prints radio status information via the seq_file
1035 * interface. The interface takes care of buffer size and over
1038 * The buffer in seq_file is PAGESIZE (4K)
1039 * so this routine should never print more or it will get truncated.
1040 * With the maximum of 32 portables and 32 poletops
1041 * reported, the routine outputs 3107 bytes into the buffer.
1043 static void strip_seq_status_info(struct seq_file *seq,
1044 const struct strip *strip_info)
1047 MetricomAddressString addr_string;
1049 /* First, we must copy all of our data to a safe place, */
1050 /* in case a serial interrupt comes in and changes it. */
1051 int tx_left = strip_info->tx_left;
1052 unsigned long rx_average_pps = strip_info->rx_average_pps;
1053 unsigned long tx_average_pps = strip_info->tx_average_pps;
1054 unsigned long sx_average_pps = strip_info->sx_average_pps;
1055 int working = strip_info->working;
1056 int firmware_level = strip_info->firmware_level;
1057 long watchdog_doprobe = strip_info->watchdog_doprobe;
1058 long watchdog_doreset = strip_info->watchdog_doreset;
1059 long gratuitous_arp = strip_info->gratuitous_arp;
1060 long arp_interval = strip_info->arp_interval;
1061 FirmwareVersion firmware_version = strip_info->firmware_version;
1062 SerialNumber serial_number = strip_info->serial_number;
1063 BatteryVoltage battery_voltage = strip_info->battery_voltage;
1064 char *if_name = strip_info->dev->name;
1065 MetricomAddress true_dev_addr = strip_info->true_dev_addr;
1066 MetricomAddress dev_dev_addr =
1067 *(MetricomAddress *) strip_info->dev->dev_addr;
1068 int manual_dev_addr = strip_info->manual_dev_addr;
1070 unsigned long rx_bytes = strip_info->rx_bytes;
1071 unsigned long tx_bytes = strip_info->tx_bytes;
1072 unsigned long rx_rbytes = strip_info->rx_rbytes;
1073 unsigned long tx_rbytes = strip_info->tx_rbytes;
1074 unsigned long rx_sbytes = strip_info->rx_sbytes;
1075 unsigned long tx_sbytes = strip_info->tx_sbytes;
1076 unsigned long rx_ebytes = strip_info->rx_ebytes;
1077 unsigned long tx_ebytes = strip_info->tx_ebytes;
1080 seq_printf(seq, "\nInterface name\t\t%s\n", if_name);
1081 seq_printf(seq, " Radio working:\t\t%s\n", working ? "Yes" : "No");
1082 radio_address_to_string(&true_dev_addr, &addr_string);
1083 seq_printf(seq, " Radio address:\t\t%s\n", addr_string.c);
1084 if (manual_dev_addr) {
1085 radio_address_to_string(&dev_dev_addr, &addr_string);
1086 seq_printf(seq, " Device address:\t%s\n", addr_string.c);
1088 seq_printf(seq, " Firmware version:\t%s", !working ? "Unknown" :
1089 !firmware_level ? "Should be upgraded" :
1090 firmware_version.c);
1091 if (firmware_level >= ChecksummedMessages)
1092 seq_printf(seq, " (Checksums Enabled)");
1093 seq_printf(seq, "\n");
1094 seq_printf(seq, " Serial number:\t\t%s\n", serial_number.c);
1095 seq_printf(seq, " Battery voltage:\t%s\n", battery_voltage.c);
1096 seq_printf(seq, " Transmit queue (bytes):%d\n", tx_left);
1097 seq_printf(seq, " Receive packet rate: %ld packets per second\n",
1098 rx_average_pps / 8);
1099 seq_printf(seq, " Transmit packet rate: %ld packets per second\n",
1100 tx_average_pps / 8);
1101 seq_printf(seq, " Sent packet rate: %ld packets per second\n",
1102 sx_average_pps / 8);
1103 seq_printf(seq, " Next watchdog probe:\t%s\n",
1104 time_delta(temp, watchdog_doprobe));
1105 seq_printf(seq, " Next watchdog reset:\t%s\n",
1106 time_delta(temp, watchdog_doreset));
1107 seq_printf(seq, " Next gratuitous ARP:\t");
1110 (strip_info->dev->dev_addr, zero_address.c,
1111 sizeof(zero_address)))
1112 seq_printf(seq, "Disabled\n");
1114 seq_printf(seq, "%s\n", time_delta(temp, gratuitous_arp));
1115 seq_printf(seq, " Next ARP interval:\t%ld seconds\n",
1116 JIFFIE_TO_SEC(arp_interval));
1121 seq_printf(seq, "\n");
1123 " Total bytes: \trx:\t%lu\ttx:\t%lu\n",
1124 rx_bytes, tx_bytes);
1126 " thru radio: \trx:\t%lu\ttx:\t%lu\n",
1127 rx_rbytes, tx_rbytes);
1129 " thru serial port: \trx:\t%lu\ttx:\t%lu\n",
1130 rx_sbytes, tx_sbytes);
1132 " Total stat/err bytes:\trx:\t%lu\ttx:\t%lu\n",
1133 rx_ebytes, tx_ebytes);
1135 strip_seq_neighbours(seq, &strip_info->poletops,
1137 strip_seq_neighbours(seq, &strip_info->portables,
1143 * This function is exports status information from the STRIP driver through
1144 * the /proc file system.
1146 static int strip_seq_show(struct seq_file *seq, void *v)
1148 if (v == SEQ_START_TOKEN)
1149 seq_printf(seq, "strip_version: %s\n", StripVersion);
1151 strip_seq_status_info(seq, (const struct strip *)v);
1156 static struct seq_operations strip_seq_ops = {
1157 .start = strip_seq_start,
1158 .next = strip_seq_next,
1159 .stop = strip_seq_stop,
1160 .show = strip_seq_show,
1163 static int strip_seq_open(struct inode *inode, struct file *file)
1165 return seq_open(file, &strip_seq_ops);
1168 static struct file_operations strip_seq_fops = {
1169 .owner = THIS_MODULE,
1170 .open = strip_seq_open,
1172 .llseek = seq_lseek,
1173 .release = seq_release,
1179 /************************************************************************/
1180 /* Sending routines */
1182 static void ResetRadio(struct strip *strip_info)
1184 struct tty_struct *tty = strip_info->tty;
1185 static const char init[] = "ate0q1dt**starmode\r**";
1186 StringDescriptor s = { init, sizeof(init) - 1 };
1189 * If the radio isn't working anymore,
1190 * we should clear the old status information.
1192 if (strip_info->working) {
1193 printk(KERN_INFO "%s: No response: Resetting radio.\n",
1194 strip_info->dev->name);
1195 strip_info->firmware_version.c[0] = '\0';
1196 strip_info->serial_number.c[0] = '\0';
1197 strip_info->battery_voltage.c[0] = '\0';
1198 strip_info->portables.num_nodes = 0;
1199 do_gettimeofday(&strip_info->portables.timestamp);
1200 strip_info->poletops.num_nodes = 0;
1201 do_gettimeofday(&strip_info->poletops.timestamp);
1204 strip_info->pps_timer = jiffies;
1205 strip_info->rx_pps_count = 0;
1206 strip_info->tx_pps_count = 0;
1207 strip_info->sx_pps_count = 0;
1208 strip_info->rx_average_pps = 0;
1209 strip_info->tx_average_pps = 0;
1210 strip_info->sx_average_pps = 0;
1212 /* Mark radio address as unknown */
1213 *(MetricomAddress *) & strip_info->true_dev_addr = zero_address;
1214 if (!strip_info->manual_dev_addr)
1215 *(MetricomAddress *) strip_info->dev->dev_addr =
1217 strip_info->working = FALSE;
1218 strip_info->firmware_level = NoStructure;
1219 strip_info->next_command = CompatibilityCommand;
1220 strip_info->watchdog_doprobe = jiffies + 10 * HZ;
1221 strip_info->watchdog_doreset = jiffies + 1 * HZ;
1223 /* If the user has selected a baud rate above 38.4 see what magic we have to do */
1224 if (strip_info->user_baud > B38400) {
1226 * Subtle stuff: Pay attention :-)
1227 * If the serial port is currently at the user's selected (>38.4) rate,
1228 * then we temporarily switch to 19.2 and issue the ATS304 command
1229 * to tell the radio to switch to the user's selected rate.
1230 * If the serial port is not currently at that rate, that means we just
1231 * issued the ATS304 command last time through, so this time we restore
1232 * the user's selected rate and issue the normal starmode reset string.
1234 if (strip_info->user_baud == get_baud(tty)) {
1235 static const char b0[] = "ate0q1s304=57600\r";
1236 static const char b1[] = "ate0q1s304=115200\r";
1237 static const StringDescriptor baudstring[2] =
1238 { {b0, sizeof(b0) - 1}
1239 , {b1, sizeof(b1) - 1}
1241 set_baud(tty, B19200);
1242 if (strip_info->user_baud == B57600)
1244 else if (strip_info->user_baud == B115200)
1247 s = baudstring[1]; /* For now */
1249 set_baud(tty, strip_info->user_baud);
1252 tty->driver->write(tty, 0, s.string, s.length);
1254 strip_info->tx_ebytes += s.length;
1259 * Called by the driver when there's room for more data. If we have
1260 * more packets to send, we send them here.
1263 static void strip_write_some_more(struct tty_struct *tty)
1265 struct strip *strip_info = (struct strip *) tty->disc_data;
1267 /* First make sure we're connected. */
1268 if (!strip_info || strip_info->magic != STRIP_MAGIC ||
1269 !netif_running(strip_info->dev))
1272 if (strip_info->tx_left > 0) {
1274 tty->driver->write(tty, 0, strip_info->tx_head,
1275 strip_info->tx_left);
1276 strip_info->tx_left -= num_written;
1277 strip_info->tx_head += num_written;
1279 strip_info->tx_sbytes += num_written;
1281 } else { /* Else start transmission of another packet */
1283 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1284 strip_unlock(strip_info);
1288 static __u8 *add_checksum(__u8 * buffer, __u8 * end)
1294 end[3] = hextable[sum & 0xF];
1296 end[2] = hextable[sum & 0xF];
1298 end[1] = hextable[sum & 0xF];
1300 end[0] = hextable[sum & 0xF];
1304 static unsigned char *strip_make_packet(unsigned char *buffer,
1305 struct strip *strip_info,
1306 struct sk_buff *skb)
1309 __u8 *stuffstate = NULL;
1310 STRIP_Header *header = (STRIP_Header *) skb->data;
1311 MetricomAddress haddr = header->dst_addr;
1312 int len = skb->len - sizeof(STRIP_Header);
1315 /*HexDump("strip_make_packet", strip_info, skb->data, skb->data + skb->len); */
1317 if (header->protocol == htons(ETH_P_IP))
1319 else if (header->protocol == htons(ETH_P_ARP))
1323 "%s: strip_make_packet: Unknown packet type 0x%04X\n",
1324 strip_info->dev->name, ntohs(header->protocol));
1328 if (len > strip_info->mtu) {
1330 "%s: Dropping oversized transmit packet: %d bytes\n",
1331 strip_info->dev->name, len);
1336 * If we're sending to ourselves, discard the packet.
1337 * (Metricom radios choke if they try to send a packet to their own address.)
1339 if (!memcmp(haddr.c, strip_info->true_dev_addr.c, sizeof(haddr))) {
1340 printk(KERN_ERR "%s: Dropping packet addressed to self\n",
1341 strip_info->dev->name);
1346 * If this is a broadcast packet, send it to our designated Metricom
1347 * 'broadcast hub' radio (First byte of address being 0xFF means broadcast)
1349 if (haddr.c[0] == 0xFF) {
1351 struct in_device *in_dev = in_dev_get(strip_info->dev);
1354 read_lock(&in_dev->lock);
1355 if (in_dev->ifa_list)
1356 brd = in_dev->ifa_list->ifa_broadcast;
1357 read_unlock(&in_dev->lock);
1360 /* arp_query returns 1 if it succeeds in looking up the address, 0 if it fails */
1361 if (!arp_query(haddr.c, brd, strip_info->dev)) {
1363 "%s: Unable to send packet (no broadcast hub configured)\n",
1364 strip_info->dev->name);
1368 * If we are the broadcast hub, don't bother sending to ourselves.
1369 * (Metricom radios choke if they try to send a packet to their own address.)
1372 (haddr.c, strip_info->true_dev_addr.c, sizeof(haddr)))
1378 *ptr++ = hextable[haddr.c[2] >> 4];
1379 *ptr++ = hextable[haddr.c[2] & 0xF];
1380 *ptr++ = hextable[haddr.c[3] >> 4];
1381 *ptr++ = hextable[haddr.c[3] & 0xF];
1383 *ptr++ = hextable[haddr.c[4] >> 4];
1384 *ptr++ = hextable[haddr.c[4] & 0xF];
1385 *ptr++ = hextable[haddr.c[5] >> 4];
1386 *ptr++ = hextable[haddr.c[5] & 0xF];
1394 StuffData(skb->data + sizeof(STRIP_Header), len, ptr,
1397 if (strip_info->firmware_level >= ChecksummedMessages)
1398 ptr = add_checksum(buffer + 1, ptr);
1404 static void strip_send(struct strip *strip_info, struct sk_buff *skb)
1406 MetricomAddress haddr;
1407 unsigned char *ptr = strip_info->tx_buff;
1408 int doreset = (long) jiffies - strip_info->watchdog_doreset >= 0;
1409 int doprobe = (long) jiffies - strip_info->watchdog_doprobe >= 0
1414 * 1. If we have a packet, encapsulate it and put it in the buffer
1417 char *newptr = strip_make_packet(ptr, strip_info, skb);
1418 strip_info->tx_pps_count++;
1420 strip_info->tx_dropped++;
1423 strip_info->sx_pps_count++;
1424 strip_info->tx_packets++; /* Count another successful packet */
1426 strip_info->tx_bytes += skb->len;
1427 strip_info->tx_rbytes += ptr - strip_info->tx_buff;
1429 /*DumpData("Sending:", strip_info, strip_info->tx_buff, ptr); */
1430 /*HexDump("Sending", strip_info, strip_info->tx_buff, ptr); */
1435 * 2. If it is time for another tickle, tack it on, after the packet
1438 StringDescriptor ts = CommandString[strip_info->next_command];
1442 do_gettimeofday(&tv);
1443 printk(KERN_INFO "**** Sending tickle string %d at %02d.%06d\n",
1444 strip_info->next_command, tv.tv_sec % 100,
1448 if (ptr == strip_info->tx_buff)
1451 *ptr++ = '*'; /* First send "**" to provoke an error message */
1454 /* Then add the command */
1455 memcpy(ptr, ts.string, ts.length);
1457 /* Add a checksum ? */
1458 if (strip_info->firmware_level < ChecksummedMessages)
1461 ptr = add_checksum(ptr, ptr + ts.length);
1463 *ptr++ = 0x0D; /* Terminate the command with a <CR> */
1465 /* Cycle to next periodic command? */
1466 if (strip_info->firmware_level >= StructuredMessages)
1467 if (++strip_info->next_command >=
1468 ARRAY_SIZE(CommandString))
1469 strip_info->next_command = 0;
1471 strip_info->tx_ebytes += ts.length;
1473 strip_info->watchdog_doprobe = jiffies + 10 * HZ;
1474 strip_info->watchdog_doreset = jiffies + 1 * HZ;
1475 /*printk(KERN_INFO "%s: Routine radio test.\n", strip_info->dev->name); */
1479 * 3. Set up the strip_info ready to send the data (if any).
1481 strip_info->tx_head = strip_info->tx_buff;
1482 strip_info->tx_left = ptr - strip_info->tx_buff;
1483 strip_info->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1486 * 4. Debugging check to make sure we're not overflowing the buffer.
1488 if (strip_info->tx_size - strip_info->tx_left < 20)
1489 printk(KERN_ERR "%s: Sending%5d bytes;%5d bytes free.\n",
1490 strip_info->dev->name, strip_info->tx_left,
1491 strip_info->tx_size - strip_info->tx_left);
1494 * 5. If watchdog has expired, reset the radio. Note: if there's data waiting in
1495 * the buffer, strip_write_some_more will send it after the reset has finished
1498 ResetRadio(strip_info);
1503 struct in_device *in_dev = in_dev_get(strip_info->dev);
1506 read_lock(&in_dev->lock);
1507 if (in_dev->ifa_list) {
1508 brd = in_dev->ifa_list->ifa_broadcast;
1509 addr = in_dev->ifa_list->ifa_local;
1511 read_unlock(&in_dev->lock);
1518 * 6. If it is time for a periodic ARP, queue one up to be sent.
1519 * We only do this if:
1520 * 1. The radio is working
1521 * 2. It's time to send another periodic ARP
1522 * 3. We really know what our address is (and it is not manually set to zero)
1523 * 4. We have a designated broadcast address configured
1524 * If we queue up an ARP packet when we don't have a designated broadcast
1525 * address configured, then the packet will just have to be discarded in
1526 * strip_make_packet. This is not fatal, but it causes misleading information
1527 * to be displayed in tcpdump. tcpdump will report that periodic APRs are
1528 * being sent, when in fact they are not, because they are all being dropped
1529 * in the strip_make_packet routine.
1531 if (strip_info->working
1532 && (long) jiffies - strip_info->gratuitous_arp >= 0
1533 && memcmp(strip_info->dev->dev_addr, zero_address.c,
1534 sizeof(zero_address))
1535 && arp_query(haddr.c, brd, strip_info->dev)) {
1536 /*printk(KERN_INFO "%s: Sending gratuitous ARP with interval %ld\n",
1537 strip_info->dev->name, strip_info->arp_interval / HZ); */
1538 strip_info->gratuitous_arp =
1539 jiffies + strip_info->arp_interval;
1540 strip_info->arp_interval *= 2;
1541 if (strip_info->arp_interval > MaxARPInterval)
1542 strip_info->arp_interval = MaxARPInterval;
1544 arp_send(ARPOP_REPLY, ETH_P_ARP, addr, /* Target address of ARP packet is our address */
1545 strip_info->dev, /* Device to send packet on */
1546 addr, /* Source IP address this ARP packet comes from */
1547 NULL, /* Destination HW address is NULL (broadcast it) */
1548 strip_info->dev->dev_addr, /* Source HW address is our HW address */
1549 strip_info->dev->dev_addr); /* Target HW address is our HW address (redundant) */
1553 * 7. All ready. Start the transmission
1555 strip_write_some_more(strip_info->tty);
1558 /* Encapsulate a datagram and kick it into a TTY queue. */
1559 static int strip_xmit(struct sk_buff *skb, struct net_device *dev)
1561 struct strip *strip_info = (struct strip *) (dev->priv);
1563 if (!netif_running(dev)) {
1564 printk(KERN_ERR "%s: xmit call when iface is down\n",
1569 netif_stop_queue(dev);
1571 del_timer(&strip_info->idle_timer);
1574 if (jiffies - strip_info->pps_timer > HZ) {
1575 unsigned long t = jiffies - strip_info->pps_timer;
1576 unsigned long rx_pps_count = (strip_info->rx_pps_count * HZ * 8 + t / 2) / t;
1577 unsigned long tx_pps_count = (strip_info->tx_pps_count * HZ * 8 + t / 2) / t;
1578 unsigned long sx_pps_count = (strip_info->sx_pps_count * HZ * 8 + t / 2) / t;
1580 strip_info->pps_timer = jiffies;
1581 strip_info->rx_pps_count = 0;
1582 strip_info->tx_pps_count = 0;
1583 strip_info->sx_pps_count = 0;
1585 strip_info->rx_average_pps = (strip_info->rx_average_pps + rx_pps_count + 1) / 2;
1586 strip_info->tx_average_pps = (strip_info->tx_average_pps + tx_pps_count + 1) / 2;
1587 strip_info->sx_average_pps = (strip_info->sx_average_pps + sx_pps_count + 1) / 2;
1589 if (rx_pps_count / 8 >= 10)
1590 printk(KERN_INFO "%s: WARNING: Receiving %ld packets per second.\n",
1591 strip_info->dev->name, rx_pps_count / 8);
1592 if (tx_pps_count / 8 >= 10)
1593 printk(KERN_INFO "%s: WARNING: Tx %ld packets per second.\n",
1594 strip_info->dev->name, tx_pps_count / 8);
1595 if (sx_pps_count / 8 >= 10)
1596 printk(KERN_INFO "%s: WARNING: Sending %ld packets per second.\n",
1597 strip_info->dev->name, sx_pps_count / 8);
1600 spin_lock_bh(&strip_lock);
1602 strip_send(strip_info, skb);
1604 spin_unlock_bh(&strip_lock);
1612 * IdleTask periodically calls strip_xmit, so even when we have no IP packets
1613 * to send for an extended period of time, the watchdog processing still gets
1614 * done to ensure that the radio stays in Starmode
1617 static void strip_IdleTask(unsigned long parameter)
1619 strip_xmit(NULL, (struct net_device *) parameter);
1623 * Create the MAC header for an arbitrary protocol layer
1625 * saddr!=NULL means use this specific address (n/a for Metricom)
1626 * saddr==NULL means use default device source address
1627 * daddr!=NULL means use this destination address
1628 * daddr==NULL means leave destination address alone
1629 * (e.g. unresolved arp -- kernel will call
1630 * rebuild_header later to fill in the address)
1633 static int strip_header(struct sk_buff *skb, struct net_device *dev,
1634 unsigned short type, void *daddr, void *saddr,
1637 struct strip *strip_info = (struct strip *) (dev->priv);
1638 STRIP_Header *header = (STRIP_Header *) skb_push(skb, sizeof(STRIP_Header));
1640 /*printk(KERN_INFO "%s: strip_header 0x%04X %s\n", dev->name, type,
1641 type == ETH_P_IP ? "IP" : type == ETH_P_ARP ? "ARP" : ""); */
1643 header->src_addr = strip_info->true_dev_addr;
1644 header->protocol = htons(type);
1646 /*HexDump("strip_header", (struct strip *)(dev->priv), skb->data, skb->data + skb->len); */
1649 return (-dev->hard_header_len);
1651 header->dst_addr = *(MetricomAddress *) daddr;
1652 return (dev->hard_header_len);
1656 * Rebuild the MAC header. This is called after an ARP
1657 * (or in future other address resolution) has completed on this
1658 * sk_buff. We now let ARP fill in the other fields.
1659 * I think this should return zero if packet is ready to send,
1660 * or non-zero if it needs more time to do an address lookup
1663 static int strip_rebuild_header(struct sk_buff *skb)
1666 STRIP_Header *header = (STRIP_Header *) skb->data;
1668 /* Arp find returns zero if if knows the address, */
1669 /* or if it doesn't know the address it sends an ARP packet and returns non-zero */
1670 return arp_find(header->dst_addr.c, skb) ? 1 : 0;
1677 /************************************************************************/
1678 /* Receiving routines */
1680 static int strip_receive_room(struct tty_struct *tty)
1682 return 0x10000; /* We can handle an infinite amount of data. :-) */
1686 * This function parses the response to the ATS300? command,
1687 * extracting the radio version and serial number.
1689 static void get_radio_version(struct strip *strip_info, __u8 * ptr, __u8 * end)
1691 __u8 *p, *value_begin, *value_end;
1694 /* Determine the beginning of the second line of the payload */
1696 while (p < end && *p != 10)
1703 /* Determine the end of line */
1704 while (p < end && *p != 10)
1711 len = value_end - value_begin;
1712 len = min_t(int, len, sizeof(FirmwareVersion) - 1);
1713 if (strip_info->firmware_version.c[0] == 0)
1714 printk(KERN_INFO "%s: Radio Firmware: %.*s\n",
1715 strip_info->dev->name, len, value_begin);
1716 sprintf(strip_info->firmware_version.c, "%.*s", len, value_begin);
1718 /* Look for the first colon */
1719 while (p < end && *p != ':')
1723 /* Skip over the space */
1725 len = sizeof(SerialNumber) - 1;
1726 if (p + len <= end) {
1727 sprintf(strip_info->serial_number.c, "%.*s", len, p);
1730 "STRIP: radio serial number shorter (%zd) than expected (%d)\n",
1736 * This function parses the response to the ATS325? command,
1737 * extracting the radio battery voltage.
1739 static void get_radio_voltage(struct strip *strip_info, __u8 * ptr, __u8 * end)
1743 len = sizeof(BatteryVoltage) - 1;
1744 if (ptr + len <= end) {
1745 sprintf(strip_info->battery_voltage.c, "%.*s", len, ptr);
1748 "STRIP: radio voltage string shorter (%zd) than expected (%d)\n",
1754 * This function parses the responses to the AT~LA and ATS311 commands,
1755 * which list the radio's neighbours.
1757 static void get_radio_neighbours(MetricomNodeTable * table, __u8 * ptr, __u8 * end)
1759 table->num_nodes = 0;
1760 while (ptr < end && table->num_nodes < NODE_TABLE_SIZE) {
1761 MetricomNode *node = &table->node[table->num_nodes++];
1762 char *dst = node->c, *limit = dst + sizeof(*node) - 1;
1763 while (ptr < end && *ptr <= 32)
1765 while (ptr < end && dst < limit && *ptr != 10)
1768 while (ptr < end && ptr[-1] != 10)
1771 do_gettimeofday(&table->timestamp);
1774 static int get_radio_address(struct strip *strip_info, __u8 * p)
1776 MetricomAddress addr;
1778 if (string_to_radio_address(&addr, p))
1781 /* See if our radio address has changed */
1782 if (memcmp(strip_info->true_dev_addr.c, addr.c, sizeof(addr))) {
1783 MetricomAddressString addr_string;
1784 radio_address_to_string(&addr, &addr_string);
1785 printk(KERN_INFO "%s: Radio address = %s\n",
1786 strip_info->dev->name, addr_string.c);
1787 strip_info->true_dev_addr = addr;
1788 if (!strip_info->manual_dev_addr)
1789 *(MetricomAddress *) strip_info->dev->dev_addr =
1791 /* Give the radio a few seconds to get its head straight, then send an arp */
1792 strip_info->gratuitous_arp = jiffies + 15 * HZ;
1793 strip_info->arp_interval = 1 * HZ;
1798 static int verify_checksum(struct strip *strip_info)
1800 __u8 *p = strip_info->sx_buff;
1801 __u8 *end = strip_info->sx_buff + strip_info->sx_count - 4;
1803 (READHEX16(end[0]) << 12) | (READHEX16(end[1]) << 8) |
1804 (READHEX16(end[2]) << 4) | (READHEX16(end[3]));
1807 if (sum == 0 && strip_info->firmware_level == StructuredMessages) {
1808 strip_info->firmware_level = ChecksummedMessages;
1809 printk(KERN_INFO "%s: Radio provides message checksums\n",
1810 strip_info->dev->name);
1815 static void RecvErr(char *msg, struct strip *strip_info)
1817 __u8 *ptr = strip_info->sx_buff;
1818 __u8 *end = strip_info->sx_buff + strip_info->sx_count;
1819 DumpData(msg, strip_info, ptr, end);
1820 strip_info->rx_errors++;
1823 static void RecvErr_Message(struct strip *strip_info, __u8 * sendername,
1824 const __u8 * msg, u_long len)
1826 if (has_prefix(msg, len, "001")) { /* Not in StarMode! */
1827 RecvErr("Error Msg:", strip_info);
1828 printk(KERN_INFO "%s: Radio %s is not in StarMode\n",
1829 strip_info->dev->name, sendername);
1832 else if (has_prefix(msg, len, "002")) { /* Remap handle */
1833 /* We ignore "Remap handle" messages for now */
1836 else if (has_prefix(msg, len, "003")) { /* Can't resolve name */
1837 RecvErr("Error Msg:", strip_info);
1838 printk(KERN_INFO "%s: Destination radio name is unknown\n",
1839 strip_info->dev->name);
1842 else if (has_prefix(msg, len, "004")) { /* Name too small or missing */
1843 strip_info->watchdog_doreset = jiffies + LongTime;
1847 do_gettimeofday(&tv);
1849 "**** Got ERR_004 response at %02d.%06d\n",
1850 tv.tv_sec % 100, tv.tv_usec);
1853 if (!strip_info->working) {
1854 strip_info->working = TRUE;
1855 printk(KERN_INFO "%s: Radio now in starmode\n",
1856 strip_info->dev->name);
1858 * If the radio has just entered a working state, we should do our first
1859 * probe ASAP, so that we find out our radio address etc. without delay.
1861 strip_info->watchdog_doprobe = jiffies;
1863 if (strip_info->firmware_level == NoStructure && sendername) {
1864 strip_info->firmware_level = StructuredMessages;
1865 strip_info->next_command = 0; /* Try to enable checksums ASAP */
1867 "%s: Radio provides structured messages\n",
1868 strip_info->dev->name);
1870 if (strip_info->firmware_level >= StructuredMessages) {
1872 * If this message has a valid checksum on the end, then the call to verify_checksum
1873 * will elevate the firmware_level to ChecksummedMessages for us. (The actual return
1874 * code from verify_checksum is ignored here.)
1876 verify_checksum(strip_info);
1878 * If the radio has structured messages but we don't yet have all our information about it,
1879 * we should do probes without delay, until we have gathered all the information
1881 if (!GOT_ALL_RADIO_INFO(strip_info))
1882 strip_info->watchdog_doprobe = jiffies;
1886 else if (has_prefix(msg, len, "005")) /* Bad count specification */
1887 RecvErr("Error Msg:", strip_info);
1889 else if (has_prefix(msg, len, "006")) /* Header too big */
1890 RecvErr("Error Msg:", strip_info);
1892 else if (has_prefix(msg, len, "007")) { /* Body too big */
1893 RecvErr("Error Msg:", strip_info);
1895 "%s: Error! Packet size too big for radio.\n",
1896 strip_info->dev->name);
1899 else if (has_prefix(msg, len, "008")) { /* Bad character in name */
1900 RecvErr("Error Msg:", strip_info);
1902 "%s: Radio name contains illegal character\n",
1903 strip_info->dev->name);
1906 else if (has_prefix(msg, len, "009")) /* No count or line terminator */
1907 RecvErr("Error Msg:", strip_info);
1909 else if (has_prefix(msg, len, "010")) /* Invalid checksum */
1910 RecvErr("Error Msg:", strip_info);
1912 else if (has_prefix(msg, len, "011")) /* Checksum didn't match */
1913 RecvErr("Error Msg:", strip_info);
1915 else if (has_prefix(msg, len, "012")) /* Failed to transmit packet */
1916 RecvErr("Error Msg:", strip_info);
1919 RecvErr("Error Msg:", strip_info);
1922 static void process_AT_response(struct strip *strip_info, __u8 * ptr,
1927 while (p < end && p[-1] != 10)
1928 p++; /* Skip past first newline character */
1929 /* Now ptr points to the AT command, and p points to the text of the response. */
1935 do_gettimeofday(&tv);
1936 printk(KERN_INFO "**** Got AT response %.7s at %02d.%06d\n",
1937 ptr, tv.tv_sec % 100, tv.tv_usec);
1941 if (has_prefix(ptr, len, "ATS300?"))
1942 get_radio_version(strip_info, p, end);
1943 else if (has_prefix(ptr, len, "ATS305?"))
1944 get_radio_address(strip_info, p);
1945 else if (has_prefix(ptr, len, "ATS311?"))
1946 get_radio_neighbours(&strip_info->poletops, p, end);
1947 else if (has_prefix(ptr, len, "ATS319=7"))
1948 verify_checksum(strip_info);
1949 else if (has_prefix(ptr, len, "ATS325?"))
1950 get_radio_voltage(strip_info, p, end);
1951 else if (has_prefix(ptr, len, "AT~LA"))
1952 get_radio_neighbours(&strip_info->portables, p, end);
1954 RecvErr("Unknown AT Response:", strip_info);
1957 static void process_ACK(struct strip *strip_info, __u8 * ptr, __u8 * end)
1959 /* Currently we don't do anything with ACKs from the radio */
1962 static void process_Info(struct strip *strip_info, __u8 * ptr, __u8 * end)
1965 RecvErr("Bad Info Msg:", strip_info);
1968 static struct net_device *get_strip_dev(struct strip *strip_info)
1970 /* If our hardware address is *manually set* to zero, and we know our */
1971 /* real radio hardware address, try to find another strip device that has been */
1972 /* manually set to that address that we can 'transfer ownership' of this packet to */
1973 if (strip_info->manual_dev_addr &&
1974 !memcmp(strip_info->dev->dev_addr, zero_address.c,
1975 sizeof(zero_address))
1976 && memcmp(&strip_info->true_dev_addr, zero_address.c,
1977 sizeof(zero_address))) {
1978 struct net_device *dev;
1979 read_lock_bh(&dev_base_lock);
1982 if (dev->type == strip_info->dev->type &&
1983 !memcmp(dev->dev_addr,
1984 &strip_info->true_dev_addr,
1985 sizeof(MetricomAddress))) {
1987 "%s: Transferred packet ownership to %s.\n",
1988 strip_info->dev->name, dev->name);
1989 read_unlock_bh(&dev_base_lock);
1994 read_unlock_bh(&dev_base_lock);
1996 return (strip_info->dev);
2000 * Send one completely decapsulated datagram to the next layer.
2003 static void deliver_packet(struct strip *strip_info, STRIP_Header * header,
2006 struct sk_buff *skb = dev_alloc_skb(sizeof(STRIP_Header) + packetlen);
2008 printk(KERN_ERR "%s: memory squeeze, dropping packet.\n",
2009 strip_info->dev->name);
2010 strip_info->rx_dropped++;
2012 memcpy(skb_put(skb, sizeof(STRIP_Header)), header,
2013 sizeof(STRIP_Header));
2014 memcpy(skb_put(skb, packetlen), strip_info->rx_buff,
2016 skb->dev = get_strip_dev(strip_info);
2017 skb->protocol = header->protocol;
2018 skb->mac.raw = skb->data;
2020 /* Having put a fake header on the front of the sk_buff for the */
2021 /* benefit of tools like tcpdump, skb_pull now 'consumes' that */
2022 /* fake header before we hand the packet up to the next layer. */
2023 skb_pull(skb, sizeof(STRIP_Header));
2025 /* Finally, hand the packet up to the next layer (e.g. IP or ARP, etc.) */
2026 strip_info->rx_packets++;
2027 strip_info->rx_pps_count++;
2029 strip_info->rx_bytes += packetlen;
2031 skb->dev->last_rx = jiffies;
2036 static void process_IP_packet(struct strip *strip_info,
2037 STRIP_Header * header, __u8 * ptr,
2042 /* Decode start of the IP packet header */
2043 ptr = UnStuffData(ptr, end, strip_info->rx_buff, 4);
2045 RecvErr("IP Packet too short", strip_info);
2049 packetlen = ((__u16) strip_info->rx_buff[2] << 8) | strip_info->rx_buff[3];
2051 if (packetlen > MAX_RECV_MTU) {
2052 printk(KERN_INFO "%s: Dropping oversized received IP packet: %d bytes\n",
2053 strip_info->dev->name, packetlen);
2054 strip_info->rx_dropped++;
2058 /*printk(KERN_INFO "%s: Got %d byte IP packet\n", strip_info->dev->name, packetlen); */
2060 /* Decode remainder of the IP packet */
2062 UnStuffData(ptr, end, strip_info->rx_buff + 4, packetlen - 4);
2064 RecvErr("IP Packet too short", strip_info);
2069 RecvErr("IP Packet too long", strip_info);
2073 header->protocol = htons(ETH_P_IP);
2075 deliver_packet(strip_info, header, packetlen);
2078 static void process_ARP_packet(struct strip *strip_info,
2079 STRIP_Header * header, __u8 * ptr,
2083 struct arphdr *arphdr = (struct arphdr *) strip_info->rx_buff;
2085 /* Decode start of the ARP packet */
2086 ptr = UnStuffData(ptr, end, strip_info->rx_buff, 8);
2088 RecvErr("ARP Packet too short", strip_info);
2092 packetlen = 8 + (arphdr->ar_hln + arphdr->ar_pln) * 2;
2094 if (packetlen > MAX_RECV_MTU) {
2096 "%s: Dropping oversized received ARP packet: %d bytes\n",
2097 strip_info->dev->name, packetlen);
2098 strip_info->rx_dropped++;
2102 /*printk(KERN_INFO "%s: Got %d byte ARP %s\n",
2103 strip_info->dev->name, packetlen,
2104 ntohs(arphdr->ar_op) == ARPOP_REQUEST ? "request" : "reply"); */
2106 /* Decode remainder of the ARP packet */
2108 UnStuffData(ptr, end, strip_info->rx_buff + 8, packetlen - 8);
2110 RecvErr("ARP Packet too short", strip_info);
2115 RecvErr("ARP Packet too long", strip_info);
2119 header->protocol = htons(ETH_P_ARP);
2121 deliver_packet(strip_info, header, packetlen);
2125 * process_text_message processes a <CR>-terminated block of data received
2126 * from the radio that doesn't begin with a '*' character. All normal
2127 * Starmode communication messages with the radio begin with a '*',
2128 * so any text that does not indicates a serial port error, a radio that
2129 * is in Hayes command mode instead of Starmode, or a radio with really
2130 * old firmware that doesn't frame its Starmode responses properly.
2132 static void process_text_message(struct strip *strip_info)
2134 __u8 *msg = strip_info->sx_buff;
2135 int len = strip_info->sx_count;
2137 /* Check for anything that looks like it might be our radio name */
2138 /* (This is here for backwards compatibility with old firmware) */
2139 if (len == 9 && get_radio_address(strip_info, msg) == 0)
2142 if (text_equal(msg, len, "OK"))
2143 return; /* Ignore 'OK' responses from prior commands */
2144 if (text_equal(msg, len, "ERROR"))
2145 return; /* Ignore 'ERROR' messages */
2146 if (has_prefix(msg, len, "ate0q1"))
2147 return; /* Ignore character echo back from the radio */
2149 /* Catch other error messages */
2150 /* (This is here for backwards compatibility with old firmware) */
2151 if (has_prefix(msg, len, "ERR_")) {
2152 RecvErr_Message(strip_info, NULL, &msg[4], len - 4);
2156 RecvErr("No initial *", strip_info);
2160 * process_message processes a <CR>-terminated block of data received
2161 * from the radio. If the radio is not in Starmode or has old firmware,
2162 * it may be a line of text in response to an AT command. Ideally, with
2163 * a current radio that's properly in Starmode, all data received should
2164 * be properly framed and checksummed radio message blocks, containing
2165 * either a starmode packet, or a other communication from the radio
2166 * firmware, like "INF_" Info messages and &COMMAND responses.
2168 static void process_message(struct strip *strip_info)
2170 STRIP_Header header = { zero_address, zero_address, 0 };
2171 __u8 *ptr = strip_info->sx_buff;
2172 __u8 *end = strip_info->sx_buff + strip_info->sx_count;
2173 __u8 sendername[32], *sptr = sendername;
2176 /*HexDump("Receiving", strip_info, ptr, end); */
2178 /* Check for start of address marker, and then skip over it */
2182 process_text_message(strip_info);
2186 /* Copy out the return address */
2187 while (ptr < end && *ptr != '*'
2188 && sptr < ARRAY_END(sendername) - 1)
2190 *sptr = 0; /* Null terminate the sender name */
2192 /* Check for end of address marker, and skip over it */
2193 if (ptr >= end || *ptr != '*') {
2194 RecvErr("No second *", strip_info);
2197 ptr++; /* Skip the second '*' */
2199 /* If the sender name is "&COMMAND", ignore this 'packet' */
2200 /* (This is here for backwards compatibility with old firmware) */
2201 if (!strcmp(sendername, "&COMMAND")) {
2202 strip_info->firmware_level = NoStructure;
2203 strip_info->next_command = CompatibilityCommand;
2207 if (ptr + 4 > end) {
2208 RecvErr("No proto key", strip_info);
2212 /* Get the protocol key out of the buffer */
2218 /* If we're using checksums, verify the checksum at the end of the packet */
2219 if (strip_info->firmware_level >= ChecksummedMessages) {
2220 end -= 4; /* Chop the last four bytes off the packet (they're the checksum) */
2222 RecvErr("Missing Checksum", strip_info);
2225 if (!verify_checksum(strip_info)) {
2226 RecvErr("Bad Checksum", strip_info);
2231 /*printk(KERN_INFO "%s: Got packet from \"%s\".\n", strip_info->dev->name, sendername); */
2234 * Fill in (pseudo) source and destination addresses in the packet.
2235 * We assume that the destination address was our address (the radio does not
2236 * tell us this). If the radio supplies a source address, then we use it.
2238 header.dst_addr = strip_info->true_dev_addr;
2239 string_to_radio_address(&header.src_addr, sendername);
2242 if (key.l == SIP0Key.l) {
2243 strip_info->rx_rbytes += (end - ptr);
2244 process_IP_packet(strip_info, &header, ptr, end);
2245 } else if (key.l == ARP0Key.l) {
2246 strip_info->rx_rbytes += (end - ptr);
2247 process_ARP_packet(strip_info, &header, ptr, end);
2248 } else if (key.l == ATR_Key.l) {
2249 strip_info->rx_ebytes += (end - ptr);
2250 process_AT_response(strip_info, ptr, end);
2251 } else if (key.l == ACK_Key.l) {
2252 strip_info->rx_ebytes += (end - ptr);
2253 process_ACK(strip_info, ptr, end);
2254 } else if (key.l == INF_Key.l) {
2255 strip_info->rx_ebytes += (end - ptr);
2256 process_Info(strip_info, ptr, end);
2257 } else if (key.l == ERR_Key.l) {
2258 strip_info->rx_ebytes += (end - ptr);
2259 RecvErr_Message(strip_info, sendername, ptr, end - ptr);
2261 RecvErr("Unrecognized protocol key", strip_info);
2263 if (key.l == SIP0Key.l)
2264 process_IP_packet(strip_info, &header, ptr, end);
2265 else if (key.l == ARP0Key.l)
2266 process_ARP_packet(strip_info, &header, ptr, end);
2267 else if (key.l == ATR_Key.l)
2268 process_AT_response(strip_info, ptr, end);
2269 else if (key.l == ACK_Key.l)
2270 process_ACK(strip_info, ptr, end);
2271 else if (key.l == INF_Key.l)
2272 process_Info(strip_info, ptr, end);
2273 else if (key.l == ERR_Key.l)
2274 RecvErr_Message(strip_info, sendername, ptr, end - ptr);
2276 RecvErr("Unrecognized protocol key", strip_info);
2280 #define TTYERROR(X) ((X) == TTY_BREAK ? "Break" : \
2281 (X) == TTY_FRAME ? "Framing Error" : \
2282 (X) == TTY_PARITY ? "Parity Error" : \
2283 (X) == TTY_OVERRUN ? "Hardware Overrun" : "Unknown Error")
2286 * Handle the 'receiver data ready' interrupt.
2287 * This function is called by the 'tty_io' module in the kernel when
2288 * a block of STRIP data has been received, which can now be decapsulated
2289 * and sent on to some IP layer for further processing.
2292 static void strip_receive_buf(struct tty_struct *tty, const unsigned char *cp,
2293 char *fp, int count)
2295 struct strip *strip_info = (struct strip *) tty->disc_data;
2296 const unsigned char *end = cp + count;
2298 if (!strip_info || strip_info->magic != STRIP_MAGIC
2299 || !netif_running(strip_info->dev))
2302 spin_lock_bh(&strip_lock);
2306 do_gettimeofday(&tv);
2308 "**** strip_receive_buf: %3d bytes at %02d.%06d\n",
2309 count, tv.tv_sec % 100, tv.tv_usec);
2314 strip_info->rx_sbytes += count;
2317 /* Read the characters out of the buffer */
2320 printk(KERN_INFO "%s: %s on serial port\n",
2321 strip_info->dev->name, TTYERROR(*fp));
2322 if (fp && *fp++ && !strip_info->discard) { /* If there's a serial error, record it */
2323 /* If we have some characters in the buffer, discard them */
2324 strip_info->discard = strip_info->sx_count;
2325 strip_info->rx_errors++;
2328 /* Leading control characters (CR, NL, Tab, etc.) are ignored */
2329 if (strip_info->sx_count > 0 || *cp >= ' ') {
2330 if (*cp == 0x0D) { /* If end of packet, decide what to do with it */
2331 if (strip_info->sx_count > 3000)
2333 "%s: Cut a %d byte packet (%zd bytes remaining)%s\n",
2334 strip_info->dev->name,
2335 strip_info->sx_count,
2338 discard ? " (discarded)" :
2340 if (strip_info->sx_count >
2341 strip_info->sx_size) {
2342 strip_info->rx_over_errors++;
2344 "%s: sx_buff overflow (%d bytes total)\n",
2345 strip_info->dev->name,
2346 strip_info->sx_count);
2347 } else if (strip_info->discard)
2349 "%s: Discarding bad packet (%d/%d)\n",
2350 strip_info->dev->name,
2351 strip_info->discard,
2352 strip_info->sx_count);
2354 process_message(strip_info);
2355 strip_info->discard = 0;
2356 strip_info->sx_count = 0;
2358 /* Make sure we have space in the buffer */
2359 if (strip_info->sx_count <
2360 strip_info->sx_size)
2361 strip_info->sx_buff[strip_info->
2364 strip_info->sx_count++;
2369 spin_unlock_bh(&strip_lock);
2373 /************************************************************************/
2374 /* General control routines */
2376 static int set_mac_address(struct strip *strip_info,
2377 MetricomAddress * addr)
2380 * We're using a manually specified address if the address is set
2381 * to anything other than all ones. Setting the address to all ones
2382 * disables manual mode and goes back to automatic address determination
2383 * (tracking the true address that the radio has).
2385 strip_info->manual_dev_addr =
2386 memcmp(addr->c, broadcast_address.c,
2387 sizeof(broadcast_address));
2388 if (strip_info->manual_dev_addr)
2389 *(MetricomAddress *) strip_info->dev->dev_addr = *addr;
2391 *(MetricomAddress *) strip_info->dev->dev_addr =
2392 strip_info->true_dev_addr;
2396 static int dev_set_mac_address(struct net_device *dev, void *addr)
2398 struct strip *strip_info = (struct strip *) (dev->priv);
2399 struct sockaddr *sa = addr;
2400 printk(KERN_INFO "%s: strip_set_dev_mac_address called\n", dev->name);
2401 set_mac_address(strip_info, (MetricomAddress *) sa->sa_data);
2405 static struct net_device_stats *strip_get_stats(struct net_device *dev)
2407 static struct net_device_stats stats;
2408 struct strip *strip_info = (struct strip *) (dev->priv);
2410 memset(&stats, 0, sizeof(struct net_device_stats));
2412 stats.rx_packets = strip_info->rx_packets;
2413 stats.tx_packets = strip_info->tx_packets;
2414 stats.rx_dropped = strip_info->rx_dropped;
2415 stats.tx_dropped = strip_info->tx_dropped;
2416 stats.tx_errors = strip_info->tx_errors;
2417 stats.rx_errors = strip_info->rx_errors;
2418 stats.rx_over_errors = strip_info->rx_over_errors;
2423 /************************************************************************/
2424 /* Opening and closing */
2427 * Here's the order things happen:
2428 * When the user runs "slattach -p strip ..."
2429 * 1. The TTY module calls strip_open
2430 * 2. strip_open calls strip_alloc
2431 * 3. strip_alloc calls register_netdev
2432 * 4. register_netdev calls strip_dev_init
2433 * 5. then strip_open finishes setting up the strip_info
2435 * When the user runs "ifconfig st<x> up address netmask ..."
2436 * 6. strip_open_low gets called
2438 * When the user runs "ifconfig st<x> down"
2439 * 7. strip_close_low gets called
2441 * When the user kills the slattach process
2442 * 8. strip_close gets called
2443 * 9. strip_close calls dev_close
2444 * 10. if the device is still up, then dev_close calls strip_close_low
2445 * 11. strip_close calls strip_free
2448 /* Open the low-level part of the STRIP channel. Easy! */
2450 static int strip_open_low(struct net_device *dev)
2452 struct strip *strip_info = (struct strip *) (dev->priv);
2454 if (strip_info->tty == NULL)
2457 if (!allocate_buffers(strip_info, dev->mtu))
2460 strip_info->sx_count = 0;
2461 strip_info->tx_left = 0;
2463 strip_info->discard = 0;
2464 strip_info->working = FALSE;
2465 strip_info->firmware_level = NoStructure;
2466 strip_info->next_command = CompatibilityCommand;
2467 strip_info->user_baud = get_baud(strip_info->tty);
2469 printk(KERN_INFO "%s: Initializing Radio.\n",
2470 strip_info->dev->name);
2471 ResetRadio(strip_info);
2472 strip_info->idle_timer.expires = jiffies + 1 * HZ;
2473 add_timer(&strip_info->idle_timer);
2474 netif_wake_queue(dev);
2480 * Close the low-level part of the STRIP channel. Easy!
2483 static int strip_close_low(struct net_device *dev)
2485 struct strip *strip_info = (struct strip *) (dev->priv);
2487 if (strip_info->tty == NULL)
2489 strip_info->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
2491 netif_stop_queue(dev);
2494 * Free all STRIP frame buffers.
2496 if (strip_info->rx_buff) {
2497 kfree(strip_info->rx_buff);
2498 strip_info->rx_buff = NULL;
2500 if (strip_info->sx_buff) {
2501 kfree(strip_info->sx_buff);
2502 strip_info->sx_buff = NULL;
2504 if (strip_info->tx_buff) {
2505 kfree(strip_info->tx_buff);
2506 strip_info->tx_buff = NULL;
2508 del_timer(&strip_info->idle_timer);
2513 * This routine is called by DDI when the
2514 * (dynamically assigned) device is registered
2517 static void strip_dev_setup(struct net_device *dev)
2520 * Finish setting up the DEVICE info.
2523 SET_MODULE_OWNER(dev);
2525 dev->trans_start = 0;
2527 dev->tx_queue_len = 30; /* Drop after 30 frames queued */
2530 dev->mtu = DEFAULT_STRIP_MTU;
2531 dev->type = ARPHRD_METRICOM; /* dtang */
2532 dev->hard_header_len = sizeof(STRIP_Header);
2534 * dev->priv Already holds a pointer to our struct strip
2537 *(MetricomAddress *) & dev->broadcast = broadcast_address;
2538 dev->dev_addr[0] = 0;
2539 dev->addr_len = sizeof(MetricomAddress);
2542 * Pointers to interface service routines.
2545 dev->open = strip_open_low;
2546 dev->stop = strip_close_low;
2547 dev->hard_start_xmit = strip_xmit;
2548 dev->hard_header = strip_header;
2549 dev->rebuild_header = strip_rebuild_header;
2550 dev->set_mac_address = dev_set_mac_address;
2551 dev->get_stats = strip_get_stats;
2552 dev->change_mtu = strip_change_mtu;
2556 * Free a STRIP channel.
2559 static void strip_free(struct strip *strip_info)
2561 spin_lock_bh(&strip_lock);
2562 list_del_rcu(&strip_info->list);
2563 spin_unlock_bh(&strip_lock);
2565 strip_info->magic = 0;
2567 free_netdev(strip_info->dev);
2572 * Allocate a new free STRIP channel
2574 static struct strip *strip_alloc(void)
2576 struct list_head *n;
2577 struct net_device *dev;
2578 struct strip *strip_info;
2580 dev = alloc_netdev(sizeof(struct strip), "st%d",
2584 return NULL; /* If no more memory, return */
2587 strip_info = dev->priv;
2588 strip_info->dev = dev;
2590 strip_info->magic = STRIP_MAGIC;
2591 strip_info->tty = NULL;
2593 strip_info->gratuitous_arp = jiffies + LongTime;
2594 strip_info->arp_interval = 0;
2595 init_timer(&strip_info->idle_timer);
2596 strip_info->idle_timer.data = (long) dev;
2597 strip_info->idle_timer.function = strip_IdleTask;
2600 spin_lock_bh(&strip_lock);
2603 * Search the list to find where to put our new entry
2604 * (and in the process decide what channel number it is
2607 list_for_each(n, &strip_list) {
2608 struct strip *s = hlist_entry(n, struct strip, list);
2610 if (s->dev->base_addr == dev->base_addr) {
2616 sprintf(dev->name, "st%ld", dev->base_addr);
2618 list_add_tail_rcu(&strip_info->list, &strip_list);
2619 spin_unlock_bh(&strip_lock);
2625 * Open the high-level part of the STRIP channel.
2626 * This function is called by the TTY module when the
2627 * STRIP line discipline is called for. Because we are
2628 * sure the tty line exists, we only have to link it to
2629 * a free STRIP channel...
2632 static int strip_open(struct tty_struct *tty)
2634 struct strip *strip_info = (struct strip *) tty->disc_data;
2637 * First make sure we're not already connected.
2640 if (strip_info && strip_info->magic == STRIP_MAGIC)
2644 * OK. Find a free STRIP channel to use.
2646 if ((strip_info = strip_alloc()) == NULL)
2650 * Register our newly created device so it can be ifconfig'd
2651 * strip_dev_init() will be called as a side-effect
2654 if (register_netdev(strip_info->dev) != 0) {
2655 printk(KERN_ERR "strip: register_netdev() failed.\n");
2656 strip_free(strip_info);
2660 strip_info->tty = tty;
2661 tty->disc_data = strip_info;
2662 if (tty->driver->flush_buffer)
2663 tty->driver->flush_buffer(tty);
2664 if (tty->ldisc.flush_buffer)
2665 tty->ldisc.flush_buffer(tty);
2668 * Restore default settings
2671 strip_info->dev->type = ARPHRD_METRICOM; /* dtang */
2677 tty->termios->c_iflag |= IGNBRK | IGNPAR; /* Ignore breaks and parity errors. */
2678 tty->termios->c_cflag |= CLOCAL; /* Ignore modem control signals. */
2679 tty->termios->c_cflag &= ~HUPCL; /* Don't close on hup */
2681 printk(KERN_INFO "STRIP: device \"%s\" activated\n",
2682 strip_info->dev->name);
2685 * Done. We have linked the TTY line to a channel.
2687 return (strip_info->dev->base_addr);
2691 * Close down a STRIP channel.
2692 * This means flushing out any pending queues, and then restoring the
2693 * TTY line discipline to what it was before it got hooked to STRIP
2694 * (which usually is TTY again).
2697 static void strip_close(struct tty_struct *tty)
2699 struct strip *strip_info = (struct strip *) tty->disc_data;
2702 * First make sure we're connected.
2705 if (!strip_info || strip_info->magic != STRIP_MAGIC)
2708 unregister_netdev(strip_info->dev);
2711 strip_info->tty = NULL;
2712 printk(KERN_INFO "STRIP: device \"%s\" closed down\n",
2713 strip_info->dev->name);
2714 strip_free(strip_info);
2715 tty->disc_data = NULL;
2719 /************************************************************************/
2720 /* Perform I/O control calls on an active STRIP channel. */
2722 static int strip_ioctl(struct tty_struct *tty, struct file *file,
2723 unsigned int cmd, unsigned long arg)
2725 struct strip *strip_info = (struct strip *) tty->disc_data;
2728 * First make sure we're connected.
2731 if (!strip_info || strip_info->magic != STRIP_MAGIC)
2736 if(copy_to_user((void *) arg, strip_info->dev->name, strlen(strip_info->dev->name) + 1))
2741 MetricomAddress addr;
2742 //printk(KERN_INFO "%s: SIOCSIFHWADDR\n", strip_info->dev->name);
2743 if(copy_from_user(&addr, (void *) arg, sizeof(MetricomAddress)))
2745 return set_mac_address(strip_info, &addr);
2748 * Allow stty to read, but not set, the serial port
2753 return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
2756 return -ENOIOCTLCMD;
2763 /************************************************************************/
2764 /* Initialization */
2766 static struct tty_ldisc strip_ldisc = {
2767 .magic = TTY_LDISC_MAGIC,
2769 .owner = THIS_MODULE,
2771 .close = strip_close,
2772 .ioctl = strip_ioctl,
2773 .receive_buf = strip_receive_buf,
2774 .receive_room = strip_receive_room,
2775 .write_wakeup = strip_write_some_more,
2779 * Initialize the STRIP driver.
2780 * This routine is called at boot time, to bootstrap the multi-channel
2784 static char signon[] __initdata =
2785 KERN_INFO "STRIP: Version %s (unlimited channels)\n";
2787 static int __init strip_init_driver(void)
2791 printk(signon, StripVersion);
2795 * Fill in our line protocol discipline, and register it
2797 if ((status = tty_register_ldisc(N_STRIP, &strip_ldisc)))
2798 printk(KERN_ERR "STRIP: can't register line discipline (err = %d)\n",
2802 * Register the status file with /proc
2804 proc_net_fops_create("strip", S_IFREG | S_IRUGO, &strip_seq_fops);
2809 module_init(strip_init_driver);
2811 static const char signoff[] __exitdata =
2812 KERN_INFO "STRIP: Module Unloaded\n";
2814 static void __exit strip_exit_driver(void)
2817 struct list_head *p,*n;
2819 /* module ref count rules assure that all entries are unregistered */
2820 list_for_each_safe(p, n, &strip_list) {
2821 struct strip *s = list_entry(p, struct strip, list);
2825 /* Unregister with the /proc/net file here. */
2826 proc_net_remove("strip");
2828 if ((i = tty_register_ldisc(N_STRIP, NULL)))
2829 printk(KERN_ERR "STRIP: can't unregister line discipline (err = %d)\n", i);
2834 module_exit(strip_exit_driver);
2836 MODULE_AUTHOR("Stuart Cheshire <cheshire@cs.stanford.edu>");
2837 MODULE_DESCRIPTION("Starmode Radio IP (STRIP) Device Driver");
2838 MODULE_LICENSE("Dual BSD/GPL");
2840 MODULE_SUPPORTED_DEVICE("Starmode Radio IP (STRIP) modem");