Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / char / n_hdlc.c
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>, Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
10  *
11  * Original release 01/11/99
12  * $Id: n_hdlc.c,v 3.2 2000/11/06 22:34:38 paul Exp $
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 seperate
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
80 #define HDLC_MAGIC 0x239e
81 #define HDLC_VERSION "3.2"
82
83 #include <linux/version.h>
84 #include <linux/config.h>
85 #include <linux/module.h>
86 #include <linux/init.h>
87 #include <linux/kernel.h>
88 #include <linux/sched.h>
89 #include <linux/types.h>
90 #include <linux/fcntl.h>
91 #include <linux/interrupt.h>
92 #include <linux/ptrace.h>
93
94 #undef VERSION
95 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
96
97 #include <linux/poll.h>
98 #include <linux/in.h>
99 #include <linux/malloc.h>
100 #include <linux/tty.h>
101 #include <linux/errno.h>
102 #include <linux/string.h>       /* used in new tty drivers */
103 #include <linux/signal.h>       /* used in new tty drivers */
104 #include <asm/system.h>
105 #include <asm/bitops.h>
106 #include <asm/termios.h>
107 #include <linux/if.h>
108
109 #include <linux/ioctl.h>
110
111 #ifdef CONFIG_KERNELD
112 #include <linux/kerneld.h>
113 #endif
114
115 #include <asm/segment.h>
116 #define GET_USER(error,value,addr) error = get_user(value,addr)
117 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
118 #define PUT_USER(error,value,addr) error = put_user(value,addr)
119 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
120
121 #include <asm/uaccess.h>
122
123 typedef ssize_t         rw_ret_t;
124 typedef size_t          rw_count_t;
125
126 /*
127  * Buffers for individual HDLC frames
128  */
129 #define MAX_HDLC_FRAME_SIZE 65535 
130 #define DEFAULT_RX_BUF_COUNT 10
131 #define MAX_RX_BUF_COUNT 60
132 #define DEFAULT_TX_BUF_COUNT 1
133
134
135 typedef struct _n_hdlc_buf
136 {
137         struct _n_hdlc_buf *link;
138         int count;
139         char buf[1];
140 } N_HDLC_BUF;
141
142 #define N_HDLC_BUF_SIZE (sizeof(N_HDLC_BUF)+maxframe)
143
144 typedef struct _n_hdlc_buf_list
145 {
146         N_HDLC_BUF *head;
147         N_HDLC_BUF *tail;
148         int count;
149         spinlock_t spinlock;
150         
151 } N_HDLC_BUF_LIST;
152
153 /*
154  * Per device instance data structure
155  */
156 struct n_hdlc {
157         int             magic;          /* magic value for structure    */
158         __u32           flags;          /* miscellaneous control flags  */
159         
160         struct tty_struct *tty;         /* ptr to TTY structure */
161         struct tty_struct *backup_tty;  /* TTY to use if tty gets closed */
162         
163         /* Queues for select() functionality */
164         wait_queue_head_t read_wait;
165         wait_queue_head_t write_wait;
166         wait_queue_head_t poll_wait;
167
168         int             tbusy;          /* reentrancy flag for tx wakeup code */
169         int             woke_up;
170         N_HDLC_BUF      *tbuf;          /* currently transmitting tx buffer */
171         N_HDLC_BUF_LIST tx_buf_list;    /* list of pending transmit frame buffers */    
172         N_HDLC_BUF_LIST rx_buf_list;    /* list of received frame buffers */
173         N_HDLC_BUF_LIST tx_free_buf_list;       /* list unused transmit frame buffers */        
174         N_HDLC_BUF_LIST rx_free_buf_list;       /* list unused received frame buffers */
175 };
176
177 /*
178  * HDLC buffer list manipulation functions
179  */
180 void n_hdlc_buf_list_init(N_HDLC_BUF_LIST *list);
181 void n_hdlc_buf_put(N_HDLC_BUF_LIST *list,N_HDLC_BUF *buf);
182 N_HDLC_BUF* n_hdlc_buf_get(N_HDLC_BUF_LIST *list);
183
184 /* Local functions */
185
186 static struct n_hdlc *n_hdlc_alloc (void);
187
188 MODULE_PARM(debuglevel, "i");
189 MODULE_PARM(maxframe, "i");
190
191
192 /* debug level can be set by insmod for debugging purposes */
193 #define DEBUG_LEVEL_INFO        1
194 int debuglevel=0;
195
196 /* max frame size for memory allocations */
197 ssize_t maxframe=4096;
198
199 /* TTY callbacks */
200
201 static rw_ret_t n_hdlc_tty_read(struct tty_struct *,
202         struct file *, __u8 *, rw_count_t);
203 static rw_ret_t n_hdlc_tty_write(struct tty_struct *,
204         struct file *, const __u8 *, rw_count_t);
205 static int n_hdlc_tty_ioctl(struct tty_struct *,
206         struct file *, unsigned int, unsigned long);
207 static unsigned int n_hdlc_tty_poll (struct tty_struct *tty, struct file *filp,
208                                   poll_table * wait);
209 static int n_hdlc_tty_open (struct tty_struct *);
210 static void n_hdlc_tty_close (struct tty_struct *);
211 static int n_hdlc_tty_room (struct tty_struct *tty);
212 static void n_hdlc_tty_receive (struct tty_struct *tty,
213         const __u8 * cp, char *fp, int count);
214 static void n_hdlc_tty_wakeup (struct tty_struct *tty);
215
216 #define bset(p,b)       ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
217
218 #define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
219 #define n_hdlc2tty(n_hdlc)      ((n_hdlc)->tty)
220
221 /* Define this string only once for all macro invocations */
222 static char szVersion[] = HDLC_VERSION;
223
224 /* n_hdlc_release()
225  *
226  *      release an n_hdlc per device line discipline info structure
227  *
228  */
229 static void n_hdlc_release (struct n_hdlc *n_hdlc)
230 {
231         struct tty_struct *tty = n_hdlc2tty (n_hdlc);
232         N_HDLC_BUF *buf;
233         
234         if (debuglevel >= DEBUG_LEVEL_INFO)     
235                 printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
236                 
237         /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
238         wake_up_interruptible (&n_hdlc->read_wait);
239         wake_up_interruptible (&n_hdlc->poll_wait);
240         wake_up_interruptible (&n_hdlc->write_wait);
241
242         if (tty != NULL && tty->disc_data == n_hdlc)
243                 tty->disc_data = NULL;  /* Break the tty->n_hdlc link */
244
245         /* Release transmit and receive buffers */
246         for(;;) {
247                 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
248                 if (buf) {
249                         kfree(buf);
250                 } else
251                         break;
252         }
253         for(;;) {
254                 buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
255                 if (buf) {
256                         kfree(buf);
257                 } else
258                         break;
259         }
260         for(;;) {
261                 buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
262                 if (buf) {
263                         kfree(buf);
264                 } else
265                         break;
266         }
267         for(;;) {
268                 buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
269                 if (buf) {
270                         kfree(buf);
271                 } else
272                         break;
273         }
274         
275         kfree(n_hdlc);
276         
277 }       /* end of n_hdlc_release() */
278
279 /* n_hdlc_tty_close()
280  *
281  *      Called when the line discipline is changed to something
282  *      else, the tty is closed, or the tty detects a hangup.
283  */
284 static void n_hdlc_tty_close(struct tty_struct *tty)
285 {
286         struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
287
288         if (debuglevel >= DEBUG_LEVEL_INFO)     
289                 printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
290                 
291         if (n_hdlc != NULL) {
292                 if (n_hdlc->magic != HDLC_MAGIC) {
293                         printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
294                         return;
295                 }
296 #if defined(TTY_NO_WRITE_SPLIT)
297                 clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
298 #endif
299                 tty->disc_data = NULL;
300                 if (tty == n_hdlc->backup_tty)
301                         n_hdlc->backup_tty = 0;
302                 if (tty != n_hdlc->tty)
303                         return;
304                 if (n_hdlc->backup_tty) {
305                         n_hdlc->tty = n_hdlc->backup_tty;
306                 } else {
307                         n_hdlc_release (n_hdlc);
308                         MOD_DEC_USE_COUNT;
309                 }
310         }
311         
312         if (debuglevel >= DEBUG_LEVEL_INFO)     
313                 printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
314                 
315 }       /* end of n_hdlc_tty_close() */
316
317 /* n_hdlc_tty_open
318  * 
319  *      called when line discipline changed to n_hdlc
320  *      
321  * Arguments:   tty     pointer to tty info structure
322  * Return Value:        0 if success, otherwise error code
323  */
324 static int n_hdlc_tty_open (struct tty_struct *tty)
325 {
326         struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
327
328         if (debuglevel >= DEBUG_LEVEL_INFO)     
329                 printk("%s(%d)n_hdlc_tty_open() called (major=%u,minor=%u)\n",
330                 __FILE__,__LINE__,
331                 MAJOR(tty->device), MINOR(tty->device));
332                 
333         /* There should not be an existing table for this slot. */
334         if (n_hdlc) {
335                 printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
336                 return -EEXIST;
337         }
338         
339         n_hdlc = n_hdlc_alloc();
340         if (!n_hdlc) {
341                 printk (KERN_ERR "n_hdlc_alloc failed\n");
342                 return -ENFILE;
343         }
344                 
345         tty->disc_data = n_hdlc;
346         n_hdlc->tty    = tty;
347         
348         MOD_INC_USE_COUNT;
349         
350 #if defined(TTY_NO_WRITE_SPLIT)
351         /* change tty_io write() to not split large writes into 8K chunks */
352         set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
353 #endif
354         
355         /* Flush any pending characters in the driver and discipline. */
356         
357         if (tty->ldisc.flush_buffer)
358                 tty->ldisc.flush_buffer (tty);
359
360         if (tty->driver.flush_buffer)
361                 tty->driver.flush_buffer (tty);
362                 
363         if (debuglevel >= DEBUG_LEVEL_INFO)     
364                 printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
365                 
366         return 0;
367         
368 }       /* end of n_tty_hdlc_open() */
369
370 /* n_hdlc_send_frames()
371  * 
372  *      send frames on pending send buffer list until the
373  *      driver does not accept a frame (busy)
374  *      this function is called after adding a frame to the
375  *      send buffer list and by the tty wakeup callback
376  *      
377  * Arguments:           n_hdlc          pointer to ldisc instance data
378  *                      tty             pointer to tty instance data
379  * Return Value:        None
380  */
381 static void n_hdlc_send_frames (struct n_hdlc *n_hdlc, struct tty_struct *tty)
382 {
383         register int actual;
384         unsigned long flags;
385         N_HDLC_BUF *tbuf;
386
387         if (debuglevel >= DEBUG_LEVEL_INFO)     
388                 printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
389  check_again:
390                 
391         save_flags(flags);
392         cli ();
393         if (n_hdlc->tbusy) {
394                 n_hdlc->woke_up = 1;
395                 restore_flags(flags);
396                 return;
397         }
398         n_hdlc->tbusy = 1;
399         n_hdlc->woke_up = 0;
400         restore_flags(flags);
401
402         /* get current transmit buffer or get new transmit */
403         /* buffer from list of pending transmit buffers */
404                 
405         tbuf = n_hdlc->tbuf;
406         if (!tbuf)
407                 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
408                 
409         while (tbuf) {
410                 if (debuglevel >= DEBUG_LEVEL_INFO)     
411                         printk("%s(%d)sending frame %p, count=%d\n",
412                                 __FILE__,__LINE__,tbuf,tbuf->count);
413                         
414                 /* Send the next block of data to device */
415                 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
416                 actual = tty->driver.write(tty, 0, tbuf->buf, tbuf->count);
417                     
418                 /* if transmit error, throw frame away by */
419                 /* pretending it was accepted by driver */
420                 if (actual < 0)
421                         actual = tbuf->count;
422                 
423                 if (actual == tbuf->count) {
424                         if (debuglevel >= DEBUG_LEVEL_INFO)     
425                                 printk("%s(%d)frame %p completed\n",
426                                         __FILE__,__LINE__,tbuf);
427                                         
428                         /* free current transmit buffer */
429                         n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,tbuf);
430                         
431                         /* this tx buffer is done */
432                         n_hdlc->tbuf = NULL;
433                         
434                         /* wait up sleeping writers */
435                         wake_up_interruptible(&n_hdlc->write_wait);
436                         wake_up_interruptible(&n_hdlc->poll_wait);
437         
438                         /* get next pending transmit buffer */
439                         tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
440                 } else {
441                         if (debuglevel >= DEBUG_LEVEL_INFO)     
442                                 printk("%s(%d)frame %p pending\n",
443                                         __FILE__,__LINE__,tbuf);
444                                         
445                         /* buffer not accepted by driver */
446                         /* set this buffer as pending buffer */
447                         n_hdlc->tbuf = tbuf;
448                         break;
449                 }
450         }
451         
452         if (!tbuf)
453                 tty->flags  &= ~(1 << TTY_DO_WRITE_WAKEUP);
454         
455         /* Clear the re-entry flag */
456         save_flags(flags);
457         cli ();
458         n_hdlc->tbusy = 0;
459         restore_flags(flags);
460         
461         if (n_hdlc->woke_up)
462           goto check_again;
463
464         if (debuglevel >= DEBUG_LEVEL_INFO)     
465                 printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
466                 
467 }       /* end of n_hdlc_send_frames() */
468
469 /* n_hdlc_tty_wakeup()
470  *
471  *      Callback for transmit wakeup. Called when low level
472  *      device driver can accept more send data.
473  *
474  * Arguments:           tty     pointer to associated tty instance data
475  * Return Value:        None
476  */
477 static void n_hdlc_tty_wakeup (struct tty_struct *tty)
478 {
479         struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
480
481         if (debuglevel >= DEBUG_LEVEL_INFO)     
482                 printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
483                 
484         if (!n_hdlc)
485                 return;
486
487         if (tty != n_hdlc->tty) {
488                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
489                 return;
490         }
491
492         n_hdlc_send_frames (n_hdlc, tty);
493                 
494 }       /* end of n_hdlc_tty_wakeup() */
495
496 /* n_hdlc_tty_room()
497  * 
498  *      Callback function from tty driver. Return the amount of 
499  *      space left in the receiver's buffer to decide if remote
500  *      transmitter is to be throttled.
501  *
502  * Arguments:           tty     pointer to associated tty instance data
503  * Return Value:        number of bytes left in receive buffer
504  */
505 static int n_hdlc_tty_room (struct tty_struct *tty)
506 {
507         if (debuglevel >= DEBUG_LEVEL_INFO)     
508                 printk("%s(%d)n_hdlc_tty_room() called\n",__FILE__,__LINE__);
509         /* always return a larger number to prevent */
510         /* throttling of remote transmitter. */
511         return 65536;
512 }       /* end of n_hdlc_tty_root() */
513
514 /* n_hdlc_tty_receive()
515  * 
516  *      Called by tty low level driver when receive data is
517  *      available. Data is interpreted as one HDLC frame.
518  *      
519  * Arguments:           tty             pointer to tty isntance data
520  *                      data            pointer to received data
521  *                      flags           pointer to flags for data
522  *                      count           count of received data in bytes
523  *      
524  * Return Value:        None
525  */
526 static void n_hdlc_tty_receive(struct tty_struct *tty,
527         const __u8 * data, char *flags, int count)
528 {
529         register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
530         register N_HDLC_BUF *buf;
531
532         if (debuglevel >= DEBUG_LEVEL_INFO)     
533                 printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
534                         __FILE__,__LINE__, count);
535                 
536         /* This can happen if stuff comes in on the backup tty */
537         if (n_hdlc == 0 || tty != n_hdlc->tty)
538                 return;
539                 
540         /* verify line is using HDLC discipline */
541         if (n_hdlc->magic != HDLC_MAGIC) {
542                 printk("%s(%d) line not using HDLC discipline\n",
543                         __FILE__,__LINE__);
544                 return;
545         }
546         
547         if ( count>maxframe ) {
548                 if (debuglevel >= DEBUG_LEVEL_INFO)     
549                         printk("%s(%d) rx count>maxframesize, data discarded\n",
550                                __FILE__,__LINE__);
551                 return;
552         }
553
554         /* get a free HDLC buffer */    
555         buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
556         if (!buf) {
557                 /* no buffers in free list, attempt to allocate another rx buffer */
558                 /* unless the maximum count has been reached */
559                 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
560                         buf = (N_HDLC_BUF*)kmalloc(N_HDLC_BUF_SIZE,GFP_ATOMIC);
561         }
562         
563         if (!buf) {
564                 if (debuglevel >= DEBUG_LEVEL_INFO)     
565                         printk("%s(%d) no more rx buffers, data discarded\n",
566                                __FILE__,__LINE__);
567                 return;
568         }
569                 
570         /* copy received data to HDLC buffer */
571         memcpy(buf->buf,data,count);
572         buf->count=count;
573
574         /* add HDLC buffer to list of received frames */
575         n_hdlc_buf_put(&n_hdlc->rx_buf_list,buf);
576         
577         /* wake up any blocked reads and perform async signalling */
578         wake_up_interruptible (&n_hdlc->read_wait);
579         wake_up_interruptible (&n_hdlc->poll_wait);
580         if (n_hdlc->tty->fasync != NULL)
581                 kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
582
583 }       /* end of n_hdlc_tty_receive() */
584
585 /* n_hdlc_tty_read()
586  * 
587  *      Called to retreive one frame of data (if available)
588  *      
589  * Arguments:
590  * 
591  *      tty             pointer to tty instance data
592  *      file            pointer to open file object
593  *      buf             pointer to returned data buffer
594  *      nr              size of returned data buffer
595  *      
596  * Return Value:
597  * 
598  *      Number of bytes returned or error code
599  */
600 static rw_ret_t n_hdlc_tty_read (struct tty_struct *tty,
601         struct file *file, __u8 * buf, rw_count_t nr)
602 {
603         struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
604         int error;
605         rw_ret_t ret;
606         N_HDLC_BUF *rbuf;
607
608         if (debuglevel >= DEBUG_LEVEL_INFO)     
609                 printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
610                 
611         /* Validate the pointers */
612         if (!n_hdlc)
613                 return -EIO;
614
615         /* verify user access to buffer */
616         error = verify_area (VERIFY_WRITE, buf, nr);
617         if (error != 0) {
618                 printk(KERN_WARNING"%s(%d) n_hdlc_tty_read() can't verify user "
619                 "buffer\n",__FILE__,__LINE__);
620                 return (error);
621         }
622
623         for (;;) {
624                 n_hdlc = tty2n_hdlc (tty);
625                 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
626                          tty != n_hdlc->tty)
627                         return 0;
628
629                 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
630                 if (rbuf)
631                         break;
632                         
633                 /* no data */
634                 if (file->f_flags & O_NONBLOCK)
635                         return -EAGAIN;
636                         
637                 interruptible_sleep_on (&n_hdlc->read_wait);
638                 if (signal_pending(current))
639                         return -EINTR;
640         }
641                 
642         if (rbuf->count > nr) {
643                 /* frame too large for caller's buffer (discard frame) */
644                 ret = (rw_ret_t)-EOVERFLOW;
645         } else {
646                 /* Copy the data to the caller's buffer */
647                 COPY_TO_USER(error,buf,rbuf->buf,rbuf->count);
648                 if (error)
649                         ret = (rw_ret_t)error;
650                 else
651                         ret = (rw_ret_t)rbuf->count;
652         }
653         
654         /* return HDLC buffer to free list unless the free list */
655         /* count has exceeded the default value, in which case the */
656         /* buffer is freed back to the OS to conserve memory */
657         if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
658                 kfree(rbuf);
659         else    
660                 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
661         
662         return ret;
663         
664 }       /* end of n_hdlc_tty_read() */
665
666 /* n_hdlc_tty_write()
667  * 
668  *      write a single frame of data to device
669  *      
670  * Arguments:   tty     pointer to associated tty device instance data
671  *              file    pointer to file object data
672  *              data    pointer to transmit data (one frame)
673  *              count   size of transmit frame in bytes
674  *              
675  * Return Value:        number of bytes written (or error code)
676  */
677 static rw_ret_t n_hdlc_tty_write (struct tty_struct *tty, struct file *file,
678         const __u8 * data, rw_count_t count)
679 {
680         struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
681         int error = 0;
682         DECLARE_WAITQUEUE(wait, current);
683         N_HDLC_BUF *tbuf;
684
685         if (debuglevel >= DEBUG_LEVEL_INFO)     
686                 printk("%s(%d)n_hdlc_tty_write() called count=%d\n",
687                         __FILE__,__LINE__,count);
688                 
689         /* Verify pointers */
690         if (!n_hdlc)
691                 return -EIO;
692
693         if (n_hdlc->magic != HDLC_MAGIC)
694                 return -EIO;
695
696         /* verify frame size */
697         if (count > maxframe ) {
698                 if (debuglevel & DEBUG_LEVEL_INFO)
699                         printk (KERN_WARNING
700                                 "n_hdlc_tty_write: truncating user packet "
701                                 "from %lu to %d\n", (unsigned long) count,
702                                 maxframe );
703                 count = maxframe;
704         }
705         
706         add_wait_queue(&n_hdlc->write_wait, &wait);
707         set_current_state(TASK_INTERRUPTIBLE);
708         
709         /* Allocate transmit buffer */
710         /* sleep until transmit buffer available */             
711         while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) {
712                 schedule();
713                         
714                 n_hdlc = tty2n_hdlc (tty);
715                 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || 
716                     tty != n_hdlc->tty) {
717                         printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
718                         error = -EIO;
719                         break;
720                 }
721                         
722                 if (signal_pending(current)) {
723                         error = -EINTR;
724                         break;
725                 }
726         }
727
728         set_current_state(TASK_RUNNING);
729         remove_wait_queue(&n_hdlc->write_wait, &wait);
730
731         if (!error) {           
732                 /* Retrieve the user's buffer */
733                 COPY_FROM_USER (error, tbuf->buf, data, count);
734                 if (error) {
735                         /* return tx buffer to free list */
736                         n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,tbuf);
737                 } else {
738                         /* Send the data */
739                         tbuf->count = error = count;
740                         n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
741                         n_hdlc_send_frames(n_hdlc,tty);
742                 }
743         }
744
745         return error;
746         
747 }       /* end of n_hdlc_tty_write() */
748
749 /* n_hdlc_tty_ioctl()
750  *
751  *      Process IOCTL system call for the tty device.
752  *
753  * Arguments:
754  *
755  *      tty             pointer to tty instance data
756  *      file            pointer to open file object for device
757  *      cmd             IOCTL command code
758  *      arg             argument for IOCTL call (cmd dependent)
759  *
760  * Return Value:        Command dependent
761  */
762 static int n_hdlc_tty_ioctl (struct tty_struct *tty, struct file * file,
763                unsigned int cmd, unsigned long arg)
764 {
765         struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
766         int error = 0;
767         int count;
768         unsigned long flags;
769         
770         if (debuglevel >= DEBUG_LEVEL_INFO)     
771                 printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
772                         __FILE__,__LINE__,cmd);
773                 
774         /* Verify the status of the device */
775         if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
776                 return -EBADF;
777
778         switch (cmd) {
779         case FIONREAD:
780                 /* report count of read data available */
781                 /* in next available frame (if any) */
782                 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
783                 if (n_hdlc->rx_buf_list.head)
784                         count = n_hdlc->rx_buf_list.head->count;
785                 else
786                         count = 0;
787                 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
788                 PUT_USER (error, count, (int *) arg);
789                 break;
790
791         case TIOCOUTQ:
792                 /* get the pending tx byte count in the driver */
793                 count = tty->driver.chars_in_buffer ?
794                                 tty->driver.chars_in_buffer(tty) : 0;
795                 /* add size of next output frame in queue */
796                 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
797                 if (n_hdlc->tx_buf_list.head)
798                         count += n_hdlc->tx_buf_list.head->count;
799                 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
800                 PUT_USER (error, count, (int*)arg);
801                 break;
802
803         default:
804                 error = n_tty_ioctl (tty, file, cmd, arg);
805                 break;
806         }
807         return error;
808         
809 }       /* end of n_hdlc_tty_ioctl() */
810
811 /* n_hdlc_tty_poll()
812  * 
813  *      TTY callback for poll system call. Determine which 
814  *      operations (read/write) will not block and return
815  *      info to caller.
816  *      
817  * Arguments:
818  * 
819  *      tty             pointer to tty instance data
820  *      filp            pointer to open file object for device
821  *      poll_table      wait queue for operations
822  * 
823  * Return Value:
824  * 
825  *      bit mask containing info on which ops will not block
826  */
827 static unsigned int n_hdlc_tty_poll (struct tty_struct *tty,
828          struct file *filp, poll_table * wait)
829 {
830         struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
831         unsigned int mask = 0;
832
833         if (debuglevel >= DEBUG_LEVEL_INFO)     
834                 printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
835                 
836         if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
837                 /* queue current process into any wait queue that */
838                 /* may awaken in the future (read and write) */
839                 poll_wait(filp, &n_hdlc->poll_wait, wait);
840
841                 /* set bits for operations that wont block */
842                 if(n_hdlc->rx_buf_list.head)
843                         mask |= POLLIN | POLLRDNORM;    /* readable */
844                 if(tty->flags & (1 << TTY_OTHER_CLOSED))
845                         mask |= POLLHUP;
846                 if(tty_hung_up_p(filp))
847                         mask |= POLLHUP;
848                 if(n_hdlc->tx_free_buf_list.head)
849                         mask |= POLLOUT | POLLWRNORM;   /* writable */
850         }
851         return mask;
852 }       /* end of n_hdlc_tty_poll() */
853
854 /* n_hdlc_alloc()
855  * 
856  *      Allocate an n_hdlc instance data structure
857  *
858  * Arguments:           None
859  * Return Value:        pointer to structure if success, otherwise 0    
860  */
861 static struct n_hdlc *n_hdlc_alloc (void)
862 {
863         struct n_hdlc   *n_hdlc;
864         N_HDLC_BUF      *buf;
865         int             i;
866         
867         n_hdlc = (struct n_hdlc *)kmalloc(sizeof(struct n_hdlc), GFP_KERNEL);
868         if (!n_hdlc)
869                 return 0;
870
871         memset(n_hdlc, 0, sizeof(*n_hdlc));
872
873         n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list);
874         n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list);
875         n_hdlc_buf_list_init(&n_hdlc->rx_buf_list);
876         n_hdlc_buf_list_init(&n_hdlc->tx_buf_list);
877         
878         /* allocate free rx buffer list */
879         for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
880                 buf = (N_HDLC_BUF*)kmalloc(N_HDLC_BUF_SIZE,GFP_KERNEL);
881                 if (buf)
882                         n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
883                 else if (debuglevel >= DEBUG_LEVEL_INFO)        
884                         printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
885         }
886         
887         /* allocate free tx buffer list */
888         for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
889                 buf = (N_HDLC_BUF*)kmalloc(N_HDLC_BUF_SIZE,GFP_KERNEL);
890                 if (buf)
891                         n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
892                 else if (debuglevel >= DEBUG_LEVEL_INFO)        
893                         printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
894         }
895         
896         /* Initialize the control block */
897         n_hdlc->magic  = HDLC_MAGIC;
898
899         n_hdlc->flags  = 0;
900         init_waitqueue_head(&n_hdlc->read_wait);
901         init_waitqueue_head(&n_hdlc->poll_wait);
902         init_waitqueue_head(&n_hdlc->write_wait);
903         
904         return n_hdlc;
905         
906 }       /* end of n_hdlc_alloc() */
907
908 /* n_hdlc_buf_list_init()
909  * 
910  *      initialize specified HDLC buffer list
911  *      
912  * Arguments:           list    pointer to buffer list
913  * Return Value:        None    
914  */
915 void n_hdlc_buf_list_init(N_HDLC_BUF_LIST *list)
916 {
917         memset(list,0,sizeof(N_HDLC_BUF_LIST));
918         spin_lock_init(&list->spinlock);
919 }       /* end of n_hdlc_buf_list_init() */
920
921 /* n_hdlc_buf_put()
922  * 
923  *      add specified HDLC buffer to tail of specified list
924  *      
925  * Arguments:
926  * 
927  *      list    pointer to buffer list
928  *      buf     pointer to buffer
929  * 
930  * Return Value:        None    
931  */
932 void n_hdlc_buf_put(N_HDLC_BUF_LIST *list,N_HDLC_BUF *buf)
933 {
934         unsigned long flags;
935         spin_lock_irqsave(&list->spinlock,flags);
936         
937         buf->link=NULL;
938         if(list->tail)
939                 list->tail->link = buf;
940         else
941                 list->head = buf;
942         list->tail = buf;
943         (list->count)++;
944         
945         spin_unlock_irqrestore(&list->spinlock,flags);
946         
947 }       /* end of n_hdlc_buf_put() */
948
949 /* n_hdlc_buf_get()
950  * 
951  *      remove and return an HDLC buffer from the
952  *      head of the specified HDLC buffer list
953  *      
954  * Arguments:
955  * 
956  *      list    pointer to HDLC buffer list
957  *      
958  * Return Value:
959  * 
960  *      pointer to HDLC buffer if available, otherwise NULL
961  */
962 N_HDLC_BUF* n_hdlc_buf_get(N_HDLC_BUF_LIST *list)
963 {
964         unsigned long flags;
965         N_HDLC_BUF *buf;
966         spin_lock_irqsave(&list->spinlock,flags);
967         
968         buf = list->head;
969         if (buf) {
970                 list->head = buf->link;
971                 (list->count)--;
972         }
973         if (!list->head)
974                 list->tail = NULL;
975         
976         spin_unlock_irqrestore(&list->spinlock,flags);
977         return buf;
978         
979 }       /* end of n_hdlc_buf_get() */
980
981 static int __init n_hdlc_init(void)
982 {
983         static struct tty_ldisc n_hdlc_ldisc;
984         int    status;
985
986         /* range check maxframe arg */
987         if ( maxframe<4096)
988                 maxframe=4096;
989         else if ( maxframe>65535)
990                 maxframe=65535;
991
992         printk("HDLC line discipline: version %s, maxframe=%u\n", 
993                 szVersion, maxframe);
994
995         /* Register the tty discipline */
996         
997         memset(&n_hdlc_ldisc, 0, sizeof (n_hdlc_ldisc));
998         n_hdlc_ldisc.magic              = TTY_LDISC_MAGIC;
999         n_hdlc_ldisc.name               = "hdlc";
1000         n_hdlc_ldisc.open               = n_hdlc_tty_open;
1001         n_hdlc_ldisc.close              = n_hdlc_tty_close;
1002         n_hdlc_ldisc.read               = n_hdlc_tty_read;
1003         n_hdlc_ldisc.write              = n_hdlc_tty_write;
1004         n_hdlc_ldisc.ioctl              = n_hdlc_tty_ioctl;
1005         n_hdlc_ldisc.poll               = n_hdlc_tty_poll;
1006         n_hdlc_ldisc.receive_room       = n_hdlc_tty_room;
1007         n_hdlc_ldisc.receive_buf        = n_hdlc_tty_receive;
1008         n_hdlc_ldisc.write_wakeup       = n_hdlc_tty_wakeup;
1009
1010         status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
1011         if (!status)
1012                 printk (KERN_INFO"N_HDLC line discipline registered.\n");
1013         else
1014                 printk (KERN_ERR"error registering line discipline: %d\n",status);
1015
1016         if (status)
1017                 printk(KERN_INFO"N_HDLC: init failure %d\n", status);
1018         return (status);
1019         
1020 }       /* end of init_module() */
1021
1022 static void __exit n_hdlc_exit(void)
1023 {
1024         int status;
1025         /* Release tty registration of line discipline */
1026         if ((status = tty_register_ldisc(N_HDLC, NULL)))
1027                 printk("N_HDLC: can't unregister line discipline (err = %d)\n", status);
1028         else
1029                 printk("N_HDLC: line discipline unregistered\n");
1030 }
1031
1032 module_init(n_hdlc_init);
1033 module_exit(n_hdlc_exit);