n_hdlc.c 27 KB

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