pcmcia: use pcmica_{read,write}_config_byte
[deliverable/linux.git] / drivers / pcmcia / pcmcia_resource.c
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-2005 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/cs.h>
30 #include <pcmcia/cistpl.h>
31 #include <pcmcia/cisreg.h>
32 #include <pcmcia/ds.h>
33
34 #include "cs_internal.h"
35
36
37 /* Access speed for IO windows */
38 static int io_speed;
39 module_param(io_speed, int, 0444);
40
41
42 int pcmcia_validate_mem(struct pcmcia_socket *s)
43 {
44 if (s->resource_ops->validate_mem)
45 return s->resource_ops->validate_mem(s);
46 /* if there is no callback, we can assume that everything is OK */
47 return 0;
48 }
49
50 struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
51 int low, struct pcmcia_socket *s)
52 {
53 if (s->resource_ops->find_mem)
54 return s->resource_ops->find_mem(base, num, align, low, s);
55 return NULL;
56 }
57
58
59 /** alloc_io_space
60 *
61 * Special stuff for managing IO windows, because they are scarce
62 */
63
64 static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
65 unsigned int *base, unsigned int num, u_int lines)
66 {
67 unsigned int align;
68
69 align = (*base) ? (lines ? 1<<lines : 0) : 1;
70 if (align && (align < num)) {
71 if (*base) {
72 dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
73 num, align);
74 align = 0;
75 } else
76 while (align && (align < num))
77 align <<= 1;
78 }
79 if (*base & ~(align-1)) {
80 dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
81 *base, align);
82 align = 0;
83 }
84
85 return s->resource_ops->find_io(s, attr, base, num, align);
86 } /* alloc_io_space */
87
88
89 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
90 unsigned int num)
91 {
92 int i;
93
94 for (i = 0; i < MAX_IO_WIN; i++) {
95 if (!s->io[i].res)
96 continue;
97 if ((s->io[i].res->start <= base) &&
98 (s->io[i].res->end >= base+num-1)) {
99 s->io[i].InUse -= num;
100 /* Free the window if no one else is using it */
101 if (s->io[i].InUse == 0) {
102 release_resource(s->io[i].res);
103 kfree(s->io[i].res);
104 s->io[i].res = NULL;
105 }
106 }
107 }
108 } /* release_io_space */
109
110
111 /**
112 * pcmcia_access_config() - read or write card configuration registers
113 *
114 * pcmcia_access_config() reads and writes configuration registers in
115 * attribute memory. Memory window 0 is reserved for this and the tuple
116 * reading services. Drivers must use pcmcia_read_config_byte() or
117 * pcmcia_write_config_byte().
118 */
119 static int pcmcia_access_config(struct pcmcia_device *p_dev,
120 off_t where, u8 *val,
121 int (*accessf) (struct pcmcia_socket *s,
122 int attr, unsigned int addr,
123 unsigned int len, void *ptr))
124 {
125 struct pcmcia_socket *s;
126 config_t *c;
127 int addr;
128 int ret = 0;
129
130 s = p_dev->socket;
131
132 mutex_lock(&s->ops_mutex);
133 c = p_dev->function_config;
134
135 if (!(c->state & CONFIG_LOCKED)) {
136 dev_dbg(&s->dev, "Configuration isnt't locked\n");
137 mutex_unlock(&s->ops_mutex);
138 return -EACCES;
139 }
140
141 addr = (c->ConfigBase + where) >> 1;
142
143 ret = accessf(s, 1, addr, 1, val);
144
145 mutex_unlock(&s->ops_mutex);
146
147 return ret;
148 } /* pcmcia_access_config */
149
150
151 /**
152 * pcmcia_read_config_byte() - read a byte from a card configuration register
153 *
154 * pcmcia_read_config_byte() reads a byte from a configuration register in
155 * attribute memory.
156 */
157 int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
158 {
159 return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
160 }
161 EXPORT_SYMBOL(pcmcia_read_config_byte);
162
163
164 /**
165 * pcmcia_write_config_byte() - write a byte to a card configuration register
166 *
167 * pcmcia_write_config_byte() writes a byte to a configuration register in
168 * attribute memory.
169 */
170 int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
171 {
172 return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
173 }
174 EXPORT_SYMBOL(pcmcia_write_config_byte);
175
176
177 int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
178 memreq_t *req)
179 {
180 struct pcmcia_socket *s = p_dev->socket;
181 int ret;
182
183 wh--;
184 if (wh >= MAX_WIN)
185 return -EINVAL;
186 if (req->Page != 0) {
187 dev_dbg(&s->dev, "failure: requested page is zero\n");
188 return -EINVAL;
189 }
190 mutex_lock(&s->ops_mutex);
191 s->win[wh].card_start = req->CardOffset;
192 ret = s->ops->set_mem_map(s, &s->win[wh]);
193 if (ret)
194 dev_warn(&s->dev, "failed to set_mem_map\n");
195 mutex_unlock(&s->ops_mutex);
196 return ret;
197 } /* pcmcia_map_mem_page */
198 EXPORT_SYMBOL(pcmcia_map_mem_page);
199
200
201 /** pcmcia_modify_configuration
202 *
203 * Modify a locked socket configuration
204 */
205 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
206 modconf_t *mod)
207 {
208 struct pcmcia_socket *s;
209 config_t *c;
210 int ret;
211
212 s = p_dev->socket;
213
214 mutex_lock(&s->ops_mutex);
215 c = p_dev->function_config;
216
217 if (!(s->state & SOCKET_PRESENT)) {
218 dev_dbg(&s->dev, "No card present\n");
219 ret = -ENODEV;
220 goto unlock;
221 }
222 if (!(c->state & CONFIG_LOCKED)) {
223 dev_dbg(&s->dev, "Configuration isnt't locked\n");
224 ret = -EACCES;
225 goto unlock;
226 }
227
228 if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) {
229 dev_dbg(&s->dev,
230 "changing Vcc or IRQ is not allowed at this time\n");
231 ret = -EINVAL;
232 goto unlock;
233 }
234
235 /* We only allow changing Vpp1 and Vpp2 to the same value */
236 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
237 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
238 if (mod->Vpp1 != mod->Vpp2) {
239 dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
240 ret = -EINVAL;
241 goto unlock;
242 }
243 s->socket.Vpp = mod->Vpp1;
244 if (s->ops->set_socket(s, &s->socket)) {
245 dev_printk(KERN_WARNING, &s->dev,
246 "Unable to set VPP\n");
247 ret = -EIO;
248 goto unlock;
249 }
250 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
251 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
252 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
253 ret = -EINVAL;
254 goto unlock;
255 }
256
257 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
258 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
259 pccard_io_map io_on;
260 int i;
261
262 io_on.speed = io_speed;
263 for (i = 0; i < MAX_IO_WIN; i++) {
264 if (!s->io[i].res)
265 continue;
266 io_off.map = i;
267 io_on.map = i;
268
269 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
270 io_on.start = s->io[i].res->start;
271 io_on.stop = s->io[i].res->end;
272
273 s->ops->set_io_map(s, &io_off);
274 mdelay(40);
275 s->ops->set_io_map(s, &io_on);
276 }
277 }
278 ret = 0;
279 unlock:
280 mutex_unlock(&s->ops_mutex);
281
282 return ret;
283 } /* modify_configuration */
284 EXPORT_SYMBOL(pcmcia_modify_configuration);
285
286
287 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
288 {
289 pccard_io_map io = { 0, 0, 0, 0, 1 };
290 struct pcmcia_socket *s = p_dev->socket;
291 config_t *c;
292 int i;
293
294 mutex_lock(&s->ops_mutex);
295 c = p_dev->function_config;
296 if (p_dev->_locked) {
297 p_dev->_locked = 0;
298 if (--(s->lock_count) == 0) {
299 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
300 s->socket.Vpp = 0;
301 s->socket.io_irq = 0;
302 s->ops->set_socket(s, &s->socket);
303 }
304 }
305 if (c->state & CONFIG_LOCKED) {
306 c->state &= ~CONFIG_LOCKED;
307 if (c->state & CONFIG_IO_REQ)
308 for (i = 0; i < MAX_IO_WIN; i++) {
309 if (!s->io[i].res)
310 continue;
311 s->io[i].Config--;
312 if (s->io[i].Config != 0)
313 continue;
314 io.map = i;
315 s->ops->set_io_map(s, &io);
316 }
317 }
318 mutex_unlock(&s->ops_mutex);
319
320 return 0;
321 } /* pcmcia_release_configuration */
322
323
324 /** pcmcia_release_io
325 *
326 * Release_io() releases the I/O ranges allocated by a client. This
327 * may be invoked some time after a card ejection has already dumped
328 * the actual socket configuration, so if the client is "stale", we
329 * don't bother checking the port ranges against the current socket
330 * values.
331 */
332 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
333 {
334 struct pcmcia_socket *s = p_dev->socket;
335 int ret = -EINVAL;
336 config_t *c;
337
338 mutex_lock(&s->ops_mutex);
339 c = p_dev->function_config;
340
341 if (!p_dev->_io)
342 goto out;
343
344 p_dev->_io = 0;
345
346 if ((c->io.BasePort1 != req->BasePort1) ||
347 (c->io.NumPorts1 != req->NumPorts1) ||
348 (c->io.BasePort2 != req->BasePort2) ||
349 (c->io.NumPorts2 != req->NumPorts2))
350 goto out;
351
352 c->state &= ~CONFIG_IO_REQ;
353
354 release_io_space(s, req->BasePort1, req->NumPorts1);
355 if (req->NumPorts2)
356 release_io_space(s, req->BasePort2, req->NumPorts2);
357
358 out:
359 mutex_unlock(&s->ops_mutex);
360
361 return ret;
362 } /* pcmcia_release_io */
363
364
365 int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
366 {
367 struct pcmcia_socket *s = p_dev->socket;
368 pccard_mem_map *win;
369
370 wh--;
371 if (wh >= MAX_WIN)
372 return -EINVAL;
373
374 mutex_lock(&s->ops_mutex);
375 win = &s->win[wh];
376
377 if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
378 dev_dbg(&s->dev, "not releasing unknown window\n");
379 mutex_unlock(&s->ops_mutex);
380 return -EINVAL;
381 }
382
383 /* Shut down memory window */
384 win->flags &= ~MAP_ACTIVE;
385 s->ops->set_mem_map(s, win);
386 s->state &= ~SOCKET_WIN_REQ(wh);
387
388 /* Release system memory */
389 if (win->res) {
390 release_resource(win->res);
391 kfree(win->res);
392 win->res = NULL;
393 }
394 p_dev->_win &= ~CLIENT_WIN_REQ(wh);
395 mutex_unlock(&s->ops_mutex);
396
397 return 0;
398 } /* pcmcia_release_window */
399 EXPORT_SYMBOL(pcmcia_release_window);
400
401
402 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
403 config_req_t *req)
404 {
405 int i;
406 u_int base;
407 struct pcmcia_socket *s = p_dev->socket;
408 config_t *c;
409 pccard_io_map iomap;
410
411 if (!(s->state & SOCKET_PRESENT))
412 return -ENODEV;
413
414 if (req->IntType & INT_CARDBUS) {
415 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
416 return -EINVAL;
417 }
418
419 mutex_lock(&s->ops_mutex);
420 c = p_dev->function_config;
421 if (c->state & CONFIG_LOCKED) {
422 mutex_unlock(&s->ops_mutex);
423 dev_dbg(&s->dev, "Configuration is locked\n");
424 return -EACCES;
425 }
426
427 /* Do power control. We don't allow changes in Vcc. */
428 s->socket.Vpp = req->Vpp;
429 if (s->ops->set_socket(s, &s->socket)) {
430 mutex_unlock(&s->ops_mutex);
431 dev_printk(KERN_WARNING, &s->dev,
432 "Unable to set socket state\n");
433 return -EINVAL;
434 }
435
436 /* Pick memory or I/O card, DMA mode, interrupt */
437 c->IntType = req->IntType;
438 c->Attributes = req->Attributes;
439 if (req->IntType & INT_MEMORY_AND_IO)
440 s->socket.flags |= SS_IOCARD;
441 if (req->IntType & INT_ZOOMED_VIDEO)
442 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
443 if (req->Attributes & CONF_ENABLE_DMA)
444 s->socket.flags |= SS_DMA_MODE;
445 if (req->Attributes & CONF_ENABLE_SPKR)
446 s->socket.flags |= SS_SPKR_ENA;
447 if (req->Attributes & CONF_ENABLE_IRQ)
448 s->socket.io_irq = s->pcmcia_irq;
449 else
450 s->socket.io_irq = 0;
451 s->ops->set_socket(s, &s->socket);
452 s->lock_count++;
453
454 /* Set up CIS configuration registers */
455 base = c->ConfigBase = req->ConfigBase;
456 c->CardValues = req->Present;
457 if (req->Present & PRESENT_COPY) {
458 c->Copy = req->Copy;
459 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
460 }
461 if (req->Present & PRESENT_OPTION) {
462 if (s->functions == 1) {
463 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
464 } else {
465 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
466 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
467 if (req->Present & PRESENT_IOBASE_0)
468 c->Option |= COR_ADDR_DECODE;
469 }
470 if ((req->Attributes & CONF_ENABLE_IRQ) &&
471 !(req->Attributes & CONF_ENABLE_PULSE_IRQ))
472 c->Option |= COR_LEVEL_REQ;
473 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
474 mdelay(40);
475 }
476 if (req->Present & PRESENT_STATUS) {
477 c->Status = req->Status;
478 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
479 }
480 if (req->Present & PRESENT_PIN_REPLACE) {
481 c->Pin = req->Pin;
482 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
483 }
484 if (req->Present & PRESENT_EXT_STATUS) {
485 c->ExtStatus = req->ExtStatus;
486 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
487 }
488 if (req->Present & PRESENT_IOBASE_0) {
489 u_char b = c->io.BasePort1 & 0xff;
490 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
491 b = (c->io.BasePort1 >> 8) & 0xff;
492 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
493 }
494 if (req->Present & PRESENT_IOSIZE) {
495 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
496 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
497 }
498
499 /* Configure I/O windows */
500 if (c->state & CONFIG_IO_REQ) {
501 iomap.speed = io_speed;
502 for (i = 0; i < MAX_IO_WIN; i++)
503 if (s->io[i].res) {
504 iomap.map = i;
505 iomap.flags = MAP_ACTIVE;
506 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
507 case IO_DATA_PATH_WIDTH_16:
508 iomap.flags |= MAP_16BIT; break;
509 case IO_DATA_PATH_WIDTH_AUTO:
510 iomap.flags |= MAP_AUTOSZ; break;
511 default:
512 break;
513 }
514 iomap.start = s->io[i].res->start;
515 iomap.stop = s->io[i].res->end;
516 s->ops->set_io_map(s, &iomap);
517 s->io[i].Config++;
518 }
519 }
520
521 c->state |= CONFIG_LOCKED;
522 p_dev->_locked = 1;
523 mutex_unlock(&s->ops_mutex);
524 return 0;
525 } /* pcmcia_request_configuration */
526 EXPORT_SYMBOL(pcmcia_request_configuration);
527
528
529 /** pcmcia_request_io
530 *
531 * Request_io() reserves ranges of port addresses for a socket.
532 * I have not implemented range sharing or alias addressing.
533 */
534 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
535 {
536 struct pcmcia_socket *s = p_dev->socket;
537 config_t *c;
538 int ret = -EINVAL;
539
540 mutex_lock(&s->ops_mutex);
541
542 if (!(s->state & SOCKET_PRESENT)) {
543 dev_dbg(&s->dev, "No card present\n");
544 goto out;
545 }
546
547 if (!req)
548 goto out;
549
550 c = p_dev->function_config;
551 if (c->state & CONFIG_LOCKED) {
552 dev_dbg(&s->dev, "Configuration is locked\n");
553 goto out;
554 }
555 if (c->state & CONFIG_IO_REQ) {
556 dev_dbg(&s->dev, "IO already configured\n");
557 goto out;
558 }
559 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
560 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
561 goto out;
562 }
563 if ((req->NumPorts2 > 0) &&
564 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
565 dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
566 goto out;
567 }
568
569 dev_dbg(&s->dev, "trying to allocate resource 1\n");
570 ret = alloc_io_space(s, req->Attributes1, &req->BasePort1,
571 req->NumPorts1, req->IOAddrLines);
572 if (ret) {
573 dev_dbg(&s->dev, "allocation of resource 1 failed\n");
574 goto out;
575 }
576
577 if (req->NumPorts2) {
578 dev_dbg(&s->dev, "trying to allocate resource 2\n");
579 ret = alloc_io_space(s, req->Attributes2, &req->BasePort2,
580 req->NumPorts2, req->IOAddrLines);
581 if (ret) {
582 dev_dbg(&s->dev, "allocation of resource 2 failed\n");
583 release_io_space(s, req->BasePort1, req->NumPorts1);
584 goto out;
585 }
586 }
587
588 c->io = *req;
589 c->state |= CONFIG_IO_REQ;
590 p_dev->_io = 1;
591 dev_dbg(&s->dev, "allocating resources succeeded: %d\n", ret);
592
593 out:
594 mutex_unlock(&s->ops_mutex);
595
596 return ret;
597 } /* pcmcia_request_io */
598 EXPORT_SYMBOL(pcmcia_request_io);
599
600
601 /**
602 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
603 *
604 * pcmcia_request_irq() is a wrapper around request_irq which will allow
605 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
606 * Drivers are free to use request_irq() directly, but then they need to
607 * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ
608 * handlers are allowed.
609 */
610 int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
611 irq_handler_t handler)
612 {
613 int ret;
614
615 if (!p_dev->irq)
616 return -EINVAL;
617
618 ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
619 p_dev->devname, p_dev->priv);
620 if (!ret)
621 p_dev->_irq = 1;
622
623 return ret;
624 }
625 EXPORT_SYMBOL(pcmcia_request_irq);
626
627
628 /**
629 * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
630 *
631 * pcmcia_request_exclusive_irq() is a wrapper around request_irq which
632 * attempts first to request an exclusive IRQ. If it fails, it also accepts
633 * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
634 * IRQ sharing and either use request_irq directly (then they need to call
635 * free_irq themselves, too), or the pcmcia_request_irq() function.
636 */
637 int __must_check
638 __pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
639 irq_handler_t handler)
640 {
641 int ret;
642
643 if (!p_dev->irq)
644 return -EINVAL;
645
646 ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
647 if (ret) {
648 ret = pcmcia_request_irq(p_dev, handler);
649 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
650 "request for exclusive IRQ could not be fulfilled.\n");
651 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
652 "needs updating to supported shared IRQ lines.\n");
653 }
654 if (ret)
655 dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
656 else
657 p_dev->_irq = 1;
658
659 return ret;
660 } /* pcmcia_request_exclusive_irq */
661 EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
662
663
664 #ifdef CONFIG_PCMCIA_PROBE
665
666 /* mask of IRQs already reserved by other cards, we should avoid using them */
667 static u8 pcmcia_used_irq[NR_IRQS];
668
669 static irqreturn_t test_action(int cpl, void *dev_id)
670 {
671 return IRQ_NONE;
672 }
673
674 /**
675 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
676 * @p_dev - the associated PCMCIA device
677 *
678 * locking note: must be called with ops_mutex locked.
679 */
680 static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
681 {
682 struct pcmcia_socket *s = p_dev->socket;
683 unsigned int try, irq;
684 u32 mask = s->irq_mask;
685 int ret = -ENODEV;
686
687 for (try = 0; try < 64; try++) {
688 irq = try % 32;
689
690 /* marked as available by driver, not blocked by userspace? */
691 if (!((mask >> irq) & 1))
692 continue;
693
694 /* avoid an IRQ which is already used by another PCMCIA card */
695 if ((try < 32) && pcmcia_used_irq[irq])
696 continue;
697
698 /* register the correct driver, if possible, to check whether
699 * registering a dummy handle works, i.e. if the IRQ isn't
700 * marked as used by the kernel resource management core */
701 ret = request_irq(irq, test_action, type, p_dev->devname,
702 p_dev);
703 if (!ret) {
704 free_irq(irq, p_dev);
705 p_dev->irq = s->pcmcia_irq = irq;
706 pcmcia_used_irq[irq]++;
707 break;
708 }
709 }
710
711 return ret;
712 }
713
714 void pcmcia_cleanup_irq(struct pcmcia_socket *s)
715 {
716 pcmcia_used_irq[s->pcmcia_irq]--;
717 s->pcmcia_irq = 0;
718 }
719
720 #else /* CONFIG_PCMCIA_PROBE */
721
722 static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
723 {
724 return -EINVAL;
725 }
726
727 void pcmcia_cleanup_irq(struct pcmcia_socket *s)
728 {
729 s->pcmcia_irq = 0;
730 return;
731 }
732
733 #endif /* CONFIG_PCMCIA_PROBE */
734
735
736 /**
737 * pcmcia_setup_irq() - determine IRQ to be used for device
738 * @p_dev - the associated PCMCIA device
739 *
740 * locking note: must be called with ops_mutex locked.
741 */
742 int pcmcia_setup_irq(struct pcmcia_device *p_dev)
743 {
744 struct pcmcia_socket *s = p_dev->socket;
745
746 if (p_dev->irq)
747 return 0;
748
749 /* already assigned? */
750 if (s->pcmcia_irq) {
751 p_dev->irq = s->pcmcia_irq;
752 return 0;
753 }
754
755 /* prefer an exclusive ISA irq */
756 if (!pcmcia_setup_isa_irq(p_dev, 0))
757 return 0;
758
759 /* but accept a shared ISA irq */
760 if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
761 return 0;
762
763 /* but use the PCI irq otherwise */
764 if (s->pci_irq) {
765 p_dev->irq = s->pcmcia_irq = s->pci_irq;
766 return 0;
767 }
768
769 return -EINVAL;
770 }
771
772
773 /** pcmcia_request_window
774 *
775 * Request_window() establishes a mapping between card memory space
776 * and system memory space.
777 */
778 int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
779 {
780 struct pcmcia_socket *s = p_dev->socket;
781 pccard_mem_map *win;
782 u_long align;
783 int w;
784
785 if (!(s->state & SOCKET_PRESENT)) {
786 dev_dbg(&s->dev, "No card present\n");
787 return -ENODEV;
788 }
789 if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
790 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
791 return -EINVAL;
792 }
793
794 /* Window size defaults to smallest available */
795 if (req->Size == 0)
796 req->Size = s->map_size;
797 align = (((s->features & SS_CAP_MEM_ALIGN) ||
798 (req->Attributes & WIN_STRICT_ALIGN)) ?
799 req->Size : s->map_size);
800 if (req->Size & (s->map_size-1)) {
801 dev_dbg(&s->dev, "invalid map size\n");
802 return -EINVAL;
803 }
804 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
805 (req->Base & (align-1))) {
806 dev_dbg(&s->dev, "invalid base address\n");
807 return -EINVAL;
808 }
809 if (req->Base)
810 align = 0;
811
812 /* Allocate system memory window */
813 for (w = 0; w < MAX_WIN; w++)
814 if (!(s->state & SOCKET_WIN_REQ(w)))
815 break;
816 if (w == MAX_WIN) {
817 dev_dbg(&s->dev, "all windows are used already\n");
818 return -EINVAL;
819 }
820
821 mutex_lock(&s->ops_mutex);
822 win = &s->win[w];
823
824 if (!(s->features & SS_CAP_STATIC_MAP)) {
825 win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
826 (req->Attributes & WIN_MAP_BELOW_1MB), s);
827 if (!win->res) {
828 dev_dbg(&s->dev, "allocating mem region failed\n");
829 mutex_unlock(&s->ops_mutex);
830 return -EINVAL;
831 }
832 }
833 p_dev->_win |= CLIENT_WIN_REQ(w);
834
835 /* Configure the socket controller */
836 win->map = w+1;
837 win->flags = 0;
838 win->speed = req->AccessSpeed;
839 if (req->Attributes & WIN_MEMORY_TYPE)
840 win->flags |= MAP_ATTRIB;
841 if (req->Attributes & WIN_ENABLE)
842 win->flags |= MAP_ACTIVE;
843 if (req->Attributes & WIN_DATA_WIDTH_16)
844 win->flags |= MAP_16BIT;
845 if (req->Attributes & WIN_USE_WAIT)
846 win->flags |= MAP_USE_WAIT;
847 win->card_start = 0;
848
849 if (s->ops->set_mem_map(s, win) != 0) {
850 dev_dbg(&s->dev, "failed to set memory mapping\n");
851 mutex_unlock(&s->ops_mutex);
852 return -EIO;
853 }
854 s->state |= SOCKET_WIN_REQ(w);
855
856 /* Return window handle */
857 if (s->features & SS_CAP_STATIC_MAP)
858 req->Base = win->static_start;
859 else
860 req->Base = win->res->start;
861
862 mutex_unlock(&s->ops_mutex);
863 *wh = w + 1;
864
865 return 0;
866 } /* pcmcia_request_window */
867 EXPORT_SYMBOL(pcmcia_request_window);
868
869 void pcmcia_disable_device(struct pcmcia_device *p_dev)
870 {
871 pcmcia_release_configuration(p_dev);
872 pcmcia_release_io(p_dev, &p_dev->io);
873 if (p_dev->_irq) {
874 free_irq(p_dev->irq, p_dev->priv);
875 p_dev->_irq = 0;
876 }
877 if (p_dev->win)
878 pcmcia_release_window(p_dev, p_dev->win);
879 }
880 EXPORT_SYMBOL(pcmcia_disable_device);
This page took 0.048489 seconds and 5 git commands to generate.