n_hdlc.c 27 KB

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