Commit 73e183c3 authored by H Hartley Sweeten's avatar H Hartley Sweeten Committed by Greg Kroah-Hartman

staging: comedi: das1800: complete the refactor to remove all forward declarations

Complete the refactor of the das1800 driver to remove all the
forward declarations.
Signed-off-by: default avatarH Hartley Sweeten <hsweeten@visionengravers.com>
Cc: Ian Abbott <abbotti@mev.co.uk>
Cc: Mori Hess <fmhess@users.sourceforge.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent c98d90fd
...@@ -183,46 +183,6 @@ enum { ...@@ -183,46 +183,6 @@ enum {
das1802hr, das1802hr_da, das1801hc, das1802hc, das1801ao, das1802ao das1802hr, das1802hr_da, das1801hc, das1802hc, das1801ao, das1802ao
}; };
static int das1800_probe(struct comedi_device *dev);
static int das1800_cancel(struct comedi_device *dev,
struct comedi_subdevice *s);
static irqreturn_t das1800_interrupt(int irq, void *d);
static int das1800_ai_poll(struct comedi_device *dev,
struct comedi_subdevice *s);
static void das1800_ai_handler(struct comedi_device *dev);
static void das1800_handle_dma(struct comedi_device *dev,
struct comedi_subdevice *s, unsigned int status);
static void das1800_flush_dma(struct comedi_device *dev,
struct comedi_subdevice *s);
static void das1800_flush_dma_channel(struct comedi_device *dev,
struct comedi_subdevice *s,
unsigned int channel, uint16_t *buffer);
static void das1800_handle_fifo_half_full(struct comedi_device *dev,
struct comedi_subdevice *s);
static void das1800_handle_fifo_not_empty(struct comedi_device *dev,
struct comedi_subdevice *s);
static int das1800_ai_do_cmdtest(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_cmd *cmd);
static int das1800_ai_do_cmd(struct comedi_device *dev,
struct comedi_subdevice *s);
static int das1800_ai_rinsn(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn, unsigned int *data);
static int das1800_ao_winsn(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn, unsigned int *data);
static int das1800_di_rbits(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn, unsigned int *data);
static int das1800_do_wbits(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn, unsigned int *data);
static int das1800_set_frequency(struct comedi_device *dev);
static unsigned int burst_convert_arg(unsigned int convert_arg, int round_mode);
static unsigned int suggest_transfer_size(struct comedi_cmd *cmd);
/* analog input ranges */ /* analog input ranges */
static const struct comedi_lrange range_ai_das1801 = { static const struct comedi_lrange range_ai_das1801 = {
8, 8,
...@@ -515,421 +475,203 @@ static const struct comedi_lrange range_ao_2 = { ...@@ -515,421 +475,203 @@ static const struct comedi_lrange range_ao_2 = {
}; };
*/ */
static int das1800_init_dma(struct comedi_device *dev, unsigned int dma0, static inline uint16_t munge_bipolar_sample(const struct comedi_device *dev,
unsigned int dma1) uint16_t sample)
{ {
unsigned long flags; sample += 1 << (thisboard->resolution - 1);
return sample;
/* need an irq to do dma */
if (dev->irq && dma0) {
/* encode dma0 and dma1 into 2 digit hexadecimal for switch */
switch ((dma0 & 0x7) | (dma1 << 4)) {
case 0x5: /* dma0 == 5 */
devpriv->dma_bits |= DMA_CH5;
break;
case 0x6: /* dma0 == 6 */
devpriv->dma_bits |= DMA_CH6;
break;
case 0x7: /* dma0 == 7 */
devpriv->dma_bits |= DMA_CH7;
break;
case 0x65: /* dma0 == 5, dma1 == 6 */
devpriv->dma_bits |= DMA_CH5_CH6;
break;
case 0x76: /* dma0 == 6, dma1 == 7 */
devpriv->dma_bits |= DMA_CH6_CH7;
break;
case 0x57: /* dma0 == 7, dma1 == 5 */
devpriv->dma_bits |= DMA_CH7_CH5;
break;
default:
dev_err(dev->hw_dev, " only supports dma channels 5 through 7\n"
" Dual dma only allows the following combinations:\n"
" dma 5,6 / 6,7 / or 7,5\n");
return -EINVAL;
break;
}
if (request_dma(dma0, dev->driver->driver_name)) {
dev_err(dev->hw_dev, "failed to allocate dma channel %i\n",
dma0);
return -EINVAL;
}
devpriv->dma0 = dma0;
devpriv->dma_current = dma0;
if (dma1) {
if (request_dma(dma1, dev->driver->driver_name)) {
dev_err(dev->hw_dev, "failed to allocate dma channel %i\n",
dma1);
return -EINVAL;
}
devpriv->dma1 = dma1;
}
devpriv->ai_buf0 = kmalloc(DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA);
if (devpriv->ai_buf0 == NULL)
return -ENOMEM;
devpriv->dma_current_buf = devpriv->ai_buf0;
if (dma1) {
devpriv->ai_buf1 =
kmalloc(DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA);
if (devpriv->ai_buf1 == NULL)
return -ENOMEM;
}
flags = claim_dma_lock();
disable_dma(devpriv->dma0);
set_dma_mode(devpriv->dma0, DMA_MODE_READ);
if (dma1) {
disable_dma(devpriv->dma1);
set_dma_mode(devpriv->dma1, DMA_MODE_READ);
}
release_dma_lock(flags);
}
return 0;
} }
static int das1800_attach(struct comedi_device *dev, static void munge_data(struct comedi_device *dev, uint16_t * array,
struct comedi_devconfig *it) unsigned int num_elements)
{ {
struct comedi_subdevice *s; unsigned int i;
unsigned long iobase = it->options[0]; int unipolar;
unsigned int irq = it->options[1];
unsigned int dma0 = it->options[2];
unsigned int dma1 = it->options[3];
unsigned long iobase2;
int board;
int retval;
/* allocate and initialize dev->private */ /* see if card is using a unipolar or bipolar range so we can munge data correctly */
if (alloc_private(dev, sizeof(struct das1800_private)) < 0) unipolar = inb(dev->iobase + DAS1800_CONTROL_C) & UB;
return -ENOMEM;
printk(KERN_DEBUG "comedi%d: %s: io 0x%lx", dev->minor, /* convert to unsigned type if we are in a bipolar mode */
dev->driver->driver_name, iobase); if (!unipolar) {
if (irq) { for (i = 0; i < num_elements; i++)
printk(KERN_CONT ", irq %u", irq); array[i] = munge_bipolar_sample(dev, array[i]);
if (dma0) {
printk(KERN_CONT ", dma %u", dma0);
if (dma1)
printk(KERN_CONT " and %u", dma1);
}
} }
printk(KERN_CONT "\n"); }
if (iobase == 0) { static void das1800_handle_fifo_half_full(struct comedi_device *dev,
dev_err(dev->hw_dev, "io base address required\n"); struct comedi_subdevice *s)
return -EINVAL; {
} int numPoints = 0; /* number of points to read */
struct comedi_cmd *cmd = &s->async->cmd;
/* check if io addresses are available */ numPoints = FIFO_SIZE / 2;
if (!request_region(iobase, DAS1800_SIZE, dev->driver->driver_name)) { /* if we only need some of the points */
printk if (cmd->stop_src == TRIG_COUNT && devpriv->count < numPoints)
(" I/O port conflict: failed to allocate ports 0x%lx to 0x%lx\n", numPoints = devpriv->count;
iobase, iobase + DAS1800_SIZE - 1); insw(dev->iobase + DAS1800_FIFO, devpriv->ai_buf0, numPoints);
return -EIO; munge_data(dev, devpriv->ai_buf0, numPoints);
} cfc_write_array_to_buffer(s, devpriv->ai_buf0,
dev->iobase = iobase; numPoints * sizeof(devpriv->ai_buf0[0]));
if (cmd->stop_src == TRIG_COUNT)
devpriv->count -= numPoints;
return;
}
board = das1800_probe(dev); static void das1800_handle_fifo_not_empty(struct comedi_device *dev,
if (board < 0) { struct comedi_subdevice *s)
dev_err(dev->hw_dev, "unable to determine board type\n"); {
return -ENODEV; short dpnt;
} int unipolar;
struct comedi_cmd *cmd = &s->async->cmd;
dev->board_ptr = das1800_boards + board; unipolar = inb(dev->iobase + DAS1800_CONTROL_C) & UB;
dev->board_name = thisboard->name;
/* if it is an 'ao' board with fancy analog out then we need extra io ports */ while (inb(dev->iobase + DAS1800_STATUS) & FNE) {
if (thisboard->ao_ability == 2) { if (cmd->stop_src == TRIG_COUNT && devpriv->count == 0)
iobase2 = iobase + IOBASE2; break;
if (!request_region(iobase2, DAS1800_SIZE, dpnt = inw(dev->iobase + DAS1800_FIFO);
dev->driver->driver_name)) { /* convert to unsigned type if we are in a bipolar mode */
printk if (!unipolar)
(" I/O port conflict: failed to allocate ports 0x%lx to 0x%lx\n", ;
iobase2, iobase2 + DAS1800_SIZE - 1); dpnt = munge_bipolar_sample(dev, dpnt);
return -EIO; cfc_write_to_buffer(s, dpnt);
} if (cmd->stop_src == TRIG_COUNT)
devpriv->iobase2 = iobase2; devpriv->count--;
} }
/* grab our IRQ */ return;
if (irq) { }
if (request_irq(irq, das1800_interrupt, 0,
dev->driver->driver_name, dev)) {
dev_dbg(dev->hw_dev, "unable to allocate irq %u\n",
irq);
return -EINVAL;
}
}
dev->irq = irq;
/* set bits that tell card which irq to use */ /* Utility function used by das1800_flush_dma() and das1800_handle_dma().
switch (irq) { * Assumes dma lock is held */
case 0: static void das1800_flush_dma_channel(struct comedi_device *dev,
break; struct comedi_subdevice *s,
case 3: unsigned int channel, uint16_t *buffer)
devpriv->irq_dma_bits |= 0x8; {
break; unsigned int num_bytes, num_samples;
case 5: struct comedi_cmd *cmd = &s->async->cmd;
devpriv->irq_dma_bits |= 0x10;
break;
case 7:
devpriv->irq_dma_bits |= 0x18;
break;
case 10:
devpriv->irq_dma_bits |= 0x28;
break;
case 11:
devpriv->irq_dma_bits |= 0x30;
break;
case 15:
devpriv->irq_dma_bits |= 0x38;
break;
default:
dev_err(dev->hw_dev, "irq out of range\n");
return -EINVAL;
break;
}
retval = das1800_init_dma(dev, dma0, dma1); disable_dma(channel);
if (retval < 0)
return retval;
if (devpriv->ai_buf0 == NULL) { /* clear flip-flop to make sure 2-byte registers
devpriv->ai_buf0 = * get set correctly */
kmalloc(FIFO_SIZE * sizeof(uint16_t), GFP_KERNEL); clear_dma_ff(channel);
if (devpriv->ai_buf0 == NULL)
return -ENOMEM;
}
if (alloc_subdevices(dev, 4) < 0) /* figure out how many points to read */
return -ENOMEM; num_bytes = devpriv->dma_transfer_size - get_dma_residue(channel);
num_samples = num_bytes / sizeof(short);
/* analog input subdevice */ /* if we only need some of the points */
s = dev->subdevices + 0; if (cmd->stop_src == TRIG_COUNT && devpriv->count < num_samples)
dev->read_subdev = s; num_samples = devpriv->count;
s->type = COMEDI_SUBD_AI;
s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_GROUND | SDF_CMD_READ;
if (thisboard->common)
s->subdev_flags |= SDF_COMMON;
s->n_chan = thisboard->qram_len;
s->len_chanlist = thisboard->qram_len;
s->maxdata = (1 << thisboard->resolution) - 1;
s->range_table = thisboard->range_ai;
s->do_cmd = das1800_ai_do_cmd;
s->do_cmdtest = das1800_ai_do_cmdtest;
s->insn_read = das1800_ai_rinsn;
s->poll = das1800_ai_poll;
s->cancel = das1800_cancel;
/* analog out */
s = dev->subdevices + 1;
if (thisboard->ao_ability == 1) {
s->type = COMEDI_SUBD_AO;
s->subdev_flags = SDF_WRITABLE;
s->n_chan = thisboard->ao_n_chan;
s->maxdata = (1 << thisboard->resolution) - 1;
s->range_table = &range_ao_1;
s->insn_write = das1800_ao_winsn;
} else {
s->type = COMEDI_SUBD_UNUSED;
}
/* di */ munge_data(dev, buffer, num_samples);
s = dev->subdevices + 2; cfc_write_array_to_buffer(s, buffer, num_bytes);
s->type = COMEDI_SUBD_DI; if (s->async->cmd.stop_src == TRIG_COUNT)
s->subdev_flags = SDF_READABLE; devpriv->count -= num_samples;
s->n_chan = 4;
s->maxdata = 1;
s->range_table = &range_digital;
s->insn_bits = das1800_di_rbits;
/* do */ return;
s = dev->subdevices + 3; }
s->type = COMEDI_SUBD_DO;
s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
s->n_chan = thisboard->do_n_chan;
s->maxdata = 1;
s->range_table = &range_digital;
s->insn_bits = das1800_do_wbits;
das1800_cancel(dev, dev->read_subdev); /* flushes remaining data from board when external trigger has stopped acquisition
* and we are using dma transfers */
static void das1800_flush_dma(struct comedi_device *dev,
struct comedi_subdevice *s)
{
unsigned long flags;
const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
/* initialize digital out channels */ flags = claim_dma_lock();
outb(devpriv->do_bits, dev->iobase + DAS1800_DIGITAL); das1800_flush_dma_channel(dev, s, devpriv->dma_current,
devpriv->dma_current_buf);
/* initialize analog out channels */ if (dual_dma) {
if (thisboard->ao_ability == 1) { /* switch to other channel and flush it */
/* select 'update' dac channel for baseAddress + 0x0 */ if (devpriv->dma_current == devpriv->dma0) {
outb(DAC(thisboard->ao_n_chan - 1), devpriv->dma_current = devpriv->dma1;
dev->iobase + DAS1800_SELECT); devpriv->dma_current_buf = devpriv->ai_buf1;
outw(devpriv->ao_update_bits, dev->iobase + DAS1800_DAC); } else {
devpriv->dma_current = devpriv->dma0;
devpriv->dma_current_buf = devpriv->ai_buf0;
}
das1800_flush_dma_channel(dev, s, devpriv->dma_current,
devpriv->dma_current_buf);
} }
return 0; release_dma_lock(flags);
};
static void das1800_detach(struct comedi_device *dev) /* get any remaining samples in fifo */
{ das1800_handle_fifo_not_empty(dev, s);
if (dev->iobase)
release_region(dev->iobase, DAS1800_SIZE);
if (dev->irq)
free_irq(dev->irq, dev);
if (dev->private) {
if (devpriv->iobase2)
release_region(devpriv->iobase2, DAS1800_SIZE);
if (devpriv->dma0)
free_dma(devpriv->dma0);
if (devpriv->dma1)
free_dma(devpriv->dma1);
kfree(devpriv->ai_buf0);
kfree(devpriv->ai_buf1);
}
};
/* probes and checks das-1800 series board type return;
*/ }
static int das1800_probe(struct comedi_device *dev)
static void das1800_handle_dma(struct comedi_device *dev,
struct comedi_subdevice *s, unsigned int status)
{ {
int id; unsigned long flags;
int board; const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
id = (inb(dev->iobase + DAS1800_DIGITAL) >> 4) & 0xf; /* get id bits */ flags = claim_dma_lock();
board = ((struct das1800_board *)dev->board_ptr) - das1800_boards; das1800_flush_dma_channel(dev, s, devpriv->dma_current,
devpriv->dma_current_buf);
/* re-enable dma channel */
set_dma_addr(devpriv->dma_current,
virt_to_bus(devpriv->dma_current_buf));
set_dma_count(devpriv->dma_current, devpriv->dma_transfer_size);
enable_dma(devpriv->dma_current);
release_dma_lock(flags);
switch (id) { if (status & DMATC) {
case 0x3: /* clear DMATC interrupt bit */
if (board == das1801st_da || board == das1802st_da || outb(CLEAR_INTR_MASK & ~DMATC, dev->iobase + DAS1800_STATUS);
board == das1701st_da || board == das1702st_da) { /* switch dma channels for next time, if appropriate */
dev_dbg(dev->hw_dev, "Board model: %s\n", if (dual_dma) {
das1800_boards[board].name); /* read data from the other channel next time */
return board; if (devpriv->dma_current == devpriv->dma0) {
} devpriv->dma_current = devpriv->dma1;
printk devpriv->dma_current_buf = devpriv->ai_buf1;
(" Board model (probed, not recommended): das-1800st-da series\n"); } else {
return das1801st; devpriv->dma_current = devpriv->dma0;
break; devpriv->dma_current_buf = devpriv->ai_buf0;
case 0x4: }
if (board == das1802hr_da || board == das1702hr_da) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1802hr-da\n");
return das1802hr;
break;
case 0x5:
if (board == das1801ao || board == das1802ao ||
board == das1701ao || board == das1702ao) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1800ao series\n");
return das1801ao;
break;
case 0x6:
if (board == das1802hr || board == das1702hr) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1802hr\n");
return das1802hr;
break;
case 0x7:
if (board == das1801st || board == das1802st ||
board == das1701st || board == das1702st) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1800st series\n");
return das1801st;
break;
case 0x8:
if (board == das1801hc || board == das1802hc) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
} }
printk
(" Board model (probed, not recommended): das-1800hc series\n");
return das1801hc;
break;
default:
printk
(" Board model: probe returned 0x%x (unknown, please report)\n",
id);
return board;
break;
} }
return -1;
return;
} }
static int das1800_ai_poll(struct comedi_device *dev, static int das1800_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
struct comedi_subdevice *s)
{ {
unsigned long flags; outb(0x0, dev->iobase + DAS1800_STATUS); /* disable conversions */
outb(0x0, dev->iobase + DAS1800_CONTROL_B); /* disable interrupts and dma */
/* prevent race with interrupt handler */ outb(0x0, dev->iobase + DAS1800_CONTROL_A); /* disable and clear fifo and stop triggering */
spin_lock_irqsave(&dev->spinlock, flags); if (devpriv->dma0)
das1800_ai_handler(dev); disable_dma(devpriv->dma0);
spin_unlock_irqrestore(&dev->spinlock, flags); if (devpriv->dma1)
disable_dma(devpriv->dma1);
return s->async->buf_write_count - s->async->buf_read_count; return 0;
} }
static irqreturn_t das1800_interrupt(int irq, void *d) /* the guts of the interrupt handler, that is shared with das1800_ai_poll */
static void das1800_ai_handler(struct comedi_device *dev)
{ {
struct comedi_device *dev = d; struct comedi_subdevice *s = dev->subdevices + 0; /* analog input subdevice */
unsigned int status; struct comedi_async *async = s->async;
struct comedi_cmd *cmd = &async->cmd;
unsigned int status = inb(dev->iobase + DAS1800_STATUS);
if (dev->attached == 0) { async->events = 0;
comedi_error(dev, "premature interrupt"); /* select adc for base address + 0 */
return IRQ_HANDLED; outb(ADC, dev->iobase + DAS1800_SELECT);
} /* dma buffer full */
if (devpriv->irq_dma_bits & DMA_ENABLED) {
/* Prevent race with das1800_ai_poll() on multi processor systems. /* look for data from dma transfer even if dma terminal count hasn't happened yet */
* Also protects indirect addressing in das1800_ai_handler */ das1800_handle_dma(dev, s, status);
spin_lock(&dev->spinlock); } else if (status & FHF) { /* if fifo half full */
status = inb(dev->iobase + DAS1800_STATUS); das1800_handle_fifo_half_full(dev, s);
} else if (status & FNE) { /* if fifo not empty */
/* if interrupt was not caused by das-1800 */ das1800_handle_fifo_not_empty(dev, s);
if (!(status & INT)) {
spin_unlock(&dev->spinlock);
return IRQ_NONE;
}
/* clear the interrupt status bit INT */
outb(CLEAR_INTR_MASK & ~INT, dev->iobase + DAS1800_STATUS);
/* handle interrupt */
das1800_ai_handler(dev);
spin_unlock(&dev->spinlock);
return IRQ_HANDLED;
}
/* the guts of the interrupt handler, that is shared with das1800_ai_poll */
static void das1800_ai_handler(struct comedi_device *dev)
{
struct comedi_subdevice *s = dev->subdevices + 0; /* analog input subdevice */
struct comedi_async *async = s->async;
struct comedi_cmd *cmd = &async->cmd;
unsigned int status = inb(dev->iobase + DAS1800_STATUS);
async->events = 0;
/* select adc for base address + 0 */
outb(ADC, dev->iobase + DAS1800_SELECT);
/* dma buffer full */
if (devpriv->irq_dma_bits & DMA_ENABLED) {
/* look for data from dma transfer even if dma terminal count hasn't happened yet */
das1800_handle_dma(dev, s, status);
} else if (status & FHF) { /* if fifo half full */
das1800_handle_fifo_half_full(dev, s);
} else if (status & FNE) { /* if fifo not empty */
das1800_handle_fifo_not_empty(dev, s);
} }
async->events |= COMEDI_CB_BLOCK; async->events |= COMEDI_CB_BLOCK;
...@@ -965,182 +707,75 @@ static void das1800_ai_handler(struct comedi_device *dev) ...@@ -965,182 +707,75 @@ static void das1800_ai_handler(struct comedi_device *dev)
return; return;
} }
static void das1800_handle_dma(struct comedi_device *dev, static int das1800_ai_poll(struct comedi_device *dev,
struct comedi_subdevice *s, unsigned int status) struct comedi_subdevice *s)
{ {
unsigned long flags; unsigned long flags;
const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
flags = claim_dma_lock();
das1800_flush_dma_channel(dev, s, devpriv->dma_current,
devpriv->dma_current_buf);
/* re-enable dma channel */
set_dma_addr(devpriv->dma_current,
virt_to_bus(devpriv->dma_current_buf));
set_dma_count(devpriv->dma_current, devpriv->dma_transfer_size);
enable_dma(devpriv->dma_current);
release_dma_lock(flags);
if (status & DMATC) {
/* clear DMATC interrupt bit */
outb(CLEAR_INTR_MASK & ~DMATC, dev->iobase + DAS1800_STATUS);
/* switch dma channels for next time, if appropriate */
if (dual_dma) {
/* read data from the other channel next time */
if (devpriv->dma_current == devpriv->dma0) {
devpriv->dma_current = devpriv->dma1;
devpriv->dma_current_buf = devpriv->ai_buf1;
} else {
devpriv->dma_current = devpriv->dma0;
devpriv->dma_current_buf = devpriv->ai_buf0;
}
}
}
return; /* prevent race with interrupt handler */
} spin_lock_irqsave(&dev->spinlock, flags);
das1800_ai_handler(dev);
spin_unlock_irqrestore(&dev->spinlock, flags);
static inline uint16_t munge_bipolar_sample(const struct comedi_device *dev, return s->async->buf_write_count - s->async->buf_read_count;
uint16_t sample)
{
sample += 1 << (thisboard->resolution - 1);
return sample;
} }
static void munge_data(struct comedi_device *dev, uint16_t * array, static irqreturn_t das1800_interrupt(int irq, void *d)
unsigned int num_elements)
{ {
unsigned int i; struct comedi_device *dev = d;
int unipolar; unsigned int status;
/* see if card is using a unipolar or bipolar range so we can munge data correctly */
unipolar = inb(dev->iobase + DAS1800_CONTROL_C) & UB;
/* convert to unsigned type if we are in a bipolar mode */ if (dev->attached == 0) {
if (!unipolar) { comedi_error(dev, "premature interrupt");
for (i = 0; i < num_elements; i++) return IRQ_HANDLED;
array[i] = munge_bipolar_sample(dev, array[i]);
} }
}
/* Utility function used by das1800_flush_dma() and das1800_handle_dma().
* Assumes dma lock is held */
static void das1800_flush_dma_channel(struct comedi_device *dev,
struct comedi_subdevice *s,
unsigned int channel, uint16_t *buffer)
{
unsigned int num_bytes, num_samples;
struct comedi_cmd *cmd = &s->async->cmd;
disable_dma(channel);
/* clear flip-flop to make sure 2-byte registers
* get set correctly */
clear_dma_ff(channel);
/* figure out how many points to read */
num_bytes = devpriv->dma_transfer_size - get_dma_residue(channel);
num_samples = num_bytes / sizeof(short);
/* if we only need some of the points */
if (cmd->stop_src == TRIG_COUNT && devpriv->count < num_samples)
num_samples = devpriv->count;
munge_data(dev, buffer, num_samples);
cfc_write_array_to_buffer(s, buffer, num_bytes);
if (s->async->cmd.stop_src == TRIG_COUNT)
devpriv->count -= num_samples;
return; /* Prevent race with das1800_ai_poll() on multi processor systems.
} * Also protects indirect addressing in das1800_ai_handler */
spin_lock(&dev->spinlock);
/* flushes remaining data from board when external trigger has stopped acquisition status = inb(dev->iobase + DAS1800_STATUS);
* and we are using dma transfers */
static void das1800_flush_dma(struct comedi_device *dev,
struct comedi_subdevice *s)
{
unsigned long flags;
const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
flags = claim_dma_lock();
das1800_flush_dma_channel(dev, s, devpriv->dma_current,
devpriv->dma_current_buf);
if (dual_dma) { /* if interrupt was not caused by das-1800 */
/* switch to other channel and flush it */ if (!(status & INT)) {
if (devpriv->dma_current == devpriv->dma0) { spin_unlock(&dev->spinlock);
devpriv->dma_current = devpriv->dma1; return IRQ_NONE;
devpriv->dma_current_buf = devpriv->ai_buf1;
} else {
devpriv->dma_current = devpriv->dma0;
devpriv->dma_current_buf = devpriv->ai_buf0;
}
das1800_flush_dma_channel(dev, s, devpriv->dma_current,
devpriv->dma_current_buf);
} }
/* clear the interrupt status bit INT */
outb(CLEAR_INTR_MASK & ~INT, dev->iobase + DAS1800_STATUS);
/* handle interrupt */
das1800_ai_handler(dev);
release_dma_lock(flags); spin_unlock(&dev->spinlock);
return IRQ_HANDLED;
/* get any remaining samples in fifo */
das1800_handle_fifo_not_empty(dev, s);
return;
}
static void das1800_handle_fifo_half_full(struct comedi_device *dev,
struct comedi_subdevice *s)
{
int numPoints = 0; /* number of points to read */
struct comedi_cmd *cmd = &s->async->cmd;
numPoints = FIFO_SIZE / 2;
/* if we only need some of the points */
if (cmd->stop_src == TRIG_COUNT && devpriv->count < numPoints)
numPoints = devpriv->count;
insw(dev->iobase + DAS1800_FIFO, devpriv->ai_buf0, numPoints);
munge_data(dev, devpriv->ai_buf0, numPoints);
cfc_write_array_to_buffer(s, devpriv->ai_buf0,
numPoints * sizeof(devpriv->ai_buf0[0]));
if (cmd->stop_src == TRIG_COUNT)
devpriv->count -= numPoints;
return;
} }
static void das1800_handle_fifo_not_empty(struct comedi_device *dev, /* converts requested conversion timing to timing compatible with
struct comedi_subdevice *s) * hardware, used only when card is in 'burst mode'
*/
static unsigned int burst_convert_arg(unsigned int convert_arg, int round_mode)
{ {
short dpnt; unsigned int micro_sec;
int unipolar;
struct comedi_cmd *cmd = &s->async->cmd;
unipolar = inb(dev->iobase + DAS1800_CONTROL_C) & UB; /* in burst mode, the maximum conversion time is 64 microseconds */
if (convert_arg > 64000)
convert_arg = 64000;
while (inb(dev->iobase + DAS1800_STATUS) & FNE) { /* the conversion time must be an integral number of microseconds */
if (cmd->stop_src == TRIG_COUNT && devpriv->count == 0) switch (round_mode) {
break; case TRIG_ROUND_NEAREST:
dpnt = inw(dev->iobase + DAS1800_FIFO); default:
/* convert to unsigned type if we are in a bipolar mode */ micro_sec = (convert_arg + 500) / 1000;
if (!unipolar) break;
; case TRIG_ROUND_DOWN:
dpnt = munge_bipolar_sample(dev, dpnt); micro_sec = convert_arg / 1000;
cfc_write_to_buffer(s, dpnt); break;
if (cmd->stop_src == TRIG_COUNT) case TRIG_ROUND_UP:
devpriv->count--; micro_sec = (convert_arg - 1) / 1000 + 1;
break;
} }
return; /* return number of nanoseconds */
} return micro_sec * 1000;
static int das1800_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
{
outb(0x0, dev->iobase + DAS1800_STATUS); /* disable conversions */
outb(0x0, dev->iobase + DAS1800_CONTROL_B); /* disable interrupts and dma */
outb(0x0, dev->iobase + DAS1800_CONTROL_A); /* disable and clear fifo and stop triggering */
if (devpriv->dma0)
disable_dma(devpriv->dma0);
if (devpriv->dma1)
disable_dma(devpriv->dma1);
return 0;
} }
/* test analog input cmd */ /* test analog input cmd */
...@@ -1322,10 +957,6 @@ static int das1800_ai_do_cmdtest(struct comedi_device *dev, ...@@ -1322,10 +957,6 @@ static int das1800_ai_do_cmdtest(struct comedi_device *dev,
return 0; return 0;
} }
/* analog input cmd interface */
/* first, some utility functions used in the main ai_do_cmd() */
/* returns appropriate bits for control register a, depending on command */ /* returns appropriate bits for control register a, depending on command */
static int control_a_bits(struct comedi_cmd cmd) static int control_a_bits(struct comedi_cmd cmd)
{ {
...@@ -1396,15 +1027,34 @@ static int control_c_bits(struct comedi_cmd cmd) ...@@ -1396,15 +1027,34 @@ static int control_c_bits(struct comedi_cmd cmd)
return control_c; return control_c;
} }
/* sets up counters */ /* loads counters with divisor1, divisor2 from private structure */
static int setup_counters(struct comedi_device *dev, struct comedi_cmd cmd) static int das1800_set_frequency(struct comedi_device *dev)
{ {
/* setup cascaded counters for conversion/scan frequency */ int err = 0;
switch (cmd.scan_begin_src) {
case TRIG_FOLLOW: /* not in burst mode */ /* counter 1, mode 2 */
if (cmd.convert_src == TRIG_TIMER) { if (i8254_load(dev->iobase + DAS1800_COUNTER, 0, 1, devpriv->divisor1,
/* set conversion frequency */ 2))
i8253_cascade_ns_to_timer_2div(TIMER_BASE, err++;
/* counter 2, mode 2 */
if (i8254_load(dev->iobase + DAS1800_COUNTER, 0, 2, devpriv->divisor2,
2))
err++;
if (err)
return -1;
return 0;
}
/* sets up counters */
static int setup_counters(struct comedi_device *dev, struct comedi_cmd cmd)
{
/* setup cascaded counters for conversion/scan frequency */
switch (cmd.scan_begin_src) {
case TRIG_FOLLOW: /* not in burst mode */
if (cmd.convert_src == TRIG_TIMER) {
/* set conversion frequency */
i8253_cascade_ns_to_timer_2div(TIMER_BASE,
&(devpriv->divisor1), &(devpriv->divisor1),
&(devpriv->divisor2), &(devpriv->divisor2),
&(cmd.convert_arg), &(cmd.convert_arg),
...@@ -1436,6 +1086,44 @@ static int setup_counters(struct comedi_device *dev, struct comedi_cmd cmd) ...@@ -1436,6 +1086,44 @@ static int setup_counters(struct comedi_device *dev, struct comedi_cmd cmd)
return 0; return 0;
} }
/* utility function that suggests a dma transfer size based on the conversion period 'ns' */
static unsigned int suggest_transfer_size(struct comedi_cmd *cmd)
{
unsigned int size = DMA_BUF_SIZE;
static const int sample_size = 2; /* size in bytes of one sample from board */
unsigned int fill_time = 300000000; /* target time in nanoseconds for filling dma buffer */
unsigned int max_size; /* maximum size we will allow for a transfer */
/* make dma buffer fill in 0.3 seconds for timed modes */
switch (cmd->scan_begin_src) {
case TRIG_FOLLOW: /* not in burst mode */
if (cmd->convert_src == TRIG_TIMER)
size = (fill_time / cmd->convert_arg) * sample_size;
break;
case TRIG_TIMER:
size = (fill_time / (cmd->scan_begin_arg * cmd->chanlist_len)) *
sample_size;
break;
default:
size = DMA_BUF_SIZE;
break;
}
/* set a minimum and maximum size allowed */
max_size = DMA_BUF_SIZE;
/* if we are taking limited number of conversions, limit transfer size to that */
if (cmd->stop_src == TRIG_COUNT &&
cmd->stop_arg * cmd->chanlist_len * sample_size < max_size)
max_size = cmd->stop_arg * cmd->chanlist_len * sample_size;
if (size > max_size)
size = max_size;
if (size < sample_size)
size = sample_size;
return size;
}
/* sets up dma */ /* sets up dma */
static void setup_dma(struct comedi_device *dev, struct comedi_cmd cmd) static void setup_dma(struct comedi_device *dev, struct comedi_cmd cmd)
{ {
...@@ -1689,91 +1377,357 @@ static int das1800_do_wbits(struct comedi_device *dev, ...@@ -1689,91 +1377,357 @@ static int das1800_do_wbits(struct comedi_device *dev,
return 2; return 2;
} }
/* loads counters with divisor1, divisor2 from private structure */ static int das1800_init_dma(struct comedi_device *dev, unsigned int dma0,
static int das1800_set_frequency(struct comedi_device *dev) unsigned int dma1)
{ {
int err = 0; unsigned long flags;
/* counter 1, mode 2 */
if (i8254_load(dev->iobase + DAS1800_COUNTER, 0, 1, devpriv->divisor1,
2))
err++;
/* counter 2, mode 2 */
if (i8254_load(dev->iobase + DAS1800_COUNTER, 0, 2, devpriv->divisor2,
2))
err++;
if (err)
return -1;
/* need an irq to do dma */
if (dev->irq && dma0) {
/* encode dma0 and dma1 into 2 digit hexadecimal for switch */
switch ((dma0 & 0x7) | (dma1 << 4)) {
case 0x5: /* dma0 == 5 */
devpriv->dma_bits |= DMA_CH5;
break;
case 0x6: /* dma0 == 6 */
devpriv->dma_bits |= DMA_CH6;
break;
case 0x7: /* dma0 == 7 */
devpriv->dma_bits |= DMA_CH7;
break;
case 0x65: /* dma0 == 5, dma1 == 6 */
devpriv->dma_bits |= DMA_CH5_CH6;
break;
case 0x76: /* dma0 == 6, dma1 == 7 */
devpriv->dma_bits |= DMA_CH6_CH7;
break;
case 0x57: /* dma0 == 7, dma1 == 5 */
devpriv->dma_bits |= DMA_CH7_CH5;
break;
default:
dev_err(dev->hw_dev, " only supports dma channels 5 through 7\n"
" Dual dma only allows the following combinations:\n"
" dma 5,6 / 6,7 / or 7,5\n");
return -EINVAL;
break;
}
if (request_dma(dma0, dev->driver->driver_name)) {
dev_err(dev->hw_dev, "failed to allocate dma channel %i\n",
dma0);
return -EINVAL;
}
devpriv->dma0 = dma0;
devpriv->dma_current = dma0;
if (dma1) {
if (request_dma(dma1, dev->driver->driver_name)) {
dev_err(dev->hw_dev, "failed to allocate dma channel %i\n",
dma1);
return -EINVAL;
}
devpriv->dma1 = dma1;
}
devpriv->ai_buf0 = kmalloc(DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA);
if (devpriv->ai_buf0 == NULL)
return -ENOMEM;
devpriv->dma_current_buf = devpriv->ai_buf0;
if (dma1) {
devpriv->ai_buf1 =
kmalloc(DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA);
if (devpriv->ai_buf1 == NULL)
return -ENOMEM;
}
flags = claim_dma_lock();
disable_dma(devpriv->dma0);
set_dma_mode(devpriv->dma0, DMA_MODE_READ);
if (dma1) {
disable_dma(devpriv->dma1);
set_dma_mode(devpriv->dma1, DMA_MODE_READ);
}
release_dma_lock(flags);
}
return 0; return 0;
} }
/* converts requested conversion timing to timing compatible with static int das1800_probe(struct comedi_device *dev)
* hardware, used only when card is in 'burst mode'
*/
static unsigned int burst_convert_arg(unsigned int convert_arg, int round_mode)
{ {
unsigned int micro_sec; int id;
int board;
/* in burst mode, the maximum conversion time is 64 microseconds */ id = (inb(dev->iobase + DAS1800_DIGITAL) >> 4) & 0xf; /* get id bits */
if (convert_arg > 64000) board = ((struct das1800_board *)dev->board_ptr) - das1800_boards;
convert_arg = 64000;
/* the conversion time must be an integral number of microseconds */ switch (id) {
switch (round_mode) { case 0x3:
case TRIG_ROUND_NEAREST: if (board == das1801st_da || board == das1802st_da ||
default: board == das1701st_da || board == das1702st_da) {
micro_sec = (convert_arg + 500) / 1000; dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1800st-da series\n");
return das1801st;
break; break;
case TRIG_ROUND_DOWN: case 0x4:
micro_sec = convert_arg / 1000; if (board == das1802hr_da || board == das1702hr_da) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1802hr-da\n");
return das1802hr;
break; break;
case TRIG_ROUND_UP: case 0x5:
micro_sec = (convert_arg - 1) / 1000 + 1; if (board == das1801ao || board == das1802ao ||
board == das1701ao || board == das1702ao) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1800ao series\n");
return das1801ao;
break;
case 0x6:
if (board == das1802hr || board == das1702hr) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1802hr\n");
return das1802hr;
break;
case 0x7:
if (board == das1801st || board == das1802st ||
board == das1701st || board == das1702st) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1800st series\n");
return das1801st;
break;
case 0x8:
if (board == das1801hc || board == das1802hc) {
dev_dbg(dev->hw_dev, "Board model: %s\n",
das1800_boards[board].name);
return board;
}
printk
(" Board model (probed, not recommended): das-1800hc series\n");
return das1801hc;
break;
default:
printk
(" Board model: probe returned 0x%x (unknown, please report)\n",
id);
return board;
break; break;
} }
return -1;
/* return number of nanoseconds */
return micro_sec * 1000;
} }
/* utility function that suggests a dma transfer size based on the conversion period 'ns' */ static int das1800_attach(struct comedi_device *dev,
static unsigned int suggest_transfer_size(struct comedi_cmd *cmd) struct comedi_devconfig *it)
{ {
unsigned int size = DMA_BUF_SIZE; struct comedi_subdevice *s;
static const int sample_size = 2; /* size in bytes of one sample from board */ unsigned long iobase = it->options[0];
unsigned int fill_time = 300000000; /* target time in nanoseconds for filling dma buffer */ unsigned int irq = it->options[1];
unsigned int max_size; /* maximum size we will allow for a transfer */ unsigned int dma0 = it->options[2];
unsigned int dma1 = it->options[3];
unsigned long iobase2;
int board;
int retval;
/* make dma buffer fill in 0.3 seconds for timed modes */ /* allocate and initialize dev->private */
switch (cmd->scan_begin_src) { if (alloc_private(dev, sizeof(struct das1800_private)) < 0)
case TRIG_FOLLOW: /* not in burst mode */ return -ENOMEM;
if (cmd->convert_src == TRIG_TIMER)
size = (fill_time / cmd->convert_arg) * sample_size; printk(KERN_DEBUG "comedi%d: %s: io 0x%lx", dev->minor,
dev->driver->driver_name, iobase);
if (irq) {
printk(KERN_CONT ", irq %u", irq);
if (dma0) {
printk(KERN_CONT ", dma %u", dma0);
if (dma1)
printk(KERN_CONT " and %u", dma1);
}
}
printk(KERN_CONT "\n");
if (iobase == 0) {
dev_err(dev->hw_dev, "io base address required\n");
return -EINVAL;
}
/* check if io addresses are available */
if (!request_region(iobase, DAS1800_SIZE, dev->driver->driver_name)) {
printk
(" I/O port conflict: failed to allocate ports 0x%lx to 0x%lx\n",
iobase, iobase + DAS1800_SIZE - 1);
return -EIO;
}
dev->iobase = iobase;
board = das1800_probe(dev);
if (board < 0) {
dev_err(dev->hw_dev, "unable to determine board type\n");
return -ENODEV;
}
dev->board_ptr = das1800_boards + board;
dev->board_name = thisboard->name;
/* if it is an 'ao' board with fancy analog out then we need extra io ports */
if (thisboard->ao_ability == 2) {
iobase2 = iobase + IOBASE2;
if (!request_region(iobase2, DAS1800_SIZE,
dev->driver->driver_name)) {
printk
(" I/O port conflict: failed to allocate ports 0x%lx to 0x%lx\n",
iobase2, iobase2 + DAS1800_SIZE - 1);
return -EIO;
}
devpriv->iobase2 = iobase2;
}
/* grab our IRQ */
if (irq) {
if (request_irq(irq, das1800_interrupt, 0,
dev->driver->driver_name, dev)) {
dev_dbg(dev->hw_dev, "unable to allocate irq %u\n",
irq);
return -EINVAL;
}
}
dev->irq = irq;
/* set bits that tell card which irq to use */
switch (irq) {
case 0:
break; break;
case TRIG_TIMER: case 3:
size = (fill_time / (cmd->scan_begin_arg * cmd->chanlist_len)) * devpriv->irq_dma_bits |= 0x8;
sample_size; break;
case 5:
devpriv->irq_dma_bits |= 0x10;
break;
case 7:
devpriv->irq_dma_bits |= 0x18;
break;
case 10:
devpriv->irq_dma_bits |= 0x28;
break;
case 11:
devpriv->irq_dma_bits |= 0x30;
break;
case 15:
devpriv->irq_dma_bits |= 0x38;
break; break;
default: default:
size = DMA_BUF_SIZE; dev_err(dev->hw_dev, "irq out of range\n");
return -EINVAL;
break; break;
} }
/* set a minimum and maximum size allowed */ retval = das1800_init_dma(dev, dma0, dma1);
max_size = DMA_BUF_SIZE; if (retval < 0)
/* if we are taking limited number of conversions, limit transfer size to that */ return retval;
if (cmd->stop_src == TRIG_COUNT &&
cmd->stop_arg * cmd->chanlist_len * sample_size < max_size)
max_size = cmd->stop_arg * cmd->chanlist_len * sample_size;
if (size > max_size) if (devpriv->ai_buf0 == NULL) {
size = max_size; devpriv->ai_buf0 =
if (size < sample_size) kmalloc(FIFO_SIZE * sizeof(uint16_t), GFP_KERNEL);
size = sample_size; if (devpriv->ai_buf0 == NULL)
return -ENOMEM;
}
return size; if (alloc_subdevices(dev, 4) < 0)
} return -ENOMEM;
/* analog input subdevice */
s = dev->subdevices + 0;
dev->read_subdev = s;
s->type = COMEDI_SUBD_AI;
s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_GROUND | SDF_CMD_READ;
if (thisboard->common)
s->subdev_flags |= SDF_COMMON;
s->n_chan = thisboard->qram_len;
s->len_chanlist = thisboard->qram_len;
s->maxdata = (1 << thisboard->resolution) - 1;
s->range_table = thisboard->range_ai;
s->do_cmd = das1800_ai_do_cmd;
s->do_cmdtest = das1800_ai_do_cmdtest;
s->insn_read = das1800_ai_rinsn;
s->poll = das1800_ai_poll;
s->cancel = das1800_cancel;
/* analog out */
s = dev->subdevices + 1;
if (thisboard->ao_ability == 1) {
s->type = COMEDI_SUBD_AO;
s->subdev_flags = SDF_WRITABLE;
s->n_chan = thisboard->ao_n_chan;
s->maxdata = (1 << thisboard->resolution) - 1;
s->range_table = &range_ao_1;
s->insn_write = das1800_ao_winsn;
} else {
s->type = COMEDI_SUBD_UNUSED;
}
/* di */
s = dev->subdevices + 2;
s->type = COMEDI_SUBD_DI;
s->subdev_flags = SDF_READABLE;
s->n_chan = 4;
s->maxdata = 1;
s->range_table = &range_digital;
s->insn_bits = das1800_di_rbits;
/* do */
s = dev->subdevices + 3;
s->type = COMEDI_SUBD_DO;
s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
s->n_chan = thisboard->do_n_chan;
s->maxdata = 1;
s->range_table = &range_digital;
s->insn_bits = das1800_do_wbits;
das1800_cancel(dev, dev->read_subdev);
/* initialize digital out channels */
outb(devpriv->do_bits, dev->iobase + DAS1800_DIGITAL);
/* initialize analog out channels */
if (thisboard->ao_ability == 1) {
/* select 'update' dac channel for baseAddress + 0x0 */
outb(DAC(thisboard->ao_n_chan - 1),
dev->iobase + DAS1800_SELECT);
outw(devpriv->ao_update_bits, dev->iobase + DAS1800_DAC);
}
return 0;
};
static void das1800_detach(struct comedi_device *dev)
{
if (dev->iobase)
release_region(dev->iobase, DAS1800_SIZE);
if (dev->irq)
free_irq(dev->irq, dev);
if (dev->private) {
if (devpriv->iobase2)
release_region(devpriv->iobase2, DAS1800_SIZE);
if (devpriv->dma0)
free_dma(devpriv->dma0);
if (devpriv->dma1)
free_dma(devpriv->dma1);
kfree(devpriv->ai_buf0);
kfree(devpriv->ai_buf1);
}
};
static struct comedi_driver das1800_driver = { static struct comedi_driver das1800_driver = {
.driver_name = "das1800", .driver_name = "das1800",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment