ds.c 36.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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
 */
Linus Torvalds's avatar
Linus Torvalds committed
15 16 17

#include <linux/config.h>
#include <linux/module.h>
18
#include <linux/moduleparam.h>
Linus Torvalds's avatar
Linus Torvalds committed
19 20 21 22 23
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/errno.h>
Linus Torvalds's avatar
Linus Torvalds committed
24
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27 28 29 30 31 32 33
#include <linux/mm.h>
#include <linux/fcntl.h>
#include <linux/sched.h>
#include <linux/smp_lock.h>
#include <linux/timer.h>
#include <linux/ioctl.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <linux/pci.h>
34
#include <linux/list.h>
35
#include <linux/delay.h>
36
#include <linux/kref.h>
37
#include <linux/workqueue.h>
Linus Torvalds's avatar
Linus Torvalds committed
38

39 40
#include <asm/atomic.h>

41
#define IN_CARD_SERVICES
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44 45 46 47
#include <pcmcia/version.h>
#include <pcmcia/cs_types.h>
#include <pcmcia/cs.h>
#include <pcmcia/bulkmem.h>
#include <pcmcia/cistpl.h>
#include <pcmcia/ds.h>
48
#include <pcmcia/ss.h>
Linus Torvalds's avatar
Linus Torvalds committed
49

50 51
#include "cs_internal.h"

Linus Torvalds's avatar
Linus Torvalds committed
52 53 54 55 56
/*====================================================================*/

/* Module parameters */

MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
57
MODULE_DESCRIPTION("PCMCIA Driver Services");
58
MODULE_LICENSE("GPL");
Linus Torvalds's avatar
Linus Torvalds committed
59

60
#ifdef DEBUG
61
int ds_pc_debug;
Linus Torvalds's avatar
Linus Torvalds committed
62

63
module_param_named(pc_debug, ds_pc_debug, int, 0644);
64 65

#define ds_dbg(lvl, fmt, arg...) do {				\
66
	if (ds_pc_debug > (lvl))					\
67 68
		printk(KERN_DEBUG "ds: " fmt , ## arg);		\
} while (0)
Linus Torvalds's avatar
Linus Torvalds committed
69
#else
70
#define ds_dbg(lvl, fmt, arg...) do { } while (0)
Linus Torvalds's avatar
Linus Torvalds committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
#endif

/*====================================================================*/

/* Device user information */
#define MAX_EVENTS	32
#define USER_MAGIC	0x7ea4
#define CHECK_USER(u) \
    (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
typedef struct user_info_t {
    u_int		user_magic;
    int			event_head, event_tail;
    event_t		event[MAX_EVENTS];
    struct user_info_t	*next;
85
    struct pcmcia_bus_socket *socket;
Linus Torvalds's avatar
Linus Torvalds committed
86 87 88
} user_info_t;

/* Socket state information */
89
struct pcmcia_bus_socket {
90
	struct kref		refcount;
91
	struct pcmcia_callback	callback;
92 93 94 95
	int			state;
	user_info_t		*user;
	int			req_pending, req_result;
	wait_queue_head_t	queue, request;
96
	struct pcmcia_socket	*parent;
97 98 99 100 101 102 103

	/* the PCMCIA devices connected to this socket (normally one, more
	 * for multifunction devices: */
	struct list_head	devices_list;
	u8			device_count; /* the number of devices, used
					       * only internally and subject
					       * to incorrectness and change */
104
};
105
static spinlock_t pcmcia_dev_list_lock;
Linus Torvalds's avatar
Linus Torvalds committed
106

107 108 109 110
#define DS_SOCKET_PRESENT		0x01
#define DS_SOCKET_BUSY			0x02
#define DS_SOCKET_REMOVAL_PENDING	0x10
#define DS_SOCKET_DEAD			0x80
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113

/*====================================================================*/

114 115
static int major_dev = -1;

Linus Torvalds's avatar
Linus Torvalds committed
116 117
/*====================================================================*/

118 119
/* code which was in cs.c before */

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/*======================================================================

    Register_client() uses the dev_info_t handle to match the
    caller with a socket.  The driver must have already been bound
    to a socket with bind_device() -- in fact, bind_device()
    allocates the client structure that will be used.

======================================================================*/

int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
{
	client_t *client = NULL;
	struct pcmcia_socket *s;

	/* Look for unbound client with matching dev_info */
	down_read(&pcmcia_socket_list_rwsem);
	list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
		client = s->clients;
		while (client != NULL) {
			if ((strcmp(client->dev_info, (char *)req->dev_info) == 0)
			    && (client->state & CLIENT_UNBOUND)) break;
			client = client->next;
		}
		if (client != NULL) break;
	}
	up_read(&pcmcia_socket_list_rwsem);
	if (client == NULL)
		return CS_OUT_OF_RESOURCE;

	/*
	 * Prevent this racing with a card insertion.
	 */
	down(&s->skt_sem);
	*handle = client;
	client->state &= ~CLIENT_UNBOUND;
	client->Socket = s;
	client->Attributes = req->Attributes;
	client->EventMask = req->EventMask;
	client->event_handler = req->event_handler;
	client->event_callback_args = req->event_callback_args;
	client->event_callback_args.client_handle = client;

	if (s->state & SOCKET_CARDBUS)
		client->state |= CLIENT_CARDBUS;

	if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
	    (client->Function != BIND_FN_ALL)) {
		cistpl_longlink_mfc_t mfc;
		if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
		    == CS_SUCCESS)
			s->functions = mfc.nfn;
		else
			s->functions = 1;
		s->config = kmalloc(sizeof(config_t) * s->functions,
				    GFP_KERNEL);
		if (!s->config)
			goto out_no_resource;
		memset(s->config, 0, sizeof(config_t) * s->functions);
	}

	ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
	       client, client->dev_info);
	if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
		EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);

	if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
		if (client->EventMask & CS_EVENT_CARD_INSERTION)
			EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
		else
			client->PendingEvents |= CS_EVENT_CARD_INSERTION;
	}

	up(&s->skt_sem);
	return CS_SUCCESS;

 out_no_resource:
	up(&s->skt_sem);
	return CS_OUT_OF_RESOURCE;
} /* register_client */
EXPORT_SYMBOL(pcmcia_register_client);

int pcmcia_deregister_client(client_handle_t handle)
{
	client_t **client;
	struct pcmcia_socket *s;
	u_long flags;
	int i;

	if (CHECK_HANDLE(handle))
		return CS_BAD_HANDLE;

	s = SOCKET(handle);
	ds_dbg(1, "deregister_client(%p)\n", handle);

	if (handle->state &
	    (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
		return CS_IN_USE;
	for (i = 0; i < MAX_WIN; i++)
		if (handle->state & CLIENT_WIN_REQ(i))
			return CS_IN_USE;

	if ((handle->state & CLIENT_STALE) ||
	    (handle->Attributes & INFO_MASTER_CLIENT)) {
		spin_lock_irqsave(&s->lock, flags);
		client = &s->clients;
		while ((*client) && ((*client) != handle))
			client = &(*client)->next;
		if (*client == NULL) {
			spin_unlock_irqrestore(&s->lock, flags);
			return CS_BAD_HANDLE;
		}
		*client = handle->next;
		handle->client_magic = 0;
		kfree(handle);
		spin_unlock_irqrestore(&s->lock, flags);
	} else {
		handle->state = CLIENT_UNBOUND;
		handle->event_handler = NULL;
	}

	return CS_SUCCESS;
} /* deregister_client */
EXPORT_SYMBOL(pcmcia_deregister_client);

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/* String tables for error messages */

typedef struct lookup_t {
    int key;
    char *msg;
} lookup_t;

static const lookup_t error_table[] = {
    { CS_SUCCESS,		"Operation succeeded" },
    { CS_BAD_ADAPTER,		"Bad adapter" },
    { CS_BAD_ATTRIBUTE, 	"Bad attribute", },
    { CS_BAD_BASE,		"Bad base address" },
    { CS_BAD_EDC,		"Bad EDC" },
    { CS_BAD_IRQ,		"Bad IRQ" },
    { CS_BAD_OFFSET,		"Bad offset" },
    { CS_BAD_PAGE,		"Bad page number" },
    { CS_READ_FAILURE,		"Read failure" },
    { CS_BAD_SIZE,		"Bad size" },
    { CS_BAD_SOCKET,		"Bad socket" },
    { CS_BAD_TYPE,		"Bad type" },
    { CS_BAD_VCC,		"Bad Vcc" },
    { CS_BAD_VPP,		"Bad Vpp" },
    { CS_BAD_WINDOW,		"Bad window" },
    { CS_WRITE_FAILURE,		"Write failure" },
    { CS_NO_CARD,		"No card present" },
    { CS_UNSUPPORTED_FUNCTION,	"Usupported function" },
    { CS_UNSUPPORTED_MODE,	"Unsupported mode" },
    { CS_BAD_SPEED,		"Bad speed" },
    { CS_BUSY,			"Resource busy" },
    { CS_GENERAL_FAILURE,	"General failure" },
    { CS_WRITE_PROTECTED,	"Write protected" },
    { CS_BAD_ARG_LENGTH,	"Bad argument length" },
    { CS_BAD_ARGS,		"Bad arguments" },
    { CS_CONFIGURATION_LOCKED,	"Configuration locked" },
    { CS_IN_USE,		"Resource in use" },
    { CS_NO_MORE_ITEMS,		"No more items" },
    { CS_OUT_OF_RESOURCE,	"Out of resource" },
    { CS_BAD_HANDLE,		"Bad handle" },
    { CS_BAD_TUPLE,		"Bad CIS tuple" }
};


static const lookup_t service_table[] = {
    { AccessConfigurationRegister,	"AccessConfigurationRegister" },
    { AddSocketServices,		"AddSocketServices" },
    { AdjustResourceInfo,		"AdjustResourceInfo" },
    { CheckEraseQueue,			"CheckEraseQueue" },
    { CloseMemory,			"CloseMemory" },
    { DeregisterClient,			"DeregisterClient" },
    { DeregisterEraseQueue,		"DeregisterEraseQueue" },
    { GetCardServicesInfo,		"GetCardServicesInfo" },
    { GetClientInfo,			"GetClientInfo" },
    { GetConfigurationInfo,		"GetConfigurationInfo" },
    { GetEventMask,			"GetEventMask" },
    { GetFirstClient,			"GetFirstClient" },
    { GetFirstRegion,			"GetFirstRegion" },
    { GetFirstTuple,			"GetFirstTuple" },
    { GetNextClient,			"GetNextClient" },
    { GetNextRegion,			"GetNextRegion" },
    { GetNextTuple,			"GetNextTuple" },
    { GetStatus,			"GetStatus" },
    { GetTupleData,			"GetTupleData" },
    { MapMemPage,			"MapMemPage" },
    { ModifyConfiguration,		"ModifyConfiguration" },
    { ModifyWindow,			"ModifyWindow" },
    { OpenMemory,			"OpenMemory" },
    { ParseTuple,			"ParseTuple" },
    { ReadMemory,			"ReadMemory" },
    { RegisterClient,			"RegisterClient" },
    { RegisterEraseQueue,		"RegisterEraseQueue" },
    { RegisterMTD,			"RegisterMTD" },
    { ReleaseConfiguration,		"ReleaseConfiguration" },
    { ReleaseIO,			"ReleaseIO" },
    { ReleaseIRQ,			"ReleaseIRQ" },
    { ReleaseWindow,			"ReleaseWindow" },
    { RequestConfiguration,		"RequestConfiguration" },
    { RequestIO,			"RequestIO" },
    { RequestIRQ,			"RequestIRQ" },
    { RequestSocketMask,		"RequestSocketMask" },
    { RequestWindow,			"RequestWindow" },
    { ResetCard,			"ResetCard" },
    { SetEventMask,			"SetEventMask" },
    { ValidateCIS,			"ValidateCIS" },
    { WriteMemory,			"WriteMemory" },
    { BindDevice,			"BindDevice" },
    { BindMTD,				"BindMTD" },
    { ReportError,			"ReportError" },
    { SuspendCard,			"SuspendCard" },
    { ResumeCard,			"ResumeCard" },
    { EjectCard,			"EjectCard" },
    { InsertCard,			"InsertCard" },
    { ReplaceCIS,			"ReplaceCIS" }
};


int pcmcia_report_error(client_handle_t handle, error_info_t *err)
{
	int i;
	char *serv;

	if (CHECK_HANDLE(handle))
		printk(KERN_NOTICE);
	else
		printk(KERN_NOTICE "%s: ", handle->dev_info);

	for (i = 0; i < ARRAY_SIZE(service_table); i++)
		if (service_table[i].key == err->func)
			break;
	if (i < ARRAY_SIZE(service_table))
		serv = service_table[i].msg;
	else
		serv = "Unknown service number";

	for (i = 0; i < ARRAY_SIZE(error_table); i++)
		if (error_table[i].key == err->retcode)
			break;
	if (i < ARRAY_SIZE(error_table))
		printk("%s: %s\n", serv, error_table[i].msg);
	else
		printk("%s: Unknown error code %#x\n", serv, err->retcode);

	return CS_SUCCESS;
} /* report_error */
EXPORT_SYMBOL(pcmcia_report_error);

369 370 371 372
/* end of code which was in cs.c before */

/*======================================================================*/

373
void cs_error(client_handle_t handle, int func, int ret)
Linus Torvalds's avatar
Linus Torvalds committed
374
{
375 376
	error_info_t err = { func, ret };
	pcmcia_report_error(handle, &err);
Linus Torvalds's avatar
Linus Torvalds committed
377
}
378
EXPORT_SYMBOL(cs_error);
Linus Torvalds's avatar
Linus Torvalds committed
379

380
/*======================================================================*/
Linus Torvalds's avatar
Linus Torvalds committed
381

382
static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
383
static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
Linus Torvalds's avatar
Linus Torvalds committed
384

385
static void pcmcia_release_bus_socket(struct kref *refcount)
386
{
387
	struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
388
	pcmcia_put_socket(s->parent);
389
	kfree(s);
390 391
}

392
static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
393
{
394 395
	kref_put(&s->refcount, pcmcia_release_bus_socket);
}
396

397 398 399 400
static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
{
	kref_get(&s->refcount);
	return (s);
401 402
}

403 404 405 406 407 408 409 410 411 412 413
/**
 * pcmcia_register_driver - register a PCMCIA driver with the bus core
 *
 * Registers a PCMCIA driver with the PCMCIA bus core.
 */
int pcmcia_register_driver(struct pcmcia_driver *driver)
{
	if (!driver)
		return -EINVAL;

	driver->drv.bus = &pcmcia_bus_type;
414
	driver->drv.owner = driver->owner;
415 416 417 418 419 420 421 422 423 424 425 426 427

	return driver_register(&driver->drv);
}
EXPORT_SYMBOL(pcmcia_register_driver);

/**
 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
 */
void pcmcia_unregister_driver(struct pcmcia_driver *driver)
{
	driver_unregister(&driver->drv);
}
EXPORT_SYMBOL(pcmcia_unregister_driver);
428

Linus Torvalds's avatar
Linus Torvalds committed
429
#ifdef CONFIG_PROC_FS
430 431
static struct proc_dir_entry *proc_pccard = NULL;

432 433 434
static int proc_read_drivers_callback(struct device_driver *driver, void *d)
{
	char **p = d;
435
	struct pcmcia_driver *p_drv = container_of(driver,
436 437
						   struct pcmcia_driver, drv);

438 439 440 441 442 443 444
	*p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
#ifdef CONFIG_MODULE_UNLOAD
		      (p_drv->owner) ? module_refcount(p_drv->owner) : 1
#else
		      1
#endif
	);
445 446 447 448 449
	d = (void *) p;

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
450 451 452
static int proc_read_drivers(char *buf, char **start, off_t pos,
			     int count, int *eof, void *data)
{
453 454 455 456 457 458
	char *p = buf;

	bus_for_each_drv(&pcmcia_bus_type, NULL, 
			 (void *) &p, proc_read_drivers_callback);

	return (p - buf);
Linus Torvalds's avatar
Linus Torvalds committed
459 460 461
}
#endif

462 463
/* pcmcia_device handling */

464 465 466 467 468 469 470 471 472 473 474 475 476 477
static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
{
	struct device *tmp_dev;
	tmp_dev = get_device(&p_dev->dev);
	if (!tmp_dev)
		return NULL;
	return to_pcmcia_dev(tmp_dev);
}

static void pcmcia_put_dev(struct pcmcia_device *p_dev)
{
	put_device(&p_dev->dev);
}

478 479 480 481
static void pcmcia_release_dev(struct device *dev)
{
	struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
	p_dev->socket->pcmcia->device_count = 0;
482
	pcmcia_put_bus_socket(p_dev->socket->pcmcia);
483 484 485 486
	kfree(p_dev);
}


Linus Torvalds's avatar
Linus Torvalds committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
/*======================================================================

    These manage a ring buffer of events pending for one user process
    
======================================================================*/

static int queue_empty(user_info_t *user)
{
    return (user->event_head == user->event_tail);
}

static event_t get_queued_event(user_info_t *user)
{
    user->event_tail = (user->event_tail+1) % MAX_EVENTS;
    return user->event[user->event_tail];
}

static void queue_event(user_info_t *user, event_t event)
{
    user->event_head = (user->event_head+1) % MAX_EVENTS;
    if (user->event_head == user->event_tail)
	user->event_tail = (user->event_tail+1) % MAX_EVENTS;
    user->event[user->event_head] = event;
}

512
static void handle_event(struct pcmcia_bus_socket *s, event_t event)
Linus Torvalds's avatar
Linus Torvalds committed
513 514 515 516 517 518 519
{
    user_info_t *user;
    for (user = s->user; user; user = user->next)
	queue_event(user, event);
    wake_up_interruptible(&s->queue);
}

520
static int handle_request(struct pcmcia_bus_socket *s, event_t event)
Linus Torvalds's avatar
Linus Torvalds committed
521 522 523
{
    if (s->req_pending != 0)
	return CS_IN_USE;
524
    if (s->state & DS_SOCKET_BUSY)
Linus Torvalds's avatar
Linus Torvalds committed
525 526
	s->req_pending = 1;
    handle_event(s, event);
527 528
    if (wait_event_interruptible(s->request, s->req_pending <= 0))
        return CS_IN_USE;
529
    if (s->state & DS_SOCKET_BUSY)
530
        return s->req_result;
Linus Torvalds's avatar
Linus Torvalds committed
531 532 533 534 535 536 537 538 539
    return CS_SUCCESS;
}

/*======================================================================

    The card status event handler.
    
======================================================================*/

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
static int send_event(struct pcmcia_socket *s, event_t event, int priority)
{
	int ret = 0;
	client_t *client;

	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;
		}
	}
	return ret;
} /* send_event */


/* Normally, the event is passed to individual drivers after
 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
 * is inversed to maintain historic compatibility.
 */

563
static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
Linus Torvalds's avatar
Linus Torvalds committed
564
{
565 566
	struct pcmcia_bus_socket *s = skt->pcmcia;
	int ret = 0;
567
	client_t *client;
Linus Torvalds's avatar
Linus Torvalds committed
568

569 570
	ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
	       event, priority, s);
Linus Torvalds's avatar
Linus Torvalds committed
571
    
572
	switch (event) {
Linus Torvalds's avatar
Linus Torvalds committed
573

574 575 576 577
	case CS_EVENT_CARD_REMOVAL:
		s->state &= ~DS_SOCKET_PRESENT;
	    	send_event(skt, event, priority);
		handle_event(s, event);
578 579
		for (client = skt->clients; client; client = client->next)
			client->state |= CLIENT_STALE;
580
		break;
Linus Torvalds's avatar
Linus Torvalds committed
581
	
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	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;
Linus Torvalds's avatar
Linus Torvalds committed
599 600 601 602 603 604 605 606 607 608 609
    }

    return 0;
} /* ds_event */

/*======================================================================

    bind_mtd() connects a memory region with an MTD client.
    
======================================================================*/

610
static int bind_mtd(struct pcmcia_bus_socket *bus_sock, mtd_info_t *mtd_info)
Linus Torvalds's avatar
Linus Torvalds committed
611
{
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	struct pcmcia_socket *s = bus_sock->parent;
	memory_handle_t region;

	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;
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633 634
} /* bind_mtd */

/*======================================================================

635 636 637
    bind_request() and bind_device() are merged by now. Individual
    descriptions:

Linus Torvalds's avatar
Linus Torvalds committed
638 639 640 641 642
    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.
    
643 644 645 646 647
    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.

Linus Torvalds's avatar
Linus Torvalds committed
648 649
======================================================================*/

650
static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
Linus Torvalds's avatar
Linus Torvalds committed
651
{
652 653
	struct pcmcia_driver *p_drv;
	struct pcmcia_device *p_dev, *tmp_dev;
654
	client_t *client;
655
	unsigned long flags;
656
	int ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
657

658 659
	if (!s)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
660

661 662 663
	ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
	       (char *)bind_info->dev_info);

664 665 666
	p_drv = get_pcmcia_driver(&bind_info->dev_info);
	if (!p_drv)
		return -EINVAL;
667

668
	if (!try_module_get(p_drv->owner))
669 670 671 672
		return -EINVAL;

	client = (client_t *) kmalloc(sizeof(client_t), GFP_KERNEL);
	if (!client) {
673 674
		ret = -ENOMEM;
		goto err_put;
675 676 677 678 679 680 681 682
	}
	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;
683
	strlcpy(client->dev_info, p_drv->drv.name, DEV_NAME_LEN);
684

685 686 687 688 689 690 691
	/* 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) {
692 693
		ret = -ENOMEM;
		goto err_free_client;
694 695 696
	}
	memset(p_dev, 0, sizeof(struct pcmcia_device));

697 698 699 700 701 702 703
	s = pcmcia_get_bus_socket(s);
 	if (!s) {
		ret = -ENODEV;
		kfree(p_dev);
		goto err_free_client;
	}

704 705 706
	p_dev->socket = s->parent;
	p_dev->device_no = (s->device_count++);
	p_dev->func   = bind_info->function;
707
	p_dev->client = client;
708 709 710 711 712

	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->device_no);
713 714 715 716
	p_dev->dev.driver = &p_drv->drv;

	ret = device_register(&p_dev->dev);
	if (ret) {
717
		kfree(p_dev);
718
		pcmcia_put_bus_socket(s);
719
		goto err_free_client;
720
	}
721 722 723

	/* Add to the list in pcmcia_bus_socket, but only if no device
	 * with the same func _and_ driver exists */
724
	spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
725 726 727 728 729 730 731 732 733
	list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list) {
		if ((tmp_dev->func == bind_info->function) &&
		    (tmp_dev->dev.driver == p_dev->dev.driver)){
			spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
			bind_info->instance = tmp_dev->instance;
			ret = -EBUSY;
			goto err_unregister;
		}
	}
734 735 736
	list_add_tail(&p_dev->socket_device_list, &s->devices_list);
	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);

737 738
	/* finally here the parent client is registered */
	s->parent->clients = client;
739

740 741 742
	if (p_drv->attach) {
		p_dev->instance = p_drv->attach();
		if (!p_dev->instance) {
743 744 745
			printk(KERN_NOTICE "ds: unable to create instance "
			       "of '%s'!\n", (char *)bind_info->dev_info);
			/* FIXME: client isn't freed here */
746 747
			ret = -ENODEV;
			goto err_unregister;
748
		}
Linus Torvalds's avatar
Linus Torvalds committed
749
	}
750

751
	return 0;
752 753 754 755 756 757 758 759

 err_unregister:
	device_unregister(&p_dev->dev);
 err_free_client:
	kfree(client);
 err_put:
	module_put(p_drv->owner);
	return (ret);
Linus Torvalds's avatar
Linus Torvalds committed
760 761 762 763
} /* bind_request */

/*====================================================================*/

764 765
extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);

766
static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
Linus Torvalds's avatar
Linus Torvalds committed
767
{
768 769 770 771
	dev_node_t *node;
	struct pcmcia_device *p_dev;
	unsigned long flags;
	int ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
772 773

#ifdef CONFIG_CARDBUS
774 775 776 777 778 779
	/*
	 * Some unbelievably ugly code to associate the PCI cardbus
	 * device and its driver with the PCMCIA "bind" information.
	 */
	{
		struct pci_bus *bus;
Linus Torvalds's avatar
Linus Torvalds committed
780

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
		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;
				}

				/* 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;
			}
Linus Torvalds's avatar
Linus Torvalds committed
805 806 807 808
		}
	}
#endif

809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
	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 = pcmcia_get_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:
	pcmcia_put_dev(p_dev);
	return (ret);
Linus Torvalds's avatar
Linus Torvalds committed
849 850 851 852
} /* get_device_info */

/*====================================================================*/

853
static int unbind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
Linus Torvalds's avatar
Linus Torvalds committed
854
{
855 856 857 858 859 860
	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);
861 862

 restart:
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
	/* 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) {
			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) {
				if ((p_drv->detach) && (p_dev->instance))
					p_drv->detach(p_dev->instance);
				module_put(p_drv->owner);
			}

			device_unregister(&p_dev->dev);

			/* multiple devices may be registered to this "function" */
			goto restart;
		}
	}
	spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
886 887 888 889 890 891 892 893 894 895
} /* unbind_request */

/*======================================================================

    The user-mode PC Card device interface

======================================================================*/

static int ds_open(struct inode *inode, struct file *file)
{
896
    socket_t i = iminor(inode);
897
    struct pcmcia_bus_socket *s;
Linus Torvalds's avatar
Linus Torvalds committed
898 899
    user_info_t *user;

900
    ds_dbg(0, "ds_open(socket %d)\n", i);
901

902 903 904 905
    s = get_socket_info_by_nr(i);
    if (!s)
	    return -ENODEV;
    s = pcmcia_get_bus_socket(s);
906 907 908
    if (!s)
	    return -ENODEV;

Linus Torvalds's avatar
Linus Torvalds committed
909
    if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
910 911 912 913
	    if (s->state & DS_SOCKET_BUSY) {
		    pcmcia_put_bus_socket(s);
		    return -EBUSY;
	    }
Linus Torvalds's avatar
Linus Torvalds committed
914
	else
915
	    s->state |= DS_SOCKET_BUSY;
Linus Torvalds's avatar
Linus Torvalds committed
916 917 918
    }
    
    user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
919 920 921 922
    if (!user) {
	    pcmcia_put_bus_socket(s);
	    return -ENOMEM;
    }
Linus Torvalds's avatar
Linus Torvalds committed
923 924 925
    user->event_tail = user->event_head = 0;
    user->next = s->user;
    user->user_magic = USER_MAGIC;
926
    user->socket = s;
Linus Torvalds's avatar
Linus Torvalds committed
927 928 929
    s->user = user;
    file->private_data = user;
    
930
    if (s->state & DS_SOCKET_PRESENT)
Linus Torvalds's avatar
Linus Torvalds committed
931 932 933 934 935 936 937 938
	queue_event(user, CS_EVENT_CARD_INSERTION);
    return 0;
} /* ds_open */

/*====================================================================*/

static int ds_release(struct inode *inode, struct file *file)
{
939
    struct pcmcia_bus_socket *s;
Linus Torvalds's avatar
Linus Torvalds committed
940 941
    user_info_t *user, **link;

942
    ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
943

Linus Torvalds's avatar
Linus Torvalds committed
944 945 946 947
    user = file->private_data;
    if (CHECK_USER(user))
	goto out;

948 949
    s = user->socket;

Linus Torvalds's avatar
Linus Torvalds committed
950
    /* Unlink user data structure */
951
    if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
952
	s->state &= ~DS_SOCKET_BUSY;
953 954 955
	s->req_pending = 0;
	wake_up_interruptible(&s->request);
    }
Linus Torvalds's avatar
Linus Torvalds committed
956 957 958 959 960 961 962 963
    file->private_data = NULL;
    for (link = &s->user; *link; link = &(*link)->next)
	if (*link == user) break;
    if (link == NULL)
	goto out;
    *link = user->next;
    user->user_magic = 0;
    kfree(user);
964
    pcmcia_put_bus_socket(s);
Linus Torvalds's avatar
Linus Torvalds committed
965 966 967 968 969 970
out:
    return 0;
} /* ds_release */

/*====================================================================*/

971
static ssize_t ds_read(struct file *file, char __user *buf,
Linus Torvalds's avatar
Linus Torvalds committed
972 973
		       size_t count, loff_t *ppos)
{
974
    struct pcmcia_bus_socket *s;
Linus Torvalds's avatar
Linus Torvalds committed
975
    user_info_t *user;
976
    int ret;
Linus Torvalds's avatar
Linus Torvalds committed
977

978
    ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
Linus Torvalds's avatar
Linus Torvalds committed
979 980 981
    
    if (count < 4)
	return -EINVAL;
982

Linus Torvalds's avatar
Linus Torvalds committed
983 984 985 986
    user = file->private_data;
    if (CHECK_USER(user))
	return -EIO;
    
987
    s = user->socket;
988
    if (s->state & DS_SOCKET_DEAD)
989 990
        return -EIO;

991 992
    ret = wait_event_interruptible(s->queue, !queue_empty(user));
    if (ret == 0)
993
	ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
Linus Torvalds's avatar
Linus Torvalds committed
994

995
    return ret;
Linus Torvalds's avatar
Linus Torvalds committed
996 997 998 999
} /* ds_read */

/*====================================================================*/

1000
static ssize_t ds_write(struct file *file, const char __user *buf,
Linus Torvalds's avatar
Linus Torvalds committed
1001 1002
			size_t count, loff_t *ppos)
{
1003
    struct pcmcia_bus_socket *s;
Linus Torvalds's avatar
Linus Torvalds committed
1004 1005
    user_info_t *user;

1006
    ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
Linus Torvalds's avatar
Linus Torvalds committed
1007 1008 1009 1010 1011
    
    if (count != 4)
	return -EINVAL;
    if ((file->f_flags & O_ACCMODE) == O_RDONLY)
	return -EBADF;
1012

Linus Torvalds's avatar
Linus Torvalds committed
1013 1014 1015 1016
    user = file->private_data;
    if (CHECK_USER(user))
	return -EIO;

1017
    s = user->socket;
1018
    if (s->state & DS_SOCKET_DEAD)
1019 1020
        return -EIO;

Linus Torvalds's avatar
Linus Torvalds committed
1021 1022
    if (s->req_pending) {
	s->req_pending--;
1023
	get_user(s->req_result, (int __user *)buf);
Linus Torvalds's avatar
Linus Torvalds committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	if ((s->req_result != 0) || (s->req_pending == 0))
	    wake_up_interruptible(&s->request);
    } else
	return -EIO;

    return 4;
} /* ds_write */

/*====================================================================*/

/* No kernel lock - fine */
static u_int ds_poll(struct file *file, poll_table *wait)
{
1037
    struct pcmcia_bus_socket *s;
Linus Torvalds's avatar
Linus Torvalds committed
1038 1039
    user_info_t *user;

1040
    ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
Linus Torvalds's avatar
Linus Torvalds committed
1041 1042 1043 1044
    
    user = file->private_data;
    if (CHECK_USER(user))
	return POLLERR;
1045 1046 1047 1048 1049
    s = user->socket;
    /*
     * We don't check for a dead socket here since that
     * will send cardmgr into an endless spin.
     */
Linus Torvalds's avatar
Linus Torvalds committed
1050 1051 1052 1053 1054 1055 1056 1057
    poll_wait(file, &s->queue, wait);
    if (!queue_empty(user))
	return POLLIN | POLLRDNORM;
    return 0;
} /* ds_poll */

/*====================================================================*/

1058
extern int pcmcia_adjust_resource_info(adjust_t *adj);
1059 1060
extern int pccard_get_next_region(struct pcmcia_socket *s, region_info_t *rgn);
extern int pccard_get_first_region(struct pcmcia_socket *s, region_info_t *rgn);
1061

Linus Torvalds's avatar
Linus Torvalds committed
1062 1063 1064
static int ds_ioctl(struct inode * inode, struct file * file,
		    u_int cmd, u_long arg)
{
1065
    struct pcmcia_bus_socket *s;
1066
    void __user *uarg = (char __user *)arg;
Linus Torvalds's avatar
Linus Torvalds committed
1067 1068 1069
    u_int size;
    int ret, err;
    ds_ioctl_arg_t buf;
1070
    user_info_t *user;
Linus Torvalds's avatar
Linus Torvalds committed
1071

1072
    ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
Linus Torvalds's avatar
Linus Torvalds committed
1073
    
1074 1075 1076 1077 1078
    user = file->private_data;
    if (CHECK_USER(user))
	return -EIO;

    s = user->socket;
1079
    if (s->state & DS_SOCKET_DEAD)
1080
        return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
1081 1082 1083 1084 1085 1086 1087 1088 1089
    
    size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
    if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;

    /* Permission check */
    if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
	return -EPERM;
	
    if (cmd & IOC_IN) {
1090
	err = verify_area(VERIFY_READ, uarg, size);
Linus Torvalds's avatar
Linus Torvalds committed
1091
	if (err) {
1092
	    ds_dbg(3, "ds_ioctl(): verify_read = %d\n", err);
Linus Torvalds's avatar
Linus Torvalds committed
1093 1094 1095 1096
	    return err;
	}
    }
    if (cmd & IOC_OUT) {
1097
	err = verify_area(VERIFY_WRITE, uarg, size);
Linus Torvalds's avatar
Linus Torvalds committed
1098
	if (err) {
1099
	    ds_dbg(3, "ds_ioctl(): verify_write = %d\n", err);
Linus Torvalds's avatar
Linus Torvalds committed
1100 1101 1102 1103 1104 1105
	    return err;
	}
    }
    
    err = ret = 0;
    
1106
    if (cmd & IOC_IN) __copy_from_user((char *)&buf, uarg, size);
Linus Torvalds's avatar
Linus Torvalds committed
1107 1108 1109
    
    switch (cmd) {
    case DS_ADJUST_RESOURCE_INFO:
1110
	ret = pcmcia_adjust_resource_info(&buf.adjust);
Linus Torvalds's avatar
Linus Torvalds committed
1111 1112 1113 1114 1115
	break;
    case DS_GET_CARD_SERVICES_INFO:
	ret = pcmcia_get_card_services_info(&buf.servinfo);
	break;
    case DS_GET_CONFIGURATION_INFO:
1116 1117 1118 1119 1120
	if (buf.config.Function && 
	   (buf.config.Function >= s->parent->functions))
	    ret = CS_BAD_ARGS;
	else
	    ret = pccard_get_configuration_info(s->parent, buf.config.Function, &buf.config);
Linus Torvalds's avatar
Linus Torvalds committed
1121 1122
	break;
    case DS_GET_FIRST_TUPLE:
Russell King's avatar
Russell King committed
1123
	pcmcia_validate_mem(s->parent);
1124
	ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf.tuple);
Linus Torvalds's avatar
Linus Torvalds committed
1125 1126
	break;
    case DS_GET_NEXT_TUPLE:
1127
	ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf.tuple);
Linus Torvalds's avatar
Linus Torvalds committed
1128 1129 1130 1131
	break;
    case DS_GET_TUPLE_DATA:
	buf.tuple.TupleData = buf.tuple_parse.data;
	buf.tuple.TupleDataMax = sizeof(buf.tuple_parse.data);
1132
	ret = pccard_get_tuple_data(s->parent, &buf.tuple);
Linus Torvalds's avatar
Linus Torvalds committed
1133 1134 1135
	break;
    case DS_PARSE_TUPLE:
	buf.tuple.TupleData = buf.tuple_parse.data;
1136
	ret = pccard_parse_tuple(&buf.tuple, &buf.tuple_parse.parse);
Linus Torvalds's avatar
Linus Torvalds committed
1137 1138
	break;
    case DS_RESET_CARD:
1139
	ret = pccard_reset_card(s->parent);
Linus Torvalds's avatar
Linus Torvalds committed
1140 1141
	break;
    case DS_GET_STATUS:
1142 1143 1144 1145 1146
	if (buf.status.Function && 
	   (buf.status.Function >= s->parent->functions))
	    ret = CS_BAD_ARGS;
	else
	ret = pccard_get_status(s->parent, buf.status.Function, &buf.status);
Linus Torvalds's avatar
Linus Torvalds committed
1147 1148
	break;
    case DS_VALIDATE_CIS:
Russell King's avatar
Russell King committed
1149
	pcmcia_validate_mem(s->parent);
1150
	ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf.cisinfo);
Linus Torvalds's avatar
Linus Torvalds committed
1151 1152
	break;
    case DS_SUSPEND_CARD:
1153
	ret = pcmcia_suspend_card(s->parent);
Linus Torvalds's avatar
Linus Torvalds committed
1154 1155
	break;
    case DS_RESUME_CARD:
1156
	ret = pcmcia_resume_card(s->parent);
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158
	break;
    case DS_EJECT_CARD:
1159
	err = pcmcia_eject_card(s->parent);
Linus Torvalds's avatar
Linus Torvalds committed
1160 1161
	break;
    case DS_INSERT_CARD:
1162
	err = pcmcia_insert_card(s->parent);
Linus Torvalds's avatar
Linus Torvalds committed
1163 1164 1165 1166
	break;
    case DS_ACCESS_CONFIGURATION_REGISTER:
	if ((buf.conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN))
	    return -EPERM;
1167 1168 1169 1170 1171
	if (buf.conf_reg.Function && 
	   (buf.conf_reg.Function >= s->parent->functions))
	    ret = CS_BAD_ARGS;
	else
	    ret = pccard_access_configuration_register(s->parent, buf.conf_reg.Function, &buf.conf_reg);
Linus Torvalds's avatar
Linus Torvalds committed
1172 1173
	break;
    case DS_GET_FIRST_REGION:
1174
        ret = pccard_get_first_region(s->parent, &buf.region);
Linus Torvalds's avatar
Linus Torvalds committed
1175 1176
	break;
    case DS_GET_NEXT_REGION:
1177
	ret = pccard_get_next_region(s->parent, &buf.region);
Linus Torvalds's avatar
Linus Torvalds committed
1178 1179
	break;
    case DS_GET_FIRST_WINDOW:
1180
	ret = pcmcia_get_window(s->parent, &buf.win_info.handle, 0, &buf.win_info.window);
Linus Torvalds's avatar
Linus Torvalds committed
1181 1182
	break;
    case DS_GET_NEXT_WINDOW:
1183
	ret = pcmcia_get_window(s->parent, &buf.win_info.handle, buf.win_info.handle->index + 1, &buf.win_info.window);
Linus Torvalds's avatar
Linus Torvalds committed
1184 1185 1186 1187 1188 1189
	break;
    case DS_GET_MEM_PAGE:
	ret = pcmcia_get_mem_page(buf.win_info.handle,
			   &buf.win_info.map);
	break;
    case DS_REPLACE_CIS:
1190
	ret = pcmcia_replace_cis(s->parent, &buf.cisdump);
Linus Torvalds's avatar
Linus Torvalds committed
1191 1192 1193
	break;
    case DS_BIND_REQUEST:
	if (!capable(CAP_SYS_ADMIN)) return -EPERM;
1194
	err = bind_request(s, &buf.bind_info);
Linus Torvalds's avatar
Linus Torvalds committed
1195 1196
	break;
    case DS_GET_DEVICE_INFO:
1197
	err = get_device_info(s, &buf.bind_info, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1198 1199
	break;
    case DS_GET_NEXT_DEVICE:
1200
	err = get_device_info(s, &buf.bind_info, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1201 1202
	break;
    case DS_UNBIND_REQUEST:
1203
	err = unbind_request(s, &buf.bind_info);
Linus Torvalds's avatar
Linus Torvalds committed
1204 1205
	break;
    case DS_BIND_MTD:
1206
	if (!capable(CAP_SYS_ADMIN)) return -EPERM;
1207
	err = bind_mtd(s, &buf.mtd_info);
Linus Torvalds's avatar
Linus Torvalds committed
1208 1209 1210 1211 1212 1213
	break;
    default:
	err = -EINVAL;
    }
    
    if ((err == 0) && (ret != CS_SUCCESS)) {
1214
	ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
Linus Torvalds's avatar
Linus Torvalds committed
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
	switch (ret) {
	case CS_BAD_SOCKET: case CS_NO_CARD:
	    err = -ENODEV; break;
	case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
	case CS_BAD_TUPLE:
	    err = -EINVAL; break;
	case CS_IN_USE:
	    err = -EBUSY; break;
	case CS_OUT_OF_RESOURCE:
	    err = -ENOSPC; break;
	case CS_NO_MORE_ITEMS:
	    err = -ENODATA; break;
	case CS_UNSUPPORTED_FUNCTION:
	    err = -ENOSYS; break;
	default:
	    err = -EIO; break;
	}
    }
1233

1234 1235 1236 1237 1238
    if (cmd & IOC_OUT) {
        if (__copy_to_user(uarg, (char *)&buf, size))
            err = -EFAULT;
    }

1239

Linus Torvalds's avatar
Linus Torvalds committed
1240 1241 1242 1243 1244 1245
    return err;
} /* ds_ioctl */

/*====================================================================*/

static struct file_operations ds_fops = {
1246 1247 1248 1249 1250 1251 1252
	.owner		= THIS_MODULE,
	.open		= ds_open,
	.release	= ds_release,
	.ioctl		= ds_ioctl,
	.read		= ds_read,
	.write		= ds_write,
	.poll		= ds_poll,
Linus Torvalds's avatar
Linus Torvalds committed
1253 1254
};

Dominik Brodowski's avatar
Dominik Brodowski committed
1255
static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1256
{
1257
	struct pcmcia_socket *socket = class_get_devdata(class_dev);
1258
	struct pcmcia_bus_socket *s;
1259
	int ret;
1260

1261
	s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1262 1263
	if(!s)
		return -ENOMEM;
1264
	memset(s, 0, sizeof(struct pcmcia_bus_socket));
1265 1266 1267 1268 1269 1270 1271 1272 1273

	/* get reference to parent socket */
	s->parent = pcmcia_get_socket(socket);
	if (!s->parent) {
		printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
		kfree (s);
		return -ENODEV;
	}

1274
	kref_init(&s->refcount);
Linus Torvalds's avatar
Linus Torvalds committed
1275
    
1276 1277 1278 1279
	/*
	 * Ugly. But we want to wait for the socket threads to have started up.
	 * We really should let the drivers themselves drive some of this..
	 */
1280
	msleep(250);
Linus Torvalds's avatar
Linus Torvalds committed
1281 1282 1283

	init_waitqueue_head(&s->queue);
	init_waitqueue_head(&s->request);
1284
	INIT_LIST_HEAD(&s->devices_list);
1285 1286

	/* Set up hotline to Card Services */
1287 1288 1289
	s->callback.owner = THIS_MODULE;
	s->callback.event = &ds_event;
	socket->pcmcia = s;
1290

1291 1292 1293 1294 1295 1296
	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);
Linus Torvalds's avatar
Linus Torvalds committed
1297 1298
	}

1299
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1300 1301 1302
}


Dominik Brodowski's avatar
Dominik Brodowski committed
1303
static void pcmcia_bus_remove_socket(struct class_device *class_dev)
Linus Torvalds's avatar
Linus Torvalds committed
1304
{
1305
	struct pcmcia_socket *socket = class_get_devdata(class_dev);
1306

Dominik Brodowski's avatar
Dominik Brodowski committed
1307
	if (!socket || !socket->pcmcia)
1308
		return;
1309

1310
	pccard_register_pcmcia(socket, NULL);
1311

1312
	socket->pcmcia->state |= DS_SOCKET_DEAD;
1313
	pcmcia_put_bus_socket(socket->pcmcia);
Dominik Brodowski's avatar
Dominik Brodowski committed
1314 1315
	socket->pcmcia = NULL;

1316
	return;
1317 1318 1319 1320
}


/* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1321 1322
static struct class_interface pcmcia_bus_interface = {
	.class = &pcmcia_socket_class,
Dominik Brodowski's avatar
Dominik Brodowski committed
1323 1324
	.add = &pcmcia_bus_add_socket,
	.remove = &pcmcia_bus_remove_socket,
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
};


struct bus_type pcmcia_bus_type = {
	.name = "pcmcia",
};
EXPORT_SYMBOL(pcmcia_bus_type);


static int __init init_pcmcia_bus(void)
{
	int i;

1338 1339
	spin_lock_init(&pcmcia_dev_list_lock);

1340
	bus_register(&pcmcia_bus_type);
1341
	class_interface_register(&pcmcia_bus_interface);
1342 1343 1344 1345 1346 1347 1348 1349 1350

	/* Set up character device for user mode clients */
	i = register_chrdev(0, "pcmcia", &ds_fops);
	if (i == -EBUSY)
		printk(KERN_NOTICE "unable to find a free device # for "
		       "Driver Services\n");
	else
		major_dev = i;

Linus Torvalds's avatar
Linus Torvalds committed
1351
#ifdef CONFIG_PROC_FS
1352
	proc_pccard = proc_mkdir("pccard", proc_bus);
1353 1354
	if (proc_pccard)
		create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1355
#endif
1356 1357

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1358
}
1359 1360
fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that 
			       * pcmcia_socket_class is already registered */
Linus Torvalds's avatar
Linus Torvalds committed
1361

1362

1363 1364
static void __exit exit_pcmcia_bus(void)
{
1365
	class_interface_unregister(&pcmcia_bus_interface);
1366

1367
#ifdef CONFIG_PROC_FS
1368
	if (proc_pccard) {
1369
		remove_proc_entry("drivers", proc_pccard);
1370 1371
		remove_proc_entry("pccard", proc_bus);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1372
#endif
1373 1374 1375 1376 1377 1378
	if (major_dev != -1)
		unregister_chrdev(major_dev, "pcmcia");

	bus_unregister(&pcmcia_bus_type);
}
module_exit(exit_pcmcia_bus);
1379

1380 1381 1382 1383


/* helpers for backwards-compatible functions */

1384
static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
1385
{
1386 1387 1388 1389 1390
	struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
	if (s && s->pcmcia)
		return s->pcmcia;
	else
		return NULL;
1391 1392
}

1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
/* backwards-compatible accessing of driver --- by name! */

struct cmp_data {
	void *dev_info;
	struct pcmcia_driver *drv;
};

static int cmp_drv_callback(struct device_driver *drv, void *data)
{
	struct cmp_data *cmp = data;
	if (strncmp((char *)cmp->dev_info, (char *)drv->name,
		    DEV_NAME_LEN) == 0) {
		cmp->drv = container_of(drv, struct pcmcia_driver, drv);
		return -EINVAL;
	}
	return 0;
}

static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
{
	int ret;
	struct cmp_data cmp = {
		.dev_info = dev_info,
	};
	
	ret = bus_for_each_drv(&pcmcia_bus_type, NULL, &cmp, cmp_drv_callback);
	if (ret)
		return cmp.drv;
	return NULL;
}
1423 1424

MODULE_ALIAS("ds");