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