Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PCMCIA client driver for AVM A1 / Fritz!PCMCIA |
| 3 | * |
| 4 | * Author Carsten Paeth |
| 5 | * Copyright 1998-2001 by Carsten Paeth <calle@calle.in-berlin.de> |
| 6 | * |
| 7 | * This software may be used and distributed according to the terms |
| 8 | * of the GNU General Public License, incorporated herein by reference. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/ptrace.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <asm/io.h> |
| 21 | #include <asm/system.h> |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <pcmcia/cs_types.h> |
| 24 | #include <pcmcia/cs.h> |
| 25 | #include <pcmcia/cistpl.h> |
| 26 | #include <pcmcia/ds.h> |
| 27 | #include "hisax_cfg.h" |
| 28 | |
| 29 | MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for AVM A1/Fritz!PCMCIA cards"); |
| 30 | MODULE_AUTHOR("Carsten Paeth"); |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /*====================================================================*/ |
| 35 | |
| 36 | /* Parameters that can be set with 'insmod' */ |
| 37 | |
| 38 | static int isdnprot = 2; |
| 39 | |
| 40 | module_param(isdnprot, int, 0); |
| 41 | |
| 42 | /*====================================================================*/ |
| 43 | |
| 44 | /* |
| 45 | The event() function is this driver's Card Services event handler. |
| 46 | It will be called by Card Services when an appropriate card status |
| 47 | event is received. The config() and release() entry points are |
| 48 | used to configure or release a socket, in response to card insertion |
| 49 | and ejection events. They are invoked from the skeleton event |
| 50 | handler. |
| 51 | */ |
| 52 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 53 | static int avma1cs_config(struct pcmcia_device *link); |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 54 | static void avma1cs_release(struct pcmcia_device *link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | The attach() and detach() entry points are used to create and destroy |
| 58 | "instances" of the driver, where each instance represents everything |
| 59 | needed to manage one actual PCMCIA card. |
| 60 | */ |
| 61 | |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 62 | static void avma1cs_detach(struct pcmcia_device *p_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | A linked list of "instances" of the skeleton device. Each actual |
| 67 | PCMCIA card corresponds to one device instance, and is described |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 68 | by one struct pcmcia_device structure (defined in ds.h). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | You may not want to use a linked list for this -- for example, the |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 71 | memory card driver uses an array of struct pcmcia_device pointers, where minor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | device numbers are used to derive the corresponding array index. |
| 73 | */ |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | A driver needs to provide a dev_node_t structure for each device |
| 77 | on a card. In some cases, there is only one device per card (for |
| 78 | example, ethernet cards, modems). In other cases, there may be |
| 79 | many actual or logical devices (SCSI adapters, memory cards with |
| 80 | multiple partitions). The dev_node_t structures need to be kept |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 81 | in a linked list starting at the 'dev' field of a struct pcmcia_device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | structure. We allocate them in the card's private data structure, |
| 83 | because they generally can't be allocated dynamically. |
| 84 | */ |
| 85 | |
| 86 | typedef struct local_info_t { |
| 87 | dev_node_t node; |
| 88 | } local_info_t; |
| 89 | |
| 90 | /*====================================================================== |
| 91 | |
| 92 | avma1cs_attach() creates an "instance" of the driver, allocating |
| 93 | local data structures for one device. The device is registered |
| 94 | with Card Services. |
| 95 | |
| 96 | The dev_link structure is initialized, but we don't actually |
| 97 | configure the card at this point -- we wait until we receive a |
| 98 | card insertion event. |
| 99 | |
| 100 | ======================================================================*/ |
| 101 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 102 | static int avma1cs_probe(struct pcmcia_device *p_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | local_info_t *local; |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 105 | |
Dominik Brodowski | e773cfe | 2009-10-24 15:50:13 +0200 | [diff] [blame] | 106 | dev_dbg(&p_dev->dev, "avma1cs_attach()\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | /* Allocate space for private device-specific data */ |
Burman Yan | 41f9693 | 2006-12-08 02:39:35 -0800 | [diff] [blame] | 109 | local = kzalloc(sizeof(local_info_t), GFP_KERNEL); |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 110 | if (!local) |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 111 | return -ENOMEM; |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 112 | |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 113 | p_dev->priv = local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | /* The io structure describes IO port mapping */ |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 116 | p_dev->io.NumPorts1 = 16; |
| 117 | p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8; |
| 118 | p_dev->io.NumPorts2 = 16; |
| 119 | p_dev->io.Attributes2 = IO_DATA_PATH_WIDTH_16; |
| 120 | p_dev->io.IOAddrLines = 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* Interrupt setup */ |
Dominik Brodowski | e15c1c1 | 2009-11-28 18:12:06 +0100 | [diff] [blame^] | 123 | p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* General socket configuration */ |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 126 | p_dev->conf.Attributes = CONF_ENABLE_IRQ; |
| 127 | p_dev->conf.IntType = INT_MEMORY_AND_IO; |
| 128 | p_dev->conf.ConfigIndex = 1; |
| 129 | p_dev->conf.Present = PRESENT_OPTION; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 131 | return avma1cs_config(p_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } /* avma1cs_attach */ |
| 133 | |
| 134 | /*====================================================================== |
| 135 | |
| 136 | This deletes a driver "instance". The device is de-registered |
| 137 | with Card Services. If it has been released, all local data |
| 138 | structures are freed. Otherwise, the structures will be freed |
| 139 | when the device is released. |
| 140 | |
| 141 | ======================================================================*/ |
| 142 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 143 | static void avma1cs_detach(struct pcmcia_device *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Dominik Brodowski | e773cfe | 2009-10-24 15:50:13 +0200 | [diff] [blame] | 145 | dev_dbg(&link->dev, "avma1cs_detach(0x%p)\n", link); |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 146 | avma1cs_release(link); |
| 147 | kfree(link->priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } /* avma1cs_detach */ |
| 149 | |
| 150 | /*====================================================================== |
| 151 | |
| 152 | avma1cs_config() is scheduled to run after a CARD_INSERTION event |
| 153 | is received, to configure the PCMCIA socket, and to make the |
| 154 | ethernet device available to the system. |
| 155 | |
| 156 | ======================================================================*/ |
| 157 | |
Dominik Brodowski | 5fcd4da | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 158 | static int avma1cs_configcheck(struct pcmcia_device *p_dev, |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 159 | cistpl_cftable_entry_t *cf, |
| 160 | cistpl_cftable_entry_t *dflt, |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 161 | unsigned int vcc, |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 162 | void *priv_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
Dominik Brodowski | 5fcd4da | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 164 | if (cf->io.nwin <= 0) |
| 165 | return -ENODEV; |
| 166 | |
Dominik Brodowski | 5fcd4da | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 167 | p_dev->io.BasePort1 = cf->io.win[0].base; |
| 168 | p_dev->io.NumPorts1 = cf->io.win[0].len; |
| 169 | p_dev->io.NumPorts2 = 0; |
| 170 | printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n", |
| 171 | p_dev->io.BasePort1, |
| 172 | p_dev->io.BasePort1+p_dev->io.NumPorts1-1); |
| 173 | return pcmcia_request_io(p_dev, &p_dev->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 177 | static int avma1cs_config(struct pcmcia_device *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | local_info_t *dev; |
| 180 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | char devname[128]; |
| 182 | IsdnCard_t icard; |
| 183 | int busy = 0; |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | dev = link->priv; |
| 186 | |
Dominik Brodowski | e773cfe | 2009-10-24 15:50:13 +0200 | [diff] [blame] | 187 | dev_dbg(&link->dev, "avma1cs_config(0x%p)\n", link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Dominik Brodowski | 5fcd4da | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 189 | devname[0] = 0; |
| 190 | if (link->prod_id[1]) |
| 191 | strlcpy(devname, link->prod_id[1], sizeof(devname)); |
| 192 | |
| 193 | if (pcmcia_loop_config(link, avma1cs_configcheck, NULL)) |
| 194 | return -ENODEV; |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /* |
| 198 | * allocate an interrupt line |
| 199 | */ |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 200 | i = pcmcia_request_irq(link, &link->irq); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 201 | if (i != 0) { |
Dominik Brodowski | 50db3fd | 2006-01-15 10:05:19 +0100 | [diff] [blame] | 202 | /* undo */ |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 203 | pcmcia_disable_device(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | break; |
| 205 | } |
Dominik Brodowski | 50db3fd | 2006-01-15 10:05:19 +0100 | [diff] [blame] | 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | /* |
| 208 | * configure the PCMCIA socket |
| 209 | */ |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 210 | i = pcmcia_request_configuration(link, &link->conf); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 211 | if (i != 0) { |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 212 | pcmcia_disable_device(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | |
| 216 | } while (0); |
| 217 | |
| 218 | /* At this point, the dev_node_t structure(s) should be |
| 219 | initialized and arranged in a linked list at link->dev. */ |
| 220 | |
| 221 | strcpy(dev->node.dev_name, "A1"); |
| 222 | dev->node.major = 45; |
| 223 | dev->node.minor = 0; |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 224 | link->dev_node = &dev->node; |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | /* If any step failed, release any partially configured state */ |
| 227 | if (i != 0) { |
| 228 | avma1cs_release(link); |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 229 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n", |
| 233 | link->io.BasePort1, link->irq.AssignedIRQ); |
| 234 | |
| 235 | icard.para[0] = link->irq.AssignedIRQ; |
| 236 | icard.para[1] = link->io.BasePort1; |
| 237 | icard.protocol = isdnprot; |
| 238 | icard.typ = ISDN_CTYPE_A1_PCMCIA; |
| 239 | |
| 240 | i = hisax_init_pcmcia(link, &busy, &icard); |
| 241 | if (i < 0) { |
| 242 | printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1); |
| 243 | avma1cs_release(link); |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 244 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | dev->node.minor = i; |
| 247 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 248 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } /* avma1cs_config */ |
| 250 | |
| 251 | /*====================================================================== |
| 252 | |
| 253 | After a card is removed, avma1cs_release() will unregister the net |
| 254 | device, and release the PCMCIA configuration. If the device is |
| 255 | still open, this will be postponed until it is closed. |
| 256 | |
| 257 | ======================================================================*/ |
| 258 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 259 | static void avma1cs_release(struct pcmcia_device *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 261 | local_info_t *local = link->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Dominik Brodowski | e773cfe | 2009-10-24 15:50:13 +0200 | [diff] [blame] | 263 | dev_dbg(&link->dev, "avma1cs_release(0x%p)\n", link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 265 | /* now unregister function with hisax */ |
| 266 | HiSax_closecard(local->node.minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 268 | pcmcia_disable_device(link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } /* avma1cs_release */ |
| 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Dominik Brodowski | c594c12 | 2005-06-27 16:28:34 -0700 | [diff] [blame] | 272 | static struct pcmcia_device_id avma1cs_ids[] = { |
| 273 | PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb), |
| 274 | PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b), |
| 275 | PCMCIA_DEVICE_NULL |
| 276 | }; |
| 277 | MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids); |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | static struct pcmcia_driver avma1cs_driver = { |
| 280 | .owner = THIS_MODULE, |
| 281 | .drv = { |
| 282 | .name = "avma1_cs", |
| 283 | }, |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 284 | .probe = avma1cs_probe, |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 285 | .remove = avma1cs_detach, |
Dominik Brodowski | c594c12 | 2005-06-27 16:28:34 -0700 | [diff] [blame] | 286 | .id_table = avma1cs_ids, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | }; |
Dominik Brodowski | 8661bb5 | 2006-03-02 00:02:33 +0100 | [diff] [blame] | 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | /*====================================================================*/ |
| 290 | |
| 291 | static int __init init_avma1_cs(void) |
| 292 | { |
| 293 | return(pcmcia_register_driver(&avma1cs_driver)); |
| 294 | } |
| 295 | |
| 296 | static void __exit exit_avma1_cs(void) |
| 297 | { |
| 298 | pcmcia_unregister_driver(&avma1cs_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | module_init(init_avma1_cs); |
| 302 | module_exit(exit_avma1_cs); |