Revert "ARM: tegra: tegratab: dummy change"
[linux-2.6.git] / drivers / connector / connector.c
index 37976dc..dde6a0f 100644 (file)
@@ -1,9 +1,9 @@
 /*
  *     connector.c
- * 
- * 2004-2005 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
+ *
+ * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
  * All rights reserved.
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
 #include <linux/netlink.h>
 #include <linux/moduleparam.h>
 #include <linux/connector.h>
+#include <linux/slab.h>
 #include <linux/mutex.h>
+#include <linux/proc_fs.h>
+#include <linux/spinlock.h>
 
 #include <net/sock.h>
 
 MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
+MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
 MODULE_DESCRIPTION("Generic userspace <-> kernelspace connector.");
-
-static u32 cn_idx = CN_IDX_CONNECTOR;
-static u32 cn_val = CN_VAL_CONNECTOR;
-
-module_param(cn_idx, uint, 0);
-module_param(cn_val, uint, 0);
-MODULE_PARM_DESC(cn_idx, "Connector's main device idx.");
-MODULE_PARM_DESC(cn_val, "Connector's main device val.");
-
-static DEFINE_MUTEX(notify_lock);
-static LIST_HEAD(notify_list);
+MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_CONNECTOR);
 
 static struct cn_dev cdev;
 
-int cn_already_initialized = 0;
+static int cn_already_initialized;
 
 /*
  * msg->seq and msg->ack are used to determine message genealogy.
@@ -127,57 +120,32 @@ EXPORT_SYMBOL_GPL(cn_netlink_send);
 /*
  * Callback helper - queues work and setup destructor for given data.
  */
-static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), void *data)
+static int cn_call_callback(struct sk_buff *skb)
 {
-       struct cn_callback_entry *__cbq, *__new_cbq;
+       struct cn_callback_entry *i, *cbq = NULL;
        struct cn_dev *dev = &cdev;
+       struct cn_msg *msg = NLMSG_DATA(nlmsg_hdr(skb));
+       struct netlink_skb_parms *nsp = &NETLINK_CB(skb);
        int err = -ENODEV;
 
        spin_lock_bh(&dev->cbdev->queue_lock);
-       list_for_each_entry(__cbq, &dev->cbdev->queue_list, callback_entry) {
-               if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
-                       if (likely(!work_pending(&__cbq->work) &&
-                                       __cbq->data.ddata == NULL)) {
-                               __cbq->data.callback_priv = msg;
-
-                               __cbq->data.ddata = data;
-                               __cbq->data.destruct_data = destruct_data;
-
-                               if (queue_work(dev->cbdev->cn_queue,
-                                                       &__cbq->work))
-                                       err = 0;
-                               else
-                                       err = -EINVAL;
-                       } else {
-                               struct cn_callback_data *d;
-                               
-                               err = -ENOMEM;
-                               __new_cbq = kzalloc(sizeof(struct cn_callback_entry), GFP_ATOMIC);
-                               if (__new_cbq) {
-                                       d = &__new_cbq->data;
-                                       d->callback_priv = msg;
-                                       d->callback = __cbq->data.callback;
-                                       d->ddata = data;
-                                       d->destruct_data = destruct_data;
-                                       d->free = __new_cbq;
-
-                                       INIT_WORK(&__new_cbq->work,
-                                                       &cn_queue_wrapper);
-
-                                       if (queue_work(dev->cbdev->cn_queue,
-                                                   &__new_cbq->work))
-                                               err = 0;
-                                       else {
-                                               kfree(__new_cbq);
-                                               err = -EINVAL;
-                                       }
-                               }
-                       }
+       list_for_each_entry(i, &dev->cbdev->queue_list, callback_entry) {
+               if (cn_cb_equal(&i->id.id, &msg->id)) {
+                       atomic_inc(&i->refcnt);
+                       cbq = i;
                        break;
                }
        }
        spin_unlock_bh(&dev->cbdev->queue_lock);
 
+       if (cbq != NULL) {
+               err = 0;
+               cbq->callback(msg, nsp);
+               kfree_skb(skb);
+               cn_queue_release_callback(cbq);
+               err = 0;
+       }
+
        return err;
 }
 
@@ -188,7 +156,6 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v
  */
 static void cn_rx_skb(struct sk_buff *__skb)
 {
-       struct cn_msg *msg;
        struct nlmsghdr *nlh;
        int err;
        struct sk_buff *skb;
@@ -205,68 +172,20 @@ static void cn_rx_skb(struct sk_buff *__skb)
                        return;
                }
 
-               msg = NLMSG_DATA(nlh);
-               err = cn_call_callback(msg, (void (*)(void *))kfree_skb, skb);
+               err = cn_call_callback(skb);
                if (err < 0)
                        kfree_skb(skb);
        }
 }
 
 /*
- * Notification routing.
- *
- * Gets id and checks if there are notification request for it's idx
- * and val.  If there are such requests notify the listeners with the
- * given notify event.
- *
- */
-static void cn_notify(struct cb_id *id, u32 notify_event)
-{
-       struct cn_ctl_entry *ent;
-
-       mutex_lock(&notify_lock);
-       list_for_each_entry(ent, &notify_list, notify_entry) {
-               int i;
-               struct cn_notify_req *req;
-               struct cn_ctl_msg *ctl = ent->msg;
-               int idx_found, val_found;
-
-               idx_found = val_found = 0;
-
-               req = (struct cn_notify_req *)ctl->data;
-               for (i = 0; i < ctl->idx_notify_num; ++i, ++req) {
-                       if (id->idx >= req->first && 
-                                       id->idx < req->first + req->range) {
-                               idx_found = 1;
-                               break;
-                       }
-               }
-
-               for (i = 0; i < ctl->val_notify_num; ++i, ++req) {
-                       if (id->val >= req->first && 
-                                       id->val < req->first + req->range) {
-                               val_found = 1;
-                               break;
-                       }
-               }
-
-               if (idx_found && val_found) {
-                       struct cn_msg m = { .ack = notify_event, };
-
-                       memcpy(&m.id, id, sizeof(m.id));
-                       cn_netlink_send(&m, ctl->group, GFP_KERNEL);
-               }
-       }
-       mutex_unlock(&notify_lock);
-}
-
-/*
  * Callback add routing - adds callback with given ID and name.
  * If there is registered callback with the same ID it will not be added.
  *
  * May sleep.
  */
-int cn_add_callback(struct cb_id *id, char *name, void (*callback)(void *))
+int cn_add_callback(struct cb_id *id, const char *name,
+                   void (*callback)(struct cn_msg *, struct netlink_skb_parms *))
 {
        int err;
        struct cn_dev *dev = &cdev;
@@ -278,8 +197,6 @@ int cn_add_callback(struct cb_id *id, char *name, void (*callback)(void *))
        if (err)
                return err;
 
-       cn_notify(id, 0);
-
        return 0;
 }
 EXPORT_SYMBOL_GPL(cn_add_callback);
@@ -297,120 +214,48 @@ void cn_del_callback(struct cb_id *id)
        struct cn_dev *dev = &cdev;
 
        cn_queue_del_callback(dev->cbdev, id);
-       cn_notify(id, 1);
 }
 EXPORT_SYMBOL_GPL(cn_del_callback);
 
-/*
- * Checks two connector's control messages to be the same.
- * Returns 1 if they are the same or if the first one is corrupted.
- */
-static int cn_ctl_msg_equals(struct cn_ctl_msg *m1, struct cn_ctl_msg *m2)
+static int cn_proc_show(struct seq_file *m, void *v)
 {
-       int i;
-       struct cn_notify_req *req1, *req2;
-
-       if (m1->idx_notify_num != m2->idx_notify_num)
-               return 0;
+       struct cn_queue_dev *dev = cdev.cbdev;
+       struct cn_callback_entry *cbq;
 
-       if (m1->val_notify_num != m2->val_notify_num)
-               return 0;
+       seq_printf(m, "Name            ID\n");
 
-       if (m1->len != m2->len)
-               return 0;
+       spin_lock_bh(&dev->queue_lock);
 
-       if ((m1->idx_notify_num + m1->val_notify_num) * sizeof(*req1) !=
-           m1->len)
-               return 1;
-
-       req1 = (struct cn_notify_req *)m1->data;
-       req2 = (struct cn_notify_req *)m2->data;
-
-       for (i = 0; i < m1->idx_notify_num; ++i) {
-               if (req1->first != req2->first || req1->range != req2->range)
-                       return 0;
-               req1++;
-               req2++;
+       list_for_each_entry(cbq, &dev->queue_list, callback_entry) {
+               seq_printf(m, "%-15s %u:%u\n",
+                          cbq->id.name,
+                          cbq->id.id.idx,
+                          cbq->id.id.val);
        }
 
-       for (i = 0; i < m1->val_notify_num; ++i) {
-               if (req1->first != req2->first || req1->range != req2->range)
-                       return 0;
-               req1++;
-               req2++;
-       }
+       spin_unlock_bh(&dev->queue_lock);
 
-       return 1;
+       return 0;
 }
 
-/*
- * Main connector device's callback.
- *
- * Used for notification of a request's processing.
- */
-static void cn_callback(void *data)
+static int cn_proc_open(struct inode *inode, struct file *file)
 {
-       struct cn_msg *msg = data;
-       struct cn_ctl_msg *ctl;
-       struct cn_ctl_entry *ent;
-       u32 size;
-
-       if (msg->len < sizeof(*ctl))
-               return;
-
-       ctl = (struct cn_ctl_msg *)msg->data;
-
-       size = (sizeof(*ctl) + ((ctl->idx_notify_num +
-                                ctl->val_notify_num) *
-                               sizeof(struct cn_notify_req)));
-
-       if (msg->len != size)
-               return;
-
-       if (ctl->len + sizeof(*ctl) != msg->len)
-               return;
-
-       /*
-        * Remove notification.
-        */
-       if (ctl->group == 0) {
-               struct cn_ctl_entry *n;
-
-               mutex_lock(&notify_lock);
-               list_for_each_entry_safe(ent, n, &notify_list, notify_entry) {
-                       if (cn_ctl_msg_equals(ent->msg, ctl)) {
-                               list_del(&ent->notify_entry);
-                               kfree(ent);
-                       }
-               }
-               mutex_unlock(&notify_lock);
-
-               return;
-       }
-
-       size += sizeof(*ent);
-
-       ent = kzalloc(size, GFP_KERNEL);
-       if (!ent)
-               return;
-
-       ent->msg = (struct cn_ctl_msg *)(ent + 1);
-
-       memcpy(ent->msg, ctl, size - sizeof(*ent));
-
-       mutex_lock(&notify_lock);
-       list_add(&ent->notify_entry, &notify_list);
-       mutex_unlock(&notify_lock);
+       return single_open(file, cn_proc_show, NULL);
 }
 
+static const struct file_operations cn_file_ops = {
+       .owner   = THIS_MODULE,
+       .open    = cn_proc_open,
+       .read    = seq_read,
+       .llseek  = seq_lseek,
+       .release = single_release
+};
+
 static int __devinit cn_init(void)
 {
        struct cn_dev *dev = &cdev;
-       int err;
 
        dev->input = cn_rx_skb;
-       dev->id.idx = cn_idx;
-       dev->id.val = cn_val;
 
        dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR,
                                         CN_NETLINK_USERS + 0xf,
@@ -420,21 +265,13 @@ static int __devinit cn_init(void)
 
        dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
        if (!dev->cbdev) {
-               if (dev->nls->sk_socket)
-                       sock_release(dev->nls->sk_socket);
+               netlink_kernel_release(dev->nls);
                return -EINVAL;
        }
-       
+
        cn_already_initialized = 1;
 
-       err = cn_add_callback(&dev->id, "connector", &cn_callback);
-       if (err) {
-               cn_already_initialized = 0;
-               cn_queue_free_dev(dev->cbdev);
-               if (dev->nls->sk_socket)
-                       sock_release(dev->nls->sk_socket);
-               return -EINVAL;
-       }
+       proc_net_fops_create(&init_net, "connector", S_IRUGO, &cn_file_ops);
 
        return 0;
 }
@@ -445,10 +282,10 @@ static void __devexit cn_fini(void)
 
        cn_already_initialized = 0;
 
-       cn_del_callback(&dev->id);
+       proc_net_remove(&init_net, "connector");
+
        cn_queue_free_dev(dev->cbdev);
-       if (dev->nls->sk_socket)
-               sock_release(dev->nls->sk_socket);
+       netlink_kernel_release(dev->nls);
 }
 
 subsys_initcall(cn_init);