Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

staging: comedi: ii_pci20kc: request and ioremap memory

The "ii_pci20kc" module is a comedi driver for Intelligent Instruments
PCI-20001C carrier board and modules. Despite the name, this is
actually an ISA board and uses 1K of ISA memory space (below 1M) for the
main board plus up to three modules. The address is set by hardware
jumpers.

When the board is attached to Comedi via the `COMEDI_DEVCONFIG` ioctl
and the driver's legacy "attach" handler, the base address is passed in.
The driver currently uses that address as-is, which is a bad idea. It
doesn't even reserve the memory region.

Fix that by sanity checking the passed in address, reserving the memory
region and ioremapping it.

Replace the current "detach" handler `comedi_legacy_detach()` with a new
handler `ii20k_detach()` which unmaps the memory and releases the
region.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Ian Abbott and committed by
Greg Kroah-Hartman
27fc26f9 96d4b0d5

+29 -3
+29 -3
drivers/staging/comedi/drivers/ii_pci20kc.c
··· 33 33 /* 34 34 * Register I/O map 35 35 */ 36 + #define II20K_SIZE 0x400 36 37 #define II20K_MOD_OFFSET 0x100 37 38 #define II20K_ID_REG 0x00 38 39 #define II20K_ID_MOD1_EMPTY (1 << 7) ··· 440 439 struct comedi_devconfig *it) 441 440 { 442 441 struct comedi_subdevice *s; 442 + unsigned int membase; 443 443 unsigned char id; 444 444 bool has_dio; 445 445 int ret; 446 446 447 - /* FIXME: this doesn't seem right, should 'mmio' be ioremap'ed? */ 448 - dev->mmio = (void __iomem *)(unsigned long)it->options[0]; 447 + membase = it->options[0]; 448 + if (!membase || (membase & ~(0x100000 - II20K_SIZE))) { 449 + dev_warn(dev->class_dev, 450 + "%s: invalid memory address specified\n", 451 + dev->board_name); 452 + return -EINVAL; 453 + } 454 + 455 + if (!request_mem_region(membase, II20K_SIZE, dev->board_name)) { 456 + dev_warn(dev->class_dev, "%s: I/O mem conflict (%#x,%u)\n", 457 + dev->board_name, membase, II20K_SIZE); 458 + return -EIO; 459 + } 460 + dev->iobase = membase; /* actually, a memory address */ 461 + 462 + dev->mmio = ioremap(membase, II20K_SIZE); 463 + if (!dev->mmio) 464 + return -ENOMEM; 449 465 450 466 id = readb(dev->mmio + II20K_ID_REG); 451 467 switch (id & II20K_ID_MASK) { ··· 527 509 return 0; 528 510 } 529 511 512 + static void ii20k_detach(struct comedi_device *dev) 513 + { 514 + if (dev->mmio) 515 + iounmap(dev->mmio); 516 + if (dev->iobase) /* actually, a memory address */ 517 + release_mem_region(dev->iobase, II20K_SIZE); 518 + } 519 + 530 520 static struct comedi_driver ii20k_driver = { 531 521 .driver_name = "ii_pci20kc", 532 522 .module = THIS_MODULE, 533 523 .attach = ii20k_attach, 534 - .detach = comedi_legacy_detach, 524 + .detach = ii20k_detach, 535 525 }; 536 526 module_comedi_driver(ii20k_driver); 537 527