nfsd: allow exports of symlinks
[safe/jmp/linux-2.6] / drivers / char / tlclk.c
index bc56df8..80ea6bc 100644 (file)
  * MPCBL0010 ATCA computer.
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/init.h>
-#include <linux/sched.h>
 #include <linux/kernel.h>      /* printk() */
 #include <linux/fs.h>          /* everything... */
 #include <linux/errno.h>       /* error codes */
-#include <linux/delay.h>       /* udelay */
+#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/ioport.h>
 #include <linux/interrupt.h>
 #include <linux/spinlock.h>
+#include <linux/smp_lock.h>
 #include <linux/timer.h>
 #include <linux/sysfs.h>
 #include <linux/device.h>
@@ -156,6 +155,8 @@ This directory exports the following interfaces.  There operation is
 documented in the MCPBL0010 TPS under the Telecom Clock API section, 11.4.
 alarms                         :
 current_ref                    :
+received_ref_clk3a             :
+received_ref_clk3b             :
 enable_clk3a_output            :
 enable_clk3b_output            :
 enable_clka0_output            :
@@ -165,7 +166,7 @@ enable_clkb1_output         :
 filter_select                  :
 hardware_switching             :
 hardware_switching_mode                :
-interrupt_switch               :
+telclock_version               :
 mode_select                    :
 refalign                       :
 reset                          :
@@ -173,7 +174,6 @@ select_amcb1_transmit_clock :
 select_amcb2_transmit_clock    :
 select_redundant_clock         :
 select_ref_frequency           :
-test_mode                      :
 
 All sysfs interfaces are integers in hex format, i.e echo 99 > refalign
 has the same effect as echo 0x99 > refalign.
@@ -187,6 +187,7 @@ static int got_event;               /* if events processing have been done */
 static void switchover_timeout(unsigned long data);
 static struct timer_list switchover_timer =
        TIMER_INITIALIZER(switchover_timeout , 0, 0);
+static unsigned long tlclk_timer_data;
 
 static struct tlclk_alarms *alarm_events;
 
@@ -194,14 +195,26 @@ static DEFINE_SPINLOCK(event_lock);
 
 static int tlclk_major = TLCLK_MAJOR;
 
-static irqreturn_t tlclk_interrupt(int irq, void *dev_id, struct pt_regs *regs);
+static irqreturn_t tlclk_interrupt(int irq, void *dev_id);
 
 static DECLARE_WAIT_QUEUE_HEAD(wq);
 
+static unsigned long useflags;
+static DEFINE_MUTEX(tlclk_mutex);
+
 static int tlclk_open(struct inode *inode, struct file *filp)
 {
        int result;
 
+       lock_kernel();
+       if (test_and_set_bit(0, &useflags)) {
+               result = -EBUSY;
+               /* this legacy device is always one per system and it doesn't
+                * know how to handle multiple concurrent clients.
+                */
+               goto out;
+       }
+
        /* Make sure there is no interrupt pending while
         * initialising interrupt handler */
        inb(TLCLK_REG6);
@@ -209,48 +222,49 @@ static int tlclk_open(struct inode *inode, struct file *filp)
        /* This device is wired through the FPGA IO space of the ATCA blade
         * we can't share this IRQ */
        result = request_irq(telclk_interrupt, &tlclk_interrupt,
-                            SA_INTERRUPT, "telco_clock", tlclk_interrupt);
-       if (result == -EBUSY) {
+                            IRQF_DISABLED, "telco_clock", tlclk_interrupt);
+       if (result == -EBUSY)
                printk(KERN_ERR "tlclk: Interrupt can't be reserved.\n");
-               return -EBUSY;
-       }
-       inb(TLCLK_REG6);        /* Clear interrupt events */
+       else
+               inb(TLCLK_REG6);        /* Clear interrupt events */
 
-       return 0;
+out:
+       unlock_kernel();
+       return result;
 }
 
 static int tlclk_release(struct inode *inode, struct file *filp)
 {
        free_irq(telclk_interrupt, tlclk_interrupt);
+       clear_bit(0, &useflags);
 
        return 0;
 }
 
-ssize_t tlclk_read(struct file *filp, char __user *buf, size_t count,
+static ssize_t tlclk_read(struct file *filp, char __user *buf, size_t count,
                loff_t *f_pos)
 {
        if (count < sizeof(struct tlclk_alarms))
                return -EIO;
+       if (mutex_lock_interruptible(&tlclk_mutex))
+               return -EINTR;
+
 
        wait_event_interruptible(wq, got_event);
-       if (copy_to_user(buf, alarm_events, sizeof(struct tlclk_alarms)))
+       if (copy_to_user(buf, alarm_events, sizeof(struct tlclk_alarms))) {
+               mutex_unlock(&tlclk_mutex);
                return -EFAULT;
+       }
 
        memset(alarm_events, 0, sizeof(struct tlclk_alarms));
        got_event = 0;
 
+       mutex_unlock(&tlclk_mutex);
        return  sizeof(struct tlclk_alarms);
 }
 
-ssize_t tlclk_write(struct file *filp, const char __user *buf, size_t count,
-           loff_t *f_pos)
-{
-       return 0;
-}
-
-static struct file_operations tlclk_fops = {
+static const struct file_operations tlclk_fops = {
        .read = tlclk_read,
-       .write = tlclk_write,
        .open = tlclk_open,
        .release = tlclk_release,
 
@@ -278,21 +292,21 @@ static ssize_t show_current_ref(struct device *d,
 static DEVICE_ATTR(current_ref, S_IRUGO, show_current_ref, NULL);
 
 
-static ssize_t show_interrupt_switch(struct device *d,
+static ssize_t show_telclock_version(struct device *d,
                struct device_attribute *attr, char *buf)
 {
        unsigned long ret_val;
        unsigned long flags;
 
        spin_lock_irqsave(&event_lock, flags);
-       ret_val = inb(TLCLK_REG6);
+       ret_val = inb(TLCLK_REG5);
        spin_unlock_irqrestore(&event_lock, flags);
 
        return sprintf(buf, "0x%lX\n", ret_val);
 }
 
-static DEVICE_ATTR(interrupt_switch, S_IRUGO,
-               show_interrupt_switch, NULL);
+static DEVICE_ATTR(telclock_version, S_IRUGO,
+               show_telclock_version, NULL);
 
 static ssize_t show_alarms(struct device *d,
                struct device_attribute *attr,  char *buf)
@@ -309,6 +323,50 @@ static ssize_t show_alarms(struct device *d,
 
 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
 
+static ssize_t store_received_ref_clk3a(struct device *d,
+                struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned long tmp;
+       unsigned char val;
+       unsigned long flags;
+
+       sscanf(buf, "%lX", &tmp);
+       dev_dbg(d, ": tmp = 0x%lX\n", tmp);
+
+       val = (unsigned char)tmp;
+       spin_lock_irqsave(&event_lock, flags);
+       SET_PORT_BITS(TLCLK_REG1, 0xef, val);
+       spin_unlock_irqrestore(&event_lock, flags);
+
+       return strnlen(buf, count);
+}
+
+static DEVICE_ATTR(received_ref_clk3a, (S_IWUSR|S_IWGRP), NULL,
+               store_received_ref_clk3a);
+
+
+static ssize_t store_received_ref_clk3b(struct device *d,
+                struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned long tmp;
+       unsigned char val;
+       unsigned long flags;
+
+       sscanf(buf, "%lX", &tmp);
+       dev_dbg(d, ": tmp = 0x%lX\n", tmp);
+
+       val = (unsigned char)tmp;
+       spin_lock_irqsave(&event_lock, flags);
+       SET_PORT_BITS(TLCLK_REG1, 0xdf, val << 1);
+       spin_unlock_irqrestore(&event_lock, flags);
+
+       return strnlen(buf, count);
+}
+
+static DEVICE_ATTR(received_ref_clk3b, (S_IWUSR|S_IWGRP), NULL,
+               store_received_ref_clk3b);
+
+
 static ssize_t store_enable_clk3b_output(struct device *d,
                 struct device_attribute *attr, const char *buf, size_t count)
 {
@@ -327,7 +385,7 @@ static ssize_t store_enable_clk3b_output(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(enable_clk3b_output, S_IWUGO, NULL,
+static DEVICE_ATTR(enable_clk3b_output, (S_IWUSR|S_IWGRP), NULL,
                store_enable_clk3b_output);
 
 static ssize_t store_enable_clk3a_output(struct device *d,
@@ -348,7 +406,7 @@ static ssize_t store_enable_clk3a_output(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(enable_clk3a_output, S_IWUGO, NULL,
+static DEVICE_ATTR(enable_clk3a_output, (S_IWUSR|S_IWGRP), NULL,
                store_enable_clk3a_output);
 
 static ssize_t store_enable_clkb1_output(struct device *d,
@@ -369,7 +427,7 @@ static ssize_t store_enable_clkb1_output(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(enable_clkb1_output, S_IWUGO, NULL,
+static DEVICE_ATTR(enable_clkb1_output, (S_IWUSR|S_IWGRP), NULL,
                store_enable_clkb1_output);
 
 
@@ -391,7 +449,7 @@ static ssize_t store_enable_clka1_output(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(enable_clka1_output, S_IWUGO, NULL,
+static DEVICE_ATTR(enable_clka1_output, (S_IWUSR|S_IWGRP), NULL,
                store_enable_clka1_output);
 
 static ssize_t store_enable_clkb0_output(struct device *d,
@@ -412,7 +470,7 @@ static ssize_t store_enable_clkb0_output(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(enable_clkb0_output, S_IWUGO, NULL,
+static DEVICE_ATTR(enable_clkb0_output, (S_IWUSR|S_IWGRP), NULL,
                store_enable_clkb0_output);
 
 static ssize_t store_enable_clka0_output(struct device *d,
@@ -433,29 +491,9 @@ static ssize_t store_enable_clka0_output(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(enable_clka0_output, S_IWUGO, NULL,
+static DEVICE_ATTR(enable_clka0_output, (S_IWUSR|S_IWGRP), NULL,
                store_enable_clka0_output);
 
-static ssize_t store_test_mode(struct device *d,
-               struct device_attribute *attr,  const char *buf, size_t count)
-{
-       unsigned long flags;
-       unsigned long tmp;
-       unsigned char val;
-
-       sscanf(buf, "%lX", &tmp);
-       dev_dbg(d, "tmp = 0x%lX\n", tmp);
-
-       val = (unsigned char)tmp;
-       spin_lock_irqsave(&event_lock, flags);
-       SET_PORT_BITS(TLCLK_REG4, 0xfd, 2);
-       spin_unlock_irqrestore(&event_lock, flags);
-
-       return strnlen(buf, count);
-}
-
-static DEVICE_ATTR(test_mode, S_IWUGO, NULL, store_test_mode);
-
 static ssize_t store_select_amcb2_transmit_clock(struct device *d,
                struct device_attribute *attr, const char *buf, size_t count)
 {
@@ -475,7 +513,7 @@ static ssize_t store_select_amcb2_transmit_clock(struct device *d,
                        SET_PORT_BITS(TLCLK_REG3, 0xc7, 0x38);
                        switch (val) {
                        case CLK_8_592MHz:
-                               SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
+                               SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
                                break;
                        case CLK_11_184MHz:
                                SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
@@ -484,7 +522,7 @@ static ssize_t store_select_amcb2_transmit_clock(struct device *d,
                                SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
                                break;
                        case CLK_44_736MHz:
-                               SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
+                               SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
                                break;
                        }
                } else
@@ -495,7 +533,7 @@ static ssize_t store_select_amcb2_transmit_clock(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(select_amcb2_transmit_clock, S_IWUGO, NULL,
+static DEVICE_ATTR(select_amcb2_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
        store_select_amcb2_transmit_clock);
 
 static ssize_t store_select_amcb1_transmit_clock(struct device *d,
@@ -517,7 +555,7 @@ static ssize_t store_select_amcb1_transmit_clock(struct device *d,
                        SET_PORT_BITS(TLCLK_REG3, 0xf8, 0x7);
                        switch (val) {
                        case CLK_8_592MHz:
-                               SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
+                               SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
                                break;
                        case CLK_11_184MHz:
                                SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
@@ -526,7 +564,7 @@ static ssize_t store_select_amcb1_transmit_clock(struct device *d,
                                SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
                                break;
                        case CLK_44_736MHz:
-                               SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
+                               SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
                                break;
                        }
                } else
@@ -536,7 +574,7 @@ static ssize_t store_select_amcb1_transmit_clock(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(select_amcb1_transmit_clock, S_IWUGO, NULL,
+static DEVICE_ATTR(select_amcb1_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
                store_select_amcb1_transmit_clock);
 
 static ssize_t store_select_redundant_clock(struct device *d,
@@ -557,7 +595,7 @@ static ssize_t store_select_redundant_clock(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(select_redundant_clock, S_IWUGO, NULL,
+static DEVICE_ATTR(select_redundant_clock, (S_IWUSR|S_IWGRP), NULL,
                store_select_redundant_clock);
 
 static ssize_t store_select_ref_frequency(struct device *d,
@@ -578,7 +616,7 @@ static ssize_t store_select_ref_frequency(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(select_ref_frequency, S_IWUGO, NULL,
+static DEVICE_ATTR(select_ref_frequency, (S_IWUSR|S_IWGRP), NULL,
                store_select_ref_frequency);
 
 static ssize_t store_filter_select(struct device *d,
@@ -599,7 +637,7 @@ static ssize_t store_filter_select(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(filter_select, S_IWUGO, NULL, store_filter_select);
+static DEVICE_ATTR(filter_select, (S_IWUSR|S_IWGRP), NULL, store_filter_select);
 
 static ssize_t store_hardware_switching_mode(struct device *d,
                 struct device_attribute *attr, const char *buf, size_t count)
@@ -619,7 +657,7 @@ static ssize_t store_hardware_switching_mode(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(hardware_switching_mode, S_IWUGO, NULL,
+static DEVICE_ATTR(hardware_switching_mode, (S_IWUSR|S_IWGRP), NULL,
                store_hardware_switching_mode);
 
 static ssize_t store_hardware_switching(struct device *d,
@@ -640,7 +678,7 @@ static ssize_t store_hardware_switching(struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(hardware_switching, S_IWUGO, NULL,
+static DEVICE_ATTR(hardware_switching, (S_IWUSR|S_IWGRP), NULL,
                store_hardware_switching);
 
 static ssize_t store_refalign (struct device *d,
@@ -653,16 +691,14 @@ static ssize_t store_refalign (struct device *d,
        dev_dbg(d, "tmp = 0x%lX\n", tmp);
        spin_lock_irqsave(&event_lock, flags);
        SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
-       udelay(2);
        SET_PORT_BITS(TLCLK_REG0, 0xf7, 0x08);
-       udelay(2);
        SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
        spin_unlock_irqrestore(&event_lock, flags);
 
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(refalign, S_IWUGO, NULL, store_refalign);
+static DEVICE_ATTR(refalign, (S_IWUSR|S_IWGRP), NULL, store_refalign);
 
 static ssize_t store_mode_select (struct device *d,
                 struct device_attribute *attr, const char *buf, size_t count)
@@ -682,7 +718,7 @@ static ssize_t store_mode_select (struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(mode_select, S_IWUGO, NULL, store_mode_select);
+static DEVICE_ATTR(mode_select, (S_IWUSR|S_IWGRP), NULL, store_mode_select);
 
 static ssize_t store_reset (struct device *d,
                 struct device_attribute *attr, const char *buf, size_t count)
@@ -702,19 +738,20 @@ static ssize_t store_reset (struct device *d,
        return strnlen(buf, count);
 }
 
-static DEVICE_ATTR(reset, S_IWUGO, NULL, store_reset);
+static DEVICE_ATTR(reset, (S_IWUSR|S_IWGRP), NULL, store_reset);
 
 static struct attribute *tlclk_sysfs_entries[] = {
        &dev_attr_current_ref.attr,
-       &dev_attr_interrupt_switch.attr,
+       &dev_attr_telclock_version.attr,
        &dev_attr_alarms.attr,
+       &dev_attr_received_ref_clk3a.attr,
+       &dev_attr_received_ref_clk3b.attr,
        &dev_attr_enable_clk3a_output.attr,
        &dev_attr_enable_clk3b_output.attr,
        &dev_attr_enable_clkb1_output.attr,
        &dev_attr_enable_clka1_output.attr,
        &dev_attr_enable_clkb0_output.attr,
        &dev_attr_enable_clka0_output.attr,
-       &dev_attr_test_mode.attr,
        &dev_attr_select_amcb1_transmit_clock.attr,
        &dev_attr_select_amcb2_transmit_clock.attr,
        &dev_attr_select_redundant_clock.attr,
@@ -744,6 +781,7 @@ static int __init tlclk_init(void)
                printk(KERN_ERR "tlclk: can't get major %d.\n", tlclk_major);
                return ret;
        }
+       tlclk_major = ret;
        alarm_events = kzalloc( sizeof(struct tlclk_alarms), GFP_KERNEL);
        if (!alarm_events)
                goto out1;
@@ -769,15 +807,14 @@ static int __init tlclk_init(void)
        ret = misc_register(&tlclk_miscdev);
        if (ret < 0) {
                printk(KERN_ERR "tlclk: misc_register returns %d.\n", ret);
-               ret = -EBUSY;
                goto out3;
        }
 
        tlclk_device = platform_device_register_simple("telco_clock",
                                -1, NULL, 0);
-       if (!tlclk_device) {
+       if (IS_ERR(tlclk_device)) {
                printk(KERN_ERR "tlclk: platform_device_register failed.\n");
-               ret = -EBUSY;
+               ret = PTR_ERR(tlclk_device);
                goto out4;
        }
 
@@ -785,8 +822,6 @@ static int __init tlclk_init(void)
                        &tlclk_attribute_group);
        if (ret) {
                printk(KERN_ERR "tlclk: failed to create sysfs device attributes.\n");
-               sysfs_remove_group(&tlclk_device->dev.kobj,
-                       &tlclk_attribute_group);
                goto out5;
        }
 
@@ -819,11 +854,13 @@ static void __exit tlclk_cleanup(void)
 
 static void switchover_timeout(unsigned long data)
 {
-       if ((data & 1)) {
-               if ((inb(TLCLK_REG1) & 0x08) != (data & 0x08))
+       unsigned long flags = *(unsigned long *) data;
+
+       if ((flags & 1)) {
+               if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
                        alarm_events->switchover_primary++;
        } else {
-               if ((inb(TLCLK_REG1) & 0x08) != (data & 0x08))
+               if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
                        alarm_events->switchover_secondary++;
        }
 
@@ -833,7 +870,7 @@ static void switchover_timeout(unsigned long data)
        wake_up(&wq);
 }
 
-static irqreturn_t tlclk_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+static irqreturn_t tlclk_interrupt(int irq, void *dev_id)
 {
        unsigned long flags;
 
@@ -881,8 +918,9 @@ static irqreturn_t tlclk_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 
                /* TIMEOUT in ~10ms */
                switchover_timer.expires = jiffies + msecs_to_jiffies(10);
-               switchover_timer.data = inb(TLCLK_REG1);
-               add_timer(&switchover_timer);
+               tlclk_timer_data = inb(TLCLK_REG1);
+               switchover_timer.data = (unsigned long) &tlclk_timer_data;
+               mod_timer(&switchover_timer, switchover_timer.expires);
        } else {
                got_event = 1;
                wake_up(&wq);