Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | * $Id: n_hdlc.c,v 4.8 2003/05/06 21:18:51 paulkf Exp $ | |
14 | * | |
15 | * This code is released under the GNU General Public License (GPL) | |
16 | * | |
17 | * This module implements the tty line discipline N_HDLC for use with | |
18 | * tty device drivers that support bit-synchronous HDLC communications. | |
19 | * | |
20 | * All HDLC data is frame oriented which means: | |
21 | * | |
22 | * 1. tty write calls represent one complete transmit frame of data | |
23 | * The device driver should accept the complete frame or none of | |
24 | * the frame (busy) in the write method. Each write call should have | |
25 | * a byte count in the range of 2-65535 bytes (2 is min HDLC frame | |
26 | * with 1 addr byte and 1 ctrl byte). The max byte count of 65535 | |
27 | * should include any crc bytes required. For example, when using | |
28 | * CCITT CRC32, 4 crc bytes are required, so the maximum size frame | |
29 | * the application may transmit is limited to 65531 bytes. For CCITT | |
30 | * CRC16, the maximum application frame size would be 65533. | |
31 | * | |
32 | * | |
33 | * 2. receive callbacks from the device driver represents | |
34 | * one received frame. The device driver should bypass | |
35 | * the tty flip buffer and call the line discipline receive | |
36 | * callback directly to avoid fragmenting or concatenating | |
37 | * multiple frames into a single receive callback. | |
38 | * | |
39 | * The HDLC line discipline queues the receive frames in separate | |
40 | * buffers so complete receive frames can be returned by the | |
41 | * tty read calls. | |
42 | * | |
43 | * 3. tty read calls returns an entire frame of data or nothing. | |
44 | * | |
45 | * 4. all send and receive data is considered raw. No processing | |
46 | * or translation is performed by the line discipline, regardless | |
47 | * of the tty flags | |
48 | * | |
49 | * 5. When line discipline is queried for the amount of receive | |
50 | * data available (FIOC), 0 is returned if no data available, | |
51 | * otherwise the count of the next available frame is returned. | |
52 | * (instead of the sum of all received frame counts). | |
53 | * | |
54 | * These conventions allow the standard tty programming interface | |
55 | * to be used for synchronous HDLC applications when used with | |
56 | * this line discipline (or another line discipline that is frame | |
57 | * oriented such as N_PPP). | |
58 | * | |
59 | * The SyncLink driver (synclink.c) implements both asynchronous | |
60 | * (using standard line discipline N_TTY) and synchronous HDLC | |
61 | * (using N_HDLC) communications, with the latter using the above | |
62 | * conventions. | |
63 | * | |
64 | * This implementation is very basic and does not maintain | |
65 | * any statistics. The main point is to enforce the raw data | |
66 | * and frame orientation of HDLC communications. | |
67 | * | |
68 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
69 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
70 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
71 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
72 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
73 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
74 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
75 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
76 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
77 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
78 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
79 | */ | |
80 | ||
81 | #define HDLC_MAGIC 0x239e | |
82 | #define HDLC_VERSION "$Revision: 4.8 $" | |
83 | ||
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/ioctl.h> | |
100 | #include <linux/slab.h> | |
101 | #include <linux/tty.h> | |
102 | #include <linux/errno.h> | |
103 | #include <linux/string.h> /* used in new tty drivers */ | |
104 | #include <linux/signal.h> /* used in new tty drivers */ | |
105 | #include <linux/if.h> | |
106 | #include <linux/bitops.h> | |
107 | ||
108 | #include <asm/system.h> | |
109 | #include <asm/termios.h> | |
110 | #include <asm/uaccess.h> | |
111 | ||
112 | /* | |
113 | * Buffers for individual HDLC frames | |
114 | */ | |
115 | #define MAX_HDLC_FRAME_SIZE 65535 | |
116 | #define DEFAULT_RX_BUF_COUNT 10 | |
117 | #define MAX_RX_BUF_COUNT 60 | |
118 | #define DEFAULT_TX_BUF_COUNT 1 | |
119 | ||
120 | struct n_hdlc_buf { | |
121 | struct n_hdlc_buf *link; | |
122 | int count; | |
123 | char buf[1]; | |
124 | }; | |
125 | ||
126 | #define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe) | |
127 | ||
128 | struct n_hdlc_buf_list { | |
129 | struct n_hdlc_buf *head; | |
130 | struct n_hdlc_buf *tail; | |
131 | int count; | |
132 | spinlock_t spinlock; | |
133 | }; | |
134 | ||
135 | /** | |
136 | * struct n_hdlc - per device instance data structure | |
137 | * @magic - magic value for structure | |
138 | * @flags - miscellaneous control flags | |
139 | * @tty - ptr to TTY structure | |
140 | * @backup_tty - TTY to use if tty gets closed | |
141 | * @tbusy - reentrancy flag for tx wakeup code | |
142 | * @woke_up - FIXME: describe this field | |
143 | * @tbuf - currently transmitting tx buffer | |
144 | * @tx_buf_list - list of pending transmit frame buffers | |
145 | * @rx_buf_list - list of received frame buffers | |
146 | * @tx_free_buf_list - list unused transmit frame buffers | |
147 | * @rx_free_buf_list - list unused received frame buffers | |
148 | */ | |
149 | struct n_hdlc { | |
150 | int magic; | |
151 | __u32 flags; | |
152 | struct tty_struct *tty; | |
153 | struct tty_struct *backup_tty; | |
154 | int tbusy; | |
155 | int woke_up; | |
156 | struct n_hdlc_buf *tbuf; | |
157 | struct n_hdlc_buf_list tx_buf_list; | |
158 | struct n_hdlc_buf_list rx_buf_list; | |
159 | struct n_hdlc_buf_list tx_free_buf_list; | |
160 | struct n_hdlc_buf_list rx_free_buf_list; | |
161 | }; | |
162 | ||
163 | /* | |
164 | * HDLC buffer list manipulation functions | |
165 | */ | |
166 | static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list); | |
167 | static void n_hdlc_buf_put(struct n_hdlc_buf_list *list, | |
168 | struct n_hdlc_buf *buf); | |
169 | static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list); | |
170 | ||
171 | /* Local functions */ | |
172 | ||
173 | static struct n_hdlc *n_hdlc_alloc (void); | |
174 | ||
175 | /* debug level can be set by insmod for debugging purposes */ | |
176 | #define DEBUG_LEVEL_INFO 1 | |
177 | static int debuglevel; | |
178 | ||
179 | /* max frame size for memory allocations */ | |
180 | static int maxframe = 4096; | |
181 | ||
182 | /* TTY callbacks */ | |
183 | ||
184 | static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, | |
185 | __u8 __user *buf, size_t nr); | |
186 | static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, | |
187 | const unsigned char *buf, size_t nr); | |
188 | static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, | |
189 | unsigned int cmd, unsigned long arg); | |
190 | static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp, | |
191 | poll_table *wait); | |
192 | static int n_hdlc_tty_open(struct tty_struct *tty); | |
193 | static void n_hdlc_tty_close(struct tty_struct *tty); | |
194 | static int n_hdlc_tty_room(struct tty_struct *tty); | |
195 | static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp, | |
196 | char *fp, int count); | |
197 | static void n_hdlc_tty_wakeup(struct tty_struct *tty); | |
198 | ||
199 | #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f))) | |
200 | ||
201 | #define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data)) | |
202 | #define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty) | |
203 | ||
204 | static struct tty_ldisc n_hdlc_ldisc = { | |
205 | .owner = THIS_MODULE, | |
206 | .magic = TTY_LDISC_MAGIC, | |
207 | .name = "hdlc", | |
208 | .open = n_hdlc_tty_open, | |
209 | .close = n_hdlc_tty_close, | |
210 | .read = n_hdlc_tty_read, | |
211 | .write = n_hdlc_tty_write, | |
212 | .ioctl = n_hdlc_tty_ioctl, | |
213 | .poll = n_hdlc_tty_poll, | |
214 | .receive_buf = n_hdlc_tty_receive, | |
1da177e4 LT |
215 | .write_wakeup = n_hdlc_tty_wakeup, |
216 | }; | |
217 | ||
218 | /** | |
219 | * n_hdlc_release - release an n_hdlc per device line discipline info structure | |
220 | * @n_hdlc - per device line discipline info structure | |
221 | */ | |
222 | static void n_hdlc_release(struct n_hdlc *n_hdlc) | |
223 | { | |
224 | struct tty_struct *tty = n_hdlc2tty (n_hdlc); | |
225 | struct n_hdlc_buf *buf; | |
226 | ||
227 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
228 | printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__); | |
229 | ||
230 | /* Ensure that the n_hdlcd process is not hanging on select()/poll() */ | |
231 | wake_up_interruptible (&tty->read_wait); | |
232 | wake_up_interruptible (&tty->write_wait); | |
233 | ||
234 | if (tty != NULL && tty->disc_data == n_hdlc) | |
235 | tty->disc_data = NULL; /* Break the tty->n_hdlc link */ | |
236 | ||
237 | /* Release transmit and receive buffers */ | |
238 | for(;;) { | |
239 | buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list); | |
240 | if (buf) { | |
241 | kfree(buf); | |
242 | } else | |
243 | break; | |
244 | } | |
245 | for(;;) { | |
246 | buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list); | |
247 | if (buf) { | |
248 | kfree(buf); | |
249 | } else | |
250 | break; | |
251 | } | |
252 | for(;;) { | |
253 | buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list); | |
254 | if (buf) { | |
255 | kfree(buf); | |
256 | } else | |
257 | break; | |
258 | } | |
259 | for(;;) { | |
260 | buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); | |
261 | if (buf) { | |
262 | kfree(buf); | |
263 | } else | |
264 | break; | |
265 | } | |
735d5661 | 266 | kfree(n_hdlc->tbuf); |
1da177e4 LT |
267 | kfree(n_hdlc); |
268 | ||
269 | } /* end of n_hdlc_release() */ | |
270 | ||
271 | /** | |
272 | * n_hdlc_tty_close - line discipline close | |
273 | * @tty - pointer to tty info structure | |
274 | * | |
275 | * Called when the line discipline is changed to something | |
276 | * else, the tty is closed, or the tty detects a hangup. | |
277 | */ | |
278 | static void n_hdlc_tty_close(struct tty_struct *tty) | |
279 | { | |
280 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | |
281 | ||
282 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
283 | printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__); | |
284 | ||
285 | if (n_hdlc != NULL) { | |
286 | if (n_hdlc->magic != HDLC_MAGIC) { | |
287 | printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n"); | |
288 | return; | |
289 | } | |
290 | #if defined(TTY_NO_WRITE_SPLIT) | |
291 | clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags); | |
292 | #endif | |
293 | tty->disc_data = NULL; | |
294 | if (tty == n_hdlc->backup_tty) | |
295 | n_hdlc->backup_tty = NULL; | |
296 | if (tty != n_hdlc->tty) | |
297 | return; | |
298 | if (n_hdlc->backup_tty) { | |
299 | n_hdlc->tty = n_hdlc->backup_tty; | |
300 | } else { | |
301 | n_hdlc_release (n_hdlc); | |
302 | } | |
303 | } | |
304 | ||
305 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
306 | printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__); | |
307 | ||
308 | } /* end of n_hdlc_tty_close() */ | |
309 | ||
310 | /** | |
311 | * n_hdlc_tty_open - called when line discipline changed to n_hdlc | |
312 | * @tty - pointer to tty info structure | |
313 | * | |
314 | * Returns 0 if success, otherwise error code | |
315 | */ | |
316 | static int n_hdlc_tty_open (struct tty_struct *tty) | |
317 | { | |
318 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | |
319 | ||
320 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
321 | printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n", | |
322 | __FILE__,__LINE__, | |
323 | tty->name); | |
324 | ||
325 | /* There should not be an existing table for this slot. */ | |
326 | if (n_hdlc) { | |
327 | printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" ); | |
328 | return -EEXIST; | |
329 | } | |
330 | ||
331 | n_hdlc = n_hdlc_alloc(); | |
332 | if (!n_hdlc) { | |
333 | printk (KERN_ERR "n_hdlc_alloc failed\n"); | |
334 | return -ENFILE; | |
335 | } | |
336 | ||
337 | tty->disc_data = n_hdlc; | |
338 | n_hdlc->tty = tty; | |
33f0f88f | 339 | tty->receive_room = 65536; |
1da177e4 LT |
340 | |
341 | #if defined(TTY_NO_WRITE_SPLIT) | |
342 | /* change tty_io write() to not split large writes into 8K chunks */ | |
343 | set_bit(TTY_NO_WRITE_SPLIT,&tty->flags); | |
344 | #endif | |
345 | ||
346 | /* Flush any pending characters in the driver and discipline. */ | |
347 | ||
348 | if (tty->ldisc.flush_buffer) | |
349 | tty->ldisc.flush_buffer (tty); | |
350 | ||
351 | if (tty->driver->flush_buffer) | |
352 | tty->driver->flush_buffer (tty); | |
353 | ||
354 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
355 | printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__); | |
356 | ||
357 | return 0; | |
358 | ||
359 | } /* end of n_tty_hdlc_open() */ | |
360 | ||
361 | /** | |
362 | * n_hdlc_send_frames - send frames on pending send buffer list | |
363 | * @n_hdlc - pointer to ldisc instance data | |
364 | * @tty - pointer to tty instance data | |
365 | * | |
366 | * Send frames on pending send buffer list until the driver does not accept a | |
367 | * frame (busy) this function is called after adding a frame to the send buffer | |
368 | * list and by the tty wakeup callback. | |
369 | */ | |
370 | static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty) | |
371 | { | |
372 | register int actual; | |
373 | unsigned long flags; | |
374 | struct n_hdlc_buf *tbuf; | |
375 | ||
376 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
377 | printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__); | |
378 | check_again: | |
379 | ||
380 | spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); | |
381 | if (n_hdlc->tbusy) { | |
382 | n_hdlc->woke_up = 1; | |
383 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); | |
384 | return; | |
385 | } | |
386 | n_hdlc->tbusy = 1; | |
387 | n_hdlc->woke_up = 0; | |
388 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); | |
389 | ||
390 | /* get current transmit buffer or get new transmit */ | |
391 | /* buffer from list of pending transmit buffers */ | |
392 | ||
393 | tbuf = n_hdlc->tbuf; | |
394 | if (!tbuf) | |
395 | tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); | |
396 | ||
397 | while (tbuf) { | |
398 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
399 | printk("%s(%d)sending frame %p, count=%d\n", | |
400 | __FILE__,__LINE__,tbuf,tbuf->count); | |
401 | ||
402 | /* Send the next block of data to device */ | |
403 | tty->flags |= (1 << TTY_DO_WRITE_WAKEUP); | |
404 | actual = tty->driver->write(tty, tbuf->buf, tbuf->count); | |
405 | ||
406 | /* if transmit error, throw frame away by */ | |
407 | /* pretending it was accepted by driver */ | |
408 | if (actual < 0) | |
409 | actual = tbuf->count; | |
410 | ||
411 | if (actual == tbuf->count) { | |
412 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
413 | printk("%s(%d)frame %p completed\n", | |
414 | __FILE__,__LINE__,tbuf); | |
415 | ||
416 | /* free current transmit buffer */ | |
417 | n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf); | |
418 | ||
419 | /* this tx buffer is done */ | |
420 | n_hdlc->tbuf = NULL; | |
421 | ||
422 | /* wait up sleeping writers */ | |
423 | wake_up_interruptible(&tty->write_wait); | |
424 | ||
425 | /* get next pending transmit buffer */ | |
426 | tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); | |
427 | } else { | |
428 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
429 | printk("%s(%d)frame %p pending\n", | |
430 | __FILE__,__LINE__,tbuf); | |
431 | ||
432 | /* buffer not accepted by driver */ | |
433 | /* set this buffer as pending buffer */ | |
434 | n_hdlc->tbuf = tbuf; | |
435 | break; | |
436 | } | |
437 | } | |
438 | ||
439 | if (!tbuf) | |
440 | tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); | |
441 | ||
442 | /* Clear the re-entry flag */ | |
443 | spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); | |
444 | n_hdlc->tbusy = 0; | |
445 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); | |
446 | ||
447 | if (n_hdlc->woke_up) | |
448 | goto check_again; | |
449 | ||
450 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
451 | printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__); | |
452 | ||
453 | } /* end of n_hdlc_send_frames() */ | |
454 | ||
455 | /** | |
456 | * n_hdlc_tty_wakeup - Callback for transmit wakeup | |
457 | * @tty - pointer to associated tty instance data | |
458 | * | |
459 | * Called when low level device driver can accept more send data. | |
460 | */ | |
461 | static void n_hdlc_tty_wakeup(struct tty_struct *tty) | |
462 | { | |
463 | struct n_hdlc *n_hdlc = tty2n_hdlc(tty); | |
464 | ||
465 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
466 | printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__); | |
467 | ||
468 | if (!n_hdlc) | |
469 | return; | |
470 | ||
471 | if (tty != n_hdlc->tty) { | |
472 | tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); | |
473 | return; | |
474 | } | |
475 | ||
476 | n_hdlc_send_frames (n_hdlc, tty); | |
477 | ||
478 | } /* end of n_hdlc_tty_wakeup() */ | |
479 | ||
1da177e4 LT |
480 | /** |
481 | * n_hdlc_tty_receive - Called by tty driver when receive data is available | |
482 | * @tty - pointer to tty instance data | |
483 | * @data - pointer to received data | |
484 | * @flags - pointer to flags for data | |
485 | * @count - count of received data in bytes | |
486 | * | |
487 | * Called by tty low level driver when receive data is available. Data is | |
488 | * interpreted as one HDLC frame. | |
489 | */ | |
490 | static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data, | |
491 | char *flags, int count) | |
492 | { | |
493 | register struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | |
494 | register struct n_hdlc_buf *buf; | |
495 | ||
496 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
497 | printk("%s(%d)n_hdlc_tty_receive() called count=%d\n", | |
498 | __FILE__,__LINE__, count); | |
499 | ||
500 | /* This can happen if stuff comes in on the backup tty */ | |
501 | if (n_hdlc == 0 || tty != n_hdlc->tty) | |
502 | return; | |
503 | ||
504 | /* verify line is using HDLC discipline */ | |
505 | if (n_hdlc->magic != HDLC_MAGIC) { | |
506 | printk("%s(%d) line not using HDLC discipline\n", | |
507 | __FILE__,__LINE__); | |
508 | return; | |
509 | } | |
510 | ||
511 | if ( count>maxframe ) { | |
512 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
513 | printk("%s(%d) rx count>maxframesize, data discarded\n", | |
514 | __FILE__,__LINE__); | |
515 | return; | |
516 | } | |
517 | ||
518 | /* get a free HDLC buffer */ | |
519 | buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list); | |
520 | if (!buf) { | |
521 | /* no buffers in free list, attempt to allocate another rx buffer */ | |
522 | /* unless the maximum count has been reached */ | |
523 | if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT) | |
524 | buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC); | |
525 | } | |
526 | ||
527 | if (!buf) { | |
528 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
529 | printk("%s(%d) no more rx buffers, data discarded\n", | |
530 | __FILE__,__LINE__); | |
531 | return; | |
532 | } | |
533 | ||
534 | /* copy received data to HDLC buffer */ | |
535 | memcpy(buf->buf,data,count); | |
536 | buf->count=count; | |
537 | ||
538 | /* add HDLC buffer to list of received frames */ | |
539 | n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf); | |
540 | ||
541 | /* wake up any blocked reads and perform async signalling */ | |
542 | wake_up_interruptible (&tty->read_wait); | |
543 | if (n_hdlc->tty->fasync != NULL) | |
544 | kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN); | |
545 | ||
546 | } /* end of n_hdlc_tty_receive() */ | |
547 | ||
548 | /** | |
4a4efbde | 549 | * n_hdlc_tty_read - Called to retrieve one frame of data (if available) |
1da177e4 LT |
550 | * @tty - pointer to tty instance data |
551 | * @file - pointer to open file object | |
552 | * @buf - pointer to returned data buffer | |
553 | * @nr - size of returned data buffer | |
554 | * | |
555 | * Returns the number of bytes returned or error code. | |
556 | */ | |
557 | static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, | |
558 | __u8 __user *buf, size_t nr) | |
559 | { | |
560 | struct n_hdlc *n_hdlc = tty2n_hdlc(tty); | |
561 | int ret; | |
562 | struct n_hdlc_buf *rbuf; | |
563 | ||
564 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
565 | printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__); | |
566 | ||
567 | /* Validate the pointers */ | |
568 | if (!n_hdlc) | |
569 | return -EIO; | |
570 | ||
571 | /* verify user access to buffer */ | |
572 | if (!access_ok(VERIFY_WRITE, buf, nr)) { | |
573 | printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user " | |
574 | "buffer\n", __FILE__, __LINE__); | |
575 | return -EFAULT; | |
576 | } | |
577 | ||
578 | for (;;) { | |
579 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | |
580 | return -EIO; | |
581 | ||
582 | n_hdlc = tty2n_hdlc (tty); | |
583 | if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || | |
584 | tty != n_hdlc->tty) | |
585 | return 0; | |
586 | ||
587 | rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list); | |
588 | if (rbuf) | |
589 | break; | |
590 | ||
591 | /* no data */ | |
592 | if (file->f_flags & O_NONBLOCK) | |
593 | return -EAGAIN; | |
594 | ||
595 | interruptible_sleep_on (&tty->read_wait); | |
596 | if (signal_pending(current)) | |
597 | return -EINTR; | |
598 | } | |
599 | ||
600 | if (rbuf->count > nr) | |
601 | /* frame too large for caller's buffer (discard frame) */ | |
602 | ret = -EOVERFLOW; | |
603 | else { | |
604 | /* Copy the data to the caller's buffer */ | |
605 | if (copy_to_user(buf, rbuf->buf, rbuf->count)) | |
606 | ret = -EFAULT; | |
607 | else | |
608 | ret = rbuf->count; | |
609 | } | |
610 | ||
611 | /* return HDLC buffer to free list unless the free list */ | |
612 | /* count has exceeded the default value, in which case the */ | |
613 | /* buffer is freed back to the OS to conserve memory */ | |
614 | if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT) | |
615 | kfree(rbuf); | |
616 | else | |
617 | n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf); | |
618 | ||
619 | return ret; | |
620 | ||
621 | } /* end of n_hdlc_tty_read() */ | |
622 | ||
623 | /** | |
624 | * n_hdlc_tty_write - write a single frame of data to device | |
625 | * @tty - pointer to associated tty device instance data | |
626 | * @file - pointer to file object data | |
627 | * @data - pointer to transmit data (one frame) | |
628 | * @count - size of transmit frame in bytes | |
629 | * | |
630 | * Returns the number of bytes written (or error code). | |
631 | */ | |
632 | static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, | |
633 | const unsigned char *data, size_t count) | |
634 | { | |
635 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | |
636 | int error = 0; | |
637 | DECLARE_WAITQUEUE(wait, current); | |
638 | struct n_hdlc_buf *tbuf; | |
639 | ||
640 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
641 | printk("%s(%d)n_hdlc_tty_write() called count=%Zd\n", | |
642 | __FILE__,__LINE__,count); | |
643 | ||
644 | /* Verify pointers */ | |
645 | if (!n_hdlc) | |
646 | return -EIO; | |
647 | ||
648 | if (n_hdlc->magic != HDLC_MAGIC) | |
649 | return -EIO; | |
650 | ||
651 | /* verify frame size */ | |
652 | if (count > maxframe ) { | |
653 | if (debuglevel & DEBUG_LEVEL_INFO) | |
654 | printk (KERN_WARNING | |
655 | "n_hdlc_tty_write: truncating user packet " | |
656 | "from %lu to %d\n", (unsigned long) count, | |
657 | maxframe ); | |
658 | count = maxframe; | |
659 | } | |
660 | ||
661 | add_wait_queue(&tty->write_wait, &wait); | |
662 | set_current_state(TASK_INTERRUPTIBLE); | |
663 | ||
664 | /* Allocate transmit buffer */ | |
665 | /* sleep until transmit buffer available */ | |
666 | while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) { | |
667 | schedule(); | |
668 | ||
669 | n_hdlc = tty2n_hdlc (tty); | |
670 | if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || | |
671 | tty != n_hdlc->tty) { | |
672 | printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc); | |
673 | error = -EIO; | |
674 | break; | |
675 | } | |
676 | ||
677 | if (signal_pending(current)) { | |
678 | error = -EINTR; | |
679 | break; | |
680 | } | |
681 | } | |
682 | ||
683 | set_current_state(TASK_RUNNING); | |
684 | remove_wait_queue(&tty->write_wait, &wait); | |
685 | ||
686 | if (!error) { | |
687 | /* Retrieve the user's buffer */ | |
688 | memcpy(tbuf->buf, data, count); | |
689 | ||
690 | /* Send the data */ | |
691 | tbuf->count = error = count; | |
692 | n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf); | |
693 | n_hdlc_send_frames(n_hdlc,tty); | |
694 | } | |
695 | ||
696 | return error; | |
697 | ||
698 | } /* end of n_hdlc_tty_write() */ | |
699 | ||
700 | /** | |
701 | * n_hdlc_tty_ioctl - process IOCTL system call for the tty device. | |
702 | * @tty - pointer to tty instance data | |
703 | * @file - pointer to open file object for device | |
704 | * @cmd - IOCTL command code | |
705 | * @arg - argument for IOCTL call (cmd dependent) | |
706 | * | |
707 | * Returns command dependent result. | |
708 | */ | |
709 | static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, | |
710 | unsigned int cmd, unsigned long arg) | |
711 | { | |
712 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | |
713 | int error = 0; | |
714 | int count; | |
715 | unsigned long flags; | |
716 | ||
717 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
718 | printk("%s(%d)n_hdlc_tty_ioctl() called %d\n", | |
719 | __FILE__,__LINE__,cmd); | |
720 | ||
721 | /* Verify the status of the device */ | |
722 | if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC) | |
723 | return -EBADF; | |
724 | ||
725 | switch (cmd) { | |
726 | case FIONREAD: | |
727 | /* report count of read data available */ | |
728 | /* in next available frame (if any) */ | |
729 | spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags); | |
730 | if (n_hdlc->rx_buf_list.head) | |
731 | count = n_hdlc->rx_buf_list.head->count; | |
732 | else | |
733 | count = 0; | |
734 | spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags); | |
735 | error = put_user(count, (int __user *)arg); | |
736 | break; | |
737 | ||
738 | case TIOCOUTQ: | |
739 | /* get the pending tx byte count in the driver */ | |
740 | count = tty->driver->chars_in_buffer ? | |
741 | tty->driver->chars_in_buffer(tty) : 0; | |
742 | /* add size of next output frame in queue */ | |
743 | spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags); | |
744 | if (n_hdlc->tx_buf_list.head) | |
745 | count += n_hdlc->tx_buf_list.head->count; | |
746 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags); | |
747 | error = put_user(count, (int __user *)arg); | |
748 | break; | |
749 | ||
750 | default: | |
751 | error = n_tty_ioctl (tty, file, cmd, arg); | |
752 | break; | |
753 | } | |
754 | return error; | |
755 | ||
756 | } /* end of n_hdlc_tty_ioctl() */ | |
757 | ||
758 | /** | |
759 | * n_hdlc_tty_poll - TTY callback for poll system call | |
760 | * @tty - pointer to tty instance data | |
761 | * @filp - pointer to open file object for device | |
762 | * @poll_table - wait queue for operations | |
763 | * | |
764 | * Determine which operations (read/write) will not block and return info | |
765 | * to caller. | |
766 | * Returns a bit mask containing info on which ops will not block. | |
767 | */ | |
768 | static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp, | |
769 | poll_table *wait) | |
770 | { | |
771 | struct n_hdlc *n_hdlc = tty2n_hdlc (tty); | |
772 | unsigned int mask = 0; | |
773 | ||
774 | if (debuglevel >= DEBUG_LEVEL_INFO) | |
775 | printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__); | |
776 | ||
777 | if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) { | |
778 | /* queue current process into any wait queue that */ | |
779 | /* may awaken in the future (read and write) */ | |
780 | ||
781 | poll_wait(filp, &tty->read_wait, wait); | |
782 | poll_wait(filp, &tty->write_wait, wait); | |
783 | ||
784 | /* set bits for operations that won't block */ | |
785 | if(n_hdlc->rx_buf_list.head) | |
786 | mask |= POLLIN | POLLRDNORM; /* readable */ | |
787 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | |
788 | mask |= POLLHUP; | |
789 | if(tty_hung_up_p(filp)) | |
790 | mask |= POLLHUP; | |
791 | if(n_hdlc->tx_free_buf_list.head) | |
792 | mask |= POLLOUT | POLLWRNORM; /* writable */ | |
793 | } | |
794 | return mask; | |
795 | } /* end of n_hdlc_tty_poll() */ | |
796 | ||
797 | /** | |
798 | * n_hdlc_alloc - allocate an n_hdlc instance data structure | |
799 | * | |
800 | * Returns a pointer to newly created structure if success, otherwise %NULL | |
801 | */ | |
802 | static struct n_hdlc *n_hdlc_alloc(void) | |
803 | { | |
804 | struct n_hdlc_buf *buf; | |
805 | int i; | |
806 | struct n_hdlc *n_hdlc = kmalloc(sizeof(*n_hdlc), GFP_KERNEL); | |
807 | ||
808 | if (!n_hdlc) | |
809 | return NULL; | |
810 | ||
811 | memset(n_hdlc, 0, sizeof(*n_hdlc)); | |
812 | ||
813 | n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list); | |
814 | n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list); | |
815 | n_hdlc_buf_list_init(&n_hdlc->rx_buf_list); | |
816 | n_hdlc_buf_list_init(&n_hdlc->tx_buf_list); | |
817 | ||
818 | /* allocate free rx buffer list */ | |
819 | for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) { | |
820 | buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL); | |
821 | if (buf) | |
822 | n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf); | |
823 | else if (debuglevel >= DEBUG_LEVEL_INFO) | |
824 | printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i); | |
825 | } | |
826 | ||
827 | /* allocate free tx buffer list */ | |
828 | for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) { | |
829 | buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL); | |
830 | if (buf) | |
831 | n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf); | |
832 | else if (debuglevel >= DEBUG_LEVEL_INFO) | |
833 | printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i); | |
834 | } | |
835 | ||
836 | /* Initialize the control block */ | |
837 | n_hdlc->magic = HDLC_MAGIC; | |
838 | n_hdlc->flags = 0; | |
839 | ||
840 | return n_hdlc; | |
841 | ||
842 | } /* end of n_hdlc_alloc() */ | |
843 | ||
844 | /** | |
845 | * n_hdlc_buf_list_init - initialize specified HDLC buffer list | |
846 | * @list - pointer to buffer list | |
847 | */ | |
848 | static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list) | |
849 | { | |
850 | memset(list, 0, sizeof(*list)); | |
851 | spin_lock_init(&list->spinlock); | |
852 | } /* end of n_hdlc_buf_list_init() */ | |
853 | ||
854 | /** | |
855 | * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list | |
856 | * @list - pointer to buffer list | |
857 | * @buf - pointer to buffer | |
858 | */ | |
859 | static void n_hdlc_buf_put(struct n_hdlc_buf_list *list, | |
860 | struct n_hdlc_buf *buf) | |
861 | { | |
862 | unsigned long flags; | |
863 | spin_lock_irqsave(&list->spinlock,flags); | |
864 | ||
865 | buf->link=NULL; | |
866 | if(list->tail) | |
867 | list->tail->link = buf; | |
868 | else | |
869 | list->head = buf; | |
870 | list->tail = buf; | |
871 | (list->count)++; | |
872 | ||
873 | spin_unlock_irqrestore(&list->spinlock,flags); | |
874 | ||
875 | } /* end of n_hdlc_buf_put() */ | |
876 | ||
877 | /** | |
878 | * n_hdlc_buf_get - remove and return an HDLC buffer from list | |
879 | * @list - pointer to HDLC buffer list | |
880 | * | |
881 | * Remove and return an HDLC buffer from the head of the specified HDLC buffer | |
882 | * list. | |
883 | * Returns a pointer to HDLC buffer if available, otherwise %NULL. | |
884 | */ | |
885 | static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list) | |
886 | { | |
887 | unsigned long flags; | |
888 | struct n_hdlc_buf *buf; | |
889 | spin_lock_irqsave(&list->spinlock,flags); | |
890 | ||
891 | buf = list->head; | |
892 | if (buf) { | |
893 | list->head = buf->link; | |
894 | (list->count)--; | |
895 | } | |
896 | if (!list->head) | |
897 | list->tail = NULL; | |
898 | ||
899 | spin_unlock_irqrestore(&list->spinlock,flags); | |
900 | return buf; | |
901 | ||
902 | } /* end of n_hdlc_buf_get() */ | |
903 | ||
904 | static char hdlc_banner[] __initdata = | |
905 | KERN_INFO "HDLC line discipline: version " HDLC_VERSION | |
906 | ", maxframe=%u\n"; | |
907 | static char hdlc_register_ok[] __initdata = | |
908 | KERN_INFO "N_HDLC line discipline registered.\n"; | |
909 | static char hdlc_register_fail[] __initdata = | |
910 | KERN_ERR "error registering line discipline: %d\n"; | |
911 | static char hdlc_init_fail[] __initdata = | |
912 | KERN_INFO "N_HDLC: init failure %d\n"; | |
913 | ||
914 | static int __init n_hdlc_init(void) | |
915 | { | |
916 | int status; | |
917 | ||
918 | /* range check maxframe arg */ | |
919 | if (maxframe < 4096) | |
920 | maxframe = 4096; | |
921 | else if (maxframe > 65535) | |
922 | maxframe = 65535; | |
923 | ||
924 | printk(hdlc_banner, maxframe); | |
925 | ||
926 | status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc); | |
927 | if (!status) | |
928 | printk(hdlc_register_ok); | |
929 | else | |
930 | printk(hdlc_register_fail, status); | |
931 | ||
932 | if (status) | |
933 | printk(hdlc_init_fail, status); | |
934 | return status; | |
935 | ||
936 | } /* end of init_module() */ | |
937 | ||
938 | static char hdlc_unregister_ok[] __exitdata = | |
939 | KERN_INFO "N_HDLC: line discipline unregistered\n"; | |
940 | static char hdlc_unregister_fail[] __exitdata = | |
941 | KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n"; | |
942 | ||
943 | static void __exit n_hdlc_exit(void) | |
944 | { | |
945 | /* Release tty registration of line discipline */ | |
64ccd715 | 946 | int status = tty_unregister_ldisc(N_HDLC); |
1da177e4 LT |
947 | |
948 | if (status) | |
949 | printk(hdlc_unregister_fail, status); | |
950 | else | |
951 | printk(hdlc_unregister_ok); | |
952 | } | |
953 | ||
954 | module_init(n_hdlc_init); | |
955 | module_exit(n_hdlc_exit); | |
956 | ||
957 | MODULE_LICENSE("GPL"); | |
958 | MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com"); | |
959 | module_param(debuglevel, int, 0); | |
960 | module_param(maxframe, int, 0); | |
961 | MODULE_ALIAS_LDISC(N_HDLC); |