n_hdlc.c 27 KB

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