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