x25_asy.c 19 KB

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