Commit ba738340352a108768f7ff2b81251093d0432205

Authored by Craig McQueen
1 parent 90a8691f

Simplify references to paired serial port.

Showing 1 changed file with 24 additions and 48 deletions
module/tty0tty.c
@@ -52,6 +52,12 @@ MODULE_LICENSE("GPL"); @@ -52,6 +52,12 @@ MODULE_LICENSE("GPL");
52 #define TINY_TTY_MAJOR 240 /* experimental range */ 52 #define TINY_TTY_MAJOR 240 /* experimental range */
53 #define TINY_TTY_MINORS 8 /* device number, always even*/ 53 #define TINY_TTY_MINORS 8 /* device number, always even*/
54 54
  55 +/* Serial ports are paired by adjacent index numbers -- 0 and 1, 2 and 3, 4 and 5.
  56 + * I.e. same upper bits of index number; only different least-significant-bit.
  57 + * XOR least-significant-bit to find index of paired serial port.
  58 + */
  59 +#define PAIRED_INDEX(INDEX) ((INDEX) ^ 1)
  60 +
55 /* fake UART values */ 61 /* fake UART values */
56 //out 62 //out
57 #define MCR_DTR 0x01 63 #define MCR_DTR 0x01
@@ -85,6 +91,7 @@ static int tty0tty_open(struct tty_struct *tty, struct file *file) @@ -85,6 +91,7 @@ static int tty0tty_open(struct tty_struct *tty, struct file *file)
85 { 91 {
86 struct tty0tty_serial *tty0tty; 92 struct tty0tty_serial *tty0tty;
87 int index; 93 int index;
  94 + int paired_index;
88 int msr=0; 95 int msr=0;
89 int mcr=0; 96 int mcr=0;
90 97
@@ -96,6 +103,7 @@ static int tty0tty_open(struct tty_struct *tty, struct file *file) @@ -96,6 +103,7 @@ static int tty0tty_open(struct tty_struct *tty, struct file *file)
96 103
97 /* get the serial object associated with this tty pointer */ 104 /* get the serial object associated with this tty pointer */
98 index = tty->index; 105 index = tty->index;
  106 + paired_index = PAIRED_INDEX(index);
99 tty0tty = tty0tty_table[index]; 107 tty0tty = tty0tty_table[index];
100 if (tty0tty == NULL) { 108 if (tty0tty == NULL) {
101 /* first time accessing this device, let's create it */ 109 /* first time accessing this device, let's create it */
@@ -114,18 +122,9 @@ static int tty0tty_open(struct tty_struct *tty, struct file *file) @@ -114,18 +122,9 @@ static int tty0tty_open(struct tty_struct *tty, struct file *file)
114 122
115 } 123 }
116 124
117 - if( (index % 2) == 0)  
118 - {  
119 - if(tty0tty_table[index+1] != NULL)  
120 - if (tty0tty_table[index+1]->open_count > 0)  
121 - mcr=tty0tty_table[index+1]->mcr;  
122 - }  
123 - else  
124 - {  
125 - if(tty0tty_table[index-1] != NULL)  
126 - if (tty0tty_table[index-1]->open_count > 0)  
127 - mcr=tty0tty_table[index-1]->mcr;  
128 - } 125 + if (tty0tty_table[paired_index] != NULL &&
  126 + tty0tty_table[paired_index]->open_count > 0)
  127 + mcr = tty0tty_table[paired_index]->mcr;
129 128
130 //null modem connection 129 //null modem connection
131 130
@@ -188,6 +187,7 @@ static int tty0tty_write(struct tty_struct *tty, const unsigned char *buffer, in @@ -188,6 +187,7 @@ static int tty0tty_write(struct tty_struct *tty, const unsigned char *buffer, in
188 struct tty0tty_serial *tty0tty = tty->driver_data; 187 struct tty0tty_serial *tty0tty = tty->driver_data;
189 int retval = -EINVAL; 188 int retval = -EINVAL;
190 struct tty_struct *ttyx = NULL; 189 struct tty_struct *ttyx = NULL;
  190 + int paired_index;
191 191
192 if (!tty0tty) 192 if (!tty0tty)
193 return -ENODEV; 193 return -ENODEV;
@@ -198,18 +198,10 @@ static int tty0tty_write(struct tty_struct *tty, const unsigned char *buffer, in @@ -198,18 +198,10 @@ static int tty0tty_write(struct tty_struct *tty, const unsigned char *buffer, in
198 /* port was not opened */ 198 /* port was not opened */
199 goto exit; 199 goto exit;
200 200
201 - if( (tty0tty->tty->index % 2) == 0)  
202 - {  
203 - if(tty0tty_table[tty0tty->tty->index+1] != NULL)  
204 - if (tty0tty_table[tty0tty->tty->index+1]->open_count > 0)  
205 - ttyx=tty0tty_table[tty0tty->tty->index+1]->tty;  
206 - }  
207 - else  
208 - {  
209 - if(tty0tty_table[tty0tty->tty->index-1] != NULL)  
210 - if (tty0tty_table[tty0tty->tty->index-1]->open_count > 0)  
211 - ttyx=tty0tty_table[tty0tty->tty->index-1]->tty;  
212 - } 201 + paired_index = PAIRED_INDEX(tty0tty->tty->index);
  202 + if (tty0tty_table[paired_index] != NULL &&
  203 + tty0tty_table[paired_index]->open_count > 0)
  204 + ttyx = tty0tty_table[paired_index]->tty;
213 205
214 // tty->low_latency=1; 206 // tty->low_latency=1;
215 207
@@ -364,23 +356,16 @@ static int tty0tty_tiocmset(struct tty_struct *tty, struct file *file, @@ -364,23 +356,16 @@ static int tty0tty_tiocmset(struct tty_struct *tty, struct file *file,
364 struct tty0tty_serial *tty0tty = tty->driver_data; 356 struct tty0tty_serial *tty0tty = tty->driver_data;
365 unsigned int mcr = tty0tty->mcr; 357 unsigned int mcr = tty0tty->mcr;
366 unsigned int msr=0; 358 unsigned int msr=0;
  359 + int paired_index;
367 360
368 #ifdef SCULL_DEBUG 361 #ifdef SCULL_DEBUG
369 printk(KERN_DEBUG "%s - \n", __FUNCTION__); 362 printk(KERN_DEBUG "%s - \n", __FUNCTION__);
370 #endif 363 #endif
371 364
372 - if( (tty0tty->tty->index % 2) == 0)  
373 - {  
374 - if(tty0tty_table[tty0tty->tty->index+1] != NULL)  
375 - if (tty0tty_table[tty0tty->tty->index+1]->open_count > 0)  
376 - msr=tty0tty_table[tty0tty->tty->index+1]->msr;  
377 - }  
378 - else  
379 - {  
380 - if(tty0tty_table[tty0tty->tty->index-1] != NULL)  
381 - if (tty0tty_table[tty0tty->tty->index-1]->open_count > 0)  
382 - msr=tty0tty_table[tty0tty->tty->index-1]->msr;  
383 - } 365 + paired_index = PAIRED_INDEX(tty0tty->tty->index);
  366 + if (tty0tty_table[paired_index] != NULL &&
  367 + tty0tty_table[paired_index]->open_count > 0)
  368 + msr = tty0tty_table[paired_index]->msr;
384 369
385 //null modem connection 370 //null modem connection
386 371
@@ -414,18 +399,9 @@ static int tty0tty_tiocmset(struct tty_struct *tty, struct file *file, @@ -414,18 +399,9 @@ static int tty0tty_tiocmset(struct tty_struct *tty, struct file *file,
414 /* set the new MCR value in the device */ 399 /* set the new MCR value in the device */
415 tty0tty->mcr = mcr; 400 tty0tty->mcr = mcr;
416 401
417 - if( (tty0tty->tty->index % 2) == 0)  
418 - {  
419 - if(tty0tty_table[tty0tty->tty->index+1] != NULL)  
420 - if (tty0tty_table[tty0tty->tty->index+1]->open_count > 0)  
421 - tty0tty_table[tty0tty->tty->index+1]->msr=msr;  
422 - }  
423 - else  
424 - {  
425 - if(tty0tty_table[tty0tty->tty->index-1] != NULL)  
426 - if (tty0tty_table[tty0tty->tty->index-1]->open_count > 0)  
427 - tty0tty_table[tty0tty->tty->index-1]->msr=msr;  
428 - } 402 + if (tty0tty_table[paired_index] != NULL &&
  403 + tty0tty_table[paired_index]->open_count > 0)
  404 + tty0tty_table[paired_index]->msr = msr;
429 return 0; 405 return 0;
430 } 406 }
431 407