]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/pcmcia/pcmcia_resource.c
pcmcia: Pass struct pcmcia_device to pcmcia_release_window()
[karo-tx-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
25 #include <pcmcia/cs_types.h>
26 #include <pcmcia/ss.h>
27 #include <pcmcia/cs.h>
28 #include <pcmcia/cistpl.h>
29 #include <pcmcia/cisreg.h>
30 #include <pcmcia/ds.h>
31
32 #include "cs_internal.h"
33
34
35 /* Access speed for IO windows */
36 static int io_speed = 0;
37 module_param(io_speed, int, 0444);
38
39
40 #ifdef CONFIG_PCMCIA_PROBE
41 #include <asm/irq.h>
42 /* mask of IRQs already reserved by other cards, we should avoid using them */
43 static u8 pcmcia_used_irq[NR_IRQS];
44 #endif
45
46
47 /** alloc_io_space
48  *
49  * Special stuff for managing IO windows, because they are scarce
50  */
51
52 static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
53                           unsigned int *base, unsigned int num, u_int lines)
54 {
55         int i;
56         unsigned int try, align;
57
58         align = (*base) ? (lines ? 1<<lines : 0) : 1;
59         if (align && (align < num)) {
60                 if (*base) {
61                         dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
62                                num, align);
63                         align = 0;
64                 } else
65                         while (align && (align < num)) align <<= 1;
66         }
67         if (*base & ~(align-1)) {
68                 dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
69                        *base, align);
70                 align = 0;
71         }
72         if ((s->features & SS_CAP_STATIC_MAP) && s->io_offset) {
73                 *base = s->io_offset | (*base & 0x0fff);
74                 return 0;
75         }
76         /* Check for an already-allocated window that must conflict with
77          * what was asked for.  It is a hack because it does not catch all
78          * potential conflicts, just the most obvious ones.
79          */
80         for (i = 0; i < MAX_IO_WIN; i++)
81                 if ((s->io[i].res) && *base &&
82                     ((s->io[i].res->start & (align-1)) == *base))
83                         return 1;
84         for (i = 0; i < MAX_IO_WIN; i++) {
85                 if (!s->io[i].res) {
86                         s->io[i].res = pcmcia_find_io_region(*base, num, align, s);
87                         if (s->io[i].res) {
88                                 *base = s->io[i].res->start;
89                                 s->io[i].res->flags = (s->io[i].res->flags & ~IORESOURCE_BITS) | (attr & IORESOURCE_BITS);
90                                 s->io[i].InUse = num;
91                                 break;
92                         } else
93                                 return 1;
94                 } else if ((s->io[i].res->flags & IORESOURCE_BITS) != (attr & IORESOURCE_BITS))
95                         continue;
96                 /* Try to extend top of window */
97                 try = s->io[i].res->end + 1;
98                 if ((*base == 0) || (*base == try))
99                         if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start,
100                                                     s->io[i].res->end + num, s) == 0) {
101                                 *base = try;
102                                 s->io[i].InUse += num;
103                                 break;
104                         }
105                 /* Try to extend bottom of window */
106                 try = s->io[i].res->start - num;
107                 if ((*base == 0) || (*base == try))
108                         if (pcmcia_adjust_io_region(s->io[i].res, s->io[i].res->start - num,
109                                                     s->io[i].res->end, s) == 0) {
110                                 *base = try;
111                                 s->io[i].InUse += num;
112                                 break;
113                         }
114         }
115         return (i == MAX_IO_WIN);
116 } /* alloc_io_space */
117
118
119 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
120                              unsigned int num)
121 {
122         int i;
123
124         for (i = 0; i < MAX_IO_WIN; i++) {
125                 if (!s->io[i].res)
126                         continue;
127                 if ((s->io[i].res->start <= base) &&
128                     (s->io[i].res->end >= base+num-1)) {
129                         s->io[i].InUse -= num;
130                         /* Free the window if no one else is using it */
131                         if (s->io[i].InUse == 0) {
132                                 release_resource(s->io[i].res);
133                                 kfree(s->io[i].res);
134                                 s->io[i].res = NULL;
135                         }
136                 }
137         }
138 } /* release_io_space */
139
140
141 /** pccard_access_configuration_register
142  *
143  * Access_configuration_register() reads and writes configuration
144  * registers in attribute memory.  Memory window 0 is reserved for
145  * this and the tuple reading services.
146  */
147
148 int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
149                                          conf_reg_t *reg)
150 {
151         struct pcmcia_socket *s;
152         config_t *c;
153         int addr;
154         u_char val;
155
156         if (!p_dev || !p_dev->function_config)
157                 return -EINVAL;
158
159         s = p_dev->socket;
160         c = p_dev->function_config;
161
162         if (!(c->state & CONFIG_LOCKED)) {
163                 dev_dbg(&s->dev, "Configuration isnt't locked\n");
164                 return -EACCES;
165         }
166
167         addr = (c->ConfigBase + reg->Offset) >> 1;
168
169         switch (reg->Action) {
170         case CS_READ:
171                 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
172                 reg->Value = val;
173                 break;
174         case CS_WRITE:
175                 val = reg->Value;
176                 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
177                 break;
178         default:
179                 dev_dbg(&s->dev, "Invalid conf register request\n");
180                 return -EINVAL;
181                 break;
182         }
183         return 0;
184 } /* pcmcia_access_configuration_register */
185 EXPORT_SYMBOL(pcmcia_access_configuration_register);
186
187
188 /** pcmcia_get_window
189  */
190 int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *handle,
191                       int idx, win_req_t *req)
192 {
193         window_t *win;
194         int w;
195
196         if (!s || !(s->state & SOCKET_PRESENT))
197                 return -ENODEV;
198         for (w = idx; w < MAX_WIN; w++)
199                 if (s->state & SOCKET_WIN_REQ(w))
200                         break;
201         if (w == MAX_WIN)
202                 return -EINVAL;
203         win = &s->win[w];
204         req->Base = win->ctl.res->start;
205         req->Size = win->ctl.res->end - win->ctl.res->start + 1;
206         req->AccessSpeed = win->ctl.speed;
207         req->Attributes = 0;
208         if (win->ctl.flags & MAP_ATTRIB)
209                 req->Attributes |= WIN_MEMORY_TYPE_AM;
210         if (win->ctl.flags & MAP_ACTIVE)
211                 req->Attributes |= WIN_ENABLE;
212         if (win->ctl.flags & MAP_16BIT)
213                 req->Attributes |= WIN_DATA_WIDTH_16;
214         if (win->ctl.flags & MAP_USE_WAIT)
215                 req->Attributes |= WIN_USE_WAIT;
216         *handle = win;
217         return 0;
218 } /* pcmcia_get_window */
219 EXPORT_SYMBOL(pcmcia_get_window);
220
221
222 /** pcmcia_get_mem_page
223  *
224  * Change the card address of an already open memory window.
225  */
226 int pcmcia_get_mem_page(window_handle_t win, memreq_t *req)
227 {
228         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
229                 return -EINVAL;
230         req->Page = 0;
231         req->CardOffset = win->ctl.card_start;
232         return 0;
233 } /* pcmcia_get_mem_page */
234 EXPORT_SYMBOL(pcmcia_get_mem_page);
235
236
237 int pcmcia_map_mem_page(window_handle_t win, memreq_t *req)
238 {
239         struct pcmcia_socket *s;
240         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
241                 return -EINVAL;
242         s = win->sock;
243         if (req->Page != 0) {
244                 dev_dbg(&s->dev, "failure: requested page is zero\n");
245                 return -EINVAL;
246         }
247         win->ctl.card_start = req->CardOffset;
248         if (s->ops->set_mem_map(s, &win->ctl) != 0) {
249                 dev_dbg(&s->dev, "failed to set_mem_map\n");
250                 return -EIO;
251         }
252         return 0;
253 } /* pcmcia_map_mem_page */
254 EXPORT_SYMBOL(pcmcia_map_mem_page);
255
256
257 /** pcmcia_modify_configuration
258  *
259  * Modify a locked socket configuration
260  */
261 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
262                                 modconf_t *mod)
263 {
264         struct pcmcia_socket *s;
265         config_t *c;
266
267         s = p_dev->socket;
268         c = p_dev->function_config;
269
270         if (!(s->state & SOCKET_PRESENT)) {
271                 dev_dbg(&s->dev, "No card present\n");
272                 return -ENODEV;
273         }
274         if (!(c->state & CONFIG_LOCKED)) {
275                 dev_dbg(&s->dev, "Configuration isnt't locked\n");
276                 return -EACCES;
277         }
278
279         if (mod->Attributes & CONF_IRQ_CHANGE_VALID) {
280                 if (mod->Attributes & CONF_ENABLE_IRQ) {
281                         c->Attributes |= CONF_ENABLE_IRQ;
282                         s->socket.io_irq = s->irq.AssignedIRQ;
283                 } else {
284                         c->Attributes &= ~CONF_ENABLE_IRQ;
285                         s->socket.io_irq = 0;
286                 }
287                 s->ops->set_socket(s, &s->socket);
288         }
289
290         if (mod->Attributes & CONF_VCC_CHANGE_VALID) {
291                 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
292                 return -EINVAL;
293         }
294
295         /* We only allow changing Vpp1 and Vpp2 to the same value */
296         if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
297             (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
298                 if (mod->Vpp1 != mod->Vpp2) {
299                         dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
300                         return -EINVAL;
301                 }
302                 s->socket.Vpp = mod->Vpp1;
303                 if (s->ops->set_socket(s, &s->socket)) {
304                         dev_printk(KERN_WARNING, &s->dev,
305                                    "Unable to set VPP\n");
306                         return -EIO;
307                 }
308         } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
309                    (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
310                 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
311                 return -EINVAL;
312         }
313
314         if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
315                 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
316                 pccard_io_map io_on;
317                 int i;
318
319                 io_on.speed = io_speed;
320                 for (i = 0; i < MAX_IO_WIN; i++) {
321                         if (!s->io[i].res)
322                                 continue;
323                         io_off.map = i;
324                         io_on.map = i;
325
326                         io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
327                         io_on.start = s->io[i].res->start;
328                         io_on.stop = s->io[i].res->end;
329
330                         s->ops->set_io_map(s, &io_off);
331                         mdelay(40);
332                         s->ops->set_io_map(s, &io_on);
333                 }
334         }
335
336         return 0;
337 } /* modify_configuration */
338 EXPORT_SYMBOL(pcmcia_modify_configuration);
339
340
341 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
342 {
343         pccard_io_map io = { 0, 0, 0, 0, 1 };
344         struct pcmcia_socket *s = p_dev->socket;
345         config_t *c = p_dev->function_config;
346         int i;
347
348         if (p_dev->_locked) {
349                 p_dev->_locked = 0;
350                 if (--(s->lock_count) == 0) {
351                         s->socket.flags = SS_OUTPUT_ENA;   /* Is this correct? */
352                         s->socket.Vpp = 0;
353                         s->socket.io_irq = 0;
354                         s->ops->set_socket(s, &s->socket);
355                 }
356         }
357         if (c->state & CONFIG_LOCKED) {
358                 c->state &= ~CONFIG_LOCKED;
359                 if (c->state & CONFIG_IO_REQ)
360                         for (i = 0; i < MAX_IO_WIN; i++) {
361                                 if (!s->io[i].res)
362                                         continue;
363                                 s->io[i].Config--;
364                                 if (s->io[i].Config != 0)
365                                         continue;
366                                 io.map = i;
367                                 s->ops->set_io_map(s, &io);
368                         }
369         }
370
371         return 0;
372 } /* pcmcia_release_configuration */
373
374
375 /** pcmcia_release_io
376  *
377  * Release_io() releases the I/O ranges allocated by a client.  This
378  * may be invoked some time after a card ejection has already dumped
379  * the actual socket configuration, so if the client is "stale", we
380  * don't bother checking the port ranges against the current socket
381  * values.
382  */
383 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
384 {
385         struct pcmcia_socket *s = p_dev->socket;
386         config_t *c = p_dev->function_config;
387
388         if (!p_dev->_io )
389                 return -EINVAL;
390
391         p_dev->_io = 0;
392
393         if ((c->io.BasePort1 != req->BasePort1) ||
394             (c->io.NumPorts1 != req->NumPorts1) ||
395             (c->io.BasePort2 != req->BasePort2) ||
396             (c->io.NumPorts2 != req->NumPorts2))
397                 return -EINVAL;
398
399         c->state &= ~CONFIG_IO_REQ;
400
401         release_io_space(s, req->BasePort1, req->NumPorts1);
402         if (req->NumPorts2)
403                 release_io_space(s, req->BasePort2, req->NumPorts2);
404
405         return 0;
406 } /* pcmcia_release_io */
407
408
409 static int pcmcia_release_irq(struct pcmcia_device *p_dev, irq_req_t *req)
410 {
411         struct pcmcia_socket *s = p_dev->socket;
412         config_t *c= p_dev->function_config;
413
414         if (!p_dev->_irq)
415                 return -EINVAL;
416         p_dev->_irq = 0;
417
418         if (c->state & CONFIG_LOCKED)
419                 return -EACCES;
420         if (c->irq.Attributes != req->Attributes) {
421                 dev_dbg(&s->dev, "IRQ attributes must match assigned ones\n");
422                 return -EINVAL;
423         }
424         if (s->irq.AssignedIRQ != req->AssignedIRQ) {
425                 dev_dbg(&s->dev, "IRQ must match assigned one\n");
426                 return -EINVAL;
427         }
428         if (--s->irq.Config == 0) {
429                 c->state &= ~CONFIG_IRQ_REQ;
430                 s->irq.AssignedIRQ = 0;
431         }
432
433         if (req->Attributes & IRQ_HANDLE_PRESENT) {
434                 free_irq(req->AssignedIRQ, req->Instance);
435         }
436
437 #ifdef CONFIG_PCMCIA_PROBE
438         pcmcia_used_irq[req->AssignedIRQ]--;
439 #endif
440
441         return 0;
442 } /* pcmcia_release_irq */
443
444
445 int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
446 {
447         struct pcmcia_socket *s;
448         window_handle_t win = wh;
449
450         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
451                 return -EINVAL;
452         s = win->sock;
453         if (!(win->handle->_win & CLIENT_WIN_REQ(win->index))) {
454                 dev_dbg(&s->dev, "not releasing unknown window\n");
455                 return -EINVAL;
456         }
457
458         /* Shut down memory window */
459         win->ctl.flags &= ~MAP_ACTIVE;
460         s->ops->set_mem_map(s, &win->ctl);
461         s->state &= ~SOCKET_WIN_REQ(win->index);
462
463         /* Release system memory */
464         if (win->ctl.res) {
465                 release_resource(win->ctl.res);
466                 kfree(win->ctl.res);
467                 win->ctl.res = NULL;
468         }
469         win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
470
471         win->magic = 0;
472
473         return 0;
474 } /* pcmcia_release_window */
475 EXPORT_SYMBOL(pcmcia_release_window);
476
477
478 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
479                                  config_req_t *req)
480 {
481         int i;
482         u_int base;
483         struct pcmcia_socket *s = p_dev->socket;
484         config_t *c;
485         pccard_io_map iomap;
486
487         if (!(s->state & SOCKET_PRESENT))
488                 return -ENODEV;
489
490         if (req->IntType & INT_CARDBUS) {
491                 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
492                 return -EINVAL;
493         }
494         c = p_dev->function_config;
495         if (c->state & CONFIG_LOCKED) {
496                 dev_dbg(&s->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 = req->Vpp;
502         if (s->ops->set_socket(s, &s->socket)) {
503                 dev_printk(KERN_WARNING, &s->dev,
504                            "Unable to set socket state\n");
505                 return -EINVAL;
506         }
507
508         /* Pick memory or I/O card, DMA mode, interrupt */
509         c->IntType = req->IntType;
510         c->Attributes = req->Attributes;
511         if (req->IntType & INT_MEMORY_AND_IO)
512                 s->socket.flags |= SS_IOCARD;
513         if (req->IntType & INT_ZOOMED_VIDEO)
514                 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
515         if (req->Attributes & CONF_ENABLE_DMA)
516                 s->socket.flags |= SS_DMA_MODE;
517         if (req->Attributes & CONF_ENABLE_SPKR)
518                 s->socket.flags |= SS_SPKR_ENA;
519         if (req->Attributes & CONF_ENABLE_IRQ)
520                 s->socket.io_irq = s->irq.AssignedIRQ;
521         else
522                 s->socket.io_irq = 0;
523         s->ops->set_socket(s, &s->socket);
524         s->lock_count++;
525
526         /* Set up CIS configuration registers */
527         base = c->ConfigBase = req->ConfigBase;
528         c->CardValues = req->Present;
529         if (req->Present & PRESENT_COPY) {
530                 c->Copy = req->Copy;
531                 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
532         }
533         if (req->Present & PRESENT_OPTION) {
534                 if (s->functions == 1) {
535                         c->Option = req->ConfigIndex & COR_CONFIG_MASK;
536                 } else {
537                         c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
538                         c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
539                         if (req->Present & PRESENT_IOBASE_0)
540                                 c->Option |= COR_ADDR_DECODE;
541                 }
542                 if (c->state & CONFIG_IRQ_REQ)
543                         if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
544                                 c->Option |= COR_LEVEL_REQ;
545                 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
546                 mdelay(40);
547         }
548         if (req->Present & PRESENT_STATUS) {
549                 c->Status = req->Status;
550                 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
551         }
552         if (req->Present & PRESENT_PIN_REPLACE) {
553                 c->Pin = req->Pin;
554                 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
555         }
556         if (req->Present & PRESENT_EXT_STATUS) {
557                 c->ExtStatus = req->ExtStatus;
558                 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
559         }
560         if (req->Present & PRESENT_IOBASE_0) {
561                 u_char b = c->io.BasePort1 & 0xff;
562                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
563                 b = (c->io.BasePort1 >> 8) & 0xff;
564                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
565         }
566         if (req->Present & PRESENT_IOSIZE) {
567                 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
568                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
569         }
570
571         /* Configure I/O windows */
572         if (c->state & CONFIG_IO_REQ) {
573                 iomap.speed = io_speed;
574                 for (i = 0; i < MAX_IO_WIN; i++)
575                         if (s->io[i].res) {
576                                 iomap.map = i;
577                                 iomap.flags = MAP_ACTIVE;
578                                 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
579                                 case IO_DATA_PATH_WIDTH_16:
580                                         iomap.flags |= MAP_16BIT; break;
581                                 case IO_DATA_PATH_WIDTH_AUTO:
582                                         iomap.flags |= MAP_AUTOSZ; break;
583                                 default:
584                                         break;
585                                 }
586                                 iomap.start = s->io[i].res->start;
587                                 iomap.stop = s->io[i].res->end;
588                                 s->ops->set_io_map(s, &iomap);
589                                 s->io[i].Config++;
590                         }
591         }
592
593         c->state |= CONFIG_LOCKED;
594         p_dev->_locked = 1;
595         return 0;
596 } /* pcmcia_request_configuration */
597 EXPORT_SYMBOL(pcmcia_request_configuration);
598
599
600 /** pcmcia_request_io
601  *
602  * Request_io() reserves ranges of port addresses for a socket.
603  * I have not implemented range sharing or alias addressing.
604  */
605 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
606 {
607         struct pcmcia_socket *s = p_dev->socket;
608         config_t *c;
609
610         if (!(s->state & SOCKET_PRESENT)) {
611                 dev_dbg(&s->dev, "No card present\n");
612                 return -ENODEV;
613         }
614
615         if (!req)
616                 return -EINVAL;
617         c = p_dev->function_config;
618         if (c->state & CONFIG_LOCKED) {
619                 dev_dbg(&s->dev, "Configuration is locked\n");
620                 return -EACCES;
621         }
622         if (c->state & CONFIG_IO_REQ) {
623                 dev_dbg(&s->dev, "IO already configured\n");
624                 return -EBUSY;
625         }
626         if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
627                 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
628                 return -EINVAL;
629         }
630         if ((req->NumPorts2 > 0) &&
631             (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
632                 dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
633                 return -EINVAL;
634         }
635
636         dev_dbg(&s->dev, "trying to allocate resource 1\n");
637         if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
638                            req->NumPorts1, req->IOAddrLines)) {
639                 dev_dbg(&s->dev, "allocation of resource 1 failed\n");
640                 return -EBUSY;
641         }
642
643         if (req->NumPorts2) {
644                 dev_dbg(&s->dev, "trying to allocate resource 2\n");
645                 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
646                                    req->NumPorts2, req->IOAddrLines)) {
647                         dev_dbg(&s->dev, "allocation of resource 2 failed\n");
648                         release_io_space(s, req->BasePort1, req->NumPorts1);
649                         return -EBUSY;
650                 }
651         }
652
653         c->io = *req;
654         c->state |= CONFIG_IO_REQ;
655         p_dev->_io = 1;
656         return 0;
657 } /* pcmcia_request_io */
658 EXPORT_SYMBOL(pcmcia_request_io);
659
660
661 /** pcmcia_request_irq
662  *
663  * Request_irq() reserves an irq for this client.
664  *
665  * Also, since Linux only reserves irq's when they are actually
666  * hooked, we don't guarantee that an irq will still be available
667  * when the configuration is locked.  Now that I think about it,
668  * there might be a way to fix this using a dummy handler.
669  */
670
671 #ifdef CONFIG_PCMCIA_PROBE
672 static irqreturn_t test_action(int cpl, void *dev_id)
673 {
674         return IRQ_NONE;
675 }
676 #endif
677
678 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
679 {
680         struct pcmcia_socket *s = p_dev->socket;
681         config_t *c;
682         int ret = -EINVAL, irq = 0;
683         int type;
684
685         if (!(s->state & SOCKET_PRESENT)) {
686                 dev_dbg(&s->dev, "No card present\n");
687                 return -ENODEV;
688         }
689         c = p_dev->function_config;
690         if (c->state & CONFIG_LOCKED) {
691                 dev_dbg(&s->dev, "Configuration is locked\n");
692                 return -EACCES;
693         }
694         if (c->state & CONFIG_IRQ_REQ) {
695                 dev_dbg(&s->dev, "IRQ already configured\n");
696                 return -EBUSY;
697         }
698
699         /* Decide what type of interrupt we are registering */
700         type = 0;
701         if (s->functions > 1)           /* All of this ought to be handled higher up */
702                 type = IRQF_SHARED;
703         else if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
704                 type = IRQF_SHARED;
705         else printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n");
706
707 #ifdef CONFIG_PCMCIA_PROBE
708
709 #ifdef IRQ_NOAUTOEN
710         /* if the underlying IRQ infrastructure allows for it, only allocate
711          * the IRQ, but do not enable it
712          */
713         if (!(req->Attributes & IRQ_HANDLE_PRESENT))
714                 type |= IRQ_NOAUTOEN;
715 #endif /* IRQ_NOAUTOEN */
716
717         if (s->irq.AssignedIRQ != 0) {
718                 /* If the interrupt is already assigned, it must be the same */
719                 irq = s->irq.AssignedIRQ;
720         } else {
721                 int try;
722                 u32 mask = s->irq_mask;
723                 void *data = &p_dev->dev.driver; /* something unique to this device */
724
725                 for (try = 0; try < 64; try++) {
726                         irq = try % 32;
727
728                         /* marked as available by driver, and not blocked by userspace? */
729                         if (!((mask >> irq) & 1))
730                                 continue;
731
732                         /* avoid an IRQ which is already used by a PCMCIA card */
733                         if ((try < 32) && pcmcia_used_irq[irq])
734                                 continue;
735
736                         /* register the correct driver, if possible, of check whether
737                          * registering a dummy handle works, i.e. if the IRQ isn't
738                          * marked as used by the kernel resource management core */
739                         ret = request_irq(irq,
740                                           (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
741                                           type,
742                                           p_dev->devname,
743                                           (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
744                         if (!ret) {
745                                 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
746                                         free_irq(irq, data);
747                                 break;
748                         }
749                 }
750         }
751 #endif
752         /* only assign PCI irq if no IRQ already assigned */
753         if (ret && !s->irq.AssignedIRQ) {
754                 if (!s->pci_irq) {
755                         dev_printk(KERN_INFO, &s->dev, "no IRQ found\n");
756                         return ret;
757                 }
758                 type = IRQF_SHARED;
759                 irq = s->pci_irq;
760         }
761
762         if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
763                 ret = request_irq(irq, req->Handler, type,
764                                   p_dev->devname, req->Instance);
765                 if (ret) {
766                         dev_printk(KERN_INFO, &s->dev,
767                                 "request_irq() failed\n");
768                         return ret;
769                 }
770         }
771
772         /* Make sure the fact the request type was overridden is passed back */
773         if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
774                 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
775                 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
776                         "request for exclusive IRQ could not be fulfilled.\n");
777                 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
778                         "needs updating to supported shared IRQ lines.\n");
779         }
780         c->irq.Attributes = req->Attributes;
781         s->irq.AssignedIRQ = req->AssignedIRQ = irq;
782         s->irq.Config++;
783
784         c->state |= CONFIG_IRQ_REQ;
785         p_dev->_irq = 1;
786
787 #ifdef CONFIG_PCMCIA_PROBE
788         pcmcia_used_irq[irq]++;
789 #endif
790
791         return 0;
792 } /* pcmcia_request_irq */
793 EXPORT_SYMBOL(pcmcia_request_irq);
794
795
796 /** pcmcia_request_window
797  *
798  * Request_window() establishes a mapping between card memory space
799  * and system memory space.
800  */
801 int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
802 {
803         struct pcmcia_socket *s = (*p_dev)->socket;
804         window_t *win;
805         u_long align;
806         int w;
807
808         if (!(s->state & SOCKET_PRESENT)) {
809                 dev_dbg(&s->dev, "No card present\n");
810                 return -ENODEV;
811         }
812         if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
813                 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
814                 return -EINVAL;
815         }
816
817         /* Window size defaults to smallest available */
818         if (req->Size == 0)
819                 req->Size = s->map_size;
820         align = (((s->features & SS_CAP_MEM_ALIGN) ||
821                   (req->Attributes & WIN_STRICT_ALIGN)) ?
822                  req->Size : s->map_size);
823         if (req->Size & (s->map_size-1)) {
824                 dev_dbg(&s->dev, "invalid map size\n");
825                 return -EINVAL;
826         }
827         if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
828             (req->Base & (align-1))) {
829                 dev_dbg(&s->dev, "invalid base address\n");
830                 return -EINVAL;
831         }
832         if (req->Base)
833                 align = 0;
834
835         /* Allocate system memory window */
836         for (w = 0; w < MAX_WIN; w++)
837                 if (!(s->state & SOCKET_WIN_REQ(w))) break;
838         if (w == MAX_WIN) {
839                 dev_dbg(&s->dev, "all windows are used already\n");
840                 return -EINVAL;
841         }
842
843         win = &s->win[w];
844         win->magic = WINDOW_MAGIC;
845         win->index = w;
846         win->handle = *p_dev;
847         win->sock = s;
848
849         if (!(s->features & SS_CAP_STATIC_MAP)) {
850                 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
851                                                       (req->Attributes & WIN_MAP_BELOW_1MB), s);
852                 if (!win->ctl.res) {
853                         dev_dbg(&s->dev, "allocating mem region failed\n");
854                         return -EINVAL;
855                 }
856         }
857         (*p_dev)->_win |= CLIENT_WIN_REQ(w);
858
859         /* Configure the socket controller */
860         win->ctl.map = w+1;
861         win->ctl.flags = 0;
862         win->ctl.speed = req->AccessSpeed;
863         if (req->Attributes & WIN_MEMORY_TYPE)
864                 win->ctl.flags |= MAP_ATTRIB;
865         if (req->Attributes & WIN_ENABLE)
866                 win->ctl.flags |= MAP_ACTIVE;
867         if (req->Attributes & WIN_DATA_WIDTH_16)
868                 win->ctl.flags |= MAP_16BIT;
869         if (req->Attributes & WIN_USE_WAIT)
870                 win->ctl.flags |= MAP_USE_WAIT;
871         win->ctl.card_start = 0;
872         if (s->ops->set_mem_map(s, &win->ctl) != 0) {
873                 dev_dbg(&s->dev, "failed to set memory mapping\n");
874                 return -EIO;
875         }
876         s->state |= SOCKET_WIN_REQ(w);
877
878         /* Return window handle */
879         if (s->features & SS_CAP_STATIC_MAP) {
880                 req->Base = win->ctl.static_start;
881         } else {
882                 req->Base = win->ctl.res->start;
883         }
884         *wh = win;
885
886         return 0;
887 } /* pcmcia_request_window */
888 EXPORT_SYMBOL(pcmcia_request_window);
889
890 void pcmcia_disable_device(struct pcmcia_device *p_dev) {
891         pcmcia_release_configuration(p_dev);
892         pcmcia_release_io(p_dev, &p_dev->io);
893         pcmcia_release_irq(p_dev, &p_dev->irq);
894         if (p_dev->win)
895                 pcmcia_release_window(p_dev, p_dev->win);
896 }
897 EXPORT_SYMBOL(pcmcia_disable_device);
898
899
900 struct pcmcia_cfg_mem {
901         struct pcmcia_device *p_dev;
902         void *priv_data;
903         int (*conf_check) (struct pcmcia_device *p_dev,
904                            cistpl_cftable_entry_t *cfg,
905                            cistpl_cftable_entry_t *dflt,
906                            unsigned int vcc,
907                            void *priv_data);
908         cisparse_t parse;
909         cistpl_cftable_entry_t dflt;
910 };
911
912 /**
913  * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
914  *
915  * pcmcia_do_loop_config() is the internal callback for the call from
916  * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
917  * by a struct pcmcia_cfg_mem.
918  */
919 static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
920 {
921         cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
922         struct pcmcia_cfg_mem *cfg_mem = priv;
923
924         /* default values */
925         cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
926         if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
927                 cfg_mem->dflt = *cfg;
928
929         return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt,
930                                    cfg_mem->p_dev->socket->socket.Vcc,
931                                    cfg_mem->priv_data);
932 }
933
934 /**
935  * pcmcia_loop_config() - loop over configuration options
936  * @p_dev:      the struct pcmcia_device which we need to loop for.
937  * @conf_check: function to call for each configuration option.
938  *              It gets passed the struct pcmcia_device, the CIS data
939  *              describing the configuration option, and private data
940  *              being passed to pcmcia_loop_config()
941  * @priv_data:  private data to be passed to the conf_check function.
942  *
943  * pcmcia_loop_config() loops over all configuration options, and calls
944  * the driver-specific conf_check() for each one, checking whether
945  * it is a valid one. Returns 0 on success or errorcode otherwise.
946  */
947 int pcmcia_loop_config(struct pcmcia_device *p_dev,
948                        int      (*conf_check)   (struct pcmcia_device *p_dev,
949                                                  cistpl_cftable_entry_t *cfg,
950                                                  cistpl_cftable_entry_t *dflt,
951                                                  unsigned int vcc,
952                                                  void *priv_data),
953                        void *priv_data)
954 {
955         struct pcmcia_cfg_mem *cfg_mem;
956         int ret;
957
958         cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
959         if (cfg_mem == NULL)
960                 return -ENOMEM;
961
962         cfg_mem->p_dev = p_dev;
963         cfg_mem->conf_check = conf_check;
964         cfg_mem->priv_data = priv_data;
965
966         ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
967                                 CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
968                                 cfg_mem, pcmcia_do_loop_config);
969
970         kfree(cfg_mem);
971         return ret;
972 }
973 EXPORT_SYMBOL(pcmcia_loop_config);
974
975
976 struct pcmcia_loop_mem {
977         struct pcmcia_device *p_dev;
978         void *priv_data;
979         int (*loop_tuple) (struct pcmcia_device *p_dev,
980                            tuple_t *tuple,
981                            void *priv_data);
982 };
983
984 /**
985  * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
986  *
987  * pcmcia_do_loop_tuple() is the internal callback for the call from
988  * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
989  * by a struct pcmcia_cfg_mem.
990  */
991 static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
992 {
993         struct pcmcia_loop_mem *loop = priv;
994
995         return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
996 };
997
998 /**
999  * pcmcia_loop_tuple() - loop over tuples in the CIS
1000  * @p_dev:      the struct pcmcia_device which we need to loop for.
1001  * @code:       which CIS code shall we look for?
1002  * @priv_data:  private data to be passed to the loop_tuple function.
1003  * @loop_tuple: function to call for each CIS entry of type @function. IT
1004  *              gets passed the raw tuple and @priv_data.
1005  *
1006  * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
1007  * calls the @loop_tuple function for each entry. If the call to @loop_tuple
1008  * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
1009  */
1010 int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1011                       int (*loop_tuple) (struct pcmcia_device *p_dev,
1012                                          tuple_t *tuple,
1013                                          void *priv_data),
1014                       void *priv_data)
1015 {
1016         struct pcmcia_loop_mem loop = {
1017                 .p_dev = p_dev,
1018                 .loop_tuple = loop_tuple,
1019                 .priv_data = priv_data};
1020
1021         return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
1022                                  &loop, pcmcia_do_loop_tuple);
1023 };
1024 EXPORT_SYMBOL(pcmcia_loop_tuple);
1025
1026
1027 struct pcmcia_loop_get {
1028         size_t len;
1029         cisdata_t **buf;
1030 };
1031
1032 /**
1033  * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
1034  *
1035  * pcmcia_do_get_tuple() is the internal callback for the call from
1036  * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
1037  * the first tuple, return 0 unconditionally. Create a memory buffer large
1038  * enough to hold the content of the tuple, and fill it with the tuple data.
1039  * The caller is responsible to free the buffer.
1040  */
1041 static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
1042                                void *priv)
1043 {
1044         struct pcmcia_loop_get *get = priv;
1045
1046         *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
1047         if (*get->buf) {
1048                 get->len = tuple->TupleDataLen;
1049                 memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
1050         } else
1051                 dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
1052         return 0;
1053 };
1054
1055 /**
1056  * pcmcia_get_tuple() - get first tuple from CIS
1057  * @p_dev:      the struct pcmcia_device which we need to loop for.
1058  * @code:       which CIS code shall we look for?
1059  * @buf:        pointer to store the buffer to.
1060  *
1061  * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
1062  * It returns the buffer length (or zero). The caller is responsible to free
1063  * the buffer passed in @buf.
1064  */
1065 size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1066                         unsigned char **buf)
1067 {
1068         struct pcmcia_loop_get get = {
1069                 .len = 0,
1070                 .buf = buf,
1071         };
1072
1073         *get.buf = NULL;
1074         pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
1075
1076         return get.len;
1077 };
1078 EXPORT_SYMBOL(pcmcia_get_tuple);
1079
1080
1081 /**
1082  * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
1083  *
1084  * pcmcia_do_get_mac() is the internal callback for the call from
1085  * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
1086  * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
1087  * to struct net_device->dev_addr[i].
1088  */
1089 static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
1090                              void *priv)
1091 {
1092         struct net_device *dev = priv;
1093         int i;
1094
1095         if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
1096                 return -EINVAL;
1097         if (tuple->TupleDataLen < ETH_ALEN + 2) {
1098                 dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
1099                         "LAN_NODE_ID\n");
1100                 return -EINVAL;
1101         }
1102
1103         if (tuple->TupleData[1] != ETH_ALEN) {
1104                 dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
1105                 return -EINVAL;
1106         }
1107         for (i = 0; i < 6; i++)
1108                 dev->dev_addr[i] = tuple->TupleData[i+2];
1109         return 0;
1110 };
1111
1112 /**
1113  * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
1114  * @p_dev:      the struct pcmcia_device for which we want the address.
1115  * @dev:        a properly prepared struct net_device to store the info to.
1116  *
1117  * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
1118  * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
1119  * must be set up properly by the driver (see examples!).
1120  */
1121 int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
1122 {
1123         return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
1124 };
1125 EXPORT_SYMBOL(pcmcia_get_mac_from_cis);
1126