x25_asy.c 19 KB

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