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