forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
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>
....@@ -107,7 +104,7 @@
107104 /*
108105 * Buffers for individual HDLC frames
109106 */
110
-#define MAX_HDLC_FRAME_SIZE 65535
107
+#define MAX_HDLC_FRAME_SIZE 65535
111108 #define DEFAULT_RX_BUF_COUNT 10
112109 #define MAX_RX_BUF_COUNT 60
113110 #define DEFAULT_TX_BUF_COUNT 3
....@@ -115,10 +112,8 @@
115112 struct n_hdlc_buf {
116113 struct list_head list_item;
117114 int count;
118
- char buf[1];
115
+ char buf[];
119116 };
120
-
121
-#define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
122117
123118 struct n_hdlc_buf_list {
124119 struct list_head list;
....@@ -128,28 +123,24 @@
128123
129124 /**
130125 * 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
126
+ * @magic: magic value for structure
127
+ * @tbusy: reentrancy flag for tx wakeup code
128
+ * @woke_up: tx wakeup needs to be run again as it was called while @tbusy
129
+ * @tx_buf_list: list of pending transmit frame buffers
130
+ * @rx_buf_list: list of received frame buffers
131
+ * @tx_free_buf_list: list unused transmit frame buffers
132
+ * @rx_free_buf_list: list unused received frame buffers
141133 */
142134 struct n_hdlc {
143135 int magic;
144
- __u32 flags;
145
- struct tty_struct *tty;
146
- struct tty_struct *backup_tty;
147
- int tbusy;
148
- int woke_up;
136
+ bool tbusy;
137
+ bool woke_up;
149138 struct n_hdlc_buf_list tx_buf_list;
150139 struct n_hdlc_buf_list rx_buf_list;
151140 struct n_hdlc_buf_list tx_free_buf_list;
152141 struct n_hdlc_buf_list rx_free_buf_list;
142
+ struct work_struct write_work;
143
+ struct tty_struct *tty_for_write_work;
153144 };
154145
155146 /*
....@@ -163,39 +154,15 @@
163154
164155 /* Local functions */
165156
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;
157
+static struct n_hdlc *n_hdlc_alloc(void);
158
+static void n_hdlc_tty_write_work(struct work_struct *work);
171159
172160 /* max frame size for memory allocations */
173161 static int maxframe = 4096;
174162
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
-
196163 static void flush_rx_queue(struct tty_struct *tty)
197164 {
198
- struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
165
+ struct n_hdlc *n_hdlc = tty->disc_data;
199166 struct n_hdlc_buf *buf;
200167
201168 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
....@@ -204,169 +171,99 @@
204171
205172 static void flush_tx_queue(struct tty_struct *tty)
206173 {
207
- struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
174
+ struct n_hdlc *n_hdlc = tty->disc_data;
208175 struct n_hdlc_buf *buf;
209176
210177 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
211178 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
212179 }
213180
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)
181
+static void n_hdlc_free_buf_list(struct n_hdlc_buf_list *list)
234182 {
235
- struct tty_struct *tty = n_hdlc2tty (n_hdlc);
236183 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);
244184
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() */
185
+ do {
186
+ buf = n_hdlc_buf_get(list);
187
+ kfree(buf);
188
+ } while (buf);
189
+}
280190
281191 /**
282192 * n_hdlc_tty_close - line discipline close
283
- * @tty - pointer to tty info structure
193
+ * @tty: pointer to tty info structure
284194 *
285195 * Called when the line discipline is changed to something
286196 * else, the tty is closed, or the tty detects a hangup.
287197 */
288198 static void n_hdlc_tty_close(struct tty_struct *tty)
289199 {
290
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
200
+ struct n_hdlc *n_hdlc = tty->disc_data;
291201
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
- }
202
+ if (n_hdlc->magic != HDLC_MAGIC) {
203
+ pr_warn("n_hdlc: trying to close unopened tty!\n");
204
+ return;
313205 }
314
-
315
- if (debuglevel >= DEBUG_LEVEL_INFO)
316
- printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
317
-
206
+#if defined(TTY_NO_WRITE_SPLIT)
207
+ clear_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
208
+#endif
209
+ tty->disc_data = NULL;
210
+
211
+ /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
212
+ wake_up_interruptible(&tty->read_wait);
213
+ wake_up_interruptible(&tty->write_wait);
214
+
215
+ cancel_work_sync(&n_hdlc->write_work);
216
+
217
+ n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list);
218
+ n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list);
219
+ n_hdlc_free_buf_list(&n_hdlc->rx_buf_list);
220
+ n_hdlc_free_buf_list(&n_hdlc->tx_buf_list);
221
+ kfree(n_hdlc);
318222 } /* end of n_hdlc_tty_close() */
319223
320224 /**
321225 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
322
- * @tty - pointer to tty info structure
226
+ * @tty: pointer to tty info structure
323227 *
324228 * Returns 0 if success, otherwise error code
325229 */
326
-static int n_hdlc_tty_open (struct tty_struct *tty)
230
+static int n_hdlc_tty_open(struct tty_struct *tty)
327231 {
328
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
232
+ struct n_hdlc *n_hdlc = tty->disc_data;
329233
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
-
234
+ pr_debug("%s() called (device=%s)\n", __func__, tty->name);
235
+
335236 /* There should not be an existing table for this slot. */
336237 if (n_hdlc) {
337
- printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
238
+ pr_err("%s: tty already associated!\n", __func__);
338239 return -EEXIST;
339240 }
340
-
241
+
341242 n_hdlc = n_hdlc_alloc();
342243 if (!n_hdlc) {
343
- printk (KERN_ERR "n_hdlc_alloc failed\n");
244
+ pr_err("%s: n_hdlc_alloc failed\n", __func__);
344245 return -ENFILE;
345246 }
346
-
247
+
248
+ INIT_WORK(&n_hdlc->write_work, n_hdlc_tty_write_work);
249
+ n_hdlc->tty_for_write_work = tty;
347250 tty->disc_data = n_hdlc;
348
- n_hdlc->tty = tty;
349251 tty->receive_room = 65536;
350
-
351
-#if defined(TTY_NO_WRITE_SPLIT)
252
+
352253 /* change tty_io write() to not split large writes into 8K chunks */
353
- set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
354
-#endif
355
-
254
+ set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
255
+
356256 /* flush receive data from driver */
357257 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
-
258
+
362259 return 0;
363
-
260
+
364261 } /* end of n_tty_hdlc_open() */
365262
366263 /**
367264 * 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
265
+ * @n_hdlc: pointer to ldisc instance data
266
+ * @tty: pointer to tty instance data
370267 *
371268 * Send frames on pending send buffer list until the driver does not accept a
372269 * frame (busy) this function is called after adding a frame to the send buffer
....@@ -378,26 +275,22 @@
378275 unsigned long flags;
379276 struct n_hdlc_buf *tbuf;
380277
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);
278
+check_again:
279
+
280
+ spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
386281 if (n_hdlc->tbusy) {
387
- n_hdlc->woke_up = 1;
388
- spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
282
+ n_hdlc->woke_up = true;
283
+ spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
389284 return;
390285 }
391
- n_hdlc->tbusy = 1;
392
- n_hdlc->woke_up = 0;
286
+ n_hdlc->tbusy = true;
287
+ n_hdlc->woke_up = false;
393288 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
394289
395290 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
396291 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
-
292
+ pr_debug("sending frame %p, count=%d\n", tbuf, tbuf->count);
293
+
401294 /* Send the next block of data to device */
402295 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
403296 actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
....@@ -411,24 +304,20 @@
411304 /* pretending it was accepted by driver */
412305 if (actual < 0)
413306 actual = tbuf->count;
414
-
307
+
415308 if (actual == tbuf->count) {
416
- if (debuglevel >= DEBUG_LEVEL_INFO)
417
- printk("%s(%d)frame %p completed\n",
418
- __FILE__,__LINE__,tbuf);
419
-
309
+ pr_debug("frame %p completed\n", tbuf);
310
+
420311 /* free current transmit buffer */
421312 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
422313
423314 /* wait up sleeping writers */
424315 wake_up_interruptible(&tty->write_wait);
425
-
316
+
426317 /* get next pending transmit buffer */
427318 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
428319 } else {
429
- if (debuglevel >= DEBUG_LEVEL_INFO)
430
- printk("%s(%d)frame %p pending\n",
431
- __FILE__,__LINE__,tbuf);
320
+ pr_debug("frame %p pending\n", tbuf);
432321
433322 /*
434323 * the buffer was not accepted by driver,
....@@ -438,54 +327,52 @@
438327 break;
439328 }
440329 }
441
-
330
+
442331 if (!tbuf)
443332 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
444
-
333
+
445334 /* Clear the re-entry flag */
446335 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;
336
+ n_hdlc->tbusy = false;
337
+ spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
452338
453
- if (debuglevel >= DEBUG_LEVEL_INFO)
454
- printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
455
-
339
+ if (n_hdlc->woke_up)
340
+ goto check_again;
456341 } /* end of n_hdlc_send_frames() */
457342
458343 /**
344
+ * n_hdlc_tty_write_work - Asynchronous callback for transmit wakeup
345
+ * @work: pointer to work_struct
346
+ *
347
+ * Called when low level device driver can accept more send data.
348
+ */
349
+static void n_hdlc_tty_write_work(struct work_struct *work)
350
+{
351
+ struct n_hdlc *n_hdlc = container_of(work, struct n_hdlc, write_work);
352
+ struct tty_struct *tty = n_hdlc->tty_for_write_work;
353
+
354
+ n_hdlc_send_frames(n_hdlc, tty);
355
+} /* end of n_hdlc_tty_write_work() */
356
+
357
+/**
459358 * n_hdlc_tty_wakeup - Callback for transmit wakeup
460
- * @tty - pointer to associated tty instance data
359
+ * @tty: pointer to associated tty instance data
461360 *
462361 * Called when low level device driver can accept more send data.
463362 */
464363 static void n_hdlc_tty_wakeup(struct tty_struct *tty)
465364 {
466
- struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
365
+ struct n_hdlc *n_hdlc = tty->disc_data;
467366
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
-
367
+ schedule_work(&n_hdlc->write_work);
481368 } /* end of n_hdlc_tty_wakeup() */
482369
483370 /**
484371 * 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
372
+ * @tty: pointer to tty instance data
373
+ * @data: pointer to received data
374
+ * @flags: pointer to flags for data
375
+ * @count: count of received data in bytes
489376 *
490377 * Called by tty low level driver when receive data is available. Data is
491378 * interpreted as one HDLC frame.
....@@ -493,91 +380,75 @@
493380 static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
494381 char *flags, int count)
495382 {
496
- register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
383
+ register struct n_hdlc *n_hdlc = tty->disc_data;
497384 register struct n_hdlc_buf *buf;
498385
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
-
386
+ pr_debug("%s() called count=%d\n", __func__, count);
387
+
507388 /* verify line is using HDLC discipline */
508389 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__);
390
+ pr_err("line not using HDLC discipline\n");
518391 return;
519392 }
520393
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__);
394
+ if (count > maxframe) {
395
+ pr_debug("rx count>maxframesize, data discarded\n");
534396 return;
535397 }
536
-
398
+
399
+ /* get a free HDLC buffer */
400
+ buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
401
+ if (!buf) {
402
+ /*
403
+ * no buffers in free list, attempt to allocate another rx
404
+ * buffer unless the maximum count has been reached
405
+ */
406
+ if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
407
+ buf = kmalloc(struct_size(buf, buf, maxframe),
408
+ GFP_ATOMIC);
409
+ }
410
+
411
+ if (!buf) {
412
+ pr_debug("no more rx buffers, data discarded\n");
413
+ return;
414
+ }
415
+
537416 /* copy received data to HDLC buffer */
538
- memcpy(buf->buf,data,count);
539
- buf->count=count;
417
+ memcpy(buf->buf, data, count);
418
+ buf->count = count;
540419
541420 /* add HDLC buffer to list of received frames */
542421 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
543
-
422
+
544423 /* 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);
424
+ wake_up_interruptible(&tty->read_wait);
425
+ if (tty->fasync != NULL)
426
+ kill_fasync(&tty->fasync, SIGIO, POLL_IN);
548427
549428 } /* end of n_hdlc_tty_receive() */
550429
551430 /**
552431 * 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
- *
432
+ * @tty: pointer to tty instance data
433
+ * @file: pointer to open file object
434
+ * @buf: pointer to returned data buffer
435
+ * @nr: size of returned data buffer
436
+ *
558437 * Returns the number of bytes returned or error code.
559438 */
560439 static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
561
- __u8 __user *buf, size_t nr)
440
+ __u8 *kbuf, size_t nr,
441
+ void **cookie, unsigned long offset)
562442 {
563
- struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
443
+ struct n_hdlc *n_hdlc = tty->disc_data;
564444 int ret = 0;
565445 struct n_hdlc_buf *rbuf;
566446 DECLARE_WAITQUEUE(wait, current);
567447
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
- }
448
+ /* Is this a repeated call for an rbuf we already found earlier? */
449
+ rbuf = *cookie;
450
+ if (rbuf)
451
+ goto have_rbuf;
581452
582453 add_wait_queue(&tty->read_wait, &wait);
583454
....@@ -592,26 +463,9 @@
592463 set_current_state(TASK_INTERRUPTIBLE);
593464
594465 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);
466
+ if (rbuf)
612467 break;
613
- }
614
-
468
+
615469 /* no data */
616470 if (tty_io_nonblock(tty, file)) {
617471 ret = -EAGAIN;
....@@ -629,53 +483,77 @@
629483 remove_wait_queue(&tty->read_wait, &wait);
630484 __set_current_state(TASK_RUNNING);
631485
486
+ if (!rbuf)
487
+ return ret;
488
+ *cookie = rbuf;
489
+
490
+have_rbuf:
491
+ /* Have we used it up entirely? */
492
+ if (offset >= rbuf->count)
493
+ goto done_with_rbuf;
494
+
495
+ /* More data to go, but can't copy any more? EOVERFLOW */
496
+ ret = -EOVERFLOW;
497
+ if (!nr)
498
+ goto done_with_rbuf;
499
+
500
+ /* Copy as much data as possible */
501
+ ret = rbuf->count - offset;
502
+ if (ret > nr)
503
+ ret = nr;
504
+ memcpy(kbuf, rbuf->buf+offset, ret);
505
+ offset += ret;
506
+
507
+ /* If we still have data left, we leave the rbuf in the cookie */
508
+ if (offset < rbuf->count)
509
+ return ret;
510
+
511
+done_with_rbuf:
512
+ *cookie = NULL;
513
+
514
+ if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
515
+ kfree(rbuf);
516
+ else
517
+ n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
518
+
632519 return ret;
633
-
520
+
634521 } /* end of n_hdlc_tty_read() */
635522
636523 /**
637524 * 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
- *
525
+ * @tty: pointer to associated tty device instance data
526
+ * @file: pointer to file object data
527
+ * @data: pointer to transmit data (one frame)
528
+ * @count: size of transmit frame in bytes
529
+ *
643530 * Returns the number of bytes written (or error code).
644531 */
645532 static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
646533 const unsigned char *data, size_t count)
647534 {
648
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
535
+ struct n_hdlc *n_hdlc = tty->disc_data;
649536 int error = 0;
650537 DECLARE_WAITQUEUE(wait, current);
651538 struct n_hdlc_buf *tbuf;
652539
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;
540
+ pr_debug("%s() called count=%zd\n", __func__, count);
660541
661542 if (n_hdlc->magic != HDLC_MAGIC)
662543 return -EIO;
663544
664545 /* 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 );
546
+ if (count > maxframe) {
547
+ pr_debug("%s: truncating user packet from %zu to %d\n",
548
+ __func__, count, maxframe);
671549 count = maxframe;
672550 }
673
-
551
+
674552 add_wait_queue(&tty->write_wait, &wait);
675553
676554 for (;;) {
677555 set_current_state(TASK_INTERRUPTIBLE);
678
-
556
+
679557 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
680558 if (tbuf)
681559 break;
....@@ -685,15 +563,7 @@
685563 break;
686564 }
687565 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
-
566
+
697567 if (signal_pending(current)) {
698568 error = -EINTR;
699569 break;
....@@ -703,58 +573,56 @@
703573 __set_current_state(TASK_RUNNING);
704574 remove_wait_queue(&tty->write_wait, &wait);
705575
706
- if (!error) {
576
+ if (!error) {
707577 /* Retrieve the user's buffer */
708578 memcpy(tbuf->buf, data, count);
709579
710580 /* Send the data */
711581 tbuf->count = error = count;
712
- n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
713
- n_hdlc_send_frames(n_hdlc,tty);
582
+ n_hdlc_buf_put(&n_hdlc->tx_buf_list, tbuf);
583
+ n_hdlc_send_frames(n_hdlc, tty);
714584 }
715585
716586 return error;
717
-
587
+
718588 } /* end of n_hdlc_tty_write() */
719589
720590 /**
721591 * 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)
592
+ * @tty: pointer to tty instance data
593
+ * @file: pointer to open file object for device
594
+ * @cmd: IOCTL command code
595
+ * @arg: argument for IOCTL call (cmd dependent)
726596 *
727597 * Returns command dependent result.
728598 */
729599 static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
730600 unsigned int cmd, unsigned long arg)
731601 {
732
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
602
+ struct n_hdlc *n_hdlc = tty->disc_data;
733603 int error = 0;
734604 int count;
735605 unsigned long flags;
736606 struct n_hdlc_buf *buf = NULL;
737607
738
- if (debuglevel >= DEBUG_LEVEL_INFO)
739
- printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
740
- __FILE__,__LINE__,cmd);
741
-
608
+ pr_debug("%s() called %d\n", __func__, cmd);
609
+
742610 /* Verify the status of the device */
743
- if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
611
+ if (n_hdlc->magic != HDLC_MAGIC)
744612 return -EBADF;
745613
746614 switch (cmd) {
747615 case FIONREAD:
748616 /* report count of read data available */
749617 /* in next available frame (if any) */
750
- spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
618
+ spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags);
751619 buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
752620 struct n_hdlc_buf, list_item);
753621 if (buf)
754622 count = buf->count;
755623 else
756624 count = 0;
757
- spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
625
+ spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags);
758626 error = put_user(count, (int __user *)arg);
759627 break;
760628
....@@ -762,12 +630,12 @@
762630 /* get the pending tx byte count in the driver */
763631 count = tty_chars_in_buffer(tty);
764632 /* add size of next output frame in queue */
765
- spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
633
+ spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
766634 buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
767635 struct n_hdlc_buf, list_item);
768636 if (buf)
769637 count += buf->count;
770
- spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
638
+ spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
771639 error = put_user(count, (int __user *)arg);
772640 break;
773641
....@@ -777,22 +645,22 @@
777645 case TCOFLUSH:
778646 flush_tx_queue(tty);
779647 }
780
- /* fall through to default */
648
+ fallthrough; /* to default */
781649
782650 default:
783651 error = n_tty_ioctl_helper(tty, file, cmd, arg);
784652 break;
785653 }
786654 return error;
787
-
655
+
788656 } /* end of n_hdlc_tty_ioctl() */
789657
790658 /**
791659 * 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
- *
660
+ * @tty: pointer to tty instance data
661
+ * @filp: pointer to open file object for device
662
+ * @wait: wait queue for operations
663
+ *
796664 * Determine which operations (read/write) will not block and return info
797665 * to caller.
798666 * Returns a bit mask containing info on which ops will not block.
....@@ -800,32 +668,49 @@
800668 static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
801669 poll_table *wait)
802670 {
803
- struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
671
+ struct n_hdlc *n_hdlc = tty->disc_data;
804672 __poll_t mask = 0;
805673
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) */
674
+ if (n_hdlc->magic != HDLC_MAGIC)
675
+ return 0;
812676
813
- poll_wait(filp, &tty->read_wait, wait);
814
- poll_wait(filp, &tty->write_wait, wait);
677
+ /*
678
+ * queue the current process into any wait queue that may awaken in the
679
+ * future (read and write)
680
+ */
681
+ poll_wait(filp, &tty->read_wait, wait);
682
+ poll_wait(filp, &tty->write_wait, wait);
815683
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
- }
684
+ /* set bits for operations that won't block */
685
+ if (!list_empty(&n_hdlc->rx_buf_list.list))
686
+ mask |= EPOLLIN | EPOLLRDNORM; /* readable */
687
+ if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
688
+ mask |= EPOLLHUP;
689
+ if (tty_hung_up_p(filp))
690
+ mask |= EPOLLHUP;
691
+ if (!tty_is_writelocked(tty) &&
692
+ !list_empty(&n_hdlc->tx_free_buf_list.list))
693
+ mask |= EPOLLOUT | EPOLLWRNORM; /* writable */
694
+
827695 return mask;
828696 } /* end of n_hdlc_tty_poll() */
697
+
698
+static void n_hdlc_alloc_buf(struct n_hdlc_buf_list *list, unsigned int count,
699
+ const char *name)
700
+{
701
+ struct n_hdlc_buf *buf;
702
+ unsigned int i;
703
+
704
+ for (i = 0; i < count; i++) {
705
+ buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL);
706
+ if (!buf) {
707
+ pr_debug("%s(), kmalloc() failed for %s buffer %u\n",
708
+ __func__, name, i);
709
+ return;
710
+ }
711
+ n_hdlc_buf_put(list, buf);
712
+ }
713
+}
829714
830715 /**
831716 * n_hdlc_alloc - allocate an n_hdlc instance data structure
....@@ -834,8 +719,6 @@
834719 */
835720 static struct n_hdlc *n_hdlc_alloc(void)
836721 {
837
- struct n_hdlc_buf *buf;
838
- int i;
839722 struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
840723
841724 if (!n_hdlc)
....@@ -851,36 +734,20 @@
851734 INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
852735 INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
853736
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
-
737
+ n_hdlc_alloc_buf(&n_hdlc->rx_free_buf_list, DEFAULT_RX_BUF_COUNT, "rx");
738
+ n_hdlc_alloc_buf(&n_hdlc->tx_free_buf_list, DEFAULT_TX_BUF_COUNT, "tx");
739
+
872740 /* Initialize the control block */
873741 n_hdlc->magic = HDLC_MAGIC;
874
- n_hdlc->flags = 0;
875
-
742
+
876743 return n_hdlc;
877
-
744
+
878745 } /* end of n_hdlc_alloc() */
879746
880747 /**
881748 * 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
749
+ * @buf_list: pointer to the buffer list
750
+ * @buf: pointer to the buffer
884751 */
885752 static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
886753 struct n_hdlc_buf *buf)
....@@ -897,8 +764,8 @@
897764
898765 /**
899766 * 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
767
+ * @buf_list: pointer to buffer list
768
+ * @buf: pointer to buffer
902769 */
903770 static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
904771 struct n_hdlc_buf *buf)
....@@ -915,8 +782,8 @@
915782
916783 /**
917784 * n_hdlc_buf_get - remove and return an HDLC buffer from list
918
- * @buf_list - pointer to HDLC buffer list
919
- *
785
+ * @buf_list: pointer to HDLC buffer list
786
+ *
920787 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
921788 * list.
922789 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
....@@ -939,44 +806,39 @@
939806 return buf;
940807 } /* end of n_hdlc_buf_get() */
941808
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";
809
+static struct tty_ldisc_ops n_hdlc_ldisc = {
810
+ .owner = THIS_MODULE,
811
+ .magic = TTY_LDISC_MAGIC,
812
+ .name = "hdlc",
813
+ .open = n_hdlc_tty_open,
814
+ .close = n_hdlc_tty_close,
815
+ .read = n_hdlc_tty_read,
816
+ .write = n_hdlc_tty_write,
817
+ .ioctl = n_hdlc_tty_ioctl,
818
+ .poll = n_hdlc_tty_poll,
819
+ .receive_buf = n_hdlc_tty_receive,
820
+ .write_wakeup = n_hdlc_tty_wakeup,
821
+ .flush_buffer = flush_rx_queue,
822
+};
948823
949824 static int __init n_hdlc_init(void)
950825 {
951826 int status;
952827
953828 /* 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);
829
+ maxframe = clamp(maxframe, 4096, MAX_HDLC_FRAME_SIZE);
960830
961831 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
962832 if (!status)
963
- printk(hdlc_register_ok);
833
+ pr_info("N_HDLC line discipline registered with maxframe=%d\n",
834
+ maxframe);
964835 else
965
- printk(hdlc_register_fail, status);
836
+ pr_err("N_HDLC: error registering line discipline: %d\n",
837
+ status);
966838
967839 return status;
968
-
840
+
969841 } /* 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";
980842
981843 static void __exit n_hdlc_exit(void)
982844 {
....@@ -984,9 +846,10 @@
984846 int status = tty_unregister_ldisc(N_HDLC);
985847
986848 if (status)
987
- printk(hdlc_unregister_fail, status);
849
+ pr_err("N_HDLC: can't unregister line discipline (err = %d)\n",
850
+ status);
988851 else
989
- printk(hdlc_unregister_ok);
852
+ pr_info("N_HDLC: line discipline unregistered\n");
990853 }
991854
992855 module_init(n_hdlc_init);
....@@ -994,6 +857,5 @@
994857
995858 MODULE_LICENSE("GPL");
996859 MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
997
-module_param(debuglevel, int, 0);
998860 module_param(maxframe, int, 0);
999861 MODULE_ALIAS_LDISC(N_HDLC);