n_hdlc.c 28 KB

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