
From: Dominik Brodowski <linux@dominikbrodowski.de>

Add pcmcia_device(s).

Signed-off-by: Dominik Brodowski <linux@brodo.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
---

 25-akpm/drivers/pcmcia/ds.c |   68 ++++++++++++++++++++++++++++++++++++++++++++
 25-akpm/include/pcmcia/ds.h |   17 +++++++++++
 2 files changed, 85 insertions(+)

diff -puN drivers/pcmcia/ds.c~pcmcia-add-pcmcia_devices drivers/pcmcia/ds.c
--- 25/drivers/pcmcia/ds.c~pcmcia-add-pcmcia_devices	2004-12-03 18:44:27.110661752 -0800
+++ 25-akpm/drivers/pcmcia/ds.c	2004-12-03 18:44:27.117660688 -0800
@@ -101,7 +101,12 @@ struct pcmcia_bus_socket {
 	wait_queue_head_t	queue, request;
 	socket_bind_t		*bind;
 	struct pcmcia_socket	*parent;
+
+	/* the PCMCIA devices connected to this socket (normally one, more
+	 * for multifunction devices: */
+	struct list_head	devices_list;
 };
+static spinlock_t pcmcia_dev_list_lock;
 
 #define DS_SOCKET_PRESENT		0x01
 #define DS_SOCKET_BUSY			0x02
@@ -328,6 +333,15 @@ static int proc_read_drivers(char *buf, 
 }
 #endif
 
+/* pcmcia_device handling */
+
+static void pcmcia_release_dev(struct device *dev)
+{
+	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
+	kfree(p_dev);
+}
+
+
 /*======================================================================
 
     These manage a ring buffer of events pending for one user process
@@ -491,8 +505,10 @@ static int bind_mtd(struct pcmcia_bus_so
 static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
 {
 	struct pcmcia_driver *driver;
+	struct pcmcia_device *p_dev;
 	socket_bind_t *b;
 	client_t *client;
+	unsigned long flags;
 
 	if (!s)
 		return -EINVAL;
@@ -543,6 +559,37 @@ static int bind_request(struct pcmcia_bu
 	b->next = s->bind;
 	s->bind = b;
 
+	/* Currently, the userspace pcmcia cardmgr detects pcmcia devices.
+	 * Here this information is translated into a kernel
+	 * struct pcmcia_device.
+	 */
+
+	p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
+	if (!p_dev) {
+		/* FIXME: client isn't freed here */
+		goto no_p_dev;
+	}
+	memset(p_dev, 0, sizeof(struct pcmcia_device));
+
+	p_dev->socket = s->parent;
+	p_dev->func   = bind_info->function;
+
+	p_dev->dev.bus = &pcmcia_bus_type;
+	p_dev->dev.parent = s->parent->dev.dev;
+	p_dev->dev.release = pcmcia_release_dev;
+	sprintf (p_dev->dev.bus_id, "pcmcia%d.%d", p_dev->socket->sock, p_dev->func);
+	p_dev->dev.driver = &driver->drv;
+	if (device_register(&p_dev->dev)) {
+		/* FIXME: client isn't freed here */
+		kfree(p_dev);
+		goto no_p_dev;
+	}
+	spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
+	list_add_tail(&p_dev->socket_device_list, &s->devices_list);
+	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
+
+ no_p_dev:
+
 	driver->use_count++;
 	if (driver->attach) {
 		b->instance = driver->attach();
@@ -632,6 +679,8 @@ static int get_device_info(struct pcmcia
 static int unbind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
 {
     socket_bind_t **b, *c;
+    struct pcmcia_device *p_dev;
+    unsigned long flags;
 
     ds_dbg(2, "unbind_request(%d, '%s')\n", s->parent->sock,
 	  (char *)bind_info->dev_info);
@@ -652,6 +701,22 @@ static int unbind_request(struct pcmcia_
     module_put(c->driver->owner);
     *b = c->next;
     kfree(c);
+
+    /* unregister the pcmcia_device */
+    spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
+    list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
+	    if (p_dev->func == bind_info->function)
+		    goto found;
+    }
+    spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
+    return 0;
+
+ found:
+    list_del(&p_dev->socket_device_list);
+    spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
+
+    device_unregister(&p_dev->dev);
+
     return 0;
 } /* unbind_request */
 
@@ -1034,6 +1099,7 @@ static int __devinit pcmcia_bus_add_sock
 
 	init_waitqueue_head(&s->queue);
 	init_waitqueue_head(&s->request);
+	INIT_LIST_HEAD(&s->devices_list);
 
 	/* initialize data */
 	s->parent = socket;
@@ -1090,6 +1156,8 @@ static int __init init_pcmcia_bus(void)
 {
 	int i;
 
+	spin_lock_init(&pcmcia_dev_list_lock);
+
 	bus_register(&pcmcia_bus_type);
 	class_interface_register(&pcmcia_bus_interface);
 
diff -puN include/pcmcia/ds.h~pcmcia-add-pcmcia_devices include/pcmcia/ds.h
--- 25/include/pcmcia/ds.h~pcmcia-add-pcmcia_devices	2004-12-03 18:44:27.111661600 -0800
+++ 25-akpm/include/pcmcia/ds.h	2004-12-03 18:44:27.118660536 -0800
@@ -127,6 +127,8 @@ typedef struct dev_link_t {
     ((l) && ((l->state & ~DEV_BUSY) == (DEV_CONFIG|DEV_PRESENT)))
 
 
+struct pcmcia_socket;
+
 extern struct bus_type pcmcia_bus_type;
 
 struct pcmcia_driver {
@@ -141,6 +143,21 @@ struct pcmcia_driver {
 int pcmcia_register_driver(struct pcmcia_driver *driver);
 void pcmcia_unregister_driver(struct pcmcia_driver *driver);
 
+struct pcmcia_device {
+	/* the socket and the function no [for multifunction devices]
+	   uniquely define a pcmcia_device */
+	struct pcmcia_socket	*socket;
+	u8			func;
+
+	struct list_head	socket_device_list;
+
+	struct device		dev;
+};
+
+#define to_pcmcia_dev(n) container_of(n, struct pcmcia_device, dev)
+#define to_pcmcia_drv(n) container_of(n, struct pcmcia_driver, drv)
+
+
 /* error reporting */
 void cs_error(client_handle_t handle, int func, int ret);
 
_
