6pack.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * 6pack.c This module implements the 6pack protocol for kernel-based
  3. * devices like TTY. It interfaces between a raw TTY and the
  4. * kernel's AX.25 protocol layers.
  5. *
  6. * Authors: Andreas Könsgen <ajk@comnets.uni-bremen.de>
  7. * Ralf Baechle DL5RB <ralf@linux-mips.org>
  8. *
  9. * Quite a lot of stuff "stolen" by Joerg Reuter from slip.c, written by
  10. *
  11. * Laurence Culhane, <loz@holmes.demon.co.uk>
  12. * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  13. */
  14. #include <linux/module.h>
  15. #include <asm/uaccess.h>
  16. #include <linux/bitops.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/in.h>
  21. #include <linux/tty.h>
  22. #include <linux/errno.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/timer.h>
  25. #include <linux/slab.h>
  26. #include <net/ax25.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/rtnetlink.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/if_arp.h>
  32. #include <linux/init.h>
  33. #include <linux/ip.h>
  34. #include <linux/tcp.h>
  35. #include <linux/semaphore.h>
  36. #include <linux/compat.h>
  37. #include <linux/atomic.h>
  38. #define SIXPACK_VERSION "Revision: 0.3.0"
  39. /* sixpack priority commands */
  40. #define SIXP_SEOF 0x40 /* start and end of a 6pack frame */
  41. #define SIXP_TX_URUN 0x48 /* transmit overrun */
  42. #define SIXP_RX_ORUN 0x50 /* receive overrun */
  43. #define SIXP_RX_BUF_OVL 0x58 /* receive buffer overflow */
  44. #define SIXP_CHKSUM 0xFF /* valid checksum of a 6pack frame */
  45. /* masks to get certain bits out of the status bytes sent by the TNC */
  46. #define SIXP_CMD_MASK 0xC0
  47. #define SIXP_CHN_MASK 0x07
  48. #define SIXP_PRIO_CMD_MASK 0x80
  49. #define SIXP_STD_CMD_MASK 0x40
  50. #define SIXP_PRIO_DATA_MASK 0x38
  51. #define SIXP_TX_MASK 0x20
  52. #define SIXP_RX_MASK 0x10
  53. #define SIXP_RX_DCD_MASK 0x18
  54. #define SIXP_LEDS_ON 0x78
  55. #define SIXP_LEDS_OFF 0x60
  56. #define SIXP_CON 0x08
  57. #define SIXP_STA 0x10
  58. #define SIXP_FOUND_TNC 0xe9
  59. #define SIXP_CON_ON 0x68
  60. #define SIXP_DCD_MASK 0x08
  61. #define SIXP_DAMA_OFF 0
  62. /* default level 2 parameters */
  63. #define SIXP_TXDELAY (HZ/4) /* in 1 s */
  64. #define SIXP_PERSIST 50 /* in 256ths */
  65. #define SIXP_SLOTTIME (HZ/10) /* in 1 s */
  66. #define SIXP_INIT_RESYNC_TIMEOUT (3*HZ/2) /* in 1 s */
  67. #define SIXP_RESYNC_TIMEOUT 5*HZ /* in 1 s */
  68. /* 6pack configuration. */
  69. #define SIXP_NRUNIT 31 /* MAX number of 6pack channels */
  70. #define SIXP_MTU 256 /* Default MTU */
  71. enum sixpack_flags {
  72. SIXPF_ERROR, /* Parity, etc. error */
  73. };
  74. struct sixpack {
  75. /* Various fields. */
  76. struct tty_struct *tty; /* ptr to TTY structure */
  77. struct net_device *dev; /* easy for intr handling */
  78. /* These are pointers to the malloc()ed frame buffers. */
  79. unsigned char *rbuff; /* receiver buffer */
  80. int rcount; /* received chars counter */
  81. unsigned char *xbuff; /* transmitter buffer */
  82. unsigned char *xhead; /* next byte to XMIT */
  83. int xleft; /* bytes left in XMIT queue */
  84. unsigned char raw_buf[4];
  85. unsigned char cooked_buf[400];
  86. unsigned int rx_count;
  87. unsigned int rx_count_cooked;
  88. int mtu; /* Our mtu (to spot changes!) */
  89. int buffsize; /* Max buffers sizes */
  90. unsigned long flags; /* Flag values/ mode etc */
  91. unsigned char mode; /* 6pack mode */
  92. /* 6pack stuff */
  93. unsigned char tx_delay;
  94. unsigned char persistence;
  95. unsigned char slottime;
  96. unsigned char duplex;
  97. unsigned char led_state;
  98. unsigned char status;
  99. unsigned char status1;
  100. unsigned char status2;
  101. unsigned char tx_enable;
  102. unsigned char tnc_state;
  103. struct timer_list tx_t;
  104. struct timer_list resync_t;
  105. atomic_t refcnt;
  106. struct semaphore dead_sem;
  107. spinlock_t lock;
  108. };
  109. #define AX25_6PACK_HEADER_LEN 0
  110. static void sixpack_decode(struct sixpack *, unsigned char[], int);
  111. static int encode_sixpack(unsigned char *, unsigned char *, int, unsigned char);
  112. /*
  113. * Perform the persistence/slottime algorithm for CSMA access. If the
  114. * persistence check was successful, write the data to the serial driver.
  115. * Note that in case of DAMA operation, the data is not sent here.
  116. */
  117. static void sp_xmit_on_air(unsigned long channel)
  118. {
  119. struct sixpack *sp = (struct sixpack *) channel;
  120. int actual, when = sp->slottime;
  121. static unsigned char random;
  122. random = random * 17 + 41;
  123. if (((sp->status1 & SIXP_DCD_MASK) == 0) && (random < sp->persistence)) {
  124. sp->led_state = 0x70;
  125. sp->tty->ops->write(sp->tty, &sp->led_state, 1);
  126. sp->tx_enable = 1;
  127. actual = sp->tty->ops->write(sp->tty, sp->xbuff, sp->status2);
  128. sp->xleft -= actual;
  129. sp->xhead += actual;
  130. sp->led_state = 0x60;
  131. sp->tty->ops->write(sp->tty, &sp->led_state, 1);
  132. sp->status2 = 0;
  133. } else
  134. mod_timer(&sp->tx_t, jiffies + ((when + 1) * HZ) / 100);
  135. }
  136. /* ----> 6pack timer interrupt handler and friends. <---- */
  137. /* Encapsulate one AX.25 frame and stuff into a TTY queue. */
  138. static void sp_encaps(struct sixpack *sp, unsigned char *icp, int len)
  139. {
  140. unsigned char *msg, *p = icp;
  141. int actual, count;
  142. if (len > sp->mtu) { /* sp->mtu = AX25_MTU = max. PACLEN = 256 */
  143. msg = "oversized transmit packet!";
  144. goto out_drop;
  145. }
  146. if (len > sp->mtu) { /* sp->mtu = AX25_MTU = max. PACLEN = 256 */
  147. msg = "oversized transmit packet!";
  148. goto out_drop;
  149. }
  150. if (p[0] > 5) {
  151. msg = "invalid KISS command";
  152. goto out_drop;
  153. }
  154. if ((p[0] != 0) && (len > 2)) {
  155. msg = "KISS control packet too long";
  156. goto out_drop;
  157. }
  158. if ((p[0] == 0) && (len < 15)) {
  159. msg = "bad AX.25 packet to transmit";
  160. goto out_drop;
  161. }
  162. count = encode_sixpack(p, sp->xbuff, len, sp->tx_delay);
  163. set_bit(TTY_DO_WRITE_WAKEUP, &sp->tty->flags);
  164. switch (p[0]) {
  165. case 1: sp->tx_delay = p[1];
  166. return;
  167. case 2: sp->persistence = p[1];
  168. return;
  169. case 3: sp->slottime = p[1];
  170. return;
  171. case 4: /* ignored */
  172. return;
  173. case 5: sp->duplex = p[1];
  174. return;
  175. }
  176. if (p[0] != 0)
  177. return;
  178. /*
  179. * In case of fullduplex or DAMA operation, we don't take care about the
  180. * state of the DCD or of any timers, as the determination of the
  181. * correct time to send is the job of the AX.25 layer. We send
  182. * immediately after data has arrived.
  183. */
  184. if (sp->duplex == 1) {
  185. sp->led_state = 0x70;
  186. sp->tty->ops->write(sp->tty, &sp->led_state, 1);
  187. sp->tx_enable = 1;
  188. actual = sp->tty->ops->write(sp->tty, sp->xbuff, count);
  189. sp->xleft = count - actual;
  190. sp->xhead = sp->xbuff + actual;
  191. sp->led_state = 0x60;
  192. sp->tty->ops->write(sp->tty, &sp->led_state, 1);
  193. } else {
  194. sp->xleft = count;
  195. sp->xhead = sp->xbuff;
  196. sp->status2 = count;
  197. sp_xmit_on_air((unsigned long)sp);
  198. }
  199. return;
  200. out_drop:
  201. sp->dev->stats.tx_dropped++;
  202. netif_start_queue(sp->dev);
  203. if (net_ratelimit())
  204. printk(KERN_DEBUG "%s: %s - dropped.\n", sp->dev->name, msg);
  205. }
  206. /* Encapsulate an IP datagram and kick it into a TTY queue. */
  207. static netdev_tx_t sp_xmit(struct sk_buff *skb, struct net_device *dev)
  208. {
  209. struct sixpack *sp = netdev_priv(dev);
  210. if (skb->protocol == htons(ETH_P_IP))
  211. return ax25_ip_xmit(skb);
  212. spin_lock_bh(&sp->lock);
  213. /* We were not busy, so we are now... :-) */
  214. netif_stop_queue(dev);
  215. dev->stats.tx_bytes += skb->len;
  216. sp_encaps(sp, skb->data, skb->len);
  217. spin_unlock_bh(&sp->lock);
  218. dev_kfree_skb(skb);
  219. return NETDEV_TX_OK;
  220. }
  221. static int sp_open_dev(struct net_device *dev)
  222. {
  223. struct sixpack *sp = netdev_priv(dev);
  224. if (sp->tty == NULL)
  225. return -ENODEV;
  226. return 0;
  227. }
  228. /* Close the low-level part of the 6pack channel. */
  229. static int sp_close(struct net_device *dev)
  230. {
  231. struct sixpack *sp = netdev_priv(dev);
  232. spin_lock_bh(&sp->lock);
  233. if (sp->tty) {
  234. /* TTY discipline is running. */
  235. clear_bit(TTY_DO_WRITE_WAKEUP, &sp->tty->flags);
  236. }
  237. netif_stop_queue(dev);
  238. spin_unlock_bh(&sp->lock);
  239. return 0;
  240. }
  241. static int sp_set_mac_address(struct net_device *dev, void *addr)
  242. {
  243. struct sockaddr_ax25 *sa = addr;
  244. netif_tx_lock_bh(dev);
  245. netif_addr_lock(dev);
  246. memcpy(dev->dev_addr, &sa->sax25_call, AX25_ADDR_LEN);
  247. netif_addr_unlock(dev);
  248. netif_tx_unlock_bh(dev);
  249. return 0;
  250. }
  251. static const struct net_device_ops sp_netdev_ops = {
  252. .ndo_open = sp_open_dev,
  253. .ndo_stop = sp_close,
  254. .ndo_start_xmit = sp_xmit,
  255. .ndo_set_mac_address = sp_set_mac_address,
  256. };
  257. static void sp_setup(struct net_device *dev)
  258. {
  259. /* Finish setting up the DEVICE info. */
  260. dev->netdev_ops = &sp_netdev_ops;
  261. dev->destructor = free_netdev;
  262. dev->mtu = SIXP_MTU;
  263. dev->hard_header_len = AX25_MAX_HEADER_LEN;
  264. dev->header_ops = &ax25_header_ops;
  265. dev->addr_len = AX25_ADDR_LEN;
  266. dev->type = ARPHRD_AX25;
  267. dev->tx_queue_len = 10;
  268. /* Only activated in AX.25 mode */
  269. memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
  270. memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN);
  271. dev->flags = 0;
  272. }
  273. /* Send one completely decapsulated IP datagram to the IP layer. */
  274. /*
  275. * This is the routine that sends the received data to the kernel AX.25.
  276. * 'cmd' is the KISS command. For AX.25 data, it is zero.
  277. */
  278. static void sp_bump(struct sixpack *sp, char cmd)
  279. {
  280. struct sk_buff *skb;
  281. int count;
  282. unsigned char *ptr;
  283. count = sp->rcount + 1;
  284. sp->dev->stats.rx_bytes += count;
  285. if ((skb = dev_alloc_skb(count)) == NULL)
  286. goto out_mem;
  287. ptr = skb_put(skb, count);
  288. *ptr++ = cmd; /* KISS command */
  289. memcpy(ptr, sp->cooked_buf + 1, count);
  290. skb->protocol = ax25_type_trans(skb, sp->dev);
  291. netif_rx(skb);
  292. sp->dev->stats.rx_packets++;
  293. return;
  294. out_mem:
  295. sp->dev->stats.rx_dropped++;
  296. }
  297. /* ----------------------------------------------------------------------- */
  298. /*
  299. * We have a potential race on dereferencing tty->disc_data, because the tty
  300. * layer provides no locking at all - thus one cpu could be running
  301. * sixpack_receive_buf while another calls sixpack_close, which zeroes
  302. * tty->disc_data and frees the memory that sixpack_receive_buf is using. The
  303. * best way to fix this is to use a rwlock in the tty struct, but for now we
  304. * use a single global rwlock for all ttys in ppp line discipline.
  305. */
  306. static DEFINE_RWLOCK(disc_data_lock);
  307. static struct sixpack *sp_get(struct tty_struct *tty)
  308. {
  309. struct sixpack *sp;
  310. read_lock(&disc_data_lock);
  311. sp = tty->disc_data;
  312. if (sp)
  313. atomic_inc(&sp->refcnt);
  314. read_unlock(&disc_data_lock);
  315. return sp;
  316. }
  317. static void sp_put(struct sixpack *sp)
  318. {
  319. if (atomic_dec_and_test(&sp->refcnt))
  320. up(&sp->dead_sem);
  321. }
  322. /*
  323. * Called by the TTY driver when there's room for more data. If we have
  324. * more packets to send, we send them here.
  325. */
  326. static void sixpack_write_wakeup(struct tty_struct *tty)
  327. {
  328. struct sixpack *sp = sp_get(tty);
  329. int actual;
  330. if (!sp)
  331. return;
  332. if (sp->xleft <= 0) {
  333. /* Now serial buffer is almost free & we can start
  334. * transmission of another packet */
  335. sp->dev->stats.tx_packets++;
  336. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  337. sp->tx_enable = 0;
  338. netif_wake_queue(sp->dev);
  339. goto out;
  340. }
  341. if (sp->tx_enable) {
  342. actual = tty->ops->write(tty, sp->xhead, sp->xleft);
  343. sp->xleft -= actual;
  344. sp->xhead += actual;
  345. }
  346. out:
  347. sp_put(sp);
  348. }
  349. /* ----------------------------------------------------------------------- */
  350. /*
  351. * Handle the 'receiver data ready' interrupt.
  352. * This function is called by the 'tty_io' module in the kernel when
  353. * a block of 6pack data has been received, which can now be decapsulated
  354. * and sent on to some IP layer for further processing.
  355. */
  356. static void sixpack_receive_buf(struct tty_struct *tty,
  357. const unsigned char *cp, char *fp, int count)
  358. {
  359. struct sixpack *sp;
  360. unsigned char buf[512];
  361. int count1;
  362. if (!count)
  363. return;
  364. sp = sp_get(tty);
  365. if (!sp)
  366. return;
  367. memcpy(buf, cp, count < sizeof(buf) ? count : sizeof(buf));
  368. /* Read the characters out of the buffer */
  369. count1 = count;
  370. while (count) {
  371. count--;
  372. if (fp && *fp++) {
  373. if (!test_and_set_bit(SIXPF_ERROR, &sp->flags))
  374. sp->dev->stats.rx_errors++;
  375. continue;
  376. }
  377. }
  378. sixpack_decode(sp, buf, count1);
  379. sp_put(sp);
  380. tty_unthrottle(tty);
  381. }
  382. /*
  383. * Try to resync the TNC. Called by the resync timer defined in
  384. * decode_prio_command
  385. */
  386. #define TNC_UNINITIALIZED 0
  387. #define TNC_UNSYNC_STARTUP 1
  388. #define TNC_UNSYNCED 2
  389. #define TNC_IN_SYNC 3
  390. static void __tnc_set_sync_state(struct sixpack *sp, int new_tnc_state)
  391. {
  392. char *msg;
  393. switch (new_tnc_state) {
  394. default: /* gcc oh piece-o-crap ... */
  395. case TNC_UNSYNC_STARTUP:
  396. msg = "Synchronizing with TNC";
  397. break;
  398. case TNC_UNSYNCED:
  399. msg = "Lost synchronization with TNC\n";
  400. break;
  401. case TNC_IN_SYNC:
  402. msg = "Found TNC";
  403. break;
  404. }
  405. sp->tnc_state = new_tnc_state;
  406. printk(KERN_INFO "%s: %s\n", sp->dev->name, msg);
  407. }
  408. static inline void tnc_set_sync_state(struct sixpack *sp, int new_tnc_state)
  409. {
  410. int old_tnc_state = sp->tnc_state;
  411. if (old_tnc_state != new_tnc_state)
  412. __tnc_set_sync_state(sp, new_tnc_state);
  413. }
  414. static void resync_tnc(unsigned long channel)
  415. {
  416. struct sixpack *sp = (struct sixpack *) channel;
  417. static char resync_cmd = 0xe8;
  418. /* clear any data that might have been received */
  419. sp->rx_count = 0;
  420. sp->rx_count_cooked = 0;
  421. /* reset state machine */
  422. sp->status = 1;
  423. sp->status1 = 1;
  424. sp->status2 = 0;
  425. /* resync the TNC */
  426. sp->led_state = 0x60;
  427. sp->tty->ops->write(sp->tty, &sp->led_state, 1);
  428. sp->tty->ops->write(sp->tty, &resync_cmd, 1);
  429. /* Start resync timer again -- the TNC might be still absent */
  430. del_timer(&sp->resync_t);
  431. sp->resync_t.data = (unsigned long) sp;
  432. sp->resync_t.function = resync_tnc;
  433. sp->resync_t.expires = jiffies + SIXP_RESYNC_TIMEOUT;
  434. add_timer(&sp->resync_t);
  435. }
  436. static inline int tnc_init(struct sixpack *sp)
  437. {
  438. unsigned char inbyte = 0xe8;
  439. tnc_set_sync_state(sp, TNC_UNSYNC_STARTUP);
  440. sp->tty->ops->write(sp->tty, &inbyte, 1);
  441. del_timer(&sp->resync_t);
  442. sp->resync_t.data = (unsigned long) sp;
  443. sp->resync_t.function = resync_tnc;
  444. sp->resync_t.expires = jiffies + SIXP_RESYNC_TIMEOUT;
  445. add_timer(&sp->resync_t);
  446. return 0;
  447. }
  448. /*
  449. * Open the high-level part of the 6pack channel.
  450. * This function is called by the TTY module when the
  451. * 6pack line discipline is called for. Because we are
  452. * sure the tty line exists, we only have to link it to
  453. * a free 6pcack channel...
  454. */
  455. static int sixpack_open(struct tty_struct *tty)
  456. {
  457. char *rbuff = NULL, *xbuff = NULL;
  458. struct net_device *dev;
  459. struct sixpack *sp;
  460. unsigned long len;
  461. int err = 0;
  462. if (!capable(CAP_NET_ADMIN))
  463. return -EPERM;
  464. if (tty->ops->write == NULL)
  465. return -EOPNOTSUPP;
  466. dev = alloc_netdev(sizeof(struct sixpack), "sp%d", NET_NAME_UNKNOWN,
  467. sp_setup);
  468. if (!dev) {
  469. err = -ENOMEM;
  470. goto out;
  471. }
  472. sp = netdev_priv(dev);
  473. sp->dev = dev;
  474. spin_lock_init(&sp->lock);
  475. atomic_set(&sp->refcnt, 1);
  476. sema_init(&sp->dead_sem, 0);
  477. /* !!! length of the buffers. MTU is IP MTU, not PACLEN! */
  478. len = dev->mtu * 2;
  479. rbuff = kmalloc(len + 4, GFP_KERNEL);
  480. xbuff = kmalloc(len + 4, GFP_KERNEL);
  481. if (rbuff == NULL || xbuff == NULL) {
  482. err = -ENOBUFS;
  483. goto out_free;
  484. }
  485. spin_lock_bh(&sp->lock);
  486. sp->tty = tty;
  487. sp->rbuff = rbuff;
  488. sp->xbuff = xbuff;
  489. sp->mtu = AX25_MTU + 73;
  490. sp->buffsize = len;
  491. sp->rcount = 0;
  492. sp->rx_count = 0;
  493. sp->rx_count_cooked = 0;
  494. sp->xleft = 0;
  495. sp->flags = 0; /* Clear ESCAPE & ERROR flags */
  496. sp->duplex = 0;
  497. sp->tx_delay = SIXP_TXDELAY;
  498. sp->persistence = SIXP_PERSIST;
  499. sp->slottime = SIXP_SLOTTIME;
  500. sp->led_state = 0x60;
  501. sp->status = 1;
  502. sp->status1 = 1;
  503. sp->status2 = 0;
  504. sp->tx_enable = 0;
  505. netif_start_queue(dev);
  506. init_timer(&sp->tx_t);
  507. sp->tx_t.function = sp_xmit_on_air;
  508. sp->tx_t.data = (unsigned long) sp;
  509. init_timer(&sp->resync_t);
  510. spin_unlock_bh(&sp->lock);
  511. /* Done. We have linked the TTY line to a channel. */
  512. tty->disc_data = sp;
  513. tty->receive_room = 65536;
  514. /* Now we're ready to register. */
  515. err = register_netdev(dev);
  516. if (err)
  517. goto out_free;
  518. tnc_init(sp);
  519. return 0;
  520. out_free:
  521. kfree(xbuff);
  522. kfree(rbuff);
  523. free_netdev(dev);
  524. out:
  525. return err;
  526. }
  527. /*
  528. * Close down a 6pack channel.
  529. * This means flushing out any pending queues, and then restoring the
  530. * TTY line discipline to what it was before it got hooked to 6pack
  531. * (which usually is TTY again).
  532. */
  533. static void sixpack_close(struct tty_struct *tty)
  534. {
  535. struct sixpack *sp;
  536. write_lock_bh(&disc_data_lock);
  537. sp = tty->disc_data;
  538. tty->disc_data = NULL;
  539. write_unlock_bh(&disc_data_lock);
  540. if (!sp)
  541. return;
  542. /*
  543. * We have now ensured that nobody can start using ap from now on, but
  544. * we have to wait for all existing users to finish.
  545. */
  546. if (!atomic_dec_and_test(&sp->refcnt))
  547. down(&sp->dead_sem);
  548. /* We must stop the queue to avoid potentially scribbling
  549. * on the free buffers. The sp->dead_sem is not sufficient
  550. * to protect us from sp->xbuff access.
  551. */
  552. netif_stop_queue(sp->dev);
  553. del_timer_sync(&sp->tx_t);
  554. del_timer_sync(&sp->resync_t);
  555. /* Free all 6pack frame buffers. */
  556. kfree(sp->rbuff);
  557. kfree(sp->xbuff);
  558. unregister_netdev(sp->dev);
  559. }
  560. /* Perform I/O control on an active 6pack channel. */
  561. static int sixpack_ioctl(struct tty_struct *tty, struct file *file,
  562. unsigned int cmd, unsigned long arg)
  563. {
  564. struct sixpack *sp = sp_get(tty);
  565. struct net_device *dev;
  566. unsigned int tmp, err;
  567. if (!sp)
  568. return -ENXIO;
  569. dev = sp->dev;
  570. switch(cmd) {
  571. case SIOCGIFNAME:
  572. err = copy_to_user((void __user *) arg, dev->name,
  573. strlen(dev->name) + 1) ? -EFAULT : 0;
  574. break;
  575. case SIOCGIFENCAP:
  576. err = put_user(0, (int __user *) arg);
  577. break;
  578. case SIOCSIFENCAP:
  579. if (get_user(tmp, (int __user *) arg)) {
  580. err = -EFAULT;
  581. break;
  582. }
  583. sp->mode = tmp;
  584. dev->addr_len = AX25_ADDR_LEN;
  585. dev->hard_header_len = AX25_KISS_HEADER_LEN +
  586. AX25_MAX_HEADER_LEN + 3;
  587. dev->type = ARPHRD_AX25;
  588. err = 0;
  589. break;
  590. case SIOCSIFHWADDR: {
  591. char addr[AX25_ADDR_LEN];
  592. if (copy_from_user(&addr,
  593. (void __user *) arg, AX25_ADDR_LEN)) {
  594. err = -EFAULT;
  595. break;
  596. }
  597. netif_tx_lock_bh(dev);
  598. memcpy(dev->dev_addr, &addr, AX25_ADDR_LEN);
  599. netif_tx_unlock_bh(dev);
  600. err = 0;
  601. break;
  602. }
  603. default:
  604. err = tty_mode_ioctl(tty, file, cmd, arg);
  605. }
  606. sp_put(sp);
  607. return err;
  608. }
  609. #ifdef CONFIG_COMPAT
  610. static long sixpack_compat_ioctl(struct tty_struct * tty, struct file * file,
  611. unsigned int cmd, unsigned long arg)
  612. {
  613. switch (cmd) {
  614. case SIOCGIFNAME:
  615. case SIOCGIFENCAP:
  616. case SIOCSIFENCAP:
  617. case SIOCSIFHWADDR:
  618. return sixpack_ioctl(tty, file, cmd,
  619. (unsigned long)compat_ptr(arg));
  620. }
  621. return -ENOIOCTLCMD;
  622. }
  623. #endif
  624. static struct tty_ldisc_ops sp_ldisc = {
  625. .owner = THIS_MODULE,
  626. .magic = TTY_LDISC_MAGIC,
  627. .name = "6pack",
  628. .open = sixpack_open,
  629. .close = sixpack_close,
  630. .ioctl = sixpack_ioctl,
  631. #ifdef CONFIG_COMPAT
  632. .compat_ioctl = sixpack_compat_ioctl,
  633. #endif
  634. .receive_buf = sixpack_receive_buf,
  635. .write_wakeup = sixpack_write_wakeup,
  636. };
  637. /* Initialize 6pack control device -- register 6pack line discipline */
  638. static const char msg_banner[] __initconst = KERN_INFO \
  639. "AX.25: 6pack driver, " SIXPACK_VERSION "\n";
  640. static const char msg_regfail[] __initconst = KERN_ERR \
  641. "6pack: can't register line discipline (err = %d)\n";
  642. static int __init sixpack_init_driver(void)
  643. {
  644. int status;
  645. printk(msg_banner);
  646. /* Register the provided line protocol discipline */
  647. if ((status = tty_register_ldisc(N_6PACK, &sp_ldisc)) != 0)
  648. printk(msg_regfail, status);
  649. return status;
  650. }
  651. static const char msg_unregfail[] = KERN_ERR \
  652. "6pack: can't unregister line discipline (err = %d)\n";
  653. static void __exit sixpack_exit_driver(void)
  654. {
  655. int ret;
  656. if ((ret = tty_unregister_ldisc(N_6PACK)))
  657. printk(msg_unregfail, ret);
  658. }
  659. /* encode an AX.25 packet into 6pack */
  660. static int encode_sixpack(unsigned char *tx_buf, unsigned char *tx_buf_raw,
  661. int length, unsigned char tx_delay)
  662. {
  663. int count = 0;
  664. unsigned char checksum = 0, buf[400];
  665. int raw_count = 0;
  666. tx_buf_raw[raw_count++] = SIXP_PRIO_CMD_MASK | SIXP_TX_MASK;
  667. tx_buf_raw[raw_count++] = SIXP_SEOF;
  668. buf[0] = tx_delay;
  669. for (count = 1; count < length; count++)
  670. buf[count] = tx_buf[count];
  671. for (count = 0; count < length; count++)
  672. checksum += buf[count];
  673. buf[length] = (unsigned char) 0xff - checksum;
  674. for (count = 0; count <= length; count++) {
  675. if ((count % 3) == 0) {
  676. tx_buf_raw[raw_count++] = (buf[count] & 0x3f);
  677. tx_buf_raw[raw_count] = ((buf[count] >> 2) & 0x30);
  678. } else if ((count % 3) == 1) {
  679. tx_buf_raw[raw_count++] |= (buf[count] & 0x0f);
  680. tx_buf_raw[raw_count] = ((buf[count] >> 2) & 0x3c);
  681. } else {
  682. tx_buf_raw[raw_count++] |= (buf[count] & 0x03);
  683. tx_buf_raw[raw_count++] = (buf[count] >> 2);
  684. }
  685. }
  686. if ((length % 3) != 2)
  687. raw_count++;
  688. tx_buf_raw[raw_count++] = SIXP_SEOF;
  689. return raw_count;
  690. }
  691. /* decode 4 sixpack-encoded bytes into 3 data bytes */
  692. static void decode_data(struct sixpack *sp, unsigned char inbyte)
  693. {
  694. unsigned char *buf;
  695. if (sp->rx_count != 3) {
  696. sp->raw_buf[sp->rx_count++] = inbyte;
  697. return;
  698. }
  699. buf = sp->raw_buf;
  700. sp->cooked_buf[sp->rx_count_cooked++] =
  701. buf[0] | ((buf[1] << 2) & 0xc0);
  702. sp->cooked_buf[sp->rx_count_cooked++] =
  703. (buf[1] & 0x0f) | ((buf[2] << 2) & 0xf0);
  704. sp->cooked_buf[sp->rx_count_cooked++] =
  705. (buf[2] & 0x03) | (inbyte << 2);
  706. sp->rx_count = 0;
  707. }
  708. /* identify and execute a 6pack priority command byte */
  709. static void decode_prio_command(struct sixpack *sp, unsigned char cmd)
  710. {
  711. unsigned char channel;
  712. int actual;
  713. channel = cmd & SIXP_CHN_MASK;
  714. if ((cmd & SIXP_PRIO_DATA_MASK) != 0) { /* idle ? */
  715. /* RX and DCD flags can only be set in the same prio command,
  716. if the DCD flag has been set without the RX flag in the previous
  717. prio command. If DCD has not been set before, something in the
  718. transmission has gone wrong. In this case, RX and DCD are
  719. cleared in order to prevent the decode_data routine from
  720. reading further data that might be corrupt. */
  721. if (((sp->status & SIXP_DCD_MASK) == 0) &&
  722. ((cmd & SIXP_RX_DCD_MASK) == SIXP_RX_DCD_MASK)) {
  723. if (sp->status != 1)
  724. printk(KERN_DEBUG "6pack: protocol violation\n");
  725. else
  726. sp->status = 0;
  727. cmd &= ~SIXP_RX_DCD_MASK;
  728. }
  729. sp->status = cmd & SIXP_PRIO_DATA_MASK;
  730. } else { /* output watchdog char if idle */
  731. if ((sp->status2 != 0) && (sp->duplex == 1)) {
  732. sp->led_state = 0x70;
  733. sp->tty->ops->write(sp->tty, &sp->led_state, 1);
  734. sp->tx_enable = 1;
  735. actual = sp->tty->ops->write(sp->tty, sp->xbuff, sp->status2);
  736. sp->xleft -= actual;
  737. sp->xhead += actual;
  738. sp->led_state = 0x60;
  739. sp->status2 = 0;
  740. }
  741. }
  742. /* needed to trigger the TNC watchdog */
  743. sp->tty->ops->write(sp->tty, &sp->led_state, 1);
  744. /* if the state byte has been received, the TNC is present,
  745. so the resync timer can be reset. */
  746. if (sp->tnc_state == TNC_IN_SYNC) {
  747. del_timer(&sp->resync_t);
  748. sp->resync_t.data = (unsigned long) sp;
  749. sp->resync_t.function = resync_tnc;
  750. sp->resync_t.expires = jiffies + SIXP_INIT_RESYNC_TIMEOUT;
  751. add_timer(&sp->resync_t);
  752. }
  753. sp->status1 = cmd & SIXP_PRIO_DATA_MASK;
  754. }
  755. /* identify and execute a standard 6pack command byte */
  756. static void decode_std_command(struct sixpack *sp, unsigned char cmd)
  757. {
  758. unsigned char checksum = 0, rest = 0, channel;
  759. short i;
  760. channel = cmd & SIXP_CHN_MASK;
  761. switch (cmd & SIXP_CMD_MASK) { /* normal command */
  762. case SIXP_SEOF:
  763. if ((sp->rx_count == 0) && (sp->rx_count_cooked == 0)) {
  764. if ((sp->status & SIXP_RX_DCD_MASK) ==
  765. SIXP_RX_DCD_MASK) {
  766. sp->led_state = 0x68;
  767. sp->tty->ops->write(sp->tty, &sp->led_state, 1);
  768. }
  769. } else {
  770. sp->led_state = 0x60;
  771. /* fill trailing bytes with zeroes */
  772. sp->tty->ops->write(sp->tty, &sp->led_state, 1);
  773. rest = sp->rx_count;
  774. if (rest != 0)
  775. for (i = rest; i <= 3; i++)
  776. decode_data(sp, 0);
  777. if (rest == 2)
  778. sp->rx_count_cooked -= 2;
  779. else if (rest == 3)
  780. sp->rx_count_cooked -= 1;
  781. for (i = 0; i < sp->rx_count_cooked; i++)
  782. checksum += sp->cooked_buf[i];
  783. if (checksum != SIXP_CHKSUM) {
  784. printk(KERN_DEBUG "6pack: bad checksum %2.2x\n", checksum);
  785. } else {
  786. sp->rcount = sp->rx_count_cooked-2;
  787. sp_bump(sp, 0);
  788. }
  789. sp->rx_count_cooked = 0;
  790. }
  791. break;
  792. case SIXP_TX_URUN: printk(KERN_DEBUG "6pack: TX underrun\n");
  793. break;
  794. case SIXP_RX_ORUN: printk(KERN_DEBUG "6pack: RX overrun\n");
  795. break;
  796. case SIXP_RX_BUF_OVL:
  797. printk(KERN_DEBUG "6pack: RX buffer overflow\n");
  798. }
  799. }
  800. /* decode a 6pack packet */
  801. static void
  802. sixpack_decode(struct sixpack *sp, unsigned char *pre_rbuff, int count)
  803. {
  804. unsigned char inbyte;
  805. int count1;
  806. for (count1 = 0; count1 < count; count1++) {
  807. inbyte = pre_rbuff[count1];
  808. if (inbyte == SIXP_FOUND_TNC) {
  809. tnc_set_sync_state(sp, TNC_IN_SYNC);
  810. del_timer(&sp->resync_t);
  811. }
  812. if ((inbyte & SIXP_PRIO_CMD_MASK) != 0)
  813. decode_prio_command(sp, inbyte);
  814. else if ((inbyte & SIXP_STD_CMD_MASK) != 0)
  815. decode_std_command(sp, inbyte);
  816. else if ((sp->status & SIXP_RX_DCD_MASK) == SIXP_RX_DCD_MASK)
  817. decode_data(sp, inbyte);
  818. }
  819. }
  820. MODULE_AUTHOR("Ralf Baechle DO1GRB <ralf@linux-mips.org>");
  821. MODULE_DESCRIPTION("6pack driver for AX.25");
  822. MODULE_LICENSE("GPL");
  823. MODULE_ALIAS_LDISC(N_6PACK);
  824. module_init(sixpack_init_driver);
  825. module_exit(sixpack_exit_driver);