n_hdlc.c 27 KB

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