hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/tty/n_hdlc.c
....@@ -18,7 +18,7 @@
1818 * All HDLC data is frame oriented which means:
1919 *
2020 * 1. tty write calls represent one complete transmit frame of data
21
- * The device driver should accept the complete frame or none of
21
+ * The device driver should accept the complete frame or none of
2222 * the frame (busy) in the write method. Each write call should have
2323 * a byte count in the range of 2-65535 bytes (2 is min HDLC frame
2424 * with 1 addr byte and 1 ctrl byte). The max byte count of 65535
....@@ -39,7 +39,7 @@
3939 * tty read calls.
4040 *
4141 * 3. tty read calls returns an entire frame of data or nothing.
42
- *
42
+ *
4343 * 4. all send and receive data is considered raw. No processing
4444 * or translation is performed by the line discipline, regardless
4545 * of the tty flags
....@@ -87,9 +87,6 @@
8787 #include <linux/interrupt.h>
8888 #include <linux/ptrace.h>
8989
90
-#undef VERSION
91
-#define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
92
-
9390 #include <linux/poll.h>
9491 #include <linux/in.h>
9592 #include <linux/ioctl.h>
....@@ -103,11 +100,12 @@
103100
104101 #include <asm/termios.h>
105102 #include <linux/uaccess.h>
103
+#include "tty.h"
106104
107105 /*
108106 * Buffers for individual HDLC frames
109107 */
110
-#define MAX_HDLC_FRAME_SIZE 65535
108
+#define MAX_HDLC_FRAME_SIZE 65535
111109 #define DEFAULT_RX_BUF_COUNT 10
112110 #define MAX_RX_BUF_COUNT 60
113111 #define DEFAULT_TX_BUF_COUNT 3
....@@ -115,10 +113,8 @@
115113 struct n_hdlc_buf {
116114 struct list_head list_item;
117115 int count;
118
- char buf[1];
116
+ char buf[];
119117 };
120
-
121
-#define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
122118
123119 struct n_hdlc_buf_list {
124120 struct list_head list;
....@@ -128,28 +124,24 @@
128124
129125 /**
130126 * struct n_hdlc - per device instance data structure
131
- * @magic - magic value for structure
132
- * @flags - miscellaneous control flags
133
- * @tty - ptr to TTY structure
134
- * @backup_tty - TTY to use if tty gets closed
135
- * @tbusy - reentrancy flag for tx wakeup code
136
- * @woke_up - FIXME: describe this field
137
- * @tx_buf_list - list of pending transmit frame buffers
138
- * @rx_buf_list - list of received frame buffers
139
- * @tx_free_buf_list - list unused transmit frame buffers
140
- * @rx_free_buf_list - list unused received frame buffers
127
+ * @magic: magic value for structure
128
+ * @tbusy: reentrancy flag for tx wakeup code
129
+ * @woke_up: tx wakeup needs to be run again as it was called while @tbusy
130
+ * @tx_buf_list: list of pending transmit frame buffers
131
+ * @rx_buf_list: list of received frame buffers
132
+ * @tx_free_buf_list: list unused transmit frame buffers
133
+ * @rx_free_buf_list: list unused received frame buffers
141134 */
142135 struct n_hdlc {
143136 int magic;
144
- __u32 flags;
145
- struct tty_struct *tty;
146
- struct tty_struct *backup_tty;
147
- int tbusy;
148
- int woke_up;
137
+ bool tbusy;
138
+ bool woke_up;
149139 struct n_hdlc_buf_list tx_buf_list;
150140 struct n_hdlc_buf_list rx_buf_list;
151141 struct n_hdlc_buf_list tx_free_buf_list;
152142 struct n_hdlc_buf_list rx_free_buf_list;
143
+ struct work_struct write_work;
144
+ struct tty_struct *tty_for_write_work;
153145 };
154146
155147 /*
....@@ -163,39 +155,15 @@
163155
164156 /* Local functions */
165157
166
-static struct n_hdlc *n_hdlc_alloc (void);
167
-
168
-/* debug level can be set by insmod for debugging purposes */
169
-#define DEBUG_LEVEL_INFO 1
170
-static int debuglevel;
158
+static struct n_hdlc *n_hdlc_alloc(void);
159
+static void n_hdlc_tty_write_work(struct work_struct *work);
171160
172161 /* max frame size for memory allocations */
173162 static int maxframe = 4096;
174163
175
-/* TTY callbacks */
176
-
177
-static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
178
- __u8 __user *buf, size_t nr);
179
-static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
180
- const unsigned char *buf, size_t nr);
181
-static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
182
- unsigned int cmd, unsigned long arg);
183
-static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
184
- poll_table *wait);
185
-static int n_hdlc_tty_open(struct tty_struct *tty);
186
-static void n_hdlc_tty_close(struct tty_struct *tty);
187
-static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
188
- char *fp, int count);
189
-static void n_hdlc_tty_wakeup(struct tty_struct *tty);
190
-
191
-#define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
192
-
193
-#define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
194
-#define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
195
-
196164 static void flush_rx_queue(struct tty_struct *tty)
197165 {
198
- struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
166
+ struct n_hdlc *n_hdlc = tty->disc_data;
199167 struct n_hdlc_buf *buf;
200168
201169 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
....@@ -204,169 +172,99 @@
204172
205173 static void flush_tx_queue(struct tty_struct *tty)
206174 {
207
- struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
175
+ struct n_hdlc *n_hdlc = tty->disc_data;
208176 struct n_hdlc_buf *buf;
209177
210178 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
211179 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
212180 }
213181
214
-static struct tty_ldisc_ops n_hdlc_ldisc = {
215
- .owner = THIS_MODULE,
216
- .magic = TTY_LDISC_MAGIC,
217
- .name = "hdlc",
218
- .open = n_hdlc_tty_open,
219
- .close = n_hdlc_tty_close,
220
- .read = n_hdlc_tty_read,
221
- .write = n_hdlc_tty_write,
222
- .ioctl = n_hdlc_tty_ioctl,
223
- .poll = n_hdlc_tty_poll,
224
- .receive_buf = n_hdlc_tty_receive,
225
- .write_wakeup = n_hdlc_tty_wakeup,
226
- .flush_buffer = flush_rx_queue,
227
-};
228
-
229
-/**
230
- * n_hdlc_release - release an n_hdlc per device line discipline info structure
231
- * @n_hdlc - per device line discipline info structure
232
- */
233
-static void n_hdlc_release(struct n_hdlc *n_hdlc)
182
+static void n_hdlc_free_buf_list(struct n_hdlc_buf_list *list)
234183 {
235
- struct tty_struct *tty = n_hdlc2tty (n_hdlc);
236184 struct n_hdlc_buf *buf;
237
-
238
- if (debuglevel >= DEBUG_LEVEL_INFO)
239
- printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
240
-
241
- /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
242
- wake_up_interruptible (&tty->read_wait);
243
- wake_up_interruptible (&tty->write_wait);
244185
245
- if (tty->disc_data == n_hdlc)
246
- tty->disc_data = NULL; /* Break the tty->n_hdlc link */
247
-
248
- /* Release transmit and receive buffers */
249
- for(;;) {
250
- buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
251
- if (buf) {
252
- kfree(buf);
253
- } else
254
- break;
255
- }
256
- for(;;) {
257
- buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
258
- if (buf) {
259
- kfree(buf);
260
- } else
261
- break;
262
- }
263
- for(;;) {
264
- buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
265
- if (buf) {
266
- kfree(buf);
267
- } else
268
- break;
269
- }
270
- for(;;) {
271
- buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
272
- if (buf) {
273
- kfree(buf);
274
- } else
275
- break;
276
- }
277
- kfree(n_hdlc);
278
-
279
-} /* end of n_hdlc_release() */
186
+ do {
187
+ buf = n_hdlc_buf_get(list);
188
+ kfree(buf);
189
+ } while (buf);
190
+}
280191
281192 /**
282193 * n_hdlc_tty_close - line discipline close
283
- * @tty - pointer to tty info structure
194
+ * @tty: pointer to tty info structure
284195 *
285196 * Called when the line discipline is changed to something
286197 * else, the tty is closed, or the tty detects a hangup.
287198 */
288199 static void n_hdlc_tty_close(struct tty_struct *tty)
289200 {
290
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
201
+ struct n_hdlc *n_hdlc = tty->disc_data;
291202
292
- if (debuglevel >= DEBUG_LEVEL_INFO)
293
- printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
294
-
295
- if (n_hdlc != NULL) {
296
- if (n_hdlc->magic != HDLC_MAGIC) {
297
- printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
298
- return;
299
- }
300
-#if defined(TTY_NO_WRITE_SPLIT)
301
- clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
302
-#endif
303
- tty->disc_data = NULL;
304
- if (tty == n_hdlc->backup_tty)
305
- n_hdlc->backup_tty = NULL;
306
- if (tty != n_hdlc->tty)
307
- return;
308
- if (n_hdlc->backup_tty) {
309
- n_hdlc->tty = n_hdlc->backup_tty;
310
- } else {
311
- n_hdlc_release (n_hdlc);
312
- }
203
+ if (n_hdlc->magic != HDLC_MAGIC) {
204
+ pr_warn("n_hdlc: trying to close unopened tty!\n");
205
+ return;
313206 }
314
-
315
- if (debuglevel >= DEBUG_LEVEL_INFO)
316
- printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
317
-
207
+#if defined(TTY_NO_WRITE_SPLIT)
208
+ clear_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
209
+#endif
210
+ tty->disc_data = NULL;
211
+
212
+ /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
213
+ wake_up_interruptible(&tty->read_wait);
214
+ wake_up_interruptible(&tty->write_wait);
215
+
216
+ cancel_work_sync(&n_hdlc->write_work);
217
+
218
+ n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list);
219
+ n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list);
220
+ n_hdlc_free_buf_list(&n_hdlc->rx_buf_list);
221
+ n_hdlc_free_buf_list(&n_hdlc->tx_buf_list);
222
+ kfree(n_hdlc);
318223 } /* end of n_hdlc_tty_close() */
319224
320225 /**
321226 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
322
- * @tty - pointer to tty info structure
227
+ * @tty: pointer to tty info structure
323228 *
324229 * Returns 0 if success, otherwise error code
325230 */
326
-static int n_hdlc_tty_open (struct tty_struct *tty)
231
+static int n_hdlc_tty_open(struct tty_struct *tty)
327232 {
328
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
233
+ struct n_hdlc *n_hdlc = tty->disc_data;
329234
330
- if (debuglevel >= DEBUG_LEVEL_INFO)
331
- printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
332
- __FILE__,__LINE__,
333
- tty->name);
334
-
235
+ pr_debug("%s() called (device=%s)\n", __func__, tty->name);
236
+
335237 /* There should not be an existing table for this slot. */
336238 if (n_hdlc) {
337
- printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
239
+ pr_err("%s: tty already associated!\n", __func__);
338240 return -EEXIST;
339241 }
340
-
242
+
341243 n_hdlc = n_hdlc_alloc();
342244 if (!n_hdlc) {
343
- printk (KERN_ERR "n_hdlc_alloc failed\n");
245
+ pr_err("%s: n_hdlc_alloc failed\n", __func__);
344246 return -ENFILE;
345247 }
346
-
248
+
249
+ INIT_WORK(&n_hdlc->write_work, n_hdlc_tty_write_work);
250
+ n_hdlc->tty_for_write_work = tty;
347251 tty->disc_data = n_hdlc;
348
- n_hdlc->tty = tty;
349252 tty->receive_room = 65536;
350
-
351
-#if defined(TTY_NO_WRITE_SPLIT)
253
+
352254 /* change tty_io write() to not split large writes into 8K chunks */
353
- set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
354
-#endif
355
-
255
+ set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
256
+
356257 /* flush receive data from driver */
357258 tty_driver_flush_buffer(tty);
358
-
359
- if (debuglevel >= DEBUG_LEVEL_INFO)
360
- printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
361
-
259
+
362260 return 0;
363
-
261
+
364262 } /* end of n_tty_hdlc_open() */
365263
366264 /**
367265 * n_hdlc_send_frames - send frames on pending send buffer list
368
- * @n_hdlc - pointer to ldisc instance data
369
- * @tty - pointer to tty instance data
266
+ * @n_hdlc: pointer to ldisc instance data
267
+ * @tty: pointer to tty instance data
370268 *
371269 * Send frames on pending send buffer list until the driver does not accept a
372270 * frame (busy) this function is called after adding a frame to the send buffer
....@@ -378,26 +276,22 @@
378276 unsigned long flags;
379277 struct n_hdlc_buf *tbuf;
380278
381
- if (debuglevel >= DEBUG_LEVEL_INFO)
382
- printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
383
- check_again:
384
-
385
- spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
279
+check_again:
280
+
281
+ spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
386282 if (n_hdlc->tbusy) {
387
- n_hdlc->woke_up = 1;
388
- spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
283
+ n_hdlc->woke_up = true;
284
+ spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
389285 return;
390286 }
391
- n_hdlc->tbusy = 1;
392
- n_hdlc->woke_up = 0;
287
+ n_hdlc->tbusy = true;
288
+ n_hdlc->woke_up = false;
393289 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
394290
395291 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
396292 while (tbuf) {
397
- if (debuglevel >= DEBUG_LEVEL_INFO)
398
- printk("%s(%d)sending frame %p, count=%d\n",
399
- __FILE__,__LINE__,tbuf,tbuf->count);
400
-
293
+ pr_debug("sending frame %p, count=%d\n", tbuf, tbuf->count);
294
+
401295 /* Send the next block of data to device */
402296 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
403297 actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
....@@ -411,24 +305,20 @@
411305 /* pretending it was accepted by driver */
412306 if (actual < 0)
413307 actual = tbuf->count;
414
-
308
+
415309 if (actual == tbuf->count) {
416
- if (debuglevel >= DEBUG_LEVEL_INFO)
417
- printk("%s(%d)frame %p completed\n",
418
- __FILE__,__LINE__,tbuf);
419
-
310
+ pr_debug("frame %p completed\n", tbuf);
311
+
420312 /* free current transmit buffer */
421313 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
422314
423315 /* wait up sleeping writers */
424316 wake_up_interruptible(&tty->write_wait);
425
-
317
+
426318 /* get next pending transmit buffer */
427319 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
428320 } else {
429
- if (debuglevel >= DEBUG_LEVEL_INFO)
430
- printk("%s(%d)frame %p pending\n",
431
- __FILE__,__LINE__,tbuf);
321
+ pr_debug("frame %p pending\n", tbuf);
432322
433323 /*
434324 * the buffer was not accepted by driver,
....@@ -438,54 +328,52 @@
438328 break;
439329 }
440330 }
441
-
331
+
442332 if (!tbuf)
443333 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
444
-
334
+
445335 /* Clear the re-entry flag */
446336 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
447
- n_hdlc->tbusy = 0;
448
- spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
449
-
450
- if (n_hdlc->woke_up)
451
- goto check_again;
337
+ n_hdlc->tbusy = false;
338
+ spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
452339
453
- if (debuglevel >= DEBUG_LEVEL_INFO)
454
- printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
455
-
340
+ if (n_hdlc->woke_up)
341
+ goto check_again;
456342 } /* end of n_hdlc_send_frames() */
457343
458344 /**
345
+ * n_hdlc_tty_write_work - Asynchronous callback for transmit wakeup
346
+ * @work: pointer to work_struct
347
+ *
348
+ * Called when low level device driver can accept more send data.
349
+ */
350
+static void n_hdlc_tty_write_work(struct work_struct *work)
351
+{
352
+ struct n_hdlc *n_hdlc = container_of(work, struct n_hdlc, write_work);
353
+ struct tty_struct *tty = n_hdlc->tty_for_write_work;
354
+
355
+ n_hdlc_send_frames(n_hdlc, tty);
356
+} /* end of n_hdlc_tty_write_work() */
357
+
358
+/**
459359 * n_hdlc_tty_wakeup - Callback for transmit wakeup
460
- * @tty - pointer to associated tty instance data
360
+ * @tty: pointer to associated tty instance data
461361 *
462362 * Called when low level device driver can accept more send data.
463363 */
464364 static void n_hdlc_tty_wakeup(struct tty_struct *tty)
465365 {
466
- struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
366
+ struct n_hdlc *n_hdlc = tty->disc_data;
467367
468
- if (debuglevel >= DEBUG_LEVEL_INFO)
469
- printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
470
-
471
- if (!n_hdlc)
472
- return;
473
-
474
- if (tty != n_hdlc->tty) {
475
- clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
476
- return;
477
- }
478
-
479
- n_hdlc_send_frames (n_hdlc, tty);
480
-
368
+ schedule_work(&n_hdlc->write_work);
481369 } /* end of n_hdlc_tty_wakeup() */
482370
483371 /**
484372 * n_hdlc_tty_receive - Called by tty driver when receive data is available
485
- * @tty - pointer to tty instance data
486
- * @data - pointer to received data
487
- * @flags - pointer to flags for data
488
- * @count - count of received data in bytes
373
+ * @tty: pointer to tty instance data
374
+ * @data: pointer to received data
375
+ * @flags: pointer to flags for data
376
+ * @count: count of received data in bytes
489377 *
490378 * Called by tty low level driver when receive data is available. Data is
491379 * interpreted as one HDLC frame.
....@@ -493,91 +381,75 @@
493381 static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
494382 char *flags, int count)
495383 {
496
- register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
384
+ register struct n_hdlc *n_hdlc = tty->disc_data;
497385 register struct n_hdlc_buf *buf;
498386
499
- if (debuglevel >= DEBUG_LEVEL_INFO)
500
- printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
501
- __FILE__,__LINE__, count);
502
-
503
- /* This can happen if stuff comes in on the backup tty */
504
- if (!n_hdlc || tty != n_hdlc->tty)
505
- return;
506
-
387
+ pr_debug("%s() called count=%d\n", __func__, count);
388
+
507389 /* verify line is using HDLC discipline */
508390 if (n_hdlc->magic != HDLC_MAGIC) {
509
- printk("%s(%d) line not using HDLC discipline\n",
510
- __FILE__,__LINE__);
511
- return;
512
- }
513
-
514
- if ( count>maxframe ) {
515
- if (debuglevel >= DEBUG_LEVEL_INFO)
516
- printk("%s(%d) rx count>maxframesize, data discarded\n",
517
- __FILE__,__LINE__);
391
+ pr_err("line not using HDLC discipline\n");
518392 return;
519393 }
520394
521
- /* get a free HDLC buffer */
522
- buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
523
- if (!buf) {
524
- /* no buffers in free list, attempt to allocate another rx buffer */
525
- /* unless the maximum count has been reached */
526
- if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
527
- buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC);
528
- }
529
-
530
- if (!buf) {
531
- if (debuglevel >= DEBUG_LEVEL_INFO)
532
- printk("%s(%d) no more rx buffers, data discarded\n",
533
- __FILE__,__LINE__);
395
+ if (count > maxframe) {
396
+ pr_debug("rx count>maxframesize, data discarded\n");
534397 return;
535398 }
536
-
399
+
400
+ /* get a free HDLC buffer */
401
+ buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
402
+ if (!buf) {
403
+ /*
404
+ * no buffers in free list, attempt to allocate another rx
405
+ * buffer unless the maximum count has been reached
406
+ */
407
+ if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
408
+ buf = kmalloc(struct_size(buf, buf, maxframe),
409
+ GFP_ATOMIC);
410
+ }
411
+
412
+ if (!buf) {
413
+ pr_debug("no more rx buffers, data discarded\n");
414
+ return;
415
+ }
416
+
537417 /* copy received data to HDLC buffer */
538
- memcpy(buf->buf,data,count);
539
- buf->count=count;
418
+ memcpy(buf->buf, data, count);
419
+ buf->count = count;
540420
541421 /* add HDLC buffer to list of received frames */
542422 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
543
-
423
+
544424 /* wake up any blocked reads and perform async signalling */
545
- wake_up_interruptible (&tty->read_wait);
546
- if (n_hdlc->tty->fasync != NULL)
547
- kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
425
+ wake_up_interruptible(&tty->read_wait);
426
+ if (tty->fasync != NULL)
427
+ kill_fasync(&tty->fasync, SIGIO, POLL_IN);
548428
549429 } /* end of n_hdlc_tty_receive() */
550430
551431 /**
552432 * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
553
- * @tty - pointer to tty instance data
554
- * @file - pointer to open file object
555
- * @buf - pointer to returned data buffer
556
- * @nr - size of returned data buffer
557
- *
433
+ * @tty: pointer to tty instance data
434
+ * @file: pointer to open file object
435
+ * @buf: pointer to returned data buffer
436
+ * @nr: size of returned data buffer
437
+ *
558438 * Returns the number of bytes returned or error code.
559439 */
560440 static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
561
- __u8 __user *buf, size_t nr)
441
+ __u8 *kbuf, size_t nr,
442
+ void **cookie, unsigned long offset)
562443 {
563
- struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
444
+ struct n_hdlc *n_hdlc = tty->disc_data;
564445 int ret = 0;
565446 struct n_hdlc_buf *rbuf;
566447 DECLARE_WAITQUEUE(wait, current);
567448
568
- if (debuglevel >= DEBUG_LEVEL_INFO)
569
- printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
570
-
571
- /* Validate the pointers */
572
- if (!n_hdlc)
573
- return -EIO;
574
-
575
- /* verify user access to buffer */
576
- if (!access_ok(VERIFY_WRITE, buf, nr)) {
577
- printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
578
- "buffer\n", __FILE__, __LINE__);
579
- return -EFAULT;
580
- }
449
+ /* Is this a repeated call for an rbuf we already found earlier? */
450
+ rbuf = *cookie;
451
+ if (rbuf)
452
+ goto have_rbuf;
581453
582454 add_wait_queue(&tty->read_wait, &wait);
583455
....@@ -592,26 +464,9 @@
592464 set_current_state(TASK_INTERRUPTIBLE);
593465
594466 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
595
- if (rbuf) {
596
- if (rbuf->count > nr) {
597
- /* too large for caller's buffer */
598
- ret = -EOVERFLOW;
599
- } else {
600
- __set_current_state(TASK_RUNNING);
601
- if (copy_to_user(buf, rbuf->buf, rbuf->count))
602
- ret = -EFAULT;
603
- else
604
- ret = rbuf->count;
605
- }
606
-
607
- if (n_hdlc->rx_free_buf_list.count >
608
- DEFAULT_RX_BUF_COUNT)
609
- kfree(rbuf);
610
- else
611
- n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
467
+ if (rbuf)
612468 break;
613
- }
614
-
469
+
615470 /* no data */
616471 if (tty_io_nonblock(tty, file)) {
617472 ret = -EAGAIN;
....@@ -629,53 +484,77 @@
629484 remove_wait_queue(&tty->read_wait, &wait);
630485 __set_current_state(TASK_RUNNING);
631486
487
+ if (!rbuf)
488
+ return ret;
489
+ *cookie = rbuf;
490
+
491
+have_rbuf:
492
+ /* Have we used it up entirely? */
493
+ if (offset >= rbuf->count)
494
+ goto done_with_rbuf;
495
+
496
+ /* More data to go, but can't copy any more? EOVERFLOW */
497
+ ret = -EOVERFLOW;
498
+ if (!nr)
499
+ goto done_with_rbuf;
500
+
501
+ /* Copy as much data as possible */
502
+ ret = rbuf->count - offset;
503
+ if (ret > nr)
504
+ ret = nr;
505
+ memcpy(kbuf, rbuf->buf+offset, ret);
506
+ offset += ret;
507
+
508
+ /* If we still have data left, we leave the rbuf in the cookie */
509
+ if (offset < rbuf->count)
510
+ return ret;
511
+
512
+done_with_rbuf:
513
+ *cookie = NULL;
514
+
515
+ if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
516
+ kfree(rbuf);
517
+ else
518
+ n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
519
+
632520 return ret;
633
-
521
+
634522 } /* end of n_hdlc_tty_read() */
635523
636524 /**
637525 * n_hdlc_tty_write - write a single frame of data to device
638
- * @tty - pointer to associated tty device instance data
639
- * @file - pointer to file object data
640
- * @data - pointer to transmit data (one frame)
641
- * @count - size of transmit frame in bytes
642
- *
526
+ * @tty: pointer to associated tty device instance data
527
+ * @file: pointer to file object data
528
+ * @data: pointer to transmit data (one frame)
529
+ * @count: size of transmit frame in bytes
530
+ *
643531 * Returns the number of bytes written (or error code).
644532 */
645533 static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
646534 const unsigned char *data, size_t count)
647535 {
648
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
536
+ struct n_hdlc *n_hdlc = tty->disc_data;
649537 int error = 0;
650538 DECLARE_WAITQUEUE(wait, current);
651539 struct n_hdlc_buf *tbuf;
652540
653
- if (debuglevel >= DEBUG_LEVEL_INFO)
654
- printk("%s(%d)n_hdlc_tty_write() called count=%zd\n",
655
- __FILE__,__LINE__,count);
656
-
657
- /* Verify pointers */
658
- if (!n_hdlc)
659
- return -EIO;
541
+ pr_debug("%s() called count=%zd\n", __func__, count);
660542
661543 if (n_hdlc->magic != HDLC_MAGIC)
662544 return -EIO;
663545
664546 /* verify frame size */
665
- if (count > maxframe ) {
666
- if (debuglevel & DEBUG_LEVEL_INFO)
667
- printk (KERN_WARNING
668
- "n_hdlc_tty_write: truncating user packet "
669
- "from %lu to %d\n", (unsigned long) count,
670
- maxframe );
547
+ if (count > maxframe) {
548
+ pr_debug("%s: truncating user packet from %zu to %d\n",
549
+ __func__, count, maxframe);
671550 count = maxframe;
672551 }
673
-
552
+
674553 add_wait_queue(&tty->write_wait, &wait);
675554
676555 for (;;) {
677556 set_current_state(TASK_INTERRUPTIBLE);
678
-
557
+
679558 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
680559 if (tbuf)
681560 break;
....@@ -685,15 +564,7 @@
685564 break;
686565 }
687566 schedule();
688
-
689
- n_hdlc = tty2n_hdlc (tty);
690
- if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
691
- tty != n_hdlc->tty) {
692
- printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
693
- error = -EIO;
694
- break;
695
- }
696
-
567
+
697568 if (signal_pending(current)) {
698569 error = -EINTR;
699570 break;
....@@ -703,58 +574,56 @@
703574 __set_current_state(TASK_RUNNING);
704575 remove_wait_queue(&tty->write_wait, &wait);
705576
706
- if (!error) {
577
+ if (!error) {
707578 /* Retrieve the user's buffer */
708579 memcpy(tbuf->buf, data, count);
709580
710581 /* Send the data */
711582 tbuf->count = error = count;
712
- n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
713
- n_hdlc_send_frames(n_hdlc,tty);
583
+ n_hdlc_buf_put(&n_hdlc->tx_buf_list, tbuf);
584
+ n_hdlc_send_frames(n_hdlc, tty);
714585 }
715586
716587 return error;
717
-
588
+
718589 } /* end of n_hdlc_tty_write() */
719590
720591 /**
721592 * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
722
- * @tty - pointer to tty instance data
723
- * @file - pointer to open file object for device
724
- * @cmd - IOCTL command code
725
- * @arg - argument for IOCTL call (cmd dependent)
593
+ * @tty: pointer to tty instance data
594
+ * @file: pointer to open file object for device
595
+ * @cmd: IOCTL command code
596
+ * @arg: argument for IOCTL call (cmd dependent)
726597 *
727598 * Returns command dependent result.
728599 */
729600 static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
730601 unsigned int cmd, unsigned long arg)
731602 {
732
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
603
+ struct n_hdlc *n_hdlc = tty->disc_data;
733604 int error = 0;
734605 int count;
735606 unsigned long flags;
736607 struct n_hdlc_buf *buf = NULL;
737608
738
- if (debuglevel >= DEBUG_LEVEL_INFO)
739
- printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
740
- __FILE__,__LINE__,cmd);
741
-
609
+ pr_debug("%s() called %d\n", __func__, cmd);
610
+
742611 /* Verify the status of the device */
743
- if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
612
+ if (n_hdlc->magic != HDLC_MAGIC)
744613 return -EBADF;
745614
746615 switch (cmd) {
747616 case FIONREAD:
748617 /* report count of read data available */
749618 /* in next available frame (if any) */
750
- spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
619
+ spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags);
751620 buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
752621 struct n_hdlc_buf, list_item);
753622 if (buf)
754623 count = buf->count;
755624 else
756625 count = 0;
757
- spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
626
+ spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags);
758627 error = put_user(count, (int __user *)arg);
759628 break;
760629
....@@ -762,12 +631,12 @@
762631 /* get the pending tx byte count in the driver */
763632 count = tty_chars_in_buffer(tty);
764633 /* add size of next output frame in queue */
765
- spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
634
+ spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
766635 buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
767636 struct n_hdlc_buf, list_item);
768637 if (buf)
769638 count += buf->count;
770
- spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
639
+ spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
771640 error = put_user(count, (int __user *)arg);
772641 break;
773642
....@@ -777,22 +646,22 @@
777646 case TCOFLUSH:
778647 flush_tx_queue(tty);
779648 }
780
- /* fall through to default */
649
+ fallthrough; /* to default */
781650
782651 default:
783652 error = n_tty_ioctl_helper(tty, file, cmd, arg);
784653 break;
785654 }
786655 return error;
787
-
656
+
788657 } /* end of n_hdlc_tty_ioctl() */
789658
790659 /**
791660 * n_hdlc_tty_poll - TTY callback for poll system call
792
- * @tty - pointer to tty instance data
793
- * @filp - pointer to open file object for device
794
- * @poll_table - wait queue for operations
795
- *
661
+ * @tty: pointer to tty instance data
662
+ * @filp: pointer to open file object for device
663
+ * @wait: wait queue for operations
664
+ *
796665 * Determine which operations (read/write) will not block and return info
797666 * to caller.
798667 * Returns a bit mask containing info on which ops will not block.
....@@ -800,32 +669,49 @@
800669 static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
801670 poll_table *wait)
802671 {
803
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
672
+ struct n_hdlc *n_hdlc = tty->disc_data;
804673 __poll_t mask = 0;
805674
806
- if (debuglevel >= DEBUG_LEVEL_INFO)
807
- printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
808
-
809
- if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
810
- /* queue current process into any wait queue that */
811
- /* may awaken in the future (read and write) */
675
+ if (n_hdlc->magic != HDLC_MAGIC)
676
+ return 0;
812677
813
- poll_wait(filp, &tty->read_wait, wait);
814
- poll_wait(filp, &tty->write_wait, wait);
678
+ /*
679
+ * queue the current process into any wait queue that may awaken in the
680
+ * future (read and write)
681
+ */
682
+ poll_wait(filp, &tty->read_wait, wait);
683
+ poll_wait(filp, &tty->write_wait, wait);
815684
816
- /* set bits for operations that won't block */
817
- if (!list_empty(&n_hdlc->rx_buf_list.list))
818
- mask |= EPOLLIN | EPOLLRDNORM; /* readable */
819
- if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
820
- mask |= EPOLLHUP;
821
- if (tty_hung_up_p(filp))
822
- mask |= EPOLLHUP;
823
- if (!tty_is_writelocked(tty) &&
824
- !list_empty(&n_hdlc->tx_free_buf_list.list))
825
- mask |= EPOLLOUT | EPOLLWRNORM; /* writable */
826
- }
685
+ /* set bits for operations that won't block */
686
+ if (!list_empty(&n_hdlc->rx_buf_list.list))
687
+ mask |= EPOLLIN | EPOLLRDNORM; /* readable */
688
+ if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
689
+ mask |= EPOLLHUP;
690
+ if (tty_hung_up_p(filp))
691
+ mask |= EPOLLHUP;
692
+ if (!tty_is_writelocked(tty) &&
693
+ !list_empty(&n_hdlc->tx_free_buf_list.list))
694
+ mask |= EPOLLOUT | EPOLLWRNORM; /* writable */
695
+
827696 return mask;
828697 } /* end of n_hdlc_tty_poll() */
698
+
699
+static void n_hdlc_alloc_buf(struct n_hdlc_buf_list *list, unsigned int count,
700
+ const char *name)
701
+{
702
+ struct n_hdlc_buf *buf;
703
+ unsigned int i;
704
+
705
+ for (i = 0; i < count; i++) {
706
+ buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL);
707
+ if (!buf) {
708
+ pr_debug("%s(), kmalloc() failed for %s buffer %u\n",
709
+ __func__, name, i);
710
+ return;
711
+ }
712
+ n_hdlc_buf_put(list, buf);
713
+ }
714
+}
829715
830716 /**
831717 * n_hdlc_alloc - allocate an n_hdlc instance data structure
....@@ -834,8 +720,6 @@
834720 */
835721 static struct n_hdlc *n_hdlc_alloc(void)
836722 {
837
- struct n_hdlc_buf *buf;
838
- int i;
839723 struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
840724
841725 if (!n_hdlc)
....@@ -851,36 +735,20 @@
851735 INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
852736 INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
853737
854
- /* allocate free rx buffer list */
855
- for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
856
- buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
857
- if (buf)
858
- n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
859
- else if (debuglevel >= DEBUG_LEVEL_INFO)
860
- printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
861
- }
862
-
863
- /* allocate free tx buffer list */
864
- for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
865
- buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
866
- if (buf)
867
- n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
868
- else if (debuglevel >= DEBUG_LEVEL_INFO)
869
- printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
870
- }
871
-
738
+ n_hdlc_alloc_buf(&n_hdlc->rx_free_buf_list, DEFAULT_RX_BUF_COUNT, "rx");
739
+ n_hdlc_alloc_buf(&n_hdlc->tx_free_buf_list, DEFAULT_TX_BUF_COUNT, "tx");
740
+
872741 /* Initialize the control block */
873742 n_hdlc->magic = HDLC_MAGIC;
874
- n_hdlc->flags = 0;
875
-
743
+
876744 return n_hdlc;
877
-
745
+
878746 } /* end of n_hdlc_alloc() */
879747
880748 /**
881749 * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
882
- * @buf_list - pointer to the buffer list
883
- * @buf - pointer to the buffer
750
+ * @buf_list: pointer to the buffer list
751
+ * @buf: pointer to the buffer
884752 */
885753 static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
886754 struct n_hdlc_buf *buf)
....@@ -897,8 +765,8 @@
897765
898766 /**
899767 * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
900
- * @buf_list - pointer to buffer list
901
- * @buf - pointer to buffer
768
+ * @buf_list: pointer to buffer list
769
+ * @buf: pointer to buffer
902770 */
903771 static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
904772 struct n_hdlc_buf *buf)
....@@ -915,8 +783,8 @@
915783
916784 /**
917785 * n_hdlc_buf_get - remove and return an HDLC buffer from list
918
- * @buf_list - pointer to HDLC buffer list
919
- *
786
+ * @buf_list: pointer to HDLC buffer list
787
+ *
920788 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
921789 * list.
922790 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
....@@ -939,44 +807,39 @@
939807 return buf;
940808 } /* end of n_hdlc_buf_get() */
941809
942
-static const char hdlc_banner[] __initconst =
943
- KERN_INFO "HDLC line discipline maxframe=%u\n";
944
-static const char hdlc_register_ok[] __initconst =
945
- KERN_INFO "N_HDLC line discipline registered.\n";
946
-static const char hdlc_register_fail[] __initconst =
947
- KERN_ERR "error registering line discipline: %d\n";
810
+static struct tty_ldisc_ops n_hdlc_ldisc = {
811
+ .owner = THIS_MODULE,
812
+ .magic = TTY_LDISC_MAGIC,
813
+ .name = "hdlc",
814
+ .open = n_hdlc_tty_open,
815
+ .close = n_hdlc_tty_close,
816
+ .read = n_hdlc_tty_read,
817
+ .write = n_hdlc_tty_write,
818
+ .ioctl = n_hdlc_tty_ioctl,
819
+ .poll = n_hdlc_tty_poll,
820
+ .receive_buf = n_hdlc_tty_receive,
821
+ .write_wakeup = n_hdlc_tty_wakeup,
822
+ .flush_buffer = flush_rx_queue,
823
+};
948824
949825 static int __init n_hdlc_init(void)
950826 {
951827 int status;
952828
953829 /* range check maxframe arg */
954
- if (maxframe < 4096)
955
- maxframe = 4096;
956
- else if (maxframe > 65535)
957
- maxframe = 65535;
958
-
959
- printk(hdlc_banner, maxframe);
830
+ maxframe = clamp(maxframe, 4096, MAX_HDLC_FRAME_SIZE);
960831
961832 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
962833 if (!status)
963
- printk(hdlc_register_ok);
834
+ pr_info("N_HDLC line discipline registered with maxframe=%d\n",
835
+ maxframe);
964836 else
965
- printk(hdlc_register_fail, status);
837
+ pr_err("N_HDLC: error registering line discipline: %d\n",
838
+ status);
966839
967840 return status;
968
-
841
+
969842 } /* end of init_module() */
970
-
971
-#ifdef CONFIG_SPARC
972
-#undef __exitdata
973
-#define __exitdata
974
-#endif
975
-
976
-static const char hdlc_unregister_ok[] __exitdata =
977
- KERN_INFO "N_HDLC: line discipline unregistered\n";
978
-static const char hdlc_unregister_fail[] __exitdata =
979
- KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
980843
981844 static void __exit n_hdlc_exit(void)
982845 {
....@@ -984,9 +847,10 @@
984847 int status = tty_unregister_ldisc(N_HDLC);
985848
986849 if (status)
987
- printk(hdlc_unregister_fail, status);
850
+ pr_err("N_HDLC: can't unregister line discipline (err = %d)\n",
851
+ status);
988852 else
989
- printk(hdlc_unregister_ok);
853
+ pr_info("N_HDLC: line discipline unregistered\n");
990854 }
991855
992856 module_init(n_hdlc_init);
....@@ -994,6 +858,5 @@
994858
995859 MODULE_LICENSE("GPL");
996860 MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
997
-module_param(debuglevel, int, 0);
998861 module_param(maxframe, int, 0);
999862 MODULE_ALIAS_LDISC(N_HDLC);