hdlcdrv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*****************************************************************************/
  2. /*
  3. * hdlcdrv.c -- HDLC packet radio network driver.
  4. *
  5. * Copyright (C) 1996-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * Please note that the GPL allows you to use the driver, NOT the radio.
  22. * In order to use the radio, you need a license from the communications
  23. * authority of your country.
  24. *
  25. * The driver was derived from Donald Beckers skeleton.c
  26. * Written 1993-94 by Donald Becker.
  27. *
  28. * History:
  29. * 0.1 21.09.1996 Started
  30. * 18.10.1996 Changed to new user space access routines
  31. * (copy_{to,from}_user)
  32. * 0.2 21.11.1996 various small changes
  33. * 0.3 03.03.1997 fixed (hopefully) IP not working with ax.25 as a module
  34. * 0.4 16.04.1997 init code/data tagged
  35. * 0.5 30.07.1997 made HDLC buffers bigger (solves a problem with the
  36. * soundmodem driver)
  37. * 0.6 05.04.1998 add spinlocks
  38. * 0.7 03.08.1999 removed some old compatibility cruft
  39. * 0.8 12.02.2000 adapted to softnet driver interface
  40. */
  41. /*****************************************************************************/
  42. #include <linux/module.h>
  43. #include <linux/types.h>
  44. #include <linux/net.h>
  45. #include <linux/in.h>
  46. #include <linux/if.h>
  47. #include <linux/slab.h>
  48. #include <linux/errno.h>
  49. #include <linux/init.h>
  50. #include <linux/bitops.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/if_arp.h>
  53. #include <linux/skbuff.h>
  54. #include <linux/hdlcdrv.h>
  55. #include <net/ax25.h>
  56. #include <asm/uaccess.h>
  57. #include <linux/crc-ccitt.h>
  58. /* --------------------------------------------------------------------- */
  59. /*
  60. * The name of the card. Is used for messages and in the requests for
  61. * io regions, irqs and dma channels
  62. */
  63. static char ax25_bcast[AX25_ADDR_LEN] =
  64. {'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1};
  65. static char ax25_nocall[AX25_ADDR_LEN] =
  66. {'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1};
  67. /* --------------------------------------------------------------------- */
  68. #define KISS_VERBOSE
  69. /* --------------------------------------------------------------------- */
  70. #define PARAM_TXDELAY 1
  71. #define PARAM_PERSIST 2
  72. #define PARAM_SLOTTIME 3
  73. #define PARAM_TXTAIL 4
  74. #define PARAM_FULLDUP 5
  75. #define PARAM_HARDWARE 6
  76. #define PARAM_RETURN 255
  77. /* --------------------------------------------------------------------- */
  78. /*
  79. * the CRC routines are stolen from WAMPES
  80. * by Dieter Deyke
  81. */
  82. /*---------------------------------------------------------------------------*/
  83. static inline void append_crc_ccitt(unsigned char *buffer, int len)
  84. {
  85. unsigned int crc = crc_ccitt(0xffff, buffer, len) ^ 0xffff;
  86. *buffer++ = crc;
  87. *buffer++ = crc >> 8;
  88. }
  89. /*---------------------------------------------------------------------------*/
  90. static inline int check_crc_ccitt(const unsigned char *buf, int cnt)
  91. {
  92. return (crc_ccitt(0xffff, buf, cnt) & 0xffff) == 0xf0b8;
  93. }
  94. /*---------------------------------------------------------------------------*/
  95. #if 0
  96. static int calc_crc_ccitt(const unsigned char *buf, int cnt)
  97. {
  98. unsigned int crc = 0xffff;
  99. for (; cnt > 0; cnt--)
  100. crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buf++) & 0xff];
  101. crc ^= 0xffff;
  102. return (crc & 0xffff);
  103. }
  104. #endif
  105. /* ---------------------------------------------------------------------- */
  106. #define tenms_to_2flags(s,tenms) ((tenms * s->par.bitrate) / 100 / 16)
  107. /* ---------------------------------------------------------------------- */
  108. /*
  109. * The HDLC routines
  110. */
  111. static int hdlc_rx_add_bytes(struct hdlcdrv_state *s, unsigned int bits,
  112. int num)
  113. {
  114. int added = 0;
  115. while (s->hdlcrx.rx_state && num >= 8) {
  116. if (s->hdlcrx.len >= sizeof(s->hdlcrx.buffer)) {
  117. s->hdlcrx.rx_state = 0;
  118. return 0;
  119. }
  120. *s->hdlcrx.bp++ = bits >> (32-num);
  121. s->hdlcrx.len++;
  122. num -= 8;
  123. added += 8;
  124. }
  125. return added;
  126. }
  127. static void hdlc_rx_flag(struct net_device *dev, struct hdlcdrv_state *s)
  128. {
  129. struct sk_buff *skb;
  130. int pkt_len;
  131. unsigned char *cp;
  132. if (s->hdlcrx.len < 4)
  133. return;
  134. if (!check_crc_ccitt(s->hdlcrx.buffer, s->hdlcrx.len))
  135. return;
  136. pkt_len = s->hdlcrx.len - 2 + 1; /* KISS kludge */
  137. if (!(skb = dev_alloc_skb(pkt_len))) {
  138. printk("%s: memory squeeze, dropping packet\n", dev->name);
  139. s->stats.rx_dropped++;
  140. return;
  141. }
  142. cp = skb_put(skb, pkt_len);
  143. *cp++ = 0; /* KISS kludge */
  144. memcpy(cp, s->hdlcrx.buffer, pkt_len - 1);
  145. skb->protocol = ax25_type_trans(skb, dev);
  146. netif_rx(skb);
  147. dev->last_rx = jiffies;
  148. s->stats.rx_packets++;
  149. }
  150. void hdlcdrv_receiver(struct net_device *dev, struct hdlcdrv_state *s)
  151. {
  152. int i;
  153. unsigned int mask1, mask2, mask3, mask4, mask5, mask6, word;
  154. if (!s || s->magic != HDLCDRV_MAGIC)
  155. return;
  156. if (test_and_set_bit(0, &s->hdlcrx.in_hdlc_rx))
  157. return;
  158. while (!hdlcdrv_hbuf_empty(&s->hdlcrx.hbuf)) {
  159. word = hdlcdrv_hbuf_get(&s->hdlcrx.hbuf);
  160. #ifdef HDLCDRV_DEBUG
  161. hdlcdrv_add_bitbuffer_word(&s->bitbuf_hdlc, word);
  162. #endif /* HDLCDRV_DEBUG */
  163. s->hdlcrx.bitstream >>= 16;
  164. s->hdlcrx.bitstream |= word << 16;
  165. s->hdlcrx.bitbuf >>= 16;
  166. s->hdlcrx.bitbuf |= word << 16;
  167. s->hdlcrx.numbits += 16;
  168. for(i = 15, mask1 = 0x1fc00, mask2 = 0x1fe00, mask3 = 0x0fc00,
  169. mask4 = 0x1f800, mask5 = 0xf800, mask6 = 0xffff;
  170. i >= 0;
  171. i--, mask1 <<= 1, mask2 <<= 1, mask3 <<= 1, mask4 <<= 1,
  172. mask5 <<= 1, mask6 = (mask6 << 1) | 1) {
  173. if ((s->hdlcrx.bitstream & mask1) == mask1)
  174. s->hdlcrx.rx_state = 0; /* abort received */
  175. else if ((s->hdlcrx.bitstream & mask2) == mask3) {
  176. /* flag received */
  177. if (s->hdlcrx.rx_state) {
  178. hdlc_rx_add_bytes(s, s->hdlcrx.bitbuf
  179. << (8+i),
  180. s->hdlcrx.numbits
  181. -8-i);
  182. hdlc_rx_flag(dev, s);
  183. }
  184. s->hdlcrx.len = 0;
  185. s->hdlcrx.bp = s->hdlcrx.buffer;
  186. s->hdlcrx.rx_state = 1;
  187. s->hdlcrx.numbits = i;
  188. } else if ((s->hdlcrx.bitstream & mask4) == mask5) {
  189. /* stuffed bit */
  190. s->hdlcrx.numbits--;
  191. s->hdlcrx.bitbuf = (s->hdlcrx.bitbuf & (~mask6)) |
  192. ((s->hdlcrx.bitbuf & mask6) << 1);
  193. }
  194. }
  195. s->hdlcrx.numbits -= hdlc_rx_add_bytes(s, s->hdlcrx.bitbuf,
  196. s->hdlcrx.numbits);
  197. }
  198. clear_bit(0, &s->hdlcrx.in_hdlc_rx);
  199. }
  200. /* ---------------------------------------------------------------------- */
  201. static inline void do_kiss_params(struct hdlcdrv_state *s,
  202. unsigned char *data, unsigned long len)
  203. {
  204. #ifdef KISS_VERBOSE
  205. #define PKP(a,b) printk(KERN_INFO "hdlcdrv.c: channel params: " a "\n", b)
  206. #else /* KISS_VERBOSE */
  207. #define PKP(a,b)
  208. #endif /* KISS_VERBOSE */
  209. if (len < 2)
  210. return;
  211. switch(data[0]) {
  212. case PARAM_TXDELAY:
  213. s->ch_params.tx_delay = data[1];
  214. PKP("TX delay = %ums", 10 * s->ch_params.tx_delay);
  215. break;
  216. case PARAM_PERSIST:
  217. s->ch_params.ppersist = data[1];
  218. PKP("p persistence = %u", s->ch_params.ppersist);
  219. break;
  220. case PARAM_SLOTTIME:
  221. s->ch_params.slottime = data[1];
  222. PKP("slot time = %ums", s->ch_params.slottime);
  223. break;
  224. case PARAM_TXTAIL:
  225. s->ch_params.tx_tail = data[1];
  226. PKP("TX tail = %ums", s->ch_params.tx_tail);
  227. break;
  228. case PARAM_FULLDUP:
  229. s->ch_params.fulldup = !!data[1];
  230. PKP("%s duplex", s->ch_params.fulldup ? "full" : "half");
  231. break;
  232. default:
  233. break;
  234. }
  235. #undef PKP
  236. }
  237. /* ---------------------------------------------------------------------- */
  238. void hdlcdrv_transmitter(struct net_device *dev, struct hdlcdrv_state *s)
  239. {
  240. unsigned int mask1, mask2, mask3;
  241. int i;
  242. struct sk_buff *skb;
  243. int pkt_len;
  244. if (!s || s->magic != HDLCDRV_MAGIC)
  245. return;
  246. if (test_and_set_bit(0, &s->hdlctx.in_hdlc_tx))
  247. return;
  248. for (;;) {
  249. if (s->hdlctx.numbits >= 16) {
  250. if (hdlcdrv_hbuf_full(&s->hdlctx.hbuf)) {
  251. clear_bit(0, &s->hdlctx.in_hdlc_tx);
  252. return;
  253. }
  254. hdlcdrv_hbuf_put(&s->hdlctx.hbuf, s->hdlctx.bitbuf);
  255. s->hdlctx.bitbuf >>= 16;
  256. s->hdlctx.numbits -= 16;
  257. }
  258. switch (s->hdlctx.tx_state) {
  259. default:
  260. clear_bit(0, &s->hdlctx.in_hdlc_tx);
  261. return;
  262. case 0:
  263. case 1:
  264. if (s->hdlctx.numflags) {
  265. s->hdlctx.numflags--;
  266. s->hdlctx.bitbuf |=
  267. 0x7e7e << s->hdlctx.numbits;
  268. s->hdlctx.numbits += 16;
  269. break;
  270. }
  271. if (s->hdlctx.tx_state == 1) {
  272. clear_bit(0, &s->hdlctx.in_hdlc_tx);
  273. return;
  274. }
  275. if (!(skb = s->skb)) {
  276. int flgs = tenms_to_2flags(s, s->ch_params.tx_tail);
  277. if (flgs < 2)
  278. flgs = 2;
  279. s->hdlctx.tx_state = 1;
  280. s->hdlctx.numflags = flgs;
  281. break;
  282. }
  283. s->skb = NULL;
  284. netif_wake_queue(dev);
  285. pkt_len = skb->len-1; /* strip KISS byte */
  286. if (pkt_len >= HDLCDRV_MAXFLEN || pkt_len < 2) {
  287. s->hdlctx.tx_state = 0;
  288. s->hdlctx.numflags = 1;
  289. dev_kfree_skb_irq(skb);
  290. break;
  291. }
  292. memcpy(s->hdlctx.buffer, skb->data+1, pkt_len);
  293. dev_kfree_skb_irq(skb);
  294. s->hdlctx.bp = s->hdlctx.buffer;
  295. append_crc_ccitt(s->hdlctx.buffer, pkt_len);
  296. s->hdlctx.len = pkt_len+2; /* the appended CRC */
  297. s->hdlctx.tx_state = 2;
  298. s->hdlctx.bitstream = 0;
  299. s->stats.tx_packets++;
  300. break;
  301. case 2:
  302. if (!s->hdlctx.len) {
  303. s->hdlctx.tx_state = 0;
  304. s->hdlctx.numflags = 1;
  305. break;
  306. }
  307. s->hdlctx.len--;
  308. s->hdlctx.bitbuf |= *s->hdlctx.bp <<
  309. s->hdlctx.numbits;
  310. s->hdlctx.bitstream >>= 8;
  311. s->hdlctx.bitstream |= (*s->hdlctx.bp++) << 16;
  312. mask1 = 0x1f000;
  313. mask2 = 0x10000;
  314. mask3 = 0xffffffff >> (31-s->hdlctx.numbits);
  315. s->hdlctx.numbits += 8;
  316. for(i = 0; i < 8; i++, mask1 <<= 1, mask2 <<= 1,
  317. mask3 = (mask3 << 1) | 1) {
  318. if ((s->hdlctx.bitstream & mask1) != mask1)
  319. continue;
  320. s->hdlctx.bitstream &= ~mask2;
  321. s->hdlctx.bitbuf =
  322. (s->hdlctx.bitbuf & mask3) |
  323. ((s->hdlctx.bitbuf &
  324. (~mask3)) << 1);
  325. s->hdlctx.numbits++;
  326. mask3 = (mask3 << 1) | 1;
  327. }
  328. break;
  329. }
  330. }
  331. }
  332. /* ---------------------------------------------------------------------- */
  333. static void start_tx(struct net_device *dev, struct hdlcdrv_state *s)
  334. {
  335. s->hdlctx.tx_state = 0;
  336. s->hdlctx.numflags = tenms_to_2flags(s, s->ch_params.tx_delay);
  337. s->hdlctx.bitbuf = s->hdlctx.bitstream = s->hdlctx.numbits = 0;
  338. hdlcdrv_transmitter(dev, s);
  339. s->hdlctx.ptt = 1;
  340. s->ptt_keyed++;
  341. }
  342. /* ---------------------------------------------------------------------- */
  343. static unsigned short random_seed;
  344. static inline unsigned short random_num(void)
  345. {
  346. random_seed = 28629 * random_seed + 157;
  347. return random_seed;
  348. }
  349. /* ---------------------------------------------------------------------- */
  350. void hdlcdrv_arbitrate(struct net_device *dev, struct hdlcdrv_state *s)
  351. {
  352. if (!s || s->magic != HDLCDRV_MAGIC || s->hdlctx.ptt || !s->skb)
  353. return;
  354. if (s->ch_params.fulldup) {
  355. start_tx(dev, s);
  356. return;
  357. }
  358. if (s->hdlcrx.dcd) {
  359. s->hdlctx.slotcnt = s->ch_params.slottime;
  360. return;
  361. }
  362. if ((--s->hdlctx.slotcnt) > 0)
  363. return;
  364. s->hdlctx.slotcnt = s->ch_params.slottime;
  365. if ((random_num() % 256) > s->ch_params.ppersist)
  366. return;
  367. start_tx(dev, s);
  368. }
  369. /* --------------------------------------------------------------------- */
  370. /*
  371. * ===================== network driver interface =========================
  372. */
  373. static int hdlcdrv_send_packet(struct sk_buff *skb, struct net_device *dev)
  374. {
  375. struct hdlcdrv_state *sm = netdev_priv(dev);
  376. if (skb->data[0] != 0) {
  377. do_kiss_params(sm, skb->data, skb->len);
  378. dev_kfree_skb(skb);
  379. return 0;
  380. }
  381. if (sm->skb)
  382. return -1;
  383. netif_stop_queue(dev);
  384. sm->skb = skb;
  385. return 0;
  386. }
  387. /* --------------------------------------------------------------------- */
  388. static int hdlcdrv_set_mac_address(struct net_device *dev, void *addr)
  389. {
  390. struct sockaddr *sa = (struct sockaddr *)addr;
  391. /* addr is an AX.25 shifted ASCII mac address */
  392. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  393. return 0;
  394. }
  395. /* --------------------------------------------------------------------- */
  396. static struct net_device_stats *hdlcdrv_get_stats(struct net_device *dev)
  397. {
  398. struct hdlcdrv_state *sm = netdev_priv(dev);
  399. /*
  400. * Get the current statistics. This may be called with the
  401. * card open or closed.
  402. */
  403. return &sm->stats;
  404. }
  405. /* --------------------------------------------------------------------- */
  406. /*
  407. * Open/initialize the board. This is called (in the current kernel)
  408. * sometime after booting when the 'ifconfig' program is run.
  409. *
  410. * This routine should set everything up anew at each open, even
  411. * registers that "should" only need to be set once at boot, so that
  412. * there is non-reboot way to recover if something goes wrong.
  413. */
  414. static int hdlcdrv_open(struct net_device *dev)
  415. {
  416. struct hdlcdrv_state *s = netdev_priv(dev);
  417. int i;
  418. if (!s->ops || !s->ops->open)
  419. return -ENODEV;
  420. /*
  421. * initialise some variables
  422. */
  423. s->opened = 1;
  424. s->hdlcrx.hbuf.rd = s->hdlcrx.hbuf.wr = 0;
  425. s->hdlcrx.in_hdlc_rx = 0;
  426. s->hdlcrx.rx_state = 0;
  427. s->hdlctx.hbuf.rd = s->hdlctx.hbuf.wr = 0;
  428. s->hdlctx.in_hdlc_tx = 0;
  429. s->hdlctx.tx_state = 1;
  430. s->hdlctx.numflags = 0;
  431. s->hdlctx.bitstream = s->hdlctx.bitbuf = s->hdlctx.numbits = 0;
  432. s->hdlctx.ptt = 0;
  433. s->hdlctx.slotcnt = s->ch_params.slottime;
  434. s->hdlctx.calibrate = 0;
  435. i = s->ops->open(dev);
  436. if (i)
  437. return i;
  438. netif_start_queue(dev);
  439. return 0;
  440. }
  441. /* --------------------------------------------------------------------- */
  442. /*
  443. * The inverse routine to hdlcdrv_open().
  444. */
  445. static int hdlcdrv_close(struct net_device *dev)
  446. {
  447. struct hdlcdrv_state *s = netdev_priv(dev);
  448. int i = 0;
  449. netif_stop_queue(dev);
  450. if (s->ops && s->ops->close)
  451. i = s->ops->close(dev);
  452. if (s->skb)
  453. dev_kfree_skb(s->skb);
  454. s->skb = NULL;
  455. s->opened = 0;
  456. return i;
  457. }
  458. /* --------------------------------------------------------------------- */
  459. static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  460. {
  461. struct hdlcdrv_state *s = netdev_priv(dev);
  462. struct hdlcdrv_ioctl bi;
  463. if (cmd != SIOCDEVPRIVATE) {
  464. if (s->ops && s->ops->ioctl)
  465. return s->ops->ioctl(dev, ifr, &bi, cmd);
  466. return -ENOIOCTLCMD;
  467. }
  468. if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
  469. return -EFAULT;
  470. switch (bi.cmd) {
  471. default:
  472. if (s->ops && s->ops->ioctl)
  473. return s->ops->ioctl(dev, ifr, &bi, cmd);
  474. return -ENOIOCTLCMD;
  475. case HDLCDRVCTL_GETCHANNELPAR:
  476. bi.data.cp.tx_delay = s->ch_params.tx_delay;
  477. bi.data.cp.tx_tail = s->ch_params.tx_tail;
  478. bi.data.cp.slottime = s->ch_params.slottime;
  479. bi.data.cp.ppersist = s->ch_params.ppersist;
  480. bi.data.cp.fulldup = s->ch_params.fulldup;
  481. break;
  482. case HDLCDRVCTL_SETCHANNELPAR:
  483. if (!capable(CAP_NET_ADMIN))
  484. return -EACCES;
  485. s->ch_params.tx_delay = bi.data.cp.tx_delay;
  486. s->ch_params.tx_tail = bi.data.cp.tx_tail;
  487. s->ch_params.slottime = bi.data.cp.slottime;
  488. s->ch_params.ppersist = bi.data.cp.ppersist;
  489. s->ch_params.fulldup = bi.data.cp.fulldup;
  490. s->hdlctx.slotcnt = 1;
  491. return 0;
  492. case HDLCDRVCTL_GETMODEMPAR:
  493. bi.data.mp.iobase = dev->base_addr;
  494. bi.data.mp.irq = dev->irq;
  495. bi.data.mp.dma = dev->dma;
  496. bi.data.mp.dma2 = s->ptt_out.dma2;
  497. bi.data.mp.seriobase = s->ptt_out.seriobase;
  498. bi.data.mp.pariobase = s->ptt_out.pariobase;
  499. bi.data.mp.midiiobase = s->ptt_out.midiiobase;
  500. break;
  501. case HDLCDRVCTL_SETMODEMPAR:
  502. if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev))
  503. return -EACCES;
  504. dev->base_addr = bi.data.mp.iobase;
  505. dev->irq = bi.data.mp.irq;
  506. dev->dma = bi.data.mp.dma;
  507. s->ptt_out.dma2 = bi.data.mp.dma2;
  508. s->ptt_out.seriobase = bi.data.mp.seriobase;
  509. s->ptt_out.pariobase = bi.data.mp.pariobase;
  510. s->ptt_out.midiiobase = bi.data.mp.midiiobase;
  511. return 0;
  512. case HDLCDRVCTL_GETSTAT:
  513. bi.data.cs.ptt = hdlcdrv_ptt(s);
  514. bi.data.cs.dcd = s->hdlcrx.dcd;
  515. bi.data.cs.ptt_keyed = s->ptt_keyed;
  516. bi.data.cs.tx_packets = s->stats.tx_packets;
  517. bi.data.cs.tx_errors = s->stats.tx_errors;
  518. bi.data.cs.rx_packets = s->stats.rx_packets;
  519. bi.data.cs.rx_errors = s->stats.rx_errors;
  520. break;
  521. case HDLCDRVCTL_OLDGETSTAT:
  522. bi.data.ocs.ptt = hdlcdrv_ptt(s);
  523. bi.data.ocs.dcd = s->hdlcrx.dcd;
  524. bi.data.ocs.ptt_keyed = s->ptt_keyed;
  525. break;
  526. case HDLCDRVCTL_CALIBRATE:
  527. if(!capable(CAP_SYS_RAWIO))
  528. return -EPERM;
  529. s->hdlctx.calibrate = bi.data.calibrate * s->par.bitrate / 16;
  530. return 0;
  531. case HDLCDRVCTL_GETSAMPLES:
  532. #ifndef HDLCDRV_DEBUG
  533. return -EPERM;
  534. #else /* HDLCDRV_DEBUG */
  535. if (s->bitbuf_channel.rd == s->bitbuf_channel.wr)
  536. return -EAGAIN;
  537. bi.data.bits =
  538. s->bitbuf_channel.buffer[s->bitbuf_channel.rd];
  539. s->bitbuf_channel.rd = (s->bitbuf_channel.rd+1) %
  540. sizeof(s->bitbuf_channel.buffer);
  541. break;
  542. #endif /* HDLCDRV_DEBUG */
  543. case HDLCDRVCTL_GETBITS:
  544. #ifndef HDLCDRV_DEBUG
  545. return -EPERM;
  546. #else /* HDLCDRV_DEBUG */
  547. if (s->bitbuf_hdlc.rd == s->bitbuf_hdlc.wr)
  548. return -EAGAIN;
  549. bi.data.bits =
  550. s->bitbuf_hdlc.buffer[s->bitbuf_hdlc.rd];
  551. s->bitbuf_hdlc.rd = (s->bitbuf_hdlc.rd+1) %
  552. sizeof(s->bitbuf_hdlc.buffer);
  553. break;
  554. #endif /* HDLCDRV_DEBUG */
  555. case HDLCDRVCTL_DRIVERNAME:
  556. if (s->ops && s->ops->drvname) {
  557. strncpy(bi.data.drivername, s->ops->drvname,
  558. sizeof(bi.data.drivername));
  559. break;
  560. }
  561. bi.data.drivername[0] = '\0';
  562. break;
  563. }
  564. if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
  565. return -EFAULT;
  566. return 0;
  567. }
  568. /* --------------------------------------------------------------------- */
  569. /*
  570. * Initialize fields in hdlcdrv
  571. */
  572. static void hdlcdrv_setup(struct net_device *dev)
  573. {
  574. static const struct hdlcdrv_channel_params dflt_ch_params = {
  575. 20, 2, 10, 40, 0
  576. };
  577. struct hdlcdrv_state *s = netdev_priv(dev);
  578. /*
  579. * initialize the hdlcdrv_state struct
  580. */
  581. s->ch_params = dflt_ch_params;
  582. s->ptt_keyed = 0;
  583. spin_lock_init(&s->hdlcrx.hbuf.lock);
  584. s->hdlcrx.hbuf.rd = s->hdlcrx.hbuf.wr = 0;
  585. s->hdlcrx.in_hdlc_rx = 0;
  586. s->hdlcrx.rx_state = 0;
  587. spin_lock_init(&s->hdlctx.hbuf.lock);
  588. s->hdlctx.hbuf.rd = s->hdlctx.hbuf.wr = 0;
  589. s->hdlctx.in_hdlc_tx = 0;
  590. s->hdlctx.tx_state = 1;
  591. s->hdlctx.numflags = 0;
  592. s->hdlctx.bitstream = s->hdlctx.bitbuf = s->hdlctx.numbits = 0;
  593. s->hdlctx.ptt = 0;
  594. s->hdlctx.slotcnt = s->ch_params.slottime;
  595. s->hdlctx.calibrate = 0;
  596. #ifdef HDLCDRV_DEBUG
  597. s->bitbuf_channel.rd = s->bitbuf_channel.wr = 0;
  598. s->bitbuf_channel.shreg = 0x80;
  599. s->bitbuf_hdlc.rd = s->bitbuf_hdlc.wr = 0;
  600. s->bitbuf_hdlc.shreg = 0x80;
  601. #endif /* HDLCDRV_DEBUG */
  602. /*
  603. * initialize the device struct
  604. */
  605. dev->open = hdlcdrv_open;
  606. dev->stop = hdlcdrv_close;
  607. dev->do_ioctl = hdlcdrv_ioctl;
  608. dev->hard_start_xmit = hdlcdrv_send_packet;
  609. dev->get_stats = hdlcdrv_get_stats;
  610. /* Fill in the fields of the device structure */
  611. s->skb = NULL;
  612. dev->hard_header = ax25_hard_header;
  613. dev->rebuild_header = ax25_rebuild_header;
  614. dev->set_mac_address = hdlcdrv_set_mac_address;
  615. dev->type = ARPHRD_AX25; /* AF_AX25 device */
  616. dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
  617. dev->mtu = AX25_DEF_PACLEN; /* eth_mtu is the default */
  618. dev->addr_len = AX25_ADDR_LEN; /* sizeof an ax.25 address */
  619. memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
  620. memcpy(dev->dev_addr, ax25_nocall, AX25_ADDR_LEN);
  621. dev->tx_queue_len = 16;
  622. }
  623. /* --------------------------------------------------------------------- */
  624. struct net_device *hdlcdrv_register(const struct hdlcdrv_ops *ops,
  625. unsigned int privsize, const char *ifname,
  626. unsigned int baseaddr, unsigned int irq,
  627. unsigned int dma)
  628. {
  629. struct net_device *dev;
  630. struct hdlcdrv_state *s;
  631. int err;
  632. BUG_ON(ops == NULL);
  633. if (privsize < sizeof(struct hdlcdrv_state))
  634. privsize = sizeof(struct hdlcdrv_state);
  635. dev = alloc_netdev(privsize, ifname, hdlcdrv_setup);
  636. if (!dev)
  637. return ERR_PTR(-ENOMEM);
  638. /*
  639. * initialize part of the hdlcdrv_state struct
  640. */
  641. s = netdev_priv(dev);
  642. s->magic = HDLCDRV_MAGIC;
  643. s->ops = ops;
  644. dev->base_addr = baseaddr;
  645. dev->irq = irq;
  646. dev->dma = dma;
  647. err = register_netdev(dev);
  648. if (err < 0) {
  649. printk(KERN_WARNING "hdlcdrv: cannot register net "
  650. "device %s\n", dev->name);
  651. free_netdev(dev);
  652. dev = ERR_PTR(err);
  653. }
  654. return dev;
  655. }
  656. /* --------------------------------------------------------------------- */
  657. void hdlcdrv_unregister(struct net_device *dev)
  658. {
  659. struct hdlcdrv_state *s = netdev_priv(dev);
  660. BUG_ON(s->magic != HDLCDRV_MAGIC);
  661. if (s->opened && s->ops->close)
  662. s->ops->close(dev);
  663. unregister_netdev(dev);
  664. free_netdev(dev);
  665. }
  666. /* --------------------------------------------------------------------- */
  667. EXPORT_SYMBOL(hdlcdrv_receiver);
  668. EXPORT_SYMBOL(hdlcdrv_transmitter);
  669. EXPORT_SYMBOL(hdlcdrv_arbitrate);
  670. EXPORT_SYMBOL(hdlcdrv_register);
  671. EXPORT_SYMBOL(hdlcdrv_unregister);
  672. /* --------------------------------------------------------------------- */
  673. static int __init hdlcdrv_init_driver(void)
  674. {
  675. printk(KERN_INFO "hdlcdrv: (C) 1996-2000 Thomas Sailer HB9JNX/AE4WA\n");
  676. printk(KERN_INFO "hdlcdrv: version 0.8 compiled " __TIME__ " " __DATE__ "\n");
  677. return 0;
  678. }
  679. /* --------------------------------------------------------------------- */
  680. static void __exit hdlcdrv_cleanup_driver(void)
  681. {
  682. printk(KERN_INFO "hdlcdrv: cleanup\n");
  683. }
  684. /* --------------------------------------------------------------------- */
  685. MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
  686. MODULE_DESCRIPTION("Packet Radio network interface HDLC encoder/decoder");
  687. MODULE_LICENSE("GPL");
  688. module_init(hdlcdrv_init_driver);
  689. module_exit(hdlcdrv_cleanup_driver);
  690. /* --------------------------------------------------------------------- */