.. | .. |
---|
18 | 18 | * All HDLC data is frame oriented which means: |
---|
19 | 19 | * |
---|
20 | 20 | * 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 |
---|
22 | 22 | * the frame (busy) in the write method. Each write call should have |
---|
23 | 23 | * a byte count in the range of 2-65535 bytes (2 is min HDLC frame |
---|
24 | 24 | * with 1 addr byte and 1 ctrl byte). The max byte count of 65535 |
---|
.. | .. |
---|
39 | 39 | * tty read calls. |
---|
40 | 40 | * |
---|
41 | 41 | * 3. tty read calls returns an entire frame of data or nothing. |
---|
42 | | - * |
---|
| 42 | + * |
---|
43 | 43 | * 4. all send and receive data is considered raw. No processing |
---|
44 | 44 | * or translation is performed by the line discipline, regardless |
---|
45 | 45 | * of the tty flags |
---|
.. | .. |
---|
87 | 87 | #include <linux/interrupt.h> |
---|
88 | 88 | #include <linux/ptrace.h> |
---|
89 | 89 | |
---|
90 | | -#undef VERSION |
---|
91 | | -#define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch)) |
---|
92 | | - |
---|
93 | 90 | #include <linux/poll.h> |
---|
94 | 91 | #include <linux/in.h> |
---|
95 | 92 | #include <linux/ioctl.h> |
---|
.. | .. |
---|
103 | 100 | |
---|
104 | 101 | #include <asm/termios.h> |
---|
105 | 102 | #include <linux/uaccess.h> |
---|
| 103 | +#include "tty.h" |
---|
106 | 104 | |
---|
107 | 105 | /* |
---|
108 | 106 | * Buffers for individual HDLC frames |
---|
109 | 107 | */ |
---|
110 | | -#define MAX_HDLC_FRAME_SIZE 65535 |
---|
| 108 | +#define MAX_HDLC_FRAME_SIZE 65535 |
---|
111 | 109 | #define DEFAULT_RX_BUF_COUNT 10 |
---|
112 | 110 | #define MAX_RX_BUF_COUNT 60 |
---|
113 | 111 | #define DEFAULT_TX_BUF_COUNT 3 |
---|
.. | .. |
---|
115 | 113 | struct n_hdlc_buf { |
---|
116 | 114 | struct list_head list_item; |
---|
117 | 115 | int count; |
---|
118 | | - char buf[1]; |
---|
| 116 | + char buf[]; |
---|
119 | 117 | }; |
---|
120 | | - |
---|
121 | | -#define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe) |
---|
122 | 118 | |
---|
123 | 119 | struct n_hdlc_buf_list { |
---|
124 | 120 | struct list_head list; |
---|
.. | .. |
---|
128 | 124 | |
---|
129 | 125 | /** |
---|
130 | 126 | * 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 |
---|
141 | 134 | */ |
---|
142 | 135 | struct n_hdlc { |
---|
143 | 136 | 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; |
---|
149 | 139 | struct n_hdlc_buf_list tx_buf_list; |
---|
150 | 140 | struct n_hdlc_buf_list rx_buf_list; |
---|
151 | 141 | struct n_hdlc_buf_list tx_free_buf_list; |
---|
152 | 142 | struct n_hdlc_buf_list rx_free_buf_list; |
---|
| 143 | + struct work_struct write_work; |
---|
| 144 | + struct tty_struct *tty_for_write_work; |
---|
153 | 145 | }; |
---|
154 | 146 | |
---|
155 | 147 | /* |
---|
.. | .. |
---|
163 | 155 | |
---|
164 | 156 | /* Local functions */ |
---|
165 | 157 | |
---|
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); |
---|
171 | 160 | |
---|
172 | 161 | /* max frame size for memory allocations */ |
---|
173 | 162 | static int maxframe = 4096; |
---|
174 | 163 | |
---|
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 | | - |
---|
196 | 164 | static void flush_rx_queue(struct tty_struct *tty) |
---|
197 | 165 | { |
---|
198 | | - struct n_hdlc *n_hdlc = tty2n_hdlc(tty); |
---|
| 166 | + struct n_hdlc *n_hdlc = tty->disc_data; |
---|
199 | 167 | struct n_hdlc_buf *buf; |
---|
200 | 168 | |
---|
201 | 169 | while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list))) |
---|
.. | .. |
---|
204 | 172 | |
---|
205 | 173 | static void flush_tx_queue(struct tty_struct *tty) |
---|
206 | 174 | { |
---|
207 | | - struct n_hdlc *n_hdlc = tty2n_hdlc(tty); |
---|
| 175 | + struct n_hdlc *n_hdlc = tty->disc_data; |
---|
208 | 176 | struct n_hdlc_buf *buf; |
---|
209 | 177 | |
---|
210 | 178 | while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list))) |
---|
211 | 179 | n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf); |
---|
212 | 180 | } |
---|
213 | 181 | |
---|
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) |
---|
234 | 183 | { |
---|
235 | | - struct tty_struct *tty = n_hdlc2tty (n_hdlc); |
---|
236 | 184 | 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); |
---|
244 | 185 | |
---|
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 | +} |
---|
280 | 191 | |
---|
281 | 192 | /** |
---|
282 | 193 | * n_hdlc_tty_close - line discipline close |
---|
283 | | - * @tty - pointer to tty info structure |
---|
| 194 | + * @tty: pointer to tty info structure |
---|
284 | 195 | * |
---|
285 | 196 | * Called when the line discipline is changed to something |
---|
286 | 197 | * else, the tty is closed, or the tty detects a hangup. |
---|
287 | 198 | */ |
---|
288 | 199 | static void n_hdlc_tty_close(struct tty_struct *tty) |
---|
289 | 200 | { |
---|
290 | | - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); |
---|
| 201 | + struct n_hdlc *n_hdlc = tty->disc_data; |
---|
291 | 202 | |
---|
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; |
---|
313 | 206 | } |
---|
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); |
---|
318 | 223 | } /* end of n_hdlc_tty_close() */ |
---|
319 | 224 | |
---|
320 | 225 | /** |
---|
321 | 226 | * 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 |
---|
323 | 228 | * |
---|
324 | 229 | * Returns 0 if success, otherwise error code |
---|
325 | 230 | */ |
---|
326 | | -static int n_hdlc_tty_open (struct tty_struct *tty) |
---|
| 231 | +static int n_hdlc_tty_open(struct tty_struct *tty) |
---|
327 | 232 | { |
---|
328 | | - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); |
---|
| 233 | + struct n_hdlc *n_hdlc = tty->disc_data; |
---|
329 | 234 | |
---|
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 | + |
---|
335 | 237 | /* There should not be an existing table for this slot. */ |
---|
336 | 238 | if (n_hdlc) { |
---|
337 | | - printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" ); |
---|
| 239 | + pr_err("%s: tty already associated!\n", __func__); |
---|
338 | 240 | return -EEXIST; |
---|
339 | 241 | } |
---|
340 | | - |
---|
| 242 | + |
---|
341 | 243 | n_hdlc = n_hdlc_alloc(); |
---|
342 | 244 | if (!n_hdlc) { |
---|
343 | | - printk (KERN_ERR "n_hdlc_alloc failed\n"); |
---|
| 245 | + pr_err("%s: n_hdlc_alloc failed\n", __func__); |
---|
344 | 246 | return -ENFILE; |
---|
345 | 247 | } |
---|
346 | | - |
---|
| 248 | + |
---|
| 249 | + INIT_WORK(&n_hdlc->write_work, n_hdlc_tty_write_work); |
---|
| 250 | + n_hdlc->tty_for_write_work = tty; |
---|
347 | 251 | tty->disc_data = n_hdlc; |
---|
348 | | - n_hdlc->tty = tty; |
---|
349 | 252 | tty->receive_room = 65536; |
---|
350 | | - |
---|
351 | | -#if defined(TTY_NO_WRITE_SPLIT) |
---|
| 253 | + |
---|
352 | 254 | /* 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 | + |
---|
356 | 257 | /* flush receive data from driver */ |
---|
357 | 258 | 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 | + |
---|
362 | 260 | return 0; |
---|
363 | | - |
---|
| 261 | + |
---|
364 | 262 | } /* end of n_tty_hdlc_open() */ |
---|
365 | 263 | |
---|
366 | 264 | /** |
---|
367 | 265 | * 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 |
---|
370 | 268 | * |
---|
371 | 269 | * Send frames on pending send buffer list until the driver does not accept a |
---|
372 | 270 | * frame (busy) this function is called after adding a frame to the send buffer |
---|
.. | .. |
---|
378 | 276 | unsigned long flags; |
---|
379 | 277 | struct n_hdlc_buf *tbuf; |
---|
380 | 278 | |
---|
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); |
---|
386 | 282 | 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); |
---|
389 | 285 | return; |
---|
390 | 286 | } |
---|
391 | | - n_hdlc->tbusy = 1; |
---|
392 | | - n_hdlc->woke_up = 0; |
---|
| 287 | + n_hdlc->tbusy = true; |
---|
| 288 | + n_hdlc->woke_up = false; |
---|
393 | 289 | spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); |
---|
394 | 290 | |
---|
395 | 291 | tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); |
---|
396 | 292 | 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 | + |
---|
401 | 295 | /* Send the next block of data to device */ |
---|
402 | 296 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
---|
403 | 297 | actual = tty->ops->write(tty, tbuf->buf, tbuf->count); |
---|
.. | .. |
---|
411 | 305 | /* pretending it was accepted by driver */ |
---|
412 | 306 | if (actual < 0) |
---|
413 | 307 | actual = tbuf->count; |
---|
414 | | - |
---|
| 308 | + |
---|
415 | 309 | 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 | + |
---|
420 | 312 | /* free current transmit buffer */ |
---|
421 | 313 | n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf); |
---|
422 | 314 | |
---|
423 | 315 | /* wait up sleeping writers */ |
---|
424 | 316 | wake_up_interruptible(&tty->write_wait); |
---|
425 | | - |
---|
| 317 | + |
---|
426 | 318 | /* get next pending transmit buffer */ |
---|
427 | 319 | tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); |
---|
428 | 320 | } 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); |
---|
432 | 322 | |
---|
433 | 323 | /* |
---|
434 | 324 | * the buffer was not accepted by driver, |
---|
.. | .. |
---|
438 | 328 | break; |
---|
439 | 329 | } |
---|
440 | 330 | } |
---|
441 | | - |
---|
| 331 | + |
---|
442 | 332 | if (!tbuf) |
---|
443 | 333 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
---|
444 | | - |
---|
| 334 | + |
---|
445 | 335 | /* Clear the re-entry flag */ |
---|
446 | 336 | 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); |
---|
452 | 339 | |
---|
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; |
---|
456 | 342 | } /* end of n_hdlc_send_frames() */ |
---|
457 | 343 | |
---|
458 | 344 | /** |
---|
| 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 | +/** |
---|
459 | 359 | * n_hdlc_tty_wakeup - Callback for transmit wakeup |
---|
460 | | - * @tty - pointer to associated tty instance data |
---|
| 360 | + * @tty: pointer to associated tty instance data |
---|
461 | 361 | * |
---|
462 | 362 | * Called when low level device driver can accept more send data. |
---|
463 | 363 | */ |
---|
464 | 364 | static void n_hdlc_tty_wakeup(struct tty_struct *tty) |
---|
465 | 365 | { |
---|
466 | | - struct n_hdlc *n_hdlc = tty2n_hdlc(tty); |
---|
| 366 | + struct n_hdlc *n_hdlc = tty->disc_data; |
---|
467 | 367 | |
---|
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); |
---|
481 | 369 | } /* end of n_hdlc_tty_wakeup() */ |
---|
482 | 370 | |
---|
483 | 371 | /** |
---|
484 | 372 | * 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 |
---|
489 | 377 | * |
---|
490 | 378 | * Called by tty low level driver when receive data is available. Data is |
---|
491 | 379 | * interpreted as one HDLC frame. |
---|
.. | .. |
---|
493 | 381 | static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data, |
---|
494 | 382 | char *flags, int count) |
---|
495 | 383 | { |
---|
496 | | - register struct n_hdlc *n_hdlc = tty2n_hdlc (tty); |
---|
| 384 | + register struct n_hdlc *n_hdlc = tty->disc_data; |
---|
497 | 385 | register struct n_hdlc_buf *buf; |
---|
498 | 386 | |
---|
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 | + |
---|
507 | 389 | /* verify line is using HDLC discipline */ |
---|
508 | 390 | 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"); |
---|
518 | 392 | return; |
---|
519 | 393 | } |
---|
520 | 394 | |
---|
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"); |
---|
534 | 397 | return; |
---|
535 | 398 | } |
---|
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 | + |
---|
537 | 417 | /* 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; |
---|
540 | 420 | |
---|
541 | 421 | /* add HDLC buffer to list of received frames */ |
---|
542 | 422 | n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf); |
---|
543 | | - |
---|
| 423 | + |
---|
544 | 424 | /* 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); |
---|
548 | 428 | |
---|
549 | 429 | } /* end of n_hdlc_tty_receive() */ |
---|
550 | 430 | |
---|
551 | 431 | /** |
---|
552 | 432 | * 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 | + * |
---|
558 | 438 | * Returns the number of bytes returned or error code. |
---|
559 | 439 | */ |
---|
560 | 440 | 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) |
---|
562 | 443 | { |
---|
563 | | - struct n_hdlc *n_hdlc = tty2n_hdlc(tty); |
---|
| 444 | + struct n_hdlc *n_hdlc = tty->disc_data; |
---|
564 | 445 | int ret = 0; |
---|
565 | 446 | struct n_hdlc_buf *rbuf; |
---|
566 | 447 | DECLARE_WAITQUEUE(wait, current); |
---|
567 | 448 | |
---|
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; |
---|
581 | 453 | |
---|
582 | 454 | add_wait_queue(&tty->read_wait, &wait); |
---|
583 | 455 | |
---|
.. | .. |
---|
592 | 464 | set_current_state(TASK_INTERRUPTIBLE); |
---|
593 | 465 | |
---|
594 | 466 | 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) |
---|
612 | 468 | break; |
---|
613 | | - } |
---|
614 | | - |
---|
| 469 | + |
---|
615 | 470 | /* no data */ |
---|
616 | 471 | if (tty_io_nonblock(tty, file)) { |
---|
617 | 472 | ret = -EAGAIN; |
---|
.. | .. |
---|
629 | 484 | remove_wait_queue(&tty->read_wait, &wait); |
---|
630 | 485 | __set_current_state(TASK_RUNNING); |
---|
631 | 486 | |
---|
| 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 | + |
---|
632 | 520 | return ret; |
---|
633 | | - |
---|
| 521 | + |
---|
634 | 522 | } /* end of n_hdlc_tty_read() */ |
---|
635 | 523 | |
---|
636 | 524 | /** |
---|
637 | 525 | * 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 | + * |
---|
643 | 531 | * Returns the number of bytes written (or error code). |
---|
644 | 532 | */ |
---|
645 | 533 | static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, |
---|
646 | 534 | const unsigned char *data, size_t count) |
---|
647 | 535 | { |
---|
648 | | - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); |
---|
| 536 | + struct n_hdlc *n_hdlc = tty->disc_data; |
---|
649 | 537 | int error = 0; |
---|
650 | 538 | DECLARE_WAITQUEUE(wait, current); |
---|
651 | 539 | struct n_hdlc_buf *tbuf; |
---|
652 | 540 | |
---|
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); |
---|
660 | 542 | |
---|
661 | 543 | if (n_hdlc->magic != HDLC_MAGIC) |
---|
662 | 544 | return -EIO; |
---|
663 | 545 | |
---|
664 | 546 | /* 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); |
---|
671 | 550 | count = maxframe; |
---|
672 | 551 | } |
---|
673 | | - |
---|
| 552 | + |
---|
674 | 553 | add_wait_queue(&tty->write_wait, &wait); |
---|
675 | 554 | |
---|
676 | 555 | for (;;) { |
---|
677 | 556 | set_current_state(TASK_INTERRUPTIBLE); |
---|
678 | | - |
---|
| 557 | + |
---|
679 | 558 | tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list); |
---|
680 | 559 | if (tbuf) |
---|
681 | 560 | break; |
---|
.. | .. |
---|
685 | 564 | break; |
---|
686 | 565 | } |
---|
687 | 566 | 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 | + |
---|
697 | 568 | if (signal_pending(current)) { |
---|
698 | 569 | error = -EINTR; |
---|
699 | 570 | break; |
---|
.. | .. |
---|
703 | 574 | __set_current_state(TASK_RUNNING); |
---|
704 | 575 | remove_wait_queue(&tty->write_wait, &wait); |
---|
705 | 576 | |
---|
706 | | - if (!error) { |
---|
| 577 | + if (!error) { |
---|
707 | 578 | /* Retrieve the user's buffer */ |
---|
708 | 579 | memcpy(tbuf->buf, data, count); |
---|
709 | 580 | |
---|
710 | 581 | /* Send the data */ |
---|
711 | 582 | 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); |
---|
714 | 585 | } |
---|
715 | 586 | |
---|
716 | 587 | return error; |
---|
717 | | - |
---|
| 588 | + |
---|
718 | 589 | } /* end of n_hdlc_tty_write() */ |
---|
719 | 590 | |
---|
720 | 591 | /** |
---|
721 | 592 | * 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) |
---|
726 | 597 | * |
---|
727 | 598 | * Returns command dependent result. |
---|
728 | 599 | */ |
---|
729 | 600 | static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file, |
---|
730 | 601 | unsigned int cmd, unsigned long arg) |
---|
731 | 602 | { |
---|
732 | | - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); |
---|
| 603 | + struct n_hdlc *n_hdlc = tty->disc_data; |
---|
733 | 604 | int error = 0; |
---|
734 | 605 | int count; |
---|
735 | 606 | unsigned long flags; |
---|
736 | 607 | struct n_hdlc_buf *buf = NULL; |
---|
737 | 608 | |
---|
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 | + |
---|
742 | 611 | /* Verify the status of the device */ |
---|
743 | | - if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC) |
---|
| 612 | + if (n_hdlc->magic != HDLC_MAGIC) |
---|
744 | 613 | return -EBADF; |
---|
745 | 614 | |
---|
746 | 615 | switch (cmd) { |
---|
747 | 616 | case FIONREAD: |
---|
748 | 617 | /* report count of read data available */ |
---|
749 | 618 | /* 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); |
---|
751 | 620 | buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list, |
---|
752 | 621 | struct n_hdlc_buf, list_item); |
---|
753 | 622 | if (buf) |
---|
754 | 623 | count = buf->count; |
---|
755 | 624 | else |
---|
756 | 625 | count = 0; |
---|
757 | | - spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags); |
---|
| 626 | + spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags); |
---|
758 | 627 | error = put_user(count, (int __user *)arg); |
---|
759 | 628 | break; |
---|
760 | 629 | |
---|
.. | .. |
---|
762 | 631 | /* get the pending tx byte count in the driver */ |
---|
763 | 632 | count = tty_chars_in_buffer(tty); |
---|
764 | 633 | /* 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); |
---|
766 | 635 | buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list, |
---|
767 | 636 | struct n_hdlc_buf, list_item); |
---|
768 | 637 | if (buf) |
---|
769 | 638 | 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); |
---|
771 | 640 | error = put_user(count, (int __user *)arg); |
---|
772 | 641 | break; |
---|
773 | 642 | |
---|
.. | .. |
---|
777 | 646 | case TCOFLUSH: |
---|
778 | 647 | flush_tx_queue(tty); |
---|
779 | 648 | } |
---|
780 | | - /* fall through to default */ |
---|
| 649 | + fallthrough; /* to default */ |
---|
781 | 650 | |
---|
782 | 651 | default: |
---|
783 | 652 | error = n_tty_ioctl_helper(tty, file, cmd, arg); |
---|
784 | 653 | break; |
---|
785 | 654 | } |
---|
786 | 655 | return error; |
---|
787 | | - |
---|
| 656 | + |
---|
788 | 657 | } /* end of n_hdlc_tty_ioctl() */ |
---|
789 | 658 | |
---|
790 | 659 | /** |
---|
791 | 660 | * 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 | + * |
---|
796 | 665 | * Determine which operations (read/write) will not block and return info |
---|
797 | 666 | * to caller. |
---|
798 | 667 | * Returns a bit mask containing info on which ops will not block. |
---|
.. | .. |
---|
800 | 669 | static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp, |
---|
801 | 670 | poll_table *wait) |
---|
802 | 671 | { |
---|
803 | | - struct n_hdlc *n_hdlc = tty2n_hdlc (tty); |
---|
| 672 | + struct n_hdlc *n_hdlc = tty->disc_data; |
---|
804 | 673 | __poll_t mask = 0; |
---|
805 | 674 | |
---|
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; |
---|
812 | 677 | |
---|
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); |
---|
815 | 684 | |
---|
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 | + |
---|
827 | 696 | return mask; |
---|
828 | 697 | } /* 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 | +} |
---|
829 | 715 | |
---|
830 | 716 | /** |
---|
831 | 717 | * n_hdlc_alloc - allocate an n_hdlc instance data structure |
---|
.. | .. |
---|
834 | 720 | */ |
---|
835 | 721 | static struct n_hdlc *n_hdlc_alloc(void) |
---|
836 | 722 | { |
---|
837 | | - struct n_hdlc_buf *buf; |
---|
838 | | - int i; |
---|
839 | 723 | struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL); |
---|
840 | 724 | |
---|
841 | 725 | if (!n_hdlc) |
---|
.. | .. |
---|
851 | 735 | INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list); |
---|
852 | 736 | INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list); |
---|
853 | 737 | |
---|
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 | + |
---|
872 | 741 | /* Initialize the control block */ |
---|
873 | 742 | n_hdlc->magic = HDLC_MAGIC; |
---|
874 | | - n_hdlc->flags = 0; |
---|
875 | | - |
---|
| 743 | + |
---|
876 | 744 | return n_hdlc; |
---|
877 | | - |
---|
| 745 | + |
---|
878 | 746 | } /* end of n_hdlc_alloc() */ |
---|
879 | 747 | |
---|
880 | 748 | /** |
---|
881 | 749 | * 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 |
---|
884 | 752 | */ |
---|
885 | 753 | static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list, |
---|
886 | 754 | struct n_hdlc_buf *buf) |
---|
.. | .. |
---|
897 | 765 | |
---|
898 | 766 | /** |
---|
899 | 767 | * 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 |
---|
902 | 770 | */ |
---|
903 | 771 | static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list, |
---|
904 | 772 | struct n_hdlc_buf *buf) |
---|
.. | .. |
---|
915 | 783 | |
---|
916 | 784 | /** |
---|
917 | 785 | * 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 | + * |
---|
920 | 788 | * Remove and return an HDLC buffer from the head of the specified HDLC buffer |
---|
921 | 789 | * list. |
---|
922 | 790 | * Returns a pointer to HDLC buffer if available, otherwise %NULL. |
---|
.. | .. |
---|
939 | 807 | return buf; |
---|
940 | 808 | } /* end of n_hdlc_buf_get() */ |
---|
941 | 809 | |
---|
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 | +}; |
---|
948 | 824 | |
---|
949 | 825 | static int __init n_hdlc_init(void) |
---|
950 | 826 | { |
---|
951 | 827 | int status; |
---|
952 | 828 | |
---|
953 | 829 | /* 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); |
---|
960 | 831 | |
---|
961 | 832 | status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc); |
---|
962 | 833 | if (!status) |
---|
963 | | - printk(hdlc_register_ok); |
---|
| 834 | + pr_info("N_HDLC line discipline registered with maxframe=%d\n", |
---|
| 835 | + maxframe); |
---|
964 | 836 | else |
---|
965 | | - printk(hdlc_register_fail, status); |
---|
| 837 | + pr_err("N_HDLC: error registering line discipline: %d\n", |
---|
| 838 | + status); |
---|
966 | 839 | |
---|
967 | 840 | return status; |
---|
968 | | - |
---|
| 841 | + |
---|
969 | 842 | } /* 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"; |
---|
980 | 843 | |
---|
981 | 844 | static void __exit n_hdlc_exit(void) |
---|
982 | 845 | { |
---|
.. | .. |
---|
984 | 847 | int status = tty_unregister_ldisc(N_HDLC); |
---|
985 | 848 | |
---|
986 | 849 | if (status) |
---|
987 | | - printk(hdlc_unregister_fail, status); |
---|
| 850 | + pr_err("N_HDLC: can't unregister line discipline (err = %d)\n", |
---|
| 851 | + status); |
---|
988 | 852 | else |
---|
989 | | - printk(hdlc_unregister_ok); |
---|
| 853 | + pr_info("N_HDLC: line discipline unregistered\n"); |
---|
990 | 854 | } |
---|
991 | 855 | |
---|
992 | 856 | module_init(n_hdlc_init); |
---|
.. | .. |
---|
994 | 858 | |
---|
995 | 859 | MODULE_LICENSE("GPL"); |
---|
996 | 860 | MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com"); |
---|
997 | | -module_param(debuglevel, int, 0); |
---|
998 | 861 | module_param(maxframe, int, 0); |
---|
999 | 862 | MODULE_ALIAS_LDISC(N_HDLC); |
---|