Commit d92cb7bed28fe6e0d05c4991127b401da8593666

Authored by Craig McQueen
1 parent f4800a02

Move hardware handshaking null-modem connections into a single function.

Showing 1 changed file with 54 additions and 50 deletions
module/tty0tty.c
... ... @@ -63,6 +63,8 @@ MODULE_LICENSE("GPL");
63 63 #define MCR_DTR 0x01
64 64 #define MCR_RTS 0x02
65 65 #define MCR_LOOP 0x04
  66 +#define MCR_OUT1 0x08
  67 +#define MCR_OUT2 0x10
66 68 //in
67 69 #define MSR_CTS 0x10
68 70 #define MSR_CD 0x20
... ... @@ -87,13 +89,36 @@ struct tty0tty_serial {
87 89 static struct tty0tty_serial *tty0tty_table[TINY_TTY_MINORS]; /* initially all NULL */
88 90  
89 91  
  92 +static inline void null_modem_signal_copy(struct tty0tty_serial * tty_to, const struct tty0tty_serial * tty_from)
  93 +{
  94 + unsigned int msr_to = 0;
  95 + unsigned int mcr_from = 0;
  96 +
  97 + if (tty_to != NULL && tty_to->open_count > 0) {
  98 + if (tty_from != NULL && tty_from->open_count > 0) {
  99 + mcr_from = tty_from->mcr;
  100 + }
  101 + msr_to = tty_to->msr & ~(MSR_CD | MSR_CTS | MSR_DSR | MSR_RI);
  102 +
  103 + /* RTS --> CTS */
  104 + if (mcr_from & MCR_RTS) {
  105 + msr_to |= MSR_CTS;
  106 + }
  107 + /* DTR --> DSR and DCD */
  108 + if (mcr_from & MCR_DTR) {
  109 + msr_to |= MSR_DSR;
  110 + msr_to |= MSR_CD;
  111 + }
  112 +
  113 + tty_to->msr = msr_to;
  114 + }
  115 +}
  116 +
90 117 static int tty0tty_open(struct tty_struct *tty, struct file *file)
91 118 {
92 119 struct tty0tty_serial *tty0tty;
93 120 int index;
94 121 int paired_index;
95   - int msr=0;
96   - int mcr=0;
97 122  
98 123 #ifdef SCULL_DEBUG
99 124 printk(KERN_DEBUG "%s - \n", __FUNCTION__);
... ... @@ -122,25 +147,6 @@ static int tty0tty_open(struct tty_struct *tty, struct file *file)
122 147  
123 148 }
124 149  
125   - if (tty0tty_table[paired_index] != NULL &&
126   - tty0tty_table[paired_index]->open_count > 0)
127   - mcr = tty0tty_table[paired_index]->mcr;
128   -
129   -//null modem connection
130   -
131   - if( (mcr & MCR_RTS) == MCR_RTS )
132   - {
133   - msr |= MSR_CTS;
134   - }
135   -
136   - if( (mcr & MCR_DTR) == MCR_DTR )
137   - {
138   - msr |= MSR_DSR;
139   - msr |= MSR_CD;
140   - }
141   -
142   - tty0tty->msr = msr;
143   -
144 150 down(&tty0tty->sem);
145 151  
146 152 /* save our structure within the tty structure */
... ... @@ -149,6 +155,8 @@ static int tty0tty_open(struct tty_struct *tty, struct file *file)
149 155  
150 156 ++tty0tty->open_count;
151 157  
  158 + null_modem_signal_copy(tty0tty, tty0tty_table[paired_index]);
  159 +
152 160 up(&tty0tty->sem);
153 161 return 0;
154 162 }
... ... @@ -339,9 +347,11 @@ static int tty0tty_tiocmget(struct tty_struct *tty, struct file *file)
339 347 unsigned int mcr = tty0tty->mcr;
340 348  
341 349  
342   - result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) | /* DTR is set */
  350 + result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) | /* DTR is set */
343 351 ((mcr & MCR_RTS) ? TIOCM_RTS : 0) | /* RTS is set */
344 352 ((mcr & MCR_LOOP) ? TIOCM_LOOP : 0) | /* LOOP is set */
  353 + ((mcr & MCR_OUT1) ? TIOCM_OUT1 : 0) | /* OUT1 is set */
  354 + ((mcr & MCR_OUT2) ? TIOCM_OUT2 : 0) | /* OUT2 is set */
345 355 ((msr & MSR_CTS) ? TIOCM_CTS : 0) | /* CTS is set */
346 356 ((msr & MSR_CD) ? TIOCM_CAR : 0) | /* Carrier detect is set*/
347 357 ((msr & MSR_RI) ? TIOCM_RI : 0) | /* Ring Indicator is set */
... ... @@ -355,53 +365,47 @@ static int tty0tty_tiocmset(struct tty_struct *tty, struct file *file,
355 365 {
356 366 struct tty0tty_serial *tty0tty = tty->driver_data;
357 367 unsigned int mcr = tty0tty->mcr;
358   - unsigned int msr=0;
359 368 int paired_index;
360 369  
361 370 #ifdef SCULL_DEBUG
362 371 printk(KERN_DEBUG "%s - \n", __FUNCTION__);
363 372 #endif
364 373  
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;
369   -
370   -//null modem connection
371   -
372   - if (set & TIOCM_RTS)
373   - {
  374 + /* Set bits */
  375 + if (set & TIOCM_RTS) {
374 376 mcr |= MCR_RTS;
375   - msr |= MSR_CTS;
376 377 }
377   -
378   - if (set & TIOCM_DTR)
379   - {
  378 + if (set & TIOCM_DTR) {
380 379 mcr |= MCR_DTR;
381   - msr |= MSR_DSR;
382   - msr |= MSR_CD;
  380 + }
  381 + if (set & TIOCM_OUT1) {
  382 + mcr |= MCR_OUT1;
  383 + }
  384 + if (set & TIOCM_OUT2) {
  385 + mcr |= MCR_OUT2;
383 386 }
384 387  
385   - if (clear & TIOCM_RTS)
386   - {
  388 + /* Clear bits */
  389 + if (clear & TIOCM_RTS) {
387 390 mcr &= ~MCR_RTS;
388   - msr &= ~MSR_CTS;
389 391 }
390   -
391   - if (clear & TIOCM_DTR)
392   - {
  392 + if (clear & TIOCM_DTR) {
393 393 mcr &= ~MCR_DTR;
394   - msr &= ~MSR_DSR;
395   - msr &= ~MSR_CD;
396 394 }
397   -
  395 + if (clear & TIOCM_OUT1) {
  396 + mcr &= ~MCR_OUT1;
  397 + }
  398 + if (clear & TIOCM_OUT2) {
  399 + mcr &= ~MCR_OUT2;
  400 + }
398 401  
399 402 /* set the new MCR value in the device */
400 403 tty0tty->mcr = mcr;
401 404  
402   - if (tty0tty_table[paired_index] != NULL &&
403   - tty0tty_table[paired_index]->open_count > 0)
404   - tty0tty_table[paired_index]->msr = msr;
  405 + //null modem connection
  406 + paired_index = PAIRED_INDEX(tty0tty->tty->index);
  407 + null_modem_signal_copy(tty0tty_table[paired_index], tty0tty);
  408 +
405 409 return 0;
406 410 }
407 411  
... ...