| 1 |
/*****************************************************************************/ |
| 2 |
|
| 3 |
/* |
| 4 |
* yaminit.c -- YAM radio modem driver setup utility. |
| 5 |
* |
| 6 |
* Copyright (C) 1998 Jean-Paul ROUBELAT ([email protected]) |
| 7 |
* Adapted from sethdlc.c written by Thomas Sailer ([email protected]) |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation; either version 2 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA |
| 22 |
* 02110-1301, USA. |
| 23 |
* |
| 24 |
* Please note that the GPL allows you to use the driver, NOT the radio. |
| 25 |
* In order to use the radio, you need a license from the communications |
| 26 |
* authority of your country. |
| 27 |
* |
| 28 |
* |
| 29 |
* History: |
| 30 |
* 0.0 29.07.98 First draft |
| 31 |
*/ |
| 32 |
|
| 33 |
/*****************************************************************************/ |
| 34 |
|
| 35 |
/* ---------------------------------------------------------------------- */ |
| 36 |
|
| 37 |
#include <stdlib.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <errno.h> |
| 40 |
#include <sys/time.h> |
| 41 |
#include <sys/types.h> |
| 42 |
#include <sys/stat.h> |
| 43 |
#include <fcntl.h> |
| 44 |
#include <unistd.h> |
| 45 |
#include <string.h> |
| 46 |
#include <sys/ioctl.h> |
| 47 |
#include <signal.h> |
| 48 |
#include <sys/socket.h> |
| 49 |
#include <net/if.h> |
| 50 |
#include <netinet/if_ether.h> |
| 51 |
#include <endian.h> |
| 52 |
#include <netinet/in.h> |
| 53 |
|
| 54 |
#include "yam.h" |
| 55 |
|
| 56 |
static unsigned char bitswap (unsigned char c) |
| 57 |
{ |
| 58 |
unsigned char r = 0; |
| 59 |
int i; |
| 60 |
|
| 61 |
for (i = 0; i < 8; i++) |
| 62 |
{ |
| 63 |
r <<= 1; |
| 64 |
if (c & 1) |
| 65 |
r |= 1; |
| 66 |
c >>= 1; |
| 67 |
} |
| 68 |
return r; |
| 69 |
} |
| 70 |
|
| 71 |
static int in2hex (char *ptr) |
| 72 |
{ |
| 73 |
char str[3]; |
| 74 |
unsigned int val; |
| 75 |
|
| 76 |
memcpy (str, ptr, 2); |
| 77 |
str[2] = '\0'; |
| 78 |
|
| 79 |
sscanf (str, "%x", &val); |
| 80 |
|
| 81 |
return val; |
| 82 |
} |
| 83 |
|
| 84 |
static int set_mcs (char *name, struct ifreq *ifr, int sock, int bitrate, char *filename) |
| 85 |
{ |
| 86 |
struct yamdrv_ioctl_mcs yammcs; |
| 87 |
FILE *fptr; |
| 88 |
int nb, i, type, pos; |
| 89 |
char buf[256]; |
| 90 |
|
| 91 |
yammcs.bitrate = bitrate; |
| 92 |
|
| 93 |
fptr = fopen (filename, "r"); |
| 94 |
if (fptr == NULL) |
| 95 |
{ |
| 96 |
fprintf (stderr, "%s: file %s not found\n", name, filename); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
|
| 100 |
pos = 0; |
| 101 |
|
| 102 |
while (fgets (buf, sizeof (buf), fptr)) |
| 103 |
{ |
| 104 |
nb = in2hex (buf + 1); |
| 105 |
type = in2hex (buf + 7); |
| 106 |
|
| 107 |
if (type != 0) |
| 108 |
continue; |
| 109 |
|
| 110 |
for (i = 0; i < nb; i++) |
| 111 |
{ |
| 112 |
if (pos == YAM_FPGA_SIZE) |
| 113 |
{ |
| 114 |
fclose (fptr); |
| 115 |
printf ("fpga bytes are more than %d in %s mcs file\n", YAM_FPGA_SIZE, filename); |
| 116 |
return -1; |
| 117 |
} |
| 118 |
yammcs.bits[pos++] = bitswap (in2hex (buf + 9 + i * 2)); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
fclose (fptr); |
| 123 |
|
| 124 |
if (pos != YAM_FPGA_SIZE) |
| 125 |
{ |
| 126 |
printf ("fpga bytes are not %d in %s mcs file\n", YAM_FPGA_SIZE, filename); |
| 127 |
return -1; |
| 128 |
} |
| 129 |
|
| 130 |
ifr->ifr_data = (caddr_t) & yammcs; |
| 131 |
yammcs.cmd = SIOCYAMSMCS; |
| 132 |
|
| 133 |
if (ioctl (sock, SIOCDEVPRIVATE, ifr) == -1) |
| 134 |
{ |
| 135 |
fprintf (stderr, "%s: Error %s (%i), cannot ioctl SIOCYAMSMCS on %s\n", |
| 136 |
name, strerror (errno), errno, ifr->ifr_name); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
|
| 140 |
printf ("mcs file %s loaded for bitrate %d\n\n", filename, yammcs.bitrate); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static int set_params (char *name, struct ifreq *ifr, int sock, int ac, char **av, unsigned *pmask) |
| 146 |
{ |
| 147 |
int len; |
| 148 |
int bitrate = 0; |
| 149 |
char *filename = NULL; |
| 150 |
struct yamdrv_ioctl_cfg yamctl; |
| 151 |
|
| 152 |
printf ("\n"); |
| 153 |
|
| 154 |
yamctl.cfg.mask = 0; |
| 155 |
|
| 156 |
while (ac >= 2) |
| 157 |
{ |
| 158 |
len = strlen (av[0]); |
| 159 |
if (!strncasecmp (av[0], "iobase", len)) |
| 160 |
{ |
| 161 |
yamctl.cfg.iobase = strtoul (av[1], NULL, 0); |
| 162 |
yamctl.cfg.mask |= YAM_IOBASE; |
| 163 |
} |
| 164 |
else if (!strncasecmp (av[0], "irq", len)) |
| 165 |
{ |
| 166 |
yamctl.cfg.irq = strtoul (av[1], NULL, 0); |
| 167 |
yamctl.cfg.mask |= YAM_IRQ; |
| 168 |
} |
| 169 |
else if (!strncasecmp (av[0], "bitrate", len)) |
| 170 |
{ |
| 171 |
yamctl.cfg.bitrate = strtoul (av[1], NULL, 0); |
| 172 |
yamctl.cfg.mask |= YAM_BITRATE; |
| 173 |
} |
| 174 |
else if (!strncasecmp (av[0], "baudrate", len)) |
| 175 |
{ |
| 176 |
yamctl.cfg.baudrate = strtoul (av[1], NULL, 0); |
| 177 |
yamctl.cfg.mask |= YAM_BAUDRATE; |
| 178 |
} |
| 179 |
else if (!strncasecmp (av[0], "duplex", len)) |
| 180 |
{ |
| 181 |
yamctl.cfg.mode = strtoul (av[1], NULL, 0); |
| 182 |
yamctl.cfg.mask |= YAM_MODE; |
| 183 |
} |
| 184 |
else if (!strncasecmp (av[0], "hold", len)) |
| 185 |
{ |
| 186 |
yamctl.cfg.holddly = strtoul (av[1], NULL, 0); |
| 187 |
yamctl.cfg.mask |= YAM_HOLDDLY; |
| 188 |
} |
| 189 |
else if (!strncasecmp (av[0], "txdelay", len)) |
| 190 |
{ |
| 191 |
yamctl.cfg.txdelay = strtoul (av[1], NULL, 0); |
| 192 |
yamctl.cfg.mask |= YAM_TXDELAY; |
| 193 |
} |
| 194 |
else if (!strncasecmp (av[0], "txtail", len)) |
| 195 |
{ |
| 196 |
yamctl.cfg.txtail = strtoul (av[1], NULL, 0); |
| 197 |
yamctl.cfg.mask |= YAM_TXTAIL; |
| 198 |
} |
| 199 |
else if (!strncasecmp (av[0], "slottime", len)) |
| 200 |
{ |
| 201 |
yamctl.cfg.slottime = strtoul (av[1], NULL, 0); |
| 202 |
yamctl.cfg.mask |= YAM_SLOTTIME; |
| 203 |
} |
| 204 |
else if (!strncasecmp (av[0], "persist", len)) |
| 205 |
{ |
| 206 |
yamctl.cfg.persist = strtoul (av[1], NULL, 0); |
| 207 |
yamctl.cfg.mask |= YAM_PERSIST; |
| 208 |
} |
| 209 |
else if (!strncasecmp (av[0], "load", len)) |
| 210 |
{ |
| 211 |
if (ac < 3) |
| 212 |
break; |
| 213 |
bitrate = strtoul (av[1], NULL, 0); |
| 214 |
filename = av[2]; |
| 215 |
av += 1; |
| 216 |
ac -= 1; |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
fprintf (stderr, "%s: invalid parameter type '%s'\n", |
| 221 |
name, av[0]); |
| 222 |
} |
| 223 |
av += 2; |
| 224 |
ac -= 2; |
| 225 |
} |
| 226 |
if (ac > 0) |
| 227 |
fprintf (stderr, "%s: orphaned parameter type '%s', no data\n", |
| 228 |
name, av[0]); |
| 229 |
|
| 230 |
if (yamctl.cfg.mask) |
| 231 |
{ |
| 232 |
ifr->ifr_data = (caddr_t) & yamctl; |
| 233 |
yamctl.cmd = SIOCYAMSCFG; |
| 234 |
*pmask = yamctl.cfg.mask; |
| 235 |
|
| 236 |
if (ioctl (sock, SIOCDEVPRIVATE, ifr) == -1) |
| 237 |
{ |
| 238 |
fprintf (stderr, "%s: Error %s (%i), cannot ioctl SIOCYAMSCFG on %s\n", |
| 239 |
name, strerror (errno), errno, ifr->ifr_name); |
| 240 |
return -1; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if (filename) |
| 245 |
return set_mcs (name, ifr, sock, bitrate, filename); |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
#define is_mask(m, n) (m & n) ? '*' : ' ' |
| 251 |
static void get_params (char *name, struct ifreq *ifr, int sock, unsigned mask) |
| 252 |
{ |
| 253 |
char *dup; |
| 254 |
struct yamdrv_ioctl_cfg yamctl; |
| 255 |
|
| 256 |
ifr->ifr_data = (caddr_t) & yamctl; |
| 257 |
yamctl.cmd = SIOCYAMGCFG; |
| 258 |
|
| 259 |
if (ioctl (sock, SIOCDEVPRIVATE, ifr) == -1) |
| 260 |
{ |
| 261 |
fprintf (stderr, "%s: Error %s (%i), cannot ioctl SIOCYAMGCFG on %s\n", |
| 262 |
name, strerror (errno), errno, ifr->ifr_name); |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
switch (yamctl.cfg.mode) |
| 267 |
{ |
| 268 |
case 0: |
| 269 |
dup = "off"; |
| 270 |
break; |
| 271 |
case 1: |
| 272 |
dup = "on"; |
| 273 |
break; |
| 274 |
case 2: |
| 275 |
dup = "on+delay"; |
| 276 |
break; |
| 277 |
default: |
| 278 |
dup = ""; |
| 279 |
break; |
| 280 |
} |
| 281 |
|
| 282 |
printf ("Device : %s\n", ifr->ifr_name); |
| 283 |
printf ("iobase :%c%04x\n", is_mask (mask, YAM_IOBASE), yamctl.cfg.iobase); |
| 284 |
printf ("bitrate :%c%u (bps)\n", is_mask (mask, YAM_BITRATE), yamctl.cfg.bitrate); |
| 285 |
printf ("irq :%c%u\n", is_mask (mask, YAM_IRQ), yamctl.cfg.irq); |
| 286 |
printf ("baudrate :%c%u (bps)\n", is_mask (mask, YAM_BAUDRATE), yamctl.cfg.baudrate); |
| 287 |
printf ("fulldup :%c%s\n", is_mask (mask, YAM_MODE), dup); |
| 288 |
if (yamctl.cfg.mode == 2) |
| 289 |
printf ("holddelay :%c%-3u (sec)\n", is_mask (mask, YAM_HOLDDLY), yamctl.cfg.holddly); |
| 290 |
printf ("txdelay :%c%-3u (ms)\n", is_mask (mask, YAM_TXDELAY), yamctl.cfg.txdelay); |
| 291 |
printf ("txtail :%c%-3u (ms)\n", is_mask (mask, YAM_TXTAIL), yamctl.cfg.txtail); |
| 292 |
printf ("slottime :%c%-3u (ms)\n", is_mask (mask, YAM_SLOTTIME), yamctl.cfg.slottime); |
| 293 |
printf ("persist :%c%u\n", is_mask (mask, YAM_PERSIST), yamctl.cfg.persist); |
| 294 |
printf ("\n"); |
| 295 |
} |
| 296 |
|
| 297 |
static const char *usage_str = |
| 298 |
"[-h] yamif\n" |
| 299 |
" [iobase <iobase>] [irq <irq>] [bitrate <bitrate>] [baudrate <baudrate>]\n" |
| 300 |
" [duplex <mode>] [hold <delay>] [txdelay <txdelay>]\n" |
| 301 |
" [txtail <txtail>] [slottime <slottime>] [persist <persistence>]\n" |
| 302 |
" [load <bitrate> <filename.mcs>]\n" |
| 303 |
" yamif: the name of the yam kernel driver interface (yam0)\n" |
| 304 |
" -h: this help\n\n"; |
| 305 |
|
| 306 |
int main (int argc, char *argv[]) |
| 307 |
{ |
| 308 |
char name[256] = "yam0"; |
| 309 |
struct ifreq ifr; |
| 310 |
int sock; |
| 311 |
unsigned int mask; |
| 312 |
|
| 313 |
printf ("\n%s: Version 0.1 (C) 1998 by Jean-Paul ROUBELAT - F6FBB\n", *argv); |
| 314 |
|
| 315 |
if ((argc < 2) || (strcasecmp (argv[1], "-h") == 0)) |
| 316 |
{ |
| 317 |
printf ("usage: %s %s", *argv, usage_str); |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
strcpy (name, argv[1]); |
| 322 |
|
| 323 |
sock = socket(PF_INET, SOCK_PACKET, htons(ETH_P_AX25)); |
| 324 |
if (sock < 0) |
| 325 |
{ |
| 326 |
fprintf (stderr, "%s: Error %s (%i), cannot open %s\n", |
| 327 |
argv[0], strerror (errno), errno, name); |
| 328 |
return 2; |
| 329 |
} |
| 330 |
|
| 331 |
strcpy (ifr.ifr_name, name); |
| 332 |
if (ioctl (sock, SIOCGIFFLAGS, &ifr) < 0) |
| 333 |
{ |
| 334 |
fprintf (stderr, "%s: Error %s (%i), cannot ioctl %s\n", |
| 335 |
argv[0], strerror (errno), errno, name); |
| 336 |
close (sock); |
| 337 |
return 3; |
| 338 |
} |
| 339 |
|
| 340 |
mask = 0; |
| 341 |
|
| 342 |
if (set_params (argv[0], &ifr, sock, argc - 2, argv + 2, &mask) == -1) |
| 343 |
{ |
| 344 |
close (sock); |
| 345 |
return 4; |
| 346 |
} |
| 347 |
|
| 348 |
get_params (argv[0], &ifr, sock, mask); |
| 349 |
|
| 350 |
close (sock); |
| 351 |
|
| 352 |
return 0; |
| 353 |
} |