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/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 = 0;
  503. struct n_hdlc_buf *rbuf;
  504. DECLARE_WAITQUEUE(wait, current);
  505. if (debuglevel >= DEBUG_LEVEL_INFO)
  506. printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
  507. /* Validate the pointers */
  508. if (!n_hdlc)
  509. return -EIO;
  510. /* verify user access to buffer */
  511. if (!access_ok(VERIFY_WRITE, buf, nr)) {
  512. printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
  513. "buffer\n", __FILE__, __LINE__);
  514. return -EFAULT;
  515. }
  516. add_wait_queue(&tty->read_wait, &wait);
  517. for (;;) {
  518. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  519. ret = -EIO;
  520. break;
  521. }
  522. if (tty_hung_up_p(file))
  523. break;
  524. set_current_state(TASK_INTERRUPTIBLE);
  525. rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
  526. if (rbuf) {
  527. if (rbuf->count > nr) {
  528. /* too large for caller's buffer */
  529. ret = -EOVERFLOW;
  530. } else {
  531. if (copy_to_user(buf, rbuf->buf, rbuf->count))
  532. ret = -EFAULT;
  533. else
  534. ret = rbuf->count;
  535. }
  536. if (n_hdlc->rx_free_buf_list.count >
  537. DEFAULT_RX_BUF_COUNT)
  538. kfree(rbuf);
  539. else
  540. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
  541. break;
  542. }
  543. /* no data */
  544. if (file->f_flags & O_NONBLOCK) {
  545. ret = -EAGAIN;
  546. break;
  547. }
  548. schedule();
  549. if (signal_pending(current)) {
  550. ret = -EINTR;
  551. break;
  552. }
  553. }
  554. remove_wait_queue(&tty->read_wait, &wait);
  555. __set_current_state(TASK_RUNNING);
  556. return ret;
  557. } /* end of n_hdlc_tty_read() */
  558. /**
  559. * n_hdlc_tty_write - write a single frame of data to device
  560. * @tty - pointer to associated tty device instance data
  561. * @file - pointer to file object data
  562. * @data - pointer to transmit data (one frame)
  563. * @count - size of transmit frame in bytes
  564. *
  565. * Returns the number of bytes written (or error code).
  566. */
  567. static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
  568. const unsigned char *data, size_t count)
  569. {
  570. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  571. int error = 0;
  572. DECLARE_WAITQUEUE(wait, current);
  573. struct n_hdlc_buf *tbuf;
  574. if (debuglevel >= DEBUG_LEVEL_INFO)
  575. printk("%s(%d)n_hdlc_tty_write() called count=%Zd\n",
  576. __FILE__,__LINE__,count);
  577. /* Verify pointers */
  578. if (!n_hdlc)
  579. return -EIO;
  580. if (n_hdlc->magic != HDLC_MAGIC)
  581. return -EIO;
  582. /* verify frame size */
  583. if (count > maxframe ) {
  584. if (debuglevel & DEBUG_LEVEL_INFO)
  585. printk (KERN_WARNING
  586. "n_hdlc_tty_write: truncating user packet "
  587. "from %lu to %d\n", (unsigned long) count,
  588. maxframe );
  589. count = maxframe;
  590. }
  591. add_wait_queue(&tty->write_wait, &wait);
  592. for (;;) {
  593. set_current_state(TASK_INTERRUPTIBLE);
  594. tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
  595. if (tbuf)
  596. break;
  597. if (file->f_flags & O_NONBLOCK) {
  598. error = -EAGAIN;
  599. break;
  600. }
  601. schedule();
  602. n_hdlc = tty2n_hdlc (tty);
  603. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
  604. tty != n_hdlc->tty) {
  605. printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
  606. error = -EIO;
  607. break;
  608. }
  609. if (signal_pending(current)) {
  610. error = -EINTR;
  611. break;
  612. }
  613. }
  614. __set_current_state(TASK_RUNNING);
  615. remove_wait_queue(&tty->write_wait, &wait);
  616. if (!error) {
  617. /* Retrieve the user's buffer */
  618. memcpy(tbuf->buf, data, count);
  619. /* Send the data */
  620. tbuf->count = error = count;
  621. n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
  622. n_hdlc_send_frames(n_hdlc,tty);
  623. }
  624. return error;
  625. } /* end of n_hdlc_tty_write() */
  626. /**
  627. * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
  628. * @tty - pointer to tty instance data
  629. * @file - pointer to open file object for device
  630. * @cmd - IOCTL command code
  631. * @arg - argument for IOCTL call (cmd dependent)
  632. *
  633. * Returns command dependent result.
  634. */
  635. static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
  636. unsigned int cmd, unsigned long arg)
  637. {
  638. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  639. int error = 0;
  640. int count;
  641. unsigned long flags;
  642. if (debuglevel >= DEBUG_LEVEL_INFO)
  643. printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
  644. __FILE__,__LINE__,cmd);
  645. /* Verify the status of the device */
  646. if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
  647. return -EBADF;
  648. switch (cmd) {
  649. case FIONREAD:
  650. /* report count of read data available */
  651. /* in next available frame (if any) */
  652. spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
  653. if (n_hdlc->rx_buf_list.head)
  654. count = n_hdlc->rx_buf_list.head->count;
  655. else
  656. count = 0;
  657. spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
  658. error = put_user(count, (int __user *)arg);
  659. break;
  660. case TIOCOUTQ:
  661. /* get the pending tx byte count in the driver */
  662. count = tty_chars_in_buffer(tty);
  663. /* add size of next output frame in queue */
  664. spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
  665. if (n_hdlc->tx_buf_list.head)
  666. count += n_hdlc->tx_buf_list.head->count;
  667. spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
  668. error = put_user(count, (int __user *)arg);
  669. break;
  670. case TCFLSH:
  671. switch (arg) {
  672. case TCIOFLUSH:
  673. case TCOFLUSH:
  674. flush_tx_queue(tty);
  675. }
  676. /* fall through to default */
  677. default:
  678. error = n_tty_ioctl_helper(tty, file, cmd, arg);
  679. break;
  680. }
  681. return error;
  682. } /* end of n_hdlc_tty_ioctl() */
  683. /**
  684. * n_hdlc_tty_poll - TTY callback for poll system call
  685. * @tty - pointer to tty instance data
  686. * @filp - pointer to open file object for device
  687. * @poll_table - wait queue for operations
  688. *
  689. * Determine which operations (read/write) will not block and return info
  690. * to caller.
  691. * Returns a bit mask containing info on which ops will not block.
  692. */
  693. static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
  694. poll_table *wait)
  695. {
  696. struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
  697. unsigned int mask = 0;
  698. if (debuglevel >= DEBUG_LEVEL_INFO)
  699. printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
  700. if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
  701. /* queue current process into any wait queue that */
  702. /* may awaken in the future (read and write) */
  703. poll_wait(filp, &tty->read_wait, wait);
  704. poll_wait(filp, &tty->write_wait, wait);
  705. /* set bits for operations that won't block */
  706. if (n_hdlc->rx_buf_list.head)
  707. mask |= POLLIN | POLLRDNORM; /* readable */
  708. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  709. mask |= POLLHUP;
  710. if (tty_hung_up_p(filp))
  711. mask |= POLLHUP;
  712. if (!tty_is_writelocked(tty) &&
  713. n_hdlc->tx_free_buf_list.head)
  714. mask |= POLLOUT | POLLWRNORM; /* writable */
  715. }
  716. return mask;
  717. } /* end of n_hdlc_tty_poll() */
  718. /**
  719. * n_hdlc_alloc - allocate an n_hdlc instance data structure
  720. *
  721. * Returns a pointer to newly created structure if success, otherwise %NULL
  722. */
  723. static struct n_hdlc *n_hdlc_alloc(void)
  724. {
  725. struct n_hdlc_buf *buf;
  726. int i;
  727. struct n_hdlc *n_hdlc = kmalloc(sizeof(*n_hdlc), GFP_KERNEL);
  728. if (!n_hdlc)
  729. return NULL;
  730. memset(n_hdlc, 0, sizeof(*n_hdlc));
  731. n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list);
  732. n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list);
  733. n_hdlc_buf_list_init(&n_hdlc->rx_buf_list);
  734. n_hdlc_buf_list_init(&n_hdlc->tx_buf_list);
  735. /* allocate free rx buffer list */
  736. for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
  737. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  738. if (buf)
  739. n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
  740. else if (debuglevel >= DEBUG_LEVEL_INFO)
  741. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
  742. }
  743. /* allocate free tx buffer list */
  744. for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
  745. buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
  746. if (buf)
  747. n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
  748. else if (debuglevel >= DEBUG_LEVEL_INFO)
  749. printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
  750. }
  751. /* Initialize the control block */
  752. n_hdlc->magic = HDLC_MAGIC;
  753. n_hdlc->flags = 0;
  754. return n_hdlc;
  755. } /* end of n_hdlc_alloc() */
  756. /**
  757. * n_hdlc_buf_list_init - initialize specified HDLC buffer list
  758. * @list - pointer to buffer list
  759. */
  760. static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list)
  761. {
  762. memset(list, 0, sizeof(*list));
  763. spin_lock_init(&list->spinlock);
  764. } /* end of n_hdlc_buf_list_init() */
  765. /**
  766. * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
  767. * @list - pointer to buffer list
  768. * @buf - pointer to buffer
  769. */
  770. static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
  771. struct n_hdlc_buf *buf)
  772. {
  773. unsigned long flags;
  774. spin_lock_irqsave(&list->spinlock,flags);
  775. buf->link=NULL;
  776. if (list->tail)
  777. list->tail->link = buf;
  778. else
  779. list->head = buf;
  780. list->tail = buf;
  781. (list->count)++;
  782. spin_unlock_irqrestore(&list->spinlock,flags);
  783. } /* end of n_hdlc_buf_put() */
  784. /**
  785. * n_hdlc_buf_get - remove and return an HDLC buffer from list
  786. * @list - pointer to HDLC buffer list
  787. *
  788. * Remove and return an HDLC buffer from the head of the specified HDLC buffer
  789. * list.
  790. * Returns a pointer to HDLC buffer if available, otherwise %NULL.
  791. */
  792. static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list)
  793. {
  794. unsigned long flags;
  795. struct n_hdlc_buf *buf;
  796. spin_lock_irqsave(&list->spinlock,flags);
  797. buf = list->head;
  798. if (buf) {
  799. list->head = buf->link;
  800. (list->count)--;
  801. }
  802. if (!list->head)
  803. list->tail = NULL;
  804. spin_unlock_irqrestore(&list->spinlock,flags);
  805. return buf;
  806. } /* end of n_hdlc_buf_get() */
  807. static char hdlc_banner[] __initdata =
  808. KERN_INFO "HDLC line discipline maxframe=%u\n";
  809. static char hdlc_register_ok[] __initdata =
  810. KERN_INFO "N_HDLC line discipline registered.\n";
  811. static char hdlc_register_fail[] __initdata =
  812. KERN_ERR "error registering line discipline: %d\n";
  813. static char hdlc_init_fail[] __initdata =
  814. KERN_INFO "N_HDLC: init failure %d\n";
  815. static int __init n_hdlc_init(void)
  816. {
  817. int status;
  818. /* range check maxframe arg */
  819. if (maxframe < 4096)
  820. maxframe = 4096;
  821. else if (maxframe > 65535)
  822. maxframe = 65535;
  823. printk(hdlc_banner, maxframe);
  824. status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
  825. if (!status)
  826. printk(hdlc_register_ok);
  827. else
  828. printk(hdlc_register_fail, status);
  829. if (status)
  830. printk(hdlc_init_fail, status);
  831. return status;
  832. } /* end of init_module() */
  833. static char hdlc_unregister_ok[] __exitdata =
  834. KERN_INFO "N_HDLC: line discipline unregistered\n";
  835. static char hdlc_unregister_fail[] __exitdata =
  836. KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
  837. static void __exit n_hdlc_exit(void)
  838. {
  839. /* Release tty registration of line discipline */
  840. int status = tty_unregister_ldisc(N_HDLC);
  841. if (status)
  842. printk(hdlc_unregister_fail, status);
  843. else
  844. printk(hdlc_unregister_ok);
  845. }
  846. module_init(n_hdlc_init);
  847. module_exit(n_hdlc_exit);
  848. MODULE_LICENSE("GPL");
  849. MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
  850. module_param(debuglevel, int, 0);
  851. module_param(maxframe, int, 0);
  852. MODULE_ALIAS_LDISC(N_HDLC);