sh_eth: Register MDIO bus before registering the network device
[linux-2.6/btrfs-unstable.git] / drivers / staging / sb105x / sb_pci_mp.c
blobc9d6ee3903adc07decc03700c4af3f2d5d54896f
1 #include "sb_pci_mp.h"
2 #include <linux/module.h>
3 #include <linux/parport.h>
5 extern 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);
11 static struct mp_device_t mp_devs[MAX_MP_DEV];
12 static int mp_nrpcibrds = sizeof(mp_pciboards)/sizeof(mppcibrd_t);
13 static int NR_BOARD=0;
14 static int NR_PORTS=0;
15 static struct mp_port multi_ports[MAX_MP_PORT];
16 static struct irq_info irq_lists[NR_IRQS];
18 static _INLINE_ unsigned int serial_in(struct mp_port *mtpt, int offset);
19 static _INLINE_ void serial_out(struct mp_port *mtpt, int offset, int value);
20 static _INLINE_ unsigned int read_option_register(struct mp_port *mtpt, int offset);
21 static int sb1054_get_register(struct sb_uart_port *port, int page, int reg);
22 static int sb1054_set_register(struct sb_uart_port *port, int page, int reg, int value);
23 static void SendATCommand(struct mp_port *mtpt);
24 static int set_deep_fifo(struct sb_uart_port *port, int status);
25 static int get_deep_fifo(struct sb_uart_port *port);
26 static int get_device_type(int arg);
27 static int set_auto_rts(struct sb_uart_port *port, int status);
28 static void mp_stop(struct tty_struct *tty);
29 static void __mp_start(struct tty_struct *tty);
30 static void mp_start(struct tty_struct *tty);
31 static void mp_tasklet_action(unsigned long data);
32 static inline void mp_update_mctrl(struct sb_uart_port *port, unsigned int set, unsigned int clear);
33 static int mp_startup(struct sb_uart_state *state, int init_hw);
34 static void mp_shutdown(struct sb_uart_state *state);
35 static void mp_change_speed(struct sb_uart_state *state, struct MP_TERMIOS *old_termios);
37 static inline int __mp_put_char(struct sb_uart_port *port, struct circ_buf *circ, unsigned char c);
38 static int mp_put_char(struct tty_struct *tty, unsigned char ch);
40 static void mp_put_chars(struct tty_struct *tty);
41 static int mp_write(struct tty_struct *tty, const unsigned char *buf, int count);
42 static int mp_write_room(struct tty_struct *tty);
43 static int mp_chars_in_buffer(struct tty_struct *tty);
44 static void mp_flush_buffer(struct tty_struct *tty);
45 static void mp_send_xchar(struct tty_struct *tty, char ch);
46 static void mp_throttle(struct tty_struct *tty);
47 static void mp_unthrottle(struct tty_struct *tty);
48 static int mp_get_info(struct sb_uart_state *state, struct serial_struct *retinfo);
49 static int mp_set_info(struct sb_uart_state *state, struct serial_struct *newinfo);
50 static int mp_get_lsr_info(struct sb_uart_state *state, unsigned int *value);
52 static int mp_tiocmget(struct tty_struct *tty);
53 static int mp_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
54 static int mp_break_ctl(struct tty_struct *tty, int break_state);
55 static int mp_do_autoconfig(struct sb_uart_state *state);
56 static int mp_wait_modem_status(struct sb_uart_state *state, unsigned long arg);
57 static int mp_get_count(struct sb_uart_state *state, struct serial_icounter_struct *icnt);
58 static int mp_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
59 static void mp_set_termios(struct tty_struct *tty, struct MP_TERMIOS *old_termios);
60 static void mp_close(struct tty_struct *tty, struct file *filp);
61 static void mp_wait_until_sent(struct tty_struct *tty, int timeout);
62 static void mp_hangup(struct tty_struct *tty);
63 static void mp_update_termios(struct sb_uart_state *state);
64 static int mp_block_til_ready(struct file *filp, struct sb_uart_state *state);
65 static struct sb_uart_state *uart_get(struct uart_driver *drv, int line);
66 static int mp_open(struct tty_struct *tty, struct file *filp);
67 static const char *mp_type(struct sb_uart_port *port);
68 static void mp_change_pm(struct sb_uart_state *state, int pm_state);
69 static inline void mp_report_port(struct uart_driver *drv, struct sb_uart_port *port);
70 static void mp_configure_port(struct uart_driver *drv, struct sb_uart_state *state, struct sb_uart_port *port);
71 static void mp_unconfigure_port(struct uart_driver *drv, struct sb_uart_state *state);
72 static int mp_register_driver(struct uart_driver *drv);
73 static void mp_unregister_driver(struct uart_driver *drv);
74 static int mp_add_one_port(struct uart_driver *drv, struct sb_uart_port *port);
75 static int mp_remove_one_port(struct uart_driver *drv, struct sb_uart_port *port);
76 static void autoconfig(struct mp_port *mtpt, unsigned int probeflags);
77 static void autoconfig_irq(struct mp_port *mtpt);
78 static void multi_stop_tx(struct sb_uart_port *port);
79 static void multi_start_tx(struct sb_uart_port *port);
80 static void multi_stop_rx(struct sb_uart_port *port);
81 static void multi_enable_ms(struct sb_uart_port *port);
82 static _INLINE_ void receive_chars(struct mp_port *mtpt, int *status );
83 static _INLINE_ void transmit_chars(struct mp_port *mtpt);
84 static _INLINE_ void check_modem_status(struct mp_port *mtpt);
85 static inline void multi_handle_port(struct mp_port *mtpt);
86 static irqreturn_t multi_interrupt(int irq, void *dev_id);
87 static void serial_do_unlink(struct irq_info *i, struct mp_port *mtpt);
88 static int serial_link_irq_chain(struct mp_port *mtpt);
89 static void serial_unlink_irq_chain(struct mp_port *mtpt);
90 static void multi_timeout(unsigned long data);
91 static unsigned int multi_tx_empty(struct sb_uart_port *port);
92 static unsigned int multi_get_mctrl(struct sb_uart_port *port);
93 static void multi_set_mctrl(struct sb_uart_port *port, unsigned int mctrl);
94 static void multi_break_ctl(struct sb_uart_port *port, int break_state);
95 static int multi_startup(struct sb_uart_port *port);
96 static void multi_shutdown(struct sb_uart_port *port);
97 static unsigned int multi_get_divisor(struct sb_uart_port *port, unsigned int baud);
98 static void multi_set_termios(struct sb_uart_port *port, struct MP_TERMIOS *termios, struct MP_TERMIOS *old);
99 static void multi_pm(struct sb_uart_port *port, unsigned int state, unsigned int oldstate);
100 static void multi_release_std_resource(struct mp_port *mtpt);
101 static void multi_release_port(struct sb_uart_port *port);
102 static int multi_request_port(struct sb_uart_port *port);
103 static void multi_config_port(struct sb_uart_port *port, int flags);
104 static int multi_verify_port(struct sb_uart_port *port, struct serial_struct *ser);
105 static const char *multi_type(struct sb_uart_port *port);
106 static void __init multi_init_ports(void);
107 static void __init multi_register_ports(struct uart_driver *drv);
108 static int init_mp_dev(struct pci_dev *pcidev, mppcibrd_t brd);
110 static int deep[256];
111 static int deep_count;
112 static int fcr_arr[256];
113 static int fcr_count;
114 static int ttr[256];
115 static int ttr_count;
116 static int rtr[256];
117 static int rtr_count;
119 module_param_array(deep,int,&deep_count,0);
120 module_param_array(fcr_arr,int,&fcr_count,0);
121 module_param_array(ttr,int,&ttr_count,0);
122 module_param_array(rtr,int,&rtr_count,0);
124 static _INLINE_ unsigned int serial_in(struct mp_port *mtpt, int offset)
126 return inb(mtpt->port.iobase + offset);
129 static _INLINE_ void serial_out(struct mp_port *mtpt, int offset, int value)
131 outb(value, mtpt->port.iobase + offset);
134 static _INLINE_ unsigned int read_option_register(struct mp_port *mtpt, int offset)
136 return inb(mtpt->option_base_addr + offset);
139 static int sb1053a_get_interface(struct mp_port *mtpt, int port_num)
141 unsigned long option_base_addr = mtpt->option_base_addr;
142 unsigned int interface = 0;
144 switch (port_num)
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;
165 port_num &= 0x1;
167 /* get interface */
168 interface = inb(option_base_addr + MP_OPTR_IIR0 + port_num);
170 /* set GPO[1:0] = 11 */
171 outb(0x03, option_base_addr + MP_OPTR_GPODR);
173 return (interface);
176 static int sb1054_get_register(struct sb_uart_port *port, int page, int reg)
178 int ret = 0;
179 unsigned int lcr = 0;
180 unsigned int mcr = 0;
181 unsigned int tmp = 0;
183 if( page <= 0)
185 printk(" page 0 can not use this function\n");
186 return -1;
189 switch(page)
191 case 1:
192 lcr = SB105X_GET_LCR(port);
193 tmp = lcr | SB105X_LCR_DLAB;
194 SB105X_PUT_LCR(port, tmp);
196 tmp = SB105X_GET_LCR(port);
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);
206 ret = SB105X_GET_REG(port,reg);
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);
216 ret = SB105X_GET_REG(port,reg);
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);
226 ret = SB105X_GET_REG(port,reg);
228 SB105X_PUT_LCR(port,lcr);
229 break;
230 default:
231 printk(" error invalid page number \n");
232 return -1;
235 return ret;
238 static int sb1054_set_register(struct sb_uart_port *port, int page, int reg, int value)
240 int lcr = 0;
241 int mcr = 0;
242 int ret = 0;
244 if( page <= 0)
246 printk(" page 0 can not use this function\n");
247 return -1;
249 switch(page)
251 case 1:
252 lcr = SB105X_GET_LCR(port);
253 SB105X_PUT_LCR(port, lcr | SB105X_LCR_DLAB);
255 SB105X_PUT_REG(port,reg,value);
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);
264 SB105X_PUT_REG(port,reg,value);
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);
274 SB105X_PUT_REG(port,reg,value);
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);
284 SB105X_PUT_REG(port,reg,value);
286 SB105X_PUT_LCR(port, lcr);
287 ret = 1;
288 break;
289 default:
290 printk(" error invalid page number \n");
291 return -1;
294 return ret;
297 static int set_multidrop_mode(struct sb_uart_port *port, unsigned int mode)
299 int mdr = SB105XA_MDR_NPS;
301 if (mode & MDMODE_ENABLE)
303 mdr |= SB105XA_MDR_MDE;
306 if (1) //(mode & MDMODE_AUTO)
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);
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);
320 return 0;
323 static int get_multidrop_addr(struct sb_uart_port *port)
325 return sb1054_get_register(port, PAGE_3, SB105X_XOFF2);
328 static int set_multidrop_addr(struct sb_uart_port *port, unsigned int addr)
330 sb1054_set_register(port, PAGE_3, SB105X_XOFF2, addr);
332 return 0;
335 static void SendATCommand(struct mp_port *mtpt)
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;
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
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){
357 serial_outp(mtpt,0,ch[i++]);
361 }// end of SendATCommand()
363 static int set_deep_fifo(struct sb_uart_port *port, int status)
365 int afr_status = 0;
366 afr_status = sb1054_get_register(port, PAGE_4, SB105X_AFR);
368 if(status == ENABLE)
370 afr_status |= SB105X_AFR_AFEN;
372 else
374 afr_status &= ~SB105X_AFR_AFEN;
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);
382 return afr_status;
385 static int get_device_type(int arg)
387 int ret;
388 ret = inb(mp_devs[arg].option_reg_addr+MP_OPTR_DIR0);
389 ret = (ret & 0xf0) >> 4;
390 switch (ret)
392 case DIR_UART_16C550:
393 return PORT_16C55X;
394 case DIR_UART_16C1050:
395 return PORT_16C105X;
396 case DIR_UART_16C1050A:
398 if (mtpt->port.line < 2)
400 return PORT_16C105XA;
402 else
404 if (mtpt->device->device_id & 0x50)
406 return PORT_16C55X;
408 else
410 return PORT_16C105X;
413 return PORT_16C105XA;
414 default:
415 return PORT_UNKNOWN;
419 static int get_deep_fifo(struct sb_uart_port *port)
421 int afr_status = 0;
422 afr_status = sb1054_get_register(port, PAGE_4, SB105X_AFR);
423 return afr_status;
426 static int set_auto_rts(struct sb_uart_port *port, int status)
428 int atr_status = 0;
430 #if 0
431 int efr_status = 0;
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
442 //ATR
443 atr_status = sb1054_get_register(port, PAGE_3, SB105X_ATR);
444 switch(status)
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;
460 sb1054_set_register(port,PAGE_3,SB105X_ATR,atr_status);
461 atr_status = sb1054_get_register(port, PAGE_3, SB105X_ATR);
463 return atr_status;
466 static void mp_stop(struct tty_struct *tty)
468 struct sb_uart_state *state = tty->driver_data;
469 struct sb_uart_port *port = state->port;
470 unsigned long flags;
472 spin_lock_irqsave(&port->lock, flags);
473 port->ops->stop_tx(port);
474 spin_unlock_irqrestore(&port->lock, flags);
477 static void __mp_start(struct tty_struct *tty)
479 struct sb_uart_state *state = tty->driver_data;
480 struct sb_uart_port *port = state->port;
482 if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&
483 !tty->stopped && !tty->hw_stopped)
484 port->ops->start_tx(port);
487 static void mp_start(struct tty_struct *tty)
489 __mp_start(tty);
492 static void mp_tasklet_action(unsigned long data)
494 struct sb_uart_state *state = (struct sb_uart_state *)data;
495 struct tty_struct *tty;
497 printk("tasklet is called!\n");
498 tty = state->info->tty;
499 tty_wakeup(tty);
502 static inline void mp_update_mctrl(struct sb_uart_port *port, unsigned int set, unsigned int clear)
504 unsigned int old;
506 old = port->mctrl;
507 port->mctrl = (old & ~clear) | set;
508 if (old != port->mctrl)
509 port->ops->set_mctrl(port, port->mctrl);
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)
515 static int mp_startup(struct sb_uart_state *state, int init_hw)
517 struct sb_uart_info *info = state->info;
518 struct sb_uart_port *port = state->port;
519 unsigned long page;
520 int retval = 0;
522 if (info->flags & UIF_INITIALIZED)
523 return 0;
525 if (info->tty)
526 set_bit(TTY_IO_ERROR, &info->tty->flags);
528 if (port->type == PORT_UNKNOWN)
529 return 0;
531 if (!info->xmit.buf) {
532 page = get_zeroed_page(GFP_KERNEL);
533 if (!page)
534 return -ENOMEM;
536 info->xmit.buf = (unsigned char *) page;
538 uart_circ_clear(&info->xmit);
541 retval = port->ops->startup(port);
542 if (retval == 0) {
543 if (init_hw) {
544 mp_change_speed(state, NULL);
546 if (info->tty && (info->tty->termios.c_cflag & CBAUD))
547 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
550 info->flags |= UIF_INITIALIZED;
552 if (info->tty)
553 clear_bit(TTY_IO_ERROR, &info->tty->flags);
556 if (retval && capable(CAP_SYS_ADMIN))
557 retval = 0;
559 return retval;
562 static void mp_shutdown(struct sb_uart_state *state)
564 struct sb_uart_info *info = state->info;
565 struct sb_uart_port *port = state->port;
567 if (info->tty)
568 set_bit(TTY_IO_ERROR, &info->tty->flags);
570 if (info->flags & UIF_INITIALIZED) {
571 info->flags &= ~UIF_INITIALIZED;
573 if (!info->tty || (info->tty->termios.c_cflag & HUPCL))
574 uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
576 wake_up_interruptible(&info->delta_msr_wait);
578 port->ops->shutdown(port);
580 synchronize_irq(port->irq);
582 tasklet_kill(&info->tlet);
584 if (info->xmit.buf) {
585 free_page((unsigned long)info->xmit.buf);
586 info->xmit.buf = NULL;
590 static void mp_change_speed(struct sb_uart_state *state, struct MP_TERMIOS *old_termios)
592 struct tty_struct *tty = state->info->tty;
593 struct sb_uart_port *port = state->port;
595 if (!tty || port->type == PORT_UNKNOWN)
596 return;
598 if (tty->termios.c_cflag & CRTSCTS)
599 state->info->flags |= UIF_CTS_FLOW;
600 else
601 state->info->flags &= ~UIF_CTS_FLOW;
603 if (tty->termios.c_cflag & CLOCAL)
604 state->info->flags &= ~UIF_CHECK_CD;
605 else
606 state->info->flags |= UIF_CHECK_CD;
608 port->ops->set_termios(port, &tty->termios, old_termios);
611 static inline int __mp_put_char(struct sb_uart_port *port, struct circ_buf *circ, unsigned char c)
613 unsigned long flags;
614 int ret = 0;
616 if (!circ->buf)
617 return 0;
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;
625 spin_unlock_irqrestore(&port->lock, flags);
626 return ret;
629 static int mp_put_char(struct tty_struct *tty, unsigned char ch)
631 struct sb_uart_state *state = tty->driver_data;
633 return __mp_put_char(state->port, &state->info->xmit, ch);
636 static void mp_put_chars(struct tty_struct *tty)
638 mp_start(tty);
641 static int mp_write(struct tty_struct *tty, const unsigned char *buf, int count)
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;
648 if (!state || !state->info) {
649 return -EL3HLT;
652 port = state->port;
653 circ = &state->info->xmit;
655 if (!circ->buf)
656 return 0;
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);
666 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
667 buf += c;
668 count -= c;
669 ret += c;
671 mp_start(tty);
672 return ret;
675 static int mp_write_room(struct tty_struct *tty)
677 struct sb_uart_state *state = tty->driver_data;
679 return uart_circ_chars_free(&state->info->xmit);
682 static int mp_chars_in_buffer(struct tty_struct *tty)
684 struct sb_uart_state *state = tty->driver_data;
686 return uart_circ_chars_pending(&state->info->xmit);
689 static void mp_flush_buffer(struct tty_struct *tty)
691 struct sb_uart_state *state = tty->driver_data;
692 struct sb_uart_port *port;
693 unsigned long flags;
695 if (!state || !state->info) {
696 return;
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);
707 static void mp_send_xchar(struct tty_struct *tty, char ch)
709 struct sb_uart_state *state = tty->driver_data;
710 struct sb_uart_port *port = state->port;
711 unsigned long flags;
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);
725 static void mp_throttle(struct tty_struct *tty)
727 struct sb_uart_state *state = tty->driver_data;
729 if (I_IXOFF(tty))
730 mp_send_xchar(tty, STOP_CHAR(tty));
732 if (tty->termios.c_cflag & CRTSCTS)
733 uart_clear_mctrl(state->port, TIOCM_RTS);
736 static void mp_unthrottle(struct tty_struct *tty)
738 struct sb_uart_state *state = tty->driver_data;
739 struct sb_uart_port *port = state->port;
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));
748 if (tty->termios.c_cflag & CRTSCTS)
749 uart_set_mctrl(port, TIOCM_RTS);
752 static int mp_get_info(struct sb_uart_state *state, struct serial_struct *retinfo)
754 struct sb_uart_port *port = state->port;
755 struct serial_struct tmp;
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;
777 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
778 return -EFAULT;
779 return 0;
782 static int mp_set_info(struct sb_uart_state *state, struct serial_struct *newinfo)
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;
792 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
793 return -EFAULT;
795 new_port = new_serial.port;
796 if (HIGH_BITS_OFFSET)
797 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
799 new_serial.irq = irq_canonicalize(new_serial.irq);
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);
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;
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;
831 if (port->ops->verify_port)
832 retval = port->ops->verify_port(port, &new_serial);
834 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
835 (new_serial.baud_base < 9600))
836 retval = -EINVAL;
838 if (retval)
839 goto exit;
841 if (change_port || change_irq) {
842 retval = -EBUSY;
844 if (uart_users(state) > 1)
845 goto exit;
847 mp_shutdown(state);
850 if (change_port) {
851 unsigned long old_iobase, old_mapbase;
852 unsigned int old_type, old_iotype, old_hub6, old_shift;
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;
861 if (old_type != PORT_UNKNOWN)
862 port->ops->release_port(port);
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;
871 if (port->type != PORT_UNKNOWN) {
872 retval = port->ops->request_port(port);
873 } else {
874 retval = 0;
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;
888 retval = -EBUSY;
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;
904 check_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);
917 mp_change_speed(state, NULL);
919 } else
920 retval = mp_startup(state, 1);
921 exit:
922 MP_STATE_UNLOCK(state);
923 return retval;
927 static int mp_get_lsr_info(struct sb_uart_state *state, unsigned int *value)
929 struct sb_uart_port *port = state->port;
930 unsigned int result;
932 result = port->ops->tx_empty(port);
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;
939 return put_user(result, value);
942 static int mp_tiocmget(struct tty_struct *tty)
944 struct sb_uart_state *state = tty->driver_data;
945 struct sb_uart_port *port = state->port;
946 int result = -EIO;
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);
955 MP_STATE_UNLOCK(state);
956 return result;
959 static int mp_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
961 struct sb_uart_state *state = tty->driver_data;
962 struct sb_uart_port *port = state->port;
963 int ret = -EIO;
966 MP_STATE_LOCK(state);
967 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
968 mp_update_mctrl(port, set, clear);
969 ret = 0;
971 MP_STATE_UNLOCK(state);
973 return ret;
976 static int mp_break_ctl(struct tty_struct *tty, int break_state)
978 struct sb_uart_state *state = tty->driver_data;
979 struct sb_uart_port *port = state->port;
981 MP_STATE_LOCK(state);
983 if (port->type != PORT_UNKNOWN)
984 port->ops->break_ctl(port, break_state);
986 MP_STATE_UNLOCK(state);
987 return 0;
990 static int mp_do_autoconfig(struct sb_uart_state *state)
992 struct sb_uart_port *port = state->port;
993 int flags, ret;
995 if (!capable(CAP_SYS_ADMIN))
996 return -EPERM;
998 if (mutex_lock_interruptible(&state->mutex))
999 return -ERESTARTSYS;
1000 ret = -EBUSY;
1001 if (uart_users(state) == 1) {
1002 mp_shutdown(state);
1004 if (port->type != PORT_UNKNOWN)
1005 port->ops->release_port(port);
1007 flags = UART_CONFIG_TYPE;
1008 if (port->flags & UPF_AUTO_IRQ)
1009 flags |= UART_CONFIG_IRQ;
1011 port->ops->config_port(port, flags);
1013 ret = mp_startup(state, 1);
1015 MP_STATE_UNLOCK(state);
1016 return ret;
1019 static int mp_wait_modem_status(struct sb_uart_state *state, unsigned long arg)
1021 struct sb_uart_port *port = state->port;
1022 DECLARE_WAITQUEUE(wait, current);
1023 struct sb_uart_icount cprev, cnow;
1024 int ret;
1026 spin_lock_irq(&port->lock);
1027 memcpy(&cprev, &port->icount, sizeof(struct sb_uart_icount));
1029 port->ops->enable_ms(port);
1030 spin_unlock_irq(&port->lock);
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);
1038 set_current_state(TASK_INTERRUPTIBLE);
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;
1048 schedule();
1050 if (signal_pending(current)) {
1051 ret = -ERESTARTSYS;
1052 break;
1055 cprev = cnow;
1058 current->state = TASK_RUNNING;
1059 remove_wait_queue(&state->info->delta_msr_wait, &wait);
1061 return ret;
1064 static int mp_get_count(struct sb_uart_state *state, struct serial_icounter_struct *icnt)
1066 struct serial_icounter_struct icount = {};
1067 struct sb_uart_icount cnow;
1068 struct sb_uart_port *port = state->port;
1070 spin_lock_irq(&port->lock);
1071 memcpy(&cnow, &port->icount, sizeof(struct sb_uart_icount));
1072 spin_unlock_irq(&port->lock);
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;
1086 return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
1089 static int mp_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1091 struct sb_uart_state *state = tty->driver_data;
1092 struct mp_port *info = (struct mp_port *)state->port;
1093 int ret = -ENOIOCTLCMD;
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)
1101 //arg &= ~0x6;
1102 //state->port->mdmode = 0;
1103 return set_multidrop_mode((struct sb_uart_port *)info, (unsigned int)arg);
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;
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;
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);
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);
1135 return ret;
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);
1141 else{
1142 serial_out(info,2,arg);
1145 return ret;
1146 case TIOCSMDADDR:
1147 /* set multi-drop address */
1148 if (info->port.type == PORT_16C105XA)
1150 state->port->mdmode |= MDMODE_ADDR;
1151 return set_multidrop_addr((struct sb_uart_port *)info, (unsigned int)arg);
1153 ret = -ENOTSUPP;
1154 break;
1156 case TIOCGMDADDR:
1157 /* set multi-drop address */
1158 if ((info->port.type == PORT_16C105XA) && (state->port->mdmode & MDMODE_ADDR))
1160 return get_multidrop_addr((struct sb_uart_port *)info);
1162 ret = -ENOTSUPP;
1163 break;
1165 case TIOCSENDADDR:
1166 /* send address in multi-drop mode */
1167 if ((info->port.type == PORT_16C105XA)
1168 && (state->port->mdmode & (MDMODE_ENABLE)))
1170 if (mp_chars_in_buffer(tty) > 0)
1172 tty_wait_until_sent(tty, 0);
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);
1179 break;
1181 case TIOCGSERIAL:
1182 ret = mp_get_info(state, (struct serial_struct *)arg);
1183 break;
1185 case TIOCSSERIAL:
1186 ret = mp_set_info(state, (struct serial_struct *)arg);
1187 break;
1189 case TIOCSERCONFIG:
1190 ret = mp_do_autoconfig(state);
1191 break;
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)
1211 /* for SB16C1053APCI */
1212 return (sb1053a_get_interface(info, info->port.line));
1214 else
1216 return (inb(mp_devs[arg].option_reg_addr+MP_OPTR_IIR0+(state->port->line/8)));
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;
1231 if (ret != -ENOIOCTLCMD)
1232 goto out;
1234 if (tty->flags & (1 << TTY_IO_ERROR)) {
1235 ret = -EIO;
1236 goto out;
1239 switch (cmd) {
1240 case TIOCMIWAIT:
1241 ret = mp_wait_modem_status(state, arg);
1242 break;
1244 case TIOCGICOUNT:
1245 ret = mp_get_count(state, (struct serial_icounter_struct *)arg);
1246 break;
1249 if (ret != -ENOIOCTLCMD)
1250 goto out;
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;
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;
1266 MP_STATE_UNLOCK(state);
1267 out:
1268 return ret;
1271 static void mp_set_termios(struct tty_struct *tty, struct MP_TERMIOS *old_termios)
1273 struct sb_uart_state *state = tty->driver_data;
1274 unsigned long flags;
1275 unsigned int cflag = tty->termios.c_cflag;
1277 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1279 if ((cflag ^ old_termios->c_cflag) == 0 &&
1280 RELEVANT_IFLAG(tty->termios.c_iflag ^ old_termios->c_iflag) == 0)
1281 return;
1283 mp_change_speed(state, old_termios);
1285 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1286 uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
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);
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);
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);
1309 spin_unlock_irqrestore(&state->port->lock, flags);
1313 static void mp_close(struct tty_struct *tty, struct file *filp)
1315 struct sb_uart_state *state = tty->driver_data;
1316 struct sb_uart_port *port;
1318 printk("mp_close!\n");
1319 if (!state || !state->port)
1320 return;
1322 port = state->port;
1324 printk("close1 %d\n", __LINE__);
1325 MP_STATE_LOCK(state);
1327 printk("close2 %d\n", __LINE__);
1328 if (tty_hung_up_p(filp))
1329 goto done;
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;
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;
1343 if (state->count)
1344 goto done;
1346 tty->closing = 1;
1348 printk("close5 %d\n", __LINE__);
1349 if (state->closing_wait != USF_CLOSING_WAIT_NONE)
1350 tty_wait_until_sent(tty, state->closing_wait);
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);
1360 printk("close7 %d\n", __LINE__);
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)
1370 if (state->close_delay)
1372 set_current_state(TASK_INTERRUPTIBLE);
1373 schedule_timeout(state->close_delay);
1376 else
1378 mp_change_pm(state, 3);
1380 printk("close8 %d\n", __LINE__);
1382 state->info->flags &= ~UIF_NORMAL_ACTIVE;
1383 wake_up_interruptible(&state->info->open_wait);
1385 done:
1386 printk("close done\n");
1387 MP_STATE_UNLOCK(state);
1388 module_put(THIS_MODULE);
1391 static void mp_wait_until_sent(struct tty_struct *tty, int timeout)
1393 struct sb_uart_state *state = tty->driver_data;
1394 struct sb_uart_port *port = state->port;
1395 unsigned long char_time, expire;
1397 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1398 return;
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;
1407 if (timeout == 0 || timeout > 2 * port->timeout)
1408 timeout = 2 * port->timeout;
1410 expire = jiffies + timeout;
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;
1420 set_current_state(TASK_RUNNING); /* might not be needed */
1423 static void mp_hangup(struct tty_struct *tty)
1425 struct sb_uart_state *state = tty->driver_data;
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);
1437 MP_STATE_UNLOCK(state);
1440 static void mp_update_termios(struct sb_uart_state *state)
1442 struct tty_struct *tty = state->info->tty;
1443 struct sb_uart_port *port = state->port;
1445 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
1446 mp_change_speed(state, NULL);
1448 if (tty->termios.c_cflag & CBAUD)
1449 uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1453 static int mp_block_til_ready(struct file *filp, struct sb_uart_state *state)
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;
1460 info->blocked_open++;
1461 state->count--;
1463 add_wait_queue(&info->open_wait, &wait);
1464 while (1) {
1465 set_current_state(TASK_INTERRUPTIBLE);
1467 if (tty_hung_up_p(filp) || info->tty == NULL)
1468 break;
1470 if (!(info->flags & UIF_INITIALIZED))
1471 break;
1473 if ((filp->f_flags & O_NONBLOCK) ||
1474 (info->tty->termios.c_cflag & CLOCAL) ||
1475 (info->tty->flags & (1 << TTY_IO_ERROR))) {
1476 break;
1479 if (info->tty->termios.c_cflag & CBAUD)
1480 uart_set_mctrl(port, TIOCM_DTR);
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;
1489 MP_STATE_UNLOCK(state);
1490 schedule();
1491 MP_STATE_LOCK(state);
1493 if (signal_pending(current))
1494 break;
1496 set_current_state(TASK_RUNNING);
1497 remove_wait_queue(&info->open_wait, &wait);
1499 state->count++;
1500 info->blocked_open--;
1502 if (signal_pending(current))
1503 return -ERESTARTSYS;
1505 if (!info->tty || tty_hung_up_p(filp))
1506 return -EAGAIN;
1508 return 0;
1511 static struct sb_uart_state *uart_get(struct uart_driver *drv, int line)
1513 struct sb_uart_state *state;
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;
1521 state->count++;
1522 if (!state->port) {
1523 state->count--;
1524 MP_STATE_UNLOCK(state);
1525 state = ERR_PTR(-ENXIO);
1526 goto out;
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);
1536 state->port->info = state->info;
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);
1547 out:
1548 MP_MUTEX_UNLOCK(mp_mutex);
1549 return state;
1552 static int mp_open(struct tty_struct *tty, struct file *filp)
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;
1560 retval = -ENODEV;
1561 if (line >= tty->driver->num)
1562 goto fail;
1564 state = uart_get(drv, line);
1566 if (IS_ERR(state)) {
1567 retval = PTR_ERR(state);
1568 goto fail;
1571 mtpt = (struct mp_port *)state->port;
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;
1578 if (tty_hung_up_p(filp)) {
1579 retval = -EAGAIN;
1580 state->count--;
1581 MP_STATE_UNLOCK(state);
1582 goto fail;
1585 if (state->count == 1)
1586 mp_change_pm(state, 0);
1588 retval = mp_startup(state, 0);
1590 if (retval == 0)
1591 retval = mp_block_til_ready(filp, state);
1592 MP_STATE_UNLOCK(state);
1594 if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
1595 state->info->flags |= UIF_NORMAL_ACTIVE;
1597 mp_update_termios(state);
1600 uart_clear_mctrl(state->port, TIOCM_RTS);
1601 try_module_get(THIS_MODULE);
1602 fail:
1603 return retval;
1607 static const char *mp_type(struct sb_uart_port *port)
1609 const char *str = NULL;
1611 if (port->ops->type)
1612 str = port->ops->type(port);
1614 if (!str)
1615 str = "unknown";
1617 return str;
1620 static void mp_change_pm(struct sb_uart_state *state, int pm_state)
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;
1628 static inline void mp_report_port(struct uart_driver *drv, struct sb_uart_port *port)
1630 char address[64];
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;
1648 printk( "%s%d at %s (irq = %d) is a %s\n",
1649 drv->dev_name, port->line, address, port->irq, mp_type(port));
1653 static void mp_configure_port(struct uart_driver *drv, struct sb_uart_state *state, struct sb_uart_port *port)
1655 unsigned int flags;
1658 if (!port->iobase && !port->mapbase && !port->membase)
1660 DPRINTK("%s error \n",__FUNCTION__);
1661 return;
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);
1671 if (port->type != PORT_UNKNOWN) {
1672 unsigned long flags;
1674 mp_report_port(drv, port);
1676 spin_lock_irqsave(&port->lock, flags);
1677 port->ops->set_mctrl(port, 0);
1678 spin_unlock_irqrestore(&port->lock, flags);
1680 mp_change_pm(state, 3);
1684 static void mp_unconfigure_port(struct uart_driver *drv, struct sb_uart_state *state)
1686 struct sb_uart_port *port = state->port;
1687 struct sb_uart_info *info = state->info;
1689 if (info && info->tty)
1690 tty_hangup(info->tty);
1692 MP_STATE_LOCK(state);
1694 state->info = NULL;
1696 if (port->type != PORT_UNKNOWN)
1697 port->ops->release_port(port);
1699 port->type = PORT_UNKNOWN;
1701 if (info) {
1702 tasklet_kill(&info->tlet);
1703 kfree(info);
1706 MP_STATE_UNLOCK(state);
1708 static 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,
1734 static int mp_register_driver(struct uart_driver *drv)
1736 struct tty_driver *normal = NULL;
1737 int i, retval;
1739 drv->state = kmalloc(sizeof(struct sb_uart_state) * drv->nr, GFP_KERNEL);
1740 retval = -ENOMEM;
1741 if (!drv->state)
1743 printk("SB PCI Error: Kernel memory allocation error!\n");
1744 goto out;
1746 memset(drv->state, 0, sizeof(struct sb_uart_state) * drv->nr);
1748 normal = alloc_tty_driver(drv->nr);
1749 if (!normal)
1751 printk("SB PCI Error: tty allocation error!\n");
1752 goto out;
1755 drv->tty_driver = normal;
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;
1764 normal->num = MAX_MP_PORT ;
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;
1773 tty_set_operations(normal, &mp_ops);
1775 for (i = 0; i < drv->nr; i++) {
1776 struct sb_uart_state *state = drv->state + i;
1778 state->close_delay = 500;
1779 state->closing_wait = 30000;
1781 mutex_init(&state->mutex);
1784 retval = tty_register_driver(normal);
1785 out:
1786 if (retval < 0) {
1787 printk("Register tty driver Fail!\n");
1788 put_tty_driver(normal);
1789 kfree(drv->state);
1792 return retval;
1795 void mp_unregister_driver(struct uart_driver *drv)
1797 struct tty_driver *normal = NULL;
1799 normal = drv->tty_driver;
1801 if (!normal)
1803 return;
1806 tty_unregister_driver(normal);
1807 put_tty_driver(normal);
1808 drv->tty_driver = NULL;
1811 kfree(drv->state);
1815 static int mp_add_one_port(struct uart_driver *drv, struct sb_uart_port *port)
1817 struct sb_uart_state *state;
1818 int ret = 0;
1821 if (port->line >= drv->nr)
1822 return -EINVAL;
1824 state = drv->state + port->line;
1826 MP_MUTEX_LOCK(mp_mutex);
1827 if (state->port) {
1828 ret = -EINVAL;
1829 goto out;
1832 state->port = port;
1834 spin_lock_init(&port->lock);
1835 port->cons = drv->cons;
1836 port->info = state->info;
1838 mp_configure_port(drv, state, port);
1840 tty_register_device(drv->tty_driver, port->line, port->dev);
1842 out:
1843 MP_MUTEX_UNLOCK(mp_mutex);
1846 return ret;
1849 static int mp_remove_one_port(struct uart_driver *drv, struct sb_uart_port *port)
1851 struct sb_uart_state *state = drv->state + port->line;
1853 if (state->port != port)
1854 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
1855 state->port, port);
1857 MP_MUTEX_LOCK(mp_mutex);
1859 tty_unregister_device(drv->tty_driver, port->line);
1861 mp_unconfigure_port(drv, state);
1862 state->port = NULL;
1863 MP_MUTEX_UNLOCK(mp_mutex);
1865 return 0;
1868 static void autoconfig(struct mp_port *mtpt, unsigned int probeflags)
1870 unsigned char status1, scratch, scratch2, scratch3;
1871 unsigned char save_lcr, save_mcr;
1872 unsigned long flags;
1874 unsigned char u_type;
1875 unsigned char b_ret = 0;
1877 if (!mtpt->port.iobase && !mtpt->port.mapbase && !mtpt->port.membase)
1878 return;
1880 DEBUG_AUTOCONF("ttyMP%d: autoconf (0x%04x, 0x%p): ",
1881 mtpt->port.line, mtpt->port.iobase, mtpt->port.membase);
1883 spin_lock_irqsave(&mtpt->port.lock, flags);
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;
1905 save_mcr = serial_in(mtpt, UART_MCR);
1906 save_lcr = serial_in(mtpt, UART_LCR);
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;
1919 serial_outp(mtpt, UART_LCR, 0xBF);
1920 serial_outp(mtpt, UART_EFR, 0);
1921 serial_outp(mtpt, UART_LCR, 0);
1923 serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
1924 scratch = serial_in(mtpt, UART_IIR) >> 6;
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 )
1934 switch (u_type)
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)
1945 mtpt->port.type = PORT_16C105XA;
1947 else
1949 if (mtpt->device->device_id & 0x50)
1951 mtpt->port.type = PORT_16C55X;
1953 else
1955 mtpt->port.type = PORT_16C105X;
1958 break;
1959 default:
1960 mtpt->port.type = PORT_UNKNOWN;
1961 break;
1965 if(mtpt->port.type == PORT_UNKNOWN )
1967 printk("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;
1980 serial_outp(mtpt, UART_LCR, save_lcr);
1982 mtpt->port.fifosize = uart_config[mtpt->port.type].dfl_xmit_fifo_size;
1983 mtpt->capabilities = uart_config[mtpt->port.type].flags;
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);
1995 out:
1996 spin_unlock_irqrestore(&mtpt->port.lock, flags);
1997 DEBUG_AUTOCONF("type=%s\n", uart_config[mtpt->port.type].name);
2000 static void autoconfig_irq(struct mp_port *mtpt)
2002 unsigned char save_mcr, save_ier;
2003 unsigned long irqs;
2004 int irq;
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);
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);
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);
2025 serial_outp(mtpt, UART_MCR, save_mcr);
2026 serial_outp(mtpt, UART_IER, save_ier);
2028 mtpt->port.irq = (irq > 0) ? irq : 0;
2031 static void multi_stop_tx(struct sb_uart_port *port)
2033 struct mp_port *mtpt = (struct mp_port *)port;
2035 if (mtpt->ier & UART_IER_THRI) {
2036 mtpt->ier &= ~UART_IER_THRI;
2037 serial_out(mtpt, UART_IER, mtpt->ier);
2040 tasklet_schedule(&port->info->tlet);
2043 static void multi_start_tx(struct sb_uart_port *port)
2045 struct mp_port *mtpt = (struct mp_port *)port;
2047 if (!(mtpt->ier & UART_IER_THRI)) {
2048 mtpt->ier |= UART_IER_THRI;
2049 serial_out(mtpt, UART_IER, mtpt->ier);
2053 static void multi_stop_rx(struct sb_uart_port *port)
2055 struct mp_port *mtpt = (struct mp_port *)port;
2057 mtpt->ier &= ~UART_IER_RLSI;
2058 mtpt->port.read_status_mask &= ~UART_LSR_DR;
2059 serial_out(mtpt, UART_IER, mtpt->ier);
2062 static void multi_enable_ms(struct sb_uart_port *port)
2064 struct mp_port *mtpt = (struct mp_port *)port;
2066 mtpt->ier |= UART_IER_MSI;
2067 serial_out(mtpt, UART_IER, mtpt->ier);
2071 static _INLINE_ void receive_chars(struct mp_port *mtpt, int *status )
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;
2079 //lsr &= mtpt->port.read_status_mask;
2081 do {
2082 if ((lsr & UART_LSR_PE) && (mtpt->port.mdmode & MDMODE_ENABLE))
2084 ch = serial_inp(mtpt, UART_RX);
2086 else if (lsr & UART_LSR_SPECIAL)
2088 flag = 0;
2089 ch = serial_inp(mtpt, UART_RX);
2091 if (lsr & UART_LSR_BI)
2094 mtpt->port.icount.brk++;
2095 flag = TTY_BREAK;
2097 if (sb_uart_handle_break(&mtpt->port))
2098 goto ignore_char;
2100 if (lsr & UART_LSR_PE)
2102 mtpt->port.icount.parity++;
2103 flag = TTY_PARITY;
2105 if (lsr & UART_LSR_FE)
2107 mtpt->port.icount.frame++;
2108 flag = TTY_FRAME;
2110 if (lsr & UART_LSR_OE)
2112 mtpt->port.icount.overrun++;
2113 flag = TTY_OVERRUN;
2115 tty_insert_flip_char(tty, ch, flag);
2117 else
2119 ch = serial_inp(mtpt, UART_RX);
2120 tty_insert_flip_char(tty, ch, 0);
2122 ignore_char:
2123 lsr = serial_inp(mtpt, UART_LSR);
2124 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
2126 tty_flip_buffer_push(tty);
2132 static _INLINE_ void transmit_chars(struct mp_port *mtpt)
2134 struct circ_buf *xmit = &mtpt->port.info->xmit;
2135 int count;
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;
2143 if (uart_circ_empty(xmit) || uart_tx_stopped(&mtpt->port)) {
2144 multi_stop_tx(&mtpt->port);
2145 return;
2148 count = uart_circ_chars_pending(xmit);
2150 if(count > mtpt->port.fifosize)
2152 count = mtpt->port.fifosize;
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))
2161 printk("send address\n");
2162 /* send multi-drop address */
2163 serial_out(mtpt, UART_SCR, xmit->buf[xmit->tail]);
2165 else
2166 #endif
2168 serial_out(mtpt, UART_TX, xmit->buf[xmit->tail]);
2170 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
2171 mtpt->port.icount.tx++;
2172 } while (--count > 0);
2177 static _INLINE_ void check_modem_status(struct mp_port *mtpt)
2179 int status;
2181 status = serial_in(mtpt, UART_MSR);
2183 if ((status & UART_MSR_ANY_DELTA) == 0)
2184 return;
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);
2195 wake_up_interruptible(&mtpt->port.info->delta_msr_wait);
2198 static inline void multi_handle_port(struct mp_port *mtpt)
2200 unsigned int status = serial_inp(mtpt, UART_LSR);
2202 //printk("lsr: %x\n", status);
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)
2209 if ((mtpt->port.type == PORT_16C105X)
2210 || (mtpt->port.type == PORT_16C105XA))
2211 transmit_chars(mtpt);
2212 else
2214 if (mtpt->interface >= RS485NE)
2215 uart_set_mctrl(&mtpt->port, TIOCM_RTS);
2217 transmit_chars(mtpt);
2220 if (mtpt->interface >= RS485NE)
2222 while((status=serial_in(mtpt,UART_LSR) &0x60)!=0x60);
2223 uart_clear_mctrl(&mtpt->port, TIOCM_RTS);
2231 static irqreturn_t multi_interrupt(int irq, void *dev_id)
2233 struct irq_info *iinfo = dev_id;
2234 struct list_head *lhead, *end = NULL;
2235 int pass_counter = 0;
2238 spin_lock(&iinfo->lock);
2240 lhead = iinfo->head;
2241 do {
2242 struct mp_port *mtpt;
2243 unsigned int iir;
2245 mtpt = list_entry(lhead, struct mp_port, list);
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))
2251 printk("interrupt handle\n");
2252 spin_lock(&mtpt->port.lock);
2253 multi_handle_port(mtpt);
2254 spin_unlock(&mtpt->port.lock);
2256 end = NULL;
2257 } else if (end == NULL)
2258 end = lhead;
2260 lhead = lhead->next;
2261 if (lhead == iinfo->head && pass_counter++ > PASS_LIMIT)
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;
2269 } while (lhead != end);
2271 spin_unlock(&iinfo->lock);
2274 return IRQ_HANDLED;
2277 static void serial_do_unlink(struct irq_info *i, struct mp_port *mtpt)
2279 spin_lock_irq(&i->lock);
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;
2289 spin_unlock_irq(&i->lock);
2292 static int serial_link_irq_chain(struct mp_port *mtpt)
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);
2298 if (i->head) {
2299 list_add(&mtpt->list, i->head);
2300 spin_unlock_irq(&i->lock);
2302 ret = 0;
2303 } else {
2304 INIT_LIST_HEAD(&mtpt->list);
2305 i->head = &mtpt->list;
2306 spin_unlock_irq(&i->lock);
2308 ret = request_irq(mtpt->port.irq, multi_interrupt,
2309 irq_flags, "serial", i);
2310 if (ret < 0)
2311 serial_do_unlink(i, mtpt);
2314 return ret;
2320 static void serial_unlink_irq_chain(struct mp_port *mtpt)
2322 struct irq_info *i = irq_lists + mtpt->port.irq;
2324 if (list_empty(i->head))
2326 free_irq(mtpt->port.irq, i);
2328 serial_do_unlink(i, mtpt);
2331 static void multi_timeout(unsigned long data)
2333 struct mp_port *mtpt = (struct mp_port *)data;
2336 spin_lock(&mtpt->port.lock);
2337 multi_handle_port(mtpt);
2338 spin_unlock(&mtpt->port.lock);
2340 mod_timer(&mtpt->timer, jiffies+1 );
2343 static unsigned int multi_tx_empty(struct sb_uart_port *port)
2345 struct mp_port *mtpt = (struct mp_port *)port;
2346 unsigned long flags;
2347 unsigned int ret;
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);
2353 return ret;
2357 static unsigned int multi_get_mctrl(struct sb_uart_port *port)
2359 struct mp_port *mtpt = (struct mp_port *)port;
2360 unsigned char status;
2361 unsigned int ret;
2363 status = serial_in(mtpt, UART_MSR);
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;
2377 static void multi_set_mctrl(struct sb_uart_port *port, unsigned int mctrl)
2379 struct mp_port *mtpt = (struct mp_port *)port;
2380 unsigned char mcr = 0;
2382 mctrl &= 0xff;
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;
2396 serial_out(mtpt, UART_MCR, mcr);
2400 static void multi_break_ctl(struct sb_uart_port *port, int break_state)
2402 struct mp_port *mtpt = (struct mp_port *)port;
2403 unsigned long flags;
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);
2416 static int multi_startup(struct sb_uart_port *port)
2418 struct mp_port *mtpt = (struct mp_port *)port;
2419 unsigned long flags;
2420 int retval;
2422 mtpt->capabilities = uart_config[mtpt->port.type].flags;
2423 mtpt->mcr = 0;
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);
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);
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;
2446 if ((!is_real_interrupt(mtpt->port.irq)) || (mtpt->poll_type==TYPE_POLL)) {
2447 unsigned int timeout = mtpt->port.timeout;
2449 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
2451 mtpt->timer.data = (unsigned long)mtpt;
2452 mod_timer(&mtpt->timer, jiffies + timeout);
2454 else
2456 retval = serial_link_irq_chain(mtpt);
2457 if (retval)
2458 return retval;
2461 serial_outp(mtpt, UART_LCR, UART_LCR_WLEN8);
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;
2467 multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2468 spin_unlock_irqrestore(&mtpt->port.lock, flags);
2471 mtpt->ier = UART_IER_RLSI | UART_IER_RDI;
2472 serial_outp(mtpt, UART_IER, mtpt->ier);
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);
2479 return 0;
2484 static void multi_shutdown(struct sb_uart_port *port)
2486 struct mp_port *mtpt = (struct mp_port *)port;
2487 unsigned long flags;
2490 mtpt->ier = 0;
2491 serial_outp(mtpt, UART_IER, 0);
2493 spin_lock_irqsave(&mtpt->port.lock, flags);
2494 mtpt->port.mctrl &= ~TIOCM_OUT2;
2496 multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2497 spin_unlock_irqrestore(&mtpt->port.lock, flags);
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);
2506 (void) serial_in(mtpt, UART_RX);
2508 if ((!is_real_interrupt(mtpt->port.irq))||(mtpt->poll_type==TYPE_POLL))
2510 del_timer_sync(&mtpt->timer);
2512 else
2514 serial_unlink_irq_chain(mtpt);
2520 static unsigned int multi_get_divisor(struct sb_uart_port *port, unsigned int baud)
2522 unsigned int quot;
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);
2533 return quot;
2539 static void multi_set_termios(struct sb_uart_port *port, struct MP_TERMIOS *termios, struct MP_TERMIOS *old)
2541 struct mp_port *mtpt = (struct mp_port *)port;
2542 unsigned char cval, fcr = 0;
2543 unsigned long flags;
2544 unsigned int baud, quot;
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;
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;
2569 #ifdef CMSPAR
2570 if (termios->c_cflag & CMSPAR)
2571 cval |= UART_LCR_SPAR;
2572 #endif
2574 baud = sb_uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
2575 quot = multi_get_divisor(port, baud);
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;
2583 // fcr = UART_FCR_ENABLE_FIFO | 0x90;
2584 fcr = fcr_arr[mtpt->port.line];
2587 spin_lock_irqsave(&mtpt->port.lock, flags);
2589 sb_uart_update_timeout(port, termios->c_cflag, baud);
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;
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;
2606 if ((termios->c_cflag & CREAD) == 0)
2607 mtpt->port.ignore_status_mask |= UART_LSR_DR;
2609 mtpt->ier &= ~UART_IER_MSI;
2610 if (UART_ENABLE_MS(&mtpt->port, termios->c_cflag))
2611 mtpt->ier |= UART_IER_MSI;
2613 serial_out(mtpt, UART_IER, mtpt->ier);
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);
2621 serial_outp(mtpt, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2623 serial_outp(mtpt, UART_DLL, quot & 0xff); /* LS of divisor */
2624 serial_outp(mtpt, UART_DLM, quot >> 8); /* MS of divisor */
2626 serial_outp(mtpt, UART_LCR, cval); /* reset DLAB */
2627 mtpt->lcr = cval; /* Save LCR */
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);
2634 serial_outp(mtpt, UART_FCR, fcr); /* set fcr */
2637 if ((mtpt->port.type == PORT_16C105X)
2638 || (mtpt->port.type == PORT_16C105XA))
2640 if(deep[mtpt->port.line]!=0)
2641 set_deep_fifo(port, ENABLE);
2643 if (mtpt->interface != RS232)
2644 set_auto_rts(port,mtpt->interface);
2647 else
2649 if (mtpt->interface >= RS485NE)
2651 uart_clear_mctrl(&mtpt->port, TIOCM_RTS);
2655 if(mtpt->device->device_id == PCI_DEVICE_ID_MP4M)
2657 SendATCommand(mtpt);
2658 printk("SendATCommand\n");
2660 multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2661 spin_unlock_irqrestore(&mtpt->port.lock, flags);
2664 static void multi_pm(struct sb_uart_port *port, unsigned int state, unsigned int oldstate)
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);
2678 if (mtpt->pm)
2679 mtpt->pm(port, state, oldstate);
2681 else
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);
2693 if (mtpt->pm)
2694 mtpt->pm(port, state, oldstate);
2698 static void multi_release_std_resource(struct mp_port *mtpt)
2700 unsigned int size = 8 << mtpt->port.regshift;
2702 switch (mtpt->port.iotype) {
2703 case UPIO_MEM:
2704 if (!mtpt->port.mapbase)
2705 break;
2707 if (mtpt->port.flags & UPF_IOREMAP) {
2708 iounmap(mtpt->port.membase);
2709 mtpt->port.membase = NULL;
2712 release_mem_region(mtpt->port.mapbase, size);
2713 break;
2715 case UPIO_HUB6:
2716 case UPIO_PORT:
2717 release_region(mtpt->port.iobase,size);
2718 break;
2722 static void multi_release_port(struct sb_uart_port *port)
2726 static int multi_request_port(struct sb_uart_port *port)
2728 return 0;
2731 static void multi_config_port(struct sb_uart_port *port, int flags)
2733 struct mp_port *mtpt = (struct mp_port *)port;
2734 int probeflags = PROBE_ANY;
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);
2741 if (mtpt->port.type == PORT_UNKNOWN)
2742 multi_release_std_resource(mtpt);
2745 static int multi_verify_port(struct sb_uart_port *port, struct serial_struct *ser)
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;
2754 static const char *multi_type(struct sb_uart_port *port)
2756 int type = port->type;
2758 if (type >= ARRAY_SIZE(uart_config))
2759 type = 0;
2760 return uart_config[type].name;
2763 static 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,
2783 static 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,
2793 static void __init multi_init_ports(void)
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;
2802 if (!first)
2803 return;
2804 first = 0;
2806 mtpt = multi_ports;
2808 for (k=0;k<NR_BOARD;k++)
2810 sbdev = &mp_devs[k];
2812 for (i = 0; i < sbdev->nr_ports; i++, mtpt++)
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;
2824 mtpt->option_base_addr = sbdev->option_reg_addr;
2826 mtpt->poll_type = sbdev->poll_type;
2828 mtpt->port.uartclk = BASE_BAUD * 16;
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;
2840 if (sbdev->revision == 0xc0)
2842 /* for SB16C1053APCI */
2843 b_ret = sb1053a_get_interface(mtpt, i);
2845 else
2847 b_ret = read_option_register(mtpt,(MP_OPTR_IIR0 + i/8));
2848 printk("IIR_RET = %x\n",b_ret);
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;
2861 static void __init multi_register_ports(struct uart_driver *drv)
2863 int i;
2865 multi_init_ports();
2867 for (i = 0; i < NR_PORTS; i++) {
2868 struct mp_port *mtpt = &multi_ports[i];
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);
2879 * pci_remap_base - remap BAR value of pci device
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
2887 * RETURNS
2888 * If this function performs successful, it returns 0. Otherwise, It returns -1.
2890 static int pci_remap_base(struct pci_dev *pcidev, unsigned int offset,
2891 unsigned int address, unsigned int size)
2893 #if 0
2894 struct resource *root;
2895 unsigned index = (offset - 0x10) >> 2;
2896 #endif
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;
2906 if (request_resource(root, &pcidev->resource[index]) != NULL)
2908 printk(KERN_ERR "pci remap conflict!! 0x%x\n", address);
2909 return (-1);
2911 #endif
2913 return (0);
2916 static int init_mp_dev(struct pci_dev *pcidev, mppcibrd_t brd)
2918 static struct mp_device_t *sbdev = mp_devs;
2919 unsigned long addr = 0;
2920 int j;
2921 struct resource *ret = NULL;
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;
2928 /* check revision. The SB16C1053APCI's option i/o address is BAR4 */
2929 if (sbdev->revision == 0xc0)
2931 /* SB16C1053APCI */
2932 sbdev->option_reg_addr = pcidev->resource[4].start & PCI_BASE_ADDRESS_IO_MASK;
2934 else
2936 sbdev->option_reg_addr = pcidev->resource[1].start & PCI_BASE_ADDRESS_IO_MASK;
2938 #if 1
2939 if (sbdev->revision == 0xc0)
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);
2945 #endif
2947 sbdev->irq = pcidev->irq;
2949 if ((brd.device_id & 0x0800) || !(brd.device_id &0xff00))
2951 sbdev->poll_type = TYPE_INTERRUPT;
2953 else
2955 sbdev->poll_type = TYPE_POLL;
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;
2973 /* serial base address remap */
2974 if (sbdev->revision == 0xc0)
2976 int prev_port_addr = 0;
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);
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;
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;
3003 /* SB16C1053APCI */
3004 if (sbdev->revision == 0xc0)
3006 int prev_port_addr = 0;
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);
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;
3020 /* SB16C1053APCI */
3021 if (sbdev->revision == 0xc0)
3023 int prev_port_addr = 0;
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);
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 :
3043 int portnum_hex=0;
3044 portnum_hex = inb(sbdev->option_reg_addr);
3045 sbdev->nr_ports = ((portnum_hex/16)*10) + (portnum_hex % 16);
3047 break;
3048 #ifdef CONFIG_PARPORT_PC
3049 case PCI_DEVICE_ID_MP2S1P :
3050 sbdev->nr_ports = 2;
3052 /* SB16C1053APCI */
3053 if (sbdev->revision == 0xc0)
3055 int prev_port_addr = 0;
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);
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
3071 ret = request_region(sbdev->uart_access_addr, (8*sbdev->nr_ports), sbdev->name);
3073 if (sbdev->revision == 0xc0)
3075 ret = request_region(sbdev->option_reg_addr, 0x40, sbdev->name);
3077 else
3079 ret = request_region(sbdev->option_reg_addr, 0x20, sbdev->name);
3083 NR_BOARD++;
3084 NR_PORTS += sbdev->nr_ports;
3086 /* Enable PCI interrupt */
3087 addr = sbdev->option_reg_addr + MP_OPTR_IMR0;
3088 for(j=0; j < (sbdev->nr_ports/8)+1; j++)
3090 if (sbdev->poll_type == TYPE_INTERRUPT)
3092 outb(0xff,addr +j);
3095 sbdev++;
3097 return 0;
3100 static int __init multi_init(void)
3102 int ret, i;
3103 struct pci_dev *dev = NULL;
3105 if(fcr_count==0)
3107 for(i=0;i<256;i++)
3109 fcr_arr[i] = 0x01;
3113 if(deep_count==0)
3115 for(i=0;i<256;i++)
3117 deep[i] = 1;
3121 if(rtr_count==0)
3123 for(i=0;i<256;i++)
3125 rtr[i] = 0x10;
3128 if(ttr_count==0)
3130 for(i=0;i<256;i++)
3132 ttr[i] = 0x38;
3137 printk("MULTI INIT\n");
3138 for( i=0; i< mp_nrpcibrds; i++)
3141 while( (dev = pci_get_device(mp_pciboards[i].vendor_id, mp_pciboards[i].device_id, dev) ) )
3144 printk("FOUND~~~\n");
3145 // Cent OS bug fix
3146 // if (mp_pciboards[i].device_id & 0x0800)
3148 int status;
3149 pci_disable_device(dev);
3150 status = pci_enable_device(dev);
3152 if (status != 0)
3154 printk("Multiport Board Enable Fail !\n\n");
3155 status = -ENXIO;
3156 return status;
3160 init_mp_dev(dev, mp_pciboards[i]);
3164 for (i = 0; i < NR_IRQS; i++)
3165 spin_lock_init(&irq_lists[i].lock);
3167 ret = mp_register_driver(&multi_reg);
3169 if (ret >= 0)
3170 multi_register_ports(&multi_reg);
3172 return ret;
3175 static void __exit multi_exit(void)
3177 int i;
3179 for (i = 0; i < NR_PORTS; i++)
3180 mp_remove_one_port(&multi_reg, &multi_ports[i].port);
3182 mp_unregister_driver(&multi_reg);
3185 module_init(multi_init);
3186 module_exit(multi_exit);
3188 MODULE_DESCRIPTION("SystemBase Multiport PCI/PCIe CORE");
3189 MODULE_LICENSE("GPL");