ni_daq_dio24.c revision 25436dc9d84f1be60ff549c9ab712bba2835f284
1/* 2 comedi/drivers/ni_daq_dio24.c 3 Driver for National Instruments PCMCIA DAQ-Card DIO-24 4 Copyright (C) 2002 Daniel Vecino Castel <dvecino@able.es> 5 6 PCMCIA crap at end of file is adapted from dummy_cs.c 1.31 2001/08/24 12:13:13 7 from the pcmcia package. 8 The initial developer of the pcmcia dummy_cs.c code is David A. Hinds 9 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds 10 are Copyright (C) 1999 David A. Hinds. All Rights Reserved. 11 12 This program is free software; you can redistribute it and/or modify 13 it under the terms of the GNU General Public License as published by 14 the Free Software Foundation; either version 2 of the License, or 15 (at your option) any later version. 16 17 This program is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 GNU General Public License for more details. 21 22 You should have received a copy of the GNU General Public License 23 along with this program; if not, write to the Free Software 24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 25 26************************************************************************ 27*/ 28/* 29Driver: ni_daq_dio24 30Description: National Instruments PCMCIA DAQ-Card DIO-24 31Author: Daniel Vecino Castel <dvecino@able.es> 32Devices: [National Instruments] PCMCIA DAQ-Card DIO-24 (ni_daq_dio24) 33Status: ? 34Updated: Thu, 07 Nov 2002 21:53:06 -0800 35 36This is just a wrapper around the 8255.o driver to properly handle 37the PCMCIA interface. 38*/ 39 40/* #define LABPC_DEBUG */ /* enable debugging messages */ 41#undef LABPC_DEBUG 42 43#include <linux/interrupt.h> 44#include "../comedidev.h" 45 46#include <linux/ioport.h> 47 48#include "8255.h" 49 50#include <pcmcia/cs_types.h> 51#include <pcmcia/cs.h> 52#include <pcmcia/cistpl.h> 53#include <pcmcia/cisreg.h> 54#include <pcmcia/ds.h> 55 56static struct pcmcia_device *pcmcia_cur_dev = NULL; 57 58#define DIO24_SIZE 4 /* size of io region used by board */ 59 60static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it); 61static int dio24_detach(struct comedi_device *dev); 62 63enum dio24_bustype { pcmcia_bustype }; 64 65struct dio24_board_struct { 66 const char *name; 67 int device_id; /* device id for pcmcia board */ 68 enum dio24_bustype bustype; /* PCMCIA */ 69 int have_dio; /* have 8255 chip */ 70 /* function pointers so we can use inb/outb or readb/writeb as appropriate */ 71 unsigned int (*read_byte) (unsigned int address); 72 void (*write_byte) (unsigned int byte, unsigned int address); 73}; 74 75static const struct dio24_board_struct dio24_boards[] = { 76 { 77 .name = "daqcard-dio24", 78 .device_id = 0x475c,/* 0x10b is manufacturer id, 0x475c is device id */ 79 .bustype = pcmcia_bustype, 80 .have_dio = 1, 81 }, 82 { 83 .name = "ni_daq_dio24", 84 .device_id = 0x475c,/* 0x10b is manufacturer id, 0x475c is device id */ 85 .bustype = pcmcia_bustype, 86 .have_dio = 1, 87 }, 88}; 89 90/* 91 * Useful for shorthand access to the particular board structure 92 */ 93#define thisboard ((const struct dio24_board_struct *)dev->board_ptr) 94 95struct dio24_private { 96 97 int data; /* number of data points left to be taken */ 98}; 99 100 101#define devpriv ((struct dio24_private *)dev->private) 102 103static struct comedi_driver driver_dio24 = { 104 .driver_name = "ni_daq_dio24", 105 .module = THIS_MODULE, 106 .attach = dio24_attach, 107 .detach = dio24_detach, 108 .num_names = ARRAY_SIZE(dio24_boards), 109 .board_name = &dio24_boards[0].name, 110 .offset = sizeof(struct dio24_board_struct), 111}; 112 113static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it) 114{ 115 struct comedi_subdevice *s; 116 unsigned long iobase = 0; 117#ifdef incomplete 118 unsigned int irq = 0; 119#endif 120 struct pcmcia_device *link; 121 122 /* allocate and initialize dev->private */ 123 if (alloc_private(dev, sizeof(struct dio24_private)) < 0) 124 return -ENOMEM; 125 126 /* get base address, irq etc. based on bustype */ 127 switch (thisboard->bustype) { 128 case pcmcia_bustype: 129 link = pcmcia_cur_dev; /* XXX hack */ 130 if (!link) 131 return -EIO; 132 iobase = link->io.BasePort1; 133#ifdef incomplete 134 irq = link->irq.AssignedIRQ; 135#endif 136 break; 137 default: 138 printk("bug! couldn't determine board type\n"); 139 return -EINVAL; 140 break; 141 } 142 printk("comedi%d: ni_daq_dio24: %s, io 0x%lx", dev->minor, 143 thisboard->name, iobase); 144#ifdef incomplete 145 if (irq) { 146 printk(", irq %u", irq); 147 } 148#endif 149 150 printk("\n"); 151 152 if (iobase == 0) { 153 printk("io base address is zero!\n"); 154 return -EINVAL; 155 } 156 157 dev->iobase = iobase; 158 159#ifdef incomplete 160 /* grab our IRQ */ 161 dev->irq = irq; 162#endif 163 164 dev->board_name = thisboard->name; 165 166 if (alloc_subdevices(dev, 1) < 0) 167 return -ENOMEM; 168 169 /* 8255 dio */ 170 s = dev->subdevices + 0; 171 subdev_8255_init(dev, s, NULL, dev->iobase); 172 173 return 0; 174}; 175 176static int dio24_detach(struct comedi_device *dev) 177{ 178 printk("comedi%d: ni_daq_dio24: remove\n", dev->minor); 179 180 if (dev->subdevices) 181 subdev_8255_cleanup(dev, dev->subdevices + 0); 182 183 if (thisboard->bustype != pcmcia_bustype && dev->iobase) 184 release_region(dev->iobase, DIO24_SIZE); 185 if (dev->irq) 186 free_irq(dev->irq, dev); 187 188 return 0; 189}; 190 191/* PCMCIA crap */ 192 193/* 194 All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If 195 you do not define PCMCIA_DEBUG at all, all the debug code will be 196 left out. If you compile with PCMCIA_DEBUG=0, the debug code will 197 be present but disabled -- but it can then be enabled for specific 198 modules at load time with a 'pc_debug=#' option to insmod. 199*/ 200#ifdef PCMCIA_DEBUG 201static int pc_debug = PCMCIA_DEBUG; 202module_param(pc_debug, int, 0644); 203#define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args) 204static char *version = "ni_daq_dio24.c, based on dummy_cs.c"; 205#else 206#define DEBUG(n, args...) 207#endif 208 209/*====================================================================*/ 210 211static void dio24_config(struct pcmcia_device *link); 212static void dio24_release(struct pcmcia_device *link); 213static int dio24_cs_suspend(struct pcmcia_device *p_dev); 214static int dio24_cs_resume(struct pcmcia_device *p_dev); 215 216/* 217 The attach() and detach() entry points are used to create and destroy 218 "instances" of the driver, where each instance represents everything 219 needed to manage one actual PCMCIA card. 220*/ 221 222static int dio24_cs_attach(struct pcmcia_device *); 223static void dio24_cs_detach(struct pcmcia_device *); 224 225/* 226 You'll also need to prototype all the functions that will actually 227 be used to talk to your device. See 'memory_cs' for a good example 228 of a fully self-sufficient driver; the other drivers rely more or 229 less on other parts of the kernel. 230*/ 231 232/* 233 The dev_info variable is the "key" that is used to match up this 234 device driver with appropriate cards, through the card configuration 235 database. 236*/ 237 238static const dev_info_t dev_info = "ni_daq_dio24"; 239 240struct local_info_t { 241 struct pcmcia_device *link; 242 dev_node_t node; 243 int stop; 244 struct bus_operations *bus; 245}; 246 247/*====================================================================== 248 249 dio24_cs_attach() creates an "instance" of the driver, allocating 250 local data structures for one device. The device is registered 251 with Card Services. 252 253 The dev_link structure is initialized, but we don't actually 254 configure the card at this point -- we wait until we receive a 255 card insertion event. 256 257======================================================================*/ 258 259static int dio24_cs_attach(struct pcmcia_device *link) 260{ 261 struct local_info_t *local; 262 263 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n"); 264 265 DEBUG(0, "dio24_cs_attach()\n"); 266 267 /* Allocate space for private device-specific data */ 268 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL); 269 if (!local) 270 return -ENOMEM; 271 local->link = link; 272 link->priv = local; 273 274 /* Interrupt setup */ 275 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE; 276 link->irq.IRQInfo1 = IRQ_LEVEL_ID; 277 link->irq.Handler = NULL; 278 279 /* 280 General socket configuration defaults can go here. In this 281 client, we assume very little, and rely on the CIS for almost 282 everything. In most clients, many details (i.e., number, sizes, 283 and attributes of IO windows) are fixed by the nature of the 284 device, and can be hard-wired here. 285 */ 286 link->conf.Attributes = 0; 287 link->conf.IntType = INT_MEMORY_AND_IO; 288 289 pcmcia_cur_dev = link; 290 291 dio24_config(link); 292 293 return 0; 294} /* dio24_cs_attach */ 295 296/*====================================================================== 297 298 This deletes a driver "instance". The device is de-registered 299 with Card Services. If it has been released, all local data 300 structures are freed. Otherwise, the structures will be freed 301 when the device is released. 302 303======================================================================*/ 304 305static void dio24_cs_detach(struct pcmcia_device *link) 306{ 307 308 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - cs-detach!\n"); 309 310 DEBUG(0, "dio24_cs_detach(0x%p)\n", link); 311 312 if (link->dev_node) { 313 ((struct local_info_t *) link->priv)->stop = 1; 314 dio24_release(link); 315 } 316 317 /* This points to the parent local_info_t struct */ 318 if (link->priv) 319 kfree(link->priv); 320 321} /* dio24_cs_detach */ 322 323/*====================================================================== 324 325 dio24_config() is scheduled to run after a CARD_INSERTION event 326 is received, to configure the PCMCIA socket, and to make the 327 device available to the system. 328 329======================================================================*/ 330 331static void dio24_config(struct pcmcia_device *link) 332{ 333 struct local_info_t *dev = link->priv; 334 tuple_t tuple; 335 cisparse_t parse; 336 int last_ret; 337 u_char buf[64]; 338 win_req_t req; 339 memreq_t map; 340 cistpl_cftable_entry_t dflt = { 0 }; 341 342 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n"); 343 344 DEBUG(0, "dio24_config(0x%p)\n", link); 345 346 /* 347 This reads the card's CONFIG tuple to find its configuration 348 registers. 349 */ 350 tuple.DesiredTuple = CISTPL_CONFIG; 351 tuple.Attributes = 0; 352 tuple.TupleData = buf; 353 tuple.TupleDataMax = sizeof(buf); 354 tuple.TupleOffset = 0; 355 356 last_ret = pcmcia_get_first_tuple(link, &tuple); 357 if (last_ret) { 358 cs_error(link, GetFirstTuple, last_ret); 359 goto cs_failed; 360 } 361 362 last_ret = pcmcia_get_tuple_data(link, &tuple); 363 if (last_ret) { 364 cs_error(link, GetTupleData, last_ret); 365 goto cs_failed; 366 } 367 368 last_ret = pcmcia_parse_tuple(&tuple, &parse); 369 if (last_ret) { 370 cs_error(link, ParseTuple, last_ret); 371 goto cs_failed; 372 } 373 link->conf.ConfigBase = parse.config.base; 374 link->conf.Present = parse.config.rmask[0]; 375 376 /* 377 In this loop, we scan the CIS for configuration table entries, 378 each of which describes a valid card configuration, including 379 voltage, IO window, memory window, and interrupt settings. 380 381 We make no assumptions about the card to be configured: we use 382 just the information available in the CIS. In an ideal world, 383 this would work for any PCMCIA card, but it requires a complete 384 and accurate CIS. In practice, a driver usually "knows" most of 385 these things without consulting the CIS, and most client drivers 386 will only use the CIS to fill in implementation-defined details. 387 */ 388 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; 389 390 last_ret = pcmcia_get_first_tuple(link, &tuple); 391 if (last_ret) { 392 cs_error(link, GetFirstTuple, last_ret); 393 goto cs_failed; 394 } 395 while (1) { 396 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry); 397 if (pcmcia_get_tuple_data(link, &tuple) != 0) 398 goto next_entry; 399 if (pcmcia_parse_tuple(&tuple, &parse) != 0) 400 goto next_entry; 401 402 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) 403 dflt = *cfg; 404 if (cfg->index == 0) 405 goto next_entry; 406 link->conf.ConfigIndex = cfg->index; 407 408 /* Does this card need audio output? */ 409 if (cfg->flags & CISTPL_CFTABLE_AUDIO) { 410 link->conf.Attributes |= CONF_ENABLE_SPKR; 411 link->conf.Status = CCSR_AUDIO_ENA; 412 } 413 414 /* Do we need to allocate an interrupt? */ 415 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1) 416 link->conf.Attributes |= CONF_ENABLE_IRQ; 417 418 /* IO window settings */ 419 link->io.NumPorts1 = link->io.NumPorts2 = 0; 420 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) { 421 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io; 422 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO; 423 if (!(io->flags & CISTPL_IO_8BIT)) 424 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16; 425 if (!(io->flags & CISTPL_IO_16BIT)) 426 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8; 427 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK; 428 link->io.BasePort1 = io->win[0].base; 429 link->io.NumPorts1 = io->win[0].len; 430 if (io->nwin > 1) { 431 link->io.Attributes2 = link->io.Attributes1; 432 link->io.BasePort2 = io->win[1].base; 433 link->io.NumPorts2 = io->win[1].len; 434 } 435 /* This reserves IO space but doesn't actually enable it */ 436 if (pcmcia_request_io(link, &link->io) != 0) 437 goto next_entry; 438 } 439 440 if ((cfg->mem.nwin > 0) || (dflt.mem.nwin > 0)) { 441 cistpl_mem_t *mem = 442 (cfg->mem.nwin) ? &cfg->mem : &dflt.mem; 443 req.Attributes = WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM; 444 req.Attributes |= WIN_ENABLE; 445 req.Base = mem->win[0].host_addr; 446 req.Size = mem->win[0].len; 447 if (req.Size < 0x1000) 448 req.Size = 0x1000; 449 req.AccessSpeed = 0; 450 if (pcmcia_request_window(&link, &req, &link->win)) 451 goto next_entry; 452 map.Page = 0; 453 map.CardOffset = mem->win[0].card_addr; 454 if (pcmcia_map_mem_page(link->win, &map)) 455 goto next_entry; 456 } 457 /* If we got this far, we're cool! */ 458 break; 459 460 next_entry: 461 462 last_ret = pcmcia_get_next_tuple(link, &tuple); 463 if (last_ret) { 464 cs_error(link, GetNextTuple, last_ret); 465 goto cs_failed; 466 } 467 } 468 469 /* 470 Allocate an interrupt line. Note that this does not assign a 471 handler to the interrupt, unless the 'Handler' member of the 472 irq structure is initialized. 473 */ 474 if (link->conf.Attributes & CONF_ENABLE_IRQ) { 475 last_ret = pcmcia_request_irq(link, &link->irq); 476 if (last_ret) { 477 cs_error(link, RequestIRQ, last_ret); 478 goto cs_failed; 479 } 480 } 481 482 /* 483 This actually configures the PCMCIA socket -- setting up 484 the I/O windows and the interrupt mapping, and putting the 485 card and host interface into "Memory and IO" mode. 486 */ 487 last_ret = pcmcia_request_configuration(link, &link->conf); 488 if (last_ret) { 489 cs_error(link, RequestConfiguration, last_ret); 490 goto cs_failed; 491 } 492 493 /* 494 At this point, the dev_node_t structure(s) need to be 495 initialized and arranged in a linked list at link->dev. 496 */ 497 sprintf(dev->node.dev_name, "ni_daq_dio24"); 498 dev->node.major = dev->node.minor = 0; 499 link->dev_node = &dev->node; 500 501 /* Finally, report what we've done */ 502 printk(KERN_INFO "%s: index 0x%02x", 503 dev->node.dev_name, link->conf.ConfigIndex); 504 if (link->conf.Attributes & CONF_ENABLE_IRQ) 505 printk(", irq %d", link->irq.AssignedIRQ); 506 if (link->io.NumPorts1) 507 printk(", io 0x%04x-0x%04x", link->io.BasePort1, 508 link->io.BasePort1 + link->io.NumPorts1 - 1); 509 if (link->io.NumPorts2) 510 printk(" & 0x%04x-0x%04x", link->io.BasePort2, 511 link->io.BasePort2 + link->io.NumPorts2 - 1); 512 if (link->win) 513 printk(", mem 0x%06lx-0x%06lx", req.Base, 514 req.Base + req.Size - 1); 515 printk("\n"); 516 517 return; 518 519 cs_failed: 520 printk(KERN_INFO "Fallo"); 521 dio24_release(link); 522 523} /* dio24_config */ 524 525static void dio24_release(struct pcmcia_device *link) 526{ 527 DEBUG(0, "dio24_release(0x%p)\n", link); 528 529 pcmcia_disable_device(link); 530} /* dio24_release */ 531 532/*====================================================================== 533 534 The card status event handler. Mostly, this schedules other 535 stuff to run after an event is received. 536 537 When a CARD_REMOVAL event is received, we immediately set a 538 private flag to block future accesses to this device. All the 539 functions that actually access the device should check this flag 540 to make sure the card is still present. 541 542======================================================================*/ 543 544static int dio24_cs_suspend(struct pcmcia_device *link) 545{ 546 struct local_info_t *local = link->priv; 547 548 /* Mark the device as stopped, to block IO until later */ 549 local->stop = 1; 550 return 0; 551} /* dio24_cs_suspend */ 552 553static int dio24_cs_resume(struct pcmcia_device *link) 554{ 555 struct local_info_t *local = link->priv; 556 557 local->stop = 0; 558 return 0; 559} /* dio24_cs_resume */ 560 561/*====================================================================*/ 562 563static struct pcmcia_device_id dio24_cs_ids[] = { 564 /* N.B. These IDs should match those in dio24_boards */ 565 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */ 566 PCMCIA_DEVICE_NULL 567}; 568 569MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids); 570 571struct pcmcia_driver dio24_cs_driver = { 572 .probe = dio24_cs_attach, 573 .remove = dio24_cs_detach, 574 .suspend = dio24_cs_suspend, 575 .resume = dio24_cs_resume, 576 .id_table = dio24_cs_ids, 577 .owner = THIS_MODULE, 578 .drv = { 579 .name = dev_info, 580 }, 581}; 582 583static int __init init_dio24_cs(void) 584{ 585 printk("ni_daq_dio24: HOLA SOY YO!\n"); 586 DEBUG(0, "%s\n", version); 587 pcmcia_register_driver(&dio24_cs_driver); 588 return 0; 589} 590 591static void __exit exit_dio24_cs(void) 592{ 593 DEBUG(0, "ni_dio24: unloading\n"); 594 pcmcia_unregister_driver(&dio24_cs_driver); 595} 596 597int __init init_module(void) 598{ 599 int ret; 600 601 ret = init_dio24_cs(); 602 if (ret < 0) 603 return ret; 604 605 return comedi_driver_register(&driver_dio24); 606} 607 608void __exit cleanup_module(void) 609{ 610 exit_dio24_cs(); 611 comedi_driver_unregister(&driver_dio24); 612} 613