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