Got rid of unnecessary casts when initializing comedi_driver.board_name
[comedi.git] / comedi / drivers / das08_cs.c
1 /*
2     comedi/drivers/das08_cs.c
3     DAS08 driver
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7     Copyright (C) 2001,2002,2003 Frank Mori Hess <fmhess@users.sourceforge.net>
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 *****************************************************************
24
25 */
26 /*
27 Driver: das08_cs.o
28 Description: DAS-08 PCMCIA boards
29 Author: Warren Jasper, ds, Frank Hess
30 Devices: [ComputerBoards] PCM-DAS08 (pcm-das08)
31 Status: works
32
33 This is the PCMCIA-specific support split off from the
34 das08 driver.
35
36 Options (for pcm-das08):
37         NONE
38
39 Command support does not exist, but could be added for this board.
40 */
41
42 #include <linux/comedidev.h>
43
44 #include <linux/delay.h>
45 #include <linux/pci.h>
46 #include <linux/version.h>
47
48 #include "das08.h"
49
50 // pcmcia includes
51 #include <pcmcia/cs_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/ds.h>
55 /*
56    A linked list of "instances" of the das08_pcmcia device.  Each actual
57    PCMCIA card corresponds to one device instance, and is described
58    by one dev_link_t structure (defined in ds.h).
59
60    You may not want to use a linked list for this -- for example, the
61    memory card driver uses an array of dev_link_t pointers, where minor
62    device numbers are used to derive the corresponding array index.
63 */
64 static dev_link_t *dev_list = NULL;
65
66 #define thisboard ((struct das08_board_struct *)dev->board_ptr)
67
68 static int das08_cs_attach(comedi_device *dev,comedi_devconfig *it);
69
70 static comedi_driver driver_das08_cs =
71 {
72         driver_name: "das08_cs",
73         module: THIS_MODULE,
74         attach: das08_cs_attach,
75         detach: das08_common_detach,
76         board_name: &das08_cs_boards[0].name,
77         num_names: sizeof(das08_cs_boards) / sizeof(struct das08_board_struct),
78         offset: sizeof(struct das08_board_struct),
79 };
80
81 static int das08_cs_attach(comedi_device *dev,comedi_devconfig *it)
82 {
83         int ret;
84         unsigned long iobase;
85         dev_link_t *link = dev_list;    // XXX hack
86
87         if((ret=alloc_private(dev,sizeof(struct das08_private_struct)))<0)
88                 return ret;
89
90         printk("comedi%d: das08_cs: ", dev->minor);
91         // deal with a pci board
92
93         if(thisboard->bustype == pcmcia)
94         {
95                 if(link == NULL)
96                 {
97                         printk(" no pcmcia cards found\n");
98                         return -EIO;
99                 }
100                 iobase = link->io.BasePort1;
101         }else{
102                 printk(" bug! board does not have PCMCIA bustype\n");
103                 return -EINVAL;
104         }
105
106         printk("\n");
107
108         return das08_common_attach( dev, iobase );
109 }
110
111 /*======================================================================
112
113     The following pcmcia code for the pcm-das08 is adapted from the
114     dummy_cs.c driver of the Linux PCMCIA Card Services package.
115
116     The initial developer of the original code is David A. Hinds
117     <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
118     are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
119
120 ======================================================================*/
121
122 /*
123    All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If
124    you do not define PCMCIA_DEBUG at all, all the debug code will be
125    left out.  If you compile with PCMCIA_DEBUG=0, the debug code will
126    be present but disabled -- but it can then be enabled for specific
127    modules at load time with a 'pc_debug=#' option to insmod.
128 */
129
130 #ifdef PCMCIA_DEBUG
131 static int pc_debug = PCMCIA_DEBUG;
132 module_param(pc_debug, int, 0644);
133 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
134 static char *version =
135 "das08.c pcmcia code (Frank Hess), modified from dummy_cs.c 1.31 2001/08/24 12:13:13 (David Hinds)";
136 #else
137 #define DEBUG(n, args...)
138 #endif
139
140 /*====================================================================*/
141 static void das08_pcmcia_config(dev_link_t *link);
142 static void das08_pcmcia_release(u_long arg);
143 static int das08_pcmcia_suspend(struct pcmcia_device *p_dev);
144 static int das08_pcmcia_resume(struct pcmcia_device *p_dev);
145
146 /*
147    The attach() and detach() entry points are used to create and destroy
148    "instances" of the driver, where each instance represents everything
149    needed to manage one actual PCMCIA card.
150 */
151
152 static int das08_pcmcia_attach(struct pcmcia_device *);
153 static void das08_pcmcia_detach(struct pcmcia_device *);
154
155 /*
156    You'll also need to prototype all the functions that will actually
157    be used to talk to your device.  See 'memory_cs' for a good example
158    of a fully self-sufficient driver; the other drivers rely more or
159    less on other parts of the kernel.
160 */
161
162 /*
163    The dev_info variable is the "key" that is used to match up this
164    device driver with appropriate cards, through the card configuration
165    database.
166 */
167
168 static dev_info_t dev_info = "pcm-das08";
169
170 /*
171    A dev_link_t structure has fields for most things that are needed
172    to keep track of a socket, but there will usually be some device
173    specific information that also needs to be kept track of.  The
174    'priv' pointer in a dev_link_t structure can be used to point to
175    a device-specific private data structure, like this.
176
177    To simplify the data structure handling, we actually include the
178    dev_link_t structure in the device's private data structure.
179
180    A driver needs to provide a dev_node_t structure for each device
181    on a card.  In some cases, there is only one device per card (for
182    example, ethernet cards, modems).  In other cases, there may be
183    many actual or logical devices (SCSI adapters, memory cards with
184    multiple partitions).  The dev_node_t structures need to be kept
185    in a linked list starting at the 'dev' field of a dev_link_t
186    structure.  We allocate them in the card's private data structure,
187    because they generally shouldn't be allocated dynamically.
188
189    In this case, we also provide a flag to indicate if a device is
190    "stopped" due to a power management event, or card ejection.  The
191    device IO routines can use a flag like this to throttle IO to a
192    card that is not ready to accept it.
193
194    The bus_operations pointer is used on platforms for which we need
195    to use special socket-specific versions of normal IO primitives
196    (inb, outb, readb, writeb, etc) for card IO.
197 */
198
199 typedef struct local_info_t {
200     dev_link_t          link;
201     dev_node_t          node;
202     int                 stop;
203     struct bus_operations *bus;
204 } local_info_t;
205
206 /*======================================================================
207
208     das08_pcmcia_attach() creates an "instance" of the driver, allocating
209     local data structures for one device.  The device is registered
210     with Card Services.
211
212     The dev_link structure is initialized, but we don't actually
213     configure the card at this point -- we wait until we receive a
214     card insertion event.
215
216 ======================================================================*/
217
218 static int das08_pcmcia_attach(struct pcmcia_device *p_dev)
219 {
220     local_info_t *local;
221     dev_link_t *link;
222
223     DEBUG(0, "das08_pcmcia_attach()\n");
224
225     /* Allocate space for private device-specific data */
226     local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
227     if (!local) return -ENOMEM;
228     memset(local, 0, sizeof(local_info_t));
229     link = &local->link; link->priv = local;
230
231     /* Interrupt setup */
232     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
233     link->irq.IRQInfo1 = IRQ_LEVEL_ID;
234     link->irq.Handler = NULL;
235
236     /*
237       General socket configuration defaults can go here.  In this
238       client, we assume very little, and rely on the CIS for almost
239       everything.  In most clients, many details (i.e., number, sizes,
240       and attributes of IO windows) are fixed by the nature of the
241       device, and can be hard-wired here.
242     */
243     link->conf.Attributes = 0;
244     link->conf.Vcc = 50;
245     link->conf.IntType = INT_MEMORY_AND_IO;
246
247     link->next = dev_list;
248     dev_list = link;
249
250     link->handle = p_dev;
251     p_dev->instance = link;
252     link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
253     das08_pcmcia_config(link);
254
255     return 0;
256 } /* das08_pcmcia_attach */
257
258 /*======================================================================
259
260     This deletes a driver "instance".  The device is de-registered
261     with Card Services.  If it has been released, all local data
262     structures are freed.  Otherwise, the structures will be freed
263     when the device is released.
264
265 ======================================================================*/
266
267 static void das08_pcmcia_detach(struct pcmcia_device *p_dev)
268 {
269         dev_link_t *link = dev_to_instance(p_dev);
270         dev_link_t **linkp;
271
272         DEBUG(0, "das08_pcmcia_detach(0x%p)\n", link);
273
274         /* Locate device structure */
275         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
276         if (*linkp == link) break;
277         if (*linkp == NULL)
278                 return;
279
280         /*
281         If the device is currently configured and active, we won't
282         actually delete it yet.  Instead, it is marked so that when
283         the release() function is called, that will trigger a proper
284         detach().
285         */
286         if (link->state & DEV_CONFIG)
287         {
288                 ((local_info_t *)link->priv)->stop = 1;
289                 das08_pcmcia_release((u_long)link);
290         }
291
292         /* Unlink device structure, and free it */
293         *linkp = link->next;
294         /* This points to the parent local_info_t struct */
295         kfree(link->priv);
296
297 } /* das08_pcmcia_detach */
298
299 /*======================================================================
300
301     das08_pcmcia_config() is scheduled to run after a CARD_INSERTION event
302     is received, to configure the PCMCIA socket, and to make the
303     device available to the system.
304
305 ======================================================================*/
306
307 static void das08_pcmcia_config(dev_link_t *link)
308 {
309         client_handle_t handle = link->handle;
310         local_info_t *dev = link->priv;
311         tuple_t tuple;
312         cisparse_t parse;
313         int last_fn, last_ret;
314         u_char buf[64];
315         config_info_t conf;
316         cistpl_cftable_entry_t dflt = { 0 };
317
318         DEBUG(0, "das08_pcmcia_config(0x%p)\n", link);
319
320         /*
321                 This reads the card's CONFIG tuple to find its configuration
322                 registers.
323         */
324         tuple.DesiredTuple = CISTPL_CONFIG;
325         tuple.Attributes = 0;
326         tuple.TupleData = buf;
327         tuple.TupleDataMax = sizeof(buf);
328         tuple.TupleOffset = 0;
329         last_fn = GetFirstTuple;
330         if((last_ret = pcmcia_get_first_tuple(handle, &tuple)) != 0) goto cs_failed;
331         last_fn = GetTupleData;
332         if((last_ret = pcmcia_get_tuple_data(handle, &tuple)) != 0) goto cs_failed;
333         last_fn = ParseTuple;
334         if((last_ret = pcmcia_parse_tuple(handle, &tuple, &parse)) != 0) goto cs_failed;
335         link->conf.ConfigBase = parse.config.base;
336         link->conf.Present = parse.config.rmask[0];
337
338         /* Configure card */
339         link->state |= DEV_CONFIG;
340
341         /* Look up the current Vcc */
342         last_fn = GetConfigurationInfo;
343         if((last_ret = pcmcia_get_configuration_info(handle, &conf)) != 0) goto cs_failed;
344         link->conf.Vcc = conf.Vcc;
345
346         /*
347         In this loop, we scan the CIS for configuration table entries,
348         each of which describes a valid card configuration, including
349         voltage, IO window, memory window, and interrupt settings.
350
351         We make no assumptions about the card to be configured: we use
352         just the information available in the CIS.  In an ideal world,
353         this would work for any PCMCIA card, but it requires a complete
354         and accurate CIS.  In practice, a driver usually "knows" most of
355         these things without consulting the CIS, and most client drivers
356         will only use the CIS to fill in implementation-defined details.
357         */
358         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
359         last_fn = GetFirstTuple;
360         if((last_ret = pcmcia_get_first_tuple(handle, &tuple)) != 0) goto cs_failed;
361         while (1) {
362         cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
363         if((last_ret = pcmcia_get_tuple_data(handle, &tuple)) != 0) goto next_entry;
364         if((last_ret = pcmcia_parse_tuple(handle, &tuple, &parse)) != 0) goto next_entry;
365
366         if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
367         if (cfg->index == 0) goto next_entry;
368         link->conf.ConfigIndex = cfg->index;
369
370         /* Does this card need audio output? */
371 /*      if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
372                 link->conf.Attributes |= CONF_ENABLE_SPKR;
373                 link->conf.Status = CCSR_AUDIO_ENA;
374         }
375 */
376         /* Use power settings for Vcc and Vpp if present */
377         /*  Note that the CIS values need to be rescaled */
378         if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
379                 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM]/10000)
380                 goto next_entry;
381         } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
382                 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM]/10000)
383                 goto next_entry;
384         }
385
386         if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
387                 link->conf.Vpp1 = link->conf.Vpp2 =
388                 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
389         else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
390                 link->conf.Vpp1 = link->conf.Vpp2 =
391                 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
392
393         /* Do we need to allocate an interrupt? */
394         if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
395                 link->conf.Attributes |= CONF_ENABLE_IRQ;
396
397         /* IO window settings */
398         link->io.NumPorts1 = link->io.NumPorts2 = 0;
399         if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
400                 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
401                 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
402                 if (!(io->flags & CISTPL_IO_8BIT))
403                 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
404                 if (!(io->flags & CISTPL_IO_16BIT))
405                 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
406                 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
407                 link->io.BasePort1 = io->win[0].base;
408                 link->io.NumPorts1 = io->win[0].len;
409                 if (io->nwin > 1) {
410                 link->io.Attributes2 = link->io.Attributes1;
411                 link->io.BasePort2 = io->win[1].base;
412                 link->io.NumPorts2 = io->win[1].len;
413                 }
414                 /* This reserves IO space but doesn't actually enable it */
415                 if(pcmcia_request_io(link->handle, &link->io) != 0) goto next_entry;
416         }
417
418         /* If we got this far, we're cool! */
419         break;
420
421         next_entry:
422         if (link->io.NumPorts1)
423                 pcmcia_release_io(link->handle, &link->io);
424         last_fn = GetNextTuple;
425         if((last_ret = pcmcia_get_next_tuple(handle, &tuple)) != 0) goto cs_failed;
426         }
427
428         /*
429                 Allocate an interrupt line.  Note that this does not assign a
430                 handler to the interrupt, unless the 'Handler' member of the
431                 irq structure is initialized.
432         */
433         if (link->conf.Attributes & CONF_ENABLE_IRQ)
434         {
435                 last_fn = RequestIRQ;
436                 if((last_ret = pcmcia_request_irq(handle, &link->irq)) != 0) goto cs_failed;
437         }
438
439         /*
440                 This actually configures the PCMCIA socket -- setting up
441                 the I/O windows and the interrupt mapping, and putting the
442                 card and host interface into "Memory and IO" mode.
443         */
444         last_fn = RequestConfiguration;
445         if((last_ret = pcmcia_request_configuration(link->handle, &link->conf)) != 0) goto cs_failed;
446
447         /*
448                 At this point, the dev_node_t structure(s) need to be
449                 initialized and arranged in a linked list at link->dev.
450         */
451         sprintf(dev->node.dev_name, "pcm-das08");
452         dev->node.major = dev->node.minor = 0;
453         link->dev = &dev->node;
454
455         /* Finally, report what we've done */
456         printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
457                 dev->node.dev_name, link->conf.ConfigIndex,
458                 link->conf.Vcc/10, link->conf.Vcc%10);
459         if (link->conf.Vpp1)
460         printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
461         if (link->conf.Attributes & CONF_ENABLE_IRQ)
462         printk(", irq %u", link->irq.AssignedIRQ);
463         if (link->io.NumPorts1)
464         printk(", io 0x%04x-0x%04x", link->io.BasePort1,
465                         link->io.BasePort1+link->io.NumPorts1-1);
466         if (link->io.NumPorts2)
467         printk(" & 0x%04x-0x%04x", link->io.BasePort2,
468                         link->io.BasePort2+link->io.NumPorts2-1);
469         printk("\n");
470
471         link->state &= ~DEV_CONFIG_PENDING;
472         return;
473
474 cs_failed:
475         cs_error(link->handle, last_fn, last_ret);
476         das08_pcmcia_release((u_long)link);
477
478 } /* das08_pcmcia_config */
479
480 /*======================================================================
481
482     After a card is removed, das08_pcmcia_release() will unregister the
483     device, and release the PCMCIA configuration.  If the device is
484     still open, this will be postponed until it is closed.
485
486 ======================================================================*/
487
488 static void das08_pcmcia_release(u_long arg)
489 {
490         dev_link_t *link = (dev_link_t *)arg;
491
492         DEBUG(0, "das08_pcmcia_release(0x%p)\n", link);
493
494         /* Unlink the device chain */
495         link->dev = NULL;
496
497     /*
498       In a normal driver, additional code may be needed to release
499       other kernel data structures associated with this device.
500     */
501
502     /* Don't bother checking to see if these succeed or not */
503         pcmcia_release_configuration(link->handle);
504         if (link->io.NumPorts1)
505                 pcmcia_release_io(link->handle, &link->io);
506         if (link->irq.AssignedIRQ)
507                 pcmcia_release_irq(link->handle, &link->irq);
508         link->state &= ~DEV_CONFIG;
509
510 } /* das08_pcmcia_release */
511
512 /*======================================================================
513
514     The card status event handler.  Mostly, this schedules other
515     stuff to run after an event is received.
516
517     When a CARD_REMOVAL event is received, we immediately set a
518     private flag to block future accesses to this device.  All the
519     functions that actually access the device should check this flag
520     to make sure the card is still present.
521
522 ======================================================================*/
523
524 static int das08_pcmcia_suspend(struct pcmcia_device *p_dev)
525 {
526         dev_link_t *link = dev_to_instance(p_dev);
527         local_info_t *local = link->priv;
528
529         link->state |= DEV_SUSPEND;
530         /* Mark the device as stopped, to block IO until later */
531         local->stop = 1;
532         if (link->state & DEV_CONFIG)
533                 pcmcia_release_configuration(link->handle);
534
535         return 0;
536 } /* das08_pcmcia_suspend */
537
538 static int das08_pcmcia_resume(struct pcmcia_device *p_dev)
539 {
540         dev_link_t *link = dev_to_instance(p_dev);
541         local_info_t *local = link->priv;
542
543         link->state &= ~DEV_SUSPEND;
544         if (link->state & DEV_CONFIG)
545                 pcmcia_request_configuration(link->handle, &link->conf);
546         local->stop = 0;
547         return 0;
548 } /* das08_pcmcia_resume */
549
550 /*====================================================================*/
551
552 static struct pcmcia_device_id das08_cs_id_table[] =
553 {
554         PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x4001),
555         PCMCIA_DEVICE_NULL
556 };
557 MODULE_DEVICE_TABLE(pcmcia, das08_cs_id_table);
558
559 struct pcmcia_driver das08_cs_driver =
560 {
561         .probe = das08_pcmcia_attach,
562         .remove = das08_pcmcia_detach,
563         .suspend = das08_pcmcia_suspend,
564         .resume = das08_pcmcia_resume,
565         .id_table = das08_cs_id_table,
566         .owner = THIS_MODULE,
567         .drv = {
568                 .name = dev_info,
569         },      
570 };
571
572 static int __init init_das08_pcmcia_cs(void)
573 {
574         DEBUG(0, "%s\n", version);
575         pcmcia_register_driver(&das08_cs_driver);
576         return 0;
577 }
578
579 static void __exit exit_das08_pcmcia_cs(void)
580 {
581         DEBUG(0, "das08_pcmcia_cs: unloading\n");
582         pcmcia_unregister_driver(&das08_cs_driver);
583         while (dev_list != NULL)
584         {
585                 if (dev_list->state & DEV_CONFIG)
586                         das08_pcmcia_release((u_long)dev_list);
587                 das08_pcmcia_detach(dev_list->handle);
588         }
589 }
590
591 static int __init das08_cs_init_module(void)
592 {
593         int ret;
594
595         ret = init_das08_pcmcia_cs();
596         if(ret < 0)
597                 return ret;
598
599         return comedi_driver_register(&driver_das08_cs);
600 }
601
602 static void __exit das08_cs_exit_module(void)
603 {
604         exit_das08_pcmcia_cs();
605         comedi_driver_unregister(&driver_das08_cs);
606 }
607
608 MODULE_LICENSE("GPL");
609 module_init( das08_cs_init_module );
610 module_exit( das08_cs_exit_module );
611