]> Pileus Git - ~andy/linux/blob - drivers/pcmcia/pcmcia_resource.c
pcmcia: use dev_dbg and dev_print in pd6729.c
[~andy/linux] / 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(window_handle_t win)
446 {
447         struct pcmcia_socket *s;
448
449         if ((win == NULL) || (win->magic != WINDOW_MAGIC))
450                 return -EINVAL;
451         s = win->sock;
452         if (!(win->handle->_win & CLIENT_WIN_REQ(win->index))) {
453                 dev_dbg(&s->dev, "not releasing unknown window\n");
454                 return -EINVAL;
455         }
456
457         /* Shut down memory window */
458         win->ctl.flags &= ~MAP_ACTIVE;
459         s->ops->set_mem_map(s, &win->ctl);
460         s->state &= ~SOCKET_WIN_REQ(win->index);
461
462         /* Release system memory */
463         if (win->ctl.res) {
464                 release_resource(win->ctl.res);
465                 kfree(win->ctl.res);
466                 win->ctl.res = NULL;
467         }
468         win->handle->_win &= ~CLIENT_WIN_REQ(win->index);
469
470         win->magic = 0;
471
472         return 0;
473 } /* pcmcia_release_window */
474 EXPORT_SYMBOL(pcmcia_release_window);
475
476
477 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
478                                  config_req_t *req)
479 {
480         int i;
481         u_int base;
482         struct pcmcia_socket *s = p_dev->socket;
483         config_t *c;
484         pccard_io_map iomap;
485
486         if (!(s->state & SOCKET_PRESENT))
487                 return -ENODEV;
488
489         if (req->IntType & INT_CARDBUS) {
490                 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
491                 return -EINVAL;
492         }
493         c = p_dev->function_config;
494         if (c->state & CONFIG_LOCKED) {
495                 dev_dbg(&s->dev, "Configuration is locked\n");
496                 return -EACCES;
497         }
498
499         /* Do power control.  We don't allow changes in Vcc. */
500         s->socket.Vpp = req->Vpp;
501         if (s->ops->set_socket(s, &s->socket)) {
502                 dev_printk(KERN_WARNING, &s->dev,
503                            "Unable to set socket state\n");
504                 return -EINVAL;
505         }
506
507         /* Pick memory or I/O card, DMA mode, interrupt */
508         c->IntType = req->IntType;
509         c->Attributes = req->Attributes;
510         if (req->IntType & INT_MEMORY_AND_IO)
511                 s->socket.flags |= SS_IOCARD;
512         if (req->IntType & INT_ZOOMED_VIDEO)
513                 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
514         if (req->Attributes & CONF_ENABLE_DMA)
515                 s->socket.flags |= SS_DMA_MODE;
516         if (req->Attributes & CONF_ENABLE_SPKR)
517                 s->socket.flags |= SS_SPKR_ENA;
518         if (req->Attributes & CONF_ENABLE_IRQ)
519                 s->socket.io_irq = s->irq.AssignedIRQ;
520         else
521                 s->socket.io_irq = 0;
522         s->ops->set_socket(s, &s->socket);
523         s->lock_count++;
524
525         /* Set up CIS configuration registers */
526         base = c->ConfigBase = req->ConfigBase;
527         c->CardValues = req->Present;
528         if (req->Present & PRESENT_COPY) {
529                 c->Copy = req->Copy;
530                 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
531         }
532         if (req->Present & PRESENT_OPTION) {
533                 if (s->functions == 1) {
534                         c->Option = req->ConfigIndex & COR_CONFIG_MASK;
535                 } else {
536                         c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
537                         c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
538                         if (req->Present & PRESENT_IOBASE_0)
539                                 c->Option |= COR_ADDR_DECODE;
540                 }
541                 if (c->state & CONFIG_IRQ_REQ)
542                         if (!(c->irq.Attributes & IRQ_FORCED_PULSE))
543                                 c->Option |= COR_LEVEL_REQ;
544                 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
545                 mdelay(40);
546         }
547         if (req->Present & PRESENT_STATUS) {
548                 c->Status = req->Status;
549                 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
550         }
551         if (req->Present & PRESENT_PIN_REPLACE) {
552                 c->Pin = req->Pin;
553                 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
554         }
555         if (req->Present & PRESENT_EXT_STATUS) {
556                 c->ExtStatus = req->ExtStatus;
557                 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
558         }
559         if (req->Present & PRESENT_IOBASE_0) {
560                 u_char b = c->io.BasePort1 & 0xff;
561                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
562                 b = (c->io.BasePort1 >> 8) & 0xff;
563                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
564         }
565         if (req->Present & PRESENT_IOSIZE) {
566                 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
567                 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
568         }
569
570         /* Configure I/O windows */
571         if (c->state & CONFIG_IO_REQ) {
572                 iomap.speed = io_speed;
573                 for (i = 0; i < MAX_IO_WIN; i++)
574                         if (s->io[i].res) {
575                                 iomap.map = i;
576                                 iomap.flags = MAP_ACTIVE;
577                                 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
578                                 case IO_DATA_PATH_WIDTH_16:
579                                         iomap.flags |= MAP_16BIT; break;
580                                 case IO_DATA_PATH_WIDTH_AUTO:
581                                         iomap.flags |= MAP_AUTOSZ; break;
582                                 default:
583                                         break;
584                                 }
585                                 iomap.start = s->io[i].res->start;
586                                 iomap.stop = s->io[i].res->end;
587                                 s->ops->set_io_map(s, &iomap);
588                                 s->io[i].Config++;
589                         }
590         }
591
592         c->state |= CONFIG_LOCKED;
593         p_dev->_locked = 1;
594         return 0;
595 } /* pcmcia_request_configuration */
596 EXPORT_SYMBOL(pcmcia_request_configuration);
597
598
599 /** pcmcia_request_io
600  *
601  * Request_io() reserves ranges of port addresses for a socket.
602  * I have not implemented range sharing or alias addressing.
603  */
604 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
605 {
606         struct pcmcia_socket *s = p_dev->socket;
607         config_t *c;
608
609         if (!(s->state & SOCKET_PRESENT)) {
610                 dev_dbg(&s->dev, "No card present\n");
611                 return -ENODEV;
612         }
613
614         if (!req)
615                 return -EINVAL;
616         c = p_dev->function_config;
617         if (c->state & CONFIG_LOCKED) {
618                 dev_dbg(&s->dev, "Configuration is locked\n");
619                 return -EACCES;
620         }
621         if (c->state & CONFIG_IO_REQ) {
622                 dev_dbg(&s->dev, "IO already configured\n");
623                 return -EBUSY;
624         }
625         if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
626                 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
627                 return -EINVAL;
628         }
629         if ((req->NumPorts2 > 0) &&
630             (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
631                 dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
632                 return -EINVAL;
633         }
634
635         dev_dbg(&s->dev, "trying to allocate resource 1\n");
636         if (alloc_io_space(s, req->Attributes1, &req->BasePort1,
637                            req->NumPorts1, req->IOAddrLines)) {
638                 dev_dbg(&s->dev, "allocation of resource 1 failed\n");
639                 return -EBUSY;
640         }
641
642         if (req->NumPorts2) {
643                 dev_dbg(&s->dev, "trying to allocate resource 2\n");
644                 if (alloc_io_space(s, req->Attributes2, &req->BasePort2,
645                                    req->NumPorts2, req->IOAddrLines)) {
646                         dev_dbg(&s->dev, "allocation of resource 2 failed\n");
647                         release_io_space(s, req->BasePort1, req->NumPorts1);
648                         return -EBUSY;
649                 }
650         }
651
652         c->io = *req;
653         c->state |= CONFIG_IO_REQ;
654         p_dev->_io = 1;
655         return 0;
656 } /* pcmcia_request_io */
657 EXPORT_SYMBOL(pcmcia_request_io);
658
659
660 /** pcmcia_request_irq
661  *
662  * Request_irq() reserves an irq for this client.
663  *
664  * Also, since Linux only reserves irq's when they are actually
665  * hooked, we don't guarantee that an irq will still be available
666  * when the configuration is locked.  Now that I think about it,
667  * there might be a way to fix this using a dummy handler.
668  */
669
670 #ifdef CONFIG_PCMCIA_PROBE
671 static irqreturn_t test_action(int cpl, void *dev_id)
672 {
673         return IRQ_NONE;
674 }
675 #endif
676
677 int pcmcia_request_irq(struct pcmcia_device *p_dev, irq_req_t *req)
678 {
679         struct pcmcia_socket *s = p_dev->socket;
680         config_t *c;
681         int ret = -EINVAL, irq = 0;
682         int type;
683
684         if (!(s->state & SOCKET_PRESENT)) {
685                 dev_dbg(&s->dev, "No card present\n");
686                 return -ENODEV;
687         }
688         c = p_dev->function_config;
689         if (c->state & CONFIG_LOCKED) {
690                 dev_dbg(&s->dev, "Configuration is locked\n");
691                 return -EACCES;
692         }
693         if (c->state & CONFIG_IRQ_REQ) {
694                 dev_dbg(&s->dev, "IRQ already configured\n");
695                 return -EBUSY;
696         }
697
698         /* Decide what type of interrupt we are registering */
699         type = 0;
700         if (s->functions > 1)           /* All of this ought to be handled higher up */
701                 type = IRQF_SHARED;
702         else if (req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)
703                 type = IRQF_SHARED;
704         else printk(KERN_WARNING "pcmcia: Driver needs updating to support IRQ sharing.\n");
705
706 #ifdef CONFIG_PCMCIA_PROBE
707
708 #ifdef IRQ_NOAUTOEN
709         /* if the underlying IRQ infrastructure allows for it, only allocate
710          * the IRQ, but do not enable it
711          */
712         if (!(req->Attributes & IRQ_HANDLE_PRESENT))
713                 type |= IRQ_NOAUTOEN;
714 #endif /* IRQ_NOAUTOEN */
715
716         if (s->irq.AssignedIRQ != 0) {
717                 /* If the interrupt is already assigned, it must be the same */
718                 irq = s->irq.AssignedIRQ;
719         } else {
720                 int try;
721                 u32 mask = s->irq_mask;
722                 void *data = &p_dev->dev.driver; /* something unique to this device */
723
724                 for (try = 0; try < 64; try++) {
725                         irq = try % 32;
726
727                         /* marked as available by driver, and not blocked by userspace? */
728                         if (!((mask >> irq) & 1))
729                                 continue;
730
731                         /* avoid an IRQ which is already used by a PCMCIA card */
732                         if ((try < 32) && pcmcia_used_irq[irq])
733                                 continue;
734
735                         /* register the correct driver, if possible, of check whether
736                          * registering a dummy handle works, i.e. if the IRQ isn't
737                          * marked as used by the kernel resource management core */
738                         ret = request_irq(irq,
739                                           (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Handler : test_action,
740                                           type,
741                                           p_dev->devname,
742                                           (req->Attributes & IRQ_HANDLE_PRESENT) ? req->Instance : data);
743                         if (!ret) {
744                                 if (!(req->Attributes & IRQ_HANDLE_PRESENT))
745                                         free_irq(irq, data);
746                                 break;
747                         }
748                 }
749         }
750 #endif
751         /* only assign PCI irq if no IRQ already assigned */
752         if (ret && !s->irq.AssignedIRQ) {
753                 if (!s->pci_irq) {
754                         dev_printk(KERN_INFO, &s->dev, "no IRQ found\n");
755                         return ret;
756                 }
757                 type = IRQF_SHARED;
758                 irq = s->pci_irq;
759         }
760
761         if (ret && (req->Attributes & IRQ_HANDLE_PRESENT)) {
762                 ret = request_irq(irq, req->Handler, type,
763                                   p_dev->devname, req->Instance);
764                 if (ret) {
765                         dev_printk(KERN_INFO, &s->dev,
766                                 "request_irq() failed\n");
767                         return ret;
768                 }
769         }
770
771         /* Make sure the fact the request type was overridden is passed back */
772         if (type == IRQF_SHARED && !(req->Attributes & IRQ_TYPE_DYNAMIC_SHARING)) {
773                 req->Attributes |= IRQ_TYPE_DYNAMIC_SHARING;
774                 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
775                         "request for exclusive IRQ could not be fulfilled.\n");
776                 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
777                         "needs updating to supported shared IRQ lines.\n");
778         }
779         c->irq.Attributes = req->Attributes;
780         s->irq.AssignedIRQ = req->AssignedIRQ = irq;
781         s->irq.Config++;
782
783         c->state |= CONFIG_IRQ_REQ;
784         p_dev->_irq = 1;
785
786 #ifdef CONFIG_PCMCIA_PROBE
787         pcmcia_used_irq[irq]++;
788 #endif
789
790         return 0;
791 } /* pcmcia_request_irq */
792 EXPORT_SYMBOL(pcmcia_request_irq);
793
794
795 /** pcmcia_request_window
796  *
797  * Request_window() establishes a mapping between card memory space
798  * and system memory space.
799  */
800 int pcmcia_request_window(struct pcmcia_device **p_dev, win_req_t *req, window_handle_t *wh)
801 {
802         struct pcmcia_socket *s = (*p_dev)->socket;
803         window_t *win;
804         u_long align;
805         int w;
806
807         if (!(s->state & SOCKET_PRESENT)) {
808                 dev_dbg(&s->dev, "No card present\n");
809                 return -ENODEV;
810         }
811         if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
812                 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
813                 return -EINVAL;
814         }
815
816         /* Window size defaults to smallest available */
817         if (req->Size == 0)
818                 req->Size = s->map_size;
819         align = (((s->features & SS_CAP_MEM_ALIGN) ||
820                   (req->Attributes & WIN_STRICT_ALIGN)) ?
821                  req->Size : s->map_size);
822         if (req->Size & (s->map_size-1)) {
823                 dev_dbg(&s->dev, "invalid map size\n");
824                 return -EINVAL;
825         }
826         if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
827             (req->Base & (align-1))) {
828                 dev_dbg(&s->dev, "invalid base address\n");
829                 return -EINVAL;
830         }
831         if (req->Base)
832                 align = 0;
833
834         /* Allocate system memory window */
835         for (w = 0; w < MAX_WIN; w++)
836                 if (!(s->state & SOCKET_WIN_REQ(w))) break;
837         if (w == MAX_WIN) {
838                 dev_dbg(&s->dev, "all windows are used already\n");
839                 return -EINVAL;
840         }
841
842         win = &s->win[w];
843         win->magic = WINDOW_MAGIC;
844         win->index = w;
845         win->handle = *p_dev;
846         win->sock = s;
847
848         if (!(s->features & SS_CAP_STATIC_MAP)) {
849                 win->ctl.res = pcmcia_find_mem_region(req->Base, req->Size, align,
850                                                       (req->Attributes & WIN_MAP_BELOW_1MB), s);
851                 if (!win->ctl.res) {
852                         dev_dbg(&s->dev, "allocating mem region failed\n");
853                         return -EINVAL;
854                 }
855         }
856         (*p_dev)->_win |= CLIENT_WIN_REQ(w);
857
858         /* Configure the socket controller */
859         win->ctl.map = w+1;
860         win->ctl.flags = 0;
861         win->ctl.speed = req->AccessSpeed;
862         if (req->Attributes & WIN_MEMORY_TYPE)
863                 win->ctl.flags |= MAP_ATTRIB;
864         if (req->Attributes & WIN_ENABLE)
865                 win->ctl.flags |= MAP_ACTIVE;
866         if (req->Attributes & WIN_DATA_WIDTH_16)
867                 win->ctl.flags |= MAP_16BIT;
868         if (req->Attributes & WIN_USE_WAIT)
869                 win->ctl.flags |= MAP_USE_WAIT;
870         win->ctl.card_start = 0;
871         if (s->ops->set_mem_map(s, &win->ctl) != 0) {
872                 dev_dbg(&s->dev, "failed to set memory mapping\n");
873                 return -EIO;
874         }
875         s->state |= SOCKET_WIN_REQ(w);
876
877         /* Return window handle */
878         if (s->features & SS_CAP_STATIC_MAP) {
879                 req->Base = win->ctl.static_start;
880         } else {
881                 req->Base = win->ctl.res->start;
882         }
883         *wh = win;
884
885         return 0;
886 } /* pcmcia_request_window */
887 EXPORT_SYMBOL(pcmcia_request_window);
888
889 void pcmcia_disable_device(struct pcmcia_device *p_dev) {
890         pcmcia_release_configuration(p_dev);
891         pcmcia_release_io(p_dev, &p_dev->io);
892         pcmcia_release_irq(p_dev, &p_dev->irq);
893         if (p_dev->win)
894                 pcmcia_release_window(p_dev->win);
895 }
896 EXPORT_SYMBOL(pcmcia_disable_device);
897
898
899 struct pcmcia_cfg_mem {
900         struct pcmcia_device *p_dev;
901         void *priv_data;
902         int (*conf_check) (struct pcmcia_device *p_dev,
903                            cistpl_cftable_entry_t *cfg,
904                            cistpl_cftable_entry_t *dflt,
905                            unsigned int vcc,
906                            void *priv_data);
907         cisparse_t parse;
908         cistpl_cftable_entry_t dflt;
909 };
910
911 /**
912  * pcmcia_do_loop_config() - internal helper for pcmcia_loop_config()
913  *
914  * pcmcia_do_loop_config() is the internal callback for the call from
915  * pcmcia_loop_config() to pccard_loop_tuple(). Data is transferred
916  * by a struct pcmcia_cfg_mem.
917  */
918 static int pcmcia_do_loop_config(tuple_t *tuple, cisparse_t *parse, void *priv)
919 {
920         cistpl_cftable_entry_t *cfg = &parse->cftable_entry;
921         struct pcmcia_cfg_mem *cfg_mem = priv;
922
923         /* default values */
924         cfg_mem->p_dev->conf.ConfigIndex = cfg->index;
925         if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
926                 cfg_mem->dflt = *cfg;
927
928         return cfg_mem->conf_check(cfg_mem->p_dev, cfg, &cfg_mem->dflt,
929                                    cfg_mem->p_dev->socket->socket.Vcc,
930                                    cfg_mem->priv_data);
931 }
932
933 /**
934  * pcmcia_loop_config() - loop over configuration options
935  * @p_dev:      the struct pcmcia_device which we need to loop for.
936  * @conf_check: function to call for each configuration option.
937  *              It gets passed the struct pcmcia_device, the CIS data
938  *              describing the configuration option, and private data
939  *              being passed to pcmcia_loop_config()
940  * @priv_data:  private data to be passed to the conf_check function.
941  *
942  * pcmcia_loop_config() loops over all configuration options, and calls
943  * the driver-specific conf_check() for each one, checking whether
944  * it is a valid one. Returns 0 on success or errorcode otherwise.
945  */
946 int pcmcia_loop_config(struct pcmcia_device *p_dev,
947                        int      (*conf_check)   (struct pcmcia_device *p_dev,
948                                                  cistpl_cftable_entry_t *cfg,
949                                                  cistpl_cftable_entry_t *dflt,
950                                                  unsigned int vcc,
951                                                  void *priv_data),
952                        void *priv_data)
953 {
954         struct pcmcia_cfg_mem *cfg_mem;
955         int ret;
956
957         cfg_mem = kzalloc(sizeof(struct pcmcia_cfg_mem), GFP_KERNEL);
958         if (cfg_mem == NULL)
959                 return -ENOMEM;
960
961         cfg_mem->p_dev = p_dev;
962         cfg_mem->conf_check = conf_check;
963         cfg_mem->priv_data = priv_data;
964
965         ret = pccard_loop_tuple(p_dev->socket, p_dev->func,
966                                 CISTPL_CFTABLE_ENTRY, &cfg_mem->parse,
967                                 cfg_mem, pcmcia_do_loop_config);
968
969         kfree(cfg_mem);
970         return ret;
971 }
972 EXPORT_SYMBOL(pcmcia_loop_config);
973
974
975 struct pcmcia_loop_mem {
976         struct pcmcia_device *p_dev;
977         void *priv_data;
978         int (*loop_tuple) (struct pcmcia_device *p_dev,
979                            tuple_t *tuple,
980                            void *priv_data);
981 };
982
983 /**
984  * pcmcia_do_loop_tuple() - internal helper for pcmcia_loop_config()
985  *
986  * pcmcia_do_loop_tuple() is the internal callback for the call from
987  * pcmcia_loop_tuple() to pccard_loop_tuple(). Data is transferred
988  * by a struct pcmcia_cfg_mem.
989  */
990 static int pcmcia_do_loop_tuple(tuple_t *tuple, cisparse_t *parse, void *priv)
991 {
992         struct pcmcia_loop_mem *loop = priv;
993
994         return loop->loop_tuple(loop->p_dev, tuple, loop->priv_data);
995 };
996
997 /**
998  * pcmcia_loop_tuple() - loop over tuples in the CIS
999  * @p_dev:      the struct pcmcia_device which we need to loop for.
1000  * @code:       which CIS code shall we look for?
1001  * @priv_data:  private data to be passed to the loop_tuple function.
1002  * @loop_tuple: function to call for each CIS entry of type @function. IT
1003  *              gets passed the raw tuple and @priv_data.
1004  *
1005  * pcmcia_loop_tuple() loops over all CIS entries of type @function, and
1006  * calls the @loop_tuple function for each entry. If the call to @loop_tuple
1007  * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
1008  */
1009 int pcmcia_loop_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1010                       int (*loop_tuple) (struct pcmcia_device *p_dev,
1011                                          tuple_t *tuple,
1012                                          void *priv_data),
1013                       void *priv_data)
1014 {
1015         struct pcmcia_loop_mem loop = {
1016                 .p_dev = p_dev,
1017                 .loop_tuple = loop_tuple,
1018                 .priv_data = priv_data};
1019
1020         return pccard_loop_tuple(p_dev->socket, p_dev->func, code, NULL,
1021                                  &loop, pcmcia_do_loop_tuple);
1022 };
1023 EXPORT_SYMBOL(pcmcia_loop_tuple);
1024
1025
1026 struct pcmcia_loop_get {
1027         size_t len;
1028         cisdata_t **buf;
1029 };
1030
1031 /**
1032  * pcmcia_do_get_tuple() - internal helper for pcmcia_get_tuple()
1033  *
1034  * pcmcia_do_get_tuple() is the internal callback for the call from
1035  * pcmcia_get_tuple() to pcmcia_loop_tuple(). As we're only interested in
1036  * the first tuple, return 0 unconditionally. Create a memory buffer large
1037  * enough to hold the content of the tuple, and fill it with the tuple data.
1038  * The caller is responsible to free the buffer.
1039  */
1040 static int pcmcia_do_get_tuple(struct pcmcia_device *p_dev, tuple_t *tuple,
1041                                void *priv)
1042 {
1043         struct pcmcia_loop_get *get = priv;
1044
1045         *get->buf = kzalloc(tuple->TupleDataLen, GFP_KERNEL);
1046         if (*get->buf) {
1047                 get->len = tuple->TupleDataLen;
1048                 memcpy(*get->buf, tuple->TupleData, tuple->TupleDataLen);
1049         } else
1050                 dev_dbg(&p_dev->dev, "do_get_tuple: out of memory\n");
1051         return 0;
1052 };
1053
1054 /**
1055  * pcmcia_get_tuple() - get first tuple from CIS
1056  * @p_dev:      the struct pcmcia_device which we need to loop for.
1057  * @code:       which CIS code shall we look for?
1058  * @buf:        pointer to store the buffer to.
1059  *
1060  * pcmcia_get_tuple() gets the content of the first CIS entry of type @code.
1061  * It returns the buffer length (or zero). The caller is responsible to free
1062  * the buffer passed in @buf.
1063  */
1064 size_t pcmcia_get_tuple(struct pcmcia_device *p_dev, cisdata_t code,
1065                         unsigned char **buf)
1066 {
1067         struct pcmcia_loop_get get = {
1068                 .len = 0,
1069                 .buf = buf,
1070         };
1071
1072         *get.buf = NULL;
1073         pcmcia_loop_tuple(p_dev, code, pcmcia_do_get_tuple, &get);
1074
1075         return get.len;
1076 };
1077 EXPORT_SYMBOL(pcmcia_get_tuple);
1078
1079
1080 /**
1081  * pcmcia_do_get_mac() - internal helper for pcmcia_get_mac_from_cis()
1082  *
1083  * pcmcia_do_get_mac() is the internal callback for the call from
1084  * pcmcia_get_mac_from_cis() to pcmcia_loop_tuple(). We check whether the
1085  * tuple contains a proper LAN_NODE_ID of length 6, and copy the data
1086  * to struct net_device->dev_addr[i].
1087  */
1088 static int pcmcia_do_get_mac(struct pcmcia_device *p_dev, tuple_t *tuple,
1089                              void *priv)
1090 {
1091         struct net_device *dev = priv;
1092         int i;
1093
1094         if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
1095                 return -EINVAL;
1096         if (tuple->TupleDataLen < ETH_ALEN + 2) {
1097                 dev_warn(&p_dev->dev, "Invalid CIS tuple length for "
1098                         "LAN_NODE_ID\n");
1099                 return -EINVAL;
1100         }
1101
1102         if (tuple->TupleData[1] != ETH_ALEN) {
1103                 dev_warn(&p_dev->dev, "Invalid header for LAN_NODE_ID\n");
1104                 return -EINVAL;
1105         }
1106         for (i = 0; i < 6; i++)
1107                 dev->dev_addr[i] = tuple->TupleData[i+2];
1108         return 0;
1109 };
1110
1111 /**
1112  * pcmcia_get_mac_from_cis() - read out MAC address from CISTPL_FUNCE
1113  * @p_dev:      the struct pcmcia_device for which we want the address.
1114  * @dev:        a properly prepared struct net_device to store the info to.
1115  *
1116  * pcmcia_get_mac_from_cis() reads out the hardware MAC address from
1117  * CISTPL_FUNCE and stores it into struct net_device *dev->dev_addr which
1118  * must be set up properly by the driver (see examples!).
1119  */
1120 int pcmcia_get_mac_from_cis(struct pcmcia_device *p_dev, struct net_device *dev)
1121 {
1122         return pcmcia_loop_tuple(p_dev, CISTPL_FUNCE, pcmcia_do_get_mac, dev);
1123 };
1124 EXPORT_SYMBOL(pcmcia_get_mac_from_cis);
1125