fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / input / serio / serport.c
index 5045d19..e1a3a79 100644 (file)
@@ -27,37 +27,51 @@ MODULE_LICENSE("GPL");
 MODULE_ALIAS_LDISC(N_MOUSE);
 
 #define SERPORT_BUSY   1
+#define SERPORT_ACTIVE 2
+#define SERPORT_DEAD   3
 
 struct serport {
        struct tty_struct *tty;
        wait_queue_head_t wait;
-       struct serio serio;
+       struct serio *serio;
+       struct serio_device_id id;
+       spinlock_t lock;
        unsigned long flags;
-       char phys[32];
 };
 
-char serport_name[] = "Serial port";
-
 /*
  * Callback functions from the serio code.
  */
 
 static int serport_serio_write(struct serio *serio, unsigned char data)
 {
-       struct serport *serport = serio->driver;
-       return -(serport->tty->driver->write(serport->tty, 0, &data, 1) != 1);
+       struct serport *serport = serio->port_data;
+       return -(serport->tty->driver->write(serport->tty, &data, 1) != 1);
 }
 
 static int serport_serio_open(struct serio *serio)
 {
-        return 0;
+       struct serport *serport = serio->port_data;
+       unsigned long flags;
+
+       spin_lock_irqsave(&serport->lock, flags);
+       set_bit(SERPORT_ACTIVE, &serport->flags);
+       spin_unlock_irqrestore(&serport->lock, flags);
+
+       return 0;
 }
 
+
 static void serport_serio_close(struct serio *serio)
 {
-       struct serport *serport = serio->driver;
+       struct serport *serport = serio->port_data;
+       unsigned long flags;
+
+       spin_lock_irqsave(&serport->lock, flags);
+       clear_bit(SERPORT_ACTIVE, &serport->flags);
+       set_bit(SERPORT_DEAD, &serport->flags);
+       spin_unlock_irqrestore(&serport->lock, flags);
 
-       serport->serio.type = 0;
        wake_up_interruptible(&serport->wait);
 }
 
@@ -69,30 +83,22 @@ static void serport_serio_close(struct serio *serio)
 static int serport_ldisc_open(struct tty_struct *tty)
 {
        struct serport *serport;
-       char name[64];
 
-       serport = kmalloc(sizeof(struct serport), GFP_KERNEL);
-       if (unlikely(!serport))
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+
+       serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
+       if (!serport)
                return -ENOMEM;
-       memset(serport, 0, sizeof(struct serport));
 
-       set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
        serport->tty = tty;
-       tty->disc_data = serport;
-
-       snprintf(serport->phys, sizeof(serport->phys), "%s/serio0", tty_name(tty, name));
-
-       serport->serio.name = serport_name;
-       serport->serio.phys = serport->phys;
-
-       serport->serio.type = SERIO_RS232;
-       serport->serio.write = serport_serio_write;
-       serport->serio.open = serport_serio_open;
-       serport->serio.close = serport_serio_close;
-       serport->serio.driver = serport;
-
+       spin_lock_init(&serport->lock);
        init_waitqueue_head(&serport->wait);
 
+       tty->disc_data = serport;
+       tty->receive_room = 256;
+       set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
+
        return 0;
 }
 
@@ -102,7 +108,8 @@ static int serport_ldisc_open(struct tty_struct *tty)
 
 static void serport_ldisc_close(struct tty_struct *tty)
 {
-       struct serport *serport = (struct serport*) tty->disc_data;
+       struct serport *serport = (struct serport *) tty->disc_data;
+
        kfree(serport);
 }
 
@@ -110,49 +117,62 @@ static void serport_ldisc_close(struct tty_struct *tty)
  * serport_ldisc_receive() is called by the low level tty driver when characters
  * are ready for us. We forward the characters, one by one to the 'interrupt'
  * routine.
- *
- * FIXME: We should get pt_regs from the tty layer and forward them to
- *       serio_interrupt here.
  */
 
 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
 {
        struct serport *serport = (struct serport*) tty->disc_data;
+       unsigned long flags;
        int i;
-       for (i = 0; i < count; i++)
-               serio_interrupt(&serport->serio, cp[i], 0, NULL);
-}
 
-/*
- * serport_ldisc_room() reports how much room we do have for receiving data.
- * Although we in fact have infinite room, we need to specify some value
- * here, and 256 seems to be reasonable.
- */
+       spin_lock_irqsave(&serport->lock, flags);
 
-static int serport_ldisc_room(struct tty_struct *tty)
-{
-       return 256;
+       if (!test_bit(SERPORT_ACTIVE, &serport->flags))
+               goto out;
+
+       for (i = 0; i < count; i++)
+               serio_interrupt(serport->serio, cp[i], 0);
+
+out:
+       spin_unlock_irqrestore(&serport->lock, flags);
 }
 
 /*
- * serport_ldisc_read() just waits indefinitely if everything goes well. 
+ * serport_ldisc_read() just waits indefinitely if everything goes well.
  * However, when the serio driver closes the serio port, it finishes,
  * returning 0 characters.
  */
 
-static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char * buf, size_t nr)
+static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
 {
        struct serport *serport = (struct serport*) tty->disc_data;
+       struct serio *serio;
        char name[64];
 
        if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
                return -EBUSY;
 
-       serio_register_port(&serport->serio);
+       serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
+       if (!serio)
+               return -ENOMEM;
+
+       strlcpy(serio->name, "Serial port", sizeof(serio->name));
+       snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
+       serio->id = serport->id;
+       serio->id.type = SERIO_RS232;
+       serio->write = serport_serio_write;
+       serio->open = serport_serio_open;
+       serio->close = serport_serio_close;
+       serio->port_data = serport;
+
+       serio_register_port(serport->serio);
        printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
-       wait_event_interruptible(serport->wait, !serport->serio.type);
-       serio_unregister_port(&serport->serio);
 
+       wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
+       serio_unregister_port(serport->serio);
+       serport->serio = NULL;
+
+       clear_bit(SERPORT_DEAD, &serport->flags);
        clear_bit(SERPORT_BUSY, &serport->flags);
 
        return 0;
@@ -165,18 +185,31 @@ static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, u
 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
 {
        struct serport *serport = (struct serport*) tty->disc_data;
-       
-       if (cmd == SPIOCSTYPE)
-               return get_user(serport->serio.type, (unsigned long *) arg);
+       unsigned long type;
+
+       if (cmd == SPIOCSTYPE) {
+               if (get_user(type, (unsigned long __user *) arg))
+                       return -EFAULT;
+
+               serport->id.proto = type & 0x000000ff;
+               serport->id.id    = (type & 0x0000ff00) >> 8;
+               serport->id.extra = (type & 0x00ff0000) >> 16;
+
+               return 0;
+       }
 
        return -EINVAL;
 }
 
 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
 {
-       struct serport *sp = (struct serport *) tty->disc_data;
+       struct serport *serport = (struct serport *) tty->disc_data;
+       unsigned long flags;
 
-       serio_dev_write_wakeup(&sp->serio);
+       spin_lock_irqsave(&serport->lock, flags);
+       if (test_bit(SERPORT_ACTIVE, &serport->flags))
+               serio_drv_write_wakeup(serport->serio);
+       spin_unlock_irqrestore(&serport->lock, flags);
 }
 
 /*
@@ -191,7 +224,6 @@ static struct tty_ldisc serport_ldisc = {
        .read =         serport_ldisc_read,
        .ioctl =        serport_ldisc_ioctl,
        .receive_buf =  serport_ldisc_receive,
-       .receive_room = serport_ldisc_room,
        .write_wakeup = serport_ldisc_write_wakeup
 };
 
@@ -211,7 +243,7 @@ static int __init serport_init(void)
 
 static void __exit serport_exit(void)
 {
-       tty_register_ldisc(N_MOUSE, NULL);
+       tty_unregister_ldisc(N_MOUSE);
 }
 
 module_init(serport_init);