n_hdlc.c 27 KB

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