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