n_hdlc.c 27 KB

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