x25_asy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Things to sort out:
  3. *
  4. * o tbusy handling
  5. * o allow users to set the parameters
  6. * o sync/async switching ?
  7. *
  8. * Note: This does _not_ implement CCITT X.25 asynchronous framing
  9. * recommendations. Its primarily for testing purposes. If you wanted
  10. * to do CCITT then in theory all you need is to nick the HDLC async
  11. * checksum routines from ppp.c
  12. * Changes:
  13. *
  14. * 2000-10-29 Henner Eisen lapb_data_indication() return status.
  15. */
  16. #include <linux/module.h>
  17. #include <asm/system.h>
  18. #include <asm/uaccess.h>
  19. #include <linux/bitops.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/in.h>
  24. #include <linux/tty.h>
  25. #include <linux/errno.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/x25.h>
  31. #include <linux/lapb.h>
  32. #include <linux/init.h>
  33. #include "x25_asy.h"
  34. #include <net/x25device.h>
  35. static struct net_device **x25_asy_devs;
  36. static int x25_asy_maxdev = SL_NRUNIT;
  37. module_param(x25_asy_maxdev, int, 0);
  38. MODULE_LICENSE("GPL");
  39. static int x25_asy_esc(unsigned char *p, unsigned char *d, int len);
  40. static void x25_asy_unesc(struct x25_asy *sl, unsigned char c);
  41. static void x25_asy_setup(struct net_device *dev);
  42. /* Find a free X.25 channel, and link in this `tty' line. */
  43. static struct x25_asy *x25_asy_alloc(void)
  44. {
  45. struct net_device *dev = NULL;
  46. struct x25_asy *sl;
  47. int i;
  48. if (x25_asy_devs == NULL)
  49. return NULL; /* Master array missing ! */
  50. for (i = 0; i < x25_asy_maxdev; i++) {
  51. dev = x25_asy_devs[i];
  52. /* Not allocated ? */
  53. if (dev == NULL)
  54. break;
  55. sl = dev->priv;
  56. /* Not in use ? */
  57. if (!test_and_set_bit(SLF_INUSE, &sl->flags))
  58. return sl;
  59. }
  60. /* Sorry, too many, all slots in use */
  61. if (i >= x25_asy_maxdev)
  62. return NULL;
  63. /* If no channels are available, allocate one */
  64. if (!dev) {
  65. char name[IFNAMSIZ];
  66. sprintf(name, "x25asy%d", i);
  67. dev = alloc_netdev(sizeof(struct x25_asy),
  68. name, x25_asy_setup);
  69. if (!dev)
  70. return NULL;
  71. /* Initialize channel control data */
  72. sl = dev->priv;
  73. dev->base_addr = i;
  74. /* register device so that it can be ifconfig'ed */
  75. if (register_netdev(dev) == 0) {
  76. /* (Re-)Set the INUSE bit. Very Important! */
  77. set_bit(SLF_INUSE, &sl->flags);
  78. x25_asy_devs[i] = dev;
  79. return sl;
  80. } else {
  81. printk("x25_asy_alloc() - register_netdev() failure.\n");
  82. free_netdev(dev);
  83. }
  84. }
  85. return NULL;
  86. }
  87. /* Free an X.25 channel. */
  88. static void x25_asy_free(struct x25_asy *sl)
  89. {
  90. /* Free all X.25 frame buffers. */
  91. kfree(sl->rbuff);
  92. sl->rbuff = NULL;
  93. kfree(sl->xbuff);
  94. sl->xbuff = NULL;
  95. if (!test_and_clear_bit(SLF_INUSE, &sl->flags)) {
  96. printk("%s: x25_asy_free for already free unit.\n", sl->dev->name);
  97. }
  98. }
  99. static int x25_asy_change_mtu(struct net_device *dev, int newmtu)
  100. {
  101. struct x25_asy *sl = dev->priv;
  102. unsigned char *xbuff, *rbuff;
  103. int len = 2* newmtu;
  104. xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
  105. rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
  106. if (xbuff == NULL || rbuff == NULL)
  107. {
  108. printk("%s: unable to grow X.25 buffers, MTU change cancelled.\n",
  109. dev->name);
  110. kfree(xbuff);
  111. kfree(rbuff);
  112. return -ENOMEM;
  113. }
  114. spin_lock_bh(&sl->lock);
  115. xbuff = xchg(&sl->xbuff, xbuff);
  116. if (sl->xleft) {
  117. if (sl->xleft <= len) {
  118. memcpy(sl->xbuff, sl->xhead, sl->xleft);
  119. } else {
  120. sl->xleft = 0;
  121. sl->stats.tx_dropped++;
  122. }
  123. }
  124. sl->xhead = sl->xbuff;
  125. rbuff = xchg(&sl->rbuff, rbuff);
  126. if (sl->rcount) {
  127. if (sl->rcount <= len) {
  128. memcpy(sl->rbuff, rbuff, sl->rcount);
  129. } else {
  130. sl->rcount = 0;
  131. sl->stats.rx_over_errors++;
  132. set_bit(SLF_ERROR, &sl->flags);
  133. }
  134. }
  135. dev->mtu = newmtu;
  136. sl->buffsize = len;
  137. spin_unlock_bh(&sl->lock);
  138. kfree(xbuff);
  139. kfree(rbuff);
  140. return 0;
  141. }
  142. /* Set the "sending" flag. This must be atomic, hence the ASM. */
  143. static inline void x25_asy_lock(struct x25_asy *sl)
  144. {
  145. netif_stop_queue(sl->dev);
  146. }
  147. /* Clear the "sending" flag. This must be atomic, hence the ASM. */
  148. static inline void x25_asy_unlock(struct x25_asy *sl)
  149. {
  150. netif_wake_queue(sl->dev);
  151. }
  152. /* Send one completely decapsulated IP datagram to the IP layer. */
  153. static void x25_asy_bump(struct x25_asy *sl)
  154. {
  155. struct sk_buff *skb;
  156. int count;
  157. int err;
  158. count = sl->rcount;
  159. sl->stats.rx_bytes+=count;
  160. skb = dev_alloc_skb(count+1);
  161. if (skb == NULL)
  162. {
  163. printk("%s: memory squeeze, dropping packet.\n", sl->dev->name);
  164. sl->stats.rx_dropped++;
  165. return;
  166. }
  167. skb_push(skb,1); /* LAPB internal control */
  168. memcpy(skb_put(skb,count), sl->rbuff, count);
  169. skb->protocol = x25_type_trans(skb, sl->dev);
  170. if((err=lapb_data_received(skb->dev, skb))!=LAPB_OK)
  171. {
  172. kfree_skb(skb);
  173. printk(KERN_DEBUG "x25_asy: data received err - %d\n",err);
  174. }
  175. else
  176. {
  177. netif_rx(skb);
  178. sl->dev->last_rx = jiffies;
  179. sl->stats.rx_packets++;
  180. }
  181. }
  182. /* Encapsulate one IP datagram and stuff into a TTY queue. */
  183. static void x25_asy_encaps(struct x25_asy *sl, unsigned char *icp, int len)
  184. {
  185. unsigned char *p;
  186. int actual, count, mtu = sl->dev->mtu;
  187. if (len > mtu)
  188. { /* Sigh, shouldn't occur BUT ... */
  189. len = mtu;
  190. printk ("%s: truncating oversized transmit packet!\n", sl->dev->name);
  191. sl->stats.tx_dropped++;
  192. x25_asy_unlock(sl);
  193. return;
  194. }
  195. p = icp;
  196. count = x25_asy_esc(p, (unsigned char *) sl->xbuff, len);
  197. /* Order of next two lines is *very* important.
  198. * When we are sending a little amount of data,
  199. * the transfer may be completed inside driver.write()
  200. * routine, because it's running with interrupts enabled.
  201. * In this case we *never* got WRITE_WAKEUP event,
  202. * if we did not request it before write operation.
  203. * 14 Oct 1994 Dmitry Gorodchanin.
  204. */
  205. sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
  206. actual = sl->tty->driver->write(sl->tty, sl->xbuff, count);
  207. sl->xleft = count - actual;
  208. sl->xhead = sl->xbuff + actual;
  209. /* VSV */
  210. clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
  211. }
  212. /*
  213. * Called by the driver when there's room for more data. If we have
  214. * more packets to send, we send them here.
  215. */
  216. static void x25_asy_write_wakeup(struct tty_struct *tty)
  217. {
  218. int actual;
  219. struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
  220. /* First make sure we're connected. */
  221. if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
  222. return;
  223. if (sl->xleft <= 0)
  224. {
  225. /* Now serial buffer is almost free & we can start
  226. * transmission of another packet */
  227. sl->stats.tx_packets++;
  228. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  229. x25_asy_unlock(sl);
  230. return;
  231. }
  232. actual = tty->driver->write(tty, sl->xhead, sl->xleft);
  233. sl->xleft -= actual;
  234. sl->xhead += actual;
  235. }
  236. static void x25_asy_timeout(struct net_device *dev)
  237. {
  238. struct x25_asy *sl = (struct x25_asy*)(dev->priv);
  239. spin_lock(&sl->lock);
  240. if (netif_queue_stopped(dev)) {
  241. /* May be we must check transmitter timeout here ?
  242. * 14 Oct 1994 Dmitry Gorodchanin.
  243. */
  244. printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
  245. (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft) ?
  246. "bad line quality" : "driver error");
  247. sl->xleft = 0;
  248. sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  249. x25_asy_unlock(sl);
  250. }
  251. spin_unlock(&sl->lock);
  252. }
  253. /* Encapsulate an IP datagram and kick it into a TTY queue. */
  254. static int x25_asy_xmit(struct sk_buff *skb, struct net_device *dev)
  255. {
  256. struct x25_asy *sl = (struct x25_asy*)(dev->priv);
  257. int err;
  258. if (!netif_running(sl->dev)) {
  259. printk("%s: xmit call when iface is down\n", dev->name);
  260. kfree_skb(skb);
  261. return 0;
  262. }
  263. switch(skb->data[0])
  264. {
  265. case 0x00:break;
  266. case 0x01: /* Connection request .. do nothing */
  267. if((err=lapb_connect_request(dev))!=LAPB_OK)
  268. printk(KERN_ERR "x25_asy: lapb_connect_request error - %d\n", err);
  269. kfree_skb(skb);
  270. return 0;
  271. case 0x02: /* Disconnect request .. do nothing - hang up ?? */
  272. if((err=lapb_disconnect_request(dev))!=LAPB_OK)
  273. printk(KERN_ERR "x25_asy: lapb_disconnect_request error - %d\n", err);
  274. default:
  275. kfree_skb(skb);
  276. return 0;
  277. }
  278. skb_pull(skb,1); /* Remove control byte */
  279. /*
  280. * If we are busy already- too bad. We ought to be able
  281. * to queue things at this point, to allow for a little
  282. * frame buffer. Oh well...
  283. * -----------------------------------------------------
  284. * I hate queues in X.25 driver. May be it's efficient,
  285. * but for me latency is more important. ;)
  286. * So, no queues !
  287. * 14 Oct 1994 Dmitry Gorodchanin.
  288. */
  289. if((err=lapb_data_request(dev,skb))!=LAPB_OK)
  290. {
  291. printk(KERN_ERR "lapbeth: lapb_data_request error - %d\n", err);
  292. kfree_skb(skb);
  293. return 0;
  294. }
  295. return 0;
  296. }
  297. /*
  298. * LAPB interface boilerplate
  299. */
  300. /*
  301. * Called when I frame data arrives. We did the work above - throw it
  302. * at the net layer.
  303. */
  304. static int x25_asy_data_indication(struct net_device *dev, struct sk_buff *skb)
  305. {
  306. skb->dev->last_rx = jiffies;
  307. return netif_rx(skb);
  308. }
  309. /*
  310. * Data has emerged from the LAPB protocol machine. We don't handle
  311. * busy cases too well. Its tricky to see how to do this nicely -
  312. * perhaps lapb should allow us to bounce this ?
  313. */
  314. static void x25_asy_data_transmit(struct net_device *dev, struct sk_buff *skb)
  315. {
  316. struct x25_asy *sl=dev->priv;
  317. spin_lock(&sl->lock);
  318. if (netif_queue_stopped(sl->dev) || sl->tty == NULL)
  319. {
  320. spin_unlock(&sl->lock);
  321. printk(KERN_ERR "x25_asy: tbusy drop\n");
  322. kfree_skb(skb);
  323. return;
  324. }
  325. /* We were not busy, so we are now... :-) */
  326. if (skb != NULL)
  327. {
  328. x25_asy_lock(sl);
  329. sl->stats.tx_bytes+=skb->len;
  330. x25_asy_encaps(sl, skb->data, skb->len);
  331. dev_kfree_skb(skb);
  332. }
  333. spin_unlock(&sl->lock);
  334. }
  335. /*
  336. * LAPB connection establish/down information.
  337. */
  338. static void x25_asy_connected(struct net_device *dev, int reason)
  339. {
  340. struct x25_asy *sl = dev->priv;
  341. struct sk_buff *skb;
  342. unsigned char *ptr;
  343. if ((skb = dev_alloc_skb(1)) == NULL) {
  344. printk(KERN_ERR "lapbeth: out of memory\n");
  345. return;
  346. }
  347. ptr = skb_put(skb, 1);
  348. *ptr = 0x01;
  349. skb->protocol = x25_type_trans(skb, sl->dev);
  350. netif_rx(skb);
  351. sl->dev->last_rx = jiffies;
  352. }
  353. static void x25_asy_disconnected(struct net_device *dev, int reason)
  354. {
  355. struct x25_asy *sl = dev->priv;
  356. struct sk_buff *skb;
  357. unsigned char *ptr;
  358. if ((skb = dev_alloc_skb(1)) == NULL) {
  359. printk(KERN_ERR "x25_asy: out of memory\n");
  360. return;
  361. }
  362. ptr = skb_put(skb, 1);
  363. *ptr = 0x02;
  364. skb->protocol = x25_type_trans(skb, sl->dev);
  365. netif_rx(skb);
  366. sl->dev->last_rx = jiffies;
  367. }
  368. static struct lapb_register_struct x25_asy_callbacks = {
  369. .connect_confirmation = x25_asy_connected,
  370. .connect_indication = x25_asy_connected,
  371. .disconnect_confirmation = x25_asy_disconnected,
  372. .disconnect_indication = x25_asy_disconnected,
  373. .data_indication = x25_asy_data_indication,
  374. .data_transmit = x25_asy_data_transmit,
  375. };
  376. /* Open the low-level part of the X.25 channel. Easy! */
  377. static int x25_asy_open(struct net_device *dev)
  378. {
  379. struct x25_asy *sl = (struct x25_asy*)(dev->priv);
  380. unsigned long len;
  381. int err;
  382. if (sl->tty == NULL)
  383. return -ENODEV;
  384. /*
  385. * Allocate the X.25 frame buffers:
  386. *
  387. * rbuff Receive buffer.
  388. * xbuff Transmit buffer.
  389. */
  390. len = dev->mtu * 2;
  391. sl->rbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
  392. if (sl->rbuff == NULL) {
  393. goto norbuff;
  394. }
  395. sl->xbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
  396. if (sl->xbuff == NULL) {
  397. goto noxbuff;
  398. }
  399. sl->buffsize = len;
  400. sl->rcount = 0;
  401. sl->xleft = 0;
  402. sl->flags &= (1 << SLF_INUSE); /* Clear ESCAPE & ERROR flags */
  403. netif_start_queue(dev);
  404. /*
  405. * Now attach LAPB
  406. */
  407. if((err=lapb_register(dev, &x25_asy_callbacks))==LAPB_OK)
  408. return 0;
  409. /* Cleanup */
  410. kfree(sl->xbuff);
  411. noxbuff:
  412. kfree(sl->rbuff);
  413. norbuff:
  414. return -ENOMEM;
  415. }
  416. /* Close the low-level part of the X.25 channel. Easy! */
  417. static int x25_asy_close(struct net_device *dev)
  418. {
  419. struct x25_asy *sl = (struct x25_asy*)(dev->priv);
  420. int err;
  421. spin_lock(&sl->lock);
  422. if (sl->tty)
  423. sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  424. netif_stop_queue(dev);
  425. sl->rcount = 0;
  426. sl->xleft = 0;
  427. if((err=lapb_unregister(dev))!=LAPB_OK)
  428. printk(KERN_ERR "x25_asy_close: lapb_unregister error -%d\n",err);
  429. spin_unlock(&sl->lock);
  430. return 0;
  431. }
  432. static int x25_asy_receive_room(struct tty_struct *tty)
  433. {
  434. return 65536; /* We can handle an infinite amount of data. :-) */
  435. }
  436. /*
  437. * Handle the 'receiver data ready' interrupt.
  438. * This function is called by the 'tty_io' module in the kernel when
  439. * a block of X.25 data has been received, which can now be decapsulated
  440. * and sent on to some IP layer for further processing.
  441. */
  442. static void x25_asy_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
  443. {
  444. struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
  445. if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
  446. return;
  447. /* Read the characters out of the buffer */
  448. while (count--) {
  449. if (fp && *fp++) {
  450. if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
  451. sl->stats.rx_errors++;
  452. }
  453. cp++;
  454. continue;
  455. }
  456. x25_asy_unesc(sl, *cp++);
  457. }
  458. }
  459. /*
  460. * Open the high-level part of the X.25 channel.
  461. * This function is called by the TTY module when the
  462. * X.25 line discipline is called for. Because we are
  463. * sure the tty line exists, we only have to link it to
  464. * a free X.25 channel...
  465. */
  466. static int x25_asy_open_tty(struct tty_struct *tty)
  467. {
  468. struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
  469. int err;
  470. /* First make sure we're not already connected. */
  471. if (sl && sl->magic == X25_ASY_MAGIC) {
  472. return -EEXIST;
  473. }
  474. /* OK. Find a free X.25 channel to use. */
  475. if ((sl = x25_asy_alloc()) == NULL) {
  476. return -ENFILE;
  477. }
  478. sl->tty = tty;
  479. tty->disc_data = sl;
  480. if (tty->driver->flush_buffer) {
  481. tty->driver->flush_buffer(tty);
  482. }
  483. if (tty->ldisc.flush_buffer) {
  484. tty->ldisc.flush_buffer(tty);
  485. }
  486. /* Restore default settings */
  487. sl->dev->type = ARPHRD_X25;
  488. /* Perform the low-level X.25 async init */
  489. if ((err = x25_asy_open(sl->dev)))
  490. return err;
  491. /* Done. We have linked the TTY line to a channel. */
  492. return sl->dev->base_addr;
  493. }
  494. /*
  495. * Close down an X.25 channel.
  496. * This means flushing out any pending queues, and then restoring the
  497. * TTY line discipline to what it was before it got hooked to X.25
  498. * (which usually is TTY again).
  499. */
  500. static void x25_asy_close_tty(struct tty_struct *tty)
  501. {
  502. struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
  503. /* First make sure we're connected. */
  504. if (!sl || sl->magic != X25_ASY_MAGIC)
  505. return;
  506. if (sl->dev->flags & IFF_UP)
  507. {
  508. (void) dev_close(sl->dev);
  509. }
  510. tty->disc_data = NULL;
  511. sl->tty = NULL;
  512. x25_asy_free(sl);
  513. }
  514. static struct net_device_stats *x25_asy_get_stats(struct net_device *dev)
  515. {
  516. struct x25_asy *sl = (struct x25_asy*)(dev->priv);
  517. return &sl->stats;
  518. }
  519. /************************************************************************
  520. * STANDARD X.25 ENCAPSULATION *
  521. ************************************************************************/
  522. int x25_asy_esc(unsigned char *s, unsigned char *d, int len)
  523. {
  524. unsigned char *ptr = d;
  525. unsigned char c;
  526. /*
  527. * Send an initial END character to flush out any
  528. * data that may have accumulated in the receiver
  529. * due to line noise.
  530. */
  531. *ptr++ = X25_END; /* Send 10111110 bit seq */
  532. /*
  533. * For each byte in the packet, send the appropriate
  534. * character sequence, according to the X.25 protocol.
  535. */
  536. while (len-- > 0)
  537. {
  538. switch(c = *s++)
  539. {
  540. case X25_END:
  541. *ptr++ = X25_ESC;
  542. *ptr++ = X25_ESCAPE(X25_END);
  543. break;
  544. case X25_ESC:
  545. *ptr++ = X25_ESC;
  546. *ptr++ = X25_ESCAPE(X25_ESC);
  547. break;
  548. default:
  549. *ptr++ = c;
  550. break;
  551. }
  552. }
  553. *ptr++ = X25_END;
  554. return (ptr - d);
  555. }
  556. static void x25_asy_unesc(struct x25_asy *sl, unsigned char s)
  557. {
  558. switch(s)
  559. {
  560. case X25_END:
  561. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))
  562. {
  563. x25_asy_bump(sl);
  564. }
  565. clear_bit(SLF_ESCAPE, &sl->flags);
  566. sl->rcount = 0;
  567. return;
  568. case X25_ESC:
  569. set_bit(SLF_ESCAPE, &sl->flags);
  570. return;
  571. case X25_ESCAPE(X25_ESC):
  572. case X25_ESCAPE(X25_END):
  573. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  574. s = X25_UNESCAPE(s);
  575. break;
  576. }
  577. if (!test_bit(SLF_ERROR, &sl->flags))
  578. {
  579. if (sl->rcount < sl->buffsize)
  580. {
  581. sl->rbuff[sl->rcount++] = s;
  582. return;
  583. }
  584. sl->stats.rx_over_errors++;
  585. set_bit(SLF_ERROR, &sl->flags);
  586. }
  587. }
  588. /* Perform I/O control on an active X.25 channel. */
  589. static int x25_asy_ioctl(struct tty_struct *tty, struct file *file,
  590. unsigned int cmd, unsigned long arg)
  591. {
  592. struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
  593. /* First make sure we're connected. */
  594. if (!sl || sl->magic != X25_ASY_MAGIC)
  595. return -EINVAL;
  596. switch(cmd) {
  597. case SIOCGIFNAME:
  598. if (copy_to_user((void __user *)arg, sl->dev->name,
  599. strlen(sl->dev->name) + 1))
  600. return -EFAULT;
  601. return 0;
  602. case SIOCSIFHWADDR:
  603. return -EINVAL;
  604. /* Allow stty to read, but not set, the serial port */
  605. case TCGETS:
  606. case TCGETA:
  607. return n_tty_ioctl(tty, file, cmd, arg);
  608. default:
  609. return -ENOIOCTLCMD;
  610. }
  611. }
  612. static int x25_asy_open_dev(struct net_device *dev)
  613. {
  614. struct x25_asy *sl = (struct x25_asy*)(dev->priv);
  615. if(sl->tty==NULL)
  616. return -ENODEV;
  617. return 0;
  618. }
  619. /* Initialise the X.25 driver. Called by the device init code */
  620. static void x25_asy_setup(struct net_device *dev)
  621. {
  622. struct x25_asy *sl = dev->priv;
  623. sl->magic = X25_ASY_MAGIC;
  624. sl->dev = dev;
  625. spin_lock_init(&sl->lock);
  626. set_bit(SLF_INUSE, &sl->flags);
  627. /*
  628. * Finish setting up the DEVICE info.
  629. */
  630. dev->mtu = SL_MTU;
  631. dev->hard_start_xmit = x25_asy_xmit;
  632. dev->tx_timeout = x25_asy_timeout;
  633. dev->watchdog_timeo = HZ*20;
  634. dev->open = x25_asy_open_dev;
  635. dev->stop = x25_asy_close;
  636. dev->get_stats = x25_asy_get_stats;
  637. dev->change_mtu = x25_asy_change_mtu;
  638. dev->hard_header_len = 0;
  639. dev->addr_len = 0;
  640. dev->type = ARPHRD_X25;
  641. dev->tx_queue_len = 10;
  642. /* New-style flags. */
  643. dev->flags = IFF_NOARP;
  644. }
  645. static struct tty_ldisc x25_ldisc = {
  646. .owner = THIS_MODULE,
  647. .magic = TTY_LDISC_MAGIC,
  648. .name = "X.25",
  649. .open = x25_asy_open_tty,
  650. .close = x25_asy_close_tty,
  651. .ioctl = x25_asy_ioctl,
  652. .receive_buf = x25_asy_receive_buf,
  653. .receive_room = x25_asy_receive_room,
  654. .write_wakeup = x25_asy_write_wakeup,
  655. };
  656. static int __init init_x25_asy(void)
  657. {
  658. if (x25_asy_maxdev < 4)
  659. x25_asy_maxdev = 4; /* Sanity */
  660. printk(KERN_INFO "X.25 async: version 0.00 ALPHA "
  661. "(dynamic channels, max=%d).\n", x25_asy_maxdev );
  662. x25_asy_devs = kmalloc(sizeof(struct net_device *)*x25_asy_maxdev,
  663. GFP_KERNEL);
  664. if (!x25_asy_devs) {
  665. printk(KERN_WARNING "X25 async: Can't allocate x25_asy_ctrls[] "
  666. "array! Uaargh! (-> No X.25 available)\n");
  667. return -ENOMEM;
  668. }
  669. memset(x25_asy_devs, 0, sizeof(struct net_device *)*x25_asy_maxdev);
  670. return tty_register_ldisc(N_X25, &x25_ldisc);
  671. }
  672. static void __exit exit_x25_asy(void)
  673. {
  674. struct net_device *dev;
  675. int i;
  676. for (i = 0; i < x25_asy_maxdev; i++) {
  677. dev = x25_asy_devs[i];
  678. if (dev) {
  679. struct x25_asy *sl = dev->priv;
  680. spin_lock_bh(&sl->lock);
  681. if (sl->tty)
  682. tty_hangup(sl->tty);
  683. spin_unlock_bh(&sl->lock);
  684. /*
  685. * VSV = if dev->start==0, then device
  686. * unregistered while close proc.
  687. */
  688. unregister_netdev(dev);
  689. free_netdev(dev);
  690. }
  691. }
  692. kfree(x25_asy_devs);
  693. tty_unregister_ldisc(N_X25);
  694. }
  695. module_init(init_x25_asy);
  696. module_exit(exit_x25_asy);