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