n_hdlc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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/config.h>
  83. #include <linux/module.h>
  84. #include <linux/init.h>
  85. #include <linux/kernel.h>
  86. #include <linux/sched.h>
  87. #include <linux/types.h>
  88. #include <linux/fcntl.h>
  89. #include <linux/interrupt.h>
  90. #include <linux/ptrace.h>
  91. #undef VERSION
  92. #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
  93. #include <linux/poll.h>
  94. #include <linux/in.h>
  95. #include <linux/ioctl.h>
  96. #include <linux/slab.h>
  97. #include <linux/tty.h>
  98. #include <linux/errno.h>
  99. #include <linux/string.h> /* used in new tty drivers */
  100. #include <linux/signal.h> /* used in new tty drivers */
  101. #include <linux/if.h>
  102. #include <linux/bitops.h>
  103. #include <asm/system.h>
  104. #include <asm/termios.h>
  105. #include <asm/uaccess.h>
  106. /*
  107. * Buffers for individual HDLC frames
  108. */
  109. #define MAX_HDLC_FRAME_SIZE 65535
  110. #define DEFAULT_RX_BUF_COUNT 10
  111. #define MAX_RX_BUF_COUNT 60
  112. #define DEFAULT_TX_BUF_COUNT 1
  113. struct n_hdlc_buf {
  114. struct n_hdlc_buf *link;
  115. int count;
  116. char buf[1];
  117. };
  118. #define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
  119. struct n_hdlc_buf_list {
  120. struct n_hdlc_buf *head;
  121. struct n_hdlc_buf *tail;
  122. int count;
  123. spinlock_t spinlock;
  124. };
  125. /**
  126. * struct n_hdlc - per device instance data structure
  127. * @magic - magic value for structure
  128. * @flags - miscellaneous control flags
  129. * @tty - ptr to TTY structure
  130. * @backup_tty - TTY to use if tty gets closed
  131. * @tbusy - reentrancy flag for tx wakeup code
  132. * @woke_up - FIXME: describe this field
  133. * @tbuf - currently transmitting tx buffer
  134. * @tx_buf_list - list of pending transmit frame buffers
  135. * @rx_buf_list - list of received frame buffers
  136. * @tx_free_buf_list - list unused transmit frame buffers
  137. * @rx_free_buf_list - list unused received frame buffers
  138. */
  139. struct n_hdlc {
  140. int magic;
  141. __u32 flags;
  142. struct tty_struct *tty;
  143. struct tty_struct *backup_tty;
  144. int tbusy;
  145. int woke_up;
  146. struct n_hdlc_buf *tbuf;
  147. struct n_hdlc_buf_list tx_buf_list;
  148. struct n_hdlc_buf_list rx_buf_list;
  149. struct n_hdlc_buf_list tx_free_buf_list;
  150. struct n_hdlc_buf_list rx_free_buf_list;
  151. };
  152. /*
  153. * HDLC buffer list manipulation functions
  154. */
  155. static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list);
  156. static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
  157. struct n_hdlc_buf *buf);
  158. static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
  159. /* Local functions */
  160. static struct n_hdlc *n_hdlc_alloc (void);
  161. /* debug level can be set by insmod for debugging purposes */
  162. #define DEBUG_LEVEL_INFO 1
  163. static int debuglevel;
  164. /* max frame size for memory allocations */
  165. static int maxframe = 4096;
  166. /* TTY callbacks */
  167. static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
  168. __u8 __user *buf, size_t nr);
  169. static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
  170. const unsigned char *buf, size_t nr);
  171. static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
  172. unsigned int cmd, unsigned long arg);
  173. static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
  174. poll_table *wait);
  175. static int n_hdlc_tty_open(struct tty_struct *tty);
  176. static void n_hdlc_tty_close(struct tty_struct *tty);
  177. static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
  178. char *fp, int count);
  179. static void n_hdlc_tty_wakeup(struct tty_struct *tty);
  180. #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
  181. #define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
  182. #define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
  183. static struct tty_ldisc n_hdlc_ldisc = {
  184. .owner = THIS_MODULE,
  185. .magic = TTY_LDISC_MAGIC,
  186. .name = "hdlc",
  187. .open = n_hdlc_tty_open,
  188. .close = n_hdlc_tty_close,
  189. .read = n_hdlc_tty_read,
  190. .write = n_hdlc_tty_write,
  191. .ioctl = n_hdlc_tty_ioctl,
  192. .poll = n_hdlc_tty_poll,
  193. .receive_buf = n_hdlc_tty_receive,
  194. .write_wakeup = n_hdlc_tty_wakeup,
  195. };
  196. /**
  197. * n_hdlc_release - release an n_hdlc per device line discipline info structure
  198. * @n_hdlc - per device line discipline info structure
  199. */
  200. static void n_hdlc_release(struct n_hdlc *n_hdlc)
  201. {
  202. struct tty_struct *tty = n_hdlc2tty (n_hdlc);
  203. struct n_hdlc_buf *buf;
  204. if (debuglevel >= DEBUG_LEVEL_INFO)
  205. printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
  206. /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
  207. wake_up_interruptible (&tty->read_wait);
  208. wake_up_interruptible (&tty->write_wait);
  209. if (tty != NULL && tty->disc_data == n_hdlc)
  210. tty->disc_data = NULL; /* Break the tty->n_hdlc link */
  211. /* Release transmit and receive buffers */
  212. for(;;) {
  213. buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
  214. if (buf) {
  215. kfree(buf);
  216. } else
  217. break;
  218. }
  219. for(;;) {
  220. buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
  221. if (buf) {
  222. kfree(buf);
  223. } else
  224. break;
  225. }
  226. for(;;) {
  227. buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
  228. if (buf) {
  229. kfree(buf);
  230. } else
  231. break;
  232. }
  233. for(;;) {
  234. buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
  235. if (buf) {
  236. kfree(buf);
  237. } else
  238. break;
  239. }
  240. kfree(n_hdlc->tbuf);
  241. kfree(n_hdlc);
  242. } /* end of n_hdlc_release() */
  243. /**
  244. * n_hdlc_tty_close - line discipline close
  245. * @tty - pointer to tty info structure
  246. *
  247. * Called when the line discipline is changed to something
  248. * else, the tty is closed, or the tty detects a hangup.
  249. */
  250. static void n_hdlc_tty_close(struct tty_struct *tty)
  251. {
  252. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  253. if (debuglevel >= DEBUG_LEVEL_INFO)
  254. printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
  255. if (n_hdlc != NULL) {
  256. if (n_hdlc->magic != HDLC_MAGIC) {
  257. printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
  258. return;
  259. }
  260. #if defined(TTY_NO_WRITE_SPLIT)
  261. clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
  262. #endif
  263. tty->disc_data = NULL;
  264. if (tty == n_hdlc->backup_tty)
  265. n_hdlc->backup_tty = NULL;
  266. if (tty != n_hdlc->tty)
  267. return;
  268. if (n_hdlc->backup_tty) {
  269. n_hdlc->tty = n_hdlc->backup_tty;
  270. } else {
  271. n_hdlc_release (n_hdlc);
  272. }
  273. }
  274. if (debuglevel >= DEBUG_LEVEL_INFO)
  275. printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
  276. } /* end of n_hdlc_tty_close() */
  277. /**
  278. * n_hdlc_tty_open - called when line discipline changed to n_hdlc
  279. * @tty - pointer to tty info structure
  280. *
  281. * Returns 0 if success, otherwise error code
  282. */
  283. static int n_hdlc_tty_open (struct tty_struct *tty)
  284. {
  285. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  286. if (debuglevel >= DEBUG_LEVEL_INFO)
  287. printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
  288. __FILE__,__LINE__,
  289. tty->name);
  290. /* There should not be an existing table for this slot. */
  291. if (n_hdlc) {
  292. printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
  293. return -EEXIST;
  294. }
  295. n_hdlc = n_hdlc_alloc();
  296. if (!n_hdlc) {
  297. printk (KERN_ERR "n_hdlc_alloc failed\n");
  298. return -ENFILE;
  299. }
  300. tty->disc_data = n_hdlc;
  301. n_hdlc->tty = tty;
  302. tty->receive_room = 65536;
  303. #if defined(TTY_NO_WRITE_SPLIT)
  304. /* change tty_io write() to not split large writes into 8K chunks */
  305. set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
  306. #endif
  307. /* Flush any pending characters in the driver and discipline. */
  308. if (tty->ldisc.flush_buffer)
  309. tty->ldisc.flush_buffer (tty);
  310. if (tty->driver->flush_buffer)
  311. tty->driver->flush_buffer (tty);
  312. if (debuglevel >= DEBUG_LEVEL_INFO)
  313. printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
  314. return 0;
  315. } /* end of n_tty_hdlc_open() */
  316. /**
  317. * n_hdlc_send_frames - send frames on pending send buffer list
  318. * @n_hdlc - pointer to ldisc instance data
  319. * @tty - pointer to tty instance data
  320. *
  321. * Send frames on pending send buffer list until the driver does not accept a
  322. * frame (busy) this function is called after adding a frame to the send buffer
  323. * list and by the tty wakeup callback.
  324. */
  325. static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
  326. {
  327. register int actual;
  328. unsigned long flags;
  329. struct n_hdlc_buf *tbuf;
  330. if (debuglevel >= DEBUG_LEVEL_INFO)
  331. printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
  332. check_again:
  333. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
  334. if (n_hdlc->tbusy) {
  335. n_hdlc->woke_up = 1;
  336. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
  337. return;
  338. }
  339. n_hdlc->tbusy = 1;
  340. n_hdlc->woke_up = 0;
  341. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
  342. /* get current transmit buffer or get new transmit */
  343. /* buffer from list of pending transmit buffers */
  344. tbuf = n_hdlc->tbuf;
  345. if (!tbuf)
  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. tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
  353. actual = tty->driver->write(tty, tbuf->buf, tbuf->count);
  354. /* if transmit error, throw frame away by */
  355. /* pretending it was accepted by driver */
  356. if (actual < 0)
  357. actual = tbuf->count;
  358. if (actual == tbuf->count) {
  359. if (debuglevel >= DEBUG_LEVEL_INFO)
  360. printk("%s(%d)frame %p completed\n",
  361. __FILE__,__LINE__,tbuf);
  362. /* free current transmit buffer */
  363. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
  364. /* this tx buffer is done */
  365. n_hdlc->tbuf = NULL;
  366. /* wait up sleeping writers */
  367. wake_up_interruptible(&tty->write_wait);
  368. /* get next pending transmit buffer */
  369. tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
  370. } else {
  371. if (debuglevel >= DEBUG_LEVEL_INFO)
  372. printk("%s(%d)frame %p pending\n",
  373. __FILE__,__LINE__,tbuf);
  374. /* buffer not accepted by driver */
  375. /* set this buffer as pending buffer */
  376. n_hdlc->tbuf = tbuf;
  377. break;
  378. }
  379. }
  380. if (!tbuf)
  381. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  382. /* Clear the re-entry flag */
  383. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
  384. n_hdlc->tbusy = 0;
  385. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
  386. if (n_hdlc->woke_up)
  387. goto check_again;
  388. if (debuglevel >= DEBUG_LEVEL_INFO)
  389. printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
  390. } /* end of n_hdlc_send_frames() */
  391. /**
  392. * n_hdlc_tty_wakeup - Callback for transmit wakeup
  393. * @tty - pointer to associated tty instance data
  394. *
  395. * Called when low level device driver can accept more send data.
  396. */
  397. static void n_hdlc_tty_wakeup(struct tty_struct *tty)
  398. {
  399. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  400. if (debuglevel >= DEBUG_LEVEL_INFO)
  401. printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
  402. if (!n_hdlc)
  403. return;
  404. if (tty != n_hdlc->tty) {
  405. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  406. return;
  407. }
  408. n_hdlc_send_frames (n_hdlc, tty);
  409. } /* end of n_hdlc_tty_wakeup() */
  410. /**
  411. * n_hdlc_tty_receive - Called by tty driver when receive data is available
  412. * @tty - pointer to tty instance data
  413. * @data - pointer to received data
  414. * @flags - pointer to flags for data
  415. * @count - count of received data in bytes
  416. *
  417. * Called by tty low level driver when receive data is available. Data is
  418. * interpreted as one HDLC frame.
  419. */
  420. static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
  421. char *flags, int count)
  422. {
  423. register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  424. register struct n_hdlc_buf *buf;
  425. if (debuglevel >= DEBUG_LEVEL_INFO)
  426. printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
  427. __FILE__,__LINE__, count);
  428. /* This can happen if stuff comes in on the backup tty */
  429. if (n_hdlc == 0 || tty != n_hdlc->tty)
  430. return;
  431. /* verify line is using HDLC discipline */
  432. if (n_hdlc->magic != HDLC_MAGIC) {
  433. printk("%s(%d) line not using HDLC discipline\n",
  434. __FILE__,__LINE__);
  435. return;
  436. }
  437. if ( count>maxframe ) {
  438. if (debuglevel >= DEBUG_LEVEL_INFO)
  439. printk("%s(%d) rx count>maxframesize, data discarded\n",
  440. __FILE__,__LINE__);
  441. return;
  442. }
  443. /* get a free HDLC buffer */
  444. buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
  445. if (!buf) {
  446. /* no buffers in free list, attempt to allocate another rx buffer */
  447. /* unless the maximum count has been reached */
  448. if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
  449. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC);
  450. }
  451. if (!buf) {
  452. if (debuglevel >= DEBUG_LEVEL_INFO)
  453. printk("%s(%d) no more rx buffers, data discarded\n",
  454. __FILE__,__LINE__);
  455. return;
  456. }
  457. /* copy received data to HDLC buffer */
  458. memcpy(buf->buf,data,count);
  459. buf->count=count;
  460. /* add HDLC buffer to list of received frames */
  461. n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
  462. /* wake up any blocked reads and perform async signalling */
  463. wake_up_interruptible (&tty->read_wait);
  464. if (n_hdlc->tty->fasync != NULL)
  465. kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
  466. } /* end of n_hdlc_tty_receive() */
  467. /**
  468. * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
  469. * @tty - pointer to tty instance data
  470. * @file - pointer to open file object
  471. * @buf - pointer to returned data buffer
  472. * @nr - size of returned data buffer
  473. *
  474. * Returns the number of bytes returned or error code.
  475. */
  476. static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
  477. __u8 __user *buf, size_t nr)
  478. {
  479. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  480. int ret;
  481. struct n_hdlc_buf *rbuf;
  482. if (debuglevel >= DEBUG_LEVEL_INFO)
  483. printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
  484. /* Validate the pointers */
  485. if (!n_hdlc)
  486. return -EIO;
  487. /* verify user access to buffer */
  488. if (!access_ok(VERIFY_WRITE, buf, nr)) {
  489. printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
  490. "buffer\n", __FILE__, __LINE__);
  491. return -EFAULT;
  492. }
  493. for (;;) {
  494. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  495. return -EIO;
  496. n_hdlc = tty2n_hdlc (tty);
  497. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
  498. tty != n_hdlc->tty)
  499. return 0;
  500. rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
  501. if (rbuf)
  502. break;
  503. /* no data */
  504. if (file->f_flags & O_NONBLOCK)
  505. return -EAGAIN;
  506. interruptible_sleep_on (&tty->read_wait);
  507. if (signal_pending(current))
  508. return -EINTR;
  509. }
  510. if (rbuf->count > nr)
  511. /* frame too large for caller's buffer (discard frame) */
  512. ret = -EOVERFLOW;
  513. else {
  514. /* Copy the data to the caller's buffer */
  515. if (copy_to_user(buf, rbuf->buf, rbuf->count))
  516. ret = -EFAULT;
  517. else
  518. ret = rbuf->count;
  519. }
  520. /* return HDLC buffer to free list unless the free list */
  521. /* count has exceeded the default value, in which case the */
  522. /* buffer is freed back to the OS to conserve memory */
  523. if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
  524. kfree(rbuf);
  525. else
  526. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
  527. return ret;
  528. } /* end of n_hdlc_tty_read() */
  529. /**
  530. * n_hdlc_tty_write - write a single frame of data to device
  531. * @tty - pointer to associated tty device instance data
  532. * @file - pointer to file object data
  533. * @data - pointer to transmit data (one frame)
  534. * @count - size of transmit frame in bytes
  535. *
  536. * Returns the number of bytes written (or error code).
  537. */
  538. static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
  539. const unsigned char *data, size_t count)
  540. {
  541. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  542. int error = 0;
  543. DECLARE_WAITQUEUE(wait, current);
  544. struct n_hdlc_buf *tbuf;
  545. if (debuglevel >= DEBUG_LEVEL_INFO)
  546. printk("%s(%d)n_hdlc_tty_write() called count=%Zd\n",
  547. __FILE__,__LINE__,count);
  548. /* Verify pointers */
  549. if (!n_hdlc)
  550. return -EIO;
  551. if (n_hdlc->magic != HDLC_MAGIC)
  552. return -EIO;
  553. /* verify frame size */
  554. if (count > maxframe ) {
  555. if (debuglevel & DEBUG_LEVEL_INFO)
  556. printk (KERN_WARNING
  557. "n_hdlc_tty_write: truncating user packet "
  558. "from %lu to %d\n", (unsigned long) count,
  559. maxframe );
  560. count = maxframe;
  561. }
  562. add_wait_queue(&tty->write_wait, &wait);
  563. set_current_state(TASK_INTERRUPTIBLE);
  564. /* Allocate transmit buffer */
  565. /* sleep until transmit buffer available */
  566. while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) {
  567. schedule();
  568. n_hdlc = tty2n_hdlc (tty);
  569. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
  570. tty != n_hdlc->tty) {
  571. printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
  572. error = -EIO;
  573. break;
  574. }
  575. if (signal_pending(current)) {
  576. error = -EINTR;
  577. break;
  578. }
  579. }
  580. set_current_state(TASK_RUNNING);
  581. remove_wait_queue(&tty->write_wait, &wait);
  582. if (!error) {
  583. /* Retrieve the user's buffer */
  584. memcpy(tbuf->buf, data, count);
  585. /* Send the data */
  586. tbuf->count = error = count;
  587. n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
  588. n_hdlc_send_frames(n_hdlc,tty);
  589. }
  590. return error;
  591. } /* end of n_hdlc_tty_write() */
  592. /**
  593. * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
  594. * @tty - pointer to tty instance data
  595. * @file - pointer to open file object for device
  596. * @cmd - IOCTL command code
  597. * @arg - argument for IOCTL call (cmd dependent)
  598. *
  599. * Returns command dependent result.
  600. */
  601. static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
  602. unsigned int cmd, unsigned long arg)
  603. {
  604. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  605. int error = 0;
  606. int count;
  607. unsigned long flags;
  608. if (debuglevel >= DEBUG_LEVEL_INFO)
  609. printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
  610. __FILE__,__LINE__,cmd);
  611. /* Verify the status of the device */
  612. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
  613. return -EBADF;
  614. switch (cmd) {
  615. case FIONREAD:
  616. /* report count of read data available */
  617. /* in next available frame (if any) */
  618. spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
  619. if (n_hdlc->rx_buf_list.head)
  620. count = n_hdlc->rx_buf_list.head->count;
  621. else
  622. count = 0;
  623. spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
  624. error = put_user(count, (int __user *)arg);
  625. break;
  626. case TIOCOUTQ:
  627. /* get the pending tx byte count in the driver */
  628. count = tty->driver->chars_in_buffer ?
  629. tty->driver->chars_in_buffer(tty) : 0;
  630. /* add size of next output frame in queue */
  631. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
  632. if (n_hdlc->tx_buf_list.head)
  633. count += n_hdlc->tx_buf_list.head->count;
  634. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
  635. error = put_user(count, (int __user *)arg);
  636. break;
  637. default:
  638. error = n_tty_ioctl (tty, file, cmd, arg);
  639. break;
  640. }
  641. return error;
  642. } /* end of n_hdlc_tty_ioctl() */
  643. /**
  644. * n_hdlc_tty_poll - TTY callback for poll system call
  645. * @tty - pointer to tty instance data
  646. * @filp - pointer to open file object for device
  647. * @poll_table - wait queue for operations
  648. *
  649. * Determine which operations (read/write) will not block and return info
  650. * to caller.
  651. * Returns a bit mask containing info on which ops will not block.
  652. */
  653. static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
  654. poll_table *wait)
  655. {
  656. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  657. unsigned int mask = 0;
  658. if (debuglevel >= DEBUG_LEVEL_INFO)
  659. printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
  660. if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
  661. /* queue current process into any wait queue that */
  662. /* may awaken in the future (read and write) */
  663. poll_wait(filp, &tty->read_wait, wait);
  664. poll_wait(filp, &tty->write_wait, wait);
  665. /* set bits for operations that won't block */
  666. if(n_hdlc->rx_buf_list.head)
  667. mask |= POLLIN | POLLRDNORM; /* readable */
  668. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  669. mask |= POLLHUP;
  670. if(tty_hung_up_p(filp))
  671. mask |= POLLHUP;
  672. if(n_hdlc->tx_free_buf_list.head)
  673. mask |= POLLOUT | POLLWRNORM; /* writable */
  674. }
  675. return mask;
  676. } /* end of n_hdlc_tty_poll() */
  677. /**
  678. * n_hdlc_alloc - allocate an n_hdlc instance data structure
  679. *
  680. * Returns a pointer to newly created structure if success, otherwise %NULL
  681. */
  682. static struct n_hdlc *n_hdlc_alloc(void)
  683. {
  684. struct n_hdlc_buf *buf;
  685. int i;
  686. struct n_hdlc *n_hdlc = kmalloc(sizeof(*n_hdlc), GFP_KERNEL);
  687. if (!n_hdlc)
  688. return NULL;
  689. memset(n_hdlc, 0, sizeof(*n_hdlc));
  690. n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list);
  691. n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list);
  692. n_hdlc_buf_list_init(&n_hdlc->rx_buf_list);
  693. n_hdlc_buf_list_init(&n_hdlc->tx_buf_list);
  694. /* allocate free rx buffer list */
  695. for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
  696. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  697. if (buf)
  698. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
  699. else if (debuglevel >= DEBUG_LEVEL_INFO)
  700. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
  701. }
  702. /* allocate free tx buffer list */
  703. for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
  704. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  705. if (buf)
  706. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
  707. else if (debuglevel >= DEBUG_LEVEL_INFO)
  708. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
  709. }
  710. /* Initialize the control block */
  711. n_hdlc->magic = HDLC_MAGIC;
  712. n_hdlc->flags = 0;
  713. return n_hdlc;
  714. } /* end of n_hdlc_alloc() */
  715. /**
  716. * n_hdlc_buf_list_init - initialize specified HDLC buffer list
  717. * @list - pointer to buffer list
  718. */
  719. static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list)
  720. {
  721. memset(list, 0, sizeof(*list));
  722. spin_lock_init(&list->spinlock);
  723. } /* end of n_hdlc_buf_list_init() */
  724. /**
  725. * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
  726. * @list - pointer to buffer list
  727. * @buf - pointer to buffer
  728. */
  729. static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
  730. struct n_hdlc_buf *buf)
  731. {
  732. unsigned long flags;
  733. spin_lock_irqsave(&list->spinlock,flags);
  734. buf->link=NULL;
  735. if(list->tail)
  736. list->tail->link = buf;
  737. else
  738. list->head = buf;
  739. list->tail = buf;
  740. (list->count)++;
  741. spin_unlock_irqrestore(&list->spinlock,flags);
  742. } /* end of n_hdlc_buf_put() */
  743. /**
  744. * n_hdlc_buf_get - remove and return an HDLC buffer from list
  745. * @list - pointer to HDLC buffer list
  746. *
  747. * Remove and return an HDLC buffer from the head of the specified HDLC buffer
  748. * list.
  749. * Returns a pointer to HDLC buffer if available, otherwise %NULL.
  750. */
  751. static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list)
  752. {
  753. unsigned long flags;
  754. struct n_hdlc_buf *buf;
  755. spin_lock_irqsave(&list->spinlock,flags);
  756. buf = list->head;
  757. if (buf) {
  758. list->head = buf->link;
  759. (list->count)--;
  760. }
  761. if (!list->head)
  762. list->tail = NULL;
  763. spin_unlock_irqrestore(&list->spinlock,flags);
  764. return buf;
  765. } /* end of n_hdlc_buf_get() */
  766. static char hdlc_banner[] __initdata =
  767. KERN_INFO "HDLC line discipline: version " HDLC_VERSION
  768. ", maxframe=%u\n";
  769. static char hdlc_register_ok[] __initdata =
  770. KERN_INFO "N_HDLC line discipline registered.\n";
  771. static char hdlc_register_fail[] __initdata =
  772. KERN_ERR "error registering line discipline: %d\n";
  773. static char hdlc_init_fail[] __initdata =
  774. KERN_INFO "N_HDLC: init failure %d\n";
  775. static int __init n_hdlc_init(void)
  776. {
  777. int status;
  778. /* range check maxframe arg */
  779. if (maxframe < 4096)
  780. maxframe = 4096;
  781. else if (maxframe > 65535)
  782. maxframe = 65535;
  783. printk(hdlc_banner, maxframe);
  784. status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
  785. if (!status)
  786. printk(hdlc_register_ok);
  787. else
  788. printk(hdlc_register_fail, status);
  789. if (status)
  790. printk(hdlc_init_fail, status);
  791. return status;
  792. } /* end of init_module() */
  793. static char hdlc_unregister_ok[] __exitdata =
  794. KERN_INFO "N_HDLC: line discipline unregistered\n";
  795. static char hdlc_unregister_fail[] __exitdata =
  796. KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
  797. static void __exit n_hdlc_exit(void)
  798. {
  799. /* Release tty registration of line discipline */
  800. int status = tty_unregister_ldisc(N_HDLC);
  801. if (status)
  802. printk(hdlc_unregister_fail, status);
  803. else
  804. printk(hdlc_unregister_ok);
  805. }
  806. module_init(n_hdlc_init);
  807. module_exit(n_hdlc_exit);
  808. MODULE_LICENSE("GPL");
  809. MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
  810. module_param(debuglevel, int, 0);
  811. module_param(maxframe, int, 0);
  812. MODULE_ALIAS_LDISC(N_HDLC);