Linux Audio

Check our new training course

Loading...
v3.1
 
   1/*
   2 * PCMCIA 16-bit resource management functions
   3 *
   4 * The initial developer of the original code is David A. Hinds
   5 * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
   6 * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
   7 *
   8 * Copyright (C) 1999	     David A. Hinds
   9 * Copyright (C) 2004-2010   Dominik Brodowski
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License version 2 as
  13 * published by the Free Software Foundation.
  14 *
  15 */
  16
  17#include <linux/module.h>
  18#include <linux/kernel.h>
  19#include <linux/interrupt.h>
  20#include <linux/delay.h>
  21#include <linux/pci.h>
  22#include <linux/device.h>
  23#include <linux/netdevice.h>
  24#include <linux/slab.h>
  25
  26#include <asm/irq.h>
  27
  28#include <pcmcia/ss.h>
  29#include <pcmcia/cistpl.h>
  30#include <pcmcia/cisreg.h>
  31#include <pcmcia/ds.h>
  32
  33#include "cs_internal.h"
  34
  35
  36/* Access speed for IO windows */
  37static int io_speed;
  38module_param(io_speed, int, 0444);
  39
  40
  41int pcmcia_validate_mem(struct pcmcia_socket *s)
  42{
  43	if (s->resource_ops->validate_mem)
  44		return s->resource_ops->validate_mem(s);
  45	/* if there is no callback, we can assume that everything is OK */
  46	return 0;
  47}
  48
  49struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
  50				 int low, struct pcmcia_socket *s)
  51{
  52	if (s->resource_ops->find_mem)
  53		return s->resource_ops->find_mem(base, num, align, low, s);
  54	return NULL;
  55}
  56
  57
  58/**
  59 * release_io_space() - release IO ports allocated with alloc_io_space()
  60 * @s: pcmcia socket
  61 * @res: resource to release
  62 *
  63 */
  64static void release_io_space(struct pcmcia_socket *s, struct resource *res)
  65{
  66	resource_size_t num = resource_size(res);
  67	int i;
  68
  69	dev_dbg(&s->dev, "release_io_space for %pR\n", res);
  70
  71	for (i = 0; i < MAX_IO_WIN; i++) {
  72		if (!s->io[i].res)
  73			continue;
  74		if ((s->io[i].res->start <= res->start) &&
  75		    (s->io[i].res->end >= res->end)) {
  76			s->io[i].InUse -= num;
  77			if (res->parent)
  78				release_resource(res);
  79			res->start = res->end = 0;
  80			res->flags = IORESOURCE_IO;
  81			/* Free the window if no one else is using it */
  82			if (s->io[i].InUse == 0) {
  83				release_resource(s->io[i].res);
  84				kfree(s->io[i].res);
  85				s->io[i].res = NULL;
  86			}
  87		}
  88	}
  89}
  90
  91
  92/**
  93 * alloc_io_space() - allocate IO ports for use by a PCMCIA device
  94 * @s: pcmcia socket
  95 * @res: resource to allocate (begin: begin, end: size)
  96 * @lines: number of IO lines decoded by the PCMCIA card
  97 *
  98 * Special stuff for managing IO windows, because they are scarce
  99 */
 100static int alloc_io_space(struct pcmcia_socket *s, struct resource *res,
 101			unsigned int lines)
 102{
 103	unsigned int align;
 104	unsigned int base = res->start;
 105	unsigned int num = res->end;
 106	int ret;
 107
 108	res->flags |= IORESOURCE_IO;
 109
 110	dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n",
 111		res, lines);
 112
 113	align = base ? (lines ? 1<<lines : 0) : 1;
 114	if (align && (align < num)) {
 115		if (base) {
 116			dev_dbg(&s->dev, "odd IO request\n");
 117			align = 0;
 118		} else
 119			while (align && (align < num))
 120				align <<= 1;
 121	}
 122	if (base & ~(align-1)) {
 123		dev_dbg(&s->dev, "odd IO request\n");
 124		align = 0;
 125	}
 126
 127	ret = s->resource_ops->find_io(s, res->flags, &base, num, align,
 128				&res->parent);
 129	if (ret) {
 130		dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret);
 131		return -EINVAL;
 132	}
 133
 134	res->start = base;
 135	res->end = res->start + num - 1;
 136
 137	if (res->parent) {
 138		ret = request_resource(res->parent, res);
 139		if (ret) {
 140			dev_warn(&s->dev,
 141				"request_resource %pR failed: %d\n", res, ret);
 142			res->parent = NULL;
 143			release_io_space(s, res);
 144		}
 145	}
 146	dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res);
 147	return ret;
 148}
 149
 150
 151/**
 152 * pcmcia_access_config() - read or write card configuration registers
 153 *
 154 * pcmcia_access_config() reads and writes configuration registers in
 155 * attribute memory.  Memory window 0 is reserved for this and the tuple
 156 * reading services. Drivers must use pcmcia_read_config_byte() or
 157 * pcmcia_write_config_byte().
 158 */
 159static int pcmcia_access_config(struct pcmcia_device *p_dev,
 160				off_t where, u8 *val,
 161				int (*accessf) (struct pcmcia_socket *s,
 162						int attr, unsigned int addr,
 163						unsigned int len, void *ptr))
 164{
 165	struct pcmcia_socket *s;
 166	config_t *c;
 167	int addr;
 168	int ret = 0;
 169
 170	s = p_dev->socket;
 171
 172	mutex_lock(&s->ops_mutex);
 173	c = p_dev->function_config;
 174
 175	if (!(c->state & CONFIG_LOCKED)) {
 176		dev_dbg(&p_dev->dev, "Configuration isn't locked\n");
 177		mutex_unlock(&s->ops_mutex);
 178		return -EACCES;
 179	}
 180
 181	addr = (p_dev->config_base + where) >> 1;
 182
 183	ret = accessf(s, 1, addr, 1, val);
 184
 185	mutex_unlock(&s->ops_mutex);
 186
 187	return ret;
 188}
 189
 190
 191/**
 192 * pcmcia_read_config_byte() - read a byte from a card configuration register
 193 *
 194 * pcmcia_read_config_byte() reads a byte from a configuration register in
 195 * attribute memory.
 196 */
 197int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
 198{
 199	return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
 200}
 201EXPORT_SYMBOL(pcmcia_read_config_byte);
 202
 203
 204/**
 205 * pcmcia_write_config_byte() - write a byte to a card configuration register
 206 *
 207 * pcmcia_write_config_byte() writes a byte to a configuration register in
 208 * attribute memory.
 209 */
 210int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
 211{
 212	return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
 213}
 214EXPORT_SYMBOL(pcmcia_write_config_byte);
 215
 216
 217/**
 218 * pcmcia_map_mem_page() - modify iomem window to point to a different offset
 219 * @p_dev: pcmcia device
 220 * @res: iomem resource already enabled by pcmcia_request_window()
 221 * @offset: card_offset to map
 222 *
 223 * pcmcia_map_mem_page() modifies what can be read and written by accessing
 224 * an iomem range previously enabled by pcmcia_request_window(), by setting
 225 * the card_offset value to @offset.
 226 */
 227int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res,
 228			unsigned int offset)
 229{
 230	struct pcmcia_socket *s = p_dev->socket;
 231	unsigned int w;
 232	int ret;
 233
 234	w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
 235	if (w >= MAX_WIN)
 236		return -EINVAL;
 237
 238	mutex_lock(&s->ops_mutex);
 239	s->win[w].card_start = offset;
 240	ret = s->ops->set_mem_map(s, &s->win[w]);
 241	if (ret)
 242		dev_warn(&p_dev->dev, "failed to set_mem_map\n");
 243	mutex_unlock(&s->ops_mutex);
 244	return ret;
 245}
 246EXPORT_SYMBOL(pcmcia_map_mem_page);
 247
 248
 249/**
 250 * pcmcia_fixup_iowidth() - reduce io width to 8bit
 251 * @p_dev: pcmcia device
 252 *
 253 * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the
 254 * IO width to 8bit after having called pcmcia_enable_device()
 255 * previously.
 256 */
 257int pcmcia_fixup_iowidth(struct pcmcia_device *p_dev)
 258{
 259	struct pcmcia_socket *s = p_dev->socket;
 260	pccard_io_map io_off = { 0, 0, 0, 0, 1 };
 261	pccard_io_map io_on;
 262	int i, ret = 0;
 263
 264	mutex_lock(&s->ops_mutex);
 265
 266	dev_dbg(&p_dev->dev, "fixup iowidth to 8bit\n");
 267
 268	if (!(s->state & SOCKET_PRESENT) ||
 269		!(p_dev->function_config->state & CONFIG_LOCKED)) {
 270		dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
 271		ret = -EACCES;
 272		goto unlock;
 273	}
 274
 275	io_on.speed = io_speed;
 276	for (i = 0; i < MAX_IO_WIN; i++) {
 277		if (!s->io[i].res)
 278			continue;
 279		io_off.map = i;
 280		io_on.map = i;
 281
 282		io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
 283		io_on.start = s->io[i].res->start;
 284		io_on.stop = s->io[i].res->end;
 285
 286		s->ops->set_io_map(s, &io_off);
 287		mdelay(40);
 288		s->ops->set_io_map(s, &io_on);
 289	}
 290unlock:
 291	mutex_unlock(&s->ops_mutex);
 292
 293	return ret;
 294}
 295EXPORT_SYMBOL(pcmcia_fixup_iowidth);
 296
 297
 298/**
 299 * pcmcia_fixup_vpp() - set Vpp to a new voltage level
 300 * @p_dev: pcmcia device
 301 * @new_vpp: new Vpp voltage
 302 *
 303 * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to
 304 * a new voltage level between calls to pcmcia_enable_device()
 305 * and pcmcia_disable_device().
 306 */
 307int pcmcia_fixup_vpp(struct pcmcia_device *p_dev, unsigned char new_vpp)
 308{
 309	struct pcmcia_socket *s = p_dev->socket;
 310	int ret = 0;
 311
 312	mutex_lock(&s->ops_mutex);
 313
 314	dev_dbg(&p_dev->dev, "fixup Vpp to %d\n", new_vpp);
 315
 316	if (!(s->state & SOCKET_PRESENT) ||
 317		!(p_dev->function_config->state & CONFIG_LOCKED)) {
 318		dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
 319		ret = -EACCES;
 320		goto unlock;
 321	}
 322
 323	s->socket.Vpp = new_vpp;
 324	if (s->ops->set_socket(s, &s->socket)) {
 325		dev_warn(&p_dev->dev, "Unable to set VPP\n");
 326		ret = -EIO;
 327		goto unlock;
 328	}
 329	p_dev->vpp = new_vpp;
 330
 331unlock:
 332	mutex_unlock(&s->ops_mutex);
 333
 334	return ret;
 335}
 336EXPORT_SYMBOL(pcmcia_fixup_vpp);
 337
 338
 339/**
 340 * pcmcia_release_configuration() - physically disable a PCMCIA device
 341 * @p_dev: pcmcia device
 342 *
 343 * pcmcia_release_configuration() is the 1:1 counterpart to
 344 * pcmcia_enable_device(): If a PCMCIA device is no longer used by any
 345 * driver, the Vpp voltage is set to 0, IRQs will no longer be generated,
 346 * and I/O ranges will be disabled. As pcmcia_release_io() and
 347 * pcmcia_release_window() still need to be called, device drivers are
 348 * expected to call pcmcia_disable_device() instead.
 349 */
 350int pcmcia_release_configuration(struct pcmcia_device *p_dev)
 351{
 352	pccard_io_map io = { 0, 0, 0, 0, 1 };
 353	struct pcmcia_socket *s = p_dev->socket;
 354	config_t *c;
 355	int i;
 356
 357	mutex_lock(&s->ops_mutex);
 358	c = p_dev->function_config;
 359	if (p_dev->_locked) {
 360		p_dev->_locked = 0;
 361		if (--(s->lock_count) == 0) {
 362			s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
 363			s->socket.Vpp = 0;
 364			s->socket.io_irq = 0;
 365			s->ops->set_socket(s, &s->socket);
 366		}
 367	}
 368	if (c->state & CONFIG_LOCKED) {
 369		c->state &= ~CONFIG_LOCKED;
 370		if (c->state & CONFIG_IO_REQ)
 371			for (i = 0; i < MAX_IO_WIN; i++) {
 372				if (!s->io[i].res)
 373					continue;
 374				s->io[i].Config--;
 375				if (s->io[i].Config != 0)
 376					continue;
 377				io.map = i;
 378				s->ops->set_io_map(s, &io);
 379			}
 380	}
 381	mutex_unlock(&s->ops_mutex);
 382
 383	return 0;
 384}
 385
 386
 387/**
 388 * pcmcia_release_io() - release I/O allocated by a PCMCIA device
 389 * @p_dev: pcmcia device
 390 *
 391 * pcmcia_release_io() releases the I/O ranges allocated by a PCMCIA
 392 * device.  This may be invoked some time after a card ejection has
 393 * already dumped the actual socket configuration, so if the client is
 394 * "stale", we don't bother checking the port ranges against the
 395 * current socket values.
 396 */
 397static int pcmcia_release_io(struct pcmcia_device *p_dev)
 398{
 399	struct pcmcia_socket *s = p_dev->socket;
 400	int ret = -EINVAL;
 401	config_t *c;
 402
 403	mutex_lock(&s->ops_mutex);
 404	if (!p_dev->_io)
 405		goto out;
 406
 407	c = p_dev->function_config;
 408
 409	release_io_space(s, &c->io[0]);
 410
 411	if (c->io[1].end)
 412		release_io_space(s, &c->io[1]);
 413
 414	p_dev->_io = 0;
 415	c->state &= ~CONFIG_IO_REQ;
 416
 417out:
 418	mutex_unlock(&s->ops_mutex);
 419
 420	return ret;
 421} /* pcmcia_release_io */
 422
 423
 424/**
 425 * pcmcia_release_window() - release reserved iomem for PCMCIA devices
 426 * @p_dev: pcmcia device
 427 * @res: iomem resource to release
 428 *
 429 * pcmcia_release_window() releases &struct resource *res which was
 430 * previously reserved by calling pcmcia_request_window().
 431 */
 432int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
 433{
 434	struct pcmcia_socket *s = p_dev->socket;
 435	pccard_mem_map *win;
 436	unsigned int w;
 437
 438	dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
 439
 440	w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
 441	if (w >= MAX_WIN)
 442		return -EINVAL;
 443
 444	mutex_lock(&s->ops_mutex);
 445	win = &s->win[w];
 446
 447	if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
 448		dev_dbg(&p_dev->dev, "not releasing unknown window\n");
 449		mutex_unlock(&s->ops_mutex);
 450		return -EINVAL;
 451	}
 452
 453	/* Shut down memory window */
 454	win->flags &= ~MAP_ACTIVE;
 455	s->ops->set_mem_map(s, win);
 456	s->state &= ~SOCKET_WIN_REQ(w);
 457
 458	/* Release system memory */
 459	if (win->res) {
 460		release_resource(res);
 461		release_resource(win->res);
 462		kfree(win->res);
 463		win->res = NULL;
 464	}
 465	res->start = res->end = 0;
 466	res->flags = IORESOURCE_MEM;
 467	p_dev->_win &= ~CLIENT_WIN_REQ(w);
 468	mutex_unlock(&s->ops_mutex);
 469
 470	return 0;
 471} /* pcmcia_release_window */
 472EXPORT_SYMBOL(pcmcia_release_window);
 473
 474
 475/**
 476 * pcmcia_enable_device() - set up and activate a PCMCIA device
 477 * @p_dev: the associated PCMCIA device
 478 *
 479 * pcmcia_enable_device() physically enables a PCMCIA device. It parses
 480 * the flags passed to in @flags and stored in @p_dev->flags and sets up
 481 * the Vpp voltage, enables the speaker line, I/O ports and store proper
 482 * values to configuration registers.
 483 */
 484int pcmcia_enable_device(struct pcmcia_device *p_dev)
 485{
 486	int i;
 487	unsigned int base;
 488	struct pcmcia_socket *s = p_dev->socket;
 489	config_t *c;
 490	pccard_io_map iomap;
 491	unsigned char status = 0;
 492	unsigned char ext_status = 0;
 493	unsigned char option = 0;
 494	unsigned int flags = p_dev->config_flags;
 495
 496	if (!(s->state & SOCKET_PRESENT))
 497		return -ENODEV;
 498
 499	mutex_lock(&s->ops_mutex);
 500	c = p_dev->function_config;
 501	if (c->state & CONFIG_LOCKED) {
 502		mutex_unlock(&s->ops_mutex);
 503		dev_dbg(&p_dev->dev, "Configuration is locked\n");
 504		return -EACCES;
 505	}
 506
 507	/* Do power control.  We don't allow changes in Vcc. */
 508	s->socket.Vpp = p_dev->vpp;
 509	if (s->ops->set_socket(s, &s->socket)) {
 510		mutex_unlock(&s->ops_mutex);
 511		dev_printk(KERN_WARNING, &p_dev->dev,
 512			   "Unable to set socket state\n");
 513		return -EINVAL;
 514	}
 515
 516	/* Pick memory or I/O card, DMA mode, interrupt */
 517	if (p_dev->_io || flags & CONF_ENABLE_IRQ)
 518		flags |= CONF_ENABLE_IOCARD;
 519	if (flags & CONF_ENABLE_IOCARD)
 520		s->socket.flags |= SS_IOCARD;
 521	if (flags & CONF_ENABLE_ZVCARD)
 522		s->socket.flags |= SS_ZVCARD | SS_IOCARD;
 523	if (flags & CONF_ENABLE_SPKR) {
 524		s->socket.flags |= SS_SPKR_ENA;
 525		status = CCSR_AUDIO_ENA;
 526		if (!(p_dev->config_regs & PRESENT_STATUS))
 527			dev_warn(&p_dev->dev, "speaker requested, but "
 528					      "PRESENT_STATUS not set!\n");
 529	}
 530	if (flags & CONF_ENABLE_IRQ)
 531		s->socket.io_irq = s->pcmcia_irq;
 532	else
 533		s->socket.io_irq = 0;
 534	if (flags & CONF_ENABLE_ESR) {
 535		p_dev->config_regs |= PRESENT_EXT_STATUS;
 536		ext_status = ESR_REQ_ATTN_ENA;
 537	}
 538	s->ops->set_socket(s, &s->socket);
 539	s->lock_count++;
 540
 541	dev_dbg(&p_dev->dev,
 542		"enable_device: V %d, flags %x, base %x, regs %x, idx %x\n",
 543		p_dev->vpp, flags, p_dev->config_base, p_dev->config_regs,
 544		p_dev->config_index);
 545
 546	/* Set up CIS configuration registers */
 547	base = p_dev->config_base;
 548	if (p_dev->config_regs & PRESENT_COPY) {
 549		u16 tmp = 0;
 550		dev_dbg(&p_dev->dev, "clearing CISREG_SCR\n");
 551		pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &tmp);
 552	}
 553	if (p_dev->config_regs & PRESENT_PIN_REPLACE) {
 554		u16 tmp = 0;
 555		dev_dbg(&p_dev->dev, "clearing CISREG_PRR\n");
 556		pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &tmp);
 557	}
 558	if (p_dev->config_regs & PRESENT_OPTION) {
 559		if (s->functions == 1) {
 560			option = p_dev->config_index & COR_CONFIG_MASK;
 561		} else {
 562			option = p_dev->config_index & COR_MFC_CONFIG_MASK;
 563			option |= COR_FUNC_ENA|COR_IREQ_ENA;
 564			if (p_dev->config_regs & PRESENT_IOBASE_0)
 565				option |= COR_ADDR_DECODE;
 566		}
 567		if ((flags & CONF_ENABLE_IRQ) &&
 568			!(flags & CONF_ENABLE_PULSE_IRQ))
 569			option |= COR_LEVEL_REQ;
 570		pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &option);
 571		mdelay(40);
 572	}
 573	if (p_dev->config_regs & PRESENT_STATUS)
 574		pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &status);
 575
 576	if (p_dev->config_regs & PRESENT_EXT_STATUS)
 577		pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1,
 578					&ext_status);
 579
 580	if (p_dev->config_regs & PRESENT_IOBASE_0) {
 581		u8 b = c->io[0].start & 0xff;
 582		pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
 583		b = (c->io[0].start >> 8) & 0xff;
 584		pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
 585	}
 586	if (p_dev->config_regs & PRESENT_IOSIZE) {
 587		u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
 588		pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
 589	}
 590
 591	/* Configure I/O windows */
 592	if (c->state & CONFIG_IO_REQ) {
 593		iomap.speed = io_speed;
 594		for (i = 0; i < MAX_IO_WIN; i++)
 595			if (s->io[i].res) {
 596				iomap.map = i;
 597				iomap.flags = MAP_ACTIVE;
 598				switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
 599				case IO_DATA_PATH_WIDTH_16:
 600					iomap.flags |= MAP_16BIT; break;
 601				case IO_DATA_PATH_WIDTH_AUTO:
 602					iomap.flags |= MAP_AUTOSZ; break;
 603				default:
 604					break;
 605				}
 606				iomap.start = s->io[i].res->start;
 607				iomap.stop = s->io[i].res->end;
 608				s->ops->set_io_map(s, &iomap);
 609				s->io[i].Config++;
 610			}
 611	}
 612
 613	c->state |= CONFIG_LOCKED;
 614	p_dev->_locked = 1;
 615	mutex_unlock(&s->ops_mutex);
 616	return 0;
 617} /* pcmcia_enable_device */
 618EXPORT_SYMBOL(pcmcia_enable_device);
 619
 620
 621/**
 622 * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
 623 * @p_dev: the associated PCMCIA device
 624 *
 625 * pcmcia_request_io() attempts to reserve the IO port ranges specified in
 626 * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
 627 * "start" value is the requested start of the IO port resource; "end"
 628 * reflects the number of ports requested. The number of IO lines requested
 629 * is specified in &struct pcmcia_device @p_dev->io_lines.
 630 */
 631int pcmcia_request_io(struct pcmcia_device *p_dev)
 632{
 633	struct pcmcia_socket *s = p_dev->socket;
 634	config_t *c = p_dev->function_config;
 635	int ret = -EINVAL;
 636
 637	mutex_lock(&s->ops_mutex);
 638	dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
 639		&c->io[0], &c->io[1]);
 640
 641	if (!(s->state & SOCKET_PRESENT)) {
 642		dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
 643		goto out;
 644	}
 645
 646	if (c->state & CONFIG_LOCKED) {
 647		dev_dbg(&p_dev->dev, "Configuration is locked\n");
 648		goto out;
 649	}
 650	if (c->state & CONFIG_IO_REQ) {
 651		dev_dbg(&p_dev->dev, "IO already configured\n");
 652		goto out;
 653	}
 654
 655	ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
 656	if (ret)
 657		goto out;
 658
 659	if (c->io[1].end) {
 660		ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
 661		if (ret) {
 662			struct resource tmp = c->io[0];
 663			/* release the previously allocated resource */
 664			release_io_space(s, &c->io[0]);
 665			/* but preserve the settings, for they worked... */
 666			c->io[0].end = resource_size(&tmp);
 667			c->io[0].start = tmp.start;
 668			c->io[0].flags = tmp.flags;
 669			goto out;
 670		}
 671	} else
 672		c->io[1].start = 0;
 673
 674	c->state |= CONFIG_IO_REQ;
 675	p_dev->_io = 1;
 676
 677	dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
 678		&c->io[0], &c->io[1]);
 679out:
 680	mutex_unlock(&s->ops_mutex);
 681
 682	return ret;
 683} /* pcmcia_request_io */
 684EXPORT_SYMBOL(pcmcia_request_io);
 685
 686
 687/**
 688 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
 689 * @p_dev: the associated PCMCIA device
 690 * @handler: IRQ handler to register
 691 *
 692 * pcmcia_request_irq() is a wrapper around request_irq() which allows
 693 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
 694 * Drivers are free to use request_irq() directly, but then they need to
 695 * call free_irq() themselfves, too. Also, only %IRQF_SHARED capable IRQ
 696 * handlers are allowed.
 697 */
 698int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
 699				    irq_handler_t handler)
 700{
 701	int ret;
 702
 703	if (!p_dev->irq)
 704		return -EINVAL;
 705
 706	ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
 707			p_dev->devname, p_dev->priv);
 708	if (!ret)
 709		p_dev->_irq = 1;
 710
 711	return ret;
 712}
 713EXPORT_SYMBOL(pcmcia_request_irq);
 714
 715
 716/**
 717 * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
 718 * @p_dev: the associated PCMCIA device
 719 * @handler: IRQ handler to register
 720 *
 721 * pcmcia_request_exclusive_irq() is a wrapper around request_irq() which
 722 * attempts first to request an exclusive IRQ. If it fails, it also accepts
 723 * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
 724 * IRQ sharing and either use request_irq directly (then they need to call
 725 * free_irq() themselves, too), or the pcmcia_request_irq() function.
 726 */
 727int __must_check
 728__pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
 729			irq_handler_t handler)
 730{
 731	int ret;
 732
 733	if (!p_dev->irq)
 734		return -EINVAL;
 735
 736	ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
 737	if (ret) {
 738		ret = pcmcia_request_irq(p_dev, handler);
 739		dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
 740			"request for exclusive IRQ could not be fulfilled.\n");
 741		dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
 742			"needs updating to supported shared IRQ lines.\n");
 743	}
 744	if (ret)
 745		dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
 746	else
 747		p_dev->_irq = 1;
 748
 749	return ret;
 750} /* pcmcia_request_exclusive_irq */
 751EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
 752
 753
 754#ifdef CONFIG_PCMCIA_PROBE
 755
 756/* mask of IRQs already reserved by other cards, we should avoid using them */
 757static u8 pcmcia_used_irq[32];
 758
 759static irqreturn_t test_action(int cpl, void *dev_id)
 760{
 761	return IRQ_NONE;
 762}
 763
 764/**
 765 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
 766 * @p_dev - the associated PCMCIA device
 
 767 *
 768 * locking note: must be called with ops_mutex locked.
 769 */
 770static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
 771{
 772	struct pcmcia_socket *s = p_dev->socket;
 773	unsigned int try, irq;
 774	u32 mask = s->irq_mask;
 775	int ret = -ENODEV;
 776
 777	for (try = 0; try < 64; try++) {
 778		irq = try % 32;
 779
 780		if (irq > NR_IRQS)
 781			continue;
 782
 783		/* marked as available by driver, not blocked by userspace? */
 784		if (!((mask >> irq) & 1))
 785			continue;
 786
 787		/* avoid an IRQ which is already used by another PCMCIA card */
 788		if ((try < 32) && pcmcia_used_irq[irq])
 789			continue;
 790
 791		/* register the correct driver, if possible, to check whether
 792		 * registering a dummy handle works, i.e. if the IRQ isn't
 793		 * marked as used by the kernel resource management core */
 794		ret = request_irq(irq, test_action, type, p_dev->devname,
 795				  p_dev);
 796		if (!ret) {
 797			free_irq(irq, p_dev);
 798			p_dev->irq = s->pcmcia_irq = irq;
 799			pcmcia_used_irq[irq]++;
 800			break;
 801		}
 802	}
 803
 804	return ret;
 805}
 806
 807void pcmcia_cleanup_irq(struct pcmcia_socket *s)
 808{
 809	pcmcia_used_irq[s->pcmcia_irq]--;
 810	s->pcmcia_irq = 0;
 811}
 812
 813#else /* CONFIG_PCMCIA_PROBE */
 814
 815static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
 816{
 817	return -EINVAL;
 818}
 819
 820void pcmcia_cleanup_irq(struct pcmcia_socket *s)
 821{
 822	s->pcmcia_irq = 0;
 823	return;
 824}
 825
 826#endif  /* CONFIG_PCMCIA_PROBE */
 827
 828
 829/**
 830 * pcmcia_setup_irq() - determine IRQ to be used for device
 831 * @p_dev - the associated PCMCIA device
 832 *
 833 * locking note: must be called with ops_mutex locked.
 834 */
 835int pcmcia_setup_irq(struct pcmcia_device *p_dev)
 836{
 837	struct pcmcia_socket *s = p_dev->socket;
 838
 839	if (p_dev->irq)
 840		return 0;
 841
 842	/* already assigned? */
 843	if (s->pcmcia_irq) {
 844		p_dev->irq = s->pcmcia_irq;
 845		return 0;
 846	}
 847
 848	/* prefer an exclusive ISA irq */
 849	if (!pcmcia_setup_isa_irq(p_dev, 0))
 850		return 0;
 851
 852	/* but accept a shared ISA irq */
 853	if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
 854		return 0;
 855
 856	/* but use the PCI irq otherwise */
 857	if (s->pci_irq) {
 858		p_dev->irq = s->pcmcia_irq = s->pci_irq;
 859		return 0;
 860	}
 861
 862	return -EINVAL;
 863}
 864
 865
 866/**
 867 * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices
 868 * @p_dev: the associated PCMCIA device
 869 * @res: &struct resource pointing to p_dev->resource[2..5]
 870 * @speed: access speed
 871 *
 872 * pcmcia_request_window() attepts to reserve an iomem ranges specified in
 873 * &struct resource @res pointing to one of the entries in
 874 * &struct pcmcia_device @p_dev->resource[2..5]. The "start" value is the
 875 * requested start of the IO mem resource; "end" reflects the size
 876 * requested.
 877 */
 878int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res,
 879			unsigned int speed)
 880{
 881	struct pcmcia_socket *s = p_dev->socket;
 882	pccard_mem_map *win;
 883	u_long align;
 884	int w;
 885
 886	dev_dbg(&p_dev->dev, "request_window %pR %d\n", res, speed);
 887
 888	if (!(s->state & SOCKET_PRESENT)) {
 889		dev_dbg(&p_dev->dev, "No card present\n");
 890		return -ENODEV;
 891	}
 892
 893	/* Window size defaults to smallest available */
 894	if (res->end == 0)
 895		res->end = s->map_size;
 896	align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size;
 897	if (res->end & (s->map_size-1)) {
 898		dev_dbg(&p_dev->dev, "invalid map size\n");
 899		return -EINVAL;
 900	}
 901	if ((res->start && (s->features & SS_CAP_STATIC_MAP)) ||
 902	    (res->start & (align-1))) {
 903		dev_dbg(&p_dev->dev, "invalid base address\n");
 904		return -EINVAL;
 905	}
 906	if (res->start)
 907		align = 0;
 908
 909	/* Allocate system memory window */
 910	mutex_lock(&s->ops_mutex);
 911	for (w = 0; w < MAX_WIN; w++)
 912		if (!(s->state & SOCKET_WIN_REQ(w)))
 913			break;
 914	if (w == MAX_WIN) {
 915		dev_dbg(&p_dev->dev, "all windows are used already\n");
 916		mutex_unlock(&s->ops_mutex);
 917		return -EINVAL;
 918	}
 919
 920	win = &s->win[w];
 921
 922	if (!(s->features & SS_CAP_STATIC_MAP)) {
 923		win->res = pcmcia_find_mem_region(res->start, res->end, align,
 924						0, s);
 925		if (!win->res) {
 926			dev_dbg(&p_dev->dev, "allocating mem region failed\n");
 927			mutex_unlock(&s->ops_mutex);
 928			return -EINVAL;
 929		}
 930	}
 931	p_dev->_win |= CLIENT_WIN_REQ(w);
 932
 933	/* Configure the socket controller */
 934	win->map = w+1;
 935	win->flags = res->flags & WIN_FLAGS_MAP;
 936	win->speed = speed;
 937	win->card_start = 0;
 938
 939	if (s->ops->set_mem_map(s, win) != 0) {
 940		dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
 941		mutex_unlock(&s->ops_mutex);
 942		return -EIO;
 943	}
 944	s->state |= SOCKET_WIN_REQ(w);
 945
 946	/* Return window handle */
 947	if (s->features & SS_CAP_STATIC_MAP)
 948		res->start = win->static_start;
 949	else
 950		res->start = win->res->start;
 951
 952	/* convert to new-style resources */
 953	res->end += res->start - 1;
 954	res->flags &= ~WIN_FLAGS_REQ;
 955	res->flags |= (win->map << 2) | IORESOURCE_MEM;
 956	res->parent = win->res;
 957	if (win->res)
 958		request_resource(&iomem_resource, res);
 959
 960	dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
 961
 962	mutex_unlock(&s->ops_mutex);
 963
 964	return 0;
 965} /* pcmcia_request_window */
 966EXPORT_SYMBOL(pcmcia_request_window);
 967
 968
 969/**
 970 * pcmcia_disable_device() - disable and clean up a PCMCIA device
 971 * @p_dev: the associated PCMCIA device
 972 *
 973 * pcmcia_disable_device() is the driver-callable counterpart to
 974 * pcmcia_enable_device(): If a PCMCIA device is no longer used,
 975 * drivers are expected to clean up and disable the device by calling
 976 * this function. Any I/O ranges (iomem and ioports) will be released,
 977 * the Vpp voltage will be set to 0, and IRQs will no longer be
 978 * generated -- at least if there is no other card function (of
 979 * multifunction devices) being used.
 980 */
 981void pcmcia_disable_device(struct pcmcia_device *p_dev)
 982{
 983	int i;
 984
 985	dev_dbg(&p_dev->dev, "disabling device\n");
 986
 987	for (i = 0; i < MAX_WIN; i++) {
 988		struct resource *res = p_dev->resource[MAX_IO_WIN + i];
 989		if (res->flags & WIN_FLAGS_REQ)
 990			pcmcia_release_window(p_dev, res);
 991	}
 992
 993	pcmcia_release_configuration(p_dev);
 994	pcmcia_release_io(p_dev);
 995	if (p_dev->_irq) {
 996		free_irq(p_dev->irq, p_dev->priv);
 997		p_dev->_irq = 0;
 998	}
 999}
1000EXPORT_SYMBOL(pcmcia_disable_device);
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * PCMCIA 16-bit resource management functions
  4 *
  5 * The initial developer of the original code is David A. Hinds
  6 * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
  7 * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
  8 *
  9 * Copyright (C) 1999	     David A. Hinds
 10 * Copyright (C) 2004-2010   Dominik Brodowski
 
 
 
 
 
 11 */
 12
 13#include <linux/module.h>
 14#include <linux/kernel.h>
 15#include <linux/interrupt.h>
 16#include <linux/delay.h>
 17#include <linux/pci.h>
 18#include <linux/device.h>
 19#include <linux/netdevice.h>
 20#include <linux/slab.h>
 21
 22#include <asm/irq.h>
 23
 24#include <pcmcia/ss.h>
 25#include <pcmcia/cistpl.h>
 26#include <pcmcia/cisreg.h>
 27#include <pcmcia/ds.h>
 28
 29#include "cs_internal.h"
 30
 31
 32/* Access speed for IO windows */
 33static int io_speed;
 34module_param(io_speed, int, 0444);
 35
 36
 37int pcmcia_validate_mem(struct pcmcia_socket *s)
 38{
 39	if (s->resource_ops->validate_mem)
 40		return s->resource_ops->validate_mem(s);
 41	/* if there is no callback, we can assume that everything is OK */
 42	return 0;
 43}
 44
 45struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
 46				 int low, struct pcmcia_socket *s)
 47{
 48	if (s->resource_ops->find_mem)
 49		return s->resource_ops->find_mem(base, num, align, low, s);
 50	return NULL;
 51}
 52
 53
 54/**
 55 * release_io_space() - release IO ports allocated with alloc_io_space()
 56 * @s: pcmcia socket
 57 * @res: resource to release
 58 *
 59 */
 60static void release_io_space(struct pcmcia_socket *s, struct resource *res)
 61{
 62	resource_size_t num = resource_size(res);
 63	int i;
 64
 65	dev_dbg(&s->dev, "release_io_space for %pR\n", res);
 66
 67	for (i = 0; i < MAX_IO_WIN; i++) {
 68		if (!s->io[i].res)
 69			continue;
 70		if ((s->io[i].res->start <= res->start) &&
 71		    (s->io[i].res->end >= res->end)) {
 72			s->io[i].InUse -= num;
 73			if (res->parent)
 74				release_resource(res);
 75			res->start = res->end = 0;
 76			res->flags = IORESOURCE_IO;
 77			/* Free the window if no one else is using it */
 78			if (s->io[i].InUse == 0) {
 79				release_resource(s->io[i].res);
 80				kfree(s->io[i].res);
 81				s->io[i].res = NULL;
 82			}
 83		}
 84	}
 85}
 86
 87
 88/**
 89 * alloc_io_space() - allocate IO ports for use by a PCMCIA device
 90 * @s: pcmcia socket
 91 * @res: resource to allocate (begin: begin, end: size)
 92 * @lines: number of IO lines decoded by the PCMCIA card
 93 *
 94 * Special stuff for managing IO windows, because they are scarce
 95 */
 96static int alloc_io_space(struct pcmcia_socket *s, struct resource *res,
 97			unsigned int lines)
 98{
 99	unsigned int align;
100	unsigned int base = res->start;
101	unsigned int num = res->end;
102	int ret;
103
104	res->flags |= IORESOURCE_IO;
105
106	dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n",
107		res, lines);
108
109	align = base ? (lines ? 1<<lines : 0) : 1;
110	if (align && (align < num)) {
111		if (base) {
112			dev_dbg(&s->dev, "odd IO request\n");
113			align = 0;
114		} else
115			while (align && (align < num))
116				align <<= 1;
117	}
118	if (base & ~(align-1)) {
119		dev_dbg(&s->dev, "odd IO request\n");
120		align = 0;
121	}
122
123	ret = s->resource_ops->find_io(s, res->flags, &base, num, align,
124				&res->parent);
125	if (ret) {
126		dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret);
127		return -EINVAL;
128	}
129
130	res->start = base;
131	res->end = res->start + num - 1;
132
133	if (res->parent) {
134		ret = request_resource(res->parent, res);
135		if (ret) {
136			dev_warn(&s->dev,
137				"request_resource %pR failed: %d\n", res, ret);
138			res->parent = NULL;
139			release_io_space(s, res);
140		}
141	}
142	dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res);
143	return ret;
144}
145
146
147/*
148 * pcmcia_access_config() - read or write card configuration registers
149 *
150 * pcmcia_access_config() reads and writes configuration registers in
151 * attribute memory.  Memory window 0 is reserved for this and the tuple
152 * reading services. Drivers must use pcmcia_read_config_byte() or
153 * pcmcia_write_config_byte().
154 */
155static int pcmcia_access_config(struct pcmcia_device *p_dev,
156				off_t where, u8 *val,
157				int (*accessf) (struct pcmcia_socket *s,
158						int attr, unsigned int addr,
159						unsigned int len, void *ptr))
160{
161	struct pcmcia_socket *s;
162	config_t *c;
163	int addr;
164	int ret = 0;
165
166	s = p_dev->socket;
167
168	mutex_lock(&s->ops_mutex);
169	c = p_dev->function_config;
170
171	if (!(c->state & CONFIG_LOCKED)) {
172		dev_dbg(&p_dev->dev, "Configuration isn't locked\n");
173		mutex_unlock(&s->ops_mutex);
174		return -EACCES;
175	}
176
177	addr = (p_dev->config_base + where) >> 1;
178
179	ret = accessf(s, 1, addr, 1, val);
180
181	mutex_unlock(&s->ops_mutex);
182
183	return ret;
184}
185
186
187/*
188 * pcmcia_read_config_byte() - read a byte from a card configuration register
189 *
190 * pcmcia_read_config_byte() reads a byte from a configuration register in
191 * attribute memory.
192 */
193int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
194{
195	return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
196}
197EXPORT_SYMBOL(pcmcia_read_config_byte);
198
199
200/*
201 * pcmcia_write_config_byte() - write a byte to a card configuration register
202 *
203 * pcmcia_write_config_byte() writes a byte to a configuration register in
204 * attribute memory.
205 */
206int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
207{
208	return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
209}
210EXPORT_SYMBOL(pcmcia_write_config_byte);
211
212
213/**
214 * pcmcia_map_mem_page() - modify iomem window to point to a different offset
215 * @p_dev: pcmcia device
216 * @res: iomem resource already enabled by pcmcia_request_window()
217 * @offset: card_offset to map
218 *
219 * pcmcia_map_mem_page() modifies what can be read and written by accessing
220 * an iomem range previously enabled by pcmcia_request_window(), by setting
221 * the card_offset value to @offset.
222 */
223int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res,
224			unsigned int offset)
225{
226	struct pcmcia_socket *s = p_dev->socket;
227	unsigned int w;
228	int ret;
229
230	w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
231	if (w >= MAX_WIN)
232		return -EINVAL;
233
234	mutex_lock(&s->ops_mutex);
235	s->win[w].card_start = offset;
236	ret = s->ops->set_mem_map(s, &s->win[w]);
237	if (ret)
238		dev_warn(&p_dev->dev, "failed to set_mem_map\n");
239	mutex_unlock(&s->ops_mutex);
240	return ret;
241}
242EXPORT_SYMBOL(pcmcia_map_mem_page);
243
244
245/**
246 * pcmcia_fixup_iowidth() - reduce io width to 8bit
247 * @p_dev: pcmcia device
248 *
249 * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the
250 * IO width to 8bit after having called pcmcia_enable_device()
251 * previously.
252 */
253int pcmcia_fixup_iowidth(struct pcmcia_device *p_dev)
254{
255	struct pcmcia_socket *s = p_dev->socket;
256	pccard_io_map io_off = { 0, 0, 0, 0, 1 };
257	pccard_io_map io_on;
258	int i, ret = 0;
259
260	mutex_lock(&s->ops_mutex);
261
262	dev_dbg(&p_dev->dev, "fixup iowidth to 8bit\n");
263
264	if (!(s->state & SOCKET_PRESENT) ||
265		!(p_dev->function_config->state & CONFIG_LOCKED)) {
266		dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
267		ret = -EACCES;
268		goto unlock;
269	}
270
271	io_on.speed = io_speed;
272	for (i = 0; i < MAX_IO_WIN; i++) {
273		if (!s->io[i].res)
274			continue;
275		io_off.map = i;
276		io_on.map = i;
277
278		io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
279		io_on.start = s->io[i].res->start;
280		io_on.stop = s->io[i].res->end;
281
282		s->ops->set_io_map(s, &io_off);
283		msleep(40);
284		s->ops->set_io_map(s, &io_on);
285	}
286unlock:
287	mutex_unlock(&s->ops_mutex);
288
289	return ret;
290}
291EXPORT_SYMBOL(pcmcia_fixup_iowidth);
292
293
294/**
295 * pcmcia_fixup_vpp() - set Vpp to a new voltage level
296 * @p_dev: pcmcia device
297 * @new_vpp: new Vpp voltage
298 *
299 * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to
300 * a new voltage level between calls to pcmcia_enable_device()
301 * and pcmcia_disable_device().
302 */
303int pcmcia_fixup_vpp(struct pcmcia_device *p_dev, unsigned char new_vpp)
304{
305	struct pcmcia_socket *s = p_dev->socket;
306	int ret = 0;
307
308	mutex_lock(&s->ops_mutex);
309
310	dev_dbg(&p_dev->dev, "fixup Vpp to %d\n", new_vpp);
311
312	if (!(s->state & SOCKET_PRESENT) ||
313		!(p_dev->function_config->state & CONFIG_LOCKED)) {
314		dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
315		ret = -EACCES;
316		goto unlock;
317	}
318
319	s->socket.Vpp = new_vpp;
320	if (s->ops->set_socket(s, &s->socket)) {
321		dev_warn(&p_dev->dev, "Unable to set VPP\n");
322		ret = -EIO;
323		goto unlock;
324	}
325	p_dev->vpp = new_vpp;
326
327unlock:
328	mutex_unlock(&s->ops_mutex);
329
330	return ret;
331}
332EXPORT_SYMBOL(pcmcia_fixup_vpp);
333
334
335/**
336 * pcmcia_release_configuration() - physically disable a PCMCIA device
337 * @p_dev: pcmcia device
338 *
339 * pcmcia_release_configuration() is the 1:1 counterpart to
340 * pcmcia_enable_device(): If a PCMCIA device is no longer used by any
341 * driver, the Vpp voltage is set to 0, IRQs will no longer be generated,
342 * and I/O ranges will be disabled. As pcmcia_release_io() and
343 * pcmcia_release_window() still need to be called, device drivers are
344 * expected to call pcmcia_disable_device() instead.
345 */
346int pcmcia_release_configuration(struct pcmcia_device *p_dev)
347{
348	pccard_io_map io = { 0, 0, 0, 0, 1 };
349	struct pcmcia_socket *s = p_dev->socket;
350	config_t *c;
351	int i;
352
353	mutex_lock(&s->ops_mutex);
354	c = p_dev->function_config;
355	if (p_dev->_locked) {
356		p_dev->_locked = 0;
357		if (--(s->lock_count) == 0) {
358			s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
359			s->socket.Vpp = 0;
360			s->socket.io_irq = 0;
361			s->ops->set_socket(s, &s->socket);
362		}
363	}
364	if (c->state & CONFIG_LOCKED) {
365		c->state &= ~CONFIG_LOCKED;
366		if (c->state & CONFIG_IO_REQ)
367			for (i = 0; i < MAX_IO_WIN; i++) {
368				if (!s->io[i].res)
369					continue;
370				s->io[i].Config--;
371				if (s->io[i].Config != 0)
372					continue;
373				io.map = i;
374				s->ops->set_io_map(s, &io);
375			}
376	}
377	mutex_unlock(&s->ops_mutex);
378
379	return 0;
380}
381
382
383/**
384 * pcmcia_release_io() - release I/O allocated by a PCMCIA device
385 * @p_dev: pcmcia device
386 *
387 * pcmcia_release_io() releases the I/O ranges allocated by a PCMCIA
388 * device.  This may be invoked some time after a card ejection has
389 * already dumped the actual socket configuration, so if the client is
390 * "stale", we don't bother checking the port ranges against the
391 * current socket values.
392 */
393static void pcmcia_release_io(struct pcmcia_device *p_dev)
394{
395	struct pcmcia_socket *s = p_dev->socket;
 
396	config_t *c;
397
398	mutex_lock(&s->ops_mutex);
399	if (!p_dev->_io)
400		goto out;
401
402	c = p_dev->function_config;
403
404	release_io_space(s, &c->io[0]);
405
406	if (c->io[1].end)
407		release_io_space(s, &c->io[1]);
408
409	p_dev->_io = 0;
410	c->state &= ~CONFIG_IO_REQ;
411
412out:
413	mutex_unlock(&s->ops_mutex);
 
 
414} /* pcmcia_release_io */
415
416
417/**
418 * pcmcia_release_window() - release reserved iomem for PCMCIA devices
419 * @p_dev: pcmcia device
420 * @res: iomem resource to release
421 *
422 * pcmcia_release_window() releases &struct resource *res which was
423 * previously reserved by calling pcmcia_request_window().
424 */
425int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
426{
427	struct pcmcia_socket *s = p_dev->socket;
428	pccard_mem_map *win;
429	unsigned int w;
430
431	dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
432
433	w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
434	if (w >= MAX_WIN)
435		return -EINVAL;
436
437	mutex_lock(&s->ops_mutex);
438	win = &s->win[w];
439
440	if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
441		dev_dbg(&p_dev->dev, "not releasing unknown window\n");
442		mutex_unlock(&s->ops_mutex);
443		return -EINVAL;
444	}
445
446	/* Shut down memory window */
447	win->flags &= ~MAP_ACTIVE;
448	s->ops->set_mem_map(s, win);
449	s->state &= ~SOCKET_WIN_REQ(w);
450
451	/* Release system memory */
452	if (win->res) {
453		release_resource(res);
454		release_resource(win->res);
455		kfree(win->res);
456		win->res = NULL;
457	}
458	res->start = res->end = 0;
459	res->flags = IORESOURCE_MEM;
460	p_dev->_win &= ~CLIENT_WIN_REQ(w);
461	mutex_unlock(&s->ops_mutex);
462
463	return 0;
464} /* pcmcia_release_window */
465EXPORT_SYMBOL(pcmcia_release_window);
466
467
468/**
469 * pcmcia_enable_device() - set up and activate a PCMCIA device
470 * @p_dev: the associated PCMCIA device
471 *
472 * pcmcia_enable_device() physically enables a PCMCIA device. It parses
473 * the flags passed to in @flags and stored in @p_dev->flags and sets up
474 * the Vpp voltage, enables the speaker line, I/O ports and store proper
475 * values to configuration registers.
476 */
477int pcmcia_enable_device(struct pcmcia_device *p_dev)
478{
479	int i;
480	unsigned int base;
481	struct pcmcia_socket *s = p_dev->socket;
482	config_t *c;
483	pccard_io_map iomap;
484	unsigned char status = 0;
485	unsigned char ext_status = 0;
486	unsigned char option = 0;
487	unsigned int flags = p_dev->config_flags;
488
489	if (!(s->state & SOCKET_PRESENT))
490		return -ENODEV;
491
492	mutex_lock(&s->ops_mutex);
493	c = p_dev->function_config;
494	if (c->state & CONFIG_LOCKED) {
495		mutex_unlock(&s->ops_mutex);
496		dev_dbg(&p_dev->dev, "Configuration is locked\n");
497		return -EACCES;
498	}
499
500	/* Do power control.  We don't allow changes in Vcc. */
501	s->socket.Vpp = p_dev->vpp;
502	if (s->ops->set_socket(s, &s->socket)) {
503		mutex_unlock(&s->ops_mutex);
504		dev_warn(&p_dev->dev, "Unable to set socket state\n");
 
505		return -EINVAL;
506	}
507
508	/* Pick memory or I/O card, DMA mode, interrupt */
509	if (p_dev->_io || flags & CONF_ENABLE_IRQ)
510		flags |= CONF_ENABLE_IOCARD;
511	if (flags & CONF_ENABLE_IOCARD)
512		s->socket.flags |= SS_IOCARD;
513	if (flags & CONF_ENABLE_ZVCARD)
514		s->socket.flags |= SS_ZVCARD | SS_IOCARD;
515	if (flags & CONF_ENABLE_SPKR) {
516		s->socket.flags |= SS_SPKR_ENA;
517		status = CCSR_AUDIO_ENA;
518		if (!(p_dev->config_regs & PRESENT_STATUS))
519			dev_warn(&p_dev->dev, "speaker requested, but "
520					      "PRESENT_STATUS not set!\n");
521	}
522	if (flags & CONF_ENABLE_IRQ)
523		s->socket.io_irq = s->pcmcia_irq;
524	else
525		s->socket.io_irq = 0;
526	if (flags & CONF_ENABLE_ESR) {
527		p_dev->config_regs |= PRESENT_EXT_STATUS;
528		ext_status = ESR_REQ_ATTN_ENA;
529	}
530	s->ops->set_socket(s, &s->socket);
531	s->lock_count++;
532
533	dev_dbg(&p_dev->dev,
534		"enable_device: V %d, flags %x, base %x, regs %x, idx %x\n",
535		p_dev->vpp, flags, p_dev->config_base, p_dev->config_regs,
536		p_dev->config_index);
537
538	/* Set up CIS configuration registers */
539	base = p_dev->config_base;
540	if (p_dev->config_regs & PRESENT_COPY) {
541		u16 tmp = 0;
542		dev_dbg(&p_dev->dev, "clearing CISREG_SCR\n");
543		pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &tmp);
544	}
545	if (p_dev->config_regs & PRESENT_PIN_REPLACE) {
546		u16 tmp = 0;
547		dev_dbg(&p_dev->dev, "clearing CISREG_PRR\n");
548		pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &tmp);
549	}
550	if (p_dev->config_regs & PRESENT_OPTION) {
551		if (s->functions == 1) {
552			option = p_dev->config_index & COR_CONFIG_MASK;
553		} else {
554			option = p_dev->config_index & COR_MFC_CONFIG_MASK;
555			option |= COR_FUNC_ENA|COR_IREQ_ENA;
556			if (p_dev->config_regs & PRESENT_IOBASE_0)
557				option |= COR_ADDR_DECODE;
558		}
559		if ((flags & CONF_ENABLE_IRQ) &&
560			!(flags & CONF_ENABLE_PULSE_IRQ))
561			option |= COR_LEVEL_REQ;
562		pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &option);
563		msleep(40);
564	}
565	if (p_dev->config_regs & PRESENT_STATUS)
566		pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &status);
567
568	if (p_dev->config_regs & PRESENT_EXT_STATUS)
569		pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1,
570					&ext_status);
571
572	if (p_dev->config_regs & PRESENT_IOBASE_0) {
573		u8 b = c->io[0].start & 0xff;
574		pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
575		b = (c->io[0].start >> 8) & 0xff;
576		pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
577	}
578	if (p_dev->config_regs & PRESENT_IOSIZE) {
579		u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
580		pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
581	}
582
583	/* Configure I/O windows */
584	if (c->state & CONFIG_IO_REQ) {
585		iomap.speed = io_speed;
586		for (i = 0; i < MAX_IO_WIN; i++)
587			if (s->io[i].res) {
588				iomap.map = i;
589				iomap.flags = MAP_ACTIVE;
590				switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
591				case IO_DATA_PATH_WIDTH_16:
592					iomap.flags |= MAP_16BIT; break;
593				case IO_DATA_PATH_WIDTH_AUTO:
594					iomap.flags |= MAP_AUTOSZ; break;
595				default:
596					break;
597				}
598				iomap.start = s->io[i].res->start;
599				iomap.stop = s->io[i].res->end;
600				s->ops->set_io_map(s, &iomap);
601				s->io[i].Config++;
602			}
603	}
604
605	c->state |= CONFIG_LOCKED;
606	p_dev->_locked = 1;
607	mutex_unlock(&s->ops_mutex);
608	return 0;
609} /* pcmcia_enable_device */
610EXPORT_SYMBOL(pcmcia_enable_device);
611
612
613/**
614 * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
615 * @p_dev: the associated PCMCIA device
616 *
617 * pcmcia_request_io() attempts to reserve the IO port ranges specified in
618 * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
619 * "start" value is the requested start of the IO port resource; "end"
620 * reflects the number of ports requested. The number of IO lines requested
621 * is specified in &struct pcmcia_device @p_dev->io_lines.
622 */
623int pcmcia_request_io(struct pcmcia_device *p_dev)
624{
625	struct pcmcia_socket *s = p_dev->socket;
626	config_t *c = p_dev->function_config;
627	int ret = -EINVAL;
628
629	mutex_lock(&s->ops_mutex);
630	dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
631		&c->io[0], &c->io[1]);
632
633	if (!(s->state & SOCKET_PRESENT)) {
634		dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
635		goto out;
636	}
637
638	if (c->state & CONFIG_LOCKED) {
639		dev_dbg(&p_dev->dev, "Configuration is locked\n");
640		goto out;
641	}
642	if (c->state & CONFIG_IO_REQ) {
643		dev_dbg(&p_dev->dev, "IO already configured\n");
644		goto out;
645	}
646
647	ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
648	if (ret)
649		goto out;
650
651	if (c->io[1].end) {
652		ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
653		if (ret) {
654			struct resource tmp = c->io[0];
655			/* release the previously allocated resource */
656			release_io_space(s, &c->io[0]);
657			/* but preserve the settings, for they worked... */
658			c->io[0].end = resource_size(&tmp);
659			c->io[0].start = tmp.start;
660			c->io[0].flags = tmp.flags;
661			goto out;
662		}
663	} else
664		c->io[1].start = 0;
665
666	c->state |= CONFIG_IO_REQ;
667	p_dev->_io = 1;
668
669	dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
670		&c->io[0], &c->io[1]);
671out:
672	mutex_unlock(&s->ops_mutex);
673
674	return ret;
675} /* pcmcia_request_io */
676EXPORT_SYMBOL(pcmcia_request_io);
677
678
679/**
680 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
681 * @p_dev: the associated PCMCIA device
682 * @handler: IRQ handler to register
683 *
684 * pcmcia_request_irq() is a wrapper around request_irq() which allows
685 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
686 * Drivers are free to use request_irq() directly, but then they need to
687 * call free_irq() themselves, too. Also, only %IRQF_SHARED capable IRQ
688 * handlers are allowed.
689 */
690int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
691				    irq_handler_t handler)
692{
693	int ret;
694
695	if (!p_dev->irq)
696		return -EINVAL;
697
698	ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
699			p_dev->devname, p_dev->priv);
700	if (!ret)
701		p_dev->_irq = 1;
702
703	return ret;
704}
705EXPORT_SYMBOL(pcmcia_request_irq);
706
707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
708#ifdef CONFIG_PCMCIA_PROBE
709
710/* mask of IRQs already reserved by other cards, we should avoid using them */
711static u8 pcmcia_used_irq[32];
712
713static irqreturn_t test_action(int cpl, void *dev_id)
714{
715	return IRQ_NONE;
716}
717
718/**
719 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
720 * @p_dev: the associated PCMCIA device
721 * @type:  IRQ type (flags)
722 *
723 * locking note: must be called with ops_mutex locked.
724 */
725static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
726{
727	struct pcmcia_socket *s = p_dev->socket;
728	unsigned int try, irq;
729	u32 mask = s->irq_mask;
730	int ret = -ENODEV;
731
732	for (try = 0; try < 64; try++) {
733		irq = try % 32;
734
735		if (irq > NR_IRQS)
736			continue;
737
738		/* marked as available by driver, not blocked by userspace? */
739		if (!((mask >> irq) & 1))
740			continue;
741
742		/* avoid an IRQ which is already used by another PCMCIA card */
743		if ((try < 32) && pcmcia_used_irq[irq])
744			continue;
745
746		/* register the correct driver, if possible, to check whether
747		 * registering a dummy handle works, i.e. if the IRQ isn't
748		 * marked as used by the kernel resource management core */
749		ret = request_irq(irq, test_action, type, p_dev->devname,
750				  p_dev);
751		if (!ret) {
752			free_irq(irq, p_dev);
753			p_dev->irq = s->pcmcia_irq = irq;
754			pcmcia_used_irq[irq]++;
755			break;
756		}
757	}
758
759	return ret;
760}
761
762void pcmcia_cleanup_irq(struct pcmcia_socket *s)
763{
764	pcmcia_used_irq[s->pcmcia_irq]--;
765	s->pcmcia_irq = 0;
766}
767
768#else /* CONFIG_PCMCIA_PROBE */
769
770static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
771{
772	return -EINVAL;
773}
774
775void pcmcia_cleanup_irq(struct pcmcia_socket *s)
776{
777	s->pcmcia_irq = 0;
778	return;
779}
780
781#endif  /* CONFIG_PCMCIA_PROBE */
782
783
784/**
785 * pcmcia_setup_irq() - determine IRQ to be used for device
786 * @p_dev: the associated PCMCIA device
787 *
788 * locking note: must be called with ops_mutex locked.
789 */
790int pcmcia_setup_irq(struct pcmcia_device *p_dev)
791{
792	struct pcmcia_socket *s = p_dev->socket;
793
794	if (p_dev->irq)
795		return 0;
796
797	/* already assigned? */
798	if (s->pcmcia_irq) {
799		p_dev->irq = s->pcmcia_irq;
800		return 0;
801	}
802
803	/* prefer an exclusive ISA irq */
804	if (!pcmcia_setup_isa_irq(p_dev, 0))
805		return 0;
806
807	/* but accept a shared ISA irq */
808	if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
809		return 0;
810
811	/* but use the PCI irq otherwise */
812	if (s->pci_irq) {
813		p_dev->irq = s->pcmcia_irq = s->pci_irq;
814		return 0;
815	}
816
817	return -EINVAL;
818}
819
820
821/**
822 * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices
823 * @p_dev: the associated PCMCIA device
824 * @res: &struct resource pointing to p_dev->resource[2..5]
825 * @speed: access speed
826 *
827 * pcmcia_request_window() attepts to reserve an iomem ranges specified in
828 * &struct resource @res pointing to one of the entries in
829 * &struct pcmcia_device @p_dev->resource[2..5]. The "start" value is the
830 * requested start of the IO mem resource; "end" reflects the size
831 * requested.
832 */
833int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res,
834			unsigned int speed)
835{
836	struct pcmcia_socket *s = p_dev->socket;
837	pccard_mem_map *win;
838	u_long align;
839	int w;
840
841	dev_dbg(&p_dev->dev, "request_window %pR %d\n", res, speed);
842
843	if (!(s->state & SOCKET_PRESENT)) {
844		dev_dbg(&p_dev->dev, "No card present\n");
845		return -ENODEV;
846	}
847
848	/* Window size defaults to smallest available */
849	if (res->end == 0)
850		res->end = s->map_size;
851	align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size;
852	if (res->end & (s->map_size-1)) {
853		dev_dbg(&p_dev->dev, "invalid map size\n");
854		return -EINVAL;
855	}
856	if ((res->start && (s->features & SS_CAP_STATIC_MAP)) ||
857	    (res->start & (align-1))) {
858		dev_dbg(&p_dev->dev, "invalid base address\n");
859		return -EINVAL;
860	}
861	if (res->start)
862		align = 0;
863
864	/* Allocate system memory window */
865	mutex_lock(&s->ops_mutex);
866	for (w = 0; w < MAX_WIN; w++)
867		if (!(s->state & SOCKET_WIN_REQ(w)))
868			break;
869	if (w == MAX_WIN) {
870		dev_dbg(&p_dev->dev, "all windows are used already\n");
871		mutex_unlock(&s->ops_mutex);
872		return -EINVAL;
873	}
874
875	win = &s->win[w];
876
877	if (!(s->features & SS_CAP_STATIC_MAP)) {
878		win->res = pcmcia_find_mem_region(res->start, res->end, align,
879						0, s);
880		if (!win->res) {
881			dev_dbg(&p_dev->dev, "allocating mem region failed\n");
882			mutex_unlock(&s->ops_mutex);
883			return -EINVAL;
884		}
885	}
886	p_dev->_win |= CLIENT_WIN_REQ(w);
887
888	/* Configure the socket controller */
889	win->map = w+1;
890	win->flags = res->flags & WIN_FLAGS_MAP;
891	win->speed = speed;
892	win->card_start = 0;
893
894	if (s->ops->set_mem_map(s, win) != 0) {
895		dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
896		mutex_unlock(&s->ops_mutex);
897		return -EIO;
898	}
899	s->state |= SOCKET_WIN_REQ(w);
900
901	/* Return window handle */
902	if (s->features & SS_CAP_STATIC_MAP)
903		res->start = win->static_start;
904	else
905		res->start = win->res->start;
906
907	/* convert to new-style resources */
908	res->end += res->start - 1;
909	res->flags &= ~WIN_FLAGS_REQ;
910	res->flags |= (win->map << 2) | IORESOURCE_MEM;
911	res->parent = win->res;
912	if (win->res)
913		request_resource(&iomem_resource, res);
914
915	dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
916
917	mutex_unlock(&s->ops_mutex);
918
919	return 0;
920} /* pcmcia_request_window */
921EXPORT_SYMBOL(pcmcia_request_window);
922
923
924/**
925 * pcmcia_disable_device() - disable and clean up a PCMCIA device
926 * @p_dev: the associated PCMCIA device
927 *
928 * pcmcia_disable_device() is the driver-callable counterpart to
929 * pcmcia_enable_device(): If a PCMCIA device is no longer used,
930 * drivers are expected to clean up and disable the device by calling
931 * this function. Any I/O ranges (iomem and ioports) will be released,
932 * the Vpp voltage will be set to 0, and IRQs will no longer be
933 * generated -- at least if there is no other card function (of
934 * multifunction devices) being used.
935 */
936void pcmcia_disable_device(struct pcmcia_device *p_dev)
937{
938	int i;
939
940	dev_dbg(&p_dev->dev, "disabling device\n");
941
942	for (i = 0; i < MAX_WIN; i++) {
943		struct resource *res = p_dev->resource[MAX_IO_WIN + i];
944		if (res->flags & WIN_FLAGS_REQ)
945			pcmcia_release_window(p_dev, res);
946	}
947
948	pcmcia_release_configuration(p_dev);
949	pcmcia_release_io(p_dev);
950	if (p_dev->_irq) {
951		free_irq(p_dev->irq, p_dev->priv);
952		p_dev->_irq = 0;
953	}
954}
955EXPORT_SYMBOL(pcmcia_disable_device);