include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / connector / connector.c
index bf48300..1d48f40 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
@@ -26,6 +26,7 @@
 #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);
-
 static struct cn_dev cdev;
 
 static int cn_already_initialized;
@@ -129,44 +119,41 @@ 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_dev *dev = &cdev;
+       struct cn_msg *msg = NLMSG_DATA(nlmsg_hdr(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.skb == NULL)) {
+                               __cbq->data.skb = skb;
 
-                               __cbq->data.ddata = data;
-                               __cbq->data.destruct_data = destruct_data;
-
-                               if (queue_work(dev->cbdev->cn_queue,
-                                                       &__cbq->work))
+                               if (queue_cn_work(__cbq, &__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->skb = skb;
                                        d->callback = __cbq->data.callback;
-                                       d->ddata = data;
-                                       d->destruct_data = destruct_data;
                                        d->free = __new_cbq;
 
+                                       __new_cbq->pdev = __cbq->pdev;
+
                                        INIT_WORK(&__new_cbq->work,
                                                        &cn_queue_wrapper);
 
-                                       if (queue_work(dev->cbdev->cn_queue,
+                                       if (queue_cn_work(__new_cbq,
                                                    &__new_cbq->work))
                                                err = 0;
                                        else {
@@ -190,7 +177,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;
@@ -207,68 +193,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, char *name,
+                   void (*callback)(struct cn_msg *, struct netlink_skb_parms *))
 {
        int err;
        struct cn_dev *dev = &cdev;
@@ -280,8 +218,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);
@@ -299,112 +235,9 @@ 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)
-{
-       int i;
-       struct cn_notify_req *req1, *req2;
-
-       if (m1->idx_notify_num != m2->idx_notify_num)
-               return 0;
-
-       if (m1->val_notify_num != m2->val_notify_num)
-               return 0;
-
-       if (m1->len != m2->len)
-               return 0;
-
-       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++;
-       }
-
-       for (i = 0; i < m1->val_notify_num; ++i) {
-               if (req1->first != req2->first || req1->range != req2->range)
-                       return 0;
-               req1++;
-               req2++;
-       }
-
-       return 1;
-}
-
-/*
- * Main connector device's callback.
- *
- * Used for notification of a request's processing.
- */
-static void cn_callback(void *data)
-{
-       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);
-}
-
 static int cn_proc_show(struct seq_file *m, void *v)
 {
        struct cn_queue_dev *dev = cdev.cbdev;
@@ -442,11 +275,8 @@ static const struct file_operations cn_file_ops = {
 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,
@@ -459,16 +289,8 @@ static int __devinit cn_init(void)
                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);
-               netlink_kernel_release(dev->nls);
-               return -EINVAL;
-       }
+       cn_already_initialized = 1;
 
        proc_net_fops_create(&init_net, "connector", S_IRUGO, &cn_file_ops);
 
@@ -483,7 +305,6 @@ static void __devexit cn_fini(void)
 
        proc_net_remove(&init_net, "connector");
 
-       cn_del_callback(&dev->id);
        cn_queue_free_dev(dev->cbdev);
        netlink_kernel_release(dev->nls);
 }