use mutex instead of semaphore in RocketPort driver
[safe/jmp/linux-2.6] / drivers / char / rocket.c
index d3bc731..61a63da 100644 (file)
 
 /****** Kernel includes ******/
 
-#ifdef MODVERSIONS
-#include <config/modversions.h>
-#endif                         
-
 #include <linux/module.h>
 #include <linux/errno.h>
 #include <linux/major.h>
@@ -85,6 +81,7 @@
 #include <linux/string.h>
 #include <linux/fcntl.h>
 #include <linux/ptrace.h>
+#include <linux/mutex.h>
 #include <linux/ioport.h>
 #include <linux/delay.h>
 #include <linux/wait.h>
@@ -93,7 +90,6 @@
 #include <asm/atomic.h>
 #include <linux/bitops.h>
 #include <linux/spinlock.h>
-#include <asm/semaphore.h>
 #include <linux/init.h>
 
 /****** RocketPort includes ******/
 
 /****** RocketPort Local Variables ******/
 
+static void rp_do_poll(unsigned long dummy);
+
 static struct tty_driver *rocket_driver;
 
 static struct rocket_version driver_version = {        
@@ -116,7 +114,7 @@ static struct r_port *rp_table[MAX_RP_PORTS];              /*  The main repository of
 static unsigned int xmit_flags[NUM_BOARDS];           /*  Bit significant, indicates port had data to transmit. */
                                                       /*  eg.  Bit 0 indicates port 0 has xmit data, ...        */
 static atomic_t rp_num_ports_open;                    /*  Number of serial ports open                           */
-static struct timer_list rocket_timer;
+static DEFINE_TIMER(rocket_timer, rp_do_poll, 0, 0);
 
 static unsigned long board1;                          /* ISA addresses, retrieved from rocketport.conf          */
 static unsigned long board2;
@@ -324,34 +322,15 @@ static void rp_do_receive(struct r_port *info,
                          CHANNEL_t * cp, unsigned int ChanStatus)
 {
        unsigned int CharNStat;
-       int ToRecv, wRecv, space = 0, count;
+       int ToRecv, wRecv, space;
        unsigned char *cbuf;
-       char *fbuf;
-       struct tty_ldisc *ld;
-
-       ld = tty_ldisc_ref(tty);
 
        ToRecv = sGetRxCnt(cp);
-       if (ld)
-               space = ld->receive_room(tty);
-       if (space > 2 * TTY_FLIPBUF_SIZE)
-               space = 2 * TTY_FLIPBUF_SIZE;
-       cbuf = tty->flip.char_buf;
-       fbuf = tty->flip.flag_buf;
-       count = 0;
 #ifdef ROCKET_DEBUG_INTR
-       printk(KERN_INFO "rp_do_receive(%d, %d)...", ToRecv, space);
+       printk(KERN_INFO "rp_do_receive(%d)...", ToRecv);
 #endif
-
-       /*
-        * determine how many we can actually read in.  If we can't
-        * read any in then we have a software overrun condition.
-        */
-       if (ToRecv > space)
-               ToRecv = space;
-
-       if (ToRecv <= 0)
-               goto done;
+       if (ToRecv == 0)
+               return;
 
        /*
         * if status indicates there are errored characters in the
@@ -379,6 +358,8 @@ static void rp_do_receive(struct r_port *info,
                       info->read_status_mask);
 #endif
                while (ToRecv) {
+                       char flag;
+
                        CharNStat = sInW(sGetTxRxDataIO(cp));
 #ifdef ROCKET_DEBUG_RECEIVE
                        printk(KERN_INFO "%x...", CharNStat);
@@ -391,17 +372,16 @@ static void rp_do_receive(struct r_port *info,
                        }
                        CharNStat &= info->read_status_mask;
                        if (CharNStat & STMBREAKH)
-                               *fbuf++ = TTY_BREAK;
+                               flag = TTY_BREAK;
                        else if (CharNStat & STMPARITYH)
-                               *fbuf++ = TTY_PARITY;
+                               flag = TTY_PARITY;
                        else if (CharNStat & STMFRAMEH)
-                               *fbuf++ = TTY_FRAME;
+                               flag = TTY_FRAME;
                        else if (CharNStat & STMRCVROVRH)
-                               *fbuf++ = TTY_OVERRUN;
+                               flag = TTY_OVERRUN;
                        else
-                               *fbuf++ = 0;
-                       *cbuf++ = CharNStat & 0xff;
-                       count++;
+                               flag = TTY_NORMAL;
+                       tty_insert_flip_char(tty, CharNStat & 0xff, flag);
                        ToRecv--;
                }
 
@@ -421,20 +401,23 @@ static void rp_do_receive(struct r_port *info,
                 * characters at time by doing repeated word IO
                 * transfer.
                 */
+               space = tty_prepare_flip_string(tty, &cbuf, ToRecv);
+               if (space < ToRecv) {
+#ifdef ROCKET_DEBUG_RECEIVE
+                       printk(KERN_INFO "rp_do_receive:insufficient space ToRecv=%d space=%d\n", ToRecv, space);
+#endif
+                       if (space <= 0)
+                               return;
+                       ToRecv = space;
+               }
                wRecv = ToRecv >> 1;
                if (wRecv)
                        sInStrW(sGetTxRxDataIO(cp), (unsigned short *) cbuf, wRecv);
                if (ToRecv & 1)
                        cbuf[ToRecv - 1] = sInB(sGetTxRxDataIO(cp));
-               memset(fbuf, 0, ToRecv);
-               cbuf += ToRecv;
-               fbuf += ToRecv;
-               count += ToRecv;
        }
        /*  Push the data up to the tty layer */
-       ld->receive_buf(tty, tty->flip.char_buf, tty->flip.flag_buf, count);
-done:
-       tty_ldisc_deref(ld);
+       tty_flip_buffer_push(tty);
 }
 
 /*
@@ -489,7 +472,6 @@ static void rp_do_transmit(struct r_port *info)
 
        if (info->xmit_cnt < WAKEUP_CHARS) {
                tty_wakeup(tty);
-               wake_up_interruptible(&tty->write_wait);
 #ifdef ROCKETPORT_HAVE_POLL_WAIT
                wake_up_interruptible(&tty->poll_wait);
 #endif
@@ -716,7 +698,7 @@ static void init_r_port(int board, int aiop, int chan, struct pci_dev *pci_dev)
                }
        }
        spin_lock_init(&info->slock);
-       sema_init(&info->write_sem, 1);
+       mutex_init(&info->write_mtx);
        rp_table[line] = info;
        if (pci_dev)
                tty_register_device(rocket_driver, line, &pci_dev->dev);
@@ -727,7 +709,7 @@ static void init_r_port(int board, int aiop, int chan, struct pci_dev *pci_dev)
  *  user mode into the driver (exception handler).  *info CD manipulation is spinlock protected.
  */
 static void configure_r_port(struct r_port *info,
-                            struct termios *old_termios)
+                            struct ktermios *old_termios)
 {
        unsigned cflag;
        unsigned long flags;
@@ -961,7 +943,7 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
 #endif
                schedule();     /*  Don't hold spinlock here, will hang PC */
        }
-       current->state = TASK_RUNNING;
+       __set_current_state(TASK_RUNNING);
        remove_wait_queue(&info->open_wait, &wait);
 
        spin_lock_irqsave(&info->slock, flags);
@@ -1032,7 +1014,7 @@ static int rp_open(struct tty_struct *tty, struct file *filp)
        /*
         * Info->count is now 1; so it's safe to sleep now.
         */
-       info->session = current->signal->session;
+       info->session = process_session(current);
        info->pgrp = process_group(current);
 
        if ((info->flags & ROCKET_INITIALIZED) == 0) {
@@ -1209,7 +1191,7 @@ static void rp_close(struct tty_struct *tty, struct file *filp)
 }
 
 static void rp_set_termios(struct tty_struct *tty,
-                          struct termios *old_termios)
+                          struct ktermios *old_termios)
 {
        struct r_port *info = (struct r_port *) tty->driver_data;
        CHANNEL_t *cp;
@@ -1616,7 +1598,7 @@ static void rp_wait_until_sent(struct tty_struct *tty, int timeout)
                if (signal_pending(current))
                        break;
        }
-       current->state = TASK_RUNNING;
+       __set_current_state(TASK_RUNNING);
 #ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
        printk(KERN_INFO "txcnt = %d (jiff=%lu)...done\n", txcnt, jiffies);
 #endif
@@ -1675,8 +1657,11 @@ static void rp_put_char(struct tty_struct *tty, unsigned char ch)
        if (rocket_paranoia_check(info, "rp_put_char"))
                return;
 
-       /*  Grab the port write semaphore, locking out other processes that try to write to this port */
-       down(&info->write_sem);
+       /*
+        * Grab the port write mutex, locking out other processes that try to
+        * write to this port
+        */
+       mutex_lock(&info->write_mtx);
 
 #ifdef ROCKET_DEBUG_WRITE
        printk(KERN_INFO "rp_put_char %c...", ch);
@@ -1698,12 +1683,12 @@ static void rp_put_char(struct tty_struct *tty, unsigned char ch)
                info->xmit_fifo_room--;
        }
        spin_unlock_irqrestore(&info->slock, flags);
-       up(&info->write_sem);
+       mutex_unlock(&info->write_mtx);
 }
 
 /*
  *  Exception handler - write routine, called when user app writes to the device.
- *  A per port write semaphore is used to protect from another process writing to
+ *  A per port write mutex is used to protect from another process writing to
  *  this port at the same time.  This other process could be running on the other CPU
  *  or get control of the CPU if the copy_from_user() blocks due to a page fault (swapped out). 
  *  Spinlocks protect the info xmit members.
@@ -1720,7 +1705,7 @@ static int rp_write(struct tty_struct *tty,
        if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
                return 0;
 
-       down_interruptible(&info->write_sem);
+       mutex_lock_interruptible(&info->write_mtx);
 
 #ifdef ROCKET_DEBUG_WRITE
        printk(KERN_INFO "rp_write %d chars...", count);
@@ -1787,12 +1772,11 @@ static int rp_write(struct tty_struct *tty,
 end:
        if (info->xmit_cnt < WAKEUP_CHARS) {
                tty_wakeup(tty);
-               wake_up_interruptible(&tty->write_wait);
 #ifdef ROCKETPORT_HAVE_POLL_WAIT
                wake_up_interruptible(&tty->poll_wait);
 #endif
        }
-       up(&info->write_sem);
+       mutex_unlock(&info->write_mtx);
        return retval;
 }
 
@@ -1856,7 +1840,6 @@ static void rp_flush_buffer(struct tty_struct *tty)
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
        spin_unlock_irqrestore(&info->slock, flags);
 
-       wake_up_interruptible(&tty->write_wait);
 #ifdef ROCKETPORT_HAVE_POLL_WAIT
        wake_up_interruptible(&tty->poll_wait);
 #endif
@@ -1868,6 +1851,12 @@ static void rp_flush_buffer(struct tty_struct *tty)
 
 #ifdef CONFIG_PCI
 
+static struct pci_device_id __devinitdata rocket_pci_ids[] = {
+       { PCI_DEVICE(PCI_VENDOR_ID_RP, PCI_ANY_ID) },
+       { }
+};
+MODULE_DEVICE_TABLE(pci, rocket_pci_ids);
+
 /*
  *  Called when a PCI card is found.  Retrieves and stores model information,
  *  init's aiopic and serial port hardware.
@@ -2229,7 +2218,7 @@ static int __init init_PCI(int boards_found)
        int count = 0;
 
        /*  Work through the PCI device list, pulling out ours */
-       while ((dev = pci_find_device(PCI_VENDOR_ID_RP, PCI_ANY_ID, dev))) {
+       while ((dev = pci_get_device(PCI_VENDOR_ID_RP, PCI_ANY_ID, dev))) {
                if (register_PCI(count + boards_found, dev))
                        count++;
        }
@@ -2349,7 +2338,7 @@ static int __init init_ISA(int i)
        return (1);
 }
 
-static struct tty_operations rocket_ops = {
+static const struct tty_operations rocket_ops = {
        .open = rp_open,
        .close = rp_close,
        .write = rp_write,
@@ -2386,12 +2375,6 @@ static int __init rp_init(void)
                return -ENOMEM;
 
        /*
-        * Set up the timer channel.
-        */
-       init_timer(&rocket_timer);
-       rocket_timer.function = rp_do_poll;
-
-       /*
         * Initialize the array of pointers to our own internal state
         * structures.
         */
@@ -2441,8 +2424,7 @@ static int __init rp_init(void)
         */
 
        rocket_driver->owner = THIS_MODULE;
-       rocket_driver->flags = TTY_DRIVER_NO_DEVFS;
-       rocket_driver->devfs_name = "tts/R";
+       rocket_driver->flags = TTY_DRIVER_DYNAMIC_DEV;
        rocket_driver->name = "ttyR";
        rocket_driver->driver_name = "Comtrol RocketPort";
        rocket_driver->major = TTY_ROCKET_MAJOR;
@@ -2452,8 +2434,10 @@ static int __init rp_init(void)
        rocket_driver->init_termios = tty_std_termios;
        rocket_driver->init_termios.c_cflag =
            B9600 | CS8 | CREAD | HUPCL | CLOCAL;
+       rocket_driver->init_termios.c_ispeed = 9600;
+       rocket_driver->init_termios.c_ospeed = 9600;
 #ifdef ROCKET_SOFT_FLOW
-       rocket_driver->flags |= TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
+       rocket_driver->flags |= TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
 #endif
        tty_set_operations(rocket_driver, &rocket_ops);