linux/drivers/staging/sb105x/sb_pci_mp.c
<<
>>
Prefs
   1#include "sb_pci_mp.h"
   2#include <linux/module.h>
   3#include <linux/parport.h>
   4
   5extern struct parport *parport_pc_probe_port(unsigned long base_lo,
   6                unsigned long base_hi,
   7                int irq, int dma,
   8                struct device *dev,
   9                int irqflags);
  10
  11static struct mp_device_t mp_devs[MAX_MP_DEV];
  12static int mp_nrpcibrds = sizeof(mp_pciboards)/sizeof(mppcibrd_t);
  13static int NR_BOARD=0;
  14static int NR_PORTS=0;
  15static struct mp_port multi_ports[MAX_MP_PORT];
  16static struct irq_info irq_lists[NR_IRQS];
  17
  18static _INLINE_ unsigned int serial_in(struct mp_port *mtpt, int offset);
  19static _INLINE_ void serial_out(struct mp_port *mtpt, int offset, int value);
  20static _INLINE_ unsigned int read_option_register(struct mp_port *mtpt, int offset);
  21static int sb1054_get_register(struct sb_uart_port *port, int page, int reg);
  22static int sb1054_set_register(struct sb_uart_port *port, int page, int reg, int value);
  23static void SendATCommand(struct mp_port *mtpt);
  24static int set_deep_fifo(struct sb_uart_port *port, int status);
  25static int get_deep_fifo(struct sb_uart_port *port);
  26static int get_device_type(int arg);
  27static int set_auto_rts(struct sb_uart_port *port, int status);
  28static void mp_stop(struct tty_struct *tty);
  29static void __mp_start(struct tty_struct *tty);
  30static void mp_start(struct tty_struct *tty);
  31static void mp_tasklet_action(unsigned long data);
  32static inline void mp_update_mctrl(struct sb_uart_port *port, unsigned int set, unsigned int clear);
  33static int mp_startup(struct sb_uart_state *state, int init_hw);
  34static void mp_shutdown(struct sb_uart_state *state);
  35static void mp_change_speed(struct sb_uart_state *state, struct MP_TERMIOS *old_termios);
  36
  37static inline int __mp_put_char(struct sb_uart_port *port, struct circ_buf *circ, unsigned char c);
  38static int mp_put_char(struct tty_struct *tty, unsigned char ch);
  39
  40static void mp_put_chars(struct tty_struct *tty);
  41static int mp_write(struct tty_struct *tty, const unsigned char *buf, int count);
  42static int mp_write_room(struct tty_struct *tty);
  43static int mp_chars_in_buffer(struct tty_struct *tty);
  44static void mp_flush_buffer(struct tty_struct *tty);
  45static void mp_send_xchar(struct tty_struct *tty, char ch);
  46static void mp_throttle(struct tty_struct *tty);
  47static void mp_unthrottle(struct tty_struct *tty);
  48static int mp_get_info(struct sb_uart_state *state, struct serial_struct *retinfo);
  49static int mp_set_info(struct sb_uart_state *state, struct serial_struct *newinfo);
  50static int mp_get_lsr_info(struct sb_uart_state *state, unsigned int *value);
  51
  52static int mp_tiocmget(struct tty_struct *tty);
  53static int mp_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
  54static int mp_break_ctl(struct tty_struct *tty, int break_state);
  55static int mp_do_autoconfig(struct sb_uart_state *state);
  56static int mp_wait_modem_status(struct sb_uart_state *state, unsigned long arg);
  57static int mp_get_count(struct sb_uart_state *state, struct serial_icounter_struct *icnt);
  58static int mp_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
  59static void mp_set_termios(struct tty_struct *tty, struct MP_TERMIOS *old_termios);
  60static void mp_close(struct tty_struct *tty, struct file *filp);
  61static void mp_wait_until_sent(struct tty_struct *tty, int timeout);
  62static void mp_hangup(struct tty_struct *tty);
  63static void mp_update_termios(struct sb_uart_state *state);
  64static int mp_block_til_ready(struct file *filp, struct sb_uart_state *state);
  65static struct sb_uart_state *uart_get(struct uart_driver *drv, int line);
  66static int mp_open(struct tty_struct *tty, struct file *filp);
  67static const char *mp_type(struct sb_uart_port *port);
  68static void mp_change_pm(struct sb_uart_state *state, int pm_state);
  69static inline void mp_report_port(struct uart_driver *drv, struct sb_uart_port *port);
  70static void mp_configure_port(struct uart_driver *drv, struct sb_uart_state *state, struct sb_uart_port *port);
  71static void mp_unconfigure_port(struct uart_driver *drv, struct sb_uart_state *state);
  72static int mp_register_driver(struct uart_driver *drv);
  73static void mp_unregister_driver(struct uart_driver *drv);
  74static int mp_add_one_port(struct uart_driver *drv, struct sb_uart_port *port);
  75static int mp_remove_one_port(struct uart_driver *drv, struct sb_uart_port *port);
  76static void autoconfig(struct mp_port *mtpt, unsigned int probeflags);
  77static void autoconfig_irq(struct mp_port *mtpt);
  78static void multi_stop_tx(struct sb_uart_port *port);
  79static void multi_start_tx(struct sb_uart_port *port);
  80static void multi_stop_rx(struct sb_uart_port *port);
  81static void multi_enable_ms(struct sb_uart_port *port);
  82static _INLINE_ void receive_chars(struct mp_port *mtpt, int *status );
  83static _INLINE_ void transmit_chars(struct mp_port *mtpt);
  84static _INLINE_ void check_modem_status(struct mp_port *mtpt);
  85static inline void multi_handle_port(struct mp_port *mtpt);
  86static irqreturn_t multi_interrupt(int irq, void *dev_id);
  87static void serial_do_unlink(struct irq_info *i, struct mp_port *mtpt);
  88static int serial_link_irq_chain(struct mp_port *mtpt);
  89static void serial_unlink_irq_chain(struct mp_port *mtpt);
  90static void multi_timeout(unsigned long data);
  91static unsigned int multi_tx_empty(struct sb_uart_port *port);
  92static unsigned int multi_get_mctrl(struct sb_uart_port *port);
  93static void multi_set_mctrl(struct sb_uart_port *port, unsigned int mctrl);
  94static void multi_break_ctl(struct sb_uart_port *port, int break_state);
  95static int multi_startup(struct sb_uart_port *port);
  96static void multi_shutdown(struct sb_uart_port *port);
  97static unsigned int multi_get_divisor(struct sb_uart_port *port, unsigned int baud);
  98static void multi_set_termios(struct sb_uart_port *port, struct MP_TERMIOS *termios, struct MP_TERMIOS *old);
  99static void multi_pm(struct sb_uart_port *port, unsigned int state, unsigned int oldstate);
 100static void multi_release_std_resource(struct mp_port *mtpt);
 101static void multi_release_port(struct sb_uart_port *port);
 102static int multi_request_port(struct sb_uart_port *port);
 103static void multi_config_port(struct sb_uart_port *port, int flags);
 104static int multi_verify_port(struct sb_uart_port *port, struct serial_struct *ser);
 105static const char *multi_type(struct sb_uart_port *port);
 106static void __init multi_init_ports(void);
 107static void __init multi_register_ports(struct uart_driver *drv);
 108static int init_mp_dev(struct pci_dev *pcidev, mppcibrd_t brd);
 109
 110static int deep[256];
 111static int deep_count;
 112static int fcr_arr[256];
 113static int fcr_count;
 114static int ttr[256];
 115static int ttr_count;
 116static int rtr[256];
 117static int rtr_count;
 118
 119module_param_array(deep,int,&deep_count,0);
 120module_param_array(fcr_arr,int,&fcr_count,0);
 121module_param_array(ttr,int,&ttr_count,0);
 122module_param_array(rtr,int,&rtr_count,0);
 123
 124static _INLINE_ unsigned int serial_in(struct mp_port *mtpt, int offset)
 125{
 126        return inb(mtpt->port.iobase + offset);
 127}
 128
 129static _INLINE_ void serial_out(struct mp_port *mtpt, int offset, int value)
 130{
 131        outb(value, mtpt->port.iobase + offset);
 132}
 133
 134static _INLINE_ unsigned int read_option_register(struct mp_port *mtpt, int offset)
 135{
 136        return inb(mtpt->option_base_addr + offset);
 137}
 138
 139static int sb1053a_get_interface(struct mp_port *mtpt, int port_num)
 140{
 141        unsigned long option_base_addr = mtpt->option_base_addr;
 142        unsigned int  interface = 0;
 143
 144        switch (port_num)
 145        {
 146                case 0:
 147                case 1:
 148                        /* set GPO[1:0] = 00 */
 149                        outb(0x00, option_base_addr + MP_OPTR_GPODR);
 150                        break;
 151                case 2:
 152                case 3:
 153                        /* set GPO[1:0] = 01 */
 154                        outb(0x01, option_base_addr + MP_OPTR_GPODR);
 155                        break;
 156                case 4:
 157                case 5:
 158                        /* set GPO[1:0] = 10 */
 159                        outb(0x02, option_base_addr + MP_OPTR_GPODR);
 160                        break;
 161                default:
 162                        break;
 163        }
 164
 165        port_num &= 0x1;
 166
 167        /* get interface */
 168        interface = inb(option_base_addr + MP_OPTR_IIR0 + port_num);
 169
 170        /* set GPO[1:0] = 11 */
 171        outb(0x03, option_base_addr + MP_OPTR_GPODR);
 172
 173        return (interface);
 174}
 175                
 176static int sb1054_get_register(struct sb_uart_port *port, int page, int reg)
 177{
 178        int ret = 0;
 179        unsigned int lcr = 0;
 180        unsigned int mcr = 0;
 181        unsigned int tmp = 0;
 182
 183        if( page <= 0)
 184        {
 185                printk(" page 0 can not use this fuction\n");
 186                return -1;
 187        }
 188
 189        switch(page)
 190        {
 191                case 1:
 192                        lcr = SB105X_GET_LCR(port);
 193                        tmp = lcr | SB105X_LCR_DLAB;
 194                        SB105X_PUT_LCR(port, tmp);
 195
 196                        tmp = SB105X_GET_LCR(port);
 197
 198                        ret = SB105X_GET_REG(port,reg);
 199                        SB105X_PUT_LCR(port,lcr);
 200                        break;
 201                case 2:
 202                        mcr = SB105X_GET_MCR(port);
 203                        tmp = mcr | SB105X_MCR_P2S;
 204                        SB105X_PUT_MCR(port,tmp);
 205
 206                        ret = SB105X_GET_REG(port,reg);
 207
 208                        SB105X_PUT_MCR(port,mcr);
 209                        break;
 210                case 3:
 211                        lcr = SB105X_GET_LCR(port);
 212                        tmp = lcr | SB105X_LCR_BF;
 213                        SB105X_PUT_LCR(port,tmp);
 214                        SB105X_PUT_REG(port,SB105X_PSR,SB105X_PSR_P3KEY);
 215
 216                        ret = SB105X_GET_REG(port,reg);
 217
 218                        SB105X_PUT_LCR(port,lcr);
 219                        break;
 220                case 4:
 221                        lcr = SB105X_GET_LCR(port);
 222                        tmp = lcr | SB105X_LCR_BF;
 223                        SB105X_PUT_LCR(port,tmp);
 224                        SB105X_PUT_REG(port,SB105X_PSR,SB105X_PSR_P4KEY);
 225
 226                        ret = SB105X_GET_REG(port,reg);
 227
 228                        SB105X_PUT_LCR(port,lcr);
 229                        break;
 230                default:
 231                        printk(" error invalid page number \n");
 232                        return -1;
 233        }
 234
 235        return ret;
 236}
 237
 238static int sb1054_set_register(struct sb_uart_port *port, int page, int reg, int value)
 239{  
 240        int lcr = 0;
 241        int mcr = 0;
 242        int ret = 0;
 243
 244        if( page <= 0)
 245        {
 246                printk(" page 0 can not use this fuction\n");
 247                return -1;
 248        }
 249        switch(page)
 250        {
 251                case 1:
 252                        lcr = SB105X_GET_LCR(port);
 253                        SB105X_PUT_LCR(port, lcr | SB105X_LCR_DLAB);
 254
 255                        SB105X_PUT_REG(port,reg,value);
 256
 257                        SB105X_PUT_LCR(port, lcr);
 258                        ret = 1;
 259                        break;
 260                case 2:
 261                        mcr = SB105X_GET_MCR(port);
 262                        SB105X_PUT_MCR(port, mcr | SB105X_MCR_P2S);
 263
 264                        SB105X_PUT_REG(port,reg,value);
 265
 266                        SB105X_PUT_MCR(port, mcr);
 267                        ret = 1;
 268                        break;
 269                case 3:
 270                        lcr = SB105X_GET_LCR(port);
 271                        SB105X_PUT_LCR(port, lcr | SB105X_LCR_BF);
 272                        SB105X_PUT_PSR(port, SB105X_PSR_P3KEY);
 273
 274                        SB105X_PUT_REG(port,reg,value);
 275
 276                        SB105X_PUT_LCR(port, lcr);
 277                        ret = 1;
 278                        break;
 279                case 4:
 280                        lcr = SB105X_GET_LCR(port);
 281                        SB105X_PUT_LCR(port, lcr | SB105X_LCR_BF);
 282                        SB105X_PUT_PSR(port, SB105X_PSR_P4KEY);
 283
 284                        SB105X_PUT_REG(port,reg,value);
 285
 286                        SB105X_PUT_LCR(port, lcr);
 287                        ret = 1;
 288                        break;
 289                default:
 290                        printk(" error invalid page number \n");
 291                        return -1;
 292        }
 293
 294        return ret;
 295}
 296
 297static int set_multidrop_mode(struct sb_uart_port *port, unsigned int mode)
 298{
 299        int mdr = SB105XA_MDR_NPS;
 300
 301        if (mode & MDMODE_ENABLE)
 302        {
 303                mdr |= SB105XA_MDR_MDE;
 304        }
 305
 306        if (1) //(mode & MDMODE_AUTO)
 307        {
 308                int efr = 0;
 309                mdr |= SB105XA_MDR_AME;
 310                efr = sb1054_get_register(port, PAGE_3, SB105X_EFR);
 311                efr |= SB105X_EFR_SCD;
 312                sb1054_set_register(port, PAGE_3, SB105X_EFR, efr);
 313        }
 314
 315        sb1054_set_register(port, PAGE_1, SB105XA_MDR, mdr);
 316        port->mdmode &= ~0x6;
 317        port->mdmode |= mode;
 318        printk("[%d] multidrop init: %x\n", port->line, port->mdmode);
 319
 320        return 0;
 321}
 322
 323static int get_multidrop_addr(struct sb_uart_port *port)
 324{
 325        return sb1054_get_register(port, PAGE_3, SB105X_XOFF2);
 326}
 327
 328static int set_multidrop_addr(struct sb_uart_port *port, unsigned int addr)
 329{
 330        sb1054_set_register(port, PAGE_3, SB105X_XOFF2, addr);
 331
 332        return 0;
 333}
 334
 335static void SendATCommand(struct mp_port *mtpt)
 336{
 337        //                    a    t    cr   lf
 338        unsigned char ch[] = {0x61,0x74,0x0d,0x0a,0x0};
 339        unsigned char lineControl;
 340        unsigned char i=0;
 341        unsigned char Divisor = 0xc;
 342
 343        lineControl = serial_inp(mtpt,UART_LCR);
 344        serial_outp(mtpt,UART_LCR,(lineControl | UART_LCR_DLAB));
 345        serial_outp(mtpt,UART_DLL,(Divisor & 0xff));
 346        serial_outp(mtpt,UART_DLM,(Divisor & 0xff00)>>8); //baudrate is 4800
 347
 348
 349        serial_outp(mtpt,UART_LCR,lineControl); 
 350        serial_outp(mtpt,UART_LCR,0x03); // N-8-1
 351        serial_outp(mtpt,UART_FCR,7); 
 352        serial_outp(mtpt,UART_MCR,0x3);
 353        while(ch[i]){
 354                while((serial_inp(mtpt,UART_LSR) & 0x60) !=0x60){
 355                        ;
 356                }
 357                serial_outp(mtpt,0,ch[i++]);
 358        }
 359
 360
 361}// end of SendATCommand()
 362
 363static int set_deep_fifo(struct sb_uart_port *port, int status)
 364{
 365        int afr_status = 0;
 366        afr_status = sb1054_get_register(port, PAGE_4, SB105X_AFR);
 367
 368        if(status == ENABLE)
 369        {
 370                afr_status |= SB105X_AFR_AFEN;
 371        }
 372        else
 373        {
 374                afr_status &= ~SB105X_AFR_AFEN;
 375        }
 376                
 377        sb1054_set_register(port,PAGE_4,SB105X_AFR,afr_status);
 378        sb1054_set_register(port,PAGE_4,SB105X_TTR,ttr[port->line]); 
 379        sb1054_set_register(port,PAGE_4,SB105X_RTR,rtr[port->line]); 
 380        afr_status = sb1054_get_register(port, PAGE_4, SB105X_AFR);
 381                
 382        return afr_status;
 383}
 384
 385static int get_device_type(int arg)
 386{
 387        int ret;
 388        ret = inb(mp_devs[arg].option_reg_addr+MP_OPTR_DIR0);
 389        ret = (ret & 0xf0) >> 4;
 390        switch (ret)
 391        {
 392               case DIR_UART_16C550:
 393                    return PORT_16C55X;
 394               case DIR_UART_16C1050:
 395                    return PORT_16C105X;
 396               case DIR_UART_16C1050A:
 397               /*
 398               if (mtpt->port.line < 2)
 399               {
 400                    return PORT_16C105XA;
 401               }
 402               else
 403               {
 404                   if (mtpt->device->device_id & 0x50)
 405                   {
 406                       return PORT_16C55X;
 407                   }
 408                   else
 409                   {
 410                       return PORT_16C105X;
 411                   }
 412               }*/
 413               return PORT_16C105XA;
 414               default:
 415                    return PORT_UNKNOWN;
 416        }
 417
 418}
 419static int get_deep_fifo(struct sb_uart_port *port)
 420{
 421        int afr_status = 0;
 422        afr_status = sb1054_get_register(port, PAGE_4, SB105X_AFR);
 423        return afr_status;
 424}
 425
 426static int set_auto_rts(struct sb_uart_port *port, int status)
 427{
 428        int atr_status = 0;
 429
 430#if 0
 431        int efr_status = 0;
 432
 433        efr_status = sb1054_get_register(port, PAGE_3, SB105X_EFR);
 434        if(status == ENABLE)
 435                efr_status |= SB105X_EFR_ARTS;
 436        else
 437                efr_status &= ~SB105X_EFR_ARTS;
 438        sb1054_set_register(port,PAGE_3,SB105X_EFR,efr_status);
 439        efr_status = sb1054_get_register(port, PAGE_3, SB105X_EFR);
 440#endif
 441                
 442//ATR
 443        atr_status = sb1054_get_register(port, PAGE_3, SB105X_ATR);
 444        switch(status)
 445        {
 446                case RS422PTP:
 447                        atr_status = (SB105X_ATR_TPS) | (SB105X_ATR_A80);
 448                        break;
 449                case RS422MD:
 450                        atr_status = (SB105X_ATR_TPS) | (SB105X_ATR_TCMS) | (SB105X_ATR_A80);
 451                        break;
 452                case RS485NE:
 453                        atr_status = (SB105X_ATR_RCMS) | (SB105X_ATR_TPS) | (SB105X_ATR_TCMS) | (SB105X_ATR_A80);
 454                        break;
 455                case RS485ECHO:
 456                        atr_status = (SB105X_ATR_TPS) | (SB105X_ATR_TCMS) | (SB105X_ATR_A80);
 457                        break;
 458        }
 459
 460        sb1054_set_register(port,PAGE_3,SB105X_ATR,atr_status);
 461        atr_status = sb1054_get_register(port, PAGE_3, SB105X_ATR);
 462
 463        return atr_status;
 464}
 465
 466static void mp_stop(struct tty_struct *tty)
 467{
 468        struct sb_uart_state *state = tty->driver_data;
 469        struct sb_uart_port *port = state->port;
 470        unsigned long flags;
 471
 472        spin_lock_irqsave(&port->lock, flags);
 473        port->ops->stop_tx(port);
 474        spin_unlock_irqrestore(&port->lock, flags);
 475}
 476
 477static void __mp_start(struct tty_struct *tty)
 478{
 479        struct sb_uart_state *state = tty->driver_data;
 480        struct sb_uart_port *port = state->port;
 481
 482        if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
 483                        !tty->stopped && !tty->hw_stopped)
 484                port->ops->start_tx(port);
 485}
 486
 487static void mp_start(struct tty_struct *tty)
 488{
 489        __mp_start(tty);
 490}
 491
 492static void mp_tasklet_action(unsigned long data)
 493{
 494        struct sb_uart_state *state = (struct sb_uart_state *)data;
 495        struct tty_struct *tty;
 496
 497        printk("tasklet is called!\n");
 498        tty = state->info->tty;
 499        tty_wakeup(tty);
 500}
 501
 502static inline void mp_update_mctrl(struct sb_uart_port *port, unsigned int set, unsigned int clear)
 503{
 504        unsigned int old;
 505
 506        old = port->mctrl;
 507        port->mctrl = (old & ~clear) | set;
 508        if (old != port->mctrl)
 509                port->ops->set_mctrl(port, port->mctrl);
 510}
 511
 512#define uart_set_mctrl(port,set)        mp_update_mctrl(port,set,0)
 513#define uart_clear_mctrl(port,clear)    mp_update_mctrl(port,0,clear)
 514
 515static int mp_startup(struct sb_uart_state *state, int init_hw)
 516{
 517        struct sb_uart_info *info = state->info;
 518        struct sb_uart_port *port = state->port;
 519        unsigned long page;
 520        int retval = 0;
 521
 522        if (info->flags & UIF_INITIALIZED)
 523                return 0;
 524
 525        if (info->tty)
 526                set_bit(TTY_IO_ERROR, &info->tty->flags);
 527
 528        if (port->type == PORT_UNKNOWN)
 529                return 0;
 530
 531        if (!info->xmit.buf) {
 532                page = get_zeroed_page(GFP_KERNEL);
 533                if (!page)
 534                        return -ENOMEM;
 535
 536                info->xmit.buf = (unsigned char *) page;
 537                        
 538                uart_circ_clear(&info->xmit);
 539        }
 540
 541        retval = port->ops->startup(port);
 542        if (retval == 0) {
 543                if (init_hw) {
 544                        mp_change_speed(state, NULL);
 545
 546                        if (info->tty->termios.c_cflag & CBAUD)
 547                                uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
 548                }
 549
 550                info->flags |= UIF_INITIALIZED;
 551
 552
 553                clear_bit(TTY_IO_ERROR, &info->tty->flags);
 554        }
 555
 556        if (retval && capable(CAP_SYS_ADMIN))
 557                retval = 0;
 558
 559        return retval;
 560}
 561
 562static void mp_shutdown(struct sb_uart_state *state)
 563{
 564        struct sb_uart_info *info = state->info;
 565        struct sb_uart_port *port = state->port;
 566
 567        if (info->tty)
 568                set_bit(TTY_IO_ERROR, &info->tty->flags);
 569
 570        if (info->flags & UIF_INITIALIZED) {
 571                info->flags &= ~UIF_INITIALIZED;
 572
 573                if (!info->tty || (info->tty->termios.c_cflag & HUPCL))
 574                        uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
 575
 576                wake_up_interruptible(&info->delta_msr_wait);
 577
 578                port->ops->shutdown(port);
 579
 580                synchronize_irq(port->irq);
 581        }
 582        tasklet_kill(&info->tlet);
 583
 584        if (info->xmit.buf) {
 585                free_page((unsigned long)info->xmit.buf);
 586                info->xmit.buf = NULL;
 587        }
 588}
 589
 590static void mp_change_speed(struct sb_uart_state *state, struct MP_TERMIOS *old_termios)
 591{
 592        struct tty_struct *tty = state->info->tty;
 593        struct sb_uart_port *port = state->port;
 594
 595        if (!tty || port->type == PORT_UNKNOWN)
 596                return;
 597
 598        if (tty->termios.c_cflag & CRTSCTS)
 599                state->info->flags |= UIF_CTS_FLOW;
 600        else
 601                state->info->flags &= ~UIF_CTS_FLOW;
 602
 603        if (tty->termios.c_cflag & CLOCAL)
 604                state->info->flags &= ~UIF_CHECK_CD;
 605        else
 606                state->info->flags |= UIF_CHECK_CD;
 607
 608        port->ops->set_termios(port, &tty->termios, old_termios);
 609}
 610
 611static inline int __mp_put_char(struct sb_uart_port *port, struct circ_buf *circ, unsigned char c)
 612{
 613        unsigned long flags;
 614        int ret = 0;
 615
 616        if (!circ->buf)
 617                return 0;
 618
 619        spin_lock_irqsave(&port->lock, flags);
 620        if (uart_circ_chars_free(circ) != 0) {
 621                circ->buf[circ->head] = c;
 622                circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
 623                ret = 1;
 624        }
 625        spin_unlock_irqrestore(&port->lock, flags);
 626        return ret;
 627}
 628
 629static int mp_put_char(struct tty_struct *tty, unsigned char ch)
 630{
 631        struct sb_uart_state *state = tty->driver_data;
 632
 633        return __mp_put_char(state->port, &state->info->xmit, ch);
 634}
 635
 636static void mp_put_chars(struct tty_struct *tty)
 637{
 638        mp_start(tty);
 639}
 640
 641static int mp_write(struct tty_struct *tty, const unsigned char *buf, int count)
 642{
 643        struct sb_uart_state *state = tty->driver_data;
 644        struct sb_uart_port *port;
 645        struct circ_buf *circ;
 646        int c, ret = 0;
 647
 648        if (!state || !state->info) {
 649                return -EL3HLT;
 650        }
 651
 652        port = state->port;
 653        circ = &state->info->xmit;
 654
 655        if (!circ->buf)
 656                return 0;
 657                
 658        while (1) {
 659                c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
 660                if (count < c)
 661                        c = count;
 662                if (c <= 0)
 663                        break;
 664        memcpy(circ->buf + circ->head, buf, c);
 665
 666                circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
 667                buf += c;
 668                count -= c;
 669                ret += c;
 670        }
 671        mp_start(tty);
 672        return ret;
 673}
 674
 675static int mp_write_room(struct tty_struct *tty)
 676{
 677        struct sb_uart_state *state = tty->driver_data;
 678
 679        return uart_circ_chars_free(&state->info->xmit);
 680}
 681
 682static int mp_chars_in_buffer(struct tty_struct *tty)
 683{
 684        struct sb_uart_state *state = tty->driver_data;
 685
 686        return uart_circ_chars_pending(&state->info->xmit);
 687}
 688
 689static void mp_flush_buffer(struct tty_struct *tty)
 690{
 691        struct sb_uart_state *state = tty->driver_data;
 692        struct sb_uart_port *port;
 693        unsigned long flags;
 694
 695        if (!state || !state->info) {
 696                return;
 697        }
 698
 699        port = state->port;
 700        spin_lock_irqsave(&port->lock, flags);
 701        uart_circ_clear(&state->info->xmit);
 702        spin_unlock_irqrestore(&port->lock, flags);
 703        wake_up_interruptible(&tty->write_wait);
 704        tty_wakeup(tty);
 705}
 706
 707static void mp_send_xchar(struct tty_struct *tty, char ch)
 708{
 709        struct sb_uart_state *state = tty->driver_data;
 710        struct sb_uart_port *port = state->port;
 711        unsigned long flags;
 712
 713        if (port->ops->send_xchar)
 714                port->ops->send_xchar(port, ch);
 715        else {
 716                port->x_char = ch;
 717                if (ch) {
 718                        spin_lock_irqsave(&port->lock, flags);
 719                        port->ops->start_tx(port);
 720                        spin_unlock_irqrestore(&port->lock, flags);
 721                }
 722        }
 723}
 724
 725static void mp_throttle(struct tty_struct *tty)
 726{
 727        struct sb_uart_state *state = tty->driver_data;
 728
 729        if (I_IXOFF(tty))
 730                mp_send_xchar(tty, STOP_CHAR(tty));
 731
 732        if (tty->termios.c_cflag & CRTSCTS)
 733                uart_clear_mctrl(state->port, TIOCM_RTS);
 734}
 735
 736static void mp_unthrottle(struct tty_struct *tty)
 737{
 738        struct sb_uart_state *state = tty->driver_data;
 739        struct sb_uart_port *port = state->port;
 740
 741        if (I_IXOFF(tty)) {
 742                if (port->x_char)
 743                        port->x_char = 0;
 744                else
 745                        mp_send_xchar(tty, START_CHAR(tty));
 746        }
 747
 748        if (tty->termios.c_cflag & CRTSCTS)
 749                uart_set_mctrl(port, TIOCM_RTS);
 750}
 751
 752static int mp_get_info(struct sb_uart_state *state, struct serial_struct *retinfo)
 753{
 754        struct sb_uart_port *port = state->port;
 755        struct serial_struct tmp;
 756
 757        memset(&tmp, 0, sizeof(tmp));
 758        tmp.type            = port->type;
 759        tmp.line            = port->line;
 760        tmp.port            = port->iobase;
 761        if (HIGH_BITS_OFFSET)
 762                tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET;
 763        tmp.irq             = port->irq;
 764        tmp.flags           = port->flags;
 765        tmp.xmit_fifo_size  = port->fifosize;
 766        tmp.baud_base       = port->uartclk / 16;
 767        tmp.close_delay     = state->close_delay;
 768        tmp.closing_wait    = state->closing_wait == USF_CLOSING_WAIT_NONE ?
 769                ASYNC_CLOSING_WAIT_NONE :
 770                state->closing_wait;
 771        tmp.custom_divisor  = port->custom_divisor;
 772        tmp.hub6            = port->hub6;
 773        tmp.io_type         = port->iotype;
 774        tmp.iomem_reg_shift = port->regshift;
 775        tmp.iomem_base      = (void *)port->mapbase;
 776
 777        if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
 778                return -EFAULT;
 779        return 0;
 780}
 781
 782static int mp_set_info(struct sb_uart_state *state, struct serial_struct *newinfo)
 783{
 784        struct serial_struct new_serial;
 785        struct sb_uart_port *port = state->port;
 786        unsigned long new_port;
 787        unsigned int change_irq, change_port, closing_wait;
 788        unsigned int old_custom_divisor;
 789        unsigned int old_flags, new_flags;
 790        int retval = 0;
 791
 792        if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
 793                return -EFAULT;
 794
 795        new_port = new_serial.port;
 796        if (HIGH_BITS_OFFSET)
 797                new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
 798
 799        new_serial.irq = irq_canonicalize(new_serial.irq);
 800
 801        closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
 802                USF_CLOSING_WAIT_NONE : new_serial.closing_wait;
 803        MP_STATE_LOCK(state);
 804
 805        change_irq  = new_serial.irq != port->irq;
 806        change_port = new_port != port->iobase ||
 807                (unsigned long)new_serial.iomem_base != port->mapbase ||
 808                new_serial.hub6 != port->hub6 ||
 809                new_serial.io_type != port->iotype ||
 810                new_serial.iomem_reg_shift != port->regshift ||
 811                new_serial.type != port->type;
 812        old_flags = port->flags;
 813        new_flags = new_serial.flags;
 814        old_custom_divisor = port->custom_divisor;
 815
 816        if (!capable(CAP_SYS_ADMIN)) {
 817                retval = -EPERM;
 818                if (change_irq || change_port ||
 819                                (new_serial.baud_base != port->uartclk / 16) ||
 820                                (new_serial.close_delay != state->close_delay) ||
 821                                (closing_wait != state->closing_wait) ||
 822                                (new_serial.xmit_fifo_size != port->fifosize) ||
 823                                (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
 824                        goto exit;
 825                port->flags = ((port->flags & ~UPF_USR_MASK) |
 826                                (new_flags & UPF_USR_MASK));
 827                port->custom_divisor = new_serial.custom_divisor;
 828                goto check_and_exit;
 829        }
 830
 831        if (port->ops->verify_port)
 832                retval = port->ops->verify_port(port, &new_serial);
 833
 834        if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
 835                        (new_serial.baud_base < 9600))
 836                retval = -EINVAL;
 837
 838        if (retval)
 839                goto exit;
 840
 841        if (change_port || change_irq) {
 842                retval = -EBUSY;
 843
 844                if (uart_users(state) > 1)
 845                        goto exit;
 846
 847                mp_shutdown(state);
 848        }
 849
 850        if (change_port) {
 851                unsigned long old_iobase, old_mapbase;
 852                unsigned int old_type, old_iotype, old_hub6, old_shift;
 853
 854                old_iobase = port->iobase;
 855                old_mapbase = port->mapbase;
 856                old_type = port->type;
 857                old_hub6 = port->hub6;
 858                old_iotype = port->iotype;
 859                old_shift = port->regshift;
 860
 861                if (old_type != PORT_UNKNOWN)
 862                        port->ops->release_port(port);
 863
 864                port->iobase = new_port;
 865                port->type = new_serial.type;
 866                port->hub6 = new_serial.hub6;
 867                port->iotype = new_serial.io_type;
 868                port->regshift = new_serial.iomem_reg_shift;
 869                port->mapbase = (unsigned long)new_serial.iomem_base;
 870
 871                if (port->type != PORT_UNKNOWN) {
 872                        retval = port->ops->request_port(port);
 873                } else {
 874                        retval = 0;
 875                }
 876
 877                if (retval && old_type != PORT_UNKNOWN) {
 878                        port->iobase = old_iobase;
 879                        port->type = old_type;
 880                        port->hub6 = old_hub6;
 881                        port->iotype = old_iotype;
 882                        port->regshift = old_shift;
 883                        port->mapbase = old_mapbase;
 884                        retval = port->ops->request_port(port);
 885                        if (retval)
 886                                port->type = PORT_UNKNOWN;
 887
 888                        retval = -EBUSY;
 889                }
 890        }
 891
 892        port->irq              = new_serial.irq;
 893        port->uartclk          = new_serial.baud_base * 16;
 894        port->flags            = (port->flags & ~UPF_CHANGE_MASK) |
 895                (new_flags & UPF_CHANGE_MASK);
 896        port->custom_divisor   = new_serial.custom_divisor;
 897        state->close_delay     = new_serial.close_delay;
 898        state->closing_wait    = closing_wait;
 899        port->fifosize         = new_serial.xmit_fifo_size;
 900        if (state->info->tty)
 901                state->info->tty->low_latency =
 902                        (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
 903
 904check_and_exit:
 905        retval = 0;
 906        if (port->type == PORT_UNKNOWN)
 907                goto exit;
 908        if (state->info->flags & UIF_INITIALIZED) {
 909                if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
 910                                old_custom_divisor != port->custom_divisor) {
 911                        if (port->flags & UPF_SPD_MASK) {
 912                                printk(KERN_NOTICE
 913                                                "%s sets custom speed on ttyMP%d. This "
 914                                                "is deprecated.\n", current->comm,
 915                                                port->line);
 916                        }
 917                        mp_change_speed(state, NULL);
 918                }
 919        } else
 920                retval = mp_startup(state, 1);
 921exit:
 922        MP_STATE_UNLOCK(state);
 923        return retval;
 924}
 925
 926
 927static int mp_get_lsr_info(struct sb_uart_state *state, unsigned int *value)
 928{
 929        struct sb_uart_port *port = state->port;
 930        unsigned int result;
 931
 932        result = port->ops->tx_empty(port);
 933
 934        if (port->x_char ||
 935                        ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
 936                                !state->info->tty->stopped && !state->info->tty->hw_stopped))
 937                result &= ~TIOCSER_TEMT;
 938
 939        return put_user(result, value);
 940}
 941
 942static int mp_tiocmget(struct tty_struct *tty)
 943{
 944        struct sb_uart_state *state = tty->driver_data;
 945        struct sb_uart_port *port = state->port;
 946        int result = -EIO;
 947
 948        MP_STATE_LOCK(state);
 949        if (!(tty->flags & (1 << TTY_IO_ERROR))) {
 950                result = port->mctrl;
 951                spin_lock_irq(&port->lock);
 952                result |= port->ops->get_mctrl(port);
 953                spin_unlock_irq(&port->lock);
 954        }
 955        MP_STATE_UNLOCK(state);
 956        return result;
 957}
 958
 959static int mp_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
 960{
 961        struct sb_uart_state *state = tty->driver_data;
 962        struct sb_uart_port *port = state->port;
 963        int ret = -EIO;
 964
 965
 966        MP_STATE_LOCK(state);
 967        if (!(tty->flags & (1 << TTY_IO_ERROR))) {
 968                mp_update_mctrl(port, set, clear);
 969                ret = 0;
 970        }
 971        MP_STATE_UNLOCK(state);
 972
 973        return ret;
 974}
 975
 976static int mp_break_ctl(struct tty_struct *tty, int break_state)
 977{
 978        struct sb_uart_state *state = tty->driver_data;
 979        struct sb_uart_port *port = state->port;
 980
 981        MP_STATE_LOCK(state);
 982
 983        if (port->type != PORT_UNKNOWN)
 984                port->ops->break_ctl(port, break_state);
 985
 986        MP_STATE_UNLOCK(state);
 987        return 0;
 988}
 989
 990static int mp_do_autoconfig(struct sb_uart_state *state)
 991{
 992        struct sb_uart_port *port = state->port;
 993        int flags, ret;
 994
 995        if (!capable(CAP_SYS_ADMIN))
 996                return -EPERM;
 997
 998        if (mutex_lock_interruptible(&state->mutex))
 999                return -ERESTARTSYS;
1000        ret = -EBUSY;
1001        if (uart_users(state) == 1) {
1002                mp_shutdown(state);
1003
1004                if (port->type != PORT_UNKNOWN)
1005                        port->ops->release_port(port);
1006
1007                flags = UART_CONFIG_TYPE;
1008                if (port->flags & UPF_AUTO_IRQ)
1009                        flags |= UART_CONFIG_IRQ;
1010
1011                port->ops->config_port(port, flags);
1012
1013                ret = mp_startup(state, 1);
1014        }
1015        MP_STATE_UNLOCK(state);
1016        return ret;
1017}
1018
1019static int mp_wait_modem_status(struct sb_uart_state *state, unsigned long arg)
1020{
1021        struct sb_uart_port *port = state->port;
1022        DECLARE_WAITQUEUE(wait, current);
1023        struct sb_uart_icount cprev, cnow;
1024        int ret;
1025
1026        spin_lock_irq(&port->lock);
1027        memcpy(&cprev, &port->icount, sizeof(struct sb_uart_icount));
1028
1029        port->ops->enable_ms(port);
1030        spin_unlock_irq(&port->lock);
1031
1032        add_wait_queue(&state->info->delta_msr_wait, &wait);
1033        for (;;) {
1034                spin_lock_irq(&port->lock);
1035                memcpy(&cnow, &port->icount, sizeof(struct sb_uart_icount));
1036                spin_unlock_irq(&port->lock);
1037
1038                set_current_state(TASK_INTERRUPTIBLE);
1039
1040                if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1041                                ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1042                                ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1043                                ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1044                        ret = 0;
1045                        break;
1046                }
1047
1048                schedule();
1049
1050                if (signal_pending(current)) {
1051                        ret = -ERESTARTSYS;
1052                        break;
1053                }
1054
1055                cprev = cnow;
1056        }
1057
1058        current->state = TASK_RUNNING;
1059        remove_wait_queue(&state->info->delta_msr_wait, &wait);
1060
1061        return ret;
1062}
1063
1064static int mp_get_count(struct sb_uart_state *state, struct serial_icounter_struct *icnt)
1065{
1066        struct serial_icounter_struct icount = {};
1067        struct sb_uart_icount cnow;
1068        struct sb_uart_port *port = state->port;
1069
1070        spin_lock_irq(&port->lock);
1071        memcpy(&cnow, &port->icount, sizeof(struct sb_uart_icount));
1072        spin_unlock_irq(&port->lock);
1073
1074        icount.cts         = cnow.cts;
1075        icount.dsr         = cnow.dsr;
1076        icount.rng         = cnow.rng;
1077        icount.dcd         = cnow.dcd;
1078        icount.rx          = cnow.rx;
1079        icount.tx          = cnow.tx;
1080        icount.frame       = cnow.frame;
1081        icount.overrun     = cnow.overrun;
1082        icount.parity      = cnow.parity;
1083        icount.brk         = cnow.brk;
1084        icount.buf_overrun = cnow.buf_overrun;
1085
1086        return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1087}
1088
1089static int mp_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1090{
1091        struct sb_uart_state *state = tty->driver_data;
1092        struct mp_port *info = (struct mp_port *)state->port;
1093        int ret = -ENOIOCTLCMD;
1094
1095
1096        switch (cmd) {
1097                case TIOCSMULTIDROP:
1098                        /* set multi-drop mode enable or disable, and default operation mode is H/W mode */
1099                        if (info->port.type == PORT_16C105XA)
1100                        {
1101                                //arg &= ~0x6;
1102                                //state->port->mdmode = 0;
1103                                return set_multidrop_mode((struct sb_uart_port *)info, (unsigned int)arg);
1104                        }
1105                        ret = -ENOTSUPP;
1106                        break;
1107                case GETDEEPFIFO:
1108                        ret = get_deep_fifo(state->port);
1109                        return ret;
1110                case SETDEEPFIFO:
1111                        ret = set_deep_fifo(state->port,arg);
1112                        deep[state->port->line] = arg;
1113                        return ret;
1114                case SETTTR:
1115                        if (info->port.type == PORT_16C105X || info->port.type == PORT_16C105XA){
1116                                ret = sb1054_set_register(state->port,PAGE_4,SB105X_TTR,arg);
1117                                ttr[state->port->line] = arg;
1118                        }
1119                        return ret;
1120                case SETRTR:
1121                        if (info->port.type == PORT_16C105X || info->port.type == PORT_16C105XA){
1122                                ret = sb1054_set_register(state->port,PAGE_4,SB105X_RTR,arg);
1123                                rtr[state->port->line] = arg;
1124                        }
1125                        return ret;
1126                case GETTTR:
1127                        if (info->port.type == PORT_16C105X || info->port.type == PORT_16C105XA){
1128                                ret = sb1054_get_register(state->port,PAGE_4,SB105X_TTR);
1129                        }
1130                        return ret;
1131                case GETRTR:
1132                        if (info->port.type == PORT_16C105X || info->port.type == PORT_16C105XA){
1133                                ret = sb1054_get_register(state->port,PAGE_4,SB105X_RTR);
1134                        }
1135                        return ret;
1136
1137                case SETFCR:
1138                        if (info->port.type == PORT_16C105X || info->port.type == PORT_16C105XA){
1139                                ret = sb1054_set_register(state->port,PAGE_1,SB105X_FCR,arg);
1140                        }
1141                        else{
1142                                serial_out(info,2,arg);
1143                        }
1144
1145                        return ret;
1146                case TIOCSMDADDR:
1147                        /* set multi-drop address */
1148                        if (info->port.type == PORT_16C105XA)
1149                        {
1150                                state->port->mdmode |= MDMODE_ADDR;
1151                                return set_multidrop_addr((struct sb_uart_port *)info, (unsigned int)arg);
1152                        }
1153                        ret = -ENOTSUPP;
1154                        break;
1155
1156                case TIOCGMDADDR:
1157                        /* set multi-drop address */
1158                        if ((info->port.type == PORT_16C105XA) && (state->port->mdmode & MDMODE_ADDR))
1159                        {
1160                                return get_multidrop_addr((struct sb_uart_port *)info);
1161                        }
1162                        ret = -ENOTSUPP;
1163                        break;
1164
1165                case TIOCSENDADDR:
1166                        /* send address in multi-drop mode */
1167                        if ((info->port.type == PORT_16C105XA) 
1168                                        && (state->port->mdmode & (MDMODE_ENABLE)))
1169                        {
1170                                if (mp_chars_in_buffer(tty) > 0)
1171                                {
1172                                        tty_wait_until_sent(tty, 0);
1173                                }
1174                                //while ((serial_in(info, UART_LSR) & 0x60) != 0x60);
1175                                //while (sb1054_get_register(state->port, PAGE_2, SB105X_TFCR) != 0);
1176                                while ((serial_in(info, UART_LSR) & 0x60) != 0x60);
1177                                serial_out(info, UART_SCR, (int)arg);
1178                        }
1179                        break;
1180
1181                case TIOCGSERIAL:
1182                        ret = mp_get_info(state, (struct serial_struct *)arg);
1183                        break;
1184
1185                case TIOCSSERIAL:
1186                        ret = mp_set_info(state, (struct serial_struct *)arg);
1187                        break;
1188
1189                case TIOCSERCONFIG:
1190                        ret = mp_do_autoconfig(state);
1191                        break;
1192
1193                case TIOCSERGWILD: /* obsolete */
1194                case TIOCSERSWILD: /* obsolete */
1195                        ret = 0;
1196                        break;
1197                        /* for Multiport */
1198                case TIOCGNUMOFPORT: /* Get number of ports */
1199                        return NR_PORTS;
1200                case TIOCGGETDEVID:
1201                        return mp_devs[arg].device_id;
1202                case TIOCGGETREV:
1203                        return mp_devs[arg].revision;
1204                case TIOCGGETNRPORTS:
1205                        return mp_devs[arg].nr_ports;
1206                case TIOCGGETBDNO:
1207                        return NR_BOARD;
1208                case TIOCGGETINTERFACE:
1209                        if (mp_devs[arg].revision == 0xc0)
1210                        {
1211                                /* for SB16C1053APCI */
1212                                return (sb1053a_get_interface(info, info->port.line));
1213                        }
1214                        else
1215                        {
1216                                return (inb(mp_devs[arg].option_reg_addr+MP_OPTR_IIR0+(state->port->line/8)));
1217                        }
1218                case TIOCGGETPORTTYPE:
1219                        ret = get_device_type(arg);;
1220                        return ret;
1221                case TIOCSMULTIECHO: /* set to multi-drop mode(RS422) or echo mode(RS485)*/
1222                        outb( ( inb(info->interface_config_addr) & ~0x03 ) | 0x01 ,  
1223                                        info->interface_config_addr);
1224                        return 0;
1225                case TIOCSPTPNOECHO: /* set to multi-drop mode(RS422) or echo mode(RS485) */
1226                        outb( ( inb(info->interface_config_addr) & ~0x03 )  ,             
1227                                        info->interface_config_addr);
1228                        return 0;
1229        }
1230
1231        if (ret != -ENOIOCTLCMD)
1232                goto out;
1233
1234        if (tty->flags & (1 << TTY_IO_ERROR)) {
1235                ret = -EIO;
1236                goto out;
1237        }
1238
1239        switch (cmd) {
1240                case TIOCMIWAIT:
1241                        ret = mp_wait_modem_status(state, arg);
1242                        break;
1243
1244                case TIOCGICOUNT:
1245                        ret = mp_get_count(state, (struct serial_icounter_struct *)arg);
1246                        break;
1247        }
1248
1249        if (ret != -ENOIOCTLCMD)
1250                goto out;
1251
1252        MP_STATE_LOCK(state);
1253        switch (cmd) {
1254                case TIOCSERGETLSR: /* Get line status register */
1255                        ret = mp_get_lsr_info(state, (unsigned int *)arg);
1256                        break;
1257
1258                default: {
1259                                        struct sb_uart_port *port = state->port;
1260                                        if (port->ops->ioctl)
1261                                                ret = port->ops->ioctl(port, cmd, arg);
1262                                        break;
1263                                }
1264        }
1265
1266        MP_STATE_UNLOCK(state);
1267out:
1268        return ret;
1269}
1270
1271static void mp_set_termios(struct tty_struct *tty, struct MP_TERMIOS *old_termios)
1272{
1273        struct sb_uart_state *state = tty->driver_data;
1274        unsigned long flags;
1275        unsigned int cflag = tty->termios.c_cflag;
1276
1277#define RELEVANT_IFLAG(iflag)   ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1278
1279        if ((cflag ^ old_termios->c_cflag) == 0 &&
1280                        RELEVANT_IFLAG(tty->termios.c_iflag ^ old_termios->c_iflag) == 0)
1281                return;
1282
1283        mp_change_speed(state, old_termios);
1284
1285        if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1286                uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
1287
1288        if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1289                unsigned int mask = TIOCM_DTR;
1290                if (!(cflag & CRTSCTS) ||
1291                                !test_bit(TTY_THROTTLED, &tty->flags))
1292                        mask |= TIOCM_RTS;
1293                uart_set_mctrl(state->port, mask);
1294        }
1295
1296        if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1297                spin_lock_irqsave(&state->port->lock, flags);
1298                tty->hw_stopped = 0;
1299                __mp_start(tty);
1300                spin_unlock_irqrestore(&state->port->lock, flags);
1301        }
1302
1303        if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1304                spin_lock_irqsave(&state->port->lock, flags);
1305                if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
1306                        tty->hw_stopped = 1;
1307                        state->port->ops->stop_tx(state->port);
1308                }
1309                spin_unlock_irqrestore(&state->port->lock, flags);
1310        }
1311}
1312
1313static void mp_close(struct tty_struct *tty, struct file *filp)
1314{
1315        struct sb_uart_state *state = tty->driver_data;
1316        struct sb_uart_port *port;
1317
1318        printk("mp_close!\n");
1319        if (!state || !state->port)
1320                return;
1321
1322        port = state->port;
1323
1324        printk("close1 %d\n", __LINE__);
1325        MP_STATE_LOCK(state);
1326
1327        printk("close2 %d\n", __LINE__);
1328        if (tty_hung_up_p(filp))
1329                goto done;
1330
1331        printk("close3 %d\n", __LINE__);
1332        if ((tty->count == 1) && (state->count != 1)) {
1333                printk("mp_close: bad serial port count; tty->count is 1, "
1334                                "state->count is %d\n", state->count);
1335                state->count = 1;
1336        }
1337        printk("close4 %d\n", __LINE__);
1338        if (--state->count < 0) {
1339                printk("rs_close: bad serial port count for ttyMP%d: %d\n",
1340                                port->line, state->count);
1341                state->count = 0;
1342        }
1343        if (state->count)
1344                goto done;
1345
1346        tty->closing = 1;
1347
1348        printk("close5 %d\n", __LINE__);
1349        if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1350                tty_wait_until_sent(tty, state->closing_wait);
1351
1352        printk("close6 %d\n", __LINE__);
1353        if (state->info->flags & UIF_INITIALIZED) {
1354                unsigned long flags;
1355                spin_lock_irqsave(&port->lock, flags);
1356                port->ops->stop_rx(port);
1357                spin_unlock_irqrestore(&port->lock, flags);
1358                mp_wait_until_sent(tty, port->timeout);
1359        }
1360        printk("close7 %d\n", __LINE__);
1361
1362        mp_shutdown(state);
1363        printk("close8 %d\n", __LINE__);
1364        mp_flush_buffer(tty);
1365        tty_ldisc_flush(tty);
1366        tty->closing = 0;
1367        state->info->tty = NULL;
1368        if (state->info->blocked_open) 
1369        {
1370                if (state->close_delay)
1371                {
1372                        set_current_state(TASK_INTERRUPTIBLE);
1373                        schedule_timeout(state->close_delay);
1374                }
1375        }
1376        else
1377        {
1378                mp_change_pm(state, 3);
1379        }
1380        printk("close8 %d\n", __LINE__);
1381
1382        state->info->flags &= ~UIF_NORMAL_ACTIVE;
1383        wake_up_interruptible(&state->info->open_wait);
1384
1385done:
1386        printk("close done\n");
1387        MP_STATE_UNLOCK(state);
1388        module_put(THIS_MODULE);
1389}
1390
1391static void mp_wait_until_sent(struct tty_struct *tty, int timeout)
1392{
1393        struct sb_uart_state *state = tty->driver_data;
1394        struct sb_uart_port *port = state->port;
1395        unsigned long char_time, expire;
1396
1397        if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1398                return;
1399
1400        char_time = (port->timeout - HZ/50) / port->fifosize;
1401        char_time = char_time / 5;
1402        if (char_time == 0)
1403                char_time = 1;
1404        if (timeout && timeout < char_time)
1405                char_time = timeout;
1406
1407        if (timeout == 0 || timeout > 2 * port->timeout)
1408                timeout = 2 * port->timeout;
1409
1410        expire = jiffies + timeout;
1411
1412        while (!port->ops->tx_empty(port)) {
1413                set_current_state(TASK_INTERRUPTIBLE);
1414                schedule_timeout(char_time);
1415                if (signal_pending(current))
1416                        break;
1417                if (time_after(jiffies, expire))
1418                        break;
1419        }
1420        set_current_state(TASK_RUNNING); /* might not be needed */
1421}
1422
1423static void mp_hangup(struct tty_struct *tty)
1424{
1425        struct sb_uart_state *state = tty->driver_data;
1426
1427        MP_STATE_LOCK(state);
1428        if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
1429                mp_flush_buffer(tty);
1430                mp_shutdown(state);
1431                state->count = 0;
1432                state->info->flags &= ~UIF_NORMAL_ACTIVE;
1433                state->info->tty = NULL;
1434                wake_up_interruptible(&state->info->open_wait);
1435                wake_up_interruptible(&state->info->delta_msr_wait);
1436        }
1437        MP_STATE_UNLOCK(state);
1438}
1439
1440static void mp_update_termios(struct sb_uart_state *state)
1441{
1442        struct tty_struct *tty = state->info->tty;
1443        struct sb_uart_port *port = state->port;
1444
1445        if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1446                mp_change_speed(state, NULL);
1447
1448                if (tty->termios.c_cflag & CBAUD)
1449                        uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1450        }
1451}
1452
1453static int mp_block_til_ready(struct file *filp, struct sb_uart_state *state)
1454{
1455        DECLARE_WAITQUEUE(wait, current);
1456        struct sb_uart_info *info = state->info;
1457        struct sb_uart_port *port = state->port;
1458        unsigned int mctrl;
1459
1460        info->blocked_open++;
1461        state->count--;
1462
1463        add_wait_queue(&info->open_wait, &wait);
1464        while (1) {
1465                set_current_state(TASK_INTERRUPTIBLE);
1466
1467                if (tty_hung_up_p(filp) || info->tty == NULL)
1468                        break;
1469
1470                if (!(info->flags & UIF_INITIALIZED))
1471                        break;
1472
1473                if ((filp->f_flags & O_NONBLOCK) ||
1474                                (info->tty->termios.c_cflag & CLOCAL) ||
1475                                (info->tty->flags & (1 << TTY_IO_ERROR))) {
1476                        break;
1477                }
1478
1479                if (info->tty->termios.c_cflag & CBAUD)
1480                        uart_set_mctrl(port, TIOCM_DTR);
1481
1482                spin_lock_irq(&port->lock);
1483                port->ops->enable_ms(port);
1484                mctrl = port->ops->get_mctrl(port);
1485                spin_unlock_irq(&port->lock);
1486                if (mctrl & TIOCM_CAR)
1487                        break;
1488
1489                MP_STATE_UNLOCK(state);
1490                schedule();
1491                MP_STATE_LOCK(state);
1492
1493                if (signal_pending(current))
1494                        break;
1495        }
1496        set_current_state(TASK_RUNNING);
1497        remove_wait_queue(&info->open_wait, &wait);
1498
1499        state->count++;
1500        info->blocked_open--;
1501
1502        if (signal_pending(current))
1503                return -ERESTARTSYS;
1504
1505        if (!info->tty || tty_hung_up_p(filp))
1506                return -EAGAIN;
1507
1508        return 0;
1509}
1510
1511static struct sb_uart_state *uart_get(struct uart_driver *drv, int line)
1512{
1513        struct sb_uart_state *state;
1514
1515        MP_MUTEX_LOCK(mp_mutex);
1516        state = drv->state + line;
1517        if (mutex_lock_interruptible(&state->mutex)) {
1518                state = ERR_PTR(-ERESTARTSYS);
1519                goto out;
1520        }
1521        state->count++;
1522        if (!state->port) {
1523                state->count--;
1524                MP_STATE_UNLOCK(state);
1525                state = ERR_PTR(-ENXIO);
1526                goto out;
1527        }
1528
1529        if (!state->info) {
1530                state->info = kmalloc(sizeof(struct sb_uart_info), GFP_KERNEL);
1531                if (state->info) {
1532                        memset(state->info, 0, sizeof(struct sb_uart_info));
1533                        init_waitqueue_head(&state->info->open_wait);
1534                        init_waitqueue_head(&state->info->delta_msr_wait);
1535
1536                        state->port->info = state->info;
1537
1538                        tasklet_init(&state->info->tlet, mp_tasklet_action,
1539                                        (unsigned long)state);
1540                } else {
1541                        state->count--;
1542                        MP_STATE_UNLOCK(state);
1543                        state = ERR_PTR(-ENOMEM);
1544                }
1545        }
1546
1547out:
1548        MP_MUTEX_UNLOCK(mp_mutex);
1549        return state;
1550}
1551
1552static int mp_open(struct tty_struct *tty, struct file *filp)
1553{
1554        struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1555        struct sb_uart_state *state;
1556        int retval;
1557        int  line = tty->index;
1558        struct mp_port *mtpt;
1559
1560        retval = -ENODEV;
1561        if (line >= tty->driver->num)
1562                goto fail;
1563
1564        state = uart_get(drv, line);
1565
1566        if (IS_ERR(state)) {
1567                retval = PTR_ERR(state);
1568                goto fail;
1569        }
1570
1571        mtpt  = (struct mp_port *)state->port;
1572
1573        tty->driver_data = state;
1574        tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1575        tty->alt_speed = 0;
1576        state->info->tty = tty;
1577
1578        if (tty_hung_up_p(filp)) {
1579                retval = -EAGAIN;
1580                state->count--;
1581                MP_STATE_UNLOCK(state);
1582                goto fail;
1583        }
1584
1585        if (state->count == 1)
1586                mp_change_pm(state, 0);
1587
1588        retval = mp_startup(state, 0);
1589
1590        if (retval == 0)
1591                retval = mp_block_til_ready(filp, state);
1592        MP_STATE_UNLOCK(state);
1593
1594        if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1595                state->info->flags |= UIF_NORMAL_ACTIVE;
1596
1597                mp_update_termios(state);
1598        }
1599
1600        uart_clear_mctrl(state->port, TIOCM_RTS);
1601        try_module_get(THIS_MODULE);
1602fail:
1603        return retval;
1604}
1605
1606
1607static const char *mp_type(struct sb_uart_port *port)
1608{
1609        const char *str = NULL;
1610
1611        if (port->ops->type)
1612                str = port->ops->type(port);
1613
1614        if (!str)
1615                str = "unknown";
1616
1617        return str;
1618}
1619
1620static void mp_change_pm(struct sb_uart_state *state, int pm_state)
1621{
1622        struct sb_uart_port *port = state->port;
1623        if (port->ops->pm)
1624                port->ops->pm(port, pm_state, state->pm_state);
1625        state->pm_state = pm_state;
1626}
1627
1628static inline void mp_report_port(struct uart_driver *drv, struct sb_uart_port *port)
1629{
1630        char address[64];
1631
1632        switch (port->iotype) {
1633                case UPIO_PORT:
1634                        snprintf(address, sizeof(address),"I/O 0x%x", port->iobase);
1635                        break;
1636                case UPIO_HUB6:
1637                        snprintf(address, sizeof(address),"I/O 0x%x offset 0x%x", port->iobase, port->hub6);
1638                        break;
1639                case UPIO_MEM:
1640                        snprintf(address, sizeof(address),"MMIO 0x%lx", port->mapbase);
1641                        break;
1642                default:
1643                        snprintf(address, sizeof(address),"*unknown*" );
1644                        strlcpy(address, "*unknown*", sizeof(address));
1645                        break;
1646        }
1647
1648        printk( "%s%d at %s (irq = %d) is a %s\n",
1649                        drv->dev_name, port->line, address, port->irq, mp_type(port));
1650
1651}
1652
1653static void mp_configure_port(struct uart_driver *drv, struct sb_uart_state *state, struct sb_uart_port *port)
1654{
1655        unsigned int flags;
1656
1657
1658        if (!port->iobase && !port->mapbase && !port->membase)
1659        {
1660                DPRINTK("%s error \n",__FUNCTION__);
1661                return;
1662        }
1663        flags = UART_CONFIG_TYPE;
1664        if (port->flags & UPF_AUTO_IRQ)
1665                flags |= UART_CONFIG_IRQ;
1666        if (port->flags & UPF_BOOT_AUTOCONF) {
1667                port->type = PORT_UNKNOWN;
1668                port->ops->config_port(port, flags);
1669        }
1670
1671        if (port->type != PORT_UNKNOWN) {
1672                unsigned long flags;
1673
1674                mp_report_port(drv, port);
1675
1676                spin_lock_irqsave(&port->lock, flags);
1677                port->ops->set_mctrl(port, 0);
1678                spin_unlock_irqrestore(&port->lock, flags);
1679
1680                mp_change_pm(state, 3);
1681        }
1682}
1683
1684static void mp_unconfigure_port(struct uart_driver *drv, struct sb_uart_state *state)
1685{
1686        struct sb_uart_port *port = state->port;
1687        struct sb_uart_info *info = state->info;
1688
1689        if (info && info->tty)
1690                tty_hangup(info->tty);
1691
1692        MP_STATE_LOCK(state);
1693
1694        state->info = NULL;
1695
1696        if (port->type != PORT_UNKNOWN)
1697                port->ops->release_port(port);
1698
1699        port->type = PORT_UNKNOWN;
1700
1701        if (info) {
1702                tasklet_kill(&info->tlet);
1703                kfree(info);
1704        }
1705
1706        MP_STATE_UNLOCK(state);
1707}
1708static struct tty_operations mp_ops = {
1709        .open           = mp_open,
1710        .close          = mp_close,
1711        .write          = mp_write,
1712        .put_char       = mp_put_char,
1713        .flush_chars    = mp_put_chars,
1714        .write_room     = mp_write_room,
1715        .chars_in_buffer= mp_chars_in_buffer,
1716        .flush_buffer   = mp_flush_buffer,
1717        .ioctl          = mp_ioctl,
1718        .throttle       = mp_throttle,
1719        .unthrottle     = mp_unthrottle,
1720        .send_xchar     = mp_send_xchar,
1721        .set_termios    = mp_set_termios,
1722        .stop           = mp_stop,
1723        .start          = mp_start,
1724        .hangup         = mp_hangup,
1725        .break_ctl      = mp_break_ctl,
1726        .wait_until_sent= mp_wait_until_sent,
1727#ifdef CONFIG_PROC_FS
1728        .proc_fops      = NULL,
1729#endif
1730        .tiocmget       = mp_tiocmget,
1731        .tiocmset       = mp_tiocmset,
1732};
1733
1734static int mp_register_driver(struct uart_driver *drv)
1735{
1736        struct tty_driver *normal = NULL;
1737        int i, retval;
1738
1739        drv->state = kmalloc(sizeof(struct sb_uart_state) * drv->nr, GFP_KERNEL);
1740        retval = -ENOMEM;
1741        if (!drv->state)
1742        {
1743                printk("SB PCI Error: Kernel memory allocation error!\n");
1744                goto out;
1745        }
1746        memset(drv->state, 0, sizeof(struct sb_uart_state) * drv->nr);
1747
1748        normal = alloc_tty_driver(drv->nr);
1749        if (!normal)
1750        {
1751                printk("SB PCI Error: tty allocation error!\n");
1752                goto out;
1753        }
1754
1755        drv->tty_driver = normal;
1756
1757        normal->owner           = drv->owner;
1758        normal->magic           = TTY_DRIVER_MAGIC;
1759        normal->driver_name     = drv->driver_name;
1760        normal->name            = drv->dev_name;
1761        normal->major           = drv->major;
1762        normal->minor_start     = drv->minor;
1763
1764        normal->num             = MAX_MP_PORT ; 
1765
1766        normal->type            = TTY_DRIVER_TYPE_SERIAL;
1767        normal->subtype         = SERIAL_TYPE_NORMAL;
1768        normal->init_termios    = tty_std_termios;
1769        normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1770        normal->flags           = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1771        normal->driver_state    = drv;
1772
1773        tty_set_operations(normal, &mp_ops);
1774
1775for (i = 0; i < drv->nr; i++) {
1776        struct sb_uart_state *state = drv->state + i;
1777
1778        state->close_delay     = 500;   
1779        state->closing_wait    = 30000; 
1780
1781        mutex_init(&state->mutex);
1782        }
1783
1784        retval = tty_register_driver(normal);
1785out:
1786        if (retval < 0) {
1787                printk("Register tty driver Fail!\n");
1788                put_tty_driver(normal);
1789                kfree(drv->state);
1790        }
1791
1792        return retval;
1793}
1794
1795void mp_unregister_driver(struct uart_driver *drv)
1796{
1797    struct tty_driver *normal = NULL;
1798
1799    normal = drv->tty_driver;
1800
1801    if (!normal)
1802    {
1803        return;
1804    }
1805
1806    tty_unregister_driver(normal);
1807    put_tty_driver(normal);
1808    drv->tty_driver = NULL;
1809
1810
1811    if (drv->state)
1812    {
1813        kfree(drv->state);
1814    }
1815
1816}
1817
1818static int mp_add_one_port(struct uart_driver *drv, struct sb_uart_port *port)
1819{
1820        struct sb_uart_state *state;
1821        int ret = 0;
1822
1823
1824        if (port->line >= drv->nr)
1825                return -EINVAL;
1826
1827        state = drv->state + port->line;
1828
1829        MP_MUTEX_LOCK(mp_mutex);
1830        if (state->port) {
1831                ret = -EINVAL;
1832                goto out;
1833        }
1834
1835        state->port = port;
1836
1837        spin_lock_init(&port->lock);
1838        port->cons = drv->cons;
1839        port->info = state->info;
1840
1841        mp_configure_port(drv, state, port);
1842
1843        tty_register_device(drv->tty_driver, port->line, port->dev);
1844
1845out:
1846        MP_MUTEX_UNLOCK(mp_mutex);
1847
1848
1849        return ret;
1850}
1851
1852static int mp_remove_one_port(struct uart_driver *drv, struct sb_uart_port *port)
1853{
1854        struct sb_uart_state *state = drv->state + port->line;
1855
1856        if (state->port != port)
1857                printk(KERN_ALERT "Removing wrong port: %p != %p\n",
1858                                state->port, port);
1859
1860        MP_MUTEX_LOCK(mp_mutex);
1861
1862        tty_unregister_device(drv->tty_driver, port->line);
1863
1864        mp_unconfigure_port(drv, state);
1865        state->port = NULL;
1866        MP_MUTEX_UNLOCK(mp_mutex);
1867
1868        return 0;
1869}
1870
1871static void autoconfig(struct mp_port *mtpt, unsigned int probeflags)
1872{
1873        unsigned char status1, scratch, scratch2, scratch3;
1874        unsigned char save_lcr, save_mcr;
1875        unsigned long flags;
1876
1877        unsigned char u_type;
1878        unsigned char b_ret = 0;
1879
1880        if (!mtpt->port.iobase && !mtpt->port.mapbase && !mtpt->port.membase)
1881                return;
1882
1883        DEBUG_AUTOCONF("ttyMP%d: autoconf (0x%04x, 0x%p): ",
1884                        mtpt->port.line, mtpt->port.iobase, mtpt->port.membase);
1885
1886        spin_lock_irqsave(&mtpt->port.lock, flags);
1887
1888        if (!(mtpt->port.flags & UPF_BUGGY_UART)) {
1889                scratch = serial_inp(mtpt, UART_IER);
1890                serial_outp(mtpt, UART_IER, 0);
1891#ifdef __i386__
1892                outb(0xff, 0x080);
1893#endif
1894                scratch2 = serial_inp(mtpt, UART_IER) & 0x0f;
1895                serial_outp(mtpt, UART_IER, 0x0F);
1896#ifdef __i386__
1897                outb(0, 0x080);
1898#endif
1899                scratch3 = serial_inp(mtpt, UART_IER) & 0x0F;
1900                serial_outp(mtpt, UART_IER, scratch);
1901                if (scratch2 != 0 || scratch3 != 0x0F) {
1902                        DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1903                                        scratch2, scratch3);
1904                        goto out;
1905                }
1906        }
1907
1908        save_mcr = serial_in(mtpt, UART_MCR);
1909        save_lcr = serial_in(mtpt, UART_LCR);
1910
1911        if (!(mtpt->port.flags & UPF_SKIP_TEST)) {
1912                serial_outp(mtpt, UART_MCR, UART_MCR_LOOP | 0x0A);
1913                status1 = serial_inp(mtpt, UART_MSR) & 0xF0;
1914                serial_outp(mtpt, UART_MCR, save_mcr);
1915                if (status1 != 0x90) {
1916                        DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1917                                        status1);
1918                        goto out;
1919                }
1920        }
1921
1922        serial_outp(mtpt, UART_LCR, 0xBF);
1923        serial_outp(mtpt, UART_EFR, 0);
1924        serial_outp(mtpt, UART_LCR, 0);
1925
1926        serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
1927        scratch = serial_in(mtpt, UART_IIR) >> 6;
1928
1929        DEBUG_AUTOCONF("iir=%d ", scratch);
1930        if(mtpt->device->nr_ports >= 8)
1931                b_ret = read_option_register(mtpt,(MP_OPTR_DIR0 + ((mtpt->port.line)/8)));
1932        else    
1933                b_ret = read_option_register(mtpt,MP_OPTR_DIR0);
1934        u_type = (b_ret & 0xf0) >> 4;
1935        if(mtpt->port.type == PORT_UNKNOWN )
1936        {
1937                switch (u_type)
1938                {
1939                        case DIR_UART_16C550:
1940                                mtpt->port.type = PORT_16C55X;
1941                                break;
1942                        case DIR_UART_16C1050:
1943                                mtpt->port.type = PORT_16C105X;
1944                                break;
1945                        case DIR_UART_16C1050A:
1946                                if (mtpt->port.line < 2)
1947                                {
1948                                        mtpt->port.type = PORT_16C105XA;
1949                                }
1950                                else
1951                                {
1952                                        if (mtpt->device->device_id & 0x50)
1953                                        {
1954                                                mtpt->port.type = PORT_16C55X;
1955                                        }
1956                                        else
1957                                        {
1958                                                mtpt->port.type = PORT_16C105X;
1959                                        }
1960                                }
1961                                break;
1962                        default:        
1963                                mtpt->port.type = PORT_UNKNOWN;
1964                                break;
1965                }
1966        }
1967
1968        if(mtpt->port.type == PORT_UNKNOWN )
1969        {
1970printk("unknow2\n");
1971                switch (scratch) {
1972                        case 0:
1973                        case 1:
1974                                mtpt->port.type = PORT_UNKNOWN;
1975                                break;
1976                        case 2:
1977                        case 3:
1978                                mtpt->port.type = PORT_16C55X;
1979                                break;
1980                }
1981        }
1982
1983        serial_outp(mtpt, UART_LCR, save_lcr);
1984
1985        mtpt->port.fifosize = uart_config[mtpt->port.type].dfl_xmit_fifo_size;
1986        mtpt->capabilities = uart_config[mtpt->port.type].flags;
1987
1988        if (mtpt->port.type == PORT_UNKNOWN)
1989                goto out;
1990        serial_outp(mtpt, UART_MCR, save_mcr);
1991        serial_outp(mtpt, UART_FCR, (UART_FCR_ENABLE_FIFO |
1992                                UART_FCR_CLEAR_RCVR |
1993                                UART_FCR_CLEAR_XMIT));
1994        serial_outp(mtpt, UART_FCR, 0);
1995        (void)serial_in(mtpt, UART_RX);
1996        serial_outp(mtpt, UART_IER, 0);
1997
1998out:
1999        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2000        DEBUG_AUTOCONF("type=%s\n", uart_config[mtpt->port.type].name);
2001}
2002
2003static void autoconfig_irq(struct mp_port *mtpt)
2004{
2005        unsigned char save_mcr, save_ier;
2006        unsigned long irqs;
2007        int irq;
2008
2009        /* forget possible initially masked and pending IRQ */
2010        probe_irq_off(probe_irq_on());
2011        save_mcr = serial_inp(mtpt, UART_MCR);
2012        save_ier = serial_inp(mtpt, UART_IER);
2013        serial_outp(mtpt, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
2014
2015        irqs = probe_irq_on();
2016        serial_outp(mtpt, UART_MCR, 0);
2017        serial_outp(mtpt, UART_MCR,
2018                UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2019
2020        serial_outp(mtpt, UART_IER, 0x0f);    /* enable all intrs */
2021        (void)serial_inp(mtpt, UART_LSR);
2022        (void)serial_inp(mtpt, UART_RX);
2023        (void)serial_inp(mtpt, UART_IIR);
2024        (void)serial_inp(mtpt, UART_MSR);
2025        serial_outp(mtpt, UART_TX, 0xFF);
2026        irq = probe_irq_off(irqs);
2027
2028        serial_outp(mtpt, UART_MCR, save_mcr);
2029        serial_outp(mtpt, UART_IER, save_ier);
2030
2031        mtpt->port.irq = (irq > 0) ? irq : 0;
2032}
2033
2034static void multi_stop_tx(struct sb_uart_port *port)
2035{
2036        struct mp_port *mtpt = (struct mp_port *)port;
2037
2038        if (mtpt->ier & UART_IER_THRI) {
2039                mtpt->ier &= ~UART_IER_THRI;
2040                serial_out(mtpt, UART_IER, mtpt->ier);
2041        }
2042
2043        tasklet_schedule(&port->info->tlet);
2044}
2045
2046static void multi_start_tx(struct sb_uart_port *port)
2047{
2048        struct mp_port *mtpt = (struct mp_port *)port;
2049
2050        if (!(mtpt->ier & UART_IER_THRI)) {
2051                mtpt->ier |= UART_IER_THRI;
2052                serial_out(mtpt, UART_IER, mtpt->ier);
2053        }
2054}
2055
2056static void multi_stop_rx(struct sb_uart_port *port)
2057{
2058        struct mp_port *mtpt = (struct mp_port *)port;
2059
2060        mtpt->ier &= ~UART_IER_RLSI;
2061        mtpt->port.read_status_mask &= ~UART_LSR_DR;
2062        serial_out(mtpt, UART_IER, mtpt->ier);
2063}
2064
2065static void multi_enable_ms(struct sb_uart_port *port)
2066{
2067        struct mp_port *mtpt = (struct mp_port *)port;
2068
2069        mtpt->ier |= UART_IER_MSI;
2070        serial_out(mtpt, UART_IER, mtpt->ier);
2071}
2072
2073
2074static _INLINE_ void receive_chars(struct mp_port *mtpt, int *status )
2075{
2076        struct tty_struct *tty = mtpt->port.info->tty;
2077        unsigned char lsr = *status;
2078        int max_count = 256;
2079        unsigned char ch;
2080        char flag;
2081
2082        //lsr &= mtpt->port.read_status_mask;
2083
2084        do {
2085                if ((lsr & UART_LSR_PE) && (mtpt->port.mdmode & MDMODE_ENABLE))
2086                {
2087                        ch = serial_inp(mtpt, UART_RX);
2088                }
2089                else if (lsr & UART_LSR_SPECIAL) 
2090                {
2091                        flag = 0;
2092                        ch = serial_inp(mtpt, UART_RX);
2093
2094                        if (lsr & UART_LSR_BI) 
2095                        {
2096
2097                                mtpt->port.icount.brk++;
2098                                flag = TTY_BREAK;
2099
2100                                if (sb_uart_handle_break(&mtpt->port))
2101                                        goto ignore_char;
2102                        } 
2103                        if (lsr & UART_LSR_PE)
2104                        {
2105                                mtpt->port.icount.parity++;
2106                                flag = TTY_PARITY;
2107                        }
2108                        if (lsr & UART_LSR_FE)
2109                        {
2110                                mtpt->port.icount.frame++;
2111                                flag = TTY_FRAME;
2112                        }
2113                        if (lsr & UART_LSR_OE)
2114                        {
2115                                mtpt->port.icount.overrun++;
2116                                flag = TTY_OVERRUN;
2117                        }
2118                        tty_insert_flip_char(tty, ch, flag);
2119                }
2120                else
2121                {
2122                        ch = serial_inp(mtpt, UART_RX);
2123                        tty_insert_flip_char(tty, ch, 0);
2124                }
2125ignore_char:
2126                lsr = serial_inp(mtpt, UART_LSR);
2127        } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
2128
2129        tty_flip_buffer_push(tty);
2130}
2131
2132
2133
2134
2135static _INLINE_ void transmit_chars(struct mp_port *mtpt)
2136{
2137        struct circ_buf *xmit = &mtpt->port.info->xmit;
2138        int count;
2139
2140        if (mtpt->port.x_char) {
2141                serial_outp(mtpt, UART_TX, mtpt->port.x_char);
2142                mtpt->port.icount.tx++;
2143                mtpt->port.x_char = 0;
2144                return;
2145        }
2146        if (uart_circ_empty(xmit) || uart_tx_stopped(&mtpt->port)) {
2147                multi_stop_tx(&mtpt->port);
2148                return;
2149        }
2150
2151        count = uart_circ_chars_pending(xmit);
2152
2153        if(count > mtpt->port.fifosize)
2154        {
2155                count = mtpt->port.fifosize;
2156        }
2157
2158        printk("[%d] mdmode: %x\n", mtpt->port.line, mtpt->port.mdmode);
2159        do {
2160#if 0
2161                /* check multi-drop mode */
2162                if ((mtpt->port.mdmode & (MDMODE_ENABLE | MDMODE_ADDR)) == (MDMODE_ENABLE | MDMODE_ADDR))
2163                {
2164                        printk("send address\n");
2165                        /* send multi-drop address */
2166                        serial_out(mtpt, UART_SCR, xmit->buf[xmit->tail]);
2167                }
2168                else
2169#endif
2170                {
2171                        serial_out(mtpt, UART_TX, xmit->buf[xmit->tail]);
2172                }
2173                xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
2174                mtpt->port.icount.tx++;
2175        } while (--count > 0);
2176}
2177
2178
2179
2180static _INLINE_ void check_modem_status(struct mp_port *mtpt)
2181{
2182        int status;
2183
2184        status = serial_in(mtpt, UART_MSR);
2185
2186        if ((status & UART_MSR_ANY_DELTA) == 0)
2187                return;
2188
2189        if (status & UART_MSR_TERI)
2190                mtpt->port.icount.rng++;
2191        if (status & UART_MSR_DDSR)
2192                mtpt->port.icount.dsr++;
2193        if (status & UART_MSR_DDCD)
2194                sb_uart_handle_dcd_change(&mtpt->port, status & UART_MSR_DCD);
2195        if (status & UART_MSR_DCTS)
2196                sb_uart_handle_cts_change(&mtpt->port, status & UART_MSR_CTS);
2197
2198        wake_up_interruptible(&mtpt->port.info->delta_msr_wait);
2199}
2200
2201static inline void multi_handle_port(struct mp_port *mtpt)
2202{
2203        unsigned int status = serial_inp(mtpt, UART_LSR);
2204
2205        //printk("lsr: %x\n", status);
2206
2207        if ((status & UART_LSR_DR) || (status & UART_LSR_SPECIAL))
2208                receive_chars(mtpt, &status);
2209        check_modem_status(mtpt);
2210        if (status & UART_LSR_THRE)
2211        {
2212                if ((mtpt->port.type == PORT_16C105X)
2213                        || (mtpt->port.type == PORT_16C105XA))
2214                        transmit_chars(mtpt);
2215                else
2216                {
2217                        if (mtpt->interface >= RS485NE)
2218                                uart_set_mctrl(&mtpt->port, TIOCM_RTS);
2219                        
2220                        transmit_chars(mtpt);
2221
2222
2223                        if (mtpt->interface >= RS485NE)
2224                        {
2225                                while((status=serial_in(mtpt,UART_LSR) &0x60)!=0x60);
2226                                uart_clear_mctrl(&mtpt->port, TIOCM_RTS);
2227                        }
2228                }
2229        }
2230}
2231
2232
2233
2234static irqreturn_t multi_interrupt(int irq, void *dev_id)
2235{
2236        struct irq_info *iinfo = dev_id;
2237        struct list_head *lhead, *end = NULL;
2238        int pass_counter = 0;
2239
2240
2241        spin_lock(&iinfo->lock);
2242
2243        lhead = iinfo->head;
2244        do {
2245                struct mp_port *mtpt;
2246                unsigned int iir;
2247
2248                mtpt = list_entry(lhead, struct mp_port, list);
2249                
2250                iir = serial_in(mtpt, UART_IIR);
2251                printk("interrupt! port %d, iir 0x%x\n", mtpt->port.line, iir); //wlee
2252                if (!(iir & UART_IIR_NO_INT)) 
2253                {
2254                        printk("interrupt handle\n");
2255                        spin_lock(&mtpt->port.lock);
2256                        multi_handle_port(mtpt);
2257                        spin_unlock(&mtpt->port.lock);
2258
2259                        end = NULL;
2260                } else if (end == NULL)
2261                        end = lhead;
2262
2263                lhead = lhead->next;
2264                if (lhead == iinfo->head && pass_counter++ > PASS_LIMIT) 
2265                {
2266                        printk(KERN_ERR "multi: too much work for "
2267                                        "irq%d\n", irq);
2268                        printk( "multi: too much work for "
2269                                        "irq%d\n", irq);
2270                        break;
2271                }
2272        } while (lhead != end);
2273
2274        spin_unlock(&iinfo->lock);
2275
2276
2277        return IRQ_HANDLED;
2278}
2279
2280static void serial_do_unlink(struct irq_info *i, struct mp_port *mtpt)
2281{
2282        spin_lock_irq(&i->lock);
2283
2284        if (!list_empty(i->head)) {
2285                if (i->head == &mtpt->list)
2286                        i->head = i->head->next;
2287                list_del(&mtpt->list);
2288        } else {
2289                i->head = NULL;
2290        }
2291
2292        spin_unlock_irq(&i->lock);
2293}
2294
2295static int serial_link_irq_chain(struct mp_port *mtpt)
2296{
2297        struct irq_info *i = irq_lists + mtpt->port.irq;
2298        int ret, irq_flags = mtpt->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
2299        spin_lock_irq(&i->lock);
2300
2301        if (i->head) {
2302                list_add(&mtpt->list, i->head);
2303                spin_unlock_irq(&i->lock);
2304
2305                ret = 0;
2306        } else {
2307                INIT_LIST_HEAD(&mtpt->list);
2308                i->head = &mtpt->list;
2309                spin_unlock_irq(&i->lock);
2310
2311                ret = request_irq(mtpt->port.irq, multi_interrupt,
2312                                irq_flags, "serial", i);
2313                if (ret < 0)
2314                        serial_do_unlink(i, mtpt);
2315        }
2316
2317        return ret;
2318}
2319
2320
2321
2322
2323static void serial_unlink_irq_chain(struct mp_port *mtpt)
2324{
2325        struct irq_info *i = irq_lists + mtpt->port.irq;
2326
2327        if (list_empty(i->head))
2328        {
2329                free_irq(mtpt->port.irq, i);
2330        }
2331        serial_do_unlink(i, mtpt);
2332}
2333
2334static void multi_timeout(unsigned long data)
2335{
2336        struct mp_port *mtpt = (struct mp_port *)data;
2337
2338
2339        spin_lock(&mtpt->port.lock);
2340        multi_handle_port(mtpt);
2341        spin_unlock(&mtpt->port.lock);
2342
2343        mod_timer(&mtpt->timer, jiffies+1 );
2344}
2345
2346static unsigned int multi_tx_empty(struct sb_uart_port *port)
2347{
2348        struct mp_port *mtpt = (struct mp_port *)port;
2349        unsigned long flags;
2350        unsigned int ret;
2351
2352        spin_lock_irqsave(&mtpt->port.lock, flags);
2353        ret = serial_in(mtpt, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
2354        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2355
2356        return ret;
2357}
2358
2359
2360static unsigned int multi_get_mctrl(struct sb_uart_port *port)
2361{
2362        struct mp_port *mtpt = (struct mp_port *)port;
2363        unsigned char status;
2364        unsigned int ret;
2365
2366        status = serial_in(mtpt, UART_MSR);
2367
2368        ret = 0;
2369        if (status & UART_MSR_DCD)
2370                ret |= TIOCM_CAR;
2371        if (status & UART_MSR_RI)
2372                ret |= TIOCM_RNG;
2373        if (status & UART_MSR_DSR)
2374                ret |= TIOCM_DSR;
2375        if (status & UART_MSR_CTS)
2376                ret |= TIOCM_CTS;
2377        return ret;
2378}
2379
2380static void multi_set_mctrl(struct sb_uart_port *port, unsigned int mctrl)
2381{
2382        struct mp_port *mtpt = (struct mp_port *)port;
2383        unsigned char mcr = 0;
2384
2385        mctrl &= 0xff;
2386
2387        if (mctrl & TIOCM_RTS)
2388                mcr |= UART_MCR_RTS;
2389        if (mctrl & TIOCM_DTR)
2390                mcr |= UART_MCR_DTR;
2391        if (mctrl & TIOCM_OUT1)
2392                mcr |= UART_MCR_OUT1;
2393        if (mctrl & TIOCM_OUT2)
2394                mcr |= UART_MCR_OUT2;
2395        if (mctrl & TIOCM_LOOP)
2396                mcr |= UART_MCR_LOOP;
2397
2398
2399        serial_out(mtpt, UART_MCR, mcr);
2400}
2401
2402
2403static void multi_break_ctl(struct sb_uart_port *port, int break_state)
2404{
2405        struct mp_port *mtpt = (struct mp_port *)port;
2406        unsigned long flags;
2407
2408        spin_lock_irqsave(&mtpt->port.lock, flags);
2409        if (break_state == -1)
2410                mtpt->lcr |= UART_LCR_SBC;
2411        else
2412                mtpt->lcr &= ~UART_LCR_SBC;
2413        serial_out(mtpt, UART_LCR, mtpt->lcr);
2414        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2415}
2416
2417
2418
2419static int multi_startup(struct sb_uart_port *port)
2420{
2421        struct mp_port *mtpt = (struct mp_port *)port;
2422        unsigned long flags;
2423        int retval;
2424
2425        mtpt->capabilities = uart_config[mtpt->port.type].flags;
2426        mtpt->mcr = 0;
2427
2428        if (mtpt->capabilities & UART_CLEAR_FIFO) {
2429                serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
2430                serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO |
2431                                UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
2432                serial_outp(mtpt, UART_FCR, 0);
2433        }
2434
2435        (void) serial_inp(mtpt, UART_LSR);
2436        (void) serial_inp(mtpt, UART_RX);
2437        (void) serial_inp(mtpt, UART_IIR);
2438        (void) serial_inp(mtpt, UART_MSR);
2439        //test-wlee 9-bit disable
2440        serial_outp(mtpt, UART_MSR, 0);
2441
2442
2443        if (!(mtpt->port.flags & UPF_BUGGY_UART) &&
2444                        (serial_inp(mtpt, UART_LSR) == 0xff)) {
2445                printk("ttyS%d: LSR safety check engaged!\n", mtpt->port.line);
2446                //return -ENODEV;
2447        }
2448
2449        if ((!is_real_interrupt(mtpt->port.irq)) || (mtpt->poll_type==TYPE_POLL)) {
2450                unsigned int timeout = mtpt->port.timeout;
2451
2452                timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
2453
2454                mtpt->timer.data = (unsigned long)mtpt;
2455                mod_timer(&mtpt->timer, jiffies + timeout);
2456        } 
2457        else 
2458        {
2459                retval = serial_link_irq_chain(mtpt);
2460                if (retval)
2461                        return retval;
2462        }
2463
2464        serial_outp(mtpt, UART_LCR, UART_LCR_WLEN8);
2465
2466        spin_lock_irqsave(&mtpt->port.lock, flags);
2467        if ((is_real_interrupt(mtpt->port.irq))||(mtpt->poll_type==TYPE_INTERRUPT))
2468                mtpt->port.mctrl |= TIOCM_OUT2;
2469
2470        multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2471        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2472
2473        
2474        mtpt->ier = UART_IER_RLSI | UART_IER_RDI;
2475        serial_outp(mtpt, UART_IER, mtpt->ier);
2476
2477        (void) serial_inp(mtpt, UART_LSR);
2478        (void) serial_inp(mtpt, UART_RX);
2479        (void) serial_inp(mtpt, UART_IIR);
2480        (void) serial_inp(mtpt, UART_MSR);
2481
2482        return 0;
2483}
2484
2485
2486
2487static void multi_shutdown(struct sb_uart_port *port)
2488{
2489        struct mp_port *mtpt = (struct mp_port *)port;
2490        unsigned long flags;
2491
2492
2493        mtpt->ier = 0;
2494        serial_outp(mtpt, UART_IER, 0);
2495
2496        spin_lock_irqsave(&mtpt->port.lock, flags);
2497        mtpt->port.mctrl &= ~TIOCM_OUT2;
2498
2499        multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2500        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2501
2502        serial_out(mtpt, UART_LCR, serial_inp(mtpt, UART_LCR) & ~UART_LCR_SBC);
2503        serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO |
2504                        UART_FCR_CLEAR_RCVR |
2505                        UART_FCR_CLEAR_XMIT);
2506        serial_outp(mtpt, UART_FCR, 0);
2507
2508
2509        (void) serial_in(mtpt, UART_RX);
2510
2511        if ((!is_real_interrupt(mtpt->port.irq))||(mtpt->poll_type==TYPE_POLL))
2512        {
2513                del_timer_sync(&mtpt->timer);
2514        }
2515        else
2516        {
2517                serial_unlink_irq_chain(mtpt);
2518        }
2519}
2520
2521
2522
2523static unsigned int multi_get_divisor(struct sb_uart_port *port, unsigned int baud)
2524{
2525        unsigned int quot;
2526
2527        if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2528                        baud == (port->uartclk/4))
2529                quot = 0x8001;
2530        else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2531                        baud == (port->uartclk/8))
2532                quot = 0x8002;
2533        else
2534                quot = sb_uart_get_divisor(port, baud);
2535
2536        return quot;
2537}
2538
2539
2540
2541
2542static void multi_set_termios(struct sb_uart_port *port, struct MP_TERMIOS *termios, struct MP_TERMIOS *old)
2543{
2544        struct mp_port *mtpt = (struct mp_port *)port;
2545        unsigned char cval, fcr = 0;
2546        unsigned long flags;
2547        unsigned int baud, quot;
2548
2549        switch (termios->c_cflag & CSIZE) {
2550                case CS5:
2551                        cval = 0x00;
2552                        break;
2553                case CS6:
2554                        cval = 0x01;
2555                        break;
2556                case CS7:
2557                        cval = 0x02;
2558                        break;
2559                default:
2560                case CS8:
2561                        cval = 0x03;
2562                        break;
2563        }
2564
2565        if (termios->c_cflag & CSTOPB)
2566                cval |= 0x04;
2567        if (termios->c_cflag & PARENB)
2568                cval |= UART_LCR_PARITY;
2569        if (!(termios->c_cflag & PARODD))
2570                cval |= UART_LCR_EPAR;
2571
2572#ifdef CMSPAR
2573        if (termios->c_cflag & CMSPAR)
2574                cval |= UART_LCR_SPAR;
2575#endif
2576
2577        baud = sb_uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
2578        quot = multi_get_divisor(port, baud);
2579
2580        if (mtpt->capabilities & UART_USE_FIFO) {
2581                //if (baud < 2400)
2582                //      fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2583                //else
2584                //      fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
2585
2586                //      fcr = UART_FCR_ENABLE_FIFO | 0x90;
2587                        fcr = fcr_arr[mtpt->port.line];
2588        }
2589
2590        spin_lock_irqsave(&mtpt->port.lock, flags);
2591
2592        sb_uart_update_timeout(port, termios->c_cflag, baud);
2593
2594        mtpt->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2595        if (termios->c_iflag & INPCK)
2596                mtpt->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2597        if (termios->c_iflag & (BRKINT | PARMRK))
2598                mtpt->port.read_status_mask |= UART_LSR_BI;
2599
2600        mtpt->port.ignore_status_mask = 0;
2601        if (termios->c_iflag & IGNPAR)
2602                mtpt->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2603        if (termios->c_iflag & IGNBRK) {
2604                mtpt->port.ignore_status_mask |= UART_LSR_BI;
2605                if (termios->c_iflag & IGNPAR)
2606                        mtpt->port.ignore_status_mask |= UART_LSR_OE;
2607        }
2608
2609        if ((termios->c_cflag & CREAD) == 0)
2610                mtpt->port.ignore_status_mask |= UART_LSR_DR;
2611
2612        mtpt->ier &= ~UART_IER_MSI;
2613        if (UART_ENABLE_MS(&mtpt->port, termios->c_cflag))
2614                mtpt->ier |= UART_IER_MSI;
2615
2616        serial_out(mtpt, UART_IER, mtpt->ier);
2617
2618        if (mtpt->capabilities & UART_STARTECH) {
2619                serial_outp(mtpt, UART_LCR, 0xBF);
2620                serial_outp(mtpt, UART_EFR,
2621                                termios->c_cflag & CRTSCTS ? UART_EFR_CTS :0);
2622        }
2623
2624        serial_outp(mtpt, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2625
2626        serial_outp(mtpt, UART_DLL, quot & 0xff);     /* LS of divisor */
2627        serial_outp(mtpt, UART_DLM, quot >> 8);       /* MS of divisor */
2628
2629        serial_outp(mtpt, UART_LCR, cval);        /* reset DLAB */
2630        mtpt->lcr = cval;                 /* Save LCR */
2631
2632        if (fcr & UART_FCR_ENABLE_FIFO) {
2633                /* emulated UARTs (Lucent Venus 167x) need two steps */
2634                serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
2635        }
2636
2637        serial_outp(mtpt, UART_FCR, fcr);     /* set fcr */
2638
2639
2640        if ((mtpt->port.type == PORT_16C105X)
2641                || (mtpt->port.type == PORT_16C105XA))
2642        {
2643                if(deep[mtpt->port.line]!=0)
2644                        set_deep_fifo(port, ENABLE);
2645
2646                if (mtpt->interface != RS232)
2647                        set_auto_rts(port,mtpt->interface);
2648
2649        }
2650        else
2651        {
2652                if (mtpt->interface >= RS485NE)
2653                {
2654                        uart_clear_mctrl(&mtpt->port, TIOCM_RTS);
2655                }
2656        }
2657
2658        if(mtpt->device->device_id == PCI_DEVICE_ID_MP4M)
2659        {
2660                SendATCommand(mtpt);
2661                printk("SendATCommand\n");
2662        }       
2663        multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2664        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2665}
2666
2667static void multi_pm(struct sb_uart_port *port, unsigned int state, unsigned int oldstate)
2668{
2669        struct mp_port *mtpt = (struct mp_port *)port;
2670        if (state) {
2671                if (mtpt->capabilities & UART_STARTECH) {
2672                        serial_outp(mtpt, UART_LCR, 0xBF);
2673                        serial_outp(mtpt, UART_EFR, UART_EFR_ECB);
2674                        serial_outp(mtpt, UART_LCR, 0);
2675                        serial_outp(mtpt, UART_IER, UART_IERX_SLEEP);
2676                        serial_outp(mtpt, UART_LCR, 0xBF);
2677                        serial_outp(mtpt, UART_EFR, 0);
2678                        serial_outp(mtpt, UART_LCR, 0);
2679                }
2680
2681                if (mtpt->pm)
2682                        mtpt->pm(port, state, oldstate);
2683        } 
2684        else 
2685        {
2686                if (mtpt->capabilities & UART_STARTECH) {
2687                        serial_outp(mtpt, UART_LCR, 0xBF);
2688                        serial_outp(mtpt, UART_EFR, UART_EFR_ECB);
2689                        serial_outp(mtpt, UART_LCR, 0);
2690                        serial_outp(mtpt, UART_IER, 0);
2691                        serial_outp(mtpt, UART_LCR, 0xBF);
2692                        serial_outp(mtpt, UART_EFR, 0);
2693                        serial_outp(mtpt, UART_LCR, 0);
2694                }
2695
2696                if (mtpt->pm)
2697                        mtpt->pm(port, state, oldstate);
2698        }
2699}
2700
2701static void multi_release_std_resource(struct mp_port *mtpt)
2702{
2703        unsigned int size = 8 << mtpt->port.regshift;
2704
2705        switch (mtpt->port.iotype) {
2706                case UPIO_MEM:
2707                        if (!mtpt->port.mapbase)
2708                                break;
2709
2710                        if (mtpt->port.flags & UPF_IOREMAP) {
2711                                iounmap(mtpt->port.membase);
2712                                mtpt->port.membase = NULL;
2713                        }
2714
2715                        release_mem_region(mtpt->port.mapbase, size);
2716                        break;
2717
2718                case UPIO_HUB6:
2719                case UPIO_PORT:
2720                        release_region(mtpt->port.iobase,size);
2721                        break;
2722        }
2723}
2724
2725static void multi_release_port(struct sb_uart_port *port)
2726{
2727}
2728
2729static int multi_request_port(struct sb_uart_port *port)
2730{
2731        return 0;
2732}
2733
2734static void multi_config_port(struct sb_uart_port *port, int flags)
2735{
2736        struct mp_port *mtpt = (struct mp_port *)port;
2737        int probeflags = PROBE_ANY;
2738
2739        if (flags & UART_CONFIG_TYPE)
2740                autoconfig(mtpt, probeflags);
2741        if (mtpt->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2742                autoconfig_irq(mtpt);
2743
2744        if (mtpt->port.type == PORT_UNKNOWN)
2745                multi_release_std_resource(mtpt);
2746}
2747
2748static int multi_verify_port(struct sb_uart_port *port, struct serial_struct *ser)
2749{
2750        if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2751                        ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2752                        ser->type == PORT_STARTECH)
2753                return -EINVAL;
2754        return 0;
2755}
2756
2757static const char *multi_type(struct sb_uart_port *port)
2758{
2759        int type = port->type;
2760
2761        if (type >= ARRAY_SIZE(uart_config))
2762                type = 0;
2763        return uart_config[type].name;
2764}
2765
2766static struct sb_uart_ops multi_pops = {
2767        .tx_empty   = multi_tx_empty,
2768        .set_mctrl  = multi_set_mctrl,
2769        .get_mctrl  = multi_get_mctrl,
2770        .stop_tx    = multi_stop_tx,
2771        .start_tx   = multi_start_tx,
2772        .stop_rx    = multi_stop_rx,
2773        .enable_ms  = multi_enable_ms,
2774        .break_ctl  = multi_break_ctl,
2775        .startup    = multi_startup,
2776        .shutdown   = multi_shutdown,
2777        .set_termios    = multi_set_termios,
2778        .pm             = multi_pm,
2779        .type           = multi_type,
2780        .release_port   = multi_release_port,
2781        .request_port   = multi_request_port,
2782        .config_port    = multi_config_port,
2783        .verify_port    = multi_verify_port,
2784};
2785
2786static struct uart_driver multi_reg = {
2787        .owner          = THIS_MODULE,
2788        .driver_name    = "goldel_tulip",
2789        .dev_name       = "ttyMP",
2790        .major          = SB_TTY_MP_MAJOR,
2791        .minor          = 0,
2792        .nr             = MAX_MP_PORT, 
2793        .cons           = NULL,
2794};
2795
2796static void __init multi_init_ports(void)
2797{
2798        struct mp_port *mtpt;
2799        static int first = 1;
2800        int i,j,k;
2801        unsigned char osc;
2802        unsigned char b_ret = 0;
2803        static struct mp_device_t *sbdev; 
2804
2805        if (!first)
2806                return;
2807        first = 0;
2808
2809        mtpt = multi_ports; 
2810
2811        for (k=0;k<NR_BOARD;k++)
2812        {
2813                sbdev = &mp_devs[k];
2814
2815                for (i = 0; i < sbdev->nr_ports; i++, mtpt++) 
2816                {
2817                        mtpt->device            = sbdev;
2818                        mtpt->port.iobase   = sbdev->uart_access_addr + 8*i;
2819                        mtpt->port.irq      = sbdev->irq;
2820                        if ( ((sbdev->device_id == PCI_DEVICE_ID_MP4)&&(sbdev->revision==0x91)))
2821                                mtpt->interface_config_addr = sbdev->option_reg_addr + 0x08 + i;
2822                        else if (sbdev->revision == 0xc0)
2823                                mtpt->interface_config_addr = sbdev->option_reg_addr + 0x08 + (i & 0x1);
2824                        else
2825                                mtpt->interface_config_addr = sbdev->option_reg_addr + 0x08 + i/8;
2826
2827                        mtpt->option_base_addr = sbdev->option_reg_addr;
2828
2829                        mtpt->poll_type = sbdev->poll_type;
2830
2831                        mtpt->port.uartclk  = BASE_BAUD * 16;
2832
2833                        /* get input clock information */
2834                        osc = inb(sbdev->option_reg_addr + MP_OPTR_DIR0 + i/8) & 0x0F;
2835                        if (osc==0x0f)
2836                                osc = 0;
2837                        for(j=0;j<osc;j++)
2838                                mtpt->port.uartclk *= 2;
2839                        mtpt->port.flags    |= STD_COM_FLAGS | UPF_SHARE_IRQ ;
2840                        mtpt->port.iotype   = UPIO_PORT;
2841                        mtpt->port.ops      = &multi_pops;
2842
2843                        if (sbdev->revision == 0xc0)
2844                        {
2845                                /* for SB16C1053APCI */
2846                                b_ret = sb1053a_get_interface(mtpt, i);
2847                        }
2848                        else
2849                        {
2850                                b_ret = read_option_register(mtpt,(MP_OPTR_IIR0 + i/8));
2851                                printk("IIR_RET = %x\n",b_ret);
2852                        }
2853
2854                        /* default to RS232 */
2855                        mtpt->interface = RS232;
2856                        if (IIR_RS422 == (b_ret & IIR_TYPE_MASK))
2857                                mtpt->interface = RS422PTP;
2858                        if (IIR_RS485 == (b_ret & IIR_TYPE_MASK))
2859                                mtpt->interface = RS485NE;
2860                }
2861        }
2862}
2863
2864static void __init multi_register_ports(struct uart_driver *drv)
2865{
2866        int i;
2867
2868        multi_init_ports();
2869
2870        for (i = 0; i < NR_PORTS; i++) {
2871                struct mp_port *mtpt = &multi_ports[i];
2872
2873                mtpt->port.line = i;
2874                mtpt->port.ops = &multi_pops;
2875                init_timer(&mtpt->timer);
2876                mtpt->timer.function = multi_timeout;
2877                mp_add_one_port(drv, &mtpt->port);
2878        }
2879}
2880
2881/**
2882 * pci_remap_base - remap BAR value of pci device
2883 *
2884 * PARAMETERS
2885 *  pcidev  - pci_dev structure address
2886 *  offset  - BAR offset PCI_BASE_ADDRESS_0 ~ PCI_BASE_ADDRESS_4
2887 *  address - address to be changed BAR value
2888 *  size        - size of address space 
2889 *
2890 * RETURNS
2891 *  If this function performs successful, it returns 0. Otherwise, It returns -1.
2892 */
2893static int pci_remap_base(struct pci_dev *pcidev, unsigned int offset, 
2894                unsigned int address, unsigned int size) 
2895{
2896#if 0
2897        struct resource *root;
2898        unsigned index = (offset - 0x10) >> 2;
2899#endif
2900
2901        pci_write_config_dword(pcidev, offset, address);
2902#if 0
2903        root = pcidev->resource[index].parent;
2904        release_resource(&pcidev->resource[index]);
2905        address &= ~0x1;
2906        pcidev->resource[index].start = address;
2907        pcidev->resource[index].end       = address + size - 1;
2908
2909        if (request_resource(root, &pcidev->resource[index]) != NULL)
2910        {
2911                printk(KERN_ERR "pci remap conflict!! 0x%x\n", address);
2912                return (-1);
2913        }
2914#endif
2915
2916        return (0);
2917}
2918
2919static int init_mp_dev(struct pci_dev *pcidev, mppcibrd_t brd)
2920{
2921        static struct mp_device_t *sbdev = mp_devs;
2922        unsigned long addr = 0;
2923        int j;
2924        struct resource *ret = NULL;
2925
2926        sbdev->device_id = brd.device_id;
2927        pci_read_config_byte(pcidev, PCI_CLASS_REVISION, &(sbdev->revision));
2928        sbdev->name = brd.name;
2929        sbdev->uart_access_addr = pcidev->resource[0].start & PCI_BASE_ADDRESS_IO_MASK;
2930
2931        /* check revision. The SB16C1053APCI's option i/o address is BAR4 */
2932        if (sbdev->revision == 0xc0)
2933        {
2934                /* SB16C1053APCI */
2935                sbdev->option_reg_addr = pcidev->resource[4].start & PCI_BASE_ADDRESS_IO_MASK;
2936        }
2937        else
2938        {
2939                sbdev->option_reg_addr = pcidev->resource[1].start & PCI_BASE_ADDRESS_IO_MASK;
2940        }
2941#if 1   
2942        if (sbdev->revision == 0xc0)
2943        {
2944                outb(0x00, sbdev->option_reg_addr + MP_OPTR_GPOCR);
2945                inb(sbdev->option_reg_addr + MP_OPTR_GPOCR);
2946                outb(0x83, sbdev->option_reg_addr + MP_OPTR_GPOCR);
2947        }
2948#endif
2949
2950        sbdev->irq = pcidev->irq;
2951
2952        if ((brd.device_id & 0x0800) || !(brd.device_id &0xff00))
2953        {
2954                sbdev->poll_type = TYPE_INTERRUPT;
2955        }
2956        else
2957        {
2958                sbdev->poll_type = TYPE_POLL;
2959        }
2960
2961        /* codes which is specific to each board*/
2962        switch(brd.device_id){
2963                case PCI_DEVICE_ID_MP1 :
2964                case PCIE_DEVICE_ID_MP1 :
2965                case PCIE_DEVICE_ID_MP1E :
2966                case PCIE_DEVICE_ID_GT_MP1 :
2967                        sbdev->nr_ports = 1;
2968                        break;
2969                case PCI_DEVICE_ID_MP2 :
2970                case PCIE_DEVICE_ID_MP2 :
2971                case PCIE_DEVICE_ID_GT_MP2 :
2972                case PCIE_DEVICE_ID_MP2B :
2973                case PCIE_DEVICE_ID_MP2E :
2974                        sbdev->nr_ports = 2;
2975
2976                        /* serial base address remap */
2977                        if (sbdev->revision == 0xc0)
2978                        {
2979                                int prev_port_addr = 0;
2980
2981                                pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
2982                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
2983                        }
2984                        break;
2985                case PCI_DEVICE_ID_MP4 :
2986                case PCI_DEVICE_ID_MP4A :
2987                case PCIE_DEVICE_ID_MP4 :
2988                case PCI_DEVICE_ID_GT_MP4 :
2989                case PCI_DEVICE_ID_GT_MP4A :
2990                case PCIE_DEVICE_ID_GT_MP4 :
2991                case PCI_DEVICE_ID_MP4M :
2992                case PCIE_DEVICE_ID_MP4B :
2993                        sbdev->nr_ports = 4;
2994
2995                        if(sbdev->revision == 0x91){
2996                                sbdev->reserved_addr[0] = pcidev->resource[0].start & PCI_BASE_ADDRESS_IO_MASK;
2997                                outb(0x03 , sbdev->reserved_addr[0] + 0x01);
2998                                outb(0x03 , sbdev->reserved_addr[0] + 0x02);
2999                                outb(0x01 , sbdev->reserved_addr[0] + 0x20);
3000                                outb(0x00 , sbdev->reserved_addr[0] + 0x21);
3001                                request_region(sbdev->reserved_addr[0], 32, sbdev->name);
3002                                sbdev->uart_access_addr = pcidev->resource[1].start & PCI_BASE_ADDRESS_IO_MASK;
3003                                sbdev->option_reg_addr = pcidev->resource[2].start & PCI_BASE_ADDRESS_IO_MASK;
3004                        }
3005
3006                        /* SB16C1053APCI */
3007                        if (sbdev->revision == 0xc0)
3008                        {
3009                                int prev_port_addr = 0;
3010
3011                                pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
3012                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
3013                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_2, prev_port_addr + 16, 8);
3014                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_3, prev_port_addr + 24, 8);
3015                        }
3016                        break;
3017                case PCI_DEVICE_ID_MP6 :
3018                case PCI_DEVICE_ID_MP6A :
3019                case PCI_DEVICE_ID_GT_MP6 :
3020                case PCI_DEVICE_ID_GT_MP6A :
3021                        sbdev->nr_ports = 6;
3022
3023                        /* SB16C1053APCI */
3024                        if (sbdev->revision == 0xc0)
3025                        {
3026                                int prev_port_addr = 0;
3027
3028                                pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
3029                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
3030                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_2, prev_port_addr + 16, 16);
3031                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_3, prev_port_addr + 32, 16);
3032                        }
3033                        break;
3034                case PCI_DEVICE_ID_MP8 :
3035                case PCIE_DEVICE_ID_MP8 :
3036                case PCI_DEVICE_ID_GT_MP8 :
3037                case PCIE_DEVICE_ID_GT_MP8 :
3038                case PCIE_DEVICE_ID_MP8B :
3039                        sbdev->nr_ports = 8;
3040                        break;
3041                case PCI_DEVICE_ID_MP32 :
3042                case PCIE_DEVICE_ID_MP32 :
3043                case PCI_DEVICE_ID_GT_MP32 :
3044                case PCIE_DEVICE_ID_GT_MP32 :
3045                        {
3046                                int portnum_hex=0;
3047                                portnum_hex = inb(sbdev->option_reg_addr);
3048                                sbdev->nr_ports = ((portnum_hex/16)*10) + (portnum_hex % 16);
3049                        }
3050                        break;
3051#ifdef CONFIG_PARPORT_PC
3052                case PCI_DEVICE_ID_MP2S1P :
3053                        sbdev->nr_ports = 2;
3054
3055                        /* SB16C1053APCI */
3056                        if (sbdev->revision == 0xc0)
3057                        {
3058                                int prev_port_addr = 0;
3059
3060                                pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
3061                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
3062                        }
3063
3064                        /* add PC compatible parallel port */
3065                        parport_pc_probe_port(pcidev->resource[2].start, pcidev->resource[3].start, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &pcidev->dev, 0);
3066                        break;
3067                case PCI_DEVICE_ID_MP1P :
3068                        /* add PC compatible parallel port */
3069                        parport_pc_probe_port(pcidev->resource[2].start, pcidev->resource[3].start, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &pcidev->dev, 0);
3070                        break;
3071#endif
3072        }
3073
3074        ret = request_region(sbdev->uart_access_addr, (8*sbdev->nr_ports), sbdev->name);
3075
3076        if (sbdev->revision == 0xc0)
3077        {
3078                ret = request_region(sbdev->option_reg_addr, 0x40, sbdev->name);
3079        }
3080        else
3081        {
3082                ret = request_region(sbdev->option_reg_addr, 0x20, sbdev->name);
3083        }
3084
3085
3086        NR_BOARD++;
3087        NR_PORTS += sbdev->nr_ports;
3088
3089        /* Enable PCI interrupt */
3090        addr = sbdev->option_reg_addr + MP_OPTR_IMR0;
3091        for(j=0; j < (sbdev->nr_ports/8)+1; j++)
3092        {
3093                if (sbdev->poll_type == TYPE_INTERRUPT)
3094                {
3095                        outb(0xff,addr +j);
3096                }
3097        }
3098        sbdev++;
3099
3100        return 0;
3101}
3102
3103static int __init multi_init(void)
3104{
3105        int ret, i;
3106        struct pci_dev  *dev = NULL;
3107
3108        if(fcr_count==0)
3109        {
3110                for(i=0;i<256;i++)
3111                {
3112                        fcr_arr[i] = 0x01;
3113                        
3114                }
3115        }
3116        if(deep_count==0)
3117        {
3118                for(i=0;i<256;i++)
3119                {
3120                        deep[i] = 1;
3121                        
3122                }
3123        }
3124        if(rtr_count==0)
3125        {
3126                for(i=0;i<256;i++)
3127                {
3128                        rtr[i] = 0x10;
3129                }
3130        }
3131        if(ttr_count==0)
3132        {
3133                for(i=0;i<256;i++)
3134                {
3135                        ttr[i] = 0x38;
3136                }
3137        }
3138
3139
3140printk("MULTI INIT\n");
3141        for( i=0; i< mp_nrpcibrds; i++)
3142        {
3143
3144                while( (dev = pci_get_device(mp_pciboards[i].vendor_id, mp_pciboards[i].device_id, dev) ) )
3145
3146                {
3147printk("FOUND~~~\n");
3148//      Cent OS bug fix
3149//                      if (mp_pciboards[i].device_id & 0x0800)
3150                        {
3151                                int status;
3152                                pci_disable_device(dev);
3153                                status = pci_enable_device(dev);
3154            
3155                                if (status != 0)
3156                                { 
3157                                        printk("Multiport Board Enable Fail !\n\n");
3158                                        status = -ENXIO;
3159                                        return status;
3160                                }
3161                        }
3162
3163                        init_mp_dev(dev, mp_pciboards[i]);      
3164                }
3165        }
3166
3167        for (i = 0; i < NR_IRQS; i++)
3168                spin_lock_init(&irq_lists[i].lock);
3169
3170        ret = mp_register_driver(&multi_reg);
3171
3172        if (ret >= 0)
3173                multi_register_ports(&multi_reg);
3174
3175        return ret;
3176}
3177
3178static void __exit multi_exit(void)
3179{
3180        int i;
3181
3182        for (i = 0; i < NR_PORTS; i++)
3183                mp_remove_one_port(&multi_reg, &multi_ports[i].port);
3184
3185        mp_unregister_driver(&multi_reg);
3186}
3187
3188module_init(multi_init);
3189module_exit(multi_exit);
3190
3191MODULE_DESCRIPTION("SystemBase Multiport PCI/PCIe CORE");
3192MODULE_LICENSE("GPL");
3193