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