
From: Dominik Brodowski <linux@dominikbrodowski.de>

01-new_ds_registration.diff

Add a new registration function to register the PCMCIA 16-bit subsystem (ds
a.k.a.  pcmcia) with the PCMICA core (cs a.k.a.  pcmcia_core).

As send_event is only called with skt->sem held, we can use that to
safeguard skt->callback(), too.  Note that the class_device_register() call
by pccardd() is done _before_ skt->sem() is held, and the
pcmcia_socket_register() doesn't hold skt->sem() as well, so there is no
chance for a deadlock.

02-send_event.diff

Call the PCMCIA 16-bit device drivers from ds.c instead of cs.c.  Also,
remove the delayed handling of CS_REMOVAL events, but keep the ordering the
same as it used to be due to the delay.

03-unify_bind_mtd.diff

Unify the pcmcia_bind_mtd and bind_mtd functions.

04-unify_bind_device.diff

Unify bind_device and pcmcia_bind_device.  Also, change bind_device so that
it conforms to CodingStyle.

05-license.diff

As discussed previously, my integration of ds.c with the driver model can
and will only be available under the GPL, as it's too much derived of other
buses' implementation of integration with the driver model.

06-pcmcia_device.diff

Add pcmcia_device(s).

07-remove_bind_t.diff

Remove struct socket_bind_t by moving "dev_link_t *instance" to struct
pcmcia_device, and transforming all users of socket_bind_t to use struct
pcmcia_device instead.  Also, CodingStyle updates for get_device_info and
unbind_request.

08-remove_use_count

Remove the internal driver use_count in ds.c, as the reference counting is
done in the module core anyways, and that reference count is available for
cardmgr's usage by a call to module_refcount.  And if
!CONFIG_MODULE_UNLOAD, rmmod is useless anyways, so avoid that call by
cardmgr at all.

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

 25-akpm/drivers/pcmcia/cs.c          |   70 ++-
 25-akpm/drivers/pcmcia/cs_internal.h |    8 
 25-akpm/drivers/pcmcia/ds.c          |  664 +++++++++++++++++------------------
 25-akpm/include/pcmcia/cs.h          |   16 
 25-akpm/include/pcmcia/ds.h          |   48 +-
 25-akpm/include/pcmcia/ss.h          |    2 
 6 files changed, 420 insertions(+), 388 deletions(-)

diff -puN drivers/pcmcia/cs.c~pcmcia-17-device-model-integration drivers/pcmcia/cs.c
--- 25/drivers/pcmcia/cs.c~pcmcia-17-device-model-integration	Mon Nov  8 14:19:44 2004
+++ 25-akpm/drivers/pcmcia/cs.c	Mon Nov  8 14:19:44 2004
@@ -349,8 +349,6 @@ static void free_regions(memory_handle_t
     }
 }
 
-static int send_event(struct pcmcia_socket *s, event_t event, int priority);
-
 static void shutdown_socket(struct pcmcia_socket *s)
 {
     client_t **c;
@@ -402,26 +400,30 @@ static void shutdown_socket(struct pcmci
     
 ======================================================================*/
 
+
+/* NOTE: send_event needs to be called with skt->sem held. */
+
 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
 {
-    client_t *client = s->clients;
-    int ret;
-    cs_dbg(s, 1, "send_event(event %d, pri %d)\n",
-	   event, priority);
-    ret = 0;
-    if (s->state & SOCKET_CARDBUS)
-	    return 0;
-    for (; client; client = client->next) { 
-	if (client->state & (CLIENT_UNBOUND|CLIENT_STALE))
-	    continue;
-	if (client->EventMask & event) {
-	    ret = EVENT(client, event, priority);
-	    if (ret != 0)
-		return ret;
-	}
-    }
-    return ret;
-} /* send_event */
+	int ret;
+
+	if (s->state & SOCKET_CARDBUS)
+		return 0;
+
+	cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
+	   event, priority, s->callback);
+
+	if (!s->callback)
+		return 0;
+	if (!try_module_get(s->callback->owner))
+		return 0;
+
+	ret = s->callback->event(s, event, priority);
+
+	module_put(s->callback->owner);
+
+	return ret;
+}
 
 static void socket_remove_drivers(struct pcmcia_socket *skt)
 {
@@ -1363,6 +1365,34 @@ int pcmcia_register_client(client_handle
     return CS_OUT_OF_RESOURCE;
 } /* register_client */
 
+/* register pcmcia_callback */
+int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
+{
+        int ret = 0;
+
+	/* s->skt_sem also protects s->callback */
+	down(&s->skt_sem);
+
+	if (c) {
+		/* registration */
+		if (s->callback) {
+			ret = -EBUSY;
+			goto err;
+		}
+
+		s->callback = c;
+
+		if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
+			send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
+	} else
+		s->callback = NULL;
+ err:
+	up(&s->skt_sem);
+
+	return ret;
+}
+EXPORT_SYMBOL(pccard_register_pcmcia);
+
 /*====================================================================*/
 
 int pcmcia_release_configuration(client_handle_t handle)
diff -puN drivers/pcmcia/cs_internal.h~pcmcia-17-device-model-integration drivers/pcmcia/cs_internal.h
--- 25/drivers/pcmcia/cs_internal.h~pcmcia-17-device-model-integration	Mon Nov  8 14:19:44 2004
+++ 25-akpm/drivers/pcmcia/cs_internal.h	Mon Nov  8 14:19:44 2004
@@ -173,6 +173,14 @@ int pccard_reset_card(struct pcmcia_sock
 int pccard_get_status(struct pcmcia_socket *s, unsigned int function, cs_status_t *status);
 int pccard_access_configuration_register(struct pcmcia_socket *s, unsigned int function, conf_reg_t *reg);
 
+
+struct pcmcia_callback{
+	struct module	*owner;
+	int		(*event) (struct pcmcia_socket *s, event_t event, int priority);
+};
+
+int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c);
+
 #define cs_socket_name(skt)	((skt)->dev.class_id)
 
 #ifdef DEBUG
diff -puN drivers/pcmcia/ds.c~pcmcia-17-device-model-integration drivers/pcmcia/ds.c
--- 25/drivers/pcmcia/ds.c~pcmcia-17-device-model-integration	Mon Nov  8 14:19:44 2004
+++ 25-akpm/drivers/pcmcia/ds.c	Mon Nov  8 14:19:44 2004
@@ -1,35 +1,17 @@
-/*======================================================================
-
-    PC Card Driver Services
-    
-    ds.c 1.112 2001/10/13 00:08:28
-    
-    The contents of this file are subject to the Mozilla Public
-    License Version 1.1 (the "License"); you may not use this file
-    except in compliance with the License. You may obtain a copy of
-    the License at http://www.mozilla.org/MPL/
-
-    Software distributed under the License is distributed on an "AS
-    IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
-    implied. See the License for the specific language governing
-    rights and limitations under the License.
-
-    The initial developer of the original code is David A. Hinds
-    <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
-    are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
-
-    Alternatively, the contents of this file may be used under the
-    terms of the GNU General Public License version 2 (the "GPL"), in
-    which case the provisions of the GPL are applicable instead of the
-    above.  If you wish to allow the use of your version of this file
-    only under the terms of the GPL and not to allow others to use
-    your version of this file under the MPL, indicate your decision
-    by deleting the provisions above and replace them with the notice
-    and other provisions required by the GPL.  If you do not delete
-    the provisions above, a recipient may use your version of this
-    file under either the MPL or the GPL.
-    
-======================================================================*/
+/*
+ * ds.c -- 16-bit PCMCIA core support
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * The initial developer of the original code is David A. Hinds
+ * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
+ * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
+ *
+ * (C) 1999		David A. Hinds
+ * (C) 2003 - 2004	Dominik Brodowski
+ */
 
 #include <linux/config.h>
 #include <linux/module.h>
@@ -72,7 +54,7 @@
 
 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
 MODULE_DESCRIPTION("PCMCIA Driver Services");
-MODULE_LICENSE("Dual MPL/GPL");
+MODULE_LICENSE("GPL");
 
 #ifdef DEBUG
 int ds_pc_debug;
@@ -89,13 +71,6 @@ module_param_named(pc_debug, ds_pc_debug
 
 /*====================================================================*/
 
-typedef struct socket_bind_t {
-    struct pcmcia_driver	*driver;
-    u_char		function;
-    dev_link_t		*instance;
-    struct socket_bind_t *next;
-} socket_bind_t;
-
 /* Device user information */
 #define MAX_EVENTS	32
 #define USER_MAGIC	0x7ea4
@@ -112,15 +87,18 @@ typedef struct user_info_t {
 /* Socket state information */
 struct pcmcia_bus_socket {
 	atomic_t		refcount;
-	client_handle_t		handle;
+	struct pcmcia_callback	callback;
 	int			state;
 	user_info_t		*user;
 	int			req_pending, req_result;
 	wait_queue_head_t	queue, request;
-	struct work_struct	removal;
-	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 = SPIN_LOCK_UNLOCKED;
 
 #define DS_SOCKET_PRESENT		0x01
 #define DS_SOCKET_BUSY			0x02
@@ -129,9 +107,6 @@ struct pcmcia_bus_socket {
 
 /*====================================================================*/
 
-/* Device driver ID passed to Card Services */
-static dev_info_t dev_info = "Driver Services";
-
 static int major_dev = -1;
 
 static struct proc_dir_entry *proc_pccard;
@@ -140,80 +115,6 @@ static struct proc_dir_entry *proc_pccar
 
 /* code which was in cs.c before */
 
-/*======================================================================
-
-    Bind_device() associates a device driver with a particular socket.
-    It is normally called by Driver Services after it has identified
-    a newly inserted card.  An instance of that driver will then be
-    eligible to register as a client of this socket.
-    
-======================================================================*/
-
-static int pcmcia_bind_device(bind_req_t *req)
-{
-	client_t *client;
-	struct pcmcia_socket *s;
-
-	s = req->Socket;
-	if (!s)
-		return CS_BAD_SOCKET;
-
-	client = (client_t *) kmalloc(sizeof(client_t), GFP_KERNEL);
-	if (!client) 
-		return CS_OUT_OF_RESOURCE;
-	memset(client, '\0', sizeof(client_t));
-	client->client_magic = CLIENT_MAGIC;
-	strlcpy(client->dev_info, (char *)req->dev_info, DEV_NAME_LEN);
-	client->Socket = s;
-	client->Function = req->Function;
-	client->state = CLIENT_UNBOUND;
-	client->next = s->clients;
-	s->clients = client;
-	ds_dbg(1, "%s: bind_device(): client 0x%p, dev %s\n",
-		cs_socket_name(client->Socket), client, client->dev_info);
-	return CS_SUCCESS;
-} /* bind_device */
-
-
-/*======================================================================
-
-    Bind_mtd() associates a device driver with a particular memory
-    region.  It is normally called by Driver Services after it has
-    identified a memory device type.  An instance of the corresponding
-    driver will then be able to register to control this region.
-    
-======================================================================*/
-
-static int pcmcia_bind_mtd(mtd_bind_t *req)
-{
-	struct pcmcia_socket *s;
-	memory_handle_t region;
-
-	s = req->Socket;
-	if (!s)
-		return CS_BAD_SOCKET;
-    
-	if (req->Attributes & REGION_TYPE_AM)
-		region = s->a_region;
-	else
-		region = s->c_region;
-
-	while (region) {
-		if (region->info.CardOffset == req->CardOffset) 
-			break;
-		region = region->info.next;
-	}
-	if (!region || (region->mtd != NULL))
-		return CS_BAD_OFFSET;
-	strlcpy(region->dev_info, (char *)req->dev_info, DEV_NAME_LEN);
-
-	ds_dbg(1, "%s: bind_mtd: attr 0x%x, offset 0x%x, dev %s\n",
-	      cs_socket_name(s), req->Attributes, req->CardOffset,
-	      (char *)req->dev_info);
-	return CS_SUCCESS;
-} /* bind_mtd */
-
-
 /* String tables for error messages */
 
 typedef struct lookup_t {
@@ -383,7 +284,6 @@ int pcmcia_register_driver(struct pcmcia
 	if (!driver)
 		return -EINVAL;
 
- 	driver->use_count = 0;
 	driver->drv.bus = &pcmcia_bus_type;
 
 	return driver_register(&driver->drv);
@@ -405,10 +305,16 @@ static struct proc_dir_entry *proc_pccar
 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
 {
 	char **p = d;
-	struct pcmcia_driver *p_dev = container_of(driver, 
+	struct pcmcia_driver *p_drv = container_of(driver,
 						   struct pcmcia_driver, drv);
 
-	*p += sprintf(*p, "%-24.24s 1 %d\n", driver->name, p_dev->use_count);
+	*p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
+#ifdef CONFIG_MODULE_UNLOAD
+		      module_refcount(p_drv->owner)
+#else
+		      1
+#endif
+	);
 	d = (void *) p;
 
 	return 0;
@@ -426,6 +332,25 @@ static int proc_read_drivers(char *buf, 
 }
 #endif
 
+/* pcmcia_device handling */
+
+static inline struct pcmcia_device *get_p_dev(struct pcmcia_device *p_dev)
+{	/* FIXME: this is unsafe */
+	return to_pcmcia_dev(get_device(&p_dev->dev));
+}
+
+static inline void put_p_dev(struct pcmcia_device *p_dev)
+{
+	put_device(&p_dev->dev);
+}
+
+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
@@ -473,50 +398,68 @@ static int handle_request(struct pcmcia_
     return CS_SUCCESS;
 }
 
-static void handle_removal(void *data)
-{
-    struct pcmcia_bus_socket *s = data;
-    handle_event(s, CS_EVENT_CARD_REMOVAL);
-    s->state &= ~DS_SOCKET_REMOVAL_PENDING;
-}
-
 /*======================================================================
 
     The card status event handler.
     
 ======================================================================*/
 
-static int ds_event(event_t event, int priority,
-		    event_callback_args_t *args)
+static int send_event(struct pcmcia_socket *s, event_t event, int priority)
 {
-    struct pcmcia_bus_socket *s;
+	int ret = 0;
+	client_t *client;
 
-    ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
-	  event, priority, args->client_handle);
-    s = args->client_data;
-    
-    switch (event) {
-	
-    case CS_EVENT_CARD_REMOVAL:
-	s->state &= ~DS_SOCKET_PRESENT;
-	if (!(s->state & DS_SOCKET_REMOVAL_PENDING)) {
-		s->state |= DS_SOCKET_REMOVAL_PENDING;
-		schedule_delayed_work(&s->removal,  HZ/10);
+	for (client = s->clients; client; client = client->next) {
+		if (client->state & (CLIENT_UNBOUND|CLIENT_STALE))
+			continue;
+		if (client->EventMask & event) {
+			ret = EVENT(client, event, priority);
+			if (ret != 0)
+				return ret;
+		}
 	}
-	break;
-	
-    case CS_EVENT_CARD_INSERTION:
-	s->state |= DS_SOCKET_PRESENT;
-	handle_event(s, event);
-	break;
+	return ret;
+} /* send_event */
 
-    case CS_EVENT_EJECTION_REQUEST:
-	return handle_request(s, event);
-	break;
+
+/* Normally, the event is passed to individual drivers after
+ * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
+ * is inversed to maintain historic compatibility.
+ */
+
+static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
+{
+	struct pcmcia_bus_socket *s = skt->pcmcia;
+	int ret = 0;
+
+	ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
+	       event, priority, s);
+
+	switch (event) {
+
+	case CS_EVENT_CARD_REMOVAL:
+		s->state &= ~DS_SOCKET_PRESENT;
+	    	send_event(skt, event, priority);
+		handle_event(s, event);
+		break;
 	
-    default:
-	handle_event(s, event);
-	break;
+	case CS_EVENT_CARD_INSERTION:
+		s->state |= DS_SOCKET_PRESENT;
+		handle_event(s, event);
+		send_event(skt, event, priority);
+		break;
+
+	case CS_EVENT_EJECTION_REQUEST:
+		ret = handle_request(s, event);
+		if (ret)
+			break;
+		ret = send_event(skt, event, priority);
+		break;
+
+	default:
+		handle_event(s, event);
+		send_event(skt, event, priority);
+		break;
     }
 
     return 0;
@@ -530,99 +473,146 @@ static int ds_event(event_t event, int p
 
 static int bind_mtd(struct pcmcia_bus_socket *bus_sock, mtd_info_t *mtd_info)
 {
-    mtd_bind_t bind_req;
-    int ret;
+	struct pcmcia_socket *s = bus_sock->parent;
+	memory_handle_t region;
 
-    bind_req.dev_info = &mtd_info->dev_info;
-    bind_req.Attributes = mtd_info->Attributes;
-    bind_req.Socket = bus_sock->parent;
-    bind_req.CardOffset = mtd_info->CardOffset;
-    ret = pcmcia_bind_mtd(&bind_req);
-    if (ret != CS_SUCCESS) {
-	cs_error(NULL, BindMTD, ret);
-	printk(KERN_NOTICE "ds: unable to bind MTD '%s' to socket %d"
-	       " offset 0x%x\n",
-	       (char *)bind_req.dev_info, bus_sock->parent->sock, bind_req.CardOffset);
-	return -ENODEV;
-    }
-    return 0;
+	if (mtd_info->Attributes & REGION_TYPE_AM)
+		region = s->a_region;
+	else
+		region = s->c_region;
+
+	while (region) {
+		if (region->info.CardOffset == mtd_info->CardOffset)
+			break;
+		region = region->info.next;
+	}
+	if (!region || (region->mtd != NULL))
+		return -ENODEV;
+
+	strlcpy(region->dev_info, mtd_info->dev_info, DEV_NAME_LEN);
+
+	return 0;
 } /* bind_mtd */
 
 /*======================================================================
 
+    bind_request() and bind_device() are merged by now. Individual
+    descriptions:
+
     bind_request() connects a socket to a particular client driver.
     It looks up the specified device ID in the list of registered
     drivers, binds it to the socket, and tries to create an instance
     of the device.  unbind_request() deletes a driver instance.
     
+    Bind_device() associates a device driver with a particular socket.
+    It is normally called by Driver Services after it has identified
+    a newly inserted card.  An instance of that driver will then be
+    eligible to register as a client of this socket.
+
 ======================================================================*/
 
 static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
 {
-    struct pcmcia_driver *driver;
-    socket_bind_t *b;
-    bind_req_t bind_req;
-    int ret;
+	struct pcmcia_driver *p_drv;
+	struct pcmcia_device *p_dev, *tmp_dev;
+	client_t *client;
+	unsigned long flags;
+	int ret = 0;
 
-    if (!s)
-	    return -EINVAL;
+	if (!s)
+		return -EINVAL;
 
-    ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
-	  (char *)bind_info->dev_info);
-    driver = get_pcmcia_driver(&bind_info->dev_info);
-    if (!driver)
-	    return -EINVAL;
-
-    for (b = s->bind; b; b = b->next)
-	if ((driver == b->driver) &&
-	    (bind_info->function == b->function))
-	    break;
-    if (b != NULL) {
-	bind_info->instance = b->instance;
-	return -EBUSY;
-    }
+	ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
+	       (char *)bind_info->dev_info);
 
-    if (!try_module_get(driver->owner))
-	    return -EINVAL;
+	p_drv = get_pcmcia_driver(&bind_info->dev_info);
+	if (!p_drv)
+		return -EINVAL;
 
-    bind_req.Socket = s->parent;
-    bind_req.Function = bind_info->function;
-    bind_req.dev_info = (dev_info_t *) driver->drv.name;
-    ret = pcmcia_bind_device(&bind_req);
-    if (ret != CS_SUCCESS) {
-	cs_error(NULL, BindDevice, ret);
-	printk(KERN_NOTICE "ds: unable to bind '%s' to socket %d\n",
-	       (char *)dev_info, s->parent->sock);
-	module_put(driver->owner);
-	return -ENODEV;
-    }
+	if (!try_module_get(p_drv->owner))
+		return -EINVAL;
 
-    /* Add binding to list for this socket */
-    driver->use_count++;
-    b = kmalloc(sizeof(socket_bind_t), GFP_KERNEL);
-    if (!b) 
-    {
-    	driver->use_count--;
-	module_put(driver->owner);
-	return -ENOMEM;    
-    }
-    b->driver = driver;
-    b->function = bind_info->function;
-    b->instance = NULL;
-    b->next = s->bind;
-    s->bind = b;
-    
-    if (driver->attach) {
-	b->instance = driver->attach();
-	if (b->instance == NULL) {
-	    printk(KERN_NOTICE "ds: unable to create instance "
-		   "of '%s'!\n", (char *)bind_info->dev_info);
-	    module_put(driver->owner);
-	    return -ENODEV;
+	client = (client_t *) kmalloc(sizeof(client_t), GFP_KERNEL);
+	if (!client) {
+		ret = -ENOMEM;
+		goto err_put;
+	}
+	memset(client, 0, sizeof(client_t));
+
+	client->client_magic = CLIENT_MAGIC;
+	client->Socket = s->parent;
+	client->Function = bind_info->function;
+	client->state = CLIENT_UNBOUND;
+	client->next = s->parent->clients;
+	strlcpy(client->dev_info, p_drv->drv.name, DEV_NAME_LEN);
+
+	/* 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) {
+		ret = -ENOMEM;
+		goto err_free_client;
+	}
+	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 = &p_drv->drv;
+
+	ret = device_register(&p_dev->dev);
+	if (ret) {
+		kfree(p_dev);
+		goto err_free_client;
+	}
+
+	/*
+	 * Add to the list in pcmcia_bus_socket. Also, assert that no
+	 * such device exists at the moment.
+	 */
+
+	spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
+	list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list) {
+		if (tmp_dev->func == bind_info->function) {
+			spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
+			bind_info->instance = tmp_dev->instance;
+			ret = -EBUSY;
+			goto err_unregister;
+		}
+	}
+	list_add_tail(&p_dev->socket_device_list, &s->devices_list);
+	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
+
+	/* finally here the parent client is registered */
+	s->parent->clients = client;
+
+	if (p_drv->attach) {
+		p_dev->instance = p_drv->attach();
+		if (!p_dev->instance) {
+			printk(KERN_NOTICE "ds: unable to create instance "
+			       "of '%s'!\n", (char *)bind_info->dev_info);
+			/* FIXME: client isn't freed here */
+			ret = -ENODEV;
+			goto err_unregister;
+		}
 	}
-    }
     
-    return 0;
+	return 0;
+
+ err_unregister:
+	device_unregister(&p_dev->dev);
+ err_free_client:
+	kfree(client);
+ err_put:
+	module_put(p_drv->owner);
+	return (ret);
 } /* bind_request */
 
 /*====================================================================*/
@@ -631,95 +621,124 @@ extern struct pci_bus *pcmcia_lookup_bus
 
 static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
 {
-    socket_bind_t *b;
-    dev_node_t *node;
+	dev_node_t *node;
+	struct pcmcia_device *p_dev;
+	unsigned long flags;
+	int ret = 0;
 
 #ifdef CONFIG_CARDBUS
-    /*
-     * Some unbelievably ugly code to associate the PCI cardbus
-     * device and its driver with the PCMCIA "bind" information.
-     */
-    {
-	struct pci_bus *bus;
+	/*
+	 * Some unbelievably ugly code to associate the PCI cardbus
+	 * device and its driver with the PCMCIA "bind" information.
+	 */
+	{
+		struct pci_bus *bus;
 
-	bus = pcmcia_lookup_bus(s->parent);
-	if (bus) {
-	    	struct list_head *list;
-		struct pci_dev *dev = NULL;
-		
-		list = bus->devices.next;
-		while (list != &bus->devices) {
-			struct pci_dev *pdev = pci_dev_b(list);
-			list = list->next;
+		bus = pcmcia_lookup_bus(s->parent);
+		if (bus) {
+			struct list_head *list;
+			struct pci_dev *dev = NULL;
+
+			list = bus->devices.next;
+			while (list != &bus->devices) {
+				struct pci_dev *pdev = pci_dev_b(list);
+				list = list->next;
+
+				if (first) {
+					dev = pdev;
+					break;
+				}
 
-			if (first) {
-				dev = pdev;
-				break;
+				/* Try to handle "next" here some way? */
+			}
+			if (dev && dev->driver) {
+				strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
+				bind_info->major = 0;
+				bind_info->minor = 0;
+				bind_info->next = NULL;
+				return 0;
 			}
-
-			/* Try to handle "next" here some way? */
-		}
-		if (dev && dev->driver) {
-			strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
-			bind_info->major = 0;
-			bind_info->minor = 0;
-			bind_info->next = NULL;
-			return 0;
 		}
 	}
-    }
 #endif
 
-    for (b = s->bind; b; b = b->next)
-	if ((strcmp((char *)b->driver->drv.name,
-		    (char *)bind_info->dev_info) == 0) &&
-	    (b->function == bind_info->function))
-	    break;
-    if (b == NULL) return -ENODEV;
-    if ((b->instance == NULL) ||
-	(b->instance->state & DEV_CONFIG_PENDING))
-	return -EAGAIN;
-    if (first)
-	node = b->instance->dev;
-    else
-	for (node = b->instance->dev; node; node = node->next)
-	    if (node == bind_info->next) break;
-    if (node == NULL) return -ENODEV;
-
-    strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
-    bind_info->major = node->major;
-    bind_info->minor = node->minor;
-    bind_info->next = node->next;
-    
-    return 0;
+	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) {
+			p_dev = get_p_dev(p_dev);
+			if (!p_dev)
+				continue;
+			goto found;
+		}
+	}
+	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
+	return -ENODEV;
+
+ found:
+	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
+
+	if ((!p_dev->instance) ||
+	    (p_dev->instance->state & DEV_CONFIG_PENDING)) {
+		ret = -EAGAIN;
+		goto err_put;
+	}
+
+	if (first)
+		node = p_dev->instance->dev;
+	else
+		for (node = p_dev->instance->dev; node; node = node->next)
+			if (node == bind_info->next)
+				break;
+	if (!node) {
+		ret = -ENODEV;
+		goto err_put;
+	}
+
+	strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
+	bind_info->major = node->major;
+	bind_info->minor = node->minor;
+	bind_info->next = node->next;
+
+ err_put:
+	put_p_dev(p_dev);
+	return (ret);
 } /* get_device_info */
 
 /*====================================================================*/
 
 static int unbind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
 {
-    socket_bind_t **b, *c;
+	struct pcmcia_device	*p_dev;
+	struct pcmcia_driver	*p_drv;
+	unsigned long		flags;
+
+	ds_dbg(2, "unbind_request(%d, '%s')\n", s->parent->sock,
+	       (char *)bind_info->dev_info);
+
+	/* 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;
 
-    ds_dbg(2, "unbind_request(%d, '%s')\n", s->parent->sock,
-	  (char *)bind_info->dev_info);
-    for (b = &s->bind; *b; b = &(*b)->next)
-	if ((strcmp((char *)(*b)->driver->drv.name,
-		    (char *)bind_info->dev_info) == 0) &&
-	    ((*b)->function == bind_info->function))
-	    break;
-    if (*b == NULL)
-	return -ENODEV;
-    
-    c = *b;
-    c->driver->use_count--;
-    if (c->driver->detach) {
-	if (c->instance)
-	    c->driver->detach(c->instance);
-    }
-    module_put(c->driver->owner);
-    *b = c->next;
-    kfree(c);
-    return 0;
+ found:
+	list_del(&p_dev->socket_device_list);
+	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
+
+	/* detach the "instance" */
+	p_drv = to_pcmcia_drv(p_dev->dev.driver);
+	if (p_drv) {
+		module_put(p_drv->owner);
+		if ((p_drv->detach) && (p_dev->instance))
+			p_drv->detach(p_dev->instance);
+	}
+
+	device_unregister(&p_dev->dev);
+
+	return 0;
 } /* unbind_request */
 
 /*======================================================================
@@ -1084,8 +1103,6 @@ static struct file_operations ds_fops = 
 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
 {
 	struct pcmcia_socket *socket = class_dev->class_data;
-	client_reg_t client_reg;
-	bind_req_t bind;
 	struct pcmcia_bus_socket *s;
 	int ret;
 
@@ -1103,41 +1120,24 @@ 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 */
-	INIT_WORK(&s->removal, handle_removal, s);
 	s->parent = socket;
 
 	/* Set up hotline to Card Services */
-	client_reg.dev_info = bind.dev_info = &dev_info;
-
-	bind.Socket = socket;
-	bind.Function = BIND_FN_ALL;
-	ret = pcmcia_bind_device(&bind);
-	if (ret != CS_SUCCESS) {
-		cs_error(NULL, BindDevice, ret);
-		kfree(s);
-		return -EINVAL;
-	}
+	s->callback.owner = THIS_MODULE;
+	s->callback.event = &ds_event;
+	socket->pcmcia = s;
 
-	client_reg.Attributes = INFO_MASTER_CLIENT;
-	client_reg.EventMask =
-		CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
-		CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
-		CS_EVENT_EJECTION_REQUEST | CS_EVENT_INSERTION_REQUEST |
-		CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
-	client_reg.event_handler = &ds_event;
-	client_reg.Version = 0x0210;
-	client_reg.event_callback_args.client_data = s;
-	ret = pcmcia_register_client(&s->handle, &client_reg);
-	if (ret != CS_SUCCESS) {
-		cs_error(NULL, RegisterClient, ret);
-		kfree(s);
-		return -EINVAL;
+	ret = pccard_register_pcmcia(socket, &s->callback);
+	if (ret) {
+		printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
+		pcmcia_put_bus_socket(s);
+		socket->pcmcia = NULL;
+		return (ret);
 	}
 
-	socket->pcmcia = s;
-
 	return 0;
 }
 
@@ -1149,9 +1149,7 @@ static void pcmcia_bus_remove_socket(str
 	if (!socket || !socket->pcmcia)
 		return;
 
-	flush_scheduled_work();
-
-	pcmcia_deregister_client(socket->pcmcia->handle);
+	pccard_register_pcmcia(socket, NULL);
 
 	socket->pcmcia->state |= DS_SOCKET_DEAD;
 	pcmcia_put_bus_socket(socket->pcmcia);
diff -puN include/pcmcia/cs.h~pcmcia-17-device-model-integration include/pcmcia/cs.h
--- 25/include/pcmcia/cs.h~pcmcia-17-device-model-integration	Mon Nov  8 14:19:44 2004
+++ 25-akpm/include/pcmcia/cs.h	Mon Nov  8 14:19:44 2004
@@ -315,23 +315,9 @@ typedef struct error_info_t {
     int		retcode;
 } error_info_t;
 
-/* Special stuff for binding drivers to sockets */
-typedef struct bind_req_t {
-    struct pcmcia_socket	*Socket;
-    u_char	Function;
-    dev_info_t	*dev_info;
-} bind_req_t;
-
 /* Flag to bind to all functions */
 #define BIND_FN_ALL	0xff
 
-typedef struct mtd_bind_t {
-    struct pcmcia_socket	*Socket;
-    u_int	Attributes;
-    u_int	CardOffset;
-    dev_info_t	*dev_info;
-} mtd_bind_t;
-
 /* Events */
 #define CS_EVENT_PRI_LOW		0
 #define CS_EVENT_PRI_HIGH		1
@@ -420,6 +406,8 @@ enum service {
     GetFirstWindow, GetNextWindow, GetMemPage
 };
 
+struct pcmcia_socket;
+
 int pcmcia_access_configuration_register(client_handle_t handle, conf_reg_t *reg);
 int pcmcia_deregister_client(client_handle_t handle);
 int pcmcia_get_configuration_info(client_handle_t handle, config_info_t *config);
diff -puN include/pcmcia/ds.h~pcmcia-17-device-model-integration include/pcmcia/ds.h
--- 25/include/pcmcia/ds.h~pcmcia-17-device-model-integration	Mon Nov  8 14:19:44 2004
+++ 25-akpm/include/pcmcia/ds.h	Mon Nov  8 14:19:44 2004
@@ -1,30 +1,16 @@
 /*
- * ds.h 1.56 2000/06/12 21:55:40
+ * ds.h -- 16-bit PCMCIA core support
  *
- * The contents of this file are subject to the Mozilla Public License
- * Version 1.1 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License
- * at http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and
- * limitations under the License. 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
  *
  * The initial developer of the original code is David A. Hinds
  * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
  * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
  *
- * Alternatively, the contents of this file may be used under the
- * terms of the GNU General Public License version 2 (the "GPL"), in which
- * case the provisions of the GPL are applicable instead of the
- * above.  If you wish to allow the use of your version of this file
- * only under the terms of the GPL and not to allow others to use
- * your version of this file under the MPL, indicate your decision by
- * deleting the provisions above and replace them with the notice and
- * other provisions required by the GPL.  If you do not delete the
- * provisions above, a recipient may use your version of this file
- * under either the MPL or the GPL.
+ * (C) 1999		David A. Hinds
+ * (C) 2003 - 2004	Dominik Brodowski
  */
 
 #ifndef _LINUX_DS_H
@@ -141,10 +127,11 @@ 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 {
-	int			use_count;
 	dev_link_t		*(*attach)(void);
 	void			(*detach)(dev_link_t *);
 	struct module		*owner;
@@ -155,6 +142,25 @@ 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;
+
+	/* deprecated, a cleaned up version will be moved into this
+	   struct soon */
+	dev_link_t		*instance;
+
+	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);
 
diff -puN include/pcmcia/ss.h~pcmcia-17-device-model-integration include/pcmcia/ss.h
--- 25/include/pcmcia/ss.h~pcmcia-17-device-model-integration	Mon Nov  8 14:19:44 2004
+++ 25-akpm/include/pcmcia/ss.h	Mon Nov  8 14:19:44 2004
@@ -159,6 +159,7 @@ typedef struct window_t {
 
 struct config_t;
 struct region_t;
+struct pcmcia_callback;
 
 struct pcmcia_socket {
 	struct module			*owner;
@@ -215,6 +216,7 @@ struct pcmcia_socket {
 
 	/* pcmcia (16-bit) */
 	struct pcmcia_bus_socket	*pcmcia;
+	struct pcmcia_callback		*callback;
 
 	/* cardbus (32-bit) */
 #ifdef CONFIG_CARDBUS
_
