hdlcdrv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. return NETDEV_TX_LOCKED;
  373. netif_stop_queue(dev);
  374. sm->skb = skb;
  375. return NETDEV_TX_OK;
  376. }
  377. /* --------------------------------------------------------------------- */
  378. static int hdlcdrv_set_mac_address(struct net_device *dev, void *addr)
  379. {
  380. struct sockaddr *sa = (struct sockaddr *)addr;
  381. /* addr is an AX.25 shifted ASCII mac address */
  382. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  383. return 0;
  384. }
  385. /* --------------------------------------------------------------------- */
  386. /*
  387. * Open/initialize the board. This is called (in the current kernel)
  388. * sometime after booting when the 'ifconfig' program is run.
  389. *
  390. * This routine should set everything up anew at each open, even
  391. * registers that "should" only need to be set once at boot, so that
  392. * there is non-reboot way to recover if something goes wrong.
  393. */
  394. static int hdlcdrv_open(struct net_device *dev)
  395. {
  396. struct hdlcdrv_state *s = netdev_priv(dev);
  397. int i;
  398. if (!s->ops || !s->ops->open)
  399. return -ENODEV;
  400. /*
  401. * initialise some variables
  402. */
  403. s->opened = 1;
  404. s->hdlcrx.hbuf.rd = s->hdlcrx.hbuf.wr = 0;
  405. s->hdlcrx.in_hdlc_rx = 0;
  406. s->hdlcrx.rx_state = 0;
  407. s->hdlctx.hbuf.rd = s->hdlctx.hbuf.wr = 0;
  408. s->hdlctx.in_hdlc_tx = 0;
  409. s->hdlctx.tx_state = 1;
  410. s->hdlctx.numflags = 0;
  411. s->hdlctx.bitstream = s->hdlctx.bitbuf = s->hdlctx.numbits = 0;
  412. s->hdlctx.ptt = 0;
  413. s->hdlctx.slotcnt = s->ch_params.slottime;
  414. s->hdlctx.calibrate = 0;
  415. i = s->ops->open(dev);
  416. if (i)
  417. return i;
  418. netif_start_queue(dev);
  419. return 0;
  420. }
  421. /* --------------------------------------------------------------------- */
  422. /*
  423. * The inverse routine to hdlcdrv_open().
  424. */
  425. static int hdlcdrv_close(struct net_device *dev)
  426. {
  427. struct hdlcdrv_state *s = netdev_priv(dev);
  428. int i = 0;
  429. netif_stop_queue(dev);
  430. if (s->ops && s->ops->close)
  431. i = s->ops->close(dev);
  432. if (s->skb)
  433. dev_kfree_skb(s->skb);
  434. s->skb = NULL;
  435. s->opened = 0;
  436. return i;
  437. }
  438. /* --------------------------------------------------------------------- */
  439. static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  440. {
  441. struct hdlcdrv_state *s = netdev_priv(dev);
  442. struct hdlcdrv_ioctl bi;
  443. if (cmd != SIOCDEVPRIVATE) {
  444. if (s->ops && s->ops->ioctl)
  445. return s->ops->ioctl(dev, ifr, &bi, cmd);
  446. return -ENOIOCTLCMD;
  447. }
  448. if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
  449. return -EFAULT;
  450. switch (bi.cmd) {
  451. default:
  452. if (s->ops && s->ops->ioctl)
  453. return s->ops->ioctl(dev, ifr, &bi, cmd);
  454. return -ENOIOCTLCMD;
  455. case HDLCDRVCTL_GETCHANNELPAR:
  456. bi.data.cp.tx_delay = s->ch_params.tx_delay;
  457. bi.data.cp.tx_tail = s->ch_params.tx_tail;
  458. bi.data.cp.slottime = s->ch_params.slottime;
  459. bi.data.cp.ppersist = s->ch_params.ppersist;
  460. bi.data.cp.fulldup = s->ch_params.fulldup;
  461. break;
  462. case HDLCDRVCTL_SETCHANNELPAR:
  463. if (!capable(CAP_NET_ADMIN))
  464. return -EACCES;
  465. s->ch_params.tx_delay = bi.data.cp.tx_delay;
  466. s->ch_params.tx_tail = bi.data.cp.tx_tail;
  467. s->ch_params.slottime = bi.data.cp.slottime;
  468. s->ch_params.ppersist = bi.data.cp.ppersist;
  469. s->ch_params.fulldup = bi.data.cp.fulldup;
  470. s->hdlctx.slotcnt = 1;
  471. return 0;
  472. case HDLCDRVCTL_GETMODEMPAR:
  473. bi.data.mp.iobase = dev->base_addr;
  474. bi.data.mp.irq = dev->irq;
  475. bi.data.mp.dma = dev->dma;
  476. bi.data.mp.dma2 = s->ptt_out.dma2;
  477. bi.data.mp.seriobase = s->ptt_out.seriobase;
  478. bi.data.mp.pariobase = s->ptt_out.pariobase;
  479. bi.data.mp.midiiobase = s->ptt_out.midiiobase;
  480. break;
  481. case HDLCDRVCTL_SETMODEMPAR:
  482. if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev))
  483. return -EACCES;
  484. dev->base_addr = bi.data.mp.iobase;
  485. dev->irq = bi.data.mp.irq;
  486. dev->dma = bi.data.mp.dma;
  487. s->ptt_out.dma2 = bi.data.mp.dma2;
  488. s->ptt_out.seriobase = bi.data.mp.seriobase;
  489. s->ptt_out.pariobase = bi.data.mp.pariobase;
  490. s->ptt_out.midiiobase = bi.data.mp.midiiobase;
  491. return 0;
  492. case HDLCDRVCTL_GETSTAT:
  493. bi.data.cs.ptt = hdlcdrv_ptt(s);
  494. bi.data.cs.dcd = s->hdlcrx.dcd;
  495. bi.data.cs.ptt_keyed = s->ptt_keyed;
  496. bi.data.cs.tx_packets = dev->stats.tx_packets;
  497. bi.data.cs.tx_errors = dev->stats.tx_errors;
  498. bi.data.cs.rx_packets = dev->stats.rx_packets;
  499. bi.data.cs.rx_errors = dev->stats.rx_errors;
  500. break;
  501. case HDLCDRVCTL_OLDGETSTAT:
  502. bi.data.ocs.ptt = hdlcdrv_ptt(s);
  503. bi.data.ocs.dcd = s->hdlcrx.dcd;
  504. bi.data.ocs.ptt_keyed = s->ptt_keyed;
  505. break;
  506. case HDLCDRVCTL_CALIBRATE:
  507. if(!capable(CAP_SYS_RAWIO))
  508. return -EPERM;
  509. if (s->par.bitrate <= 0)
  510. return -EINVAL;
  511. if (bi.data.calibrate > INT_MAX / s->par.bitrate)
  512. return -EINVAL;
  513. s->hdlctx.calibrate = bi.data.calibrate * s->par.bitrate / 16;
  514. return 0;
  515. case HDLCDRVCTL_GETSAMPLES:
  516. #ifndef HDLCDRV_DEBUG
  517. return -EPERM;
  518. #else /* HDLCDRV_DEBUG */
  519. if (s->bitbuf_channel.rd == s->bitbuf_channel.wr)
  520. return -EAGAIN;
  521. bi.data.bits =
  522. s->bitbuf_channel.buffer[s->bitbuf_channel.rd];
  523. s->bitbuf_channel.rd = (s->bitbuf_channel.rd+1) %
  524. sizeof(s->bitbuf_channel.buffer);
  525. break;
  526. #endif /* HDLCDRV_DEBUG */
  527. case HDLCDRVCTL_GETBITS:
  528. #ifndef HDLCDRV_DEBUG
  529. return -EPERM;
  530. #else /* HDLCDRV_DEBUG */
  531. if (s->bitbuf_hdlc.rd == s->bitbuf_hdlc.wr)
  532. return -EAGAIN;
  533. bi.data.bits =
  534. s->bitbuf_hdlc.buffer[s->bitbuf_hdlc.rd];
  535. s->bitbuf_hdlc.rd = (s->bitbuf_hdlc.rd+1) %
  536. sizeof(s->bitbuf_hdlc.buffer);
  537. break;
  538. #endif /* HDLCDRV_DEBUG */
  539. case HDLCDRVCTL_DRIVERNAME:
  540. if (s->ops && s->ops->drvname) {
  541. strncpy(bi.data.drivername, s->ops->drvname,
  542. sizeof(bi.data.drivername));
  543. break;
  544. }
  545. bi.data.drivername[0] = '\0';
  546. break;
  547. }
  548. if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
  549. return -EFAULT;
  550. return 0;
  551. }
  552. /* --------------------------------------------------------------------- */
  553. static const struct net_device_ops hdlcdrv_netdev = {
  554. .ndo_open = hdlcdrv_open,
  555. .ndo_stop = hdlcdrv_close,
  556. .ndo_start_xmit = hdlcdrv_send_packet,
  557. .ndo_do_ioctl = hdlcdrv_ioctl,
  558. .ndo_set_mac_address = hdlcdrv_set_mac_address,
  559. };
  560. /*
  561. * Initialize fields in hdlcdrv
  562. */
  563. static void hdlcdrv_setup(struct net_device *dev)
  564. {
  565. static const struct hdlcdrv_channel_params dflt_ch_params = {
  566. 20, 2, 10, 40, 0
  567. };
  568. struct hdlcdrv_state *s = netdev_priv(dev);
  569. /*
  570. * initialize the hdlcdrv_state struct
  571. */
  572. s->ch_params = dflt_ch_params;
  573. s->ptt_keyed = 0;
  574. spin_lock_init(&s->hdlcrx.hbuf.lock);
  575. s->hdlcrx.hbuf.rd = s->hdlcrx.hbuf.wr = 0;
  576. s->hdlcrx.in_hdlc_rx = 0;
  577. s->hdlcrx.rx_state = 0;
  578. spin_lock_init(&s->hdlctx.hbuf.lock);
  579. s->hdlctx.hbuf.rd = s->hdlctx.hbuf.wr = 0;
  580. s->hdlctx.in_hdlc_tx = 0;
  581. s->hdlctx.tx_state = 1;
  582. s->hdlctx.numflags = 0;
  583. s->hdlctx.bitstream = s->hdlctx.bitbuf = s->hdlctx.numbits = 0;
  584. s->hdlctx.ptt = 0;
  585. s->hdlctx.slotcnt = s->ch_params.slottime;
  586. s->hdlctx.calibrate = 0;
  587. #ifdef HDLCDRV_DEBUG
  588. s->bitbuf_channel.rd = s->bitbuf_channel.wr = 0;
  589. s->bitbuf_channel.shreg = 0x80;
  590. s->bitbuf_hdlc.rd = s->bitbuf_hdlc.wr = 0;
  591. s->bitbuf_hdlc.shreg = 0x80;
  592. #endif /* HDLCDRV_DEBUG */
  593. /* Fill in the fields of the device structure */
  594. s->skb = NULL;
  595. dev->netdev_ops = &hdlcdrv_netdev;
  596. dev->header_ops = &ax25_header_ops;
  597. dev->type = ARPHRD_AX25; /* AF_AX25 device */
  598. dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
  599. dev->mtu = AX25_DEF_PACLEN; /* eth_mtu is the default */
  600. dev->addr_len = AX25_ADDR_LEN; /* sizeof an ax.25 address */
  601. memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
  602. memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
  603. dev->tx_queue_len = 16;
  604. }
  605. /* --------------------------------------------------------------------- */
  606. struct net_device *hdlcdrv_register(const struct hdlcdrv_ops *ops,
  607. unsigned int privsize, const char *ifname,
  608. unsigned int baseaddr, unsigned int irq,
  609. unsigned int dma)
  610. {
  611. struct net_device *dev;
  612. struct hdlcdrv_state *s;
  613. int err;
  614. BUG_ON(ops == NULL);
  615. if (privsize < sizeof(struct hdlcdrv_state))
  616. privsize = sizeof(struct hdlcdrv_state);
  617. dev = alloc_netdev(privsize, ifname, NET_NAME_UNKNOWN, hdlcdrv_setup);
  618. if (!dev)
  619. return ERR_PTR(-ENOMEM);
  620. /*
  621. * initialize part of the hdlcdrv_state struct
  622. */
  623. s = netdev_priv(dev);
  624. s->magic = HDLCDRV_MAGIC;
  625. s->ops = ops;
  626. dev->base_addr = baseaddr;
  627. dev->irq = irq;
  628. dev->dma = dma;
  629. err = register_netdev(dev);
  630. if (err < 0) {
  631. printk(KERN_WARNING "hdlcdrv: cannot register net "
  632. "device %s\n", dev->name);
  633. free_netdev(dev);
  634. dev = ERR_PTR(err);
  635. }
  636. return dev;
  637. }
  638. /* --------------------------------------------------------------------- */
  639. void hdlcdrv_unregister(struct net_device *dev)
  640. {
  641. struct hdlcdrv_state *s = netdev_priv(dev);
  642. BUG_ON(s->magic != HDLCDRV_MAGIC);
  643. if (s->opened && s->ops->close)
  644. s->ops->close(dev);
  645. unregister_netdev(dev);
  646. free_netdev(dev);
  647. }
  648. /* --------------------------------------------------------------------- */
  649. EXPORT_SYMBOL(hdlcdrv_receiver);
  650. EXPORT_SYMBOL(hdlcdrv_transmitter);
  651. EXPORT_SYMBOL(hdlcdrv_arbitrate);
  652. EXPORT_SYMBOL(hdlcdrv_register);
  653. EXPORT_SYMBOL(hdlcdrv_unregister);
  654. /* --------------------------------------------------------------------- */
  655. static int __init hdlcdrv_init_driver(void)
  656. {
  657. printk(KERN_INFO "hdlcdrv: (C) 1996-2000 Thomas Sailer HB9JNX/AE4WA\n");
  658. printk(KERN_INFO "hdlcdrv: version 0.8\n");
  659. return 0;
  660. }
  661. /* --------------------------------------------------------------------- */
  662. static void __exit hdlcdrv_cleanup_driver(void)
  663. {
  664. printk(KERN_INFO "hdlcdrv: cleanup\n");
  665. }
  666. /* --------------------------------------------------------------------- */
  667. MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
  668. MODULE_DESCRIPTION("Packet Radio network interface HDLC encoder/decoder");
  669. MODULE_LICENSE("GPL");
  670. module_init(hdlcdrv_init_driver);
  671. module_exit(hdlcdrv_cleanup_driver);
  672. /* --------------------------------------------------------------------- */