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 && (info->tty->termios.c_cflag & CBAUD))
 547                                uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
 548                }
 549
 550                info->flags |= UIF_INITIALIZED;
 551
 552                if (info->tty)
 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    kfree(drv->state);
1812
1813}
1814
1815static int mp_add_one_port(struct uart_driver *drv, struct sb_uart_port *port)
1816{
1817        struct sb_uart_state *state;
1818        int ret = 0;
1819
1820
1821        if (port->line >= drv->nr)
1822                return -EINVAL;
1823
1824        state = drv->state + port->line;
1825
1826        MP_MUTEX_LOCK(mp_mutex);
1827        if (state->port) {
1828                ret = -EINVAL;
1829                goto out;
1830        }
1831
1832        state->port = port;
1833
1834        spin_lock_init(&port->lock);
1835        port->cons = drv->cons;
1836        port->info = state->info;
1837
1838        mp_configure_port(drv, state, port);
1839
1840        tty_register_device(drv->tty_driver, port->line, port->dev);
1841
1842out:
1843        MP_MUTEX_UNLOCK(mp_mutex);
1844
1845
1846        return ret;
1847}
1848
1849static int mp_remove_one_port(struct uart_driver *drv, struct sb_uart_port *port)
1850{
1851        struct sb_uart_state *state = drv->state + port->line;
1852
1853        if (state->port != port)
1854                printk(KERN_ALERT "Removing wrong port: %p != %p\n",
1855                                state->port, port);
1856
1857        MP_MUTEX_LOCK(mp_mutex);
1858
1859        tty_unregister_device(drv->tty_driver, port->line);
1860
1861        mp_unconfigure_port(drv, state);
1862        state->port = NULL;
1863        MP_MUTEX_UNLOCK(mp_mutex);
1864
1865        return 0;
1866}
1867
1868static void autoconfig(struct mp_port *mtpt, unsigned int probeflags)
1869{
1870        unsigned char status1, scratch, scratch2, scratch3;
1871        unsigned char save_lcr, save_mcr;
1872        unsigned long flags;
1873
1874        unsigned char u_type;
1875        unsigned char b_ret = 0;
1876
1877        if (!mtpt->port.iobase && !mtpt->port.mapbase && !mtpt->port.membase)
1878                return;
1879
1880        DEBUG_AUTOCONF("ttyMP%d: autoconf (0x%04x, 0x%p): ",
1881                        mtpt->port.line, mtpt->port.iobase, mtpt->port.membase);
1882
1883        spin_lock_irqsave(&mtpt->port.lock, flags);
1884
1885        if (!(mtpt->port.flags & UPF_BUGGY_UART)) {
1886                scratch = serial_inp(mtpt, UART_IER);
1887                serial_outp(mtpt, UART_IER, 0);
1888#ifdef __i386__
1889                outb(0xff, 0x080);
1890#endif
1891                scratch2 = serial_inp(mtpt, UART_IER) & 0x0f;
1892                serial_outp(mtpt, UART_IER, 0x0F);
1893#ifdef __i386__
1894                outb(0, 0x080);
1895#endif
1896                scratch3 = serial_inp(mtpt, UART_IER) & 0x0F;
1897                serial_outp(mtpt, UART_IER, scratch);
1898                if (scratch2 != 0 || scratch3 != 0x0F) {
1899                        DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1900                                        scratch2, scratch3);
1901                        goto out;
1902                }
1903        }
1904
1905        save_mcr = serial_in(mtpt, UART_MCR);
1906        save_lcr = serial_in(mtpt, UART_LCR);
1907
1908        if (!(mtpt->port.flags & UPF_SKIP_TEST)) {
1909                serial_outp(mtpt, UART_MCR, UART_MCR_LOOP | 0x0A);
1910                status1 = serial_inp(mtpt, UART_MSR) & 0xF0;
1911                serial_outp(mtpt, UART_MCR, save_mcr);
1912                if (status1 != 0x90) {
1913                        DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1914                                        status1);
1915                        goto out;
1916                }
1917        }
1918
1919        serial_outp(mtpt, UART_LCR, 0xBF);
1920        serial_outp(mtpt, UART_EFR, 0);
1921        serial_outp(mtpt, UART_LCR, 0);
1922
1923        serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
1924        scratch = serial_in(mtpt, UART_IIR) >> 6;
1925
1926        DEBUG_AUTOCONF("iir=%d ", scratch);
1927        if(mtpt->device->nr_ports >= 8)
1928                b_ret = read_option_register(mtpt,(MP_OPTR_DIR0 + ((mtpt->port.line)/8)));
1929        else    
1930                b_ret = read_option_register(mtpt,MP_OPTR_DIR0);
1931        u_type = (b_ret & 0xf0) >> 4;
1932        if(mtpt->port.type == PORT_UNKNOWN )
1933        {
1934                switch (u_type)
1935                {
1936                        case DIR_UART_16C550:
1937                                mtpt->port.type = PORT_16C55X;
1938                                break;
1939                        case DIR_UART_16C1050:
1940                                mtpt->port.type = PORT_16C105X;
1941                                break;
1942                        case DIR_UART_16C1050A:
1943                                if (mtpt->port.line < 2)
1944                                {
1945                                        mtpt->port.type = PORT_16C105XA;
1946                                }
1947                                else
1948                                {
1949                                        if (mtpt->device->device_id & 0x50)
1950                                        {
1951                                                mtpt->port.type = PORT_16C55X;
1952                                        }
1953                                        else
1954                                        {
1955                                                mtpt->port.type = PORT_16C105X;
1956                                        }
1957                                }
1958                                break;
1959                        default:        
1960                                mtpt->port.type = PORT_UNKNOWN;
1961                                break;
1962                }
1963        }
1964
1965        if(mtpt->port.type == PORT_UNKNOWN )
1966        {
1967printk("unknow2\n");
1968                switch (scratch) {
1969                        case 0:
1970                        case 1:
1971                                mtpt->port.type = PORT_UNKNOWN;
1972                                break;
1973                        case 2:
1974                        case 3:
1975                                mtpt->port.type = PORT_16C55X;
1976                                break;
1977                }
1978        }
1979
1980        serial_outp(mtpt, UART_LCR, save_lcr);
1981
1982        mtpt->port.fifosize = uart_config[mtpt->port.type].dfl_xmit_fifo_size;
1983        mtpt->capabilities = uart_config[mtpt->port.type].flags;
1984
1985        if (mtpt->port.type == PORT_UNKNOWN)
1986                goto out;
1987        serial_outp(mtpt, UART_MCR, save_mcr);
1988        serial_outp(mtpt, UART_FCR, (UART_FCR_ENABLE_FIFO |
1989                                UART_FCR_CLEAR_RCVR |
1990                                UART_FCR_CLEAR_XMIT));
1991        serial_outp(mtpt, UART_FCR, 0);
1992        (void)serial_in(mtpt, UART_RX);
1993        serial_outp(mtpt, UART_IER, 0);
1994
1995out:
1996        spin_unlock_irqrestore(&mtpt->port.lock, flags);
1997        DEBUG_AUTOCONF("type=%s\n", uart_config[mtpt->port.type].name);
1998}
1999
2000static void autoconfig_irq(struct mp_port *mtpt)
2001{
2002        unsigned char save_mcr, save_ier;
2003        unsigned long irqs;
2004        int irq;
2005
2006        /* forget possible initially masked and pending IRQ */
2007        probe_irq_off(probe_irq_on());
2008        save_mcr = serial_inp(mtpt, UART_MCR);
2009        save_ier = serial_inp(mtpt, UART_IER);
2010        serial_outp(mtpt, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
2011
2012        irqs = probe_irq_on();
2013        serial_outp(mtpt, UART_MCR, 0);
2014        serial_outp(mtpt, UART_MCR,
2015                UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2016
2017        serial_outp(mtpt, UART_IER, 0x0f);    /* enable all intrs */
2018        (void)serial_inp(mtpt, UART_LSR);
2019        (void)serial_inp(mtpt, UART_RX);
2020        (void)serial_inp(mtpt, UART_IIR);
2021        (void)serial_inp(mtpt, UART_MSR);
2022        serial_outp(mtpt, UART_TX, 0xFF);
2023        irq = probe_irq_off(irqs);
2024
2025        serial_outp(mtpt, UART_MCR, save_mcr);
2026        serial_outp(mtpt, UART_IER, save_ier);
2027
2028        mtpt->port.irq = (irq > 0) ? irq : 0;
2029}
2030
2031static void multi_stop_tx(struct sb_uart_port *port)
2032{
2033        struct mp_port *mtpt = (struct mp_port *)port;
2034
2035        if (mtpt->ier & UART_IER_THRI) {
2036                mtpt->ier &= ~UART_IER_THRI;
2037                serial_out(mtpt, UART_IER, mtpt->ier);
2038        }
2039
2040        tasklet_schedule(&port->info->tlet);
2041}
2042
2043static void multi_start_tx(struct sb_uart_port *port)
2044{
2045        struct mp_port *mtpt = (struct mp_port *)port;
2046
2047        if (!(mtpt->ier & UART_IER_THRI)) {
2048                mtpt->ier |= UART_IER_THRI;
2049                serial_out(mtpt, UART_IER, mtpt->ier);
2050        }
2051}
2052
2053static void multi_stop_rx(struct sb_uart_port *port)
2054{
2055        struct mp_port *mtpt = (struct mp_port *)port;
2056
2057        mtpt->ier &= ~UART_IER_RLSI;
2058        mtpt->port.read_status_mask &= ~UART_LSR_DR;
2059        serial_out(mtpt, UART_IER, mtpt->ier);
2060}
2061
2062static void multi_enable_ms(struct sb_uart_port *port)
2063{
2064        struct mp_port *mtpt = (struct mp_port *)port;
2065
2066        mtpt->ier |= UART_IER_MSI;
2067        serial_out(mtpt, UART_IER, mtpt->ier);
2068}
2069
2070
2071static _INLINE_ void receive_chars(struct mp_port *mtpt, int *status )
2072{
2073        struct tty_struct *tty = mtpt->port.info->tty;
2074        unsigned char lsr = *status;
2075        int max_count = 256;
2076        unsigned char ch;
2077        char flag;
2078
2079        //lsr &= mtpt->port.read_status_mask;
2080
2081        do {
2082                if ((lsr & UART_LSR_PE) && (mtpt->port.mdmode & MDMODE_ENABLE))
2083                {
2084                        ch = serial_inp(mtpt, UART_RX);
2085                }
2086                else if (lsr & UART_LSR_SPECIAL) 
2087                {
2088                        flag = 0;
2089                        ch = serial_inp(mtpt, UART_RX);
2090
2091                        if (lsr & UART_LSR_BI) 
2092                        {
2093
2094                                mtpt->port.icount.brk++;
2095                                flag = TTY_BREAK;
2096
2097                                if (sb_uart_handle_break(&mtpt->port))
2098                                        goto ignore_char;
2099                        } 
2100                        if (lsr & UART_LSR_PE)
2101                        {
2102                                mtpt->port.icount.parity++;
2103                                flag = TTY_PARITY;
2104                        }
2105                        if (lsr & UART_LSR_FE)
2106                        {
2107                                mtpt->port.icount.frame++;
2108                                flag = TTY_FRAME;
2109                        }
2110                        if (lsr & UART_LSR_OE)
2111                        {
2112                                mtpt->port.icount.overrun++;
2113                                flag = TTY_OVERRUN;
2114                        }
2115                        tty_insert_flip_char(tty, ch, flag);
2116                }
2117                else
2118                {
2119                        ch = serial_inp(mtpt, UART_RX);
2120                        tty_insert_flip_char(tty, ch, 0);
2121                }
2122ignore_char:
2123                lsr = serial_inp(mtpt, UART_LSR);
2124        } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
2125
2126        tty_flip_buffer_push(tty);
2127}
2128
2129
2130
2131
2132static _INLINE_ void transmit_chars(struct mp_port *mtpt)
2133{
2134        struct circ_buf *xmit = &mtpt->port.info->xmit;
2135        int count;
2136
2137        if (mtpt->port.x_char) {
2138                serial_outp(mtpt, UART_TX, mtpt->port.x_char);
2139                mtpt->port.icount.tx++;
2140                mtpt->port.x_char = 0;
2141                return;
2142        }
2143        if (uart_circ_empty(xmit) || uart_tx_stopped(&mtpt->port)) {
2144                multi_stop_tx(&mtpt->port);
2145                return;
2146        }
2147
2148        count = uart_circ_chars_pending(xmit);
2149
2150        if(count > mtpt->port.fifosize)
2151        {
2152                count = mtpt->port.fifosize;
2153        }
2154
2155        printk("[%d] mdmode: %x\n", mtpt->port.line, mtpt->port.mdmode);
2156        do {
2157#if 0
2158                /* check multi-drop mode */
2159                if ((mtpt->port.mdmode & (MDMODE_ENABLE | MDMODE_ADDR)) == (MDMODE_ENABLE | MDMODE_ADDR))
2160                {
2161                        printk("send address\n");
2162                        /* send multi-drop address */
2163                        serial_out(mtpt, UART_SCR, xmit->buf[xmit->tail]);
2164                }
2165                else
2166#endif
2167                {
2168                        serial_out(mtpt, UART_TX, xmit->buf[xmit->tail]);
2169                }
2170                xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
2171                mtpt->port.icount.tx++;
2172        } while (--count > 0);
2173}
2174
2175
2176
2177static _INLINE_ void check_modem_status(struct mp_port *mtpt)
2178{
2179        int status;
2180
2181        status = serial_in(mtpt, UART_MSR);
2182
2183        if ((status & UART_MSR_ANY_DELTA) == 0)
2184                return;
2185
2186        if (status & UART_MSR_TERI)
2187                mtpt->port.icount.rng++;
2188        if (status & UART_MSR_DDSR)
2189                mtpt->port.icount.dsr++;
2190        if (status & UART_MSR_DDCD)
2191                sb_uart_handle_dcd_change(&mtpt->port, status & UART_MSR_DCD);
2192        if (status & UART_MSR_DCTS)
2193                sb_uart_handle_cts_change(&mtpt->port, status & UART_MSR_CTS);
2194
2195        wake_up_interruptible(&mtpt->port.info->delta_msr_wait);
2196}
2197
2198static inline void multi_handle_port(struct mp_port *mtpt)
2199{
2200        unsigned int status = serial_inp(mtpt, UART_LSR);
2201
2202        //printk("lsr: %x\n", status);
2203
2204        if ((status & UART_LSR_DR) || (status & UART_LSR_SPECIAL))
2205                receive_chars(mtpt, &status);
2206        check_modem_status(mtpt);
2207        if (status & UART_LSR_THRE)
2208        {
2209                if ((mtpt->port.type == PORT_16C105X)
2210                        || (mtpt->port.type == PORT_16C105XA))
2211                        transmit_chars(mtpt);
2212                else
2213                {
2214                        if (mtpt->interface >= RS485NE)
2215                                uart_set_mctrl(&mtpt->port, TIOCM_RTS);
2216                        
2217                        transmit_chars(mtpt);
2218
2219
2220                        if (mtpt->interface >= RS485NE)
2221                        {
2222                                while((status=serial_in(mtpt,UART_LSR) &0x60)!=0x60);
2223                                uart_clear_mctrl(&mtpt->port, TIOCM_RTS);
2224                        }
2225                }
2226        }
2227}
2228
2229
2230
2231static irqreturn_t multi_interrupt(int irq, void *dev_id)
2232{
2233        struct irq_info *iinfo = dev_id;
2234        struct list_head *lhead, *end = NULL;
2235        int pass_counter = 0;
2236
2237
2238        spin_lock(&iinfo->lock);
2239
2240        lhead = iinfo->head;
2241        do {
2242                struct mp_port *mtpt;
2243                unsigned int iir;
2244
2245                mtpt = list_entry(lhead, struct mp_port, list);
2246                
2247                iir = serial_in(mtpt, UART_IIR);
2248                printk("interrupt! port %d, iir 0x%x\n", mtpt->port.line, iir); //wlee
2249                if (!(iir & UART_IIR_NO_INT)) 
2250                {
2251                        printk("interrupt handle\n");
2252                        spin_lock(&mtpt->port.lock);
2253                        multi_handle_port(mtpt);
2254                        spin_unlock(&mtpt->port.lock);
2255
2256                        end = NULL;
2257                } else if (end == NULL)
2258                        end = lhead;
2259
2260                lhead = lhead->next;
2261                if (lhead == iinfo->head && pass_counter++ > PASS_LIMIT) 
2262                {
2263                        printk(KERN_ERR "multi: too much work for "
2264                                        "irq%d\n", irq);
2265                        printk( "multi: too much work for "
2266                                        "irq%d\n", irq);
2267                        break;
2268                }
2269        } while (lhead != end);
2270
2271        spin_unlock(&iinfo->lock);
2272
2273
2274        return IRQ_HANDLED;
2275}
2276
2277static void serial_do_unlink(struct irq_info *i, struct mp_port *mtpt)
2278{
2279        spin_lock_irq(&i->lock);
2280
2281        if (!list_empty(i->head)) {
2282                if (i->head == &mtpt->list)
2283                        i->head = i->head->next;
2284                list_del(&mtpt->list);
2285        } else {
2286                i->head = NULL;
2287        }
2288
2289        spin_unlock_irq(&i->lock);
2290}
2291
2292static int serial_link_irq_chain(struct mp_port *mtpt)
2293{
2294        struct irq_info *i = irq_lists + mtpt->port.irq;
2295        int ret, irq_flags = mtpt->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
2296        spin_lock_irq(&i->lock);
2297
2298        if (i->head) {
2299                list_add(&mtpt->list, i->head);
2300                spin_unlock_irq(&i->lock);
2301
2302                ret = 0;
2303        } else {
2304                INIT_LIST_HEAD(&mtpt->list);
2305                i->head = &mtpt->list;
2306                spin_unlock_irq(&i->lock);
2307
2308                ret = request_irq(mtpt->port.irq, multi_interrupt,
2309                                irq_flags, "serial", i);
2310                if (ret < 0)
2311                        serial_do_unlink(i, mtpt);
2312        }
2313
2314        return ret;
2315}
2316
2317
2318
2319
2320static void serial_unlink_irq_chain(struct mp_port *mtpt)
2321{
2322        struct irq_info *i = irq_lists + mtpt->port.irq;
2323
2324        if (list_empty(i->head))
2325        {
2326                free_irq(mtpt->port.irq, i);
2327        }
2328        serial_do_unlink(i, mtpt);
2329}
2330
2331static void multi_timeout(unsigned long data)
2332{
2333        struct mp_port *mtpt = (struct mp_port *)data;
2334
2335
2336        spin_lock(&mtpt->port.lock);
2337        multi_handle_port(mtpt);
2338        spin_unlock(&mtpt->port.lock);
2339
2340        mod_timer(&mtpt->timer, jiffies+1 );
2341}
2342
2343static unsigned int multi_tx_empty(struct sb_uart_port *port)
2344{
2345        struct mp_port *mtpt = (struct mp_port *)port;
2346        unsigned long flags;
2347        unsigned int ret;
2348
2349        spin_lock_irqsave(&mtpt->port.lock, flags);
2350        ret = serial_in(mtpt, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
2351        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2352
2353        return ret;
2354}
2355
2356
2357static unsigned int multi_get_mctrl(struct sb_uart_port *port)
2358{
2359        struct mp_port *mtpt = (struct mp_port *)port;
2360        unsigned char status;
2361        unsigned int ret;
2362
2363        status = serial_in(mtpt, UART_MSR);
2364
2365        ret = 0;
2366        if (status & UART_MSR_DCD)
2367                ret |= TIOCM_CAR;
2368        if (status & UART_MSR_RI)
2369                ret |= TIOCM_RNG;
2370        if (status & UART_MSR_DSR)
2371                ret |= TIOCM_DSR;
2372        if (status & UART_MSR_CTS)
2373                ret |= TIOCM_CTS;
2374        return ret;
2375}
2376
2377static void multi_set_mctrl(struct sb_uart_port *port, unsigned int mctrl)
2378{
2379        struct mp_port *mtpt = (struct mp_port *)port;
2380        unsigned char mcr = 0;
2381
2382        mctrl &= 0xff;
2383
2384        if (mctrl & TIOCM_RTS)
2385                mcr |= UART_MCR_RTS;
2386        if (mctrl & TIOCM_DTR)
2387                mcr |= UART_MCR_DTR;
2388        if (mctrl & TIOCM_OUT1)
2389                mcr |= UART_MCR_OUT1;
2390        if (mctrl & TIOCM_OUT2)
2391                mcr |= UART_MCR_OUT2;
2392        if (mctrl & TIOCM_LOOP)
2393                mcr |= UART_MCR_LOOP;
2394
2395
2396        serial_out(mtpt, UART_MCR, mcr);
2397}
2398
2399
2400static void multi_break_ctl(struct sb_uart_port *port, int break_state)
2401{
2402        struct mp_port *mtpt = (struct mp_port *)port;
2403        unsigned long flags;
2404
2405        spin_lock_irqsave(&mtpt->port.lock, flags);
2406        if (break_state == -1)
2407                mtpt->lcr |= UART_LCR_SBC;
2408        else
2409                mtpt->lcr &= ~UART_LCR_SBC;
2410        serial_out(mtpt, UART_LCR, mtpt->lcr);
2411        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2412}
2413
2414
2415
2416static int multi_startup(struct sb_uart_port *port)
2417{
2418        struct mp_port *mtpt = (struct mp_port *)port;
2419        unsigned long flags;
2420        int retval;
2421
2422        mtpt->capabilities = uart_config[mtpt->port.type].flags;
2423        mtpt->mcr = 0;
2424
2425        if (mtpt->capabilities & UART_CLEAR_FIFO) {
2426                serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
2427                serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO |
2428                                UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
2429                serial_outp(mtpt, UART_FCR, 0);
2430        }
2431
2432        (void) serial_inp(mtpt, UART_LSR);
2433        (void) serial_inp(mtpt, UART_RX);
2434        (void) serial_inp(mtpt, UART_IIR);
2435        (void) serial_inp(mtpt, UART_MSR);
2436        //test-wlee 9-bit disable
2437        serial_outp(mtpt, UART_MSR, 0);
2438
2439
2440        if (!(mtpt->port.flags & UPF_BUGGY_UART) &&
2441                        (serial_inp(mtpt, UART_LSR) == 0xff)) {
2442                printk("ttyS%d: LSR safety check engaged!\n", mtpt->port.line);
2443                //return -ENODEV;
2444        }
2445
2446        if ((!is_real_interrupt(mtpt->port.irq)) || (mtpt->poll_type==TYPE_POLL)) {
2447                unsigned int timeout = mtpt->port.timeout;
2448
2449                timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
2450
2451                mtpt->timer.data = (unsigned long)mtpt;
2452                mod_timer(&mtpt->timer, jiffies + timeout);
2453        } 
2454        else 
2455        {
2456                retval = serial_link_irq_chain(mtpt);
2457                if (retval)
2458                        return retval;
2459        }
2460
2461        serial_outp(mtpt, UART_LCR, UART_LCR_WLEN8);
2462
2463        spin_lock_irqsave(&mtpt->port.lock, flags);
2464        if ((is_real_interrupt(mtpt->port.irq))||(mtpt->poll_type==TYPE_INTERRUPT))
2465                mtpt->port.mctrl |= TIOCM_OUT2;
2466
2467        multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2468        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2469
2470        
2471        mtpt->ier = UART_IER_RLSI | UART_IER_RDI;
2472        serial_outp(mtpt, UART_IER, mtpt->ier);
2473
2474        (void) serial_inp(mtpt, UART_LSR);
2475        (void) serial_inp(mtpt, UART_RX);
2476        (void) serial_inp(mtpt, UART_IIR);
2477        (void) serial_inp(mtpt, UART_MSR);
2478
2479        return 0;
2480}
2481
2482
2483
2484static void multi_shutdown(struct sb_uart_port *port)
2485{
2486        struct mp_port *mtpt = (struct mp_port *)port;
2487        unsigned long flags;
2488
2489
2490        mtpt->ier = 0;
2491        serial_outp(mtpt, UART_IER, 0);
2492
2493        spin_lock_irqsave(&mtpt->port.lock, flags);
2494        mtpt->port.mctrl &= ~TIOCM_OUT2;
2495
2496        multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2497        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2498
2499        serial_out(mtpt, UART_LCR, serial_inp(mtpt, UART_LCR) & ~UART_LCR_SBC);
2500        serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO |
2501                        UART_FCR_CLEAR_RCVR |
2502                        UART_FCR_CLEAR_XMIT);
2503        serial_outp(mtpt, UART_FCR, 0);
2504
2505
2506        (void) serial_in(mtpt, UART_RX);
2507
2508        if ((!is_real_interrupt(mtpt->port.irq))||(mtpt->poll_type==TYPE_POLL))
2509        {
2510                del_timer_sync(&mtpt->timer);
2511        }
2512        else
2513        {
2514                serial_unlink_irq_chain(mtpt);
2515        }
2516}
2517
2518
2519
2520static unsigned int multi_get_divisor(struct sb_uart_port *port, unsigned int baud)
2521{
2522        unsigned int quot;
2523
2524        if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2525                        baud == (port->uartclk/4))
2526                quot = 0x8001;
2527        else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2528                        baud == (port->uartclk/8))
2529                quot = 0x8002;
2530        else
2531                quot = sb_uart_get_divisor(port, baud);
2532
2533        return quot;
2534}
2535
2536
2537
2538
2539static void multi_set_termios(struct sb_uart_port *port, struct MP_TERMIOS *termios, struct MP_TERMIOS *old)
2540{
2541        struct mp_port *mtpt = (struct mp_port *)port;
2542        unsigned char cval, fcr = 0;
2543        unsigned long flags;
2544        unsigned int baud, quot;
2545
2546        switch (termios->c_cflag & CSIZE) {
2547                case CS5:
2548                        cval = 0x00;
2549                        break;
2550                case CS6:
2551                        cval = 0x01;
2552                        break;
2553                case CS7:
2554                        cval = 0x02;
2555                        break;
2556                default:
2557                case CS8:
2558                        cval = 0x03;
2559                        break;
2560        }
2561
2562        if (termios->c_cflag & CSTOPB)
2563                cval |= 0x04;
2564        if (termios->c_cflag & PARENB)
2565                cval |= UART_LCR_PARITY;
2566        if (!(termios->c_cflag & PARODD))
2567                cval |= UART_LCR_EPAR;
2568
2569#ifdef CMSPAR
2570        if (termios->c_cflag & CMSPAR)
2571                cval |= UART_LCR_SPAR;
2572#endif
2573
2574        baud = sb_uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
2575        quot = multi_get_divisor(port, baud);
2576
2577        if (mtpt->capabilities & UART_USE_FIFO) {
2578                //if (baud < 2400)
2579                //      fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2580                //else
2581                //      fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
2582
2583                //      fcr = UART_FCR_ENABLE_FIFO | 0x90;
2584                        fcr = fcr_arr[mtpt->port.line];
2585        }
2586
2587        spin_lock_irqsave(&mtpt->port.lock, flags);
2588
2589        sb_uart_update_timeout(port, termios->c_cflag, baud);
2590
2591        mtpt->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2592        if (termios->c_iflag & INPCK)
2593                mtpt->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2594        if (termios->c_iflag & (BRKINT | PARMRK))
2595                mtpt->port.read_status_mask |= UART_LSR_BI;
2596
2597        mtpt->port.ignore_status_mask = 0;
2598        if (termios->c_iflag & IGNPAR)
2599                mtpt->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2600        if (termios->c_iflag & IGNBRK) {
2601                mtpt->port.ignore_status_mask |= UART_LSR_BI;
2602                if (termios->c_iflag & IGNPAR)
2603                        mtpt->port.ignore_status_mask |= UART_LSR_OE;
2604        }
2605
2606        if ((termios->c_cflag & CREAD) == 0)
2607                mtpt->port.ignore_status_mask |= UART_LSR_DR;
2608
2609        mtpt->ier &= ~UART_IER_MSI;
2610        if (UART_ENABLE_MS(&mtpt->port, termios->c_cflag))
2611                mtpt->ier |= UART_IER_MSI;
2612
2613        serial_out(mtpt, UART_IER, mtpt->ier);
2614
2615        if (mtpt->capabilities & UART_STARTECH) {
2616                serial_outp(mtpt, UART_LCR, 0xBF);
2617                serial_outp(mtpt, UART_EFR,
2618                                termios->c_cflag & CRTSCTS ? UART_EFR_CTS :0);
2619        }
2620
2621        serial_outp(mtpt, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2622
2623        serial_outp(mtpt, UART_DLL, quot & 0xff);     /* LS of divisor */
2624        serial_outp(mtpt, UART_DLM, quot >> 8);       /* MS of divisor */
2625
2626        serial_outp(mtpt, UART_LCR, cval);        /* reset DLAB */
2627        mtpt->lcr = cval;                 /* Save LCR */
2628
2629        if (fcr & UART_FCR_ENABLE_FIFO) {
2630                /* emulated UARTs (Lucent Venus 167x) need two steps */
2631                serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
2632        }
2633
2634        serial_outp(mtpt, UART_FCR, fcr);     /* set fcr */
2635
2636
2637        if ((mtpt->port.type == PORT_16C105X)
2638                || (mtpt->port.type == PORT_16C105XA))
2639        {
2640                if(deep[mtpt->port.line]!=0)
2641                        set_deep_fifo(port, ENABLE);
2642
2643                if (mtpt->interface != RS232)
2644                        set_auto_rts(port,mtpt->interface);
2645
2646        }
2647        else
2648        {
2649                if (mtpt->interface >= RS485NE)
2650                {
2651                        uart_clear_mctrl(&mtpt->port, TIOCM_RTS);
2652                }
2653        }
2654
2655        if(mtpt->device->device_id == PCI_DEVICE_ID_MP4M)
2656        {
2657                SendATCommand(mtpt);
2658                printk("SendATCommand\n");
2659        }       
2660        multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2661        spin_unlock_irqrestore(&mtpt->port.lock, flags);
2662}
2663
2664static void multi_pm(struct sb_uart_port *port, unsigned int state, unsigned int oldstate)
2665{
2666        struct mp_port *mtpt = (struct mp_port *)port;
2667        if (state) {
2668                if (mtpt->capabilities & UART_STARTECH) {
2669                        serial_outp(mtpt, UART_LCR, 0xBF);
2670                        serial_outp(mtpt, UART_EFR, UART_EFR_ECB);
2671                        serial_outp(mtpt, UART_LCR, 0);
2672                        serial_outp(mtpt, UART_IER, UART_IERX_SLEEP);
2673                        serial_outp(mtpt, UART_LCR, 0xBF);
2674                        serial_outp(mtpt, UART_EFR, 0);
2675                        serial_outp(mtpt, UART_LCR, 0);
2676                }
2677
2678                if (mtpt->pm)
2679                        mtpt->pm(port, state, oldstate);
2680        } 
2681        else 
2682        {
2683                if (mtpt->capabilities & UART_STARTECH) {
2684                        serial_outp(mtpt, UART_LCR, 0xBF);
2685                        serial_outp(mtpt, UART_EFR, UART_EFR_ECB);
2686                        serial_outp(mtpt, UART_LCR, 0);
2687                        serial_outp(mtpt, UART_IER, 0);
2688                        serial_outp(mtpt, UART_LCR, 0xBF);
2689                        serial_outp(mtpt, UART_EFR, 0);
2690                        serial_outp(mtpt, UART_LCR, 0);
2691                }
2692
2693                if (mtpt->pm)
2694                        mtpt->pm(port, state, oldstate);
2695        }
2696}
2697
2698static void multi_release_std_resource(struct mp_port *mtpt)
2699{
2700        unsigned int size = 8 << mtpt->port.regshift;
2701
2702        switch (mtpt->port.iotype) {
2703                case UPIO_MEM:
2704                        if (!mtpt->port.mapbase)
2705                                break;
2706
2707                        if (mtpt->port.flags & UPF_IOREMAP) {
2708                                iounmap(mtpt->port.membase);
2709                                mtpt->port.membase = NULL;
2710                        }
2711
2712                        release_mem_region(mtpt->port.mapbase, size);
2713                        break;
2714
2715                case UPIO_HUB6:
2716                case UPIO_PORT:
2717                        release_region(mtpt->port.iobase,size);
2718                        break;
2719        }
2720}
2721
2722static void multi_release_port(struct sb_uart_port *port)
2723{
2724}
2725
2726static int multi_request_port(struct sb_uart_port *port)
2727{
2728        return 0;
2729}
2730
2731static void multi_config_port(struct sb_uart_port *port, int flags)
2732{
2733        struct mp_port *mtpt = (struct mp_port *)port;
2734        int probeflags = PROBE_ANY;
2735
2736        if (flags & UART_CONFIG_TYPE)
2737                autoconfig(mtpt, probeflags);
2738        if (mtpt->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2739                autoconfig_irq(mtpt);
2740
2741        if (mtpt->port.type == PORT_UNKNOWN)
2742                multi_release_std_resource(mtpt);
2743}
2744
2745static int multi_verify_port(struct sb_uart_port *port, struct serial_struct *ser)
2746{
2747        if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2748                        ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2749                        ser->type == PORT_STARTECH)
2750                return -EINVAL;
2751        return 0;
2752}
2753
2754static const char *multi_type(struct sb_uart_port *port)
2755{
2756        int type = port->type;
2757
2758        if (type >= ARRAY_SIZE(uart_config))
2759                type = 0;
2760        return uart_config[type].name;
2761}
2762
2763static struct sb_uart_ops multi_pops = {
2764        .tx_empty   = multi_tx_empty,
2765        .set_mctrl  = multi_set_mctrl,
2766        .get_mctrl  = multi_get_mctrl,
2767        .stop_tx    = multi_stop_tx,
2768        .start_tx   = multi_start_tx,
2769        .stop_rx    = multi_stop_rx,
2770        .enable_ms  = multi_enable_ms,
2771        .break_ctl  = multi_break_ctl,
2772        .startup    = multi_startup,
2773        .shutdown   = multi_shutdown,
2774        .set_termios    = multi_set_termios,
2775        .pm             = multi_pm,
2776        .type           = multi_type,
2777        .release_port   = multi_release_port,
2778        .request_port   = multi_request_port,
2779        .config_port    = multi_config_port,
2780        .verify_port    = multi_verify_port,
2781};
2782
2783static struct uart_driver multi_reg = {
2784        .owner          = THIS_MODULE,
2785        .driver_name    = "goldel_tulip",
2786        .dev_name       = "ttyMP",
2787        .major          = SB_TTY_MP_MAJOR,
2788        .minor          = 0,
2789        .nr             = MAX_MP_PORT, 
2790        .cons           = NULL,
2791};
2792
2793static void __init multi_init_ports(void)
2794{
2795        struct mp_port *mtpt;
2796        static int first = 1;
2797        int i,j,k;
2798        unsigned char osc;
2799        unsigned char b_ret = 0;
2800        static struct mp_device_t *sbdev; 
2801
2802        if (!first)
2803                return;
2804        first = 0;
2805
2806        mtpt = multi_ports; 
2807
2808        for (k=0;k<NR_BOARD;k++)
2809        {
2810                sbdev = &mp_devs[k];
2811
2812                for (i = 0; i < sbdev->nr_ports; i++, mtpt++) 
2813                {
2814                        mtpt->device            = sbdev;
2815                        mtpt->port.iobase   = sbdev->uart_access_addr + 8*i;
2816                        mtpt->port.irq      = sbdev->irq;
2817                        if ( ((sbdev->device_id == PCI_DEVICE_ID_MP4)&&(sbdev->revision==0x91)))
2818                                mtpt->interface_config_addr = sbdev->option_reg_addr + 0x08 + i;
2819                        else if (sbdev->revision == 0xc0)
2820                                mtpt->interface_config_addr = sbdev->option_reg_addr + 0x08 + (i & 0x1);
2821                        else
2822                                mtpt->interface_config_addr = sbdev->option_reg_addr + 0x08 + i/8;
2823
2824                        mtpt->option_base_addr = sbdev->option_reg_addr;
2825
2826                        mtpt->poll_type = sbdev->poll_type;
2827
2828                        mtpt->port.uartclk  = BASE_BAUD * 16;
2829
2830                        /* get input clock information */
2831                        osc = inb(sbdev->option_reg_addr + MP_OPTR_DIR0 + i/8) & 0x0F;
2832                        if (osc==0x0f)
2833                                osc = 0;
2834                        for(j=0;j<osc;j++)
2835                                mtpt->port.uartclk *= 2;
2836                        mtpt->port.flags    |= STD_COM_FLAGS | UPF_SHARE_IRQ ;
2837                        mtpt->port.iotype   = UPIO_PORT;
2838                        mtpt->port.ops      = &multi_pops;
2839
2840                        if (sbdev->revision == 0xc0)
2841                        {
2842                                /* for SB16C1053APCI */
2843                                b_ret = sb1053a_get_interface(mtpt, i);
2844                        }
2845                        else
2846                        {
2847                                b_ret = read_option_register(mtpt,(MP_OPTR_IIR0 + i/8));
2848                                printk("IIR_RET = %x\n",b_ret);
2849                        }
2850
2851                        /* default to RS232 */
2852                        mtpt->interface = RS232;
2853                        if (IIR_RS422 == (b_ret & IIR_TYPE_MASK))
2854                                mtpt->interface = RS422PTP;
2855                        if (IIR_RS485 == (b_ret & IIR_TYPE_MASK))
2856                                mtpt->interface = RS485NE;
2857                }
2858        }
2859}
2860
2861static void __init multi_register_ports(struct uart_driver *drv)
2862{
2863        int i;
2864
2865        multi_init_ports();
2866
2867        for (i = 0; i < NR_PORTS; i++) {
2868                struct mp_port *mtpt = &multi_ports[i];
2869
2870                mtpt->port.line = i;
2871                mtpt->port.ops = &multi_pops;
2872                init_timer(&mtpt->timer);
2873                mtpt->timer.function = multi_timeout;
2874                mp_add_one_port(drv, &mtpt->port);
2875        }
2876}
2877
2878/**
2879 * pci_remap_base - remap BAR value of pci device
2880 *
2881 * PARAMETERS
2882 *  pcidev  - pci_dev structure address
2883 *  offset  - BAR offset PCI_BASE_ADDRESS_0 ~ PCI_BASE_ADDRESS_4
2884 *  address - address to be changed BAR value
2885 *  size        - size of address space 
2886 *
2887 * RETURNS
2888 *  If this function performs successful, it returns 0. Otherwise, It returns -1.
2889 */
2890static int pci_remap_base(struct pci_dev *pcidev, unsigned int offset, 
2891                unsigned int address, unsigned int size) 
2892{
2893#if 0
2894        struct resource *root;
2895        unsigned index = (offset - 0x10) >> 2;
2896#endif
2897
2898        pci_write_config_dword(pcidev, offset, address);
2899#if 0
2900        root = pcidev->resource[index].parent;
2901        release_resource(&pcidev->resource[index]);
2902        address &= ~0x1;
2903        pcidev->resource[index].start = address;
2904        pcidev->resource[index].end       = address + size - 1;
2905
2906        if (request_resource(root, &pcidev->resource[index]) != NULL)
2907        {
2908                printk(KERN_ERR "pci remap conflict!! 0x%x\n", address);
2909                return (-1);
2910        }
2911#endif
2912
2913        return (0);
2914}
2915
2916static int init_mp_dev(struct pci_dev *pcidev, mppcibrd_t brd)
2917{
2918        static struct mp_device_t *sbdev = mp_devs;
2919        unsigned long addr = 0;
2920        int j;
2921        struct resource *ret = NULL;
2922
2923        sbdev->device_id = brd.device_id;
2924        pci_read_config_byte(pcidev, PCI_CLASS_REVISION, &(sbdev->revision));
2925        sbdev->name = brd.name;
2926        sbdev->uart_access_addr = pcidev->resource[0].start & PCI_BASE_ADDRESS_IO_MASK;
2927
2928        /* check revision. The SB16C1053APCI's option i/o address is BAR4 */
2929        if (sbdev->revision == 0xc0)
2930        {
2931                /* SB16C1053APCI */
2932                sbdev->option_reg_addr = pcidev->resource[4].start & PCI_BASE_ADDRESS_IO_MASK;
2933        }
2934        else
2935        {
2936                sbdev->option_reg_addr = pcidev->resource[1].start & PCI_BASE_ADDRESS_IO_MASK;
2937        }
2938#if 1   
2939        if (sbdev->revision == 0xc0)
2940        {
2941                outb(0x00, sbdev->option_reg_addr + MP_OPTR_GPOCR);
2942                inb(sbdev->option_reg_addr + MP_OPTR_GPOCR);
2943                outb(0x83, sbdev->option_reg_addr + MP_OPTR_GPOCR);
2944        }
2945#endif
2946
2947        sbdev->irq = pcidev->irq;
2948
2949        if ((brd.device_id & 0x0800) || !(brd.device_id &0xff00))
2950        {
2951                sbdev->poll_type = TYPE_INTERRUPT;
2952        }
2953        else
2954        {
2955                sbdev->poll_type = TYPE_POLL;
2956        }
2957
2958        /* codes which is specific to each board*/
2959        switch(brd.device_id){
2960                case PCI_DEVICE_ID_MP1 :
2961                case PCIE_DEVICE_ID_MP1 :
2962                case PCIE_DEVICE_ID_MP1E :
2963                case PCIE_DEVICE_ID_GT_MP1 :
2964                        sbdev->nr_ports = 1;
2965                        break;
2966                case PCI_DEVICE_ID_MP2 :
2967                case PCIE_DEVICE_ID_MP2 :
2968                case PCIE_DEVICE_ID_GT_MP2 :
2969                case PCIE_DEVICE_ID_MP2B :
2970                case PCIE_DEVICE_ID_MP2E :
2971                        sbdev->nr_ports = 2;
2972
2973                        /* serial base address remap */
2974                        if (sbdev->revision == 0xc0)
2975                        {
2976                                int prev_port_addr = 0;
2977
2978                                pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
2979                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
2980                        }
2981                        break;
2982                case PCI_DEVICE_ID_MP4 :
2983                case PCI_DEVICE_ID_MP4A :
2984                case PCIE_DEVICE_ID_MP4 :
2985                case PCI_DEVICE_ID_GT_MP4 :
2986                case PCI_DEVICE_ID_GT_MP4A :
2987                case PCIE_DEVICE_ID_GT_MP4 :
2988                case PCI_DEVICE_ID_MP4M :
2989                case PCIE_DEVICE_ID_MP4B :
2990                        sbdev->nr_ports = 4;
2991
2992                        if(sbdev->revision == 0x91){
2993                                sbdev->reserved_addr[0] = pcidev->resource[0].start & PCI_BASE_ADDRESS_IO_MASK;
2994                                outb(0x03 , sbdev->reserved_addr[0] + 0x01);
2995                                outb(0x03 , sbdev->reserved_addr[0] + 0x02);
2996                                outb(0x01 , sbdev->reserved_addr[0] + 0x20);
2997                                outb(0x00 , sbdev->reserved_addr[0] + 0x21);
2998                                request_region(sbdev->reserved_addr[0], 32, sbdev->name);
2999                                sbdev->uart_access_addr = pcidev->resource[1].start & PCI_BASE_ADDRESS_IO_MASK;
3000                                sbdev->option_reg_addr = pcidev->resource[2].start & PCI_BASE_ADDRESS_IO_MASK;
3001                        }
3002
3003                        /* SB16C1053APCI */
3004                        if (sbdev->revision == 0xc0)
3005                        {
3006                                int prev_port_addr = 0;
3007
3008                                pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
3009                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
3010                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_2, prev_port_addr + 16, 8);
3011                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_3, prev_port_addr + 24, 8);
3012                        }
3013                        break;
3014                case PCI_DEVICE_ID_MP6 :
3015                case PCI_DEVICE_ID_MP6A :
3016                case PCI_DEVICE_ID_GT_MP6 :
3017                case PCI_DEVICE_ID_GT_MP6A :
3018                        sbdev->nr_ports = 6;
3019
3020                        /* SB16C1053APCI */
3021                        if (sbdev->revision == 0xc0)
3022                        {
3023                                int prev_port_addr = 0;
3024
3025                                pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
3026                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
3027                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_2, prev_port_addr + 16, 16);
3028                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_3, prev_port_addr + 32, 16);
3029                        }
3030                        break;
3031                case PCI_DEVICE_ID_MP8 :
3032                case PCIE_DEVICE_ID_MP8 :
3033                case PCI_DEVICE_ID_GT_MP8 :
3034                case PCIE_DEVICE_ID_GT_MP8 :
3035                case PCIE_DEVICE_ID_MP8B :
3036                        sbdev->nr_ports = 8;
3037                        break;
3038                case PCI_DEVICE_ID_MP32 :
3039                case PCIE_DEVICE_ID_MP32 :
3040                case PCI_DEVICE_ID_GT_MP32 :
3041                case PCIE_DEVICE_ID_GT_MP32 :
3042                        {
3043                                int portnum_hex=0;
3044                                portnum_hex = inb(sbdev->option_reg_addr);
3045                                sbdev->nr_ports = ((portnum_hex/16)*10) + (portnum_hex % 16);
3046                        }
3047                        break;
3048#ifdef CONFIG_PARPORT_PC
3049                case PCI_DEVICE_ID_MP2S1P :
3050                        sbdev->nr_ports = 2;
3051
3052                        /* SB16C1053APCI */
3053                        if (sbdev->revision == 0xc0)
3054                        {
3055                                int prev_port_addr = 0;
3056
3057                                pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
3058                                pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
3059                        }
3060
3061                        /* add PC compatible parallel port */
3062                        parport_pc_probe_port(pcidev->resource[2].start, pcidev->resource[3].start, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &pcidev->dev, 0);
3063                        break;
3064                case PCI_DEVICE_ID_MP1P :
3065                        /* add PC compatible parallel port */
3066                        parport_pc_probe_port(pcidev->resource[2].start, pcidev->resource[3].start, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &pcidev->dev, 0);
3067                        break;
3068#endif
3069        }
3070
3071        ret = request_region(sbdev->uart_access_addr, (8*sbdev->nr_ports), sbdev->name);
3072
3073        if (sbdev->revision == 0xc0)
3074        {
3075                ret = request_region(sbdev->option_reg_addr, 0x40, sbdev->name);
3076        }
3077        else
3078        {
3079                ret = request_region(sbdev->option_reg_addr, 0x20, sbdev->name);
3080        }
3081
3082
3083        NR_BOARD++;
3084        NR_PORTS += sbdev->nr_ports;
3085
3086        /* Enable PCI interrupt */
3087        addr = sbdev->option_reg_addr + MP_OPTR_IMR0;
3088        for(j=0; j < (sbdev->nr_ports/8)+1; j++)
3089        {
3090                if (sbdev->poll_type == TYPE_INTERRUPT)
3091                {
3092                        outb(0xff,addr +j);
3093                }
3094        }
3095        sbdev++;
3096
3097        return 0;
3098}
3099
3100static int __init multi_init(void)
3101{
3102        int ret, i;
3103        struct pci_dev  *dev = NULL;
3104
3105        if(fcr_count==0)
3106        {
3107                for(i=0;i<256;i++)
3108                {
3109                        fcr_arr[i] = 0x01;
3110                        
3111                }
3112        }
3113        if(deep_count==0)
3114        {
3115                for(i=0;i<256;i++)
3116                {
3117                        deep[i] = 1;
3118                        
3119                }
3120        }
3121        if(rtr_count==0)
3122        {
3123                for(i=0;i<256;i++)
3124                {
3125                        rtr[i] = 0x10;
3126                }
3127        }
3128        if(ttr_count==0)
3129        {
3130                for(i=0;i<256;i++)
3131                {
3132                        ttr[i] = 0x38;
3133                }
3134        }
3135
3136
3137printk("MULTI INIT\n");
3138        for( i=0; i< mp_nrpcibrds; i++)
3139        {
3140
3141                while( (dev = pci_get_device(mp_pciboards[i].vendor_id, mp_pciboards[i].device_id, dev) ) )
3142
3143                {
3144printk("FOUND~~~\n");
3145//      Cent OS bug fix
3146//                      if (mp_pciboards[i].device_id & 0x0800)
3147                        {
3148                                int status;
3149                                pci_disable_device(dev);
3150                                status = pci_enable_device(dev);
3151            
3152                                if (status != 0)
3153                                { 
3154                                        printk("Multiport Board Enable Fail !\n\n");
3155                                        status = -ENXIO;
3156                                        return status;
3157                                }
3158                        }
3159
3160                        init_mp_dev(dev, mp_pciboards[i]);      
3161                }
3162        }
3163
3164        for (i = 0; i < NR_IRQS; i++)
3165                spin_lock_init(&irq_lists[i].lock);
3166
3167        ret = mp_register_driver(&multi_reg);
3168
3169        if (ret >= 0)
3170                multi_register_ports(&multi_reg);
3171
3172        return ret;
3173}
3174
3175static void __exit multi_exit(void)
3176{
3177        int i;
3178
3179        for (i = 0; i < NR_PORTS; i++)
3180                mp_remove_one_port(&multi_reg, &multi_ports[i].port);
3181
3182        mp_unregister_driver(&multi_reg);
3183}
3184
3185module_init(multi_init);
3186module_exit(multi_exit);
3187
3188MODULE_DESCRIPTION("SystemBase Multiport PCI/PCIe CORE");
3189MODULE_LICENSE("GPL");
3190