n_hdlc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /* generic HDLC line discipline for Linux
  3. *
  4. * Written by Paul Fulghum paulkf@microgate.com
  5. * for Microgate Corporation
  6. *
  7. * Microgate and SyncLink are registered trademarks of Microgate Corporation
  8. *
  9. * Adapted from ppp.c, written by Michael Callahan <callahan@maths.ox.ac.uk>,
  10. * Al Longyear <longyear@netcom.com>,
  11. * Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
  12. *
  13. * Original release 01/11/99
  14. *
  15. * This module implements the tty line discipline N_HDLC for use with
  16. * tty device drivers that support bit-synchronous HDLC communications.
  17. *
  18. * All HDLC data is frame oriented which means:
  19. *
  20. * 1. tty write calls represent one complete transmit frame of data
  21. * The device driver should accept the complete frame or none of
  22. * the frame (busy) in the write method. Each write call should have
  23. * a byte count in the range of 2-65535 bytes (2 is min HDLC frame
  24. * with 1 addr byte and 1 ctrl byte). The max byte count of 65535
  25. * should include any crc bytes required. For example, when using
  26. * CCITT CRC32, 4 crc bytes are required, so the maximum size frame
  27. * the application may transmit is limited to 65531 bytes. For CCITT
  28. * CRC16, the maximum application frame size would be 65533.
  29. *
  30. *
  31. * 2. receive callbacks from the device driver represents
  32. * one received frame. The device driver should bypass
  33. * the tty flip buffer and call the line discipline receive
  34. * callback directly to avoid fragmenting or concatenating
  35. * multiple frames into a single receive callback.
  36. *
  37. * The HDLC line discipline queues the receive frames in separate
  38. * buffers so complete receive frames can be returned by the
  39. * tty read calls.
  40. *
  41. * 3. tty read calls returns an entire frame of data or nothing.
  42. *
  43. * 4. all send and receive data is considered raw. No processing
  44. * or translation is performed by the line discipline, regardless
  45. * of the tty flags
  46. *
  47. * 5. When line discipline is queried for the amount of receive
  48. * data available (FIOC), 0 is returned if no data available,
  49. * otherwise the count of the next available frame is returned.
  50. * (instead of the sum of all received frame counts).
  51. *
  52. * These conventions allow the standard tty programming interface
  53. * to be used for synchronous HDLC applications when used with
  54. * this line discipline (or another line discipline that is frame
  55. * oriented such as N_PPP).
  56. *
  57. * The SyncLink driver (synclink.c) implements both asynchronous
  58. * (using standard line discipline N_TTY) and synchronous HDLC
  59. * (using N_HDLC) communications, with the latter using the above
  60. * conventions.
  61. *
  62. * This implementation is very basic and does not maintain
  63. * any statistics. The main point is to enforce the raw data
  64. * and frame orientation of HDLC communications.
  65. *
  66. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  67. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  68. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  69. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  70. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  71. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  72. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  73. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  74. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  75. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  76. * OF THE POSSIBILITY OF SUCH DAMAGE.
  77. */
  78. #define HDLC_MAGIC 0x239e
  79. #include <linux/module.h>
  80. #include <linux/init.h>
  81. #include <linux/kernel.h>
  82. #include <linux/sched.h>
  83. #include <linux/types.h>
  84. #include <linux/fcntl.h>
  85. #include <linux/interrupt.h>
  86. #include <linux/ptrace.h>
  87. #undef VERSION
  88. #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
  89. #include <linux/poll.h>
  90. #include <linux/in.h>
  91. #include <linux/ioctl.h>
  92. #include <linux/slab.h>
  93. #include <linux/tty.h>
  94. #include <linux/errno.h>
  95. #include <linux/string.h> /* used in new tty drivers */
  96. #include <linux/signal.h> /* used in new tty drivers */
  97. #include <linux/if.h>
  98. #include <linux/bitops.h>
  99. #include <asm/termios.h>
  100. #include <linux/uaccess.h>
  101. /*
  102. * Buffers for individual HDLC frames
  103. */
  104. #define MAX_HDLC_FRAME_SIZE 65535
  105. #define DEFAULT_RX_BUF_COUNT 10
  106. #define MAX_RX_BUF_COUNT 60
  107. #define DEFAULT_TX_BUF_COUNT 3
  108. struct n_hdlc_buf {
  109. struct list_head list_item;
  110. int count;
  111. char buf[1];
  112. };
  113. #define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
  114. struct n_hdlc_buf_list {
  115. struct list_head list;
  116. int count;
  117. spinlock_t spinlock;
  118. };
  119. /**
  120. * struct n_hdlc - per device instance data structure
  121. * @magic - magic value for structure
  122. * @flags - miscellaneous control flags
  123. * @tty - ptr to TTY structure
  124. * @backup_tty - TTY to use if tty gets closed
  125. * @tbusy - reentrancy flag for tx wakeup code
  126. * @woke_up - FIXME: describe this field
  127. * @tx_buf_list - list of pending transmit frame buffers
  128. * @rx_buf_list - list of received frame buffers
  129. * @tx_free_buf_list - list unused transmit frame buffers
  130. * @rx_free_buf_list - list unused received frame buffers
  131. */
  132. struct n_hdlc {
  133. int magic;
  134. __u32 flags;
  135. struct tty_struct *tty;
  136. struct tty_struct *backup_tty;
  137. int tbusy;
  138. int woke_up;
  139. struct n_hdlc_buf_list tx_buf_list;
  140. struct n_hdlc_buf_list rx_buf_list;
  141. struct n_hdlc_buf_list tx_free_buf_list;
  142. struct n_hdlc_buf_list rx_free_buf_list;
  143. };
  144. /*
  145. * HDLC buffer list manipulation functions
  146. */
  147. static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
  148. struct n_hdlc_buf *buf);
  149. static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
  150. struct n_hdlc_buf *buf);
  151. static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
  152. /* Local functions */
  153. static struct n_hdlc *n_hdlc_alloc (void);
  154. /* debug level can be set by insmod for debugging purposes */
  155. #define DEBUG_LEVEL_INFO 1
  156. static int debuglevel;
  157. /* max frame size for memory allocations */
  158. static int maxframe = 4096;
  159. /* TTY callbacks */
  160. static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
  161. __u8 __user *buf, size_t nr);
  162. static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
  163. const unsigned char *buf, size_t nr);
  164. static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
  165. unsigned int cmd, unsigned long arg);
  166. static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
  167. poll_table *wait);
  168. static int n_hdlc_tty_open(struct tty_struct *tty);
  169. static void n_hdlc_tty_close(struct tty_struct *tty);
  170. static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
  171. char *fp, int count);
  172. static void n_hdlc_tty_wakeup(struct tty_struct *tty);
  173. #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
  174. #define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
  175. #define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
  176. static void flush_rx_queue(struct tty_struct *tty)
  177. {
  178. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  179. struct n_hdlc_buf *buf;
  180. while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
  181. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
  182. }
  183. static void flush_tx_queue(struct tty_struct *tty)
  184. {
  185. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  186. struct n_hdlc_buf *buf;
  187. while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
  188. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
  189. }
  190. static struct tty_ldisc_ops n_hdlc_ldisc = {
  191. .owner = THIS_MODULE,
  192. .magic = TTY_LDISC_MAGIC,
  193. .name = "hdlc",
  194. .open = n_hdlc_tty_open,
  195. .close = n_hdlc_tty_close,
  196. .read = n_hdlc_tty_read,
  197. .write = n_hdlc_tty_write,
  198. .ioctl = n_hdlc_tty_ioctl,
  199. .poll = n_hdlc_tty_poll,
  200. .receive_buf = n_hdlc_tty_receive,
  201. .write_wakeup = n_hdlc_tty_wakeup,
  202. .flush_buffer = flush_rx_queue,
  203. };
  204. /**
  205. * n_hdlc_release - release an n_hdlc per device line discipline info structure
  206. * @n_hdlc - per device line discipline info structure
  207. */
  208. static void n_hdlc_release(struct n_hdlc *n_hdlc)
  209. {
  210. struct tty_struct *tty = n_hdlc2tty (n_hdlc);
  211. struct n_hdlc_buf *buf;
  212. if (debuglevel >= DEBUG_LEVEL_INFO)
  213. printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
  214. /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
  215. wake_up_interruptible (&tty->read_wait);
  216. wake_up_interruptible (&tty->write_wait);
  217. if (tty->disc_data == n_hdlc)
  218. tty->disc_data = NULL; /* Break the tty->n_hdlc link */
  219. /* Release transmit and receive buffers */
  220. for(;;) {
  221. buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
  222. if (buf) {
  223. kfree(buf);
  224. } else
  225. break;
  226. }
  227. for(;;) {
  228. buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
  229. if (buf) {
  230. kfree(buf);
  231. } else
  232. break;
  233. }
  234. for(;;) {
  235. buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
  236. if (buf) {
  237. kfree(buf);
  238. } else
  239. break;
  240. }
  241. for(;;) {
  242. buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
  243. if (buf) {
  244. kfree(buf);
  245. } else
  246. break;
  247. }
  248. kfree(n_hdlc);
  249. } /* end of n_hdlc_release() */
  250. /**
  251. * n_hdlc_tty_close - line discipline close
  252. * @tty - pointer to tty info structure
  253. *
  254. * Called when the line discipline is changed to something
  255. * else, the tty is closed, or the tty detects a hangup.
  256. */
  257. static void n_hdlc_tty_close(struct tty_struct *tty)
  258. {
  259. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  260. if (debuglevel >= DEBUG_LEVEL_INFO)
  261. printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
  262. if (n_hdlc != NULL) {
  263. if (n_hdlc->magic != HDLC_MAGIC) {
  264. printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
  265. return;
  266. }
  267. #if defined(TTY_NO_WRITE_SPLIT)
  268. clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
  269. #endif
  270. tty->disc_data = NULL;
  271. if (tty == n_hdlc->backup_tty)
  272. n_hdlc->backup_tty = NULL;
  273. if (tty != n_hdlc->tty)
  274. return;
  275. if (n_hdlc->backup_tty) {
  276. n_hdlc->tty = n_hdlc->backup_tty;
  277. } else {
  278. n_hdlc_release (n_hdlc);
  279. }
  280. }
  281. if (debuglevel >= DEBUG_LEVEL_INFO)
  282. printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
  283. } /* end of n_hdlc_tty_close() */
  284. /**
  285. * n_hdlc_tty_open - called when line discipline changed to n_hdlc
  286. * @tty - pointer to tty info structure
  287. *
  288. * Returns 0 if success, otherwise error code
  289. */
  290. static int n_hdlc_tty_open (struct tty_struct *tty)
  291. {
  292. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  293. if (debuglevel >= DEBUG_LEVEL_INFO)
  294. printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
  295. __FILE__,__LINE__,
  296. tty->name);
  297. /* There should not be an existing table for this slot. */
  298. if (n_hdlc) {
  299. printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
  300. return -EEXIST;
  301. }
  302. n_hdlc = n_hdlc_alloc();
  303. if (!n_hdlc) {
  304. printk (KERN_ERR "n_hdlc_alloc failed\n");
  305. return -ENFILE;
  306. }
  307. tty->disc_data = n_hdlc;
  308. n_hdlc->tty = tty;
  309. tty->receive_room = 65536;
  310. #if defined(TTY_NO_WRITE_SPLIT)
  311. /* change tty_io write() to not split large writes into 8K chunks */
  312. set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
  313. #endif
  314. /* flush receive data from driver */
  315. tty_driver_flush_buffer(tty);
  316. if (debuglevel >= DEBUG_LEVEL_INFO)
  317. printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
  318. return 0;
  319. } /* end of n_tty_hdlc_open() */
  320. /**
  321. * n_hdlc_send_frames - send frames on pending send buffer list
  322. * @n_hdlc - pointer to ldisc instance data
  323. * @tty - pointer to tty instance data
  324. *
  325. * Send frames on pending send buffer list until the driver does not accept a
  326. * frame (busy) this function is called after adding a frame to the send buffer
  327. * list and by the tty wakeup callback.
  328. */
  329. static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
  330. {
  331. register int actual;
  332. unsigned long flags;
  333. struct n_hdlc_buf *tbuf;
  334. if (debuglevel >= DEBUG_LEVEL_INFO)
  335. printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
  336. check_again:
  337. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
  338. if (n_hdlc->tbusy) {
  339. n_hdlc->woke_up = 1;
  340. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
  341. return;
  342. }
  343. n_hdlc->tbusy = 1;
  344. n_hdlc->woke_up = 0;
  345. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
  346. tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
  347. while (tbuf) {
  348. if (debuglevel >= DEBUG_LEVEL_INFO)
  349. printk("%s(%d)sending frame %p, count=%d\n",
  350. __FILE__,__LINE__,tbuf,tbuf->count);
  351. /* Send the next block of data to device */
  352. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  353. actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
  354. /* rollback was possible and has been done */
  355. if (actual == -ERESTARTSYS) {
  356. n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
  357. break;
  358. }
  359. /* if transmit error, throw frame away by */
  360. /* pretending it was accepted by driver */
  361. if (actual < 0)
  362. actual = tbuf->count;
  363. if (actual == tbuf->count) {
  364. if (debuglevel >= DEBUG_LEVEL_INFO)
  365. printk("%s(%d)frame %p completed\n",
  366. __FILE__,__LINE__,tbuf);
  367. /* free current transmit buffer */
  368. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
  369. /* wait up sleeping writers */
  370. wake_up_interruptible(&tty->write_wait);
  371. /* get next pending transmit buffer */
  372. tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
  373. } else {
  374. if (debuglevel >= DEBUG_LEVEL_INFO)
  375. printk("%s(%d)frame %p pending\n",
  376. __FILE__,__LINE__,tbuf);
  377. /*
  378. * the buffer was not accepted by driver,
  379. * return it back into tx queue
  380. */
  381. n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
  382. break;
  383. }
  384. }
  385. if (!tbuf)
  386. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  387. /* Clear the re-entry flag */
  388. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
  389. n_hdlc->tbusy = 0;
  390. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
  391. if (n_hdlc->woke_up)
  392. goto check_again;
  393. if (debuglevel >= DEBUG_LEVEL_INFO)
  394. printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
  395. } /* end of n_hdlc_send_frames() */
  396. /**
  397. * n_hdlc_tty_wakeup - Callback for transmit wakeup
  398. * @tty - pointer to associated tty instance data
  399. *
  400. * Called when low level device driver can accept more send data.
  401. */
  402. static void n_hdlc_tty_wakeup(struct tty_struct *tty)
  403. {
  404. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  405. if (debuglevel >= DEBUG_LEVEL_INFO)
  406. printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
  407. if (!n_hdlc)
  408. return;
  409. if (tty != n_hdlc->tty) {
  410. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  411. return;
  412. }
  413. n_hdlc_send_frames (n_hdlc, tty);
  414. } /* end of n_hdlc_tty_wakeup() */
  415. /**
  416. * n_hdlc_tty_receive - Called by tty driver when receive data is available
  417. * @tty - pointer to tty instance data
  418. * @data - pointer to received data
  419. * @flags - pointer to flags for data
  420. * @count - count of received data in bytes
  421. *
  422. * Called by tty low level driver when receive data is available. Data is
  423. * interpreted as one HDLC frame.
  424. */
  425. static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
  426. char *flags, int count)
  427. {
  428. register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  429. register struct n_hdlc_buf *buf;
  430. if (debuglevel >= DEBUG_LEVEL_INFO)
  431. printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
  432. __FILE__,__LINE__, count);
  433. /* This can happen if stuff comes in on the backup tty */
  434. if (!n_hdlc || tty != n_hdlc->tty)
  435. return;
  436. /* verify line is using HDLC discipline */
  437. if (n_hdlc->magic != HDLC_MAGIC) {
  438. printk("%s(%d) line not using HDLC discipline\n",
  439. __FILE__,__LINE__);
  440. return;
  441. }
  442. if ( count>maxframe ) {
  443. if (debuglevel >= DEBUG_LEVEL_INFO)
  444. printk("%s(%d) rx count>maxframesize, data discarded\n",
  445. __FILE__,__LINE__);
  446. return;
  447. }
  448. /* get a free HDLC buffer */
  449. buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
  450. if (!buf) {
  451. /* no buffers in free list, attempt to allocate another rx buffer */
  452. /* unless the maximum count has been reached */
  453. if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
  454. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC);
  455. }
  456. if (!buf) {
  457. if (debuglevel >= DEBUG_LEVEL_INFO)
  458. printk("%s(%d) no more rx buffers, data discarded\n",
  459. __FILE__,__LINE__);
  460. return;
  461. }
  462. /* copy received data to HDLC buffer */
  463. memcpy(buf->buf,data,count);
  464. buf->count=count;
  465. /* add HDLC buffer to list of received frames */
  466. n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
  467. /* wake up any blocked reads and perform async signalling */
  468. wake_up_interruptible (&tty->read_wait);
  469. if (n_hdlc->tty->fasync != NULL)
  470. kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
  471. } /* end of n_hdlc_tty_receive() */
  472. /**
  473. * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
  474. * @tty - pointer to tty instance data
  475. * @file - pointer to open file object
  476. * @buf - pointer to returned data buffer
  477. * @nr - size of returned data buffer
  478. *
  479. * Returns the number of bytes returned or error code.
  480. */
  481. static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
  482. __u8 __user *buf, size_t nr)
  483. {
  484. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  485. int ret = 0;
  486. struct n_hdlc_buf *rbuf;
  487. DECLARE_WAITQUEUE(wait, current);
  488. if (debuglevel >= DEBUG_LEVEL_INFO)
  489. printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
  490. /* Validate the pointers */
  491. if (!n_hdlc)
  492. return -EIO;
  493. /* verify user access to buffer */
  494. if (!access_ok(buf, nr)) {
  495. printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
  496. "buffer\n", __FILE__, __LINE__);
  497. return -EFAULT;
  498. }
  499. add_wait_queue(&tty->read_wait, &wait);
  500. for (;;) {
  501. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  502. ret = -EIO;
  503. break;
  504. }
  505. if (tty_hung_up_p(file))
  506. break;
  507. set_current_state(TASK_INTERRUPTIBLE);
  508. rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
  509. if (rbuf) {
  510. if (rbuf->count > nr) {
  511. /* too large for caller's buffer */
  512. ret = -EOVERFLOW;
  513. } else {
  514. __set_current_state(TASK_RUNNING);
  515. if (copy_to_user(buf, rbuf->buf, rbuf->count))
  516. ret = -EFAULT;
  517. else
  518. ret = rbuf->count;
  519. }
  520. if (n_hdlc->rx_free_buf_list.count >
  521. DEFAULT_RX_BUF_COUNT)
  522. kfree(rbuf);
  523. else
  524. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
  525. break;
  526. }
  527. /* no data */
  528. if (tty_io_nonblock(tty, file)) {
  529. ret = -EAGAIN;
  530. break;
  531. }
  532. schedule();
  533. if (signal_pending(current)) {
  534. ret = -EINTR;
  535. break;
  536. }
  537. }
  538. remove_wait_queue(&tty->read_wait, &wait);
  539. __set_current_state(TASK_RUNNING);
  540. return ret;
  541. } /* end of n_hdlc_tty_read() */
  542. /**
  543. * n_hdlc_tty_write - write a single frame of data to device
  544. * @tty - pointer to associated tty device instance data
  545. * @file - pointer to file object data
  546. * @data - pointer to transmit data (one frame)
  547. * @count - size of transmit frame in bytes
  548. *
  549. * Returns the number of bytes written (or error code).
  550. */
  551. static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
  552. const unsigned char *data, size_t count)
  553. {
  554. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  555. int error = 0;
  556. DECLARE_WAITQUEUE(wait, current);
  557. struct n_hdlc_buf *tbuf;
  558. if (debuglevel >= DEBUG_LEVEL_INFO)
  559. printk("%s(%d)n_hdlc_tty_write() called count=%zd\n",
  560. __FILE__,__LINE__,count);
  561. /* Verify pointers */
  562. if (!n_hdlc)
  563. return -EIO;
  564. if (n_hdlc->magic != HDLC_MAGIC)
  565. return -EIO;
  566. /* verify frame size */
  567. if (count > maxframe ) {
  568. if (debuglevel & DEBUG_LEVEL_INFO)
  569. printk (KERN_WARNING
  570. "n_hdlc_tty_write: truncating user packet "
  571. "from %lu to %d\n", (unsigned long) count,
  572. maxframe );
  573. count = maxframe;
  574. }
  575. add_wait_queue(&tty->write_wait, &wait);
  576. for (;;) {
  577. set_current_state(TASK_INTERRUPTIBLE);
  578. tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
  579. if (tbuf)
  580. break;
  581. if (tty_io_nonblock(tty, file)) {
  582. error = -EAGAIN;
  583. break;
  584. }
  585. schedule();
  586. n_hdlc = tty2n_hdlc (tty);
  587. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
  588. tty != n_hdlc->tty) {
  589. printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
  590. error = -EIO;
  591. break;
  592. }
  593. if (signal_pending(current)) {
  594. error = -EINTR;
  595. break;
  596. }
  597. }
  598. __set_current_state(TASK_RUNNING);
  599. remove_wait_queue(&tty->write_wait, &wait);
  600. if (!error) {
  601. /* Retrieve the user's buffer */
  602. memcpy(tbuf->buf, data, count);
  603. /* Send the data */
  604. tbuf->count = error = count;
  605. n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
  606. n_hdlc_send_frames(n_hdlc,tty);
  607. }
  608. return error;
  609. } /* end of n_hdlc_tty_write() */
  610. /**
  611. * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
  612. * @tty - pointer to tty instance data
  613. * @file - pointer to open file object for device
  614. * @cmd - IOCTL command code
  615. * @arg - argument for IOCTL call (cmd dependent)
  616. *
  617. * Returns command dependent result.
  618. */
  619. static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
  620. unsigned int cmd, unsigned long arg)
  621. {
  622. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  623. int error = 0;
  624. int count;
  625. unsigned long flags;
  626. struct n_hdlc_buf *buf = NULL;
  627. if (debuglevel >= DEBUG_LEVEL_INFO)
  628. printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
  629. __FILE__,__LINE__,cmd);
  630. /* Verify the status of the device */
  631. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
  632. return -EBADF;
  633. switch (cmd) {
  634. case FIONREAD:
  635. /* report count of read data available */
  636. /* in next available frame (if any) */
  637. spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
  638. buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
  639. struct n_hdlc_buf, list_item);
  640. if (buf)
  641. count = buf->count;
  642. else
  643. count = 0;
  644. spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
  645. error = put_user(count, (int __user *)arg);
  646. break;
  647. case TIOCOUTQ:
  648. /* get the pending tx byte count in the driver */
  649. count = tty_chars_in_buffer(tty);
  650. /* add size of next output frame in queue */
  651. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
  652. buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
  653. struct n_hdlc_buf, list_item);
  654. if (buf)
  655. count += buf->count;
  656. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
  657. error = put_user(count, (int __user *)arg);
  658. break;
  659. case TCFLSH:
  660. switch (arg) {
  661. case TCIOFLUSH:
  662. case TCOFLUSH:
  663. flush_tx_queue(tty);
  664. }
  665. /* fall through - to default */
  666. default:
  667. error = n_tty_ioctl_helper(tty, file, cmd, arg);
  668. break;
  669. }
  670. return error;
  671. } /* end of n_hdlc_tty_ioctl() */
  672. /**
  673. * n_hdlc_tty_poll - TTY callback for poll system call
  674. * @tty - pointer to tty instance data
  675. * @filp - pointer to open file object for device
  676. * @poll_table - wait queue for operations
  677. *
  678. * Determine which operations (read/write) will not block and return info
  679. * to caller.
  680. * Returns a bit mask containing info on which ops will not block.
  681. */
  682. static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
  683. poll_table *wait)
  684. {
  685. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  686. __poll_t mask = 0;
  687. if (debuglevel >= DEBUG_LEVEL_INFO)
  688. printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
  689. if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
  690. /* queue current process into any wait queue that */
  691. /* may awaken in the future (read and write) */
  692. poll_wait(filp, &tty->read_wait, wait);
  693. poll_wait(filp, &tty->write_wait, wait);
  694. /* set bits for operations that won't block */
  695. if (!list_empty(&n_hdlc->rx_buf_list.list))
  696. mask |= EPOLLIN | EPOLLRDNORM; /* readable */
  697. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  698. mask |= EPOLLHUP;
  699. if (tty_hung_up_p(filp))
  700. mask |= EPOLLHUP;
  701. if (!tty_is_writelocked(tty) &&
  702. !list_empty(&n_hdlc->tx_free_buf_list.list))
  703. mask |= EPOLLOUT | EPOLLWRNORM; /* writable */
  704. }
  705. return mask;
  706. } /* end of n_hdlc_tty_poll() */
  707. /**
  708. * n_hdlc_alloc - allocate an n_hdlc instance data structure
  709. *
  710. * Returns a pointer to newly created structure if success, otherwise %NULL
  711. */
  712. static struct n_hdlc *n_hdlc_alloc(void)
  713. {
  714. struct n_hdlc_buf *buf;
  715. int i;
  716. struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
  717. if (!n_hdlc)
  718. return NULL;
  719. spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock);
  720. spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock);
  721. spin_lock_init(&n_hdlc->rx_buf_list.spinlock);
  722. spin_lock_init(&n_hdlc->tx_buf_list.spinlock);
  723. INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list);
  724. INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list);
  725. INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
  726. INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
  727. /* allocate free rx buffer list */
  728. for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
  729. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  730. if (buf)
  731. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
  732. else if (debuglevel >= DEBUG_LEVEL_INFO)
  733. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
  734. }
  735. /* allocate free tx buffer list */
  736. for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
  737. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  738. if (buf)
  739. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
  740. else if (debuglevel >= DEBUG_LEVEL_INFO)
  741. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
  742. }
  743. /* Initialize the control block */
  744. n_hdlc->magic = HDLC_MAGIC;
  745. n_hdlc->flags = 0;
  746. return n_hdlc;
  747. } /* end of n_hdlc_alloc() */
  748. /**
  749. * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
  750. * @buf_list - pointer to the buffer list
  751. * @buf - pointer to the buffer
  752. */
  753. static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
  754. struct n_hdlc_buf *buf)
  755. {
  756. unsigned long flags;
  757. spin_lock_irqsave(&buf_list->spinlock, flags);
  758. list_add(&buf->list_item, &buf_list->list);
  759. buf_list->count++;
  760. spin_unlock_irqrestore(&buf_list->spinlock, flags);
  761. }
  762. /**
  763. * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
  764. * @buf_list - pointer to buffer list
  765. * @buf - pointer to buffer
  766. */
  767. static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
  768. struct n_hdlc_buf *buf)
  769. {
  770. unsigned long flags;
  771. spin_lock_irqsave(&buf_list->spinlock, flags);
  772. list_add_tail(&buf->list_item, &buf_list->list);
  773. buf_list->count++;
  774. spin_unlock_irqrestore(&buf_list->spinlock, flags);
  775. } /* end of n_hdlc_buf_put() */
  776. /**
  777. * n_hdlc_buf_get - remove and return an HDLC buffer from list
  778. * @buf_list - pointer to HDLC buffer list
  779. *
  780. * Remove and return an HDLC buffer from the head of the specified HDLC buffer
  781. * list.
  782. * Returns a pointer to HDLC buffer if available, otherwise %NULL.
  783. */
  784. static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
  785. {
  786. unsigned long flags;
  787. struct n_hdlc_buf *buf;
  788. spin_lock_irqsave(&buf_list->spinlock, flags);
  789. buf = list_first_entry_or_null(&buf_list->list,
  790. struct n_hdlc_buf, list_item);
  791. if (buf) {
  792. list_del(&buf->list_item);
  793. buf_list->count--;
  794. }
  795. spin_unlock_irqrestore(&buf_list->spinlock, flags);
  796. return buf;
  797. } /* end of n_hdlc_buf_get() */
  798. static const char hdlc_banner[] __initconst =
  799. KERN_INFO "HDLC line discipline maxframe=%u\n";
  800. static const char hdlc_register_ok[] __initconst =
  801. KERN_INFO "N_HDLC line discipline registered.\n";
  802. static const char hdlc_register_fail[] __initconst =
  803. KERN_ERR "error registering line discipline: %d\n";
  804. static int __init n_hdlc_init(void)
  805. {
  806. int status;
  807. /* range check maxframe arg */
  808. if (maxframe < 4096)
  809. maxframe = 4096;
  810. else if (maxframe > 65535)
  811. maxframe = 65535;
  812. printk(hdlc_banner, maxframe);
  813. status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
  814. if (!status)
  815. printk(hdlc_register_ok);
  816. else
  817. printk(hdlc_register_fail, status);
  818. return status;
  819. } /* end of init_module() */
  820. #ifdef CONFIG_SPARC
  821. #undef __exitdata
  822. #define __exitdata
  823. #endif
  824. static const char hdlc_unregister_ok[] __exitdata =
  825. KERN_INFO "N_HDLC: line discipline unregistered\n";
  826. static const char hdlc_unregister_fail[] __exitdata =
  827. KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
  828. static void __exit n_hdlc_exit(void)
  829. {
  830. /* Release tty registration of line discipline */
  831. int status = tty_unregister_ldisc(N_HDLC);
  832. if (status)
  833. printk(hdlc_unregister_fail, status);
  834. else
  835. printk(hdlc_unregister_ok);
  836. }
  837. module_init(n_hdlc_init);
  838. module_exit(n_hdlc_exit);
  839. MODULE_LICENSE("GPL");
  840. MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
  841. module_param(debuglevel, int, 0);
  842. module_param(maxframe, int, 0);
  843. MODULE_ALIAS_LDISC(N_HDLC);