n_hdlc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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(VERIFY_WRITE, 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. if (copy_to_user(buf, rbuf->buf, rbuf->count))
  515. ret = -EFAULT;
  516. else
  517. ret = rbuf->count;
  518. }
  519. if (n_hdlc->rx_free_buf_list.count >
  520. DEFAULT_RX_BUF_COUNT)
  521. kfree(rbuf);
  522. else
  523. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
  524. break;
  525. }
  526. /* no data */
  527. if (file->f_flags & O_NONBLOCK) {
  528. ret = -EAGAIN;
  529. break;
  530. }
  531. schedule();
  532. if (signal_pending(current)) {
  533. ret = -EINTR;
  534. break;
  535. }
  536. }
  537. remove_wait_queue(&tty->read_wait, &wait);
  538. __set_current_state(TASK_RUNNING);
  539. return ret;
  540. } /* end of n_hdlc_tty_read() */
  541. /**
  542. * n_hdlc_tty_write - write a single frame of data to device
  543. * @tty - pointer to associated tty device instance data
  544. * @file - pointer to file object data
  545. * @data - pointer to transmit data (one frame)
  546. * @count - size of transmit frame in bytes
  547. *
  548. * Returns the number of bytes written (or error code).
  549. */
  550. static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
  551. const unsigned char *data, size_t count)
  552. {
  553. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  554. int error = 0;
  555. DECLARE_WAITQUEUE(wait, current);
  556. struct n_hdlc_buf *tbuf;
  557. if (debuglevel >= DEBUG_LEVEL_INFO)
  558. printk("%s(%d)n_hdlc_tty_write() called count=%zd\n",
  559. __FILE__,__LINE__,count);
  560. /* Verify pointers */
  561. if (!n_hdlc)
  562. return -EIO;
  563. if (n_hdlc->magic != HDLC_MAGIC)
  564. return -EIO;
  565. /* verify frame size */
  566. if (count > maxframe ) {
  567. if (debuglevel & DEBUG_LEVEL_INFO)
  568. printk (KERN_WARNING
  569. "n_hdlc_tty_write: truncating user packet "
  570. "from %lu to %d\n", (unsigned long) count,
  571. maxframe );
  572. count = maxframe;
  573. }
  574. add_wait_queue(&tty->write_wait, &wait);
  575. for (;;) {
  576. set_current_state(TASK_INTERRUPTIBLE);
  577. tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
  578. if (tbuf)
  579. break;
  580. if (file->f_flags & O_NONBLOCK) {
  581. error = -EAGAIN;
  582. break;
  583. }
  584. schedule();
  585. n_hdlc = tty2n_hdlc (tty);
  586. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
  587. tty != n_hdlc->tty) {
  588. printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
  589. error = -EIO;
  590. break;
  591. }
  592. if (signal_pending(current)) {
  593. error = -EINTR;
  594. break;
  595. }
  596. }
  597. __set_current_state(TASK_RUNNING);
  598. remove_wait_queue(&tty->write_wait, &wait);
  599. if (!error) {
  600. /* Retrieve the user's buffer */
  601. memcpy(tbuf->buf, data, count);
  602. /* Send the data */
  603. tbuf->count = error = count;
  604. n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
  605. n_hdlc_send_frames(n_hdlc,tty);
  606. }
  607. return error;
  608. } /* end of n_hdlc_tty_write() */
  609. /**
  610. * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
  611. * @tty - pointer to tty instance data
  612. * @file - pointer to open file object for device
  613. * @cmd - IOCTL command code
  614. * @arg - argument for IOCTL call (cmd dependent)
  615. *
  616. * Returns command dependent result.
  617. */
  618. static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
  619. unsigned int cmd, unsigned long arg)
  620. {
  621. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  622. int error = 0;
  623. int count;
  624. unsigned long flags;
  625. struct n_hdlc_buf *buf = NULL;
  626. if (debuglevel >= DEBUG_LEVEL_INFO)
  627. printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
  628. __FILE__,__LINE__,cmd);
  629. /* Verify the status of the device */
  630. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
  631. return -EBADF;
  632. switch (cmd) {
  633. case FIONREAD:
  634. /* report count of read data available */
  635. /* in next available frame (if any) */
  636. spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
  637. buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
  638. struct n_hdlc_buf, list_item);
  639. if (buf)
  640. count = buf->count;
  641. else
  642. count = 0;
  643. spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
  644. error = put_user(count, (int __user *)arg);
  645. break;
  646. case TIOCOUTQ:
  647. /* get the pending tx byte count in the driver */
  648. count = tty_chars_in_buffer(tty);
  649. /* add size of next output frame in queue */
  650. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
  651. buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
  652. struct n_hdlc_buf, list_item);
  653. if (buf)
  654. count += buf->count;
  655. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
  656. error = put_user(count, (int __user *)arg);
  657. break;
  658. case TCFLSH:
  659. switch (arg) {
  660. case TCIOFLUSH:
  661. case TCOFLUSH:
  662. flush_tx_queue(tty);
  663. }
  664. /* fall through to default */
  665. default:
  666. error = n_tty_ioctl_helper(tty, file, cmd, arg);
  667. break;
  668. }
  669. return error;
  670. } /* end of n_hdlc_tty_ioctl() */
  671. /**
  672. * n_hdlc_tty_poll - TTY callback for poll system call
  673. * @tty - pointer to tty instance data
  674. * @filp - pointer to open file object for device
  675. * @poll_table - wait queue for operations
  676. *
  677. * Determine which operations (read/write) will not block and return info
  678. * to caller.
  679. * Returns a bit mask containing info on which ops will not block.
  680. */
  681. static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
  682. poll_table *wait)
  683. {
  684. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  685. __poll_t mask = 0;
  686. if (debuglevel >= DEBUG_LEVEL_INFO)
  687. printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
  688. if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
  689. /* queue current process into any wait queue that */
  690. /* may awaken in the future (read and write) */
  691. poll_wait(filp, &tty->read_wait, wait);
  692. poll_wait(filp, &tty->write_wait, wait);
  693. /* set bits for operations that won't block */
  694. if (!list_empty(&n_hdlc->rx_buf_list.list))
  695. mask |= EPOLLIN | EPOLLRDNORM; /* readable */
  696. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  697. mask |= EPOLLHUP;
  698. if (tty_hung_up_p(filp))
  699. mask |= EPOLLHUP;
  700. if (!tty_is_writelocked(tty) &&
  701. !list_empty(&n_hdlc->tx_free_buf_list.list))
  702. mask |= EPOLLOUT | EPOLLWRNORM; /* writable */
  703. }
  704. return mask;
  705. } /* end of n_hdlc_tty_poll() */
  706. /**
  707. * n_hdlc_alloc - allocate an n_hdlc instance data structure
  708. *
  709. * Returns a pointer to newly created structure if success, otherwise %NULL
  710. */
  711. static struct n_hdlc *n_hdlc_alloc(void)
  712. {
  713. struct n_hdlc_buf *buf;
  714. int i;
  715. struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
  716. if (!n_hdlc)
  717. return NULL;
  718. spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock);
  719. spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock);
  720. spin_lock_init(&n_hdlc->rx_buf_list.spinlock);
  721. spin_lock_init(&n_hdlc->tx_buf_list.spinlock);
  722. INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list);
  723. INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list);
  724. INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
  725. INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
  726. /* allocate free rx buffer list */
  727. for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
  728. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  729. if (buf)
  730. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
  731. else if (debuglevel >= DEBUG_LEVEL_INFO)
  732. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
  733. }
  734. /* allocate free tx buffer list */
  735. for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
  736. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  737. if (buf)
  738. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
  739. else if (debuglevel >= DEBUG_LEVEL_INFO)
  740. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
  741. }
  742. /* Initialize the control block */
  743. n_hdlc->magic = HDLC_MAGIC;
  744. n_hdlc->flags = 0;
  745. return n_hdlc;
  746. } /* end of n_hdlc_alloc() */
  747. /**
  748. * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
  749. * @buf_list - pointer to the buffer list
  750. * @buf - pointer to the buffer
  751. */
  752. static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
  753. struct n_hdlc_buf *buf)
  754. {
  755. unsigned long flags;
  756. spin_lock_irqsave(&buf_list->spinlock, flags);
  757. list_add(&buf->list_item, &buf_list->list);
  758. buf_list->count++;
  759. spin_unlock_irqrestore(&buf_list->spinlock, flags);
  760. }
  761. /**
  762. * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
  763. * @buf_list - pointer to buffer list
  764. * @buf - pointer to buffer
  765. */
  766. static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
  767. struct n_hdlc_buf *buf)
  768. {
  769. unsigned long flags;
  770. spin_lock_irqsave(&buf_list->spinlock, flags);
  771. list_add_tail(&buf->list_item, &buf_list->list);
  772. buf_list->count++;
  773. spin_unlock_irqrestore(&buf_list->spinlock, flags);
  774. } /* end of n_hdlc_buf_put() */
  775. /**
  776. * n_hdlc_buf_get - remove and return an HDLC buffer from list
  777. * @buf_list - pointer to HDLC buffer list
  778. *
  779. * Remove and return an HDLC buffer from the head of the specified HDLC buffer
  780. * list.
  781. * Returns a pointer to HDLC buffer if available, otherwise %NULL.
  782. */
  783. static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
  784. {
  785. unsigned long flags;
  786. struct n_hdlc_buf *buf;
  787. spin_lock_irqsave(&buf_list->spinlock, flags);
  788. buf = list_first_entry_or_null(&buf_list->list,
  789. struct n_hdlc_buf, list_item);
  790. if (buf) {
  791. list_del(&buf->list_item);
  792. buf_list->count--;
  793. }
  794. spin_unlock_irqrestore(&buf_list->spinlock, flags);
  795. return buf;
  796. } /* end of n_hdlc_buf_get() */
  797. static const char hdlc_banner[] __initconst =
  798. KERN_INFO "HDLC line discipline maxframe=%u\n";
  799. static const char hdlc_register_ok[] __initconst =
  800. KERN_INFO "N_HDLC line discipline registered.\n";
  801. static const char hdlc_register_fail[] __initconst =
  802. KERN_ERR "error registering line discipline: %d\n";
  803. static int __init n_hdlc_init(void)
  804. {
  805. int status;
  806. /* range check maxframe arg */
  807. if (maxframe < 4096)
  808. maxframe = 4096;
  809. else if (maxframe > 65535)
  810. maxframe = 65535;
  811. printk(hdlc_banner, maxframe);
  812. status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
  813. if (!status)
  814. printk(hdlc_register_ok);
  815. else
  816. printk(hdlc_register_fail, status);
  817. return status;
  818. } /* end of init_module() */
  819. static const char hdlc_unregister_ok[] __exitdata =
  820. KERN_INFO "N_HDLC: line discipline unregistered\n";
  821. static const char hdlc_unregister_fail[] __exitdata =
  822. KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
  823. static void __exit n_hdlc_exit(void)
  824. {
  825. /* Release tty registration of line discipline */
  826. int status = tty_unregister_ldisc(N_HDLC);
  827. if (status)
  828. printk(hdlc_unregister_fail, status);
  829. else
  830. printk(hdlc_unregister_ok);
  831. }
  832. module_init(n_hdlc_init);
  833. module_exit(n_hdlc_exit);
  834. MODULE_LICENSE("GPL");
  835. MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
  836. module_param(debuglevel, int, 0);
  837. module_param(maxframe, int, 0);
  838. MODULE_ALIAS_LDISC(N_HDLC);