n_hdlc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. #include <linux/module.h>
  79. #include <linux/init.h>
  80. #include <linux/kernel.h>
  81. #include <linux/sched.h>
  82. #include <linux/types.h>
  83. #include <linux/fcntl.h>
  84. #include <linux/interrupt.h>
  85. #include <linux/ptrace.h>
  86. #include <linux/poll.h>
  87. #include <linux/in.h>
  88. #include <linux/ioctl.h>
  89. #include <linux/slab.h>
  90. #include <linux/tty.h>
  91. #include <linux/errno.h>
  92. #include <linux/string.h> /* used in new tty drivers */
  93. #include <linux/signal.h> /* used in new tty drivers */
  94. #include <linux/if.h>
  95. #include <linux/bitops.h>
  96. #include <linux/uaccess.h>
  97. #include "tty.h"
  98. /*
  99. * Buffers for individual HDLC frames
  100. */
  101. #define MAX_HDLC_FRAME_SIZE 65535
  102. #define DEFAULT_RX_BUF_COUNT 10
  103. #define MAX_RX_BUF_COUNT 60
  104. #define DEFAULT_TX_BUF_COUNT 3
  105. struct n_hdlc_buf {
  106. struct list_head list_item;
  107. size_t count;
  108. u8 buf[];
  109. };
  110. struct n_hdlc_buf_list {
  111. struct list_head list;
  112. int count;
  113. spinlock_t spinlock;
  114. };
  115. /**
  116. * struct n_hdlc - per device instance data structure
  117. * @tbusy: reentrancy flag for tx wakeup code
  118. * @woke_up: tx wakeup needs to be run again as it was called while @tbusy
  119. * @tx_buf_list: list of pending transmit frame buffers
  120. * @rx_buf_list: list of received frame buffers
  121. * @tx_free_buf_list: list unused transmit frame buffers
  122. * @rx_free_buf_list: list unused received frame buffers
  123. * @write_work: work struct for deferred frame transmission
  124. * @tty_for_write_work: pointer to tty instance used by the @write_work
  125. */
  126. struct n_hdlc {
  127. bool tbusy;
  128. bool woke_up;
  129. struct n_hdlc_buf_list tx_buf_list;
  130. struct n_hdlc_buf_list rx_buf_list;
  131. struct n_hdlc_buf_list tx_free_buf_list;
  132. struct n_hdlc_buf_list rx_free_buf_list;
  133. struct work_struct write_work;
  134. struct tty_struct *tty_for_write_work;
  135. };
  136. /*
  137. * HDLC buffer list manipulation functions
  138. */
  139. static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
  140. struct n_hdlc_buf *buf);
  141. static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
  142. struct n_hdlc_buf *buf);
  143. static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
  144. /* Local functions */
  145. static struct n_hdlc *n_hdlc_alloc(void);
  146. static void n_hdlc_tty_write_work(struct work_struct *work);
  147. /* max frame size for memory allocations */
  148. static int maxframe = 4096;
  149. static void flush_rx_queue(struct tty_struct *tty)
  150. {
  151. struct n_hdlc *n_hdlc = tty->disc_data;
  152. struct n_hdlc_buf *buf;
  153. while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
  154. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
  155. }
  156. static void flush_tx_queue(struct tty_struct *tty)
  157. {
  158. struct n_hdlc *n_hdlc = tty->disc_data;
  159. struct n_hdlc_buf *buf;
  160. while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
  161. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
  162. }
  163. static void n_hdlc_free_buf_list(struct n_hdlc_buf_list *list)
  164. {
  165. struct n_hdlc_buf *buf;
  166. do {
  167. buf = n_hdlc_buf_get(list);
  168. kfree(buf);
  169. } while (buf);
  170. }
  171. /**
  172. * n_hdlc_tty_close - line discipline close
  173. * @tty: pointer to tty info structure
  174. *
  175. * Called when the line discipline is changed to something
  176. * else, the tty is closed, or the tty detects a hangup.
  177. */
  178. static void n_hdlc_tty_close(struct tty_struct *tty)
  179. {
  180. struct n_hdlc *n_hdlc = tty->disc_data;
  181. #if defined(TTY_NO_WRITE_SPLIT)
  182. clear_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  183. #endif
  184. tty->disc_data = NULL;
  185. /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
  186. wake_up_interruptible(&tty->read_wait);
  187. wake_up_interruptible(&tty->write_wait);
  188. cancel_work_sync(&n_hdlc->write_work);
  189. n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list);
  190. n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list);
  191. n_hdlc_free_buf_list(&n_hdlc->rx_buf_list);
  192. n_hdlc_free_buf_list(&n_hdlc->tx_buf_list);
  193. kfree(n_hdlc);
  194. } /* end of n_hdlc_tty_close() */
  195. /**
  196. * n_hdlc_tty_open - called when line discipline changed to n_hdlc
  197. * @tty: pointer to tty info structure
  198. *
  199. * Returns 0 if success, otherwise error code
  200. */
  201. static int n_hdlc_tty_open(struct tty_struct *tty)
  202. {
  203. struct n_hdlc *n_hdlc = tty->disc_data;
  204. pr_debug("%s() called (device=%s)\n", __func__, tty->name);
  205. /* There should not be an existing table for this slot. */
  206. if (n_hdlc) {
  207. pr_err("%s: tty already associated!\n", __func__);
  208. return -EEXIST;
  209. }
  210. n_hdlc = n_hdlc_alloc();
  211. if (!n_hdlc) {
  212. pr_err("%s: n_hdlc_alloc failed\n", __func__);
  213. return -ENFILE;
  214. }
  215. INIT_WORK(&n_hdlc->write_work, n_hdlc_tty_write_work);
  216. n_hdlc->tty_for_write_work = tty;
  217. tty->disc_data = n_hdlc;
  218. tty->receive_room = 65536;
  219. /* change tty_io write() to not split large writes into 8K chunks */
  220. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  221. /* flush receive data from driver */
  222. tty_driver_flush_buffer(tty);
  223. return 0;
  224. } /* end of n_tty_hdlc_open() */
  225. /**
  226. * n_hdlc_send_frames - send frames on pending send buffer list
  227. * @n_hdlc: pointer to ldisc instance data
  228. * @tty: pointer to tty instance data
  229. *
  230. * Send frames on pending send buffer list until the driver does not accept a
  231. * frame (busy) this function is called after adding a frame to the send buffer
  232. * list and by the tty wakeup callback.
  233. */
  234. static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
  235. {
  236. struct n_hdlc_buf *tbuf;
  237. ssize_t actual;
  238. check_again:
  239. scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock) {
  240. if (n_hdlc->tbusy) {
  241. n_hdlc->woke_up = true;
  242. return;
  243. }
  244. n_hdlc->tbusy = true;
  245. n_hdlc->woke_up = false;
  246. }
  247. tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
  248. while (tbuf) {
  249. pr_debug("sending frame %p, count=%zu\n", tbuf, tbuf->count);
  250. /* Send the next block of data to device */
  251. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  252. actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
  253. /* rollback was possible and has been done */
  254. if (actual == -ERESTARTSYS) {
  255. n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
  256. break;
  257. }
  258. /* if transmit error, throw frame away by */
  259. /* pretending it was accepted by driver */
  260. if (actual < 0)
  261. actual = tbuf->count;
  262. if (actual == tbuf->count) {
  263. pr_debug("frame %p completed\n", tbuf);
  264. /* free current transmit buffer */
  265. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
  266. /* wait up sleeping writers */
  267. wake_up_interruptible(&tty->write_wait);
  268. /* get next pending transmit buffer */
  269. tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
  270. } else {
  271. pr_debug("frame %p pending\n", tbuf);
  272. /*
  273. * the buffer was not accepted by driver,
  274. * return it back into tx queue
  275. */
  276. n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
  277. break;
  278. }
  279. }
  280. if (!tbuf)
  281. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  282. /* Clear the re-entry flag */
  283. scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock)
  284. n_hdlc->tbusy = false;
  285. if (n_hdlc->woke_up)
  286. goto check_again;
  287. } /* end of n_hdlc_send_frames() */
  288. /**
  289. * n_hdlc_tty_write_work - Asynchronous callback for transmit wakeup
  290. * @work: pointer to work_struct
  291. *
  292. * Called when low level device driver can accept more send data.
  293. */
  294. static void n_hdlc_tty_write_work(struct work_struct *work)
  295. {
  296. struct n_hdlc *n_hdlc = container_of(work, struct n_hdlc, write_work);
  297. struct tty_struct *tty = n_hdlc->tty_for_write_work;
  298. n_hdlc_send_frames(n_hdlc, tty);
  299. } /* end of n_hdlc_tty_write_work() */
  300. /**
  301. * n_hdlc_tty_wakeup - Callback for transmit wakeup
  302. * @tty: pointer to associated tty instance data
  303. *
  304. * Called when low level device driver can accept more send data.
  305. */
  306. static void n_hdlc_tty_wakeup(struct tty_struct *tty)
  307. {
  308. struct n_hdlc *n_hdlc = tty->disc_data;
  309. schedule_work(&n_hdlc->write_work);
  310. } /* end of n_hdlc_tty_wakeup() */
  311. /**
  312. * n_hdlc_tty_receive - Called by tty driver when receive data is available
  313. * @tty: pointer to tty instance data
  314. * @data: pointer to received data
  315. * @flags: pointer to flags for data
  316. * @count: count of received data in bytes
  317. *
  318. * Called by tty low level driver when receive data is available. Data is
  319. * interpreted as one HDLC frame.
  320. */
  321. static void n_hdlc_tty_receive(struct tty_struct *tty, const u8 *data,
  322. const u8 *flags, size_t count)
  323. {
  324. register struct n_hdlc *n_hdlc = tty->disc_data;
  325. register struct n_hdlc_buf *buf;
  326. pr_debug("%s() called count=%zu\n", __func__, count);
  327. if (count > maxframe) {
  328. pr_debug("rx count>maxframesize, data discarded\n");
  329. return;
  330. }
  331. /* get a free HDLC buffer */
  332. buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
  333. if (!buf) {
  334. /*
  335. * no buffers in free list, attempt to allocate another rx
  336. * buffer unless the maximum count has been reached
  337. */
  338. if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
  339. buf = kmalloc_flex(*buf, buf, maxframe, GFP_ATOMIC);
  340. }
  341. if (!buf) {
  342. pr_debug("no more rx buffers, data discarded\n");
  343. return;
  344. }
  345. /* copy received data to HDLC buffer */
  346. memcpy(buf->buf, data, count);
  347. buf->count = count;
  348. /* add HDLC buffer to list of received frames */
  349. n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
  350. /* wake up any blocked reads and perform async signalling */
  351. wake_up_interruptible(&tty->read_wait);
  352. if (tty->fasync != NULL)
  353. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  354. } /* end of n_hdlc_tty_receive() */
  355. /**
  356. * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
  357. * @tty: pointer to tty instance data
  358. * @file: pointer to open file object
  359. * @kbuf: pointer to returned data buffer
  360. * @nr: size of returned data buffer
  361. * @cookie: stored rbuf from previous run
  362. * @offset: offset into the data buffer
  363. *
  364. * Returns the number of bytes returned or error code.
  365. */
  366. static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
  367. u8 *kbuf, size_t nr, void **cookie,
  368. unsigned long offset)
  369. {
  370. struct n_hdlc *n_hdlc = tty->disc_data;
  371. int ret = 0;
  372. struct n_hdlc_buf *rbuf;
  373. DECLARE_WAITQUEUE(wait, current);
  374. /* Is this a repeated call for an rbuf we already found earlier? */
  375. rbuf = *cookie;
  376. if (rbuf)
  377. goto have_rbuf;
  378. add_wait_queue(&tty->read_wait, &wait);
  379. for (;;) {
  380. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  381. ret = -EIO;
  382. break;
  383. }
  384. if (tty_hung_up_p(file))
  385. break;
  386. set_current_state(TASK_INTERRUPTIBLE);
  387. rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
  388. if (rbuf)
  389. break;
  390. /* no data */
  391. if (tty_io_nonblock(tty, file)) {
  392. ret = -EAGAIN;
  393. break;
  394. }
  395. schedule();
  396. if (signal_pending(current)) {
  397. ret = -EINTR;
  398. break;
  399. }
  400. }
  401. remove_wait_queue(&tty->read_wait, &wait);
  402. __set_current_state(TASK_RUNNING);
  403. if (!rbuf)
  404. return ret;
  405. *cookie = rbuf;
  406. have_rbuf:
  407. /* Have we used it up entirely? */
  408. if (offset >= rbuf->count)
  409. goto done_with_rbuf;
  410. /* More data to go, but can't copy any more? EOVERFLOW */
  411. ret = -EOVERFLOW;
  412. if (!nr)
  413. goto done_with_rbuf;
  414. /* Copy as much data as possible */
  415. ret = rbuf->count - offset;
  416. if (ret > nr)
  417. ret = nr;
  418. memcpy(kbuf, rbuf->buf+offset, ret);
  419. offset += ret;
  420. /* If we still have data left, we leave the rbuf in the cookie */
  421. if (offset < rbuf->count)
  422. return ret;
  423. done_with_rbuf:
  424. *cookie = NULL;
  425. if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
  426. kfree(rbuf);
  427. else
  428. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
  429. return ret;
  430. } /* end of n_hdlc_tty_read() */
  431. /**
  432. * n_hdlc_tty_write - write a single frame of data to device
  433. * @tty: pointer to associated tty device instance data
  434. * @file: pointer to file object data
  435. * @data: pointer to transmit data (one frame)
  436. * @count: size of transmit frame in bytes
  437. *
  438. * Returns the number of bytes written (or error code).
  439. */
  440. static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
  441. const u8 *data, size_t count)
  442. {
  443. struct n_hdlc *n_hdlc = tty->disc_data;
  444. DECLARE_WAITQUEUE(wait, current);
  445. struct n_hdlc_buf *tbuf;
  446. ssize_t error = 0;
  447. pr_debug("%s() called count=%zd\n", __func__, count);
  448. /* verify frame size */
  449. if (count > maxframe) {
  450. pr_debug("%s: truncating user packet from %zu to %d\n",
  451. __func__, count, maxframe);
  452. count = maxframe;
  453. }
  454. add_wait_queue(&tty->write_wait, &wait);
  455. for (;;) {
  456. set_current_state(TASK_INTERRUPTIBLE);
  457. tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
  458. if (tbuf)
  459. break;
  460. if (tty_io_nonblock(tty, file)) {
  461. error = -EAGAIN;
  462. break;
  463. }
  464. schedule();
  465. if (signal_pending(current)) {
  466. error = -EINTR;
  467. break;
  468. }
  469. }
  470. __set_current_state(TASK_RUNNING);
  471. remove_wait_queue(&tty->write_wait, &wait);
  472. if (!error) {
  473. /* Retrieve the user's buffer */
  474. memcpy(tbuf->buf, data, count);
  475. /* Send the data */
  476. tbuf->count = error = count;
  477. n_hdlc_buf_put(&n_hdlc->tx_buf_list, tbuf);
  478. n_hdlc_send_frames(n_hdlc, tty);
  479. }
  480. return error;
  481. } /* end of n_hdlc_tty_write() */
  482. /**
  483. * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
  484. * @tty: pointer to tty instance data
  485. * @cmd: IOCTL command code
  486. * @arg: argument for IOCTL call (cmd dependent)
  487. *
  488. * Returns command dependent result.
  489. */
  490. static int n_hdlc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
  491. unsigned long arg)
  492. {
  493. struct n_hdlc *n_hdlc = tty->disc_data;
  494. int count;
  495. struct n_hdlc_buf *buf = NULL;
  496. pr_debug("%s() called %d\n", __func__, cmd);
  497. switch (cmd) {
  498. case FIONREAD:
  499. /* report count of read data available */
  500. /* in next available frame (if any) */
  501. scoped_guard(spinlock_irqsave, &n_hdlc->rx_buf_list.spinlock) {
  502. buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
  503. struct n_hdlc_buf, list_item);
  504. if (buf)
  505. count = buf->count;
  506. else
  507. count = 0;
  508. }
  509. return put_user(count, (int __user *)arg);
  510. case TIOCOUTQ:
  511. /* get the pending tx byte count in the driver */
  512. count = tty_chars_in_buffer(tty);
  513. /* add size of next output frame in queue */
  514. scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock) {
  515. buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
  516. struct n_hdlc_buf, list_item);
  517. if (buf)
  518. count += buf->count;
  519. }
  520. return put_user(count, (int __user *)arg);
  521. case TCFLSH:
  522. switch (arg) {
  523. case TCIOFLUSH:
  524. case TCOFLUSH:
  525. flush_tx_queue(tty);
  526. }
  527. fallthrough; /* to default */
  528. default:
  529. return n_tty_ioctl_helper(tty, cmd, arg);
  530. }
  531. } /* end of n_hdlc_tty_ioctl() */
  532. /**
  533. * n_hdlc_tty_poll - TTY callback for poll system call
  534. * @tty: pointer to tty instance data
  535. * @filp: pointer to open file object for device
  536. * @wait: wait queue for operations
  537. *
  538. * Determine which operations (read/write) will not block and return info
  539. * to caller.
  540. * Returns a bit mask containing info on which ops will not block.
  541. */
  542. static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
  543. poll_table *wait)
  544. {
  545. struct n_hdlc *n_hdlc = tty->disc_data;
  546. __poll_t mask = 0;
  547. /*
  548. * queue the current process into any wait queue that may awaken in the
  549. * future (read and write)
  550. */
  551. poll_wait(filp, &tty->read_wait, wait);
  552. poll_wait(filp, &tty->write_wait, wait);
  553. /* set bits for operations that won't block */
  554. if (!list_empty(&n_hdlc->rx_buf_list.list))
  555. mask |= EPOLLIN | EPOLLRDNORM; /* readable */
  556. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  557. mask |= EPOLLHUP;
  558. if (tty_hung_up_p(filp))
  559. mask |= EPOLLHUP;
  560. if (!tty_is_writelocked(tty) &&
  561. !list_empty(&n_hdlc->tx_free_buf_list.list))
  562. mask |= EPOLLOUT | EPOLLWRNORM; /* writable */
  563. return mask;
  564. } /* end of n_hdlc_tty_poll() */
  565. static void n_hdlc_alloc_buf(struct n_hdlc_buf_list *list, unsigned int count,
  566. const char *name)
  567. {
  568. struct n_hdlc_buf *buf;
  569. unsigned int i;
  570. for (i = 0; i < count; i++) {
  571. buf = kmalloc_flex(*buf, buf, maxframe);
  572. if (!buf) {
  573. pr_debug("%s(), kmalloc() failed for %s buffer %u\n",
  574. __func__, name, i);
  575. return;
  576. }
  577. n_hdlc_buf_put(list, buf);
  578. }
  579. }
  580. /**
  581. * n_hdlc_alloc - allocate an n_hdlc instance data structure
  582. *
  583. * Returns a pointer to newly created structure if success, otherwise %NULL
  584. */
  585. static struct n_hdlc *n_hdlc_alloc(void)
  586. {
  587. struct n_hdlc *n_hdlc = kzalloc_obj(*n_hdlc);
  588. if (!n_hdlc)
  589. return NULL;
  590. spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock);
  591. spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock);
  592. spin_lock_init(&n_hdlc->rx_buf_list.spinlock);
  593. spin_lock_init(&n_hdlc->tx_buf_list.spinlock);
  594. INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list);
  595. INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list);
  596. INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
  597. INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
  598. n_hdlc_alloc_buf(&n_hdlc->rx_free_buf_list, DEFAULT_RX_BUF_COUNT, "rx");
  599. n_hdlc_alloc_buf(&n_hdlc->tx_free_buf_list, DEFAULT_TX_BUF_COUNT, "tx");
  600. return n_hdlc;
  601. } /* end of n_hdlc_alloc() */
  602. /**
  603. * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
  604. * @buf_list: pointer to the buffer list
  605. * @buf: pointer to the buffer
  606. */
  607. static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
  608. struct n_hdlc_buf *buf)
  609. {
  610. guard(spinlock_irqsave)(&buf_list->spinlock);
  611. list_add(&buf->list_item, &buf_list->list);
  612. buf_list->count++;
  613. }
  614. /**
  615. * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
  616. * @buf_list: pointer to buffer list
  617. * @buf: pointer to buffer
  618. */
  619. static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
  620. struct n_hdlc_buf *buf)
  621. {
  622. guard(spinlock_irqsave)(&buf_list->spinlock);
  623. list_add_tail(&buf->list_item, &buf_list->list);
  624. buf_list->count++;
  625. } /* end of n_hdlc_buf_put() */
  626. /**
  627. * n_hdlc_buf_get - remove and return an HDLC buffer from list
  628. * @buf_list: pointer to HDLC buffer list
  629. *
  630. * Remove and return an HDLC buffer from the head of the specified HDLC buffer
  631. * list.
  632. * Returns a pointer to HDLC buffer if available, otherwise %NULL.
  633. */
  634. static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
  635. {
  636. struct n_hdlc_buf *buf;
  637. guard(spinlock_irqsave)(&buf_list->spinlock);
  638. buf = list_first_entry_or_null(&buf_list->list,
  639. struct n_hdlc_buf, list_item);
  640. if (buf) {
  641. list_del(&buf->list_item);
  642. buf_list->count--;
  643. }
  644. return buf;
  645. } /* end of n_hdlc_buf_get() */
  646. static struct tty_ldisc_ops n_hdlc_ldisc = {
  647. .owner = THIS_MODULE,
  648. .num = N_HDLC,
  649. .name = "hdlc",
  650. .open = n_hdlc_tty_open,
  651. .close = n_hdlc_tty_close,
  652. .read = n_hdlc_tty_read,
  653. .write = n_hdlc_tty_write,
  654. .ioctl = n_hdlc_tty_ioctl,
  655. .poll = n_hdlc_tty_poll,
  656. .receive_buf = n_hdlc_tty_receive,
  657. .write_wakeup = n_hdlc_tty_wakeup,
  658. .flush_buffer = flush_rx_queue,
  659. };
  660. static int __init n_hdlc_init(void)
  661. {
  662. int status;
  663. /* range check maxframe arg */
  664. maxframe = clamp(maxframe, 4096, MAX_HDLC_FRAME_SIZE);
  665. status = tty_register_ldisc(&n_hdlc_ldisc);
  666. if (!status)
  667. pr_info("N_HDLC line discipline registered with maxframe=%d\n",
  668. maxframe);
  669. else
  670. pr_err("N_HDLC: error registering line discipline: %d\n",
  671. status);
  672. return status;
  673. } /* end of init_module() */
  674. static void __exit n_hdlc_exit(void)
  675. {
  676. tty_unregister_ldisc(&n_hdlc_ldisc);
  677. }
  678. module_init(n_hdlc_init);
  679. module_exit(n_hdlc_exit);
  680. MODULE_DESCRIPTION("HDLC line discipline support");
  681. MODULE_LICENSE("GPL");
  682. MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
  683. module_param(maxframe, int, 0);
  684. MODULE_ALIAS_LDISC(N_HDLC);