[CONNECTOR]: Fix warning in cn_queue.c
[linux-2.6.git] / drivers / connector / connector.c
index aaf6d46..3589707 100644 (file)
@@ -26,6 +26,7 @@
 #include <linux/netlink.h>
 #include <linux/moduleparam.h>
 #include <linux/connector.h>
+#include <linux/mutex.h>
 
 #include <net/sock.h>
 
@@ -41,7 +42,7 @@ 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 DECLARE_MUTEX(notify_lock);
+static DEFINE_MUTEX(notify_lock);
 static LIST_HEAD(notify_list);
 
 static struct cn_dev cdev;
@@ -69,7 +70,7 @@ int cn_already_initialized = 0;
  * a new message.
  *
  */
-int cn_netlink_send(struct cn_msg *msg, u32 __group, int gfp_mask)
+int cn_netlink_send(struct cn_msg *msg, u32 __group, gfp_t gfp_mask)
 {
        struct cn_callback_entry *__cbq;
        unsigned int size;
@@ -84,7 +85,7 @@ int cn_netlink_send(struct cn_msg *msg, u32 __group, int gfp_mask)
                spin_lock_bh(&dev->cbdev->queue_lock);
                list_for_each_entry(__cbq, &dev->cbdev->queue_list,
                                    callback_entry) {
-                       if (cn_cb_equal(&__cbq->cb->id, &msg->id)) {
+                       if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
                                found = 1;
                                group = __cbq->group;
                        }
@@ -97,6 +98,9 @@ int cn_netlink_send(struct cn_msg *msg, u32 __group, int gfp_mask)
                group = __group;
        }
 
+       if (!netlink_has_listeners(dev->nls, group))
+               return -ESRCH;
+
        size = NLMSG_SPACE(sizeof(*msg) + msg->len);
 
        skb = alloc_skb(size, gfp_mask);
@@ -111,9 +115,7 @@ int cn_netlink_send(struct cn_msg *msg, u32 __group, int gfp_mask)
 
        NETLINK_CB(skb).dst_group = group;
 
-       netlink_broadcast(dev->nls, skb, 0, group, gfp_mask);
-
-       return 0;
+       return netlink_broadcast(dev->nls, skb, 0, group, gfp_mask);
 
 nlmsg_failure:
        kfree_skb(skb);
@@ -127,42 +129,56 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v
 {
        struct cn_callback_entry *__cbq;
        struct cn_dev *dev = &cdev;
-       int found = 0;
+       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->cb->id, &msg->id)) {
-                       /*
-                        * Let's scream if there is some magic and the
-                        * data will arrive asynchronously here.
-                        * [i.e. netlink messages will be queued].
-                        * After the first warning I will fix it
-                        * quickly, but now I think it is
-                        * impossible. --zbr (2004_04_27).
-                        */
+               if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
                        if (likely(!test_bit(0, &__cbq->work.pending) &&
-                                       __cbq->ddata == NULL)) {
-                               __cbq->cb->priv = msg;
+                                       __cbq->data.ddata == NULL)) {
+                               __cbq->data.callback_priv = msg;
 
-                               __cbq->ddata = data;
-                               __cbq->destruct_data = destruct_data;
+                               __cbq->data.ddata = data;
+                               __cbq->data.destruct_data = destruct_data;
 
                                if (queue_work(dev->cbdev->cn_queue,
                                                &__cbq->work))
-                                       found = 1;
+                                       err = 0;
                        } else {
-                               printk("%s: cbq->data=%p, "
-                                      "work->pending=%08lx.\n",
-                                      __func__, __cbq->ddata,
-                                      __cbq->work.pending);
-                               WARN_ON(1);
+                               struct work_struct *w;
+                               struct cn_callback_data *d;
+                               
+                               w = kzalloc(sizeof(*w) + sizeof(*d), GFP_ATOMIC);
+                               if (w) {
+                                       d = (struct cn_callback_data *)(w+1);
+
+                                       d->callback_priv = msg;
+                                       d->callback = __cbq->data.callback;
+                                       d->ddata = data;
+                                       d->destruct_data = destruct_data;
+                                       d->free = w;
+
+                                       INIT_LIST_HEAD(&w->entry);
+                                       w->pending = 0;
+                                       w->func = &cn_queue_wrapper;
+                                       w->data = d;
+                                       init_timer(&w->timer);
+                                       
+                                       if (queue_work(dev->cbdev->cn_queue, w))
+                                               err = 0;
+                                       else {
+                                               kfree(w);
+                                               err = -EINVAL;
+                                       }
+                               } else
+                                       err = -ENOMEM;
                        }
                        break;
                }
        }
        spin_unlock_bh(&dev->cbdev->queue_lock);
 
-       return found ? 0 : -ENODEV;
+       return err;
 }
 
 /*
@@ -245,7 +261,7 @@ static void cn_notify(struct cb_id *id, u32 notify_event)
 {
        struct cn_ctl_entry *ent;
 
-       down(&notify_lock);
+       mutex_lock(&notify_lock);
        list_for_each_entry(ent, &notify_list, notify_entry) {
                int i;
                struct cn_notify_req *req;
@@ -278,7 +294,7 @@ static void cn_notify(struct cb_id *id, u32 notify_event)
                        cn_netlink_send(&m, ctl->group, GFP_KERNEL);
                }
        }
-       up(&notify_lock);
+       mutex_unlock(&notify_lock);
 }
 
 /*
@@ -291,22 +307,10 @@ int cn_add_callback(struct cb_id *id, char *name, void (*callback)(void *))
 {
        int err;
        struct cn_dev *dev = &cdev;
-       struct cn_callback *cb;
-
-       cb = kzalloc(sizeof(*cb), GFP_KERNEL);
-       if (!cb)
-               return -ENOMEM;
-
-       scnprintf(cb->name, sizeof(cb->name), "%s", name);
-
-       memcpy(&cb->id, id, sizeof(cb->id));
-       cb->callback = callback;
 
-       err = cn_queue_add_callback(dev->cbdev, cb);
-       if (err) {
-               kfree(cb);
+       err = cn_queue_add_callback(dev->cbdev, name, id, callback);
+       if (err)
                return err;
-       }
 
        cn_notify(id, 0);
 
@@ -404,14 +408,14 @@ static void cn_callback(void *data)
        if (ctl->group == 0) {
                struct cn_ctl_entry *n;
 
-               down(&notify_lock);
+               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);
                        }
                }
-               up(&notify_lock);
+               mutex_unlock(&notify_lock);
 
                return;
        }
@@ -426,9 +430,9 @@ static void cn_callback(void *data)
 
        memcpy(ent->msg, ctl, size - sizeof(*ent));
 
-       down(&notify_lock);
+       mutex_lock(&notify_lock);
        list_add(&ent->notify_entry, &notify_list);
-       up(&notify_lock);
+       mutex_unlock(&notify_lock);
 }
 
 static int __init cn_init(void)