Merge branch 'drm-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / comedi / drivers / ni_daq_dio24.c
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 /*
29 Driver: ni_daq_dio24
30 Description: National Instruments PCMCIA DAQ-Card DIO-24
31 Author: Daniel Vecino Castel <dvecino@able.es>
32 Devices: [National Instruments] PCMCIA DAQ-Card DIO-24 (ni_daq_dio24)
33 Status: ?
34 Updated: Thu, 07 Nov 2002 21:53:06 -0800
35
36 This is just a wrapper around the 8255.o driver to properly handle
37 the 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
56 static struct pcmcia_device *pcmcia_cur_dev = NULL;
57
58 #define DIO24_SIZE 4 /* size of io region used by board */
59
60 static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it);
61 static int dio24_detach(struct comedi_device *dev);
62
63 enum dio24_bustype { pcmcia_bustype };
64
65 struct 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
75 static 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
95 struct dio24_private {
96
97 int data; /* number of data points left to be taken */
98 };
99
100 #define devpriv ((struct dio24_private *)dev->private)
101
102 static struct comedi_driver driver_dio24 = {
103 .driver_name = "ni_daq_dio24",
104 .module = THIS_MODULE,
105 .attach = dio24_attach,
106 .detach = dio24_detach,
107 .num_names = ARRAY_SIZE(dio24_boards),
108 .board_name = &dio24_boards[0].name,
109 .offset = sizeof(struct dio24_board_struct),
110 };
111
112 static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it)
113 {
114 struct comedi_subdevice *s;
115 unsigned long iobase = 0;
116 #ifdef incomplete
117 unsigned int irq = 0;
118 #endif
119 struct pcmcia_device *link;
120
121 /* allocate and initialize dev->private */
122 if (alloc_private(dev, sizeof(struct dio24_private)) < 0)
123 return -ENOMEM;
124
125 /* get base address, irq etc. based on bustype */
126 switch (thisboard->bustype) {
127 case pcmcia_bustype:
128 link = pcmcia_cur_dev; /* XXX hack */
129 if (!link)
130 return -EIO;
131 iobase = link->io.BasePort1;
132 #ifdef incomplete
133 irq = link->irq.AssignedIRQ;
134 #endif
135 break;
136 default:
137 printk("bug! couldn't determine board type\n");
138 return -EINVAL;
139 break;
140 }
141 printk("comedi%d: ni_daq_dio24: %s, io 0x%lx", dev->minor,
142 thisboard->name, iobase);
143 #ifdef incomplete
144 if (irq) {
145 printk(", irq %u", irq);
146 }
147 #endif
148
149 printk("\n");
150
151 if (iobase == 0) {
152 printk("io base address is zero!\n");
153 return -EINVAL;
154 }
155
156 dev->iobase = iobase;
157
158 #ifdef incomplete
159 /* grab our IRQ */
160 dev->irq = irq;
161 #endif
162
163 dev->board_name = thisboard->name;
164
165 if (alloc_subdevices(dev, 1) < 0)
166 return -ENOMEM;
167
168 /* 8255 dio */
169 s = dev->subdevices + 0;
170 subdev_8255_init(dev, s, NULL, dev->iobase);
171
172 return 0;
173 };
174
175 static int dio24_detach(struct comedi_device *dev)
176 {
177 printk("comedi%d: ni_daq_dio24: remove\n", dev->minor);
178
179 if (dev->subdevices)
180 subdev_8255_cleanup(dev, dev->subdevices + 0);
181
182 if (thisboard->bustype != pcmcia_bustype && dev->iobase)
183 release_region(dev->iobase, DIO24_SIZE);
184 if (dev->irq)
185 free_irq(dev->irq, dev);
186
187 return 0;
188 };
189
190 /* PCMCIA crap -- watch your words! */
191
192 static void dio24_config(struct pcmcia_device *link);
193 static void dio24_release(struct pcmcia_device *link);
194 static int dio24_cs_suspend(struct pcmcia_device *p_dev);
195 static int dio24_cs_resume(struct pcmcia_device *p_dev);
196
197 /*
198 The attach() and detach() entry points are used to create and destroy
199 "instances" of the driver, where each instance represents everything
200 needed to manage one actual PCMCIA card.
201 */
202
203 static int dio24_cs_attach(struct pcmcia_device *);
204 static void dio24_cs_detach(struct pcmcia_device *);
205
206 /*
207 You'll also need to prototype all the functions that will actually
208 be used to talk to your device. See 'memory_cs' for a good example
209 of a fully self-sufficient driver; the other drivers rely more or
210 less on other parts of the kernel.
211 */
212
213 /*
214 The dev_info variable is the "key" that is used to match up this
215 device driver with appropriate cards, through the card configuration
216 database.
217 */
218
219 static const dev_info_t dev_info = "ni_daq_dio24";
220
221 struct local_info_t {
222 struct pcmcia_device *link;
223 dev_node_t node;
224 int stop;
225 struct bus_operations *bus;
226 };
227
228 /*======================================================================
229
230 dio24_cs_attach() creates an "instance" of the driver, allocating
231 local data structures for one device. The device is registered
232 with Card Services.
233
234 The dev_link structure is initialized, but we don't actually
235 configure the card at this point -- we wait until we receive a
236 card insertion event.
237
238 ======================================================================*/
239
240 static int dio24_cs_attach(struct pcmcia_device *link)
241 {
242 struct local_info_t *local;
243
244 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n");
245
246 dev_dbg(&link->dev, "dio24_cs_attach()\n");
247
248 /* Allocate space for private device-specific data */
249 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
250 if (!local)
251 return -ENOMEM;
252 local->link = link;
253 link->priv = local;
254
255 /* Interrupt setup */
256 link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
257 link->irq.Handler = NULL;
258
259 /*
260 General socket configuration defaults can go here. In this
261 client, we assume very little, and rely on the CIS for almost
262 everything. In most clients, many details (i.e., number, sizes,
263 and attributes of IO windows) are fixed by the nature of the
264 device, and can be hard-wired here.
265 */
266 link->conf.Attributes = 0;
267 link->conf.IntType = INT_MEMORY_AND_IO;
268
269 pcmcia_cur_dev = link;
270
271 dio24_config(link);
272
273 return 0;
274 } /* dio24_cs_attach */
275
276 /*======================================================================
277
278 This deletes a driver "instance". The device is de-registered
279 with Card Services. If it has been released, all local data
280 structures are freed. Otherwise, the structures will be freed
281 when the device is released.
282
283 ======================================================================*/
284
285 static void dio24_cs_detach(struct pcmcia_device *link)
286 {
287
288 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - cs-detach!\n");
289
290 dev_dbg(&link->dev, "dio24_cs_detach\n");
291
292 if (link->dev_node) {
293 ((struct local_info_t *)link->priv)->stop = 1;
294 dio24_release(link);
295 }
296
297 /* This points to the parent local_info_t struct */
298 if (link->priv)
299 kfree(link->priv);
300
301 } /* dio24_cs_detach */
302
303 /*======================================================================
304
305 dio24_config() is scheduled to run after a CARD_INSERTION event
306 is received, to configure the PCMCIA socket, and to make the
307 device available to the system.
308
309 ======================================================================*/
310
311 static int dio24_pcmcia_config_loop(struct pcmcia_device *p_dev,
312 cistpl_cftable_entry_t *cfg,
313 cistpl_cftable_entry_t *dflt,
314 unsigned int vcc,
315 void *priv_data)
316 {
317 win_req_t *req = priv_data;
318 memreq_t map;
319
320 if (cfg->index == 0)
321 return -ENODEV;
322
323 /* Does this card need audio output? */
324 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
325 p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
326 p_dev->conf.Status = CCSR_AUDIO_ENA;
327 }
328
329 /* Do we need to allocate an interrupt? */
330 if (cfg->irq.IRQInfo1 || dflt->irq.IRQInfo1)
331 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
332
333 /* IO window settings */
334 p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
335 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
336 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
337 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
338 if (!(io->flags & CISTPL_IO_8BIT))
339 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
340 if (!(io->flags & CISTPL_IO_16BIT))
341 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
342 p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
343 p_dev->io.BasePort1 = io->win[0].base;
344 p_dev->io.NumPorts1 = io->win[0].len;
345 if (io->nwin > 1) {
346 p_dev->io.Attributes2 = p_dev->io.Attributes1;
347 p_dev->io.BasePort2 = io->win[1].base;
348 p_dev->io.NumPorts2 = io->win[1].len;
349 }
350 /* This reserves IO space but doesn't actually enable it */
351 if (pcmcia_request_io(p_dev, &p_dev->io) != 0)
352 return -ENODEV;
353 }
354
355 if ((cfg->mem.nwin > 0) || (dflt->mem.nwin > 0)) {
356 cistpl_mem_t *mem =
357 (cfg->mem.nwin) ? &cfg->mem : &dflt->mem;
358 req->Attributes = WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
359 req->Attributes |= WIN_ENABLE;
360 req->Base = mem->win[0].host_addr;
361 req->Size = mem->win[0].len;
362 if (req->Size < 0x1000)
363 req->Size = 0x1000;
364 req->AccessSpeed = 0;
365 if (pcmcia_request_window(p_dev, req, &p_dev->win))
366 return -ENODEV;
367 map.Page = 0;
368 map.CardOffset = mem->win[0].card_addr;
369 if (pcmcia_map_mem_page(p_dev, p_dev->win, &map))
370 return -ENODEV;
371 }
372 /* If we got this far, we're cool! */
373 return 0;
374 }
375
376 static void dio24_config(struct pcmcia_device *link)
377 {
378 struct local_info_t *dev = link->priv;
379 int ret;
380 win_req_t req;
381
382 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n");
383
384 dev_dbg(&link->dev, "dio24_config\n");
385
386 ret = pcmcia_loop_config(link, dio24_pcmcia_config_loop, &req);
387 if (ret) {
388 dev_warn(&link->dev, "no configuration found\n");
389 goto failed;
390 }
391
392 /*
393 Allocate an interrupt line. Note that this does not assign a
394 handler to the interrupt, unless the 'Handler' member of the
395 irq structure is initialized.
396 */
397 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
398 ret = pcmcia_request_irq(link, &link->irq);
399 if (ret)
400 goto failed;
401 }
402
403 /*
404 This actually configures the PCMCIA socket -- setting up
405 the I/O windows and the interrupt mapping, and putting the
406 card and host interface into "Memory and IO" mode.
407 */
408 ret = pcmcia_request_configuration(link, &link->conf);
409 if (ret)
410 goto failed;
411
412 /*
413 At this point, the dev_node_t structure(s) need to be
414 initialized and arranged in a linked list at link->dev.
415 */
416 sprintf(dev->node.dev_name, "ni_daq_dio24");
417 dev->node.major = dev->node.minor = 0;
418 link->dev_node = &dev->node;
419
420 /* Finally, report what we've done */
421 printk(KERN_INFO "%s: index 0x%02x",
422 dev->node.dev_name, link->conf.ConfigIndex);
423 if (link->conf.Attributes & CONF_ENABLE_IRQ)
424 printk(", irq %d", link->irq.AssignedIRQ);
425 if (link->io.NumPorts1)
426 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
427 link->io.BasePort1 + link->io.NumPorts1 - 1);
428 if (link->io.NumPorts2)
429 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
430 link->io.BasePort2 + link->io.NumPorts2 - 1);
431 if (link->win)
432 printk(", mem 0x%06lx-0x%06lx", req.Base,
433 req.Base + req.Size - 1);
434 printk("\n");
435
436 return;
437
438 failed:
439 printk(KERN_INFO "Fallo");
440 dio24_release(link);
441
442 } /* dio24_config */
443
444 static void dio24_release(struct pcmcia_device *link)
445 {
446 dev_dbg(&link->dev, "dio24_release\n");
447
448 pcmcia_disable_device(link);
449 } /* dio24_release */
450
451 /*======================================================================
452
453 The card status event handler. Mostly, this schedules other
454 stuff to run after an event is received.
455
456 When a CARD_REMOVAL event is received, we immediately set a
457 private flag to block future accesses to this device. All the
458 functions that actually access the device should check this flag
459 to make sure the card is still present.
460
461 ======================================================================*/
462
463 static int dio24_cs_suspend(struct pcmcia_device *link)
464 {
465 struct local_info_t *local = link->priv;
466
467 /* Mark the device as stopped, to block IO until later */
468 local->stop = 1;
469 return 0;
470 } /* dio24_cs_suspend */
471
472 static int dio24_cs_resume(struct pcmcia_device *link)
473 {
474 struct local_info_t *local = link->priv;
475
476 local->stop = 0;
477 return 0;
478 } /* dio24_cs_resume */
479
480 /*====================================================================*/
481
482 static struct pcmcia_device_id dio24_cs_ids[] = {
483 /* N.B. These IDs should match those in dio24_boards */
484 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */
485 PCMCIA_DEVICE_NULL
486 };
487
488 MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids);
489
490 struct pcmcia_driver dio24_cs_driver = {
491 .probe = dio24_cs_attach,
492 .remove = dio24_cs_detach,
493 .suspend = dio24_cs_suspend,
494 .resume = dio24_cs_resume,
495 .id_table = dio24_cs_ids,
496 .owner = THIS_MODULE,
497 .drv = {
498 .name = dev_info,
499 },
500 };
501
502 static int __init init_dio24_cs(void)
503 {
504 printk("ni_daq_dio24: HOLA SOY YO!\n");
505 pcmcia_register_driver(&dio24_cs_driver);
506 return 0;
507 }
508
509 static void __exit exit_dio24_cs(void)
510 {
511 pcmcia_unregister_driver(&dio24_cs_driver);
512 }
513
514 int __init init_module(void)
515 {
516 int ret;
517
518 ret = init_dio24_cs();
519 if (ret < 0)
520 return ret;
521
522 return comedi_driver_register(&driver_dio24);
523 }
524
525 void __exit cleanup_module(void)
526 {
527 exit_dio24_cs();
528 comedi_driver_unregister(&driver_dio24);
529 }