n_hdlc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. *
  14. * This code is released under the GNU General Public License (GPL)
  15. *
  16. * This module implements the tty line discipline N_HDLC for use with
  17. * tty device drivers that support bit-synchronous HDLC communications.
  18. *
  19. * All HDLC data is frame oriented which means:
  20. *
  21. * 1. tty write calls represent one complete transmit frame of data
  22. * The device driver should accept the complete frame or none of
  23. * the frame (busy) in the write method. Each write call should have
  24. * a byte count in the range of 2-65535 bytes (2 is min HDLC frame
  25. * with 1 addr byte and 1 ctrl byte). The max byte count of 65535
  26. * should include any crc bytes required. For example, when using
  27. * CCITT CRC32, 4 crc bytes are required, so the maximum size frame
  28. * the application may transmit is limited to 65531 bytes. For CCITT
  29. * CRC16, the maximum application frame size would be 65533.
  30. *
  31. *
  32. * 2. receive callbacks from the device driver represents
  33. * one received frame. The device driver should bypass
  34. * the tty flip buffer and call the line discipline receive
  35. * callback directly to avoid fragmenting or concatenating
  36. * multiple frames into a single receive callback.
  37. *
  38. * The HDLC line discipline queues the receive frames in separate
  39. * buffers so complete receive frames can be returned by the
  40. * tty read calls.
  41. *
  42. * 3. tty read calls returns an entire frame of data or nothing.
  43. *
  44. * 4. all send and receive data is considered raw. No processing
  45. * or translation is performed by the line discipline, regardless
  46. * of the tty flags
  47. *
  48. * 5. When line discipline is queried for the amount of receive
  49. * data available (FIOC), 0 is returned if no data available,
  50. * otherwise the count of the next available frame is returned.
  51. * (instead of the sum of all received frame counts).
  52. *
  53. * These conventions allow the standard tty programming interface
  54. * to be used for synchronous HDLC applications when used with
  55. * this line discipline (or another line discipline that is frame
  56. * oriented such as N_PPP).
  57. *
  58. * The SyncLink driver (synclink.c) implements both asynchronous
  59. * (using standard line discipline N_TTY) and synchronous HDLC
  60. * (using N_HDLC) communications, with the latter using the above
  61. * conventions.
  62. *
  63. * This implementation is very basic and does not maintain
  64. * any statistics. The main point is to enforce the raw data
  65. * and frame orientation of HDLC communications.
  66. *
  67. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  68. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  69. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  70. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  71. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  72. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  74. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  75. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  76. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  77. * OF THE POSSIBILITY OF SUCH DAMAGE.
  78. */
  79. #define HDLC_MAGIC 0x239e
  80. #include <linux/module.h>
  81. #include <linux/init.h>
  82. #include <linux/kernel.h>
  83. #include <linux/sched.h>
  84. #include <linux/types.h>
  85. #include <linux/fcntl.h>
  86. #include <linux/interrupt.h>
  87. #include <linux/ptrace.h>
  88. #undef VERSION
  89. #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
  90. #include <linux/poll.h>
  91. #include <linux/in.h>
  92. #include <linux/ioctl.h>
  93. #include <linux/slab.h>
  94. #include <linux/tty.h>
  95. #include <linux/errno.h>
  96. #include <linux/string.h> /* used in new tty drivers */
  97. #include <linux/signal.h> /* used in new tty drivers */
  98. #include <linux/if.h>
  99. #include <linux/bitops.h>
  100. #include <asm/termios.h>
  101. #include <asm/uaccess.h>
  102. /*
  103. * Buffers for individual HDLC frames
  104. */
  105. #define MAX_HDLC_FRAME_SIZE 65535
  106. #define DEFAULT_RX_BUF_COUNT 10
  107. #define MAX_RX_BUF_COUNT 60
  108. #define DEFAULT_TX_BUF_COUNT 3
  109. struct n_hdlc_buf {
  110. struct list_head list_item;
  111. int count;
  112. char buf[1];
  113. };
  114. #define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
  115. struct n_hdlc_buf_list {
  116. struct list_head list;
  117. int count;
  118. spinlock_t spinlock;
  119. };
  120. /**
  121. * struct n_hdlc - per device instance data structure
  122. * @magic - magic value for structure
  123. * @flags - miscellaneous control flags
  124. * @tty - ptr to TTY structure
  125. * @backup_tty - TTY to use if tty gets closed
  126. * @tbusy - reentrancy flag for tx wakeup code
  127. * @woke_up - FIXME: describe this field
  128. * @tx_buf_list - list of pending transmit frame buffers
  129. * @rx_buf_list - list of received frame buffers
  130. * @tx_free_buf_list - list unused transmit frame buffers
  131. * @rx_free_buf_list - list unused received frame buffers
  132. */
  133. struct n_hdlc {
  134. int magic;
  135. __u32 flags;
  136. struct tty_struct *tty;
  137. struct tty_struct *backup_tty;
  138. int tbusy;
  139. int woke_up;
  140. struct n_hdlc_buf_list tx_buf_list;
  141. struct n_hdlc_buf_list rx_buf_list;
  142. struct n_hdlc_buf_list tx_free_buf_list;
  143. struct n_hdlc_buf_list rx_free_buf_list;
  144. };
  145. /*
  146. * HDLC buffer list manipulation functions
  147. */
  148. static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
  149. struct n_hdlc_buf *buf);
  150. static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
  151. struct n_hdlc_buf *buf);
  152. static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
  153. /* Local functions */
  154. static struct n_hdlc *n_hdlc_alloc (void);
  155. /* debug level can be set by insmod for debugging purposes */
  156. #define DEBUG_LEVEL_INFO 1
  157. static int debuglevel;
  158. /* max frame size for memory allocations */
  159. static int maxframe = 4096;
  160. /* TTY callbacks */
  161. static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
  162. __u8 __user *buf, size_t nr);
  163. static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
  164. const unsigned char *buf, size_t nr);
  165. static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
  166. unsigned int cmd, unsigned long arg);
  167. static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
  168. poll_table *wait);
  169. static int n_hdlc_tty_open(struct tty_struct *tty);
  170. static void n_hdlc_tty_close(struct tty_struct *tty);
  171. static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
  172. char *fp, int count);
  173. static void n_hdlc_tty_wakeup(struct tty_struct *tty);
  174. #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
  175. #define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
  176. #define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
  177. static void flush_rx_queue(struct tty_struct *tty)
  178. {
  179. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  180. struct n_hdlc_buf *buf;
  181. while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
  182. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
  183. }
  184. static void flush_tx_queue(struct tty_struct *tty)
  185. {
  186. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  187. struct n_hdlc_buf *buf;
  188. while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
  189. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
  190. }
  191. static struct tty_ldisc_ops n_hdlc_ldisc = {
  192. .owner = THIS_MODULE,
  193. .magic = TTY_LDISC_MAGIC,
  194. .name = "hdlc",
  195. .open = n_hdlc_tty_open,
  196. .close = n_hdlc_tty_close,
  197. .read = n_hdlc_tty_read,
  198. .write = n_hdlc_tty_write,
  199. .ioctl = n_hdlc_tty_ioctl,
  200. .poll = n_hdlc_tty_poll,
  201. .receive_buf = n_hdlc_tty_receive,
  202. .write_wakeup = n_hdlc_tty_wakeup,
  203. .flush_buffer = flush_rx_queue,
  204. };
  205. /**
  206. * n_hdlc_release - release an n_hdlc per device line discipline info structure
  207. * @n_hdlc - per device line discipline info structure
  208. */
  209. static void n_hdlc_release(struct n_hdlc *n_hdlc)
  210. {
  211. struct tty_struct *tty = n_hdlc2tty (n_hdlc);
  212. struct n_hdlc_buf *buf;
  213. if (debuglevel >= DEBUG_LEVEL_INFO)
  214. printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
  215. /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
  216. wake_up_interruptible (&tty->read_wait);
  217. wake_up_interruptible (&tty->write_wait);
  218. if (tty->disc_data == n_hdlc)
  219. tty->disc_data = NULL; /* Break the tty->n_hdlc link */
  220. /* Release transmit and receive buffers */
  221. for(;;) {
  222. buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
  223. if (buf) {
  224. kfree(buf);
  225. } else
  226. break;
  227. }
  228. for(;;) {
  229. buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
  230. if (buf) {
  231. kfree(buf);
  232. } else
  233. break;
  234. }
  235. for(;;) {
  236. buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
  237. if (buf) {
  238. kfree(buf);
  239. } else
  240. break;
  241. }
  242. for(;;) {
  243. buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
  244. if (buf) {
  245. kfree(buf);
  246. } else
  247. break;
  248. }
  249. kfree(n_hdlc);
  250. } /* end of n_hdlc_release() */
  251. /**
  252. * n_hdlc_tty_close - line discipline close
  253. * @tty - pointer to tty info structure
  254. *
  255. * Called when the line discipline is changed to something
  256. * else, the tty is closed, or the tty detects a hangup.
  257. */
  258. static void n_hdlc_tty_close(struct tty_struct *tty)
  259. {
  260. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  261. if (debuglevel >= DEBUG_LEVEL_INFO)
  262. printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
  263. if (n_hdlc != NULL) {
  264. if (n_hdlc->magic != HDLC_MAGIC) {
  265. printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
  266. return;
  267. }
  268. #if defined(TTY_NO_WRITE_SPLIT)
  269. clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
  270. #endif
  271. tty->disc_data = NULL;
  272. if (tty == n_hdlc->backup_tty)
  273. n_hdlc->backup_tty = NULL;
  274. if (tty != n_hdlc->tty)
  275. return;
  276. if (n_hdlc->backup_tty) {
  277. n_hdlc->tty = n_hdlc->backup_tty;
  278. } else {
  279. n_hdlc_release (n_hdlc);
  280. }
  281. }
  282. if (debuglevel >= DEBUG_LEVEL_INFO)
  283. printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
  284. } /* end of n_hdlc_tty_close() */
  285. /**
  286. * n_hdlc_tty_open - called when line discipline changed to n_hdlc
  287. * @tty - pointer to tty info structure
  288. *
  289. * Returns 0 if success, otherwise error code
  290. */
  291. static int n_hdlc_tty_open (struct tty_struct *tty)
  292. {
  293. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  294. if (debuglevel >= DEBUG_LEVEL_INFO)
  295. printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
  296. __FILE__,__LINE__,
  297. tty->name);
  298. /* There should not be an existing table for this slot. */
  299. if (n_hdlc) {
  300. printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
  301. return -EEXIST;
  302. }
  303. n_hdlc = n_hdlc_alloc();
  304. if (!n_hdlc) {
  305. printk (KERN_ERR "n_hdlc_alloc failed\n");
  306. return -ENFILE;
  307. }
  308. tty->disc_data = n_hdlc;
  309. n_hdlc->tty = tty;
  310. tty->receive_room = 65536;
  311. #if defined(TTY_NO_WRITE_SPLIT)
  312. /* change tty_io write() to not split large writes into 8K chunks */
  313. set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
  314. #endif
  315. /* flush receive data from driver */
  316. tty_driver_flush_buffer(tty);
  317. if (debuglevel >= DEBUG_LEVEL_INFO)
  318. printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
  319. return 0;
  320. } /* end of n_tty_hdlc_open() */
  321. /**
  322. * n_hdlc_send_frames - send frames on pending send buffer list
  323. * @n_hdlc - pointer to ldisc instance data
  324. * @tty - pointer to tty instance data
  325. *
  326. * Send frames on pending send buffer list until the driver does not accept a
  327. * frame (busy) this function is called after adding a frame to the send buffer
  328. * list and by the tty wakeup callback.
  329. */
  330. static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
  331. {
  332. register int actual;
  333. unsigned long flags;
  334. struct n_hdlc_buf *tbuf;
  335. if (debuglevel >= DEBUG_LEVEL_INFO)
  336. printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
  337. check_again:
  338. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
  339. if (n_hdlc->tbusy) {
  340. n_hdlc->woke_up = 1;
  341. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
  342. return;
  343. }
  344. n_hdlc->tbusy = 1;
  345. n_hdlc->woke_up = 0;
  346. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
  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. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  354. actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
  355. /* rollback was possible and has been done */
  356. if (actual == -ERESTARTSYS) {
  357. n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
  358. break;
  359. }
  360. /* if transmit error, throw frame away by */
  361. /* pretending it was accepted by driver */
  362. if (actual < 0)
  363. actual = tbuf->count;
  364. if (actual == tbuf->count) {
  365. if (debuglevel >= DEBUG_LEVEL_INFO)
  366. printk("%s(%d)frame %p completed\n",
  367. __FILE__,__LINE__,tbuf);
  368. /* free current transmit buffer */
  369. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
  370. /* wait up sleeping writers */
  371. wake_up_interruptible(&tty->write_wait);
  372. /* get next pending transmit buffer */
  373. tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
  374. } else {
  375. if (debuglevel >= DEBUG_LEVEL_INFO)
  376. printk("%s(%d)frame %p pending\n",
  377. __FILE__,__LINE__,tbuf);
  378. /*
  379. * the buffer was not accepted by driver,
  380. * return it back into tx queue
  381. */
  382. n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
  383. break;
  384. }
  385. }
  386. if (!tbuf)
  387. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  388. /* Clear the re-entry flag */
  389. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
  390. n_hdlc->tbusy = 0;
  391. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
  392. if (n_hdlc->woke_up)
  393. goto check_again;
  394. if (debuglevel >= DEBUG_LEVEL_INFO)
  395. printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
  396. } /* end of n_hdlc_send_frames() */
  397. /**
  398. * n_hdlc_tty_wakeup - Callback for transmit wakeup
  399. * @tty - pointer to associated tty instance data
  400. *
  401. * Called when low level device driver can accept more send data.
  402. */
  403. static void n_hdlc_tty_wakeup(struct tty_struct *tty)
  404. {
  405. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  406. if (debuglevel >= DEBUG_LEVEL_INFO)
  407. printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
  408. if (!n_hdlc)
  409. return;
  410. if (tty != n_hdlc->tty) {
  411. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  412. return;
  413. }
  414. n_hdlc_send_frames (n_hdlc, tty);
  415. } /* end of n_hdlc_tty_wakeup() */
  416. /**
  417. * n_hdlc_tty_receive - Called by tty driver when receive data is available
  418. * @tty - pointer to tty instance data
  419. * @data - pointer to received data
  420. * @flags - pointer to flags for data
  421. * @count - count of received data in bytes
  422. *
  423. * Called by tty low level driver when receive data is available. Data is
  424. * interpreted as one HDLC frame.
  425. */
  426. static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
  427. char *flags, int count)
  428. {
  429. register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  430. register struct n_hdlc_buf *buf;
  431. if (debuglevel >= DEBUG_LEVEL_INFO)
  432. printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
  433. __FILE__,__LINE__, count);
  434. /* This can happen if stuff comes in on the backup tty */
  435. if (!n_hdlc || tty != n_hdlc->tty)
  436. return;
  437. /* verify line is using HDLC discipline */
  438. if (n_hdlc->magic != HDLC_MAGIC) {
  439. printk("%s(%d) line not using HDLC discipline\n",
  440. __FILE__,__LINE__);
  441. return;
  442. }
  443. if ( count>maxframe ) {
  444. if (debuglevel >= DEBUG_LEVEL_INFO)
  445. printk("%s(%d) rx count>maxframesize, data discarded\n",
  446. __FILE__,__LINE__);
  447. return;
  448. }
  449. /* get a free HDLC buffer */
  450. buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
  451. if (!buf) {
  452. /* no buffers in free list, attempt to allocate another rx buffer */
  453. /* unless the maximum count has been reached */
  454. if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
  455. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC);
  456. }
  457. if (!buf) {
  458. if (debuglevel >= DEBUG_LEVEL_INFO)
  459. printk("%s(%d) no more rx buffers, data discarded\n",
  460. __FILE__,__LINE__);
  461. return;
  462. }
  463. /* copy received data to HDLC buffer */
  464. memcpy(buf->buf,data,count);
  465. buf->count=count;
  466. /* add HDLC buffer to list of received frames */
  467. n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
  468. /* wake up any blocked reads and perform async signalling */
  469. wake_up_interruptible (&tty->read_wait);
  470. if (n_hdlc->tty->fasync != NULL)
  471. kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
  472. } /* end of n_hdlc_tty_receive() */
  473. /**
  474. * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
  475. * @tty - pointer to tty instance data
  476. * @file - pointer to open file object
  477. * @buf - pointer to returned data buffer
  478. * @nr - size of returned data buffer
  479. *
  480. * Returns the number of bytes returned or error code.
  481. */
  482. static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
  483. __u8 __user *buf, size_t nr)
  484. {
  485. struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
  486. int ret = 0;
  487. struct n_hdlc_buf *rbuf;
  488. DECLARE_WAITQUEUE(wait, current);
  489. if (debuglevel >= DEBUG_LEVEL_INFO)
  490. printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
  491. /* Validate the pointers */
  492. if (!n_hdlc)
  493. return -EIO;
  494. /* verify user access to buffer */
  495. if (!access_ok(VERIFY_WRITE, buf, nr)) {
  496. printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
  497. "buffer\n", __FILE__, __LINE__);
  498. return -EFAULT;
  499. }
  500. add_wait_queue(&tty->read_wait, &wait);
  501. for (;;) {
  502. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  503. ret = -EIO;
  504. break;
  505. }
  506. if (tty_hung_up_p(file))
  507. break;
  508. set_current_state(TASK_INTERRUPTIBLE);
  509. rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
  510. if (rbuf) {
  511. if (rbuf->count > nr) {
  512. /* too large for caller's buffer */
  513. ret = -EOVERFLOW;
  514. } else {
  515. if (copy_to_user(buf, rbuf->buf, rbuf->count))
  516. ret = -EFAULT;
  517. else
  518. ret = rbuf->count;
  519. }
  520. if (n_hdlc->rx_free_buf_list.count >
  521. DEFAULT_RX_BUF_COUNT)
  522. kfree(rbuf);
  523. else
  524. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
  525. break;
  526. }
  527. /* no data */
  528. if (file->f_flags & O_NONBLOCK) {
  529. ret = -EAGAIN;
  530. break;
  531. }
  532. schedule();
  533. if (signal_pending(current)) {
  534. ret = -EINTR;
  535. break;
  536. }
  537. }
  538. remove_wait_queue(&tty->read_wait, &wait);
  539. __set_current_state(TASK_RUNNING);
  540. return ret;
  541. } /* end of n_hdlc_tty_read() */
  542. /**
  543. * n_hdlc_tty_write - write a single frame of data to device
  544. * @tty - pointer to associated tty device instance data
  545. * @file - pointer to file object data
  546. * @data - pointer to transmit data (one frame)
  547. * @count - size of transmit frame in bytes
  548. *
  549. * Returns the number of bytes written (or error code).
  550. */
  551. static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
  552. const unsigned char *data, size_t count)
  553. {
  554. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  555. int error = 0;
  556. DECLARE_WAITQUEUE(wait, current);
  557. struct n_hdlc_buf *tbuf;
  558. if (debuglevel >= DEBUG_LEVEL_INFO)
  559. printk("%s(%d)n_hdlc_tty_write() called count=%Zd\n",
  560. __FILE__,__LINE__,count);
  561. /* Verify pointers */
  562. if (!n_hdlc)
  563. return -EIO;
  564. if (n_hdlc->magic != HDLC_MAGIC)
  565. return -EIO;
  566. /* verify frame size */
  567. if (count > maxframe ) {
  568. if (debuglevel & DEBUG_LEVEL_INFO)
  569. printk (KERN_WARNING
  570. "n_hdlc_tty_write: truncating user packet "
  571. "from %lu to %d\n", (unsigned long) count,
  572. maxframe );
  573. count = maxframe;
  574. }
  575. add_wait_queue(&tty->write_wait, &wait);
  576. for (;;) {
  577. set_current_state(TASK_INTERRUPTIBLE);
  578. tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
  579. if (tbuf)
  580. break;
  581. if (file->f_flags & O_NONBLOCK) {
  582. error = -EAGAIN;
  583. break;
  584. }
  585. schedule();
  586. n_hdlc = tty2n_hdlc (tty);
  587. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
  588. tty != n_hdlc->tty) {
  589. printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
  590. error = -EIO;
  591. break;
  592. }
  593. if (signal_pending(current)) {
  594. error = -EINTR;
  595. break;
  596. }
  597. }
  598. __set_current_state(TASK_RUNNING);
  599. remove_wait_queue(&tty->write_wait, &wait);
  600. if (!error) {
  601. /* Retrieve the user's buffer */
  602. memcpy(tbuf->buf, data, count);
  603. /* Send the data */
  604. tbuf->count = error = count;
  605. n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
  606. n_hdlc_send_frames(n_hdlc,tty);
  607. }
  608. return error;
  609. } /* end of n_hdlc_tty_write() */
  610. /**
  611. * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
  612. * @tty - pointer to tty instance data
  613. * @file - pointer to open file object for device
  614. * @cmd - IOCTL command code
  615. * @arg - argument for IOCTL call (cmd dependent)
  616. *
  617. * Returns command dependent result.
  618. */
  619. static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
  620. unsigned int cmd, unsigned long arg)
  621. {
  622. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  623. int error = 0;
  624. int count;
  625. unsigned long flags;
  626. struct n_hdlc_buf *buf = NULL;
  627. if (debuglevel >= DEBUG_LEVEL_INFO)
  628. printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
  629. __FILE__,__LINE__,cmd);
  630. /* Verify the status of the device */
  631. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
  632. return -EBADF;
  633. switch (cmd) {
  634. case FIONREAD:
  635. /* report count of read data available */
  636. /* in next available frame (if any) */
  637. spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
  638. buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
  639. struct n_hdlc_buf, list_item);
  640. if (buf)
  641. count = buf->count;
  642. else
  643. count = 0;
  644. spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
  645. error = put_user(count, (int __user *)arg);
  646. break;
  647. case TIOCOUTQ:
  648. /* get the pending tx byte count in the driver */
  649. count = tty_chars_in_buffer(tty);
  650. /* add size of next output frame in queue */
  651. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
  652. buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
  653. struct n_hdlc_buf, list_item);
  654. if (buf)
  655. count += buf->count;
  656. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
  657. error = put_user(count, (int __user *)arg);
  658. break;
  659. case TCFLSH:
  660. switch (arg) {
  661. case TCIOFLUSH:
  662. case TCOFLUSH:
  663. flush_tx_queue(tty);
  664. }
  665. /* fall through to default */
  666. default:
  667. error = n_tty_ioctl_helper(tty, file, cmd, arg);
  668. break;
  669. }
  670. return error;
  671. } /* end of n_hdlc_tty_ioctl() */
  672. /**
  673. * n_hdlc_tty_poll - TTY callback for poll system call
  674. * @tty - pointer to tty instance data
  675. * @filp - pointer to open file object for device
  676. * @poll_table - wait queue for operations
  677. *
  678. * Determine which operations (read/write) will not block and return info
  679. * to caller.
  680. * Returns a bit mask containing info on which ops will not block.
  681. */
  682. static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
  683. poll_table *wait)
  684. {
  685. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  686. unsigned int mask = 0;
  687. if (debuglevel >= DEBUG_LEVEL_INFO)
  688. printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
  689. if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
  690. /* queue current process into any wait queue that */
  691. /* may awaken in the future (read and write) */
  692. poll_wait(filp, &tty->read_wait, wait);
  693. poll_wait(filp, &tty->write_wait, wait);
  694. /* set bits for operations that won't block */
  695. if (!list_empty(&n_hdlc->rx_buf_list.list))
  696. mask |= POLLIN | POLLRDNORM; /* readable */
  697. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  698. mask |= POLLHUP;
  699. if (tty_hung_up_p(filp))
  700. mask |= POLLHUP;
  701. if (!tty_is_writelocked(tty) &&
  702. !list_empty(&n_hdlc->tx_free_buf_list.list))
  703. mask |= POLLOUT | POLLWRNORM; /* writable */
  704. }
  705. return mask;
  706. } /* end of n_hdlc_tty_poll() */
  707. /**
  708. * n_hdlc_alloc - allocate an n_hdlc instance data structure
  709. *
  710. * Returns a pointer to newly created structure if success, otherwise %NULL
  711. */
  712. static struct n_hdlc *n_hdlc_alloc(void)
  713. {
  714. struct n_hdlc_buf *buf;
  715. int i;
  716. struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
  717. if (!n_hdlc)
  718. return NULL;
  719. spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock);
  720. spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock);
  721. spin_lock_init(&n_hdlc->rx_buf_list.spinlock);
  722. spin_lock_init(&n_hdlc->tx_buf_list.spinlock);
  723. INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list);
  724. INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list);
  725. INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
  726. INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
  727. /* allocate free rx buffer list */
  728. for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
  729. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  730. if (buf)
  731. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
  732. else if (debuglevel >= DEBUG_LEVEL_INFO)
  733. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
  734. }
  735. /* allocate free tx buffer list */
  736. for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
  737. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  738. if (buf)
  739. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
  740. else if (debuglevel >= DEBUG_LEVEL_INFO)
  741. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
  742. }
  743. /* Initialize the control block */
  744. n_hdlc->magic = HDLC_MAGIC;
  745. n_hdlc->flags = 0;
  746. return n_hdlc;
  747. } /* end of n_hdlc_alloc() */
  748. /**
  749. * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
  750. * @buf_list - pointer to the buffer list
  751. * @buf - pointer to the buffer
  752. */
  753. static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
  754. struct n_hdlc_buf *buf)
  755. {
  756. unsigned long flags;
  757. spin_lock_irqsave(&buf_list->spinlock, flags);
  758. list_add(&buf->list_item, &buf_list->list);
  759. buf_list->count++;
  760. spin_unlock_irqrestore(&buf_list->spinlock, flags);
  761. }
  762. /**
  763. * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
  764. * @buf_list - pointer to buffer list
  765. * @buf - pointer to buffer
  766. */
  767. static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
  768. struct n_hdlc_buf *buf)
  769. {
  770. unsigned long flags;
  771. spin_lock_irqsave(&buf_list->spinlock, flags);
  772. list_add_tail(&buf->list_item, &buf_list->list);
  773. buf_list->count++;
  774. spin_unlock_irqrestore(&buf_list->spinlock, flags);
  775. } /* end of n_hdlc_buf_put() */
  776. /**
  777. * n_hdlc_buf_get - remove and return an HDLC buffer from list
  778. * @buf_list - pointer to HDLC buffer list
  779. *
  780. * Remove and return an HDLC buffer from the head of the specified HDLC buffer
  781. * list.
  782. * Returns a pointer to HDLC buffer if available, otherwise %NULL.
  783. */
  784. static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
  785. {
  786. unsigned long flags;
  787. struct n_hdlc_buf *buf;
  788. spin_lock_irqsave(&buf_list->spinlock, flags);
  789. buf = list_first_entry_or_null(&buf_list->list,
  790. struct n_hdlc_buf, list_item);
  791. if (buf) {
  792. list_del(&buf->list_item);
  793. buf_list->count--;
  794. }
  795. spin_unlock_irqrestore(&buf_list->spinlock, flags);
  796. return buf;
  797. } /* end of n_hdlc_buf_get() */
  798. static char hdlc_banner[] __initdata =
  799. KERN_INFO "HDLC line discipline maxframe=%u\n";
  800. static char hdlc_register_ok[] __initdata =
  801. KERN_INFO "N_HDLC line discipline registered.\n";
  802. static char hdlc_register_fail[] __initdata =
  803. KERN_ERR "error registering line discipline: %d\n";
  804. static int __init n_hdlc_init(void)
  805. {
  806. int status;
  807. /* range check maxframe arg */
  808. if (maxframe < 4096)
  809. maxframe = 4096;
  810. else if (maxframe > 65535)
  811. maxframe = 65535;
  812. printk(hdlc_banner, maxframe);
  813. status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
  814. if (!status)
  815. printk(hdlc_register_ok);
  816. else
  817. printk(hdlc_register_fail, status);
  818. return status;
  819. } /* end of init_module() */
  820. static char hdlc_unregister_ok[] __exitdata =
  821. KERN_INFO "N_HDLC: line discipline unregistered\n";
  822. static char hdlc_unregister_fail[] __exitdata =
  823. KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
  824. static void __exit n_hdlc_exit(void)
  825. {
  826. /* Release tty registration of line discipline */
  827. int status = tty_unregister_ldisc(N_HDLC);
  828. if (status)
  829. printk(hdlc_unregister_fail, status);
  830. else
  831. printk(hdlc_unregister_ok);
  832. }
  833. module_init(n_hdlc_init);
  834. module_exit(n_hdlc_exit);
  835. MODULE_LICENSE("GPL");
  836. MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
  837. module_param(debuglevel, int, 0);
  838. module_param(maxframe, int, 0);
  839. MODULE_ALIAS_LDISC(N_HDLC);