hdlcdrv.c 21 KB

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