n_hdlc.c 22 KB

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