x25_asy.c 18 KB

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