| David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * PCMCIA driver for SL811HS (as found in REX-CFU1U) | 
|  | 3 | * Filename: sl811_cs.c | 
|  | 4 | * Author:   Yukio Yamamoto | 
|  | 5 | * | 
|  | 6 | *  Port to sl811-hcd and 2.6.x by | 
|  | 7 | *    Botond Botyanszki <boti@rocketmail.com> | 
|  | 8 | *    Simon Pickering | 
|  | 9 | * | 
|  | 10 | *  Last update: 2005-05-12 | 
|  | 11 | */ | 
|  | 12 |  | 
|  | 13 | #include <linux/kernel.h> | 
|  | 14 | #include <linux/module.h> | 
|  | 15 | #include <linux/init.h> | 
|  | 16 | #include <linux/sched.h> | 
|  | 17 | #include <linux/ptrace.h> | 
|  | 18 | #include <linux/slab.h> | 
|  | 19 | #include <linux/string.h> | 
|  | 20 | #include <linux/timer.h> | 
|  | 21 | #include <linux/ioport.h> | 
|  | 22 |  | 
| David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 23 | #include <pcmcia/cs_types.h> | 
|  | 24 | #include <pcmcia/cs.h> | 
|  | 25 | #include <pcmcia/cistpl.h> | 
|  | 26 | #include <pcmcia/cisreg.h> | 
|  | 27 | #include <pcmcia/ds.h> | 
|  | 28 |  | 
|  | 29 | #include <linux/usb_sl811.h> | 
|  | 30 |  | 
|  | 31 | MODULE_AUTHOR("Botond Botyanszki"); | 
|  | 32 | MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6"); | 
|  | 33 | MODULE_LICENSE("GPL"); | 
|  | 34 |  | 
|  | 35 |  | 
|  | 36 | /*====================================================================*/ | 
|  | 37 | /* MACROS                                                             */ | 
|  | 38 | /*====================================================================*/ | 
|  | 39 |  | 
|  | 40 | #if defined(DEBUG) || defined(CONFIG_USB_DEBUG) || defined(PCMCIA_DEBUG) | 
|  | 41 |  | 
|  | 42 | static int pc_debug = 0; | 
|  | 43 | module_param(pc_debug, int, 0644); | 
|  | 44 |  | 
|  | 45 | #define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args) | 
|  | 46 |  | 
|  | 47 | #else | 
|  | 48 | #define DBG(n, args...) do{}while(0) | 
|  | 49 | #endif	/* no debugging */ | 
|  | 50 |  | 
|  | 51 | #define INFO(args...) printk(KERN_INFO "sl811_cs: " args) | 
|  | 52 |  | 
|  | 53 | #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444) | 
|  | 54 |  | 
|  | 55 | #define CS_CHECK(fn, ret) \ | 
|  | 56 | do { \ | 
|  | 57 | last_fn = (fn); \ | 
|  | 58 | if ((last_ret = (ret)) != 0) \ | 
|  | 59 | goto cs_failed; \ | 
|  | 60 | } while (0) | 
|  | 61 |  | 
|  | 62 | /*====================================================================*/ | 
|  | 63 | /* VARIABLES                                                          */ | 
|  | 64 | /*====================================================================*/ | 
|  | 65 |  | 
|  | 66 | static const char driver_name[DEV_NAME_LEN]  = "sl811_cs"; | 
|  | 67 |  | 
|  | 68 | static dev_link_t *dev_list = NULL; | 
|  | 69 |  | 
| David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 70 | typedef struct local_info_t { | 
|  | 71 | dev_link_t		link; | 
|  | 72 | dev_node_t		node; | 
|  | 73 | } local_info_t; | 
|  | 74 |  | 
|  | 75 | /*====================================================================*/ | 
|  | 76 |  | 
|  | 77 | static void release_platform_dev(struct device * dev) | 
|  | 78 | { | 
|  | 79 | DBG(0, "sl811_cs platform_dev release\n"); | 
|  | 80 | dev->parent = NULL; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | static struct sl811_platform_data platform_data = { | 
|  | 84 | .potpg		= 100, | 
|  | 85 | .power		= 50,		/* == 100mA */ | 
|  | 86 | // .reset	= ... FIXME:  invoke CF reset on the card | 
|  | 87 | }; | 
|  | 88 |  | 
|  | 89 | static struct resource resources[] = { | 
|  | 90 | [0] = { | 
|  | 91 | .flags	= IORESOURCE_IRQ, | 
|  | 92 | }, | 
|  | 93 | [1] = { | 
|  | 94 | // .name   = "address", | 
|  | 95 | .flags	= IORESOURCE_IO, | 
|  | 96 | }, | 
|  | 97 | [2] = { | 
|  | 98 | // .name   = "data", | 
|  | 99 | .flags	= IORESOURCE_IO, | 
|  | 100 | }, | 
|  | 101 | }; | 
|  | 102 |  | 
|  | 103 | extern struct device_driver sl811h_driver; | 
|  | 104 |  | 
|  | 105 | static struct platform_device platform_dev = { | 
|  | 106 | .id			= -1, | 
|  | 107 | .dev = { | 
|  | 108 | .platform_data = &platform_data, | 
|  | 109 | .release       = release_platform_dev, | 
|  | 110 | }, | 
|  | 111 | .resource		= resources, | 
|  | 112 | .num_resources		= ARRAY_SIZE(resources), | 
|  | 113 | }; | 
|  | 114 |  | 
|  | 115 | static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq) | 
|  | 116 | { | 
|  | 117 | if (platform_dev.dev.parent) | 
|  | 118 | return -EBUSY; | 
|  | 119 | platform_dev.dev.parent = parent; | 
|  | 120 |  | 
|  | 121 | /* finish seting up the platform device */ | 
|  | 122 | resources[0].start = irq; | 
|  | 123 |  | 
|  | 124 | resources[1].start = base_addr; | 
|  | 125 | resources[1].end = base_addr; | 
|  | 126 |  | 
|  | 127 | resources[2].start = base_addr + 1; | 
|  | 128 | resources[2].end   = base_addr + 1; | 
|  | 129 |  | 
|  | 130 | /* The driver core will probe for us.  We know sl811-hcd has been | 
|  | 131 | * initialized already because of the link order dependency. | 
|  | 132 | */ | 
|  | 133 | platform_dev.name = sl811h_driver.name; | 
|  | 134 | return platform_device_register(&platform_dev); | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | /*====================================================================*/ | 
|  | 138 |  | 
|  | 139 | static void sl811_cs_detach(dev_link_t *link) | 
|  | 140 | { | 
|  | 141 | dev_link_t **linkp; | 
|  | 142 |  | 
|  | 143 | DBG(0, "sl811_cs_detach(0x%p)\n", link); | 
|  | 144 |  | 
|  | 145 | /* Locate device structure */ | 
|  | 146 | for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) { | 
|  | 147 | if (*linkp == link) | 
|  | 148 | break; | 
|  | 149 | } | 
|  | 150 | if (*linkp == NULL) | 
|  | 151 | return; | 
|  | 152 |  | 
|  | 153 | /* Break the link with Card Services */ | 
|  | 154 | if (link->handle) | 
|  | 155 | pcmcia_deregister_client(link->handle); | 
|  | 156 |  | 
|  | 157 | /* Unlink device structure, and free it */ | 
|  | 158 | *linkp = link->next; | 
|  | 159 | /* This points to the parent local_info_t struct */ | 
|  | 160 | kfree(link->priv); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | static void sl811_cs_release(dev_link_t * link) | 
|  | 164 | { | 
|  | 165 |  | 
|  | 166 | DBG(0, "sl811_cs_release(0x%p)\n", link); | 
|  | 167 |  | 
|  | 168 | if (link->open) { | 
|  | 169 | DBG(1, "sl811_cs: release postponed, '%s' still open\n", | 
|  | 170 | link->dev->dev_name); | 
|  | 171 | link->state |= DEV_STALE_CONFIG; | 
|  | 172 | return; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | /* Unlink the device chain */ | 
|  | 176 | link->dev = NULL; | 
|  | 177 |  | 
|  | 178 | platform_device_unregister(&platform_dev); | 
|  | 179 | pcmcia_release_configuration(link->handle); | 
|  | 180 | if (link->io.NumPorts1) | 
|  | 181 | pcmcia_release_io(link->handle, &link->io); | 
|  | 182 | if (link->irq.AssignedIRQ) | 
|  | 183 | pcmcia_release_irq(link->handle, &link->irq); | 
|  | 184 | link->state &= ~DEV_CONFIG; | 
|  | 185 |  | 
|  | 186 | if (link->state & DEV_STALE_LINK) | 
|  | 187 | sl811_cs_detach(link); | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | static void sl811_cs_config(dev_link_t *link) | 
|  | 191 | { | 
|  | 192 | client_handle_t		handle = link->handle; | 
|  | 193 | struct device		*parent = &handle_to_dev(handle); | 
|  | 194 | local_info_t		*dev = link->priv; | 
|  | 195 | tuple_t			tuple; | 
|  | 196 | cisparse_t		parse; | 
|  | 197 | int			last_fn, last_ret; | 
|  | 198 | u_char			buf[64]; | 
|  | 199 | config_info_t		conf; | 
|  | 200 | cistpl_cftable_entry_t	dflt = { 0 }; | 
|  | 201 |  | 
|  | 202 | DBG(0, "sl811_cs_config(0x%p)\n", link); | 
|  | 203 |  | 
|  | 204 | tuple.DesiredTuple = CISTPL_CONFIG; | 
|  | 205 | tuple.Attributes = 0; | 
|  | 206 | tuple.TupleData = buf; | 
|  | 207 | tuple.TupleDataMax = sizeof(buf); | 
|  | 208 | tuple.TupleOffset = 0; | 
|  | 209 | CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple)); | 
|  | 210 | CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple)); | 
|  | 211 | CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse)); | 
|  | 212 | link->conf.ConfigBase = parse.config.base; | 
|  | 213 | link->conf.Present = parse.config.rmask[0]; | 
|  | 214 |  | 
|  | 215 | /* Configure card */ | 
|  | 216 | link->state |= DEV_CONFIG; | 
|  | 217 |  | 
|  | 218 | /* Look up the current Vcc */ | 
|  | 219 | CS_CHECK(GetConfigurationInfo, | 
|  | 220 | pcmcia_get_configuration_info(handle, &conf)); | 
|  | 221 | link->conf.Vcc = conf.Vcc; | 
|  | 222 |  | 
|  | 223 | tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; | 
|  | 224 | CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple)); | 
|  | 225 | while (1) { | 
|  | 226 | cistpl_cftable_entry_t	*cfg = &(parse.cftable_entry); | 
|  | 227 |  | 
|  | 228 | if (pcmcia_get_tuple_data(handle, &tuple) != 0 | 
|  | 229 | || pcmcia_parse_tuple(handle, &tuple, &parse) | 
|  | 230 | != 0) | 
|  | 231 | goto next_entry; | 
|  | 232 |  | 
|  | 233 | if (cfg->flags & CISTPL_CFTABLE_DEFAULT) { | 
|  | 234 | dflt = *cfg; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | if (cfg->index == 0) | 
|  | 238 | goto next_entry; | 
|  | 239 |  | 
|  | 240 | link->conf.ConfigIndex = cfg->index; | 
|  | 241 |  | 
|  | 242 | /* Use power settings for Vcc and Vpp if present */ | 
|  | 243 | /*  Note that the CIS values need to be rescaled */ | 
|  | 244 | if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) { | 
|  | 245 | if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000 | 
|  | 246 | != conf.Vcc) | 
|  | 247 | goto next_entry; | 
|  | 248 | } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) { | 
|  | 249 | if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000 | 
|  | 250 | != conf.Vcc) | 
|  | 251 | goto next_entry; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM)) | 
|  | 255 | link->conf.Vpp1 = link->conf.Vpp2 = | 
|  | 256 | cfg->vpp1.param[CISTPL_POWER_VNOM]/10000; | 
|  | 257 | else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM)) | 
|  | 258 | link->conf.Vpp1 = link->conf.Vpp2 = | 
|  | 259 | dflt.vpp1.param[CISTPL_POWER_VNOM]/10000; | 
|  | 260 |  | 
|  | 261 | /* we need an interrupt */ | 
|  | 262 | if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1) | 
|  | 263 | link->conf.Attributes |= CONF_ENABLE_IRQ; | 
|  | 264 |  | 
|  | 265 | /* IO window settings */ | 
|  | 266 | link->io.NumPorts1 = link->io.NumPorts2 = 0; | 
|  | 267 | if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) { | 
|  | 268 | cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io; | 
|  | 269 |  | 
|  | 270 | link->io.Attributes1 = IO_DATA_PATH_WIDTH_8; | 
|  | 271 | link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK; | 
|  | 272 | link->io.BasePort1 = io->win[0].base; | 
|  | 273 | link->io.NumPorts1 = io->win[0].len; | 
|  | 274 |  | 
|  | 275 | if (pcmcia_request_io(link->handle, &link->io) != 0) | 
|  | 276 | goto next_entry; | 
|  | 277 | } | 
|  | 278 | break; | 
|  | 279 |  | 
|  | 280 | next_entry: | 
|  | 281 | if (link->io.NumPorts1) | 
|  | 282 | pcmcia_release_io(link->handle, &link->io); | 
|  | 283 | last_ret = pcmcia_get_next_tuple(handle, &tuple); | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | /* require an IRQ and two registers */ | 
|  | 287 | if (!link->io.NumPorts1 || link->io.NumPorts1 < 2) | 
|  | 288 | goto cs_failed; | 
|  | 289 | if (link->conf.Attributes & CONF_ENABLE_IRQ) | 
|  | 290 | CS_CHECK(RequestIRQ, | 
|  | 291 | pcmcia_request_irq(link->handle, &link->irq)); | 
|  | 292 | else | 
|  | 293 | goto cs_failed; | 
|  | 294 |  | 
|  | 295 | CS_CHECK(RequestConfiguration, | 
|  | 296 | pcmcia_request_configuration(link->handle, &link->conf)); | 
|  | 297 |  | 
|  | 298 | sprintf(dev->node.dev_name, driver_name); | 
|  | 299 | dev->node.major = dev->node.minor = 0; | 
|  | 300 | link->dev = &dev->node; | 
|  | 301 |  | 
|  | 302 | printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d", | 
|  | 303 | dev->node.dev_name, link->conf.ConfigIndex, | 
|  | 304 | link->conf.Vcc/10, link->conf.Vcc%10); | 
|  | 305 | if (link->conf.Vpp1) | 
|  | 306 | printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10); | 
|  | 307 | printk(", irq %d", link->irq.AssignedIRQ); | 
|  | 308 | printk(", io 0x%04x-0x%04x", link->io.BasePort1, | 
|  | 309 | link->io.BasePort1+link->io.NumPorts1-1); | 
|  | 310 | printk("\n"); | 
|  | 311 |  | 
|  | 312 | link->state &= ~DEV_CONFIG_PENDING; | 
|  | 313 |  | 
|  | 314 | if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ) | 
|  | 315 | < 0) { | 
|  | 316 | cs_failed: | 
|  | 317 | printk("sl811_cs_config failed\n"); | 
|  | 318 | cs_error(link->handle, last_fn, last_ret); | 
|  | 319 | sl811_cs_release(link); | 
|  | 320 | link->state &= ~DEV_CONFIG_PENDING; | 
|  | 321 | } | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | static int | 
|  | 325 | sl811_cs_event(event_t event, int priority, event_callback_args_t *args) | 
|  | 326 | { | 
|  | 327 | dev_link_t *link = args->client_data; | 
|  | 328 |  | 
|  | 329 | DBG(1, "sl811_cs_event(0x%06x)\n", event); | 
|  | 330 |  | 
|  | 331 | switch (event) { | 
|  | 332 | case CS_EVENT_CARD_REMOVAL: | 
|  | 333 | link->state &= ~DEV_PRESENT; | 
|  | 334 | if (link->state & DEV_CONFIG) | 
|  | 335 | sl811_cs_release(link); | 
|  | 336 | break; | 
|  | 337 |  | 
|  | 338 | case CS_EVENT_CARD_INSERTION: | 
|  | 339 | link->state |= DEV_PRESENT | DEV_CONFIG_PENDING; | 
|  | 340 | sl811_cs_config(link); | 
|  | 341 | break; | 
|  | 342 |  | 
|  | 343 | case CS_EVENT_PM_SUSPEND: | 
|  | 344 | link->state |= DEV_SUSPEND; | 
|  | 345 | /* Fall through... */ | 
|  | 346 | case CS_EVENT_RESET_PHYSICAL: | 
|  | 347 | if (link->state & DEV_CONFIG) | 
|  | 348 | pcmcia_release_configuration(link->handle); | 
|  | 349 | break; | 
|  | 350 |  | 
|  | 351 | case CS_EVENT_PM_RESUME: | 
|  | 352 | link->state &= ~DEV_SUSPEND; | 
|  | 353 | /* Fall through... */ | 
|  | 354 | case CS_EVENT_CARD_RESET: | 
|  | 355 | if (link->state & DEV_CONFIG) | 
|  | 356 | pcmcia_request_configuration(link->handle, &link->conf); | 
|  | 357 | DBG(0, "reset sl811-hcd here?\n"); | 
|  | 358 | break; | 
|  | 359 | } | 
|  | 360 | return 0; | 
|  | 361 | } | 
|  | 362 |  | 
|  | 363 | static dev_link_t *sl811_cs_attach(void) | 
|  | 364 | { | 
|  | 365 | local_info_t *local; | 
|  | 366 | dev_link_t *link; | 
|  | 367 | client_reg_t client_reg; | 
| David Brownell | 22f3a8f | 2005-06-27 16:28:43 -0700 | [diff] [blame] | 368 | int ret; | 
| David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 369 |  | 
|  | 370 | local = kmalloc(sizeof(local_info_t), GFP_KERNEL); | 
|  | 371 | if (!local) | 
|  | 372 | return NULL; | 
|  | 373 | memset(local, 0, sizeof(local_info_t)); | 
|  | 374 | link = &local->link; | 
|  | 375 | link->priv = local; | 
|  | 376 |  | 
|  | 377 | /* Initialize */ | 
|  | 378 | link->irq.Attributes = IRQ_TYPE_EXCLUSIVE; | 
|  | 379 | link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID; | 
| David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 380 | link->irq.Handler = NULL; | 
|  | 381 |  | 
|  | 382 | link->conf.Attributes = 0; | 
|  | 383 | link->conf.Vcc = 33; | 
|  | 384 | link->conf.IntType = INT_MEMORY_AND_IO; | 
|  | 385 |  | 
|  | 386 | /* Register with Card Services */ | 
|  | 387 | link->next = dev_list; | 
|  | 388 | dev_list = link; | 
|  | 389 | client_reg.dev_info = (dev_info_t *) &driver_name; | 
|  | 390 | client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE; | 
| David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 391 | client_reg.Version = 0x0210; | 
|  | 392 | client_reg.event_callback_args.client_data = link; | 
|  | 393 | ret = pcmcia_register_client(&link->handle, &client_reg); | 
|  | 394 | if (ret != CS_SUCCESS) { | 
|  | 395 | cs_error(link->handle, RegisterClient, ret); | 
|  | 396 | sl811_cs_detach(link); | 
|  | 397 | return NULL; | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | return link; | 
|  | 401 | } | 
|  | 402 |  | 
| David Brownell | 22f3a8f | 2005-06-27 16:28:43 -0700 | [diff] [blame] | 403 | static struct pcmcia_device_id sl811_ids[] = { | 
|  | 404 | PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */ | 
|  | 405 | PCMCIA_DEVICE_NULL, | 
|  | 406 | }; | 
|  | 407 | MODULE_DEVICE_TABLE(pcmcia, sl811_ids); | 
|  | 408 |  | 
| David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 409 | static struct pcmcia_driver sl811_cs_driver = { | 
|  | 410 | .owner		= THIS_MODULE, | 
|  | 411 | .drv		= { | 
|  | 412 | .name	= (char *)driver_name, | 
|  | 413 | }, | 
|  | 414 | .attach		= sl811_cs_attach, | 
| Dominik Brodowski | 1e212f3 | 2005-07-07 17:59:00 -0700 | [diff] [blame] | 415 | .event		= sl811_cs_event, | 
| David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 416 | .detach		= sl811_cs_detach, | 
| David Brownell | 22f3a8f | 2005-06-27 16:28:43 -0700 | [diff] [blame] | 417 | .id_table	= sl811_ids, | 
| David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 418 | }; | 
|  | 419 |  | 
|  | 420 | /*====================================================================*/ | 
|  | 421 |  | 
|  | 422 | static int __init init_sl811_cs(void) | 
|  | 423 | { | 
|  | 424 | return pcmcia_register_driver(&sl811_cs_driver); | 
|  | 425 | } | 
|  | 426 | module_init(init_sl811_cs); | 
|  | 427 |  | 
|  | 428 | static void __exit exit_sl811_cs(void) | 
|  | 429 | { | 
|  | 430 | pcmcia_unregister_driver(&sl811_cs_driver); | 
|  | 431 | } | 
|  | 432 | module_exit(exit_sl811_cs); |