Staging: sb105x: info leak in mp_get_count()
[linux-2.6/btrfs-unstable.git] / drivers / staging / sb105x / sb_pci_mp.c
bloba10cdb17038bf9717bbb20f979fb104dda9ccb27
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 fuction\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 fuction\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->termios.c_cflag & CBAUD)
547 uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
550 info->flags |= UIF_INITIALIZED;
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 if (drv->state)
1813 kfree(drv->state);
1818 static int mp_add_one_port(struct uart_driver *drv, struct sb_uart_port *port)
1820 struct sb_uart_state *state;
1821 int ret = 0;
1824 if (port->line >= drv->nr)
1825 return -EINVAL;
1827 state = drv->state + port->line;
1829 MP_MUTEX_LOCK(mp_mutex);
1830 if (state->port) {
1831 ret = -EINVAL;
1832 goto out;
1835 state->port = port;
1837 spin_lock_init(&port->lock);
1838 port->cons = drv->cons;
1839 port->info = state->info;
1841 mp_configure_port(drv, state, port);
1843 tty_register_device(drv->tty_driver, port->line, port->dev);
1845 out:
1846 MP_MUTEX_UNLOCK(mp_mutex);
1849 return ret;
1852 static int mp_remove_one_port(struct uart_driver *drv, struct sb_uart_port *port)
1854 struct sb_uart_state *state = drv->state + port->line;
1856 if (state->port != port)
1857 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
1858 state->port, port);
1860 MP_MUTEX_LOCK(mp_mutex);
1862 tty_unregister_device(drv->tty_driver, port->line);
1864 mp_unconfigure_port(drv, state);
1865 state->port = NULL;
1866 MP_MUTEX_UNLOCK(mp_mutex);
1868 return 0;
1871 static void autoconfig(struct mp_port *mtpt, unsigned int probeflags)
1873 unsigned char status1, scratch, scratch2, scratch3;
1874 unsigned char save_lcr, save_mcr;
1875 unsigned long flags;
1877 unsigned char u_type;
1878 unsigned char b_ret = 0;
1880 if (!mtpt->port.iobase && !mtpt->port.mapbase && !mtpt->port.membase)
1881 return;
1883 DEBUG_AUTOCONF("ttyMP%d: autoconf (0x%04x, 0x%p): ",
1884 mtpt->port.line, mtpt->port.iobase, mtpt->port.membase);
1886 spin_lock_irqsave(&mtpt->port.lock, flags);
1888 if (!(mtpt->port.flags & UPF_BUGGY_UART)) {
1889 scratch = serial_inp(mtpt, UART_IER);
1890 serial_outp(mtpt, UART_IER, 0);
1891 #ifdef __i386__
1892 outb(0xff, 0x080);
1893 #endif
1894 scratch2 = serial_inp(mtpt, UART_IER) & 0x0f;
1895 serial_outp(mtpt, UART_IER, 0x0F);
1896 #ifdef __i386__
1897 outb(0, 0x080);
1898 #endif
1899 scratch3 = serial_inp(mtpt, UART_IER) & 0x0F;
1900 serial_outp(mtpt, UART_IER, scratch);
1901 if (scratch2 != 0 || scratch3 != 0x0F) {
1902 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1903 scratch2, scratch3);
1904 goto out;
1908 save_mcr = serial_in(mtpt, UART_MCR);
1909 save_lcr = serial_in(mtpt, UART_LCR);
1911 if (!(mtpt->port.flags & UPF_SKIP_TEST)) {
1912 serial_outp(mtpt, UART_MCR, UART_MCR_LOOP | 0x0A);
1913 status1 = serial_inp(mtpt, UART_MSR) & 0xF0;
1914 serial_outp(mtpt, UART_MCR, save_mcr);
1915 if (status1 != 0x90) {
1916 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1917 status1);
1918 goto out;
1922 serial_outp(mtpt, UART_LCR, 0xBF);
1923 serial_outp(mtpt, UART_EFR, 0);
1924 serial_outp(mtpt, UART_LCR, 0);
1926 serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
1927 scratch = serial_in(mtpt, UART_IIR) >> 6;
1929 DEBUG_AUTOCONF("iir=%d ", scratch);
1930 if(mtpt->device->nr_ports >= 8)
1931 b_ret = read_option_register(mtpt,(MP_OPTR_DIR0 + ((mtpt->port.line)/8)));
1932 else
1933 b_ret = read_option_register(mtpt,MP_OPTR_DIR0);
1934 u_type = (b_ret & 0xf0) >> 4;
1935 if(mtpt->port.type == PORT_UNKNOWN )
1937 switch (u_type)
1939 case DIR_UART_16C550:
1940 mtpt->port.type = PORT_16C55X;
1941 break;
1942 case DIR_UART_16C1050:
1943 mtpt->port.type = PORT_16C105X;
1944 break;
1945 case DIR_UART_16C1050A:
1946 if (mtpt->port.line < 2)
1948 mtpt->port.type = PORT_16C105XA;
1950 else
1952 if (mtpt->device->device_id & 0x50)
1954 mtpt->port.type = PORT_16C55X;
1956 else
1958 mtpt->port.type = PORT_16C105X;
1961 break;
1962 default:
1963 mtpt->port.type = PORT_UNKNOWN;
1964 break;
1968 if(mtpt->port.type == PORT_UNKNOWN )
1970 printk("unknow2\n");
1971 switch (scratch) {
1972 case 0:
1973 case 1:
1974 mtpt->port.type = PORT_UNKNOWN;
1975 break;
1976 case 2:
1977 case 3:
1978 mtpt->port.type = PORT_16C55X;
1979 break;
1983 serial_outp(mtpt, UART_LCR, save_lcr);
1985 mtpt->port.fifosize = uart_config[mtpt->port.type].dfl_xmit_fifo_size;
1986 mtpt->capabilities = uart_config[mtpt->port.type].flags;
1988 if (mtpt->port.type == PORT_UNKNOWN)
1989 goto out;
1990 serial_outp(mtpt, UART_MCR, save_mcr);
1991 serial_outp(mtpt, UART_FCR, (UART_FCR_ENABLE_FIFO |
1992 UART_FCR_CLEAR_RCVR |
1993 UART_FCR_CLEAR_XMIT));
1994 serial_outp(mtpt, UART_FCR, 0);
1995 (void)serial_in(mtpt, UART_RX);
1996 serial_outp(mtpt, UART_IER, 0);
1998 out:
1999 spin_unlock_irqrestore(&mtpt->port.lock, flags);
2000 DEBUG_AUTOCONF("type=%s\n", uart_config[mtpt->port.type].name);
2003 static void autoconfig_irq(struct mp_port *mtpt)
2005 unsigned char save_mcr, save_ier;
2006 unsigned long irqs;
2007 int irq;
2009 /* forget possible initially masked and pending IRQ */
2010 probe_irq_off(probe_irq_on());
2011 save_mcr = serial_inp(mtpt, UART_MCR);
2012 save_ier = serial_inp(mtpt, UART_IER);
2013 serial_outp(mtpt, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
2015 irqs = probe_irq_on();
2016 serial_outp(mtpt, UART_MCR, 0);
2017 serial_outp(mtpt, UART_MCR,
2018 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2020 serial_outp(mtpt, UART_IER, 0x0f); /* enable all intrs */
2021 (void)serial_inp(mtpt, UART_LSR);
2022 (void)serial_inp(mtpt, UART_RX);
2023 (void)serial_inp(mtpt, UART_IIR);
2024 (void)serial_inp(mtpt, UART_MSR);
2025 serial_outp(mtpt, UART_TX, 0xFF);
2026 irq = probe_irq_off(irqs);
2028 serial_outp(mtpt, UART_MCR, save_mcr);
2029 serial_outp(mtpt, UART_IER, save_ier);
2031 mtpt->port.irq = (irq > 0) ? irq : 0;
2034 static void multi_stop_tx(struct sb_uart_port *port)
2036 struct mp_port *mtpt = (struct mp_port *)port;
2038 if (mtpt->ier & UART_IER_THRI) {
2039 mtpt->ier &= ~UART_IER_THRI;
2040 serial_out(mtpt, UART_IER, mtpt->ier);
2043 tasklet_schedule(&port->info->tlet);
2046 static void multi_start_tx(struct sb_uart_port *port)
2048 struct mp_port *mtpt = (struct mp_port *)port;
2050 if (!(mtpt->ier & UART_IER_THRI)) {
2051 mtpt->ier |= UART_IER_THRI;
2052 serial_out(mtpt, UART_IER, mtpt->ier);
2056 static void multi_stop_rx(struct sb_uart_port *port)
2058 struct mp_port *mtpt = (struct mp_port *)port;
2060 mtpt->ier &= ~UART_IER_RLSI;
2061 mtpt->port.read_status_mask &= ~UART_LSR_DR;
2062 serial_out(mtpt, UART_IER, mtpt->ier);
2065 static void multi_enable_ms(struct sb_uart_port *port)
2067 struct mp_port *mtpt = (struct mp_port *)port;
2069 mtpt->ier |= UART_IER_MSI;
2070 serial_out(mtpt, UART_IER, mtpt->ier);
2074 static _INLINE_ void receive_chars(struct mp_port *mtpt, int *status )
2076 struct tty_struct *tty = mtpt->port.info->tty;
2077 unsigned char lsr = *status;
2078 int max_count = 256;
2079 unsigned char ch;
2080 char flag;
2082 //lsr &= mtpt->port.read_status_mask;
2084 do {
2085 if ((lsr & UART_LSR_PE) && (mtpt->port.mdmode & MDMODE_ENABLE))
2087 ch = serial_inp(mtpt, UART_RX);
2089 else if (lsr & UART_LSR_SPECIAL)
2091 flag = 0;
2092 ch = serial_inp(mtpt, UART_RX);
2094 if (lsr & UART_LSR_BI)
2097 mtpt->port.icount.brk++;
2098 flag = TTY_BREAK;
2100 if (sb_uart_handle_break(&mtpt->port))
2101 goto ignore_char;
2103 if (lsr & UART_LSR_PE)
2105 mtpt->port.icount.parity++;
2106 flag = TTY_PARITY;
2108 if (lsr & UART_LSR_FE)
2110 mtpt->port.icount.frame++;
2111 flag = TTY_FRAME;
2113 if (lsr & UART_LSR_OE)
2115 mtpt->port.icount.overrun++;
2116 flag = TTY_OVERRUN;
2118 tty_insert_flip_char(tty, ch, flag);
2120 else
2122 ch = serial_inp(mtpt, UART_RX);
2123 tty_insert_flip_char(tty, ch, 0);
2125 ignore_char:
2126 lsr = serial_inp(mtpt, UART_LSR);
2127 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
2129 tty_flip_buffer_push(tty);
2135 static _INLINE_ void transmit_chars(struct mp_port *mtpt)
2137 struct circ_buf *xmit = &mtpt->port.info->xmit;
2138 int count;
2140 if (mtpt->port.x_char) {
2141 serial_outp(mtpt, UART_TX, mtpt->port.x_char);
2142 mtpt->port.icount.tx++;
2143 mtpt->port.x_char = 0;
2144 return;
2146 if (uart_circ_empty(xmit) || uart_tx_stopped(&mtpt->port)) {
2147 multi_stop_tx(&mtpt->port);
2148 return;
2151 count = uart_circ_chars_pending(xmit);
2153 if(count > mtpt->port.fifosize)
2155 count = mtpt->port.fifosize;
2158 printk("[%d] mdmode: %x\n", mtpt->port.line, mtpt->port.mdmode);
2159 do {
2160 #if 0
2161 /* check multi-drop mode */
2162 if ((mtpt->port.mdmode & (MDMODE_ENABLE | MDMODE_ADDR)) == (MDMODE_ENABLE | MDMODE_ADDR))
2164 printk("send address\n");
2165 /* send multi-drop address */
2166 serial_out(mtpt, UART_SCR, xmit->buf[xmit->tail]);
2168 else
2169 #endif
2171 serial_out(mtpt, UART_TX, xmit->buf[xmit->tail]);
2173 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
2174 mtpt->port.icount.tx++;
2175 } while (--count > 0);
2180 static _INLINE_ void check_modem_status(struct mp_port *mtpt)
2182 int status;
2184 status = serial_in(mtpt, UART_MSR);
2186 if ((status & UART_MSR_ANY_DELTA) == 0)
2187 return;
2189 if (status & UART_MSR_TERI)
2190 mtpt->port.icount.rng++;
2191 if (status & UART_MSR_DDSR)
2192 mtpt->port.icount.dsr++;
2193 if (status & UART_MSR_DDCD)
2194 sb_uart_handle_dcd_change(&mtpt->port, status & UART_MSR_DCD);
2195 if (status & UART_MSR_DCTS)
2196 sb_uart_handle_cts_change(&mtpt->port, status & UART_MSR_CTS);
2198 wake_up_interruptible(&mtpt->port.info->delta_msr_wait);
2201 static inline void multi_handle_port(struct mp_port *mtpt)
2203 unsigned int status = serial_inp(mtpt, UART_LSR);
2205 //printk("lsr: %x\n", status);
2207 if ((status & UART_LSR_DR) || (status & UART_LSR_SPECIAL))
2208 receive_chars(mtpt, &status);
2209 check_modem_status(mtpt);
2210 if (status & UART_LSR_THRE)
2212 if ((mtpt->port.type == PORT_16C105X)
2213 || (mtpt->port.type == PORT_16C105XA))
2214 transmit_chars(mtpt);
2215 else
2217 if (mtpt->interface >= RS485NE)
2218 uart_set_mctrl(&mtpt->port, TIOCM_RTS);
2220 transmit_chars(mtpt);
2223 if (mtpt->interface >= RS485NE)
2225 while((status=serial_in(mtpt,UART_LSR) &0x60)!=0x60);
2226 uart_clear_mctrl(&mtpt->port, TIOCM_RTS);
2234 static irqreturn_t multi_interrupt(int irq, void *dev_id)
2236 struct irq_info *iinfo = dev_id;
2237 struct list_head *lhead, *end = NULL;
2238 int pass_counter = 0;
2241 spin_lock(&iinfo->lock);
2243 lhead = iinfo->head;
2244 do {
2245 struct mp_port *mtpt;
2246 unsigned int iir;
2248 mtpt = list_entry(lhead, struct mp_port, list);
2250 iir = serial_in(mtpt, UART_IIR);
2251 printk("interrupt! port %d, iir 0x%x\n", mtpt->port.line, iir); //wlee
2252 if (!(iir & UART_IIR_NO_INT))
2254 printk("interrupt handle\n");
2255 spin_lock(&mtpt->port.lock);
2256 multi_handle_port(mtpt);
2257 spin_unlock(&mtpt->port.lock);
2259 end = NULL;
2260 } else if (end == NULL)
2261 end = lhead;
2263 lhead = lhead->next;
2264 if (lhead == iinfo->head && pass_counter++ > PASS_LIMIT)
2266 printk(KERN_ERR "multi: too much work for "
2267 "irq%d\n", irq);
2268 printk( "multi: too much work for "
2269 "irq%d\n", irq);
2270 break;
2272 } while (lhead != end);
2274 spin_unlock(&iinfo->lock);
2277 return IRQ_HANDLED;
2280 static void serial_do_unlink(struct irq_info *i, struct mp_port *mtpt)
2282 spin_lock_irq(&i->lock);
2284 if (!list_empty(i->head)) {
2285 if (i->head == &mtpt->list)
2286 i->head = i->head->next;
2287 list_del(&mtpt->list);
2288 } else {
2289 i->head = NULL;
2292 spin_unlock_irq(&i->lock);
2295 static int serial_link_irq_chain(struct mp_port *mtpt)
2297 struct irq_info *i = irq_lists + mtpt->port.irq;
2298 int ret, irq_flags = mtpt->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
2299 spin_lock_irq(&i->lock);
2301 if (i->head) {
2302 list_add(&mtpt->list, i->head);
2303 spin_unlock_irq(&i->lock);
2305 ret = 0;
2306 } else {
2307 INIT_LIST_HEAD(&mtpt->list);
2308 i->head = &mtpt->list;
2309 spin_unlock_irq(&i->lock);
2311 ret = request_irq(mtpt->port.irq, multi_interrupt,
2312 irq_flags, "serial", i);
2313 if (ret < 0)
2314 serial_do_unlink(i, mtpt);
2317 return ret;
2323 static void serial_unlink_irq_chain(struct mp_port *mtpt)
2325 struct irq_info *i = irq_lists + mtpt->port.irq;
2327 if (list_empty(i->head))
2329 free_irq(mtpt->port.irq, i);
2331 serial_do_unlink(i, mtpt);
2334 static void multi_timeout(unsigned long data)
2336 struct mp_port *mtpt = (struct mp_port *)data;
2339 spin_lock(&mtpt->port.lock);
2340 multi_handle_port(mtpt);
2341 spin_unlock(&mtpt->port.lock);
2343 mod_timer(&mtpt->timer, jiffies+1 );
2346 static unsigned int multi_tx_empty(struct sb_uart_port *port)
2348 struct mp_port *mtpt = (struct mp_port *)port;
2349 unsigned long flags;
2350 unsigned int ret;
2352 spin_lock_irqsave(&mtpt->port.lock, flags);
2353 ret = serial_in(mtpt, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
2354 spin_unlock_irqrestore(&mtpt->port.lock, flags);
2356 return ret;
2360 static unsigned int multi_get_mctrl(struct sb_uart_port *port)
2362 struct mp_port *mtpt = (struct mp_port *)port;
2363 unsigned char status;
2364 unsigned int ret;
2366 status = serial_in(mtpt, UART_MSR);
2368 ret = 0;
2369 if (status & UART_MSR_DCD)
2370 ret |= TIOCM_CAR;
2371 if (status & UART_MSR_RI)
2372 ret |= TIOCM_RNG;
2373 if (status & UART_MSR_DSR)
2374 ret |= TIOCM_DSR;
2375 if (status & UART_MSR_CTS)
2376 ret |= TIOCM_CTS;
2377 return ret;
2380 static void multi_set_mctrl(struct sb_uart_port *port, unsigned int mctrl)
2382 struct mp_port *mtpt = (struct mp_port *)port;
2383 unsigned char mcr = 0;
2385 mctrl &= 0xff;
2387 if (mctrl & TIOCM_RTS)
2388 mcr |= UART_MCR_RTS;
2389 if (mctrl & TIOCM_DTR)
2390 mcr |= UART_MCR_DTR;
2391 if (mctrl & TIOCM_OUT1)
2392 mcr |= UART_MCR_OUT1;
2393 if (mctrl & TIOCM_OUT2)
2394 mcr |= UART_MCR_OUT2;
2395 if (mctrl & TIOCM_LOOP)
2396 mcr |= UART_MCR_LOOP;
2399 serial_out(mtpt, UART_MCR, mcr);
2403 static void multi_break_ctl(struct sb_uart_port *port, int break_state)
2405 struct mp_port *mtpt = (struct mp_port *)port;
2406 unsigned long flags;
2408 spin_lock_irqsave(&mtpt->port.lock, flags);
2409 if (break_state == -1)
2410 mtpt->lcr |= UART_LCR_SBC;
2411 else
2412 mtpt->lcr &= ~UART_LCR_SBC;
2413 serial_out(mtpt, UART_LCR, mtpt->lcr);
2414 spin_unlock_irqrestore(&mtpt->port.lock, flags);
2419 static int multi_startup(struct sb_uart_port *port)
2421 struct mp_port *mtpt = (struct mp_port *)port;
2422 unsigned long flags;
2423 int retval;
2425 mtpt->capabilities = uart_config[mtpt->port.type].flags;
2426 mtpt->mcr = 0;
2428 if (mtpt->capabilities & UART_CLEAR_FIFO) {
2429 serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
2430 serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO |
2431 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
2432 serial_outp(mtpt, UART_FCR, 0);
2435 (void) serial_inp(mtpt, UART_LSR);
2436 (void) serial_inp(mtpt, UART_RX);
2437 (void) serial_inp(mtpt, UART_IIR);
2438 (void) serial_inp(mtpt, UART_MSR);
2439 //test-wlee 9-bit disable
2440 serial_outp(mtpt, UART_MSR, 0);
2443 if (!(mtpt->port.flags & UPF_BUGGY_UART) &&
2444 (serial_inp(mtpt, UART_LSR) == 0xff)) {
2445 printk("ttyS%d: LSR safety check engaged!\n", mtpt->port.line);
2446 //return -ENODEV;
2449 if ((!is_real_interrupt(mtpt->port.irq)) || (mtpt->poll_type==TYPE_POLL)) {
2450 unsigned int timeout = mtpt->port.timeout;
2452 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
2454 mtpt->timer.data = (unsigned long)mtpt;
2455 mod_timer(&mtpt->timer, jiffies + timeout);
2457 else
2459 retval = serial_link_irq_chain(mtpt);
2460 if (retval)
2461 return retval;
2464 serial_outp(mtpt, UART_LCR, UART_LCR_WLEN8);
2466 spin_lock_irqsave(&mtpt->port.lock, flags);
2467 if ((is_real_interrupt(mtpt->port.irq))||(mtpt->poll_type==TYPE_INTERRUPT))
2468 mtpt->port.mctrl |= TIOCM_OUT2;
2470 multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2471 spin_unlock_irqrestore(&mtpt->port.lock, flags);
2474 mtpt->ier = UART_IER_RLSI | UART_IER_RDI;
2475 serial_outp(mtpt, UART_IER, mtpt->ier);
2477 (void) serial_inp(mtpt, UART_LSR);
2478 (void) serial_inp(mtpt, UART_RX);
2479 (void) serial_inp(mtpt, UART_IIR);
2480 (void) serial_inp(mtpt, UART_MSR);
2482 return 0;
2487 static void multi_shutdown(struct sb_uart_port *port)
2489 struct mp_port *mtpt = (struct mp_port *)port;
2490 unsigned long flags;
2493 mtpt->ier = 0;
2494 serial_outp(mtpt, UART_IER, 0);
2496 spin_lock_irqsave(&mtpt->port.lock, flags);
2497 mtpt->port.mctrl &= ~TIOCM_OUT2;
2499 multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2500 spin_unlock_irqrestore(&mtpt->port.lock, flags);
2502 serial_out(mtpt, UART_LCR, serial_inp(mtpt, UART_LCR) & ~UART_LCR_SBC);
2503 serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO |
2504 UART_FCR_CLEAR_RCVR |
2505 UART_FCR_CLEAR_XMIT);
2506 serial_outp(mtpt, UART_FCR, 0);
2509 (void) serial_in(mtpt, UART_RX);
2511 if ((!is_real_interrupt(mtpt->port.irq))||(mtpt->poll_type==TYPE_POLL))
2513 del_timer_sync(&mtpt->timer);
2515 else
2517 serial_unlink_irq_chain(mtpt);
2523 static unsigned int multi_get_divisor(struct sb_uart_port *port, unsigned int baud)
2525 unsigned int quot;
2527 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2528 baud == (port->uartclk/4))
2529 quot = 0x8001;
2530 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2531 baud == (port->uartclk/8))
2532 quot = 0x8002;
2533 else
2534 quot = sb_uart_get_divisor(port, baud);
2536 return quot;
2542 static void multi_set_termios(struct sb_uart_port *port, struct MP_TERMIOS *termios, struct MP_TERMIOS *old)
2544 struct mp_port *mtpt = (struct mp_port *)port;
2545 unsigned char cval, fcr = 0;
2546 unsigned long flags;
2547 unsigned int baud, quot;
2549 switch (termios->c_cflag & CSIZE) {
2550 case CS5:
2551 cval = 0x00;
2552 break;
2553 case CS6:
2554 cval = 0x01;
2555 break;
2556 case CS7:
2557 cval = 0x02;
2558 break;
2559 default:
2560 case CS8:
2561 cval = 0x03;
2562 break;
2565 if (termios->c_cflag & CSTOPB)
2566 cval |= 0x04;
2567 if (termios->c_cflag & PARENB)
2568 cval |= UART_LCR_PARITY;
2569 if (!(termios->c_cflag & PARODD))
2570 cval |= UART_LCR_EPAR;
2572 #ifdef CMSPAR
2573 if (termios->c_cflag & CMSPAR)
2574 cval |= UART_LCR_SPAR;
2575 #endif
2577 baud = sb_uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
2578 quot = multi_get_divisor(port, baud);
2580 if (mtpt->capabilities & UART_USE_FIFO) {
2581 //if (baud < 2400)
2582 // fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2583 //else
2584 // fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
2586 // fcr = UART_FCR_ENABLE_FIFO | 0x90;
2587 fcr = fcr_arr[mtpt->port.line];
2590 spin_lock_irqsave(&mtpt->port.lock, flags);
2592 sb_uart_update_timeout(port, termios->c_cflag, baud);
2594 mtpt->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2595 if (termios->c_iflag & INPCK)
2596 mtpt->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2597 if (termios->c_iflag & (BRKINT | PARMRK))
2598 mtpt->port.read_status_mask |= UART_LSR_BI;
2600 mtpt->port.ignore_status_mask = 0;
2601 if (termios->c_iflag & IGNPAR)
2602 mtpt->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2603 if (termios->c_iflag & IGNBRK) {
2604 mtpt->port.ignore_status_mask |= UART_LSR_BI;
2605 if (termios->c_iflag & IGNPAR)
2606 mtpt->port.ignore_status_mask |= UART_LSR_OE;
2609 if ((termios->c_cflag & CREAD) == 0)
2610 mtpt->port.ignore_status_mask |= UART_LSR_DR;
2612 mtpt->ier &= ~UART_IER_MSI;
2613 if (UART_ENABLE_MS(&mtpt->port, termios->c_cflag))
2614 mtpt->ier |= UART_IER_MSI;
2616 serial_out(mtpt, UART_IER, mtpt->ier);
2618 if (mtpt->capabilities & UART_STARTECH) {
2619 serial_outp(mtpt, UART_LCR, 0xBF);
2620 serial_outp(mtpt, UART_EFR,
2621 termios->c_cflag & CRTSCTS ? UART_EFR_CTS :0);
2624 serial_outp(mtpt, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2626 serial_outp(mtpt, UART_DLL, quot & 0xff); /* LS of divisor */
2627 serial_outp(mtpt, UART_DLM, quot >> 8); /* MS of divisor */
2629 serial_outp(mtpt, UART_LCR, cval); /* reset DLAB */
2630 mtpt->lcr = cval; /* Save LCR */
2632 if (fcr & UART_FCR_ENABLE_FIFO) {
2633 /* emulated UARTs (Lucent Venus 167x) need two steps */
2634 serial_outp(mtpt, UART_FCR, UART_FCR_ENABLE_FIFO);
2637 serial_outp(mtpt, UART_FCR, fcr); /* set fcr */
2640 if ((mtpt->port.type == PORT_16C105X)
2641 || (mtpt->port.type == PORT_16C105XA))
2643 if(deep[mtpt->port.line]!=0)
2644 set_deep_fifo(port, ENABLE);
2646 if (mtpt->interface != RS232)
2647 set_auto_rts(port,mtpt->interface);
2650 else
2652 if (mtpt->interface >= RS485NE)
2654 uart_clear_mctrl(&mtpt->port, TIOCM_RTS);
2658 if(mtpt->device->device_id == PCI_DEVICE_ID_MP4M)
2660 SendATCommand(mtpt);
2661 printk("SendATCommand\n");
2663 multi_set_mctrl(&mtpt->port, mtpt->port.mctrl);
2664 spin_unlock_irqrestore(&mtpt->port.lock, flags);
2667 static void multi_pm(struct sb_uart_port *port, unsigned int state, unsigned int oldstate)
2669 struct mp_port *mtpt = (struct mp_port *)port;
2670 if (state) {
2671 if (mtpt->capabilities & UART_STARTECH) {
2672 serial_outp(mtpt, UART_LCR, 0xBF);
2673 serial_outp(mtpt, UART_EFR, UART_EFR_ECB);
2674 serial_outp(mtpt, UART_LCR, 0);
2675 serial_outp(mtpt, UART_IER, UART_IERX_SLEEP);
2676 serial_outp(mtpt, UART_LCR, 0xBF);
2677 serial_outp(mtpt, UART_EFR, 0);
2678 serial_outp(mtpt, UART_LCR, 0);
2681 if (mtpt->pm)
2682 mtpt->pm(port, state, oldstate);
2684 else
2686 if (mtpt->capabilities & UART_STARTECH) {
2687 serial_outp(mtpt, UART_LCR, 0xBF);
2688 serial_outp(mtpt, UART_EFR, UART_EFR_ECB);
2689 serial_outp(mtpt, UART_LCR, 0);
2690 serial_outp(mtpt, UART_IER, 0);
2691 serial_outp(mtpt, UART_LCR, 0xBF);
2692 serial_outp(mtpt, UART_EFR, 0);
2693 serial_outp(mtpt, UART_LCR, 0);
2696 if (mtpt->pm)
2697 mtpt->pm(port, state, oldstate);
2701 static void multi_release_std_resource(struct mp_port *mtpt)
2703 unsigned int size = 8 << mtpt->port.regshift;
2705 switch (mtpt->port.iotype) {
2706 case UPIO_MEM:
2707 if (!mtpt->port.mapbase)
2708 break;
2710 if (mtpt->port.flags & UPF_IOREMAP) {
2711 iounmap(mtpt->port.membase);
2712 mtpt->port.membase = NULL;
2715 release_mem_region(mtpt->port.mapbase, size);
2716 break;
2718 case UPIO_HUB6:
2719 case UPIO_PORT:
2720 release_region(mtpt->port.iobase,size);
2721 break;
2725 static void multi_release_port(struct sb_uart_port *port)
2729 static int multi_request_port(struct sb_uart_port *port)
2731 return 0;
2734 static void multi_config_port(struct sb_uart_port *port, int flags)
2736 struct mp_port *mtpt = (struct mp_port *)port;
2737 int probeflags = PROBE_ANY;
2739 if (flags & UART_CONFIG_TYPE)
2740 autoconfig(mtpt, probeflags);
2741 if (mtpt->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2742 autoconfig_irq(mtpt);
2744 if (mtpt->port.type == PORT_UNKNOWN)
2745 multi_release_std_resource(mtpt);
2748 static int multi_verify_port(struct sb_uart_port *port, struct serial_struct *ser)
2750 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2751 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2752 ser->type == PORT_STARTECH)
2753 return -EINVAL;
2754 return 0;
2757 static const char *multi_type(struct sb_uart_port *port)
2759 int type = port->type;
2761 if (type >= ARRAY_SIZE(uart_config))
2762 type = 0;
2763 return uart_config[type].name;
2766 static struct sb_uart_ops multi_pops = {
2767 .tx_empty = multi_tx_empty,
2768 .set_mctrl = multi_set_mctrl,
2769 .get_mctrl = multi_get_mctrl,
2770 .stop_tx = multi_stop_tx,
2771 .start_tx = multi_start_tx,
2772 .stop_rx = multi_stop_rx,
2773 .enable_ms = multi_enable_ms,
2774 .break_ctl = multi_break_ctl,
2775 .startup = multi_startup,
2776 .shutdown = multi_shutdown,
2777 .set_termios = multi_set_termios,
2778 .pm = multi_pm,
2779 .type = multi_type,
2780 .release_port = multi_release_port,
2781 .request_port = multi_request_port,
2782 .config_port = multi_config_port,
2783 .verify_port = multi_verify_port,
2786 static struct uart_driver multi_reg = {
2787 .owner = THIS_MODULE,
2788 .driver_name = "goldel_tulip",
2789 .dev_name = "ttyMP",
2790 .major = SB_TTY_MP_MAJOR,
2791 .minor = 0,
2792 .nr = MAX_MP_PORT,
2793 .cons = NULL,
2796 static void __init multi_init_ports(void)
2798 struct mp_port *mtpt;
2799 static int first = 1;
2800 int i,j,k;
2801 unsigned char osc;
2802 unsigned char b_ret = 0;
2803 static struct mp_device_t *sbdev;
2805 if (!first)
2806 return;
2807 first = 0;
2809 mtpt = multi_ports;
2811 for (k=0;k<NR_BOARD;k++)
2813 sbdev = &mp_devs[k];
2815 for (i = 0; i < sbdev->nr_ports; i++, mtpt++)
2817 mtpt->device = sbdev;
2818 mtpt->port.iobase = sbdev->uart_access_addr + 8*i;
2819 mtpt->port.irq = sbdev->irq;
2820 if ( ((sbdev->device_id == PCI_DEVICE_ID_MP4)&&(sbdev->revision==0x91)))
2821 mtpt->interface_config_addr = sbdev->option_reg_addr + 0x08 + i;
2822 else if (sbdev->revision == 0xc0)
2823 mtpt->interface_config_addr = sbdev->option_reg_addr + 0x08 + (i & 0x1);
2824 else
2825 mtpt->interface_config_addr = sbdev->option_reg_addr + 0x08 + i/8;
2827 mtpt->option_base_addr = sbdev->option_reg_addr;
2829 mtpt->poll_type = sbdev->poll_type;
2831 mtpt->port.uartclk = BASE_BAUD * 16;
2833 /* get input clock information */
2834 osc = inb(sbdev->option_reg_addr + MP_OPTR_DIR0 + i/8) & 0x0F;
2835 if (osc==0x0f)
2836 osc = 0;
2837 for(j=0;j<osc;j++)
2838 mtpt->port.uartclk *= 2;
2839 mtpt->port.flags |= STD_COM_FLAGS | UPF_SHARE_IRQ ;
2840 mtpt->port.iotype = UPIO_PORT;
2841 mtpt->port.ops = &multi_pops;
2843 if (sbdev->revision == 0xc0)
2845 /* for SB16C1053APCI */
2846 b_ret = sb1053a_get_interface(mtpt, i);
2848 else
2850 b_ret = read_option_register(mtpt,(MP_OPTR_IIR0 + i/8));
2851 printk("IIR_RET = %x\n",b_ret);
2854 /* default to RS232 */
2855 mtpt->interface = RS232;
2856 if (IIR_RS422 == (b_ret & IIR_TYPE_MASK))
2857 mtpt->interface = RS422PTP;
2858 if (IIR_RS485 == (b_ret & IIR_TYPE_MASK))
2859 mtpt->interface = RS485NE;
2864 static void __init multi_register_ports(struct uart_driver *drv)
2866 int i;
2868 multi_init_ports();
2870 for (i = 0; i < NR_PORTS; i++) {
2871 struct mp_port *mtpt = &multi_ports[i];
2873 mtpt->port.line = i;
2874 mtpt->port.ops = &multi_pops;
2875 init_timer(&mtpt->timer);
2876 mtpt->timer.function = multi_timeout;
2877 mp_add_one_port(drv, &mtpt->port);
2882 * pci_remap_base - remap BAR value of pci device
2884 * PARAMETERS
2885 * pcidev - pci_dev structure address
2886 * offset - BAR offset PCI_BASE_ADDRESS_0 ~ PCI_BASE_ADDRESS_4
2887 * address - address to be changed BAR value
2888 * size - size of address space
2890 * RETURNS
2891 * If this function performs successful, it returns 0. Otherwise, It returns -1.
2893 static int pci_remap_base(struct pci_dev *pcidev, unsigned int offset,
2894 unsigned int address, unsigned int size)
2896 #if 0
2897 struct resource *root;
2898 unsigned index = (offset - 0x10) >> 2;
2899 #endif
2901 pci_write_config_dword(pcidev, offset, address);
2902 #if 0
2903 root = pcidev->resource[index].parent;
2904 release_resource(&pcidev->resource[index]);
2905 address &= ~0x1;
2906 pcidev->resource[index].start = address;
2907 pcidev->resource[index].end = address + size - 1;
2909 if (request_resource(root, &pcidev->resource[index]) != NULL)
2911 printk(KERN_ERR "pci remap conflict!! 0x%x\n", address);
2912 return (-1);
2914 #endif
2916 return (0);
2919 static int init_mp_dev(struct pci_dev *pcidev, mppcibrd_t brd)
2921 static struct mp_device_t *sbdev = mp_devs;
2922 unsigned long addr = 0;
2923 int j;
2924 struct resource *ret = NULL;
2926 sbdev->device_id = brd.device_id;
2927 pci_read_config_byte(pcidev, PCI_CLASS_REVISION, &(sbdev->revision));
2928 sbdev->name = brd.name;
2929 sbdev->uart_access_addr = pcidev->resource[0].start & PCI_BASE_ADDRESS_IO_MASK;
2931 /* check revision. The SB16C1053APCI's option i/o address is BAR4 */
2932 if (sbdev->revision == 0xc0)
2934 /* SB16C1053APCI */
2935 sbdev->option_reg_addr = pcidev->resource[4].start & PCI_BASE_ADDRESS_IO_MASK;
2937 else
2939 sbdev->option_reg_addr = pcidev->resource[1].start & PCI_BASE_ADDRESS_IO_MASK;
2941 #if 1
2942 if (sbdev->revision == 0xc0)
2944 outb(0x00, sbdev->option_reg_addr + MP_OPTR_GPOCR);
2945 inb(sbdev->option_reg_addr + MP_OPTR_GPOCR);
2946 outb(0x83, sbdev->option_reg_addr + MP_OPTR_GPOCR);
2948 #endif
2950 sbdev->irq = pcidev->irq;
2952 if ((brd.device_id & 0x0800) || !(brd.device_id &0xff00))
2954 sbdev->poll_type = TYPE_INTERRUPT;
2956 else
2958 sbdev->poll_type = TYPE_POLL;
2961 /* codes which is specific to each board*/
2962 switch(brd.device_id){
2963 case PCI_DEVICE_ID_MP1 :
2964 case PCIE_DEVICE_ID_MP1 :
2965 case PCIE_DEVICE_ID_MP1E :
2966 case PCIE_DEVICE_ID_GT_MP1 :
2967 sbdev->nr_ports = 1;
2968 break;
2969 case PCI_DEVICE_ID_MP2 :
2970 case PCIE_DEVICE_ID_MP2 :
2971 case PCIE_DEVICE_ID_GT_MP2 :
2972 case PCIE_DEVICE_ID_MP2B :
2973 case PCIE_DEVICE_ID_MP2E :
2974 sbdev->nr_ports = 2;
2976 /* serial base address remap */
2977 if (sbdev->revision == 0xc0)
2979 int prev_port_addr = 0;
2981 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
2982 pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
2984 break;
2985 case PCI_DEVICE_ID_MP4 :
2986 case PCI_DEVICE_ID_MP4A :
2987 case PCIE_DEVICE_ID_MP4 :
2988 case PCI_DEVICE_ID_GT_MP4 :
2989 case PCI_DEVICE_ID_GT_MP4A :
2990 case PCIE_DEVICE_ID_GT_MP4 :
2991 case PCI_DEVICE_ID_MP4M :
2992 case PCIE_DEVICE_ID_MP4B :
2993 sbdev->nr_ports = 4;
2995 if(sbdev->revision == 0x91){
2996 sbdev->reserved_addr[0] = pcidev->resource[0].start & PCI_BASE_ADDRESS_IO_MASK;
2997 outb(0x03 , sbdev->reserved_addr[0] + 0x01);
2998 outb(0x03 , sbdev->reserved_addr[0] + 0x02);
2999 outb(0x01 , sbdev->reserved_addr[0] + 0x20);
3000 outb(0x00 , sbdev->reserved_addr[0] + 0x21);
3001 request_region(sbdev->reserved_addr[0], 32, sbdev->name);
3002 sbdev->uart_access_addr = pcidev->resource[1].start & PCI_BASE_ADDRESS_IO_MASK;
3003 sbdev->option_reg_addr = pcidev->resource[2].start & PCI_BASE_ADDRESS_IO_MASK;
3006 /* SB16C1053APCI */
3007 if (sbdev->revision == 0xc0)
3009 int prev_port_addr = 0;
3011 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
3012 pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
3013 pci_remap_base(pcidev, PCI_BASE_ADDRESS_2, prev_port_addr + 16, 8);
3014 pci_remap_base(pcidev, PCI_BASE_ADDRESS_3, prev_port_addr + 24, 8);
3016 break;
3017 case PCI_DEVICE_ID_MP6 :
3018 case PCI_DEVICE_ID_MP6A :
3019 case PCI_DEVICE_ID_GT_MP6 :
3020 case PCI_DEVICE_ID_GT_MP6A :
3021 sbdev->nr_ports = 6;
3023 /* SB16C1053APCI */
3024 if (sbdev->revision == 0xc0)
3026 int prev_port_addr = 0;
3028 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
3029 pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
3030 pci_remap_base(pcidev, PCI_BASE_ADDRESS_2, prev_port_addr + 16, 16);
3031 pci_remap_base(pcidev, PCI_BASE_ADDRESS_3, prev_port_addr + 32, 16);
3033 break;
3034 case PCI_DEVICE_ID_MP8 :
3035 case PCIE_DEVICE_ID_MP8 :
3036 case PCI_DEVICE_ID_GT_MP8 :
3037 case PCIE_DEVICE_ID_GT_MP8 :
3038 case PCIE_DEVICE_ID_MP8B :
3039 sbdev->nr_ports = 8;
3040 break;
3041 case PCI_DEVICE_ID_MP32 :
3042 case PCIE_DEVICE_ID_MP32 :
3043 case PCI_DEVICE_ID_GT_MP32 :
3044 case PCIE_DEVICE_ID_GT_MP32 :
3046 int portnum_hex=0;
3047 portnum_hex = inb(sbdev->option_reg_addr);
3048 sbdev->nr_ports = ((portnum_hex/16)*10) + (portnum_hex % 16);
3050 break;
3051 #ifdef CONFIG_PARPORT_PC
3052 case PCI_DEVICE_ID_MP2S1P :
3053 sbdev->nr_ports = 2;
3055 /* SB16C1053APCI */
3056 if (sbdev->revision == 0xc0)
3058 int prev_port_addr = 0;
3060 pci_read_config_dword(pcidev, PCI_BASE_ADDRESS_0, &prev_port_addr);
3061 pci_remap_base(pcidev, PCI_BASE_ADDRESS_1, prev_port_addr + 8, 8);
3064 /* add PC compatible parallel port */
3065 parport_pc_probe_port(pcidev->resource[2].start, pcidev->resource[3].start, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &pcidev->dev, 0);
3066 break;
3067 case PCI_DEVICE_ID_MP1P :
3068 /* add PC compatible parallel port */
3069 parport_pc_probe_port(pcidev->resource[2].start, pcidev->resource[3].start, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &pcidev->dev, 0);
3070 break;
3071 #endif
3074 ret = request_region(sbdev->uart_access_addr, (8*sbdev->nr_ports), sbdev->name);
3076 if (sbdev->revision == 0xc0)
3078 ret = request_region(sbdev->option_reg_addr, 0x40, sbdev->name);
3080 else
3082 ret = request_region(sbdev->option_reg_addr, 0x20, sbdev->name);
3086 NR_BOARD++;
3087 NR_PORTS += sbdev->nr_ports;
3089 /* Enable PCI interrupt */
3090 addr = sbdev->option_reg_addr + MP_OPTR_IMR0;
3091 for(j=0; j < (sbdev->nr_ports/8)+1; j++)
3093 if (sbdev->poll_type == TYPE_INTERRUPT)
3095 outb(0xff,addr +j);
3098 sbdev++;
3100 return 0;
3103 static int __init multi_init(void)
3105 int ret, i;
3106 struct pci_dev *dev = NULL;
3108 if(fcr_count==0)
3110 for(i=0;i<256;i++)
3112 fcr_arr[i] = 0x01;
3116 if(deep_count==0)
3118 for(i=0;i<256;i++)
3120 deep[i] = 1;
3124 if(rtr_count==0)
3126 for(i=0;i<256;i++)
3128 rtr[i] = 0x10;
3131 if(ttr_count==0)
3133 for(i=0;i<256;i++)
3135 ttr[i] = 0x38;
3140 printk("MULTI INIT\n");
3141 for( i=0; i< mp_nrpcibrds; i++)
3144 while( (dev = pci_get_device(mp_pciboards[i].vendor_id, mp_pciboards[i].device_id, dev) ) )
3147 printk("FOUND~~~\n");
3148 // Cent OS bug fix
3149 // if (mp_pciboards[i].device_id & 0x0800)
3151 int status;
3152 pci_disable_device(dev);
3153 status = pci_enable_device(dev);
3155 if (status != 0)
3157 printk("Multiport Board Enable Fail !\n\n");
3158 status = -ENXIO;
3159 return status;
3163 init_mp_dev(dev, mp_pciboards[i]);
3167 for (i = 0; i < NR_IRQS; i++)
3168 spin_lock_init(&irq_lists[i].lock);
3170 ret = mp_register_driver(&multi_reg);
3172 if (ret >= 0)
3173 multi_register_ports(&multi_reg);
3175 return ret;
3178 static void __exit multi_exit(void)
3180 int i;
3182 for (i = 0; i < NR_PORTS; i++)
3183 mp_remove_one_port(&multi_reg, &multi_ports[i].port);
3185 mp_unregister_driver(&multi_reg);
3188 module_init(multi_init);
3189 module_exit(multi_exit);
3191 MODULE_DESCRIPTION("SystemBase Multiport PCI/PCIe CORE");
3192 MODULE_LICENSE("GPL");