Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[PATCH] Char: n_r3964, cleanup

- Lindent the code
- allow semicolons after macros by 'do {} while (0)'
- eliminate C++ comments

Signed-off-by: Jiri Slaby <jirislaby@gmail.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Jiri Slaby and committed by
Linus Torvalds
5e07e1cc 44bafdf3

+914 -1049
+914 -1049
drivers/char/n_r3964.c
··· 60 60 #include <linux/slab.h> 61 61 #include <linux/tty.h> 62 62 #include <linux/errno.h> 63 - #include <linux/string.h> /* used in new tty drivers */ 64 - #include <linux/signal.h> /* used in new tty drivers */ 63 + #include <linux/string.h> /* used in new tty drivers */ 64 + #include <linux/signal.h> /* used in new tty drivers */ 65 65 #include <linux/ioctl.h> 66 66 #include <linux/n_r3964.h> 67 67 #include <linux/poll.h> 68 68 #include <linux/init.h> 69 69 #include <asm/uaccess.h> 70 70 71 - 72 - //#define DEBUG_QUEUE 71 + /*#define DEBUG_QUEUE*/ 73 72 74 73 /* Log successful handshake and protocol operations */ 75 - //#define DEBUG_PROTO_S 74 + /*#define DEBUG_PROTO_S*/ 76 75 77 76 /* Log handshake and protocol errors: */ 78 - //#define DEBUG_PROTO_E 77 + /*#define DEBUG_PROTO_E*/ 79 78 80 79 /* Log Linediscipline operations (open, close, read, write...): */ 81 - //#define DEBUG_LDISC 80 + /*#define DEBUG_LDISC*/ 82 81 83 82 /* Log module and memory operations (init, cleanup; kmalloc, kfree): */ 84 - //#define DEBUG_MODUL 83 + /*#define DEBUG_MODUL*/ 85 84 86 85 /* Macro helpers for debug output: */ 87 - #define TRACE(format, args...) printk("r3964: " format "\n" , ## args); 86 + #define TRACE(format, args...) printk("r3964: " format "\n" , ## args) 88 87 89 88 #ifdef DEBUG_MODUL 90 - #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args); 89 + #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args) 91 90 #else 92 - #define TRACE_M(fmt, arg...) /**/ 91 + #define TRACE_M(fmt, arg...) do {} while (0) 93 92 #endif 94 - 95 93 #ifdef DEBUG_PROTO_S 96 - #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args); 94 + #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args) 97 95 #else 98 - #define TRACE_PS(fmt, arg...) /**/ 96 + #define TRACE_PS(fmt, arg...) do {} while (0) 99 97 #endif 100 - 101 98 #ifdef DEBUG_PROTO_E 102 - #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args); 99 + #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args) 103 100 #else 104 - #define TRACE_PE(fmt, arg...) /**/ 101 + #define TRACE_PE(fmt, arg...) do {} while (0) 105 102 #endif 106 - 107 103 #ifdef DEBUG_LDISC 108 - #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args); 104 + #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args) 109 105 #else 110 - #define TRACE_L(fmt, arg...) /**/ 106 + #define TRACE_L(fmt, arg...) do {} while (0) 111 107 #endif 112 - 113 108 #ifdef DEBUG_QUEUE 114 - #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args); 109 + #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args) 115 110 #else 116 - #define TRACE_Q(fmt, arg...) /**/ 111 + #define TRACE_Q(fmt, arg...) do {} while (0) 117 112 #endif 118 - 119 113 static void add_tx_queue(struct r3964_info *, struct r3964_block_header *); 120 114 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code); 121 115 static void put_char(struct r3964_info *pInfo, unsigned char ch); ··· 120 126 static void receive_error(struct r3964_info *pInfo, const char flag); 121 127 static void on_timeout(unsigned long priv); 122 128 static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg); 123 - static int read_telegram(struct r3964_info *pInfo, struct pid *pid, unsigned char __user *buf); 129 + static int read_telegram(struct r3964_info *pInfo, struct pid *pid, 130 + unsigned char __user * buf); 124 131 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg, 125 - int error_code, struct r3964_block_header *pBlock); 126 - static struct r3964_message* remove_msg(struct r3964_info *pInfo, 127 - struct r3964_client_info *pClient); 128 - static void remove_client_block(struct r3964_info *pInfo, 129 - struct r3964_client_info *pClient); 132 + int error_code, struct r3964_block_header *pBlock); 133 + static struct r3964_message *remove_msg(struct r3964_info *pInfo, 134 + struct r3964_client_info *pClient); 135 + static void remove_client_block(struct r3964_info *pInfo, 136 + struct r3964_client_info *pClient); 130 137 131 - static int r3964_open(struct tty_struct *tty); 138 + static int r3964_open(struct tty_struct *tty); 132 139 static void r3964_close(struct tty_struct *tty); 133 140 static ssize_t r3964_read(struct tty_struct *tty, struct file *file, 134 - unsigned char __user *buf, size_t nr); 135 - static ssize_t r3964_write(struct tty_struct * tty, struct file * file, 136 - const unsigned char * buf, size_t nr); 137 - static int r3964_ioctl(struct tty_struct * tty, struct file * file, 138 - unsigned int cmd, unsigned long arg); 139 - static void r3964_set_termios(struct tty_struct *tty, struct ktermios * old); 140 - static unsigned int r3964_poll(struct tty_struct * tty, struct file * file, 141 - struct poll_table_struct *wait); 141 + unsigned char __user * buf, size_t nr); 142 + static ssize_t r3964_write(struct tty_struct *tty, struct file *file, 143 + const unsigned char *buf, size_t nr); 144 + static int r3964_ioctl(struct tty_struct *tty, struct file *file, 145 + unsigned int cmd, unsigned long arg); 146 + static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old); 147 + static unsigned int r3964_poll(struct tty_struct *tty, struct file *file, 148 + struct poll_table_struct *wait); 142 149 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp, 143 - char *fp, int count); 150 + char *fp, int count); 144 151 145 152 static struct tty_ldisc tty_ldisc_N_R3964 = { 146 - .owner = THIS_MODULE, 147 - .magic = TTY_LDISC_MAGIC, 148 - .name = "R3964", 149 - .open = r3964_open, 150 - .close = r3964_close, 151 - .read = r3964_read, 152 - .write = r3964_write, 153 - .ioctl = r3964_ioctl, 153 + .owner = THIS_MODULE, 154 + .magic = TTY_LDISC_MAGIC, 155 + .name = "R3964", 156 + .open = r3964_open, 157 + .close = r3964_close, 158 + .read = r3964_read, 159 + .write = r3964_write, 160 + .ioctl = r3964_ioctl, 154 161 .set_termios = r3964_set_termios, 155 - .poll = r3964_poll, 162 + .poll = r3964_poll, 156 163 .receive_buf = r3964_receive_buf, 157 164 }; 158 165 159 - 160 - 161 166 static void dump_block(const unsigned char *block, unsigned int length) 162 167 { 163 - unsigned int i,j; 164 - char linebuf[16*3+1]; 165 - 166 - for(i=0;i<length;i+=16) 167 - { 168 - for(j=0;(j<16) && (j+i<length);j++) 169 - { 170 - sprintf(linebuf+3*j,"%02x ",block[i+j]); 171 - } 172 - linebuf[3*j]='\0'; 173 - TRACE_PS("%s",linebuf); 174 - } 168 + unsigned int i, j; 169 + char linebuf[16 * 3 + 1]; 170 + 171 + for (i = 0; i < length; i += 16) { 172 + for (j = 0; (j < 16) && (j + i < length); j++) { 173 + sprintf(linebuf + 3 * j, "%02x ", block[i + j]); 174 + } 175 + linebuf[3 * j] = '\0'; 176 + TRACE_PS("%s", linebuf); 177 + } 175 178 } 176 - 177 - 178 - 179 179 180 180 /************************************************************* 181 181 * Driver initialisation 182 182 *************************************************************/ 183 - 184 183 185 184 /************************************************************* 186 185 * Module support routines ··· 181 194 182 195 static void __exit r3964_exit(void) 183 196 { 184 - int status; 185 - 186 - TRACE_M ("cleanup_module()"); 197 + int status; 187 198 188 - status=tty_unregister_ldisc(N_R3964); 189 - 190 - if(status!=0) 191 - { 192 - printk(KERN_ERR "r3964: error unregistering linediscipline: %d\n", status); 193 - } 194 - else 195 - { 196 - TRACE_L("linediscipline successfully unregistered"); 197 - } 198 - 199 + TRACE_M("cleanup_module()"); 200 + 201 + status = tty_unregister_ldisc(N_R3964); 202 + 203 + if (status != 0) { 204 + printk(KERN_ERR "r3964: error unregistering linediscipline: " 205 + "%d\n", status); 206 + } else { 207 + TRACE_L("linediscipline successfully unregistered"); 208 + } 199 209 } 200 210 201 211 static int __init r3964_init(void) 202 212 { 203 - int status; 204 - 205 - printk ("r3964: Philips r3964 Driver $Revision: 1.10 $\n"); 213 + int status; 206 214 207 - /* 208 - * Register the tty line discipline 209 - */ 210 - 211 - status = tty_register_ldisc (N_R3964, &tty_ldisc_N_R3964); 212 - if (status == 0) 213 - { 214 - TRACE_L("line discipline %d registered", N_R3964); 215 - TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags, 216 - tty_ldisc_N_R3964.num); 217 - TRACE_L("open=%p", tty_ldisc_N_R3964.open); 218 - TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964); 219 - } 220 - else 221 - { 222 - printk (KERN_ERR "r3964: error registering line discipline: %d\n", status); 223 - } 224 - return status; 215 + printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n"); 216 + 217 + /* 218 + * Register the tty line discipline 219 + */ 220 + 221 + status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964); 222 + if (status == 0) { 223 + TRACE_L("line discipline %d registered", N_R3964); 224 + TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags, 225 + tty_ldisc_N_R3964.num); 226 + TRACE_L("open=%p", tty_ldisc_N_R3964.open); 227 + TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964); 228 + } else { 229 + printk(KERN_ERR "r3964: error registering line discipline: " 230 + "%d\n", status); 231 + } 232 + return status; 225 233 } 226 234 227 235 module_init(r3964_init); 228 236 module_exit(r3964_exit); 229 237 230 - 231 238 /************************************************************* 232 239 * Protocol implementation routines 233 240 *************************************************************/ 234 241 235 - static void add_tx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader) 242 + static void add_tx_queue(struct r3964_info *pInfo, 243 + struct r3964_block_header *pHeader) 236 244 { 237 - unsigned long flags; 238 - 239 - spin_lock_irqsave(&pInfo->lock, flags); 245 + unsigned long flags; 240 246 241 - pHeader->next = NULL; 247 + spin_lock_irqsave(&pInfo->lock, flags); 242 248 243 - if(pInfo->tx_last == NULL) 244 - { 245 - pInfo->tx_first = pInfo->tx_last = pHeader; 246 - } 247 - else 248 - { 249 - pInfo->tx_last->next = pHeader; 250 - pInfo->tx_last = pHeader; 251 - } 252 - 253 - spin_unlock_irqrestore(&pInfo->lock, flags); 249 + pHeader->next = NULL; 254 250 255 - TRACE_Q("add_tx_queue %p, length %d, tx_first = %p", 256 - pHeader, pHeader->length, pInfo->tx_first ); 251 + if (pInfo->tx_last == NULL) { 252 + pInfo->tx_first = pInfo->tx_last = pHeader; 253 + } else { 254 + pInfo->tx_last->next = pHeader; 255 + pInfo->tx_last = pHeader; 256 + } 257 + 258 + spin_unlock_irqrestore(&pInfo->lock, flags); 259 + 260 + TRACE_Q("add_tx_queue %p, length %d, tx_first = %p", 261 + pHeader, pHeader->length, pInfo->tx_first); 257 262 } 258 263 259 264 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code) 260 265 { 261 - struct r3964_block_header *pHeader; 262 - unsigned long flags; 266 + struct r3964_block_header *pHeader; 267 + unsigned long flags; 263 268 #ifdef DEBUG_QUEUE 264 - struct r3964_block_header *pDump; 265 - #endif 266 - 267 - pHeader = pInfo->tx_first; 268 - 269 - if(pHeader==NULL) 270 - return; 271 - 272 - #ifdef DEBUG_QUEUE 273 - printk("r3964: remove_from_tx_queue: %p, length %u - ", 274 - pHeader, pHeader->length ); 275 - for(pDump=pHeader;pDump;pDump=pDump->next) 276 - printk("%p ", pDump); 277 - printk("\n"); 269 + struct r3964_block_header *pDump; 278 270 #endif 279 271 272 + pHeader = pInfo->tx_first; 280 273 281 - if(pHeader->owner) 282 - { 283 - if(error_code) 284 - { 285 - add_msg(pHeader->owner, R3964_MSG_ACK, 0, 286 - error_code, NULL); 287 - } 288 - else 289 - { 290 - add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length, 291 - error_code, NULL); 292 - } 293 - wake_up_interruptible (&pInfo->read_wait); 294 - } 274 + if (pHeader == NULL) 275 + return; 295 276 296 - spin_lock_irqsave(&pInfo->lock, flags); 277 + #ifdef DEBUG_QUEUE 278 + printk("r3964: remove_from_tx_queue: %p, length %u - ", 279 + pHeader, pHeader->length); 280 + for (pDump = pHeader; pDump; pDump = pDump->next) 281 + printk("%p ", pDump); 282 + printk("\n"); 283 + #endif 297 284 298 - pInfo->tx_first = pHeader->next; 299 - if(pInfo->tx_first==NULL) 300 - { 301 - pInfo->tx_last = NULL; 302 - } 285 + if (pHeader->owner) { 286 + if (error_code) { 287 + add_msg(pHeader->owner, R3964_MSG_ACK, 0, 288 + error_code, NULL); 289 + } else { 290 + add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length, 291 + error_code, NULL); 292 + } 293 + wake_up_interruptible(&pInfo->read_wait); 294 + } 303 295 304 - spin_unlock_irqrestore(&pInfo->lock, flags); 296 + spin_lock_irqsave(&pInfo->lock, flags); 305 297 306 - kfree(pHeader); 307 - TRACE_M("remove_from_tx_queue - kfree %p",pHeader); 298 + pInfo->tx_first = pHeader->next; 299 + if (pInfo->tx_first == NULL) { 300 + pInfo->tx_last = NULL; 301 + } 308 302 309 - TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p", 310 - pInfo->tx_first, pInfo->tx_last ); 303 + spin_unlock_irqrestore(&pInfo->lock, flags); 304 + 305 + kfree(pHeader); 306 + TRACE_M("remove_from_tx_queue - kfree %p", pHeader); 307 + 308 + TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p", 309 + pInfo->tx_first, pInfo->tx_last); 311 310 } 312 311 313 - static void add_rx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader) 312 + static void add_rx_queue(struct r3964_info *pInfo, 313 + struct r3964_block_header *pHeader) 314 314 { 315 - unsigned long flags; 316 - 317 - spin_lock_irqsave(&pInfo->lock, flags); 315 + unsigned long flags; 318 316 319 - pHeader->next = NULL; 317 + spin_lock_irqsave(&pInfo->lock, flags); 320 318 321 - if(pInfo->rx_last == NULL) 322 - { 323 - pInfo->rx_first = pInfo->rx_last = pHeader; 324 - } 325 - else 326 - { 327 - pInfo->rx_last->next = pHeader; 328 - pInfo->rx_last = pHeader; 329 - } 330 - pInfo->blocks_in_rx_queue++; 331 - 332 - spin_unlock_irqrestore(&pInfo->lock, flags); 319 + pHeader->next = NULL; 333 320 334 - TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d", 335 - pHeader, pHeader->length, 336 - pInfo->rx_first, pInfo->blocks_in_rx_queue); 321 + if (pInfo->rx_last == NULL) { 322 + pInfo->rx_first = pInfo->rx_last = pHeader; 323 + } else { 324 + pInfo->rx_last->next = pHeader; 325 + pInfo->rx_last = pHeader; 326 + } 327 + pInfo->blocks_in_rx_queue++; 328 + 329 + spin_unlock_irqrestore(&pInfo->lock, flags); 330 + 331 + TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d", 332 + pHeader, pHeader->length, 333 + pInfo->rx_first, pInfo->blocks_in_rx_queue); 337 334 } 338 335 339 336 static void remove_from_rx_queue(struct r3964_info *pInfo, 340 - struct r3964_block_header *pHeader) 337 + struct r3964_block_header *pHeader) 341 338 { 342 - unsigned long flags; 343 - struct r3964_block_header *pFind; 344 - 345 - if(pHeader==NULL) 346 - return; 339 + unsigned long flags; 340 + struct r3964_block_header *pFind; 347 341 348 - TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d", 349 - pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue ); 350 - TRACE_Q("remove_from_rx_queue: %p, length %u", 351 - pHeader, pHeader->length ); 342 + if (pHeader == NULL) 343 + return; 352 344 353 - spin_lock_irqsave(&pInfo->lock, flags); 345 + TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d", 346 + pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue); 347 + TRACE_Q("remove_from_rx_queue: %p, length %u", 348 + pHeader, pHeader->length); 354 349 355 - if(pInfo->rx_first == pHeader) 356 - { 357 - /* Remove the first block in the linked list: */ 358 - pInfo->rx_first = pHeader->next; 359 - 360 - if(pInfo->rx_first==NULL) 361 - { 362 - pInfo->rx_last = NULL; 363 - } 364 - pInfo->blocks_in_rx_queue--; 365 - } 366 - else 367 - { 368 - /* Find block to remove: */ 369 - for(pFind=pInfo->rx_first; pFind; pFind=pFind->next) 370 - { 371 - if(pFind->next == pHeader) 372 - { 373 - /* Got it. */ 374 - pFind->next = pHeader->next; 375 - pInfo->blocks_in_rx_queue--; 376 - if(pFind->next==NULL) 377 - { 378 - /* Oh, removed the last one! */ 379 - pInfo->rx_last = pFind; 380 - } 381 - break; 382 - } 383 - } 384 - } 350 + spin_lock_irqsave(&pInfo->lock, flags); 385 351 386 - spin_unlock_irqrestore(&pInfo->lock, flags); 352 + if (pInfo->rx_first == pHeader) { 353 + /* Remove the first block in the linked list: */ 354 + pInfo->rx_first = pHeader->next; 387 355 388 - kfree(pHeader); 389 - TRACE_M("remove_from_rx_queue - kfree %p",pHeader); 356 + if (pInfo->rx_first == NULL) { 357 + pInfo->rx_last = NULL; 358 + } 359 + pInfo->blocks_in_rx_queue--; 360 + } else { 361 + /* Find block to remove: */ 362 + for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) { 363 + if (pFind->next == pHeader) { 364 + /* Got it. */ 365 + pFind->next = pHeader->next; 366 + pInfo->blocks_in_rx_queue--; 367 + if (pFind->next == NULL) { 368 + /* Oh, removed the last one! */ 369 + pInfo->rx_last = pFind; 370 + } 371 + break; 372 + } 373 + } 374 + } 390 375 391 - TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d", 392 - pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue ); 376 + spin_unlock_irqrestore(&pInfo->lock, flags); 377 + 378 + kfree(pHeader); 379 + TRACE_M("remove_from_rx_queue - kfree %p", pHeader); 380 + 381 + TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d", 382 + pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue); 393 383 } 394 384 395 385 static void put_char(struct r3964_info *pInfo, unsigned char ch) 396 386 { 397 - struct tty_struct *tty = pInfo->tty; 387 + struct tty_struct *tty = pInfo->tty; 398 388 399 - if(tty==NULL) 400 - return; 389 + if (tty == NULL) 390 + return; 401 391 402 - if(tty->driver->put_char) 403 - { 404 - tty->driver->put_char(tty, ch); 405 - } 406 - pInfo->bcc ^= ch; 392 + if (tty->driver->put_char) { 393 + tty->driver->put_char(tty, ch); 394 + } 395 + pInfo->bcc ^= ch; 407 396 } 408 397 409 398 static void flush(struct r3964_info *pInfo) 410 399 { 411 - struct tty_struct *tty = pInfo->tty; 400 + struct tty_struct *tty = pInfo->tty; 412 401 413 - if(tty==NULL) 414 - return; 402 + if (tty == NULL) 403 + return; 415 404 416 - if(tty->driver->flush_chars) 417 - { 418 - tty->driver->flush_chars(tty); 419 - } 405 + if (tty->driver->flush_chars) { 406 + tty->driver->flush_chars(tty); 407 + } 420 408 } 421 409 422 410 static void trigger_transmit(struct r3964_info *pInfo) 423 411 { 424 - unsigned long flags; 425 - 412 + unsigned long flags; 426 413 427 - spin_lock_irqsave(&pInfo->lock, flags); 414 + spin_lock_irqsave(&pInfo->lock, flags); 428 415 429 - if((pInfo->state == R3964_IDLE) && (pInfo->tx_first!=NULL)) 430 - { 431 - pInfo->state = R3964_TX_REQUEST; 432 - pInfo->nRetry=0; 433 - pInfo->flags &= ~R3964_ERROR; 434 - mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); 416 + if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) { 417 + pInfo->state = R3964_TX_REQUEST; 418 + pInfo->nRetry = 0; 419 + pInfo->flags &= ~R3964_ERROR; 420 + mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); 435 421 436 - spin_unlock_irqrestore(&pInfo->lock, flags); 422 + spin_unlock_irqrestore(&pInfo->lock, flags); 437 423 438 - TRACE_PS("trigger_transmit - sent STX"); 424 + TRACE_PS("trigger_transmit - sent STX"); 439 425 440 - put_char(pInfo, STX); 441 - flush(pInfo); 426 + put_char(pInfo, STX); 427 + flush(pInfo); 442 428 443 - pInfo->bcc = 0; 444 - } 445 - else 446 - { 447 - spin_unlock_irqrestore(&pInfo->lock, flags); 448 - } 429 + pInfo->bcc = 0; 430 + } else { 431 + spin_unlock_irqrestore(&pInfo->lock, flags); 432 + } 449 433 } 450 434 451 435 static void retry_transmit(struct r3964_info *pInfo) 452 436 { 453 - if(pInfo->nRetry<R3964_MAX_RETRIES) 454 - { 455 - TRACE_PE("transmission failed. Retry #%d", 456 - pInfo->nRetry); 457 - pInfo->bcc = 0; 458 - put_char(pInfo, STX); 459 - flush(pInfo); 460 - pInfo->state = R3964_TX_REQUEST; 461 - pInfo->nRetry++; 462 - mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); 463 - } 464 - else 465 - { 466 - TRACE_PE("transmission failed after %d retries", 467 - R3964_MAX_RETRIES); 437 + if (pInfo->nRetry < R3964_MAX_RETRIES) { 438 + TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry); 439 + pInfo->bcc = 0; 440 + put_char(pInfo, STX); 441 + flush(pInfo); 442 + pInfo->state = R3964_TX_REQUEST; 443 + pInfo->nRetry++; 444 + mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); 445 + } else { 446 + TRACE_PE("transmission failed after %d retries", 447 + R3964_MAX_RETRIES); 468 448 469 - remove_from_tx_queue(pInfo, R3964_TX_FAIL); 470 - 471 - put_char(pInfo, NAK); 472 - flush(pInfo); 473 - pInfo->state = R3964_IDLE; 449 + remove_from_tx_queue(pInfo, R3964_TX_FAIL); 474 450 475 - trigger_transmit(pInfo); 476 - } 451 + put_char(pInfo, NAK); 452 + flush(pInfo); 453 + pInfo->state = R3964_IDLE; 454 + 455 + trigger_transmit(pInfo); 456 + } 477 457 } 478 - 479 458 480 459 static void transmit_block(struct r3964_info *pInfo) 481 460 { 482 - struct tty_struct *tty = pInfo->tty; 483 - struct r3964_block_header *pBlock = pInfo->tx_first; 484 - int room=0; 461 + struct tty_struct *tty = pInfo->tty; 462 + struct r3964_block_header *pBlock = pInfo->tx_first; 463 + int room = 0; 485 464 486 - if((tty==NULL) || (pBlock==NULL)) 487 - { 488 - return; 489 - } 465 + if ((tty == NULL) || (pBlock == NULL)) { 466 + return; 467 + } 490 468 491 - if(tty->driver->write_room) 492 - room=tty->driver->write_room(tty); 469 + if (tty->driver->write_room) 470 + room = tty->driver->write_room(tty); 493 471 494 - TRACE_PS("transmit_block %p, room %d, length %d", 495 - pBlock, room, pBlock->length); 496 - 497 - while(pInfo->tx_position < pBlock->length) 498 - { 499 - if(room<2) 500 - break; 501 - 502 - if(pBlock->data[pInfo->tx_position]==DLE) 503 - { 504 - /* send additional DLE char: */ 505 - put_char(pInfo, DLE); 506 - } 507 - put_char(pInfo, pBlock->data[pInfo->tx_position++]); 508 - 509 - room--; 510 - } 472 + TRACE_PS("transmit_block %p, room %d, length %d", 473 + pBlock, room, pBlock->length); 511 474 512 - if((pInfo->tx_position == pBlock->length) && (room>=3)) 513 - { 514 - put_char(pInfo, DLE); 515 - put_char(pInfo, ETX); 516 - if(pInfo->flags & R3964_BCC) 517 - { 518 - put_char(pInfo, pInfo->bcc); 519 - } 520 - pInfo->state = R3964_WAIT_FOR_TX_ACK; 521 - mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); 522 - } 523 - flush(pInfo); 475 + while (pInfo->tx_position < pBlock->length) { 476 + if (room < 2) 477 + break; 478 + 479 + if (pBlock->data[pInfo->tx_position] == DLE) { 480 + /* send additional DLE char: */ 481 + put_char(pInfo, DLE); 482 + } 483 + put_char(pInfo, pBlock->data[pInfo->tx_position++]); 484 + 485 + room--; 486 + } 487 + 488 + if ((pInfo->tx_position == pBlock->length) && (room >= 3)) { 489 + put_char(pInfo, DLE); 490 + put_char(pInfo, ETX); 491 + if (pInfo->flags & R3964_BCC) { 492 + put_char(pInfo, pInfo->bcc); 493 + } 494 + pInfo->state = R3964_WAIT_FOR_TX_ACK; 495 + mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); 496 + } 497 + flush(pInfo); 524 498 } 525 499 526 500 static void on_receive_block(struct r3964_info *pInfo) 527 501 { 528 - unsigned int length; 529 - struct r3964_client_info *pClient; 530 - struct r3964_block_header *pBlock; 531 - 532 - length=pInfo->rx_position; 502 + unsigned int length; 503 + struct r3964_client_info *pClient; 504 + struct r3964_block_header *pBlock; 533 505 534 - /* compare byte checksum characters: */ 535 - if(pInfo->flags & R3964_BCC) 536 - { 537 - if(pInfo->bcc!=pInfo->last_rx) 538 - { 539 - TRACE_PE("checksum error - got %x but expected %x", 540 - pInfo->last_rx, pInfo->bcc); 541 - pInfo->flags |= R3964_CHECKSUM; 542 - } 543 - } 506 + length = pInfo->rx_position; 544 507 545 - /* check for errors (parity, overrun,...): */ 546 - if(pInfo->flags & R3964_ERROR) 547 - { 548 - TRACE_PE("on_receive_block - transmission failed error %x", 549 - pInfo->flags & R3964_ERROR); 550 - 551 - put_char(pInfo, NAK); 552 - flush(pInfo); 553 - if(pInfo->nRetry<R3964_MAX_RETRIES) 554 - { 555 - pInfo->state=R3964_WAIT_FOR_RX_REPEAT; 556 - pInfo->nRetry++; 557 - mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC); 558 - } 559 - else 560 - { 561 - TRACE_PE("on_receive_block - failed after max retries"); 562 - pInfo->state=R3964_IDLE; 563 - } 564 - return; 565 - } 508 + /* compare byte checksum characters: */ 509 + if (pInfo->flags & R3964_BCC) { 510 + if (pInfo->bcc != pInfo->last_rx) { 511 + TRACE_PE("checksum error - got %x but expected %x", 512 + pInfo->last_rx, pInfo->bcc); 513 + pInfo->flags |= R3964_CHECKSUM; 514 + } 515 + } 566 516 567 - 568 - /* received block; submit DLE: */ 569 - put_char(pInfo, DLE); 570 - flush(pInfo); 571 - del_timer_sync(&pInfo->tmr); 572 - TRACE_PS(" rx success: got %d chars", length); 517 + /* check for errors (parity, overrun,...): */ 518 + if (pInfo->flags & R3964_ERROR) { 519 + TRACE_PE("on_receive_block - transmission failed error %x", 520 + pInfo->flags & R3964_ERROR); 573 521 574 - /* prepare struct r3964_block_header: */ 575 - pBlock = kmalloc(length+sizeof(struct r3964_block_header), GFP_KERNEL); 576 - TRACE_M("on_receive_block - kmalloc %p",pBlock); 522 + put_char(pInfo, NAK); 523 + flush(pInfo); 524 + if (pInfo->nRetry < R3964_MAX_RETRIES) { 525 + pInfo->state = R3964_WAIT_FOR_RX_REPEAT; 526 + pInfo->nRetry++; 527 + mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC); 528 + } else { 529 + TRACE_PE("on_receive_block - failed after max retries"); 530 + pInfo->state = R3964_IDLE; 531 + } 532 + return; 533 + } 577 534 578 - if(pBlock==NULL) 579 - return; 535 + /* received block; submit DLE: */ 536 + put_char(pInfo, DLE); 537 + flush(pInfo); 538 + del_timer_sync(&pInfo->tmr); 539 + TRACE_PS(" rx success: got %d chars", length); 580 540 581 - pBlock->length = length; 582 - pBlock->data = ((unsigned char*)pBlock)+sizeof(struct r3964_block_header); 583 - pBlock->locks = 0; 584 - pBlock->next = NULL; 585 - pBlock->owner = NULL; 541 + /* prepare struct r3964_block_header: */ 542 + pBlock = kmalloc(length + sizeof(struct r3964_block_header), 543 + GFP_KERNEL); 544 + TRACE_M("on_receive_block - kmalloc %p", pBlock); 586 545 587 - memcpy(pBlock->data, pInfo->rx_buf, length); 546 + if (pBlock == NULL) 547 + return; 588 548 589 - /* queue block into rx_queue: */ 590 - add_rx_queue(pInfo, pBlock); 549 + pBlock->length = length; 550 + pBlock->data = ((unsigned char *)pBlock) + 551 + sizeof(struct r3964_block_header); 552 + pBlock->locks = 0; 553 + pBlock->next = NULL; 554 + pBlock->owner = NULL; 591 555 592 - /* notify attached client processes: */ 593 - for(pClient=pInfo->firstClient; pClient; pClient=pClient->next) 594 - { 595 - if(pClient->sig_flags & R3964_SIG_DATA) 596 - { 597 - add_msg(pClient, R3964_MSG_DATA, length, R3964_OK, pBlock); 598 - } 599 - } 600 - wake_up_interruptible (&pInfo->read_wait); 601 - 602 - pInfo->state = R3964_IDLE; 556 + memcpy(pBlock->data, pInfo->rx_buf, length); 603 557 604 - trigger_transmit(pInfo); 558 + /* queue block into rx_queue: */ 559 + add_rx_queue(pInfo, pBlock); 560 + 561 + /* notify attached client processes: */ 562 + for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) { 563 + if (pClient->sig_flags & R3964_SIG_DATA) { 564 + add_msg(pClient, R3964_MSG_DATA, length, R3964_OK, 565 + pBlock); 566 + } 567 + } 568 + wake_up_interruptible(&pInfo->read_wait); 569 + 570 + pInfo->state = R3964_IDLE; 571 + 572 + trigger_transmit(pInfo); 605 573 } 606 - 607 574 608 575 static void receive_char(struct r3964_info *pInfo, const unsigned char c) 609 576 { 610 - switch(pInfo->state) 611 - { 612 - case R3964_TX_REQUEST: 613 - if(c==DLE) 614 - { 615 - TRACE_PS("TX_REQUEST - got DLE"); 577 + switch (pInfo->state) { 578 + case R3964_TX_REQUEST: 579 + if (c == DLE) { 580 + TRACE_PS("TX_REQUEST - got DLE"); 616 581 617 - pInfo->state = R3964_TRANSMITTING; 618 - pInfo->tx_position = 0; 619 - 620 - transmit_block(pInfo); 621 - } 622 - else if(c==STX) 623 - { 624 - if(pInfo->nRetry==0) 625 - { 626 - TRACE_PE("TX_REQUEST - init conflict"); 627 - if(pInfo->priority == R3964_SLAVE) 628 - { 629 - goto start_receiving; 630 - } 631 - } 632 - else 633 - { 634 - TRACE_PE("TX_REQUEST - secondary init conflict!?" 635 - " Switching to SLAVE mode for next rx."); 636 - goto start_receiving; 637 - } 638 - } 639 - else 640 - { 641 - TRACE_PE("TX_REQUEST - char != DLE: %x", c); 642 - retry_transmit(pInfo); 643 - } 644 - break; 645 - case R3964_TRANSMITTING: 646 - if(c==NAK) 647 - { 648 - TRACE_PE("TRANSMITTING - got NAK"); 649 - retry_transmit(pInfo); 650 - } 651 - else 652 - { 653 - TRACE_PE("TRANSMITTING - got invalid char"); 654 - 655 - pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY; 656 - mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); 657 - } 658 - break; 659 - case R3964_WAIT_FOR_TX_ACK: 660 - if(c==DLE) 661 - { 662 - TRACE_PS("WAIT_FOR_TX_ACK - got DLE"); 663 - remove_from_tx_queue(pInfo, R3964_OK); 664 - 665 - pInfo->state = R3964_IDLE; 666 - trigger_transmit(pInfo); 667 - } 668 - else 669 - { 670 - retry_transmit(pInfo); 671 - } 672 - break; 673 - case R3964_WAIT_FOR_RX_REPEAT: 674 - /* FALLTROUGH */ 675 - case R3964_IDLE: 676 - if(c==STX) 677 - { 678 - /* Prevent rx_queue from overflow: */ 679 - if(pInfo->blocks_in_rx_queue >= R3964_MAX_BLOCKS_IN_RX_QUEUE) 680 - { 681 - TRACE_PE("IDLE - got STX but no space in rx_queue!"); 682 - pInfo->state=R3964_WAIT_FOR_RX_BUF; 683 - mod_timer(&pInfo->tmr, jiffies + R3964_TO_NO_BUF); 684 - break; 685 - } 582 + pInfo->state = R3964_TRANSMITTING; 583 + pInfo->tx_position = 0; 584 + 585 + transmit_block(pInfo); 586 + } else if (c == STX) { 587 + if (pInfo->nRetry == 0) { 588 + TRACE_PE("TX_REQUEST - init conflict"); 589 + if (pInfo->priority == R3964_SLAVE) { 590 + goto start_receiving; 591 + } 592 + } else { 593 + TRACE_PE("TX_REQUEST - secondary init " 594 + "conflict!? Switching to SLAVE mode " 595 + "for next rx."); 596 + goto start_receiving; 597 + } 598 + } else { 599 + TRACE_PE("TX_REQUEST - char != DLE: %x", c); 600 + retry_transmit(pInfo); 601 + } 602 + break; 603 + case R3964_TRANSMITTING: 604 + if (c == NAK) { 605 + TRACE_PE("TRANSMITTING - got NAK"); 606 + retry_transmit(pInfo); 607 + } else { 608 + TRACE_PE("TRANSMITTING - got invalid char"); 609 + 610 + pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY; 611 + mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); 612 + } 613 + break; 614 + case R3964_WAIT_FOR_TX_ACK: 615 + if (c == DLE) { 616 + TRACE_PS("WAIT_FOR_TX_ACK - got DLE"); 617 + remove_from_tx_queue(pInfo, R3964_OK); 618 + 619 + pInfo->state = R3964_IDLE; 620 + trigger_transmit(pInfo); 621 + } else { 622 + retry_transmit(pInfo); 623 + } 624 + break; 625 + case R3964_WAIT_FOR_RX_REPEAT: 626 + /* FALLTROUGH */ 627 + case R3964_IDLE: 628 + if (c == STX) { 629 + /* Prevent rx_queue from overflow: */ 630 + if (pInfo->blocks_in_rx_queue >= 631 + R3964_MAX_BLOCKS_IN_RX_QUEUE) { 632 + TRACE_PE("IDLE - got STX but no space in " 633 + "rx_queue!"); 634 + pInfo->state = R3964_WAIT_FOR_RX_BUF; 635 + mod_timer(&pInfo->tmr, 636 + jiffies + R3964_TO_NO_BUF); 637 + break; 638 + } 686 639 start_receiving: 687 - /* Ok, start receiving: */ 688 - TRACE_PS("IDLE - got STX"); 689 - pInfo->rx_position = 0; 690 - pInfo->last_rx = 0; 691 - pInfo->flags &= ~R3964_ERROR; 692 - pInfo->state=R3964_RECEIVING; 693 - mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); 694 - pInfo->nRetry = 0; 695 - put_char(pInfo, DLE); 696 - flush(pInfo); 697 - pInfo->bcc = 0; 698 - } 699 - break; 700 - case R3964_RECEIVING: 701 - if(pInfo->rx_position < RX_BUF_SIZE) 702 - { 703 - pInfo->bcc ^= c; 704 - 705 - if(c==DLE) 706 - { 707 - if(pInfo->last_rx==DLE) 708 - { 709 - pInfo->last_rx = 0; 710 - goto char_to_buf; 711 - } 712 - pInfo->last_rx = DLE; 713 - break; 714 - } 715 - else if((c==ETX) && (pInfo->last_rx==DLE)) 716 - { 717 - if(pInfo->flags & R3964_BCC) 718 - { 719 - pInfo->state = R3964_WAIT_FOR_BCC; 720 - mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); 721 - } 722 - else 723 - { 724 - on_receive_block(pInfo); 725 - } 726 - } 727 - else 728 - { 729 - pInfo->last_rx = c; 640 + /* Ok, start receiving: */ 641 + TRACE_PS("IDLE - got STX"); 642 + pInfo->rx_position = 0; 643 + pInfo->last_rx = 0; 644 + pInfo->flags &= ~R3964_ERROR; 645 + pInfo->state = R3964_RECEIVING; 646 + mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); 647 + pInfo->nRetry = 0; 648 + put_char(pInfo, DLE); 649 + flush(pInfo); 650 + pInfo->bcc = 0; 651 + } 652 + break; 653 + case R3964_RECEIVING: 654 + if (pInfo->rx_position < RX_BUF_SIZE) { 655 + pInfo->bcc ^= c; 656 + 657 + if (c == DLE) { 658 + if (pInfo->last_rx == DLE) { 659 + pInfo->last_rx = 0; 660 + goto char_to_buf; 661 + } 662 + pInfo->last_rx = DLE; 663 + break; 664 + } else if ((c == ETX) && (pInfo->last_rx == DLE)) { 665 + if (pInfo->flags & R3964_BCC) { 666 + pInfo->state = R3964_WAIT_FOR_BCC; 667 + mod_timer(&pInfo->tmr, 668 + jiffies + R3964_TO_ZVZ); 669 + } else { 670 + on_receive_block(pInfo); 671 + } 672 + } else { 673 + pInfo->last_rx = c; 730 674 char_to_buf: 731 - pInfo->rx_buf[pInfo->rx_position++] = c; 732 - mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); 733 - } 734 - } 735 - /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */ 736 - break; 737 - case R3964_WAIT_FOR_BCC: 738 - pInfo->last_rx = c; 739 - on_receive_block(pInfo); 740 - break; 741 - } 675 + pInfo->rx_buf[pInfo->rx_position++] = c; 676 + mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); 677 + } 678 + } 679 + /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */ 680 + break; 681 + case R3964_WAIT_FOR_BCC: 682 + pInfo->last_rx = c; 683 + on_receive_block(pInfo); 684 + break; 685 + } 742 686 } 743 687 744 688 static void receive_error(struct r3964_info *pInfo, const char flag) 745 689 { 746 - switch (flag) 747 - { 748 - case TTY_NORMAL: 749 - break; 750 - case TTY_BREAK: 751 - TRACE_PE("received break") 752 - pInfo->flags |= R3964_BREAK; 753 - break; 754 - case TTY_PARITY: 755 - TRACE_PE("parity error") 756 - pInfo->flags |= R3964_PARITY; 757 - break; 758 - case TTY_FRAME: 759 - TRACE_PE("frame error") 760 - pInfo->flags |= R3964_FRAME; 761 - break; 762 - case TTY_OVERRUN: 763 - TRACE_PE("frame overrun") 764 - pInfo->flags |= R3964_OVERRUN; 765 - break; 766 - default: 767 - TRACE_PE("receive_error - unknown flag %d", flag); 768 - pInfo->flags |= R3964_UNKNOWN; 769 - break; 770 - } 690 + switch (flag) { 691 + case TTY_NORMAL: 692 + break; 693 + case TTY_BREAK: 694 + TRACE_PE("received break"); 695 + pInfo->flags |= R3964_BREAK; 696 + break; 697 + case TTY_PARITY: 698 + TRACE_PE("parity error"); 699 + pInfo->flags |= R3964_PARITY; 700 + break; 701 + case TTY_FRAME: 702 + TRACE_PE("frame error"); 703 + pInfo->flags |= R3964_FRAME; 704 + break; 705 + case TTY_OVERRUN: 706 + TRACE_PE("frame overrun"); 707 + pInfo->flags |= R3964_OVERRUN; 708 + break; 709 + default: 710 + TRACE_PE("receive_error - unknown flag %d", flag); 711 + pInfo->flags |= R3964_UNKNOWN; 712 + break; 713 + } 771 714 } 772 715 773 716 static void on_timeout(unsigned long priv) 774 717 { 775 - struct r3964_info *pInfo = (void *)priv; 718 + struct r3964_info *pInfo = (void *)priv; 776 719 777 - switch(pInfo->state) 778 - { 779 - case R3964_TX_REQUEST: 780 - TRACE_PE("TX_REQUEST - timeout"); 781 - retry_transmit(pInfo); 782 - break; 783 - case R3964_WAIT_ZVZ_BEFORE_TX_RETRY: 784 - put_char(pInfo, NAK); 785 - flush(pInfo); 786 - retry_transmit(pInfo); 787 - break; 788 - case R3964_WAIT_FOR_TX_ACK: 789 - TRACE_PE("WAIT_FOR_TX_ACK - timeout"); 790 - retry_transmit(pInfo); 791 - break; 792 - case R3964_WAIT_FOR_RX_BUF: 793 - TRACE_PE("WAIT_FOR_RX_BUF - timeout"); 794 - put_char(pInfo, NAK); 795 - flush(pInfo); 796 - pInfo->state=R3964_IDLE; 797 - break; 798 - case R3964_RECEIVING: 799 - TRACE_PE("RECEIVING - timeout after %d chars", 800 - pInfo->rx_position); 801 - put_char(pInfo, NAK); 802 - flush(pInfo); 803 - pInfo->state=R3964_IDLE; 804 - break; 805 - case R3964_WAIT_FOR_RX_REPEAT: 806 - TRACE_PE("WAIT_FOR_RX_REPEAT - timeout"); 807 - pInfo->state=R3964_IDLE; 808 - break; 809 - case R3964_WAIT_FOR_BCC: 810 - TRACE_PE("WAIT_FOR_BCC - timeout"); 811 - put_char(pInfo, NAK); 812 - flush(pInfo); 813 - pInfo->state=R3964_IDLE; 814 - break; 815 - } 720 + switch (pInfo->state) { 721 + case R3964_TX_REQUEST: 722 + TRACE_PE("TX_REQUEST - timeout"); 723 + retry_transmit(pInfo); 724 + break; 725 + case R3964_WAIT_ZVZ_BEFORE_TX_RETRY: 726 + put_char(pInfo, NAK); 727 + flush(pInfo); 728 + retry_transmit(pInfo); 729 + break; 730 + case R3964_WAIT_FOR_TX_ACK: 731 + TRACE_PE("WAIT_FOR_TX_ACK - timeout"); 732 + retry_transmit(pInfo); 733 + break; 734 + case R3964_WAIT_FOR_RX_BUF: 735 + TRACE_PE("WAIT_FOR_RX_BUF - timeout"); 736 + put_char(pInfo, NAK); 737 + flush(pInfo); 738 + pInfo->state = R3964_IDLE; 739 + break; 740 + case R3964_RECEIVING: 741 + TRACE_PE("RECEIVING - timeout after %d chars", 742 + pInfo->rx_position); 743 + put_char(pInfo, NAK); 744 + flush(pInfo); 745 + pInfo->state = R3964_IDLE; 746 + break; 747 + case R3964_WAIT_FOR_RX_REPEAT: 748 + TRACE_PE("WAIT_FOR_RX_REPEAT - timeout"); 749 + pInfo->state = R3964_IDLE; 750 + break; 751 + case R3964_WAIT_FOR_BCC: 752 + TRACE_PE("WAIT_FOR_BCC - timeout"); 753 + put_char(pInfo, NAK); 754 + flush(pInfo); 755 + pInfo->state = R3964_IDLE; 756 + break; 757 + } 816 758 } 817 759 818 - static struct r3964_client_info *findClient( 819 - struct r3964_info *pInfo, struct pid *pid) 760 + static struct r3964_client_info *findClient(struct r3964_info *pInfo, 761 + struct pid *pid) 820 762 { 821 - struct r3964_client_info *pClient; 822 - 823 - for(pClient=pInfo->firstClient; pClient; pClient=pClient->next) 824 - { 825 - if(pClient->pid == pid) 826 - { 827 - return pClient; 828 - } 829 - } 830 - return NULL; 763 + struct r3964_client_info *pClient; 764 + 765 + for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) { 766 + if (pClient->pid == pid) { 767 + return pClient; 768 + } 769 + } 770 + return NULL; 831 771 } 832 772 833 773 static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg) 834 774 { 835 - struct r3964_client_info *pClient; 836 - struct r3964_client_info **ppClient; 837 - struct r3964_message *pMsg; 838 - 839 - if((arg & R3964_SIG_ALL)==0) 840 - { 841 - /* Remove client from client list */ 842 - for(ppClient=&pInfo->firstClient; *ppClient; ppClient=&(*ppClient)->next) 843 - { 844 - pClient = *ppClient; 845 - 846 - if(pClient->pid == pid) 847 - { 848 - TRACE_PS("removing client %d from client list", pid_nr(pid)); 849 - *ppClient = pClient->next; 850 - while(pClient->msg_count) 851 - { 852 - pMsg=remove_msg(pInfo, pClient); 853 - if(pMsg) 854 - { 855 - kfree(pMsg); 856 - TRACE_M("enable_signals - msg kfree %p",pMsg); 857 - } 858 - } 859 - put_pid(pClient->pid); 860 - kfree(pClient); 861 - TRACE_M("enable_signals - kfree %p",pClient); 862 - return 0; 863 - } 864 - } 865 - return -EINVAL; 866 - } 867 - else 868 - { 869 - pClient=findClient(pInfo, pid); 870 - if(pClient) 871 - { 872 - /* update signal options */ 873 - pClient->sig_flags=arg; 874 - } 875 - else 876 - { 877 - /* add client to client list */ 878 - pClient=kmalloc(sizeof(struct r3964_client_info), GFP_KERNEL); 879 - TRACE_M("enable_signals - kmalloc %p",pClient); 880 - if(pClient==NULL) 881 - return -ENOMEM; 775 + struct r3964_client_info *pClient; 776 + struct r3964_client_info **ppClient; 777 + struct r3964_message *pMsg; 882 778 883 - TRACE_PS("add client %d to client list", pid_nr(pid)); 884 - spin_lock_init(&pClient->lock); 885 - pClient->sig_flags=arg; 886 - pClient->pid = get_pid(pid); 887 - pClient->next=pInfo->firstClient; 888 - pClient->first_msg = NULL; 889 - pClient->last_msg = NULL; 890 - pClient->next_block_to_read = NULL; 891 - pClient->msg_count = 0; 892 - pInfo->firstClient=pClient; 893 - } 894 - } 779 + if ((arg & R3964_SIG_ALL) == 0) { 780 + /* Remove client from client list */ 781 + for (ppClient = &pInfo->firstClient; *ppClient; 782 + ppClient = &(*ppClient)->next) { 783 + pClient = *ppClient; 895 784 896 - return 0; 785 + if (pClient->pid == pid) { 786 + TRACE_PS("removing client %d from client list", 787 + pid_nr(pid)); 788 + *ppClient = pClient->next; 789 + while (pClient->msg_count) { 790 + pMsg = remove_msg(pInfo, pClient); 791 + if (pMsg) { 792 + kfree(pMsg); 793 + TRACE_M("enable_signals - msg " 794 + "kfree %p", pMsg); 795 + } 796 + } 797 + put_pid(pClient->pid); 798 + kfree(pClient); 799 + TRACE_M("enable_signals - kfree %p", pClient); 800 + return 0; 801 + } 802 + } 803 + return -EINVAL; 804 + } else { 805 + pClient = findClient(pInfo, pid); 806 + if (pClient) { 807 + /* update signal options */ 808 + pClient->sig_flags = arg; 809 + } else { 810 + /* add client to client list */ 811 + pClient = kmalloc(sizeof(struct r3964_client_info), 812 + GFP_KERNEL); 813 + TRACE_M("enable_signals - kmalloc %p", pClient); 814 + if (pClient == NULL) 815 + return -ENOMEM; 816 + 817 + TRACE_PS("add client %d to client list", pid_nr(pid)); 818 + spin_lock_init(&pClient->lock); 819 + pClient->sig_flags = arg; 820 + pClient->pid = get_pid(pid); 821 + pClient->next = pInfo->firstClient; 822 + pClient->first_msg = NULL; 823 + pClient->last_msg = NULL; 824 + pClient->next_block_to_read = NULL; 825 + pClient->msg_count = 0; 826 + pInfo->firstClient = pClient; 827 + } 828 + } 829 + 830 + return 0; 897 831 } 898 832 899 - static int read_telegram(struct r3964_info *pInfo, struct pid *pid, unsigned char __user *buf) 833 + static int read_telegram(struct r3964_info *pInfo, struct pid *pid, 834 + unsigned char __user * buf) 900 835 { 901 - struct r3964_client_info *pClient; 902 - struct r3964_block_header *block; 836 + struct r3964_client_info *pClient; 837 + struct r3964_block_header *block; 903 838 904 - if(!buf) 905 - { 906 - return -EINVAL; 907 - } 839 + if (!buf) { 840 + return -EINVAL; 841 + } 908 842 909 - pClient=findClient(pInfo,pid); 910 - if(pClient==NULL) 911 - { 912 - return -EINVAL; 913 - } 914 - 915 - block=pClient->next_block_to_read; 916 - if(!block) 917 - { 918 - return 0; 919 - } 920 - else 921 - { 922 - if (copy_to_user (buf, block->data, block->length)) 923 - return -EFAULT; 843 + pClient = findClient(pInfo, pid); 844 + if (pClient == NULL) { 845 + return -EINVAL; 846 + } 924 847 925 - remove_client_block(pInfo, pClient); 926 - return block->length; 927 - } 848 + block = pClient->next_block_to_read; 849 + if (!block) { 850 + return 0; 851 + } else { 852 + if (copy_to_user(buf, block->data, block->length)) 853 + return -EFAULT; 928 854 929 - return -EINVAL; 855 + remove_client_block(pInfo, pClient); 856 + return block->length; 857 + } 858 + 859 + return -EINVAL; 930 860 } 931 861 932 862 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg, 933 - int error_code, struct r3964_block_header *pBlock) 863 + int error_code, struct r3964_block_header *pBlock) 934 864 { 935 - struct r3964_message *pMsg; 936 - unsigned long flags; 937 - 938 - if(pClient->msg_count<R3964_MAX_MSG_COUNT-1) 939 - { 865 + struct r3964_message *pMsg; 866 + unsigned long flags; 867 + 868 + if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) { 940 869 queue_the_message: 941 870 942 - pMsg = kmalloc(sizeof(struct r3964_message), 943 - error_code?GFP_ATOMIC:GFP_KERNEL); 944 - TRACE_M("add_msg - kmalloc %p",pMsg); 945 - if(pMsg==NULL) { 946 - return; 947 - } 871 + pMsg = kmalloc(sizeof(struct r3964_message), 872 + error_code ? GFP_ATOMIC : GFP_KERNEL); 873 + TRACE_M("add_msg - kmalloc %p", pMsg); 874 + if (pMsg == NULL) { 875 + return; 876 + } 948 877 949 - spin_lock_irqsave(&pClient->lock, flags); 878 + spin_lock_irqsave(&pClient->lock, flags); 950 879 951 - pMsg->msg_id = msg_id; 952 - pMsg->arg = arg; 953 - pMsg->error_code = error_code; 954 - pMsg->block = pBlock; 955 - pMsg->next = NULL; 956 - 957 - if(pClient->last_msg==NULL) 958 - { 959 - pClient->first_msg=pClient->last_msg=pMsg; 960 - } 961 - else 962 - { 963 - pClient->last_msg->next = pMsg; 964 - pClient->last_msg=pMsg; 965 - } 880 + pMsg->msg_id = msg_id; 881 + pMsg->arg = arg; 882 + pMsg->error_code = error_code; 883 + pMsg->block = pBlock; 884 + pMsg->next = NULL; 966 885 967 - pClient->msg_count++; 886 + if (pClient->last_msg == NULL) { 887 + pClient->first_msg = pClient->last_msg = pMsg; 888 + } else { 889 + pClient->last_msg->next = pMsg; 890 + pClient->last_msg = pMsg; 891 + } 968 892 969 - if(pBlock!=NULL) 970 - { 971 - pBlock->locks++; 972 - } 973 - spin_unlock_irqrestore(&pClient->lock, flags); 974 - } 975 - else 976 - { 977 - if((pClient->last_msg->msg_id == R3964_MSG_ACK) 978 - && (pClient->last_msg->error_code==R3964_OVERFLOW)) 979 - { 980 - pClient->last_msg->arg++; 981 - TRACE_PE("add_msg - inc prev OVERFLOW-msg"); 982 - } 983 - else 984 - { 985 - msg_id = R3964_MSG_ACK; 986 - arg = 0; 987 - error_code = R3964_OVERFLOW; 988 - pBlock = NULL; 989 - TRACE_PE("add_msg - queue OVERFLOW-msg"); 990 - goto queue_the_message; 991 - } 992 - } 993 - /* Send SIGIO signal to client process: */ 994 - if(pClient->sig_flags & R3964_USE_SIGIO) 995 - { 996 - kill_pid(pClient->pid, SIGIO, 1); 997 - } 893 + pClient->msg_count++; 894 + 895 + if (pBlock != NULL) { 896 + pBlock->locks++; 897 + } 898 + spin_unlock_irqrestore(&pClient->lock, flags); 899 + } else { 900 + if ((pClient->last_msg->msg_id == R3964_MSG_ACK) 901 + && (pClient->last_msg->error_code == R3964_OVERFLOW)) { 902 + pClient->last_msg->arg++; 903 + TRACE_PE("add_msg - inc prev OVERFLOW-msg"); 904 + } else { 905 + msg_id = R3964_MSG_ACK; 906 + arg = 0; 907 + error_code = R3964_OVERFLOW; 908 + pBlock = NULL; 909 + TRACE_PE("add_msg - queue OVERFLOW-msg"); 910 + goto queue_the_message; 911 + } 912 + } 913 + /* Send SIGIO signal to client process: */ 914 + if (pClient->sig_flags & R3964_USE_SIGIO) { 915 + kill_pid(pClient->pid, SIGIO, 1); 916 + } 998 917 } 999 918 1000 919 static struct r3964_message *remove_msg(struct r3964_info *pInfo, 1001 - struct r3964_client_info *pClient) 920 + struct r3964_client_info *pClient) 1002 921 { 1003 - struct r3964_message *pMsg=NULL; 1004 - unsigned long flags; 922 + struct r3964_message *pMsg = NULL; 923 + unsigned long flags; 1005 924 1006 - if(pClient->first_msg) 1007 - { 1008 - spin_lock_irqsave(&pClient->lock, flags); 925 + if (pClient->first_msg) { 926 + spin_lock_irqsave(&pClient->lock, flags); 1009 927 1010 - pMsg = pClient->first_msg; 1011 - pClient->first_msg = pMsg->next; 1012 - if(pClient->first_msg==NULL) 1013 - { 1014 - pClient->last_msg = NULL; 1015 - } 1016 - 1017 - pClient->msg_count--; 1018 - if(pMsg->block) 1019 - { 1020 - remove_client_block(pInfo, pClient); 1021 - pClient->next_block_to_read = pMsg->block; 1022 - } 1023 - spin_unlock_irqrestore(&pClient->lock, flags); 1024 - } 1025 - return pMsg; 928 + pMsg = pClient->first_msg; 929 + pClient->first_msg = pMsg->next; 930 + if (pClient->first_msg == NULL) { 931 + pClient->last_msg = NULL; 932 + } 933 + 934 + pClient->msg_count--; 935 + if (pMsg->block) { 936 + remove_client_block(pInfo, pClient); 937 + pClient->next_block_to_read = pMsg->block; 938 + } 939 + spin_unlock_irqrestore(&pClient->lock, flags); 940 + } 941 + return pMsg; 1026 942 } 1027 943 1028 - static void remove_client_block(struct r3964_info *pInfo, 1029 - struct r3964_client_info *pClient) 944 + static void remove_client_block(struct r3964_info *pInfo, 945 + struct r3964_client_info *pClient) 1030 946 { 1031 - struct r3964_block_header *block; 947 + struct r3964_block_header *block; 1032 948 1033 - TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid)); 949 + TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid)); 1034 950 1035 - block=pClient->next_block_to_read; 1036 - if(block) 1037 - { 1038 - block->locks--; 1039 - if(block->locks==0) 1040 - { 1041 - remove_from_rx_queue(pInfo, block); 1042 - } 1043 - } 1044 - pClient->next_block_to_read = NULL; 951 + block = pClient->next_block_to_read; 952 + if (block) { 953 + block->locks--; 954 + if (block->locks == 0) { 955 + remove_from_rx_queue(pInfo, block); 956 + } 957 + } 958 + pClient->next_block_to_read = NULL; 1045 959 } 1046 - 1047 960 1048 961 /************************************************************* 1049 962 * Line discipline routines ··· 951 1064 952 1065 static int r3964_open(struct tty_struct *tty) 953 1066 { 954 - struct r3964_info *pInfo; 955 - 956 - TRACE_L("open"); 957 - TRACE_L("tty=%p, PID=%d, disc_data=%p", 958 - tty, current->pid, tty->disc_data); 959 - 960 - pInfo=kmalloc(sizeof(struct r3964_info), GFP_KERNEL); 961 - TRACE_M("r3964_open - info kmalloc %p",pInfo); 1067 + struct r3964_info *pInfo; 962 1068 963 - if(!pInfo) 964 - { 965 - printk(KERN_ERR "r3964: failed to alloc info structure\n"); 966 - return -ENOMEM; 967 - } 1069 + TRACE_L("open"); 1070 + TRACE_L("tty=%p, PID=%d, disc_data=%p", 1071 + tty, current->pid, tty->disc_data); 968 1072 969 - pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL); 970 - TRACE_M("r3964_open - rx_buf kmalloc %p",pInfo->rx_buf); 1073 + pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL); 1074 + TRACE_M("r3964_open - info kmalloc %p", pInfo); 971 1075 972 - if(!pInfo->rx_buf) 973 - { 974 - printk(KERN_ERR "r3964: failed to alloc receive buffer\n"); 975 - kfree(pInfo); 976 - TRACE_M("r3964_open - info kfree %p",pInfo); 977 - return -ENOMEM; 978 - } 979 - 980 - pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL); 981 - TRACE_M("r3964_open - tx_buf kmalloc %p",pInfo->tx_buf); 1076 + if (!pInfo) { 1077 + printk(KERN_ERR "r3964: failed to alloc info structure\n"); 1078 + return -ENOMEM; 1079 + } 982 1080 983 - if(!pInfo->tx_buf) 984 - { 985 - printk(KERN_ERR "r3964: failed to alloc transmit buffer\n"); 986 - kfree(pInfo->rx_buf); 987 - TRACE_M("r3964_open - rx_buf kfree %p",pInfo->rx_buf); 988 - kfree(pInfo); 989 - TRACE_M("r3964_open - info kfree %p",pInfo); 990 - return -ENOMEM; 991 - } 1081 + pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL); 1082 + TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf); 992 1083 993 - spin_lock_init(&pInfo->lock); 994 - pInfo->tty = tty; 995 - init_waitqueue_head (&pInfo->read_wait); 996 - pInfo->priority = R3964_MASTER; 997 - pInfo->rx_first = pInfo->rx_last = NULL; 998 - pInfo->tx_first = pInfo->tx_last = NULL; 999 - pInfo->rx_position = 0; 1000 - pInfo->tx_position = 0; 1001 - pInfo->last_rx = 0; 1002 - pInfo->blocks_in_rx_queue = 0; 1003 - pInfo->firstClient=NULL; 1004 - pInfo->state=R3964_IDLE; 1005 - pInfo->flags = R3964_DEBUG; 1006 - pInfo->nRetry = 0; 1007 - 1008 - tty->disc_data = pInfo; 1009 - tty->receive_room = 65536; 1084 + if (!pInfo->rx_buf) { 1085 + printk(KERN_ERR "r3964: failed to alloc receive buffer\n"); 1086 + kfree(pInfo); 1087 + TRACE_M("r3964_open - info kfree %p", pInfo); 1088 + return -ENOMEM; 1089 + } 1010 1090 1011 - init_timer(&pInfo->tmr); 1012 - pInfo->tmr.data = (unsigned long)pInfo; 1013 - pInfo->tmr.function = on_timeout; 1091 + pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL); 1092 + TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf); 1014 1093 1015 - return 0; 1094 + if (!pInfo->tx_buf) { 1095 + printk(KERN_ERR "r3964: failed to alloc transmit buffer\n"); 1096 + kfree(pInfo->rx_buf); 1097 + TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf); 1098 + kfree(pInfo); 1099 + TRACE_M("r3964_open - info kfree %p", pInfo); 1100 + return -ENOMEM; 1101 + } 1102 + 1103 + spin_lock_init(&pInfo->lock); 1104 + pInfo->tty = tty; 1105 + init_waitqueue_head(&pInfo->read_wait); 1106 + pInfo->priority = R3964_MASTER; 1107 + pInfo->rx_first = pInfo->rx_last = NULL; 1108 + pInfo->tx_first = pInfo->tx_last = NULL; 1109 + pInfo->rx_position = 0; 1110 + pInfo->tx_position = 0; 1111 + pInfo->last_rx = 0; 1112 + pInfo->blocks_in_rx_queue = 0; 1113 + pInfo->firstClient = NULL; 1114 + pInfo->state = R3964_IDLE; 1115 + pInfo->flags = R3964_DEBUG; 1116 + pInfo->nRetry = 0; 1117 + 1118 + tty->disc_data = pInfo; 1119 + tty->receive_room = 65536; 1120 + 1121 + init_timer(&pInfo->tmr); 1122 + pInfo->tmr.data = (unsigned long)pInfo; 1123 + pInfo->tmr.function = on_timeout; 1124 + 1125 + return 0; 1016 1126 } 1017 1127 1018 1128 static void r3964_close(struct tty_struct *tty) 1019 1129 { 1020 - struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; 1021 - struct r3964_client_info *pClient, *pNext; 1022 - struct r3964_message *pMsg; 1023 - struct r3964_block_header *pHeader, *pNextHeader; 1024 - unsigned long flags; 1130 + struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data; 1131 + struct r3964_client_info *pClient, *pNext; 1132 + struct r3964_message *pMsg; 1133 + struct r3964_block_header *pHeader, *pNextHeader; 1134 + unsigned long flags; 1025 1135 1026 - TRACE_L("close"); 1136 + TRACE_L("close"); 1027 1137 1028 - /* 1029 - * Make sure that our task queue isn't activated. If it 1030 - * is, take it out of the linked list. 1031 - */ 1032 - del_timer_sync(&pInfo->tmr); 1138 + /* 1139 + * Make sure that our task queue isn't activated. If it 1140 + * is, take it out of the linked list. 1141 + */ 1142 + del_timer_sync(&pInfo->tmr); 1033 1143 1034 - /* Remove client-structs and message queues: */ 1035 - pClient=pInfo->firstClient; 1036 - while(pClient) 1037 - { 1038 - pNext=pClient->next; 1039 - while(pClient->msg_count) 1040 - { 1041 - pMsg=remove_msg(pInfo, pClient); 1042 - if(pMsg) 1043 - { 1044 - kfree(pMsg); 1045 - TRACE_M("r3964_close - msg kfree %p",pMsg); 1046 - } 1047 - } 1048 - put_pid(pClient->pid); 1049 - kfree(pClient); 1050 - TRACE_M("r3964_close - client kfree %p",pClient); 1051 - pClient=pNext; 1052 - } 1053 - /* Remove jobs from tx_queue: */ 1054 - spin_lock_irqsave(&pInfo->lock, flags); 1055 - pHeader=pInfo->tx_first; 1056 - pInfo->tx_first=pInfo->tx_last=NULL; 1144 + /* Remove client-structs and message queues: */ 1145 + pClient = pInfo->firstClient; 1146 + while (pClient) { 1147 + pNext = pClient->next; 1148 + while (pClient->msg_count) { 1149 + pMsg = remove_msg(pInfo, pClient); 1150 + if (pMsg) { 1151 + kfree(pMsg); 1152 + TRACE_M("r3964_close - msg kfree %p", pMsg); 1153 + } 1154 + } 1155 + put_pid(pClient->pid); 1156 + kfree(pClient); 1157 + TRACE_M("r3964_close - client kfree %p", pClient); 1158 + pClient = pNext; 1159 + } 1160 + /* Remove jobs from tx_queue: */ 1161 + spin_lock_irqsave(&pInfo->lock, flags); 1162 + pHeader = pInfo->tx_first; 1163 + pInfo->tx_first = pInfo->tx_last = NULL; 1057 1164 spin_unlock_irqrestore(&pInfo->lock, flags); 1058 - 1059 - while(pHeader) 1060 - { 1061 - pNextHeader=pHeader->next; 1062 - kfree(pHeader); 1063 - pHeader=pNextHeader; 1165 + 1166 + while (pHeader) { 1167 + pNextHeader = pHeader->next; 1168 + kfree(pHeader); 1169 + pHeader = pNextHeader; 1064 1170 } 1065 1171 1066 - /* Free buffers: */ 1067 - wake_up_interruptible(&pInfo->read_wait); 1068 - kfree(pInfo->rx_buf); 1069 - TRACE_M("r3964_close - rx_buf kfree %p",pInfo->rx_buf); 1070 - kfree(pInfo->tx_buf); 1071 - TRACE_M("r3964_close - tx_buf kfree %p",pInfo->tx_buf); 1072 - kfree(pInfo); 1073 - TRACE_M("r3964_close - info kfree %p",pInfo); 1172 + /* Free buffers: */ 1173 + wake_up_interruptible(&pInfo->read_wait); 1174 + kfree(pInfo->rx_buf); 1175 + TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf); 1176 + kfree(pInfo->tx_buf); 1177 + TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf); 1178 + kfree(pInfo); 1179 + TRACE_M("r3964_close - info kfree %p", pInfo); 1074 1180 } 1075 1181 1076 1182 static ssize_t r3964_read(struct tty_struct *tty, struct file *file, 1077 - unsigned char __user *buf, size_t nr) 1183 + unsigned char __user * buf, size_t nr) 1078 1184 { 1079 - struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; 1080 - struct r3964_client_info *pClient; 1081 - struct r3964_message *pMsg; 1082 - struct r3964_client_message theMsg; 1083 - DECLARE_WAITQUEUE (wait, current); 1084 - 1085 - int count; 1086 - 1087 - TRACE_L("read()"); 1088 - 1089 - pClient=findClient(pInfo, task_pid(current)); 1090 - if(pClient) 1091 - { 1092 - pMsg = remove_msg(pInfo, pClient); 1093 - if(pMsg==NULL) 1094 - { 1095 - /* no messages available. */ 1096 - if (file->f_flags & O_NONBLOCK) 1097 - { 1098 - return -EAGAIN; 1099 - } 1100 - /* block until there is a message: */ 1101 - add_wait_queue(&pInfo->read_wait, &wait); 1185 + struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data; 1186 + struct r3964_client_info *pClient; 1187 + struct r3964_message *pMsg; 1188 + struct r3964_client_message theMsg; 1189 + DECLARE_WAITQUEUE(wait, current); 1190 + 1191 + int count; 1192 + 1193 + TRACE_L("read()"); 1194 + 1195 + pClient = findClient(pInfo, task_pid(current)); 1196 + if (pClient) { 1197 + pMsg = remove_msg(pInfo, pClient); 1198 + if (pMsg == NULL) { 1199 + /* no messages available. */ 1200 + if (file->f_flags & O_NONBLOCK) { 1201 + return -EAGAIN; 1202 + } 1203 + /* block until there is a message: */ 1204 + add_wait_queue(&pInfo->read_wait, &wait); 1102 1205 repeat: 1103 - current->state = TASK_INTERRUPTIBLE; 1104 - pMsg = remove_msg(pInfo, pClient); 1105 - if (!pMsg && !signal_pending(current)) 1106 - { 1107 - schedule(); 1108 - goto repeat; 1109 - } 1110 - current->state = TASK_RUNNING; 1111 - remove_wait_queue(&pInfo->read_wait, &wait); 1112 - } 1113 - 1114 - /* If we still haven't got a message, we must have been signalled */ 1206 + current->state = TASK_INTERRUPTIBLE; 1207 + pMsg = remove_msg(pInfo, pClient); 1208 + if (!pMsg && !signal_pending(current)) { 1209 + schedule(); 1210 + goto repeat; 1211 + } 1212 + current->state = TASK_RUNNING; 1213 + remove_wait_queue(&pInfo->read_wait, &wait); 1214 + } 1115 1215 1116 - if (!pMsg) return -EINTR; 1216 + /* If we still haven't got a message, we must have been signalled */ 1117 1217 1118 - /* deliver msg to client process: */ 1119 - theMsg.msg_id = pMsg->msg_id; 1120 - theMsg.arg = pMsg->arg; 1121 - theMsg.error_code = pMsg->error_code; 1122 - count = sizeof(struct r3964_client_message); 1218 + if (!pMsg) 1219 + return -EINTR; 1123 1220 1124 - kfree(pMsg); 1125 - TRACE_M("r3964_read - msg kfree %p",pMsg); 1221 + /* deliver msg to client process: */ 1222 + theMsg.msg_id = pMsg->msg_id; 1223 + theMsg.arg = pMsg->arg; 1224 + theMsg.error_code = pMsg->error_code; 1225 + count = sizeof(struct r3964_client_message); 1126 1226 1127 - if (copy_to_user(buf,&theMsg, count)) 1128 - return -EFAULT; 1227 + kfree(pMsg); 1228 + TRACE_M("r3964_read - msg kfree %p", pMsg); 1129 1229 1130 - TRACE_PS("read - return %d", count); 1131 - return count; 1132 - } 1133 - return -EPERM; 1230 + if (copy_to_user(buf, &theMsg, count)) 1231 + return -EFAULT; 1232 + 1233 + TRACE_PS("read - return %d", count); 1234 + return count; 1235 + } 1236 + return -EPERM; 1134 1237 } 1135 1238 1136 - static ssize_t r3964_write(struct tty_struct * tty, struct file * file, 1239 + static ssize_t r3964_write(struct tty_struct *tty, struct file *file, 1137 1240 const unsigned char *data, size_t count) 1138 1241 { 1139 - struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; 1140 - struct r3964_block_header *pHeader; 1141 - struct r3964_client_info *pClient; 1142 - unsigned char *new_data; 1143 - 1144 - TRACE_L("write request, %d characters", count); 1242 + struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data; 1243 + struct r3964_block_header *pHeader; 1244 + struct r3964_client_info *pClient; 1245 + unsigned char *new_data; 1246 + 1247 + TRACE_L("write request, %d characters", count); 1145 1248 /* 1146 1249 * Verify the pointers 1147 1250 */ 1148 1251 1149 - if(!pInfo) 1150 - return -EIO; 1252 + if (!pInfo) 1253 + return -EIO; 1151 1254 1152 1255 /* 1153 1256 * Ensure that the caller does not wish to send too much. 1154 1257 */ 1155 - if (count > R3964_MTU) 1156 - { 1157 - if (pInfo->flags & R3964_DEBUG) 1158 - { 1159 - TRACE_L (KERN_WARNING 1160 - "r3964_write: truncating user packet " 1161 - "from %u to mtu %d", count, R3964_MTU); 1162 - } 1163 - count = R3964_MTU; 1164 - } 1258 + if (count > R3964_MTU) { 1259 + if (pInfo->flags & R3964_DEBUG) { 1260 + TRACE_L(KERN_WARNING "r3964_write: truncating user " 1261 + "packet from %u to mtu %d", count, R3964_MTU); 1262 + } 1263 + count = R3964_MTU; 1264 + } 1165 1265 /* 1166 1266 * Allocate a buffer for the data and copy it from the buffer with header prepended 1167 1267 */ 1168 - new_data = kmalloc (count+sizeof(struct r3964_block_header), GFP_KERNEL); 1169 - TRACE_M("r3964_write - kmalloc %p",new_data); 1170 - if (new_data == NULL) { 1171 - if (pInfo->flags & R3964_DEBUG) 1172 - { 1173 - printk (KERN_ERR 1174 - "r3964_write: no memory\n"); 1175 - } 1176 - return -ENOSPC; 1177 - } 1178 - 1179 - pHeader = (struct r3964_block_header *)new_data; 1180 - pHeader->data = new_data + sizeof(struct r3964_block_header); 1181 - pHeader->length = count; 1182 - pHeader->locks = 0; 1183 - pHeader->owner = NULL; 1184 - 1185 - pClient=findClient(pInfo, task_pid(current)); 1186 - if(pClient) 1187 - { 1188 - pHeader->owner = pClient; 1189 - } 1268 + new_data = kmalloc(count + sizeof(struct r3964_block_header), 1269 + GFP_KERNEL); 1270 + TRACE_M("r3964_write - kmalloc %p", new_data); 1271 + if (new_data == NULL) { 1272 + if (pInfo->flags & R3964_DEBUG) { 1273 + printk(KERN_ERR "r3964_write: no memory\n"); 1274 + } 1275 + return -ENOSPC; 1276 + } 1190 1277 1191 - memcpy(pHeader->data, data, count); /* We already verified this */ 1278 + pHeader = (struct r3964_block_header *)new_data; 1279 + pHeader->data = new_data + sizeof(struct r3964_block_header); 1280 + pHeader->length = count; 1281 + pHeader->locks = 0; 1282 + pHeader->owner = NULL; 1192 1283 1193 - if(pInfo->flags & R3964_DEBUG) 1194 - { 1195 - dump_block(pHeader->data, count); 1196 - } 1284 + pClient = findClient(pInfo, task_pid(current)); 1285 + if (pClient) { 1286 + pHeader->owner = pClient; 1287 + } 1288 + 1289 + memcpy(pHeader->data, data, count); /* We already verified this */ 1290 + 1291 + if (pInfo->flags & R3964_DEBUG) { 1292 + dump_block(pHeader->data, count); 1293 + } 1197 1294 1198 1295 /* 1199 1296 * Add buffer to transmit-queue: 1200 1297 */ 1201 - add_tx_queue(pInfo, pHeader); 1202 - trigger_transmit(pInfo); 1203 - 1204 - return 0; 1298 + add_tx_queue(pInfo, pHeader); 1299 + trigger_transmit(pInfo); 1300 + 1301 + return 0; 1205 1302 } 1206 1303 1207 - static int r3964_ioctl(struct tty_struct * tty, struct file * file, 1208 - unsigned int cmd, unsigned long arg) 1304 + static int r3964_ioctl(struct tty_struct *tty, struct file *file, 1305 + unsigned int cmd, unsigned long arg) 1209 1306 { 1210 - struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; 1211 - if(pInfo==NULL) 1212 - return -EINVAL; 1213 - switch(cmd) 1214 - { 1215 - case R3964_ENABLE_SIGNALS: 1216 - return enable_signals(pInfo, task_pid(current), arg); 1217 - case R3964_SETPRIORITY: 1218 - if(arg<R3964_MASTER || arg>R3964_SLAVE) 1219 - return -EINVAL; 1220 - pInfo->priority = arg & 0xff; 1221 - return 0; 1222 - case R3964_USE_BCC: 1223 - if(arg) 1224 - pInfo->flags |= R3964_BCC; 1225 - else 1226 - pInfo->flags &= ~R3964_BCC; 1227 - return 0; 1228 - case R3964_READ_TELEGRAM: 1229 - return read_telegram(pInfo, task_pid(current), (unsigned char __user *)arg); 1230 - default: 1231 - return -ENOIOCTLCMD; 1232 - } 1307 + struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data; 1308 + if (pInfo == NULL) 1309 + return -EINVAL; 1310 + switch (cmd) { 1311 + case R3964_ENABLE_SIGNALS: 1312 + return enable_signals(pInfo, task_pid(current), arg); 1313 + case R3964_SETPRIORITY: 1314 + if (arg < R3964_MASTER || arg > R3964_SLAVE) 1315 + return -EINVAL; 1316 + pInfo->priority = arg & 0xff; 1317 + return 0; 1318 + case R3964_USE_BCC: 1319 + if (arg) 1320 + pInfo->flags |= R3964_BCC; 1321 + else 1322 + pInfo->flags &= ~R3964_BCC; 1323 + return 0; 1324 + case R3964_READ_TELEGRAM: 1325 + return read_telegram(pInfo, task_pid(current), 1326 + (unsigned char __user *)arg); 1327 + default: 1328 + return -ENOIOCTLCMD; 1329 + } 1233 1330 } 1234 1331 1235 - static void r3964_set_termios(struct tty_struct *tty, struct ktermios * old) 1332 + static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old) 1236 1333 { 1237 - TRACE_L("set_termios"); 1334 + TRACE_L("set_termios"); 1238 1335 } 1239 1336 1240 1337 /* Called without the kernel lock held - fine */ 1241 - static unsigned int r3964_poll(struct tty_struct * tty, struct file * file, 1242 - struct poll_table_struct *wait) 1338 + static unsigned int r3964_poll(struct tty_struct *tty, struct file *file, 1339 + struct poll_table_struct *wait) 1243 1340 { 1244 - struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; 1245 - struct r3964_client_info *pClient; 1246 - struct r3964_message *pMsg=NULL; 1247 - unsigned long flags; 1248 - int result = POLLOUT; 1341 + struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data; 1342 + struct r3964_client_info *pClient; 1343 + struct r3964_message *pMsg = NULL; 1344 + unsigned long flags; 1345 + int result = POLLOUT; 1249 1346 1250 - TRACE_L("POLL"); 1347 + TRACE_L("POLL"); 1251 1348 1252 - pClient=findClient(pInfo, task_pid(current)); 1253 - if(pClient) 1254 - { 1255 - poll_wait(file, &pInfo->read_wait, wait); 1256 - spin_lock_irqsave(&pInfo->lock, flags); 1257 - pMsg=pClient->first_msg; 1258 - spin_unlock_irqrestore(&pInfo->lock, flags); 1259 - if(pMsg) 1260 - result |= POLLIN | POLLRDNORM; 1261 - } 1262 - else 1263 - { 1264 - result = -EINVAL; 1265 - } 1266 - return result; 1349 + pClient = findClient(pInfo, task_pid(current)); 1350 + if (pClient) { 1351 + poll_wait(file, &pInfo->read_wait, wait); 1352 + spin_lock_irqsave(&pInfo->lock, flags); 1353 + pMsg = pClient->first_msg; 1354 + spin_unlock_irqrestore(&pInfo->lock, flags); 1355 + if (pMsg) 1356 + result |= POLLIN | POLLRDNORM; 1357 + } else { 1358 + result = -EINVAL; 1359 + } 1360 + return result; 1267 1361 } 1268 1362 1269 1363 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp, 1270 - char *fp, int count) 1364 + char *fp, int count) 1271 1365 { 1272 - struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; 1273 - const unsigned char *p; 1274 - char *f, flags = 0; 1275 - int i; 1366 + struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data; 1367 + const unsigned char *p; 1368 + char *f, flags = 0; 1369 + int i; 1276 1370 1277 - for (i=count, p = cp, f = fp; i; i--, p++) { 1278 - if (f) 1279 - flags = *f++; 1280 - if(flags==TTY_NORMAL) 1281 - { 1282 - receive_char(pInfo, *p); 1283 - } 1284 - else 1285 - { 1286 - receive_error(pInfo, flags); 1287 - } 1288 - 1289 - } 1371 + for (i = count, p = cp, f = fp; i; i--, p++) { 1372 + if (f) 1373 + flags = *f++; 1374 + if (flags == TTY_NORMAL) { 1375 + receive_char(pInfo, *p); 1376 + } else { 1377 + receive_error(pInfo, flags); 1378 + } 1379 + 1380 + } 1290 1381 } 1291 1382 1292 1383 MODULE_LICENSE("GPL");