PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/staging/comedi/drivers/amplc_pc263.c

https://bitbucket.org/codefirex/kernel_samsung_jf
C | 489 lines | 315 code | 49 blank | 125 comment | 46 complexity | 89bb0aedddba6c5202f90c2f27280eef MD5 | raw file
  1. /*
  2. comedi/drivers/amplc_pc263.c
  3. Driver for Amplicon PC263 and PCI263 relay boards.
  4. Copyright (C) 2002 MEV Ltd. <http://www.mev.co.uk/>
  5. COMEDI - Linux Control and Measurement Device Interface
  6. Copyright (C) 2000 David A. Schleef <ds@schleef.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /*
  20. Driver: amplc_pc263
  21. Description: Amplicon PC263, PCI263
  22. Author: Ian Abbott <abbotti@mev.co.uk>
  23. Devices: [Amplicon] PC263 (pc263), PCI263 (pci263 or amplc_pc263)
  24. Updated: Wed, 22 Oct 2008 14:10:53 +0100
  25. Status: works
  26. Configuration options - PC263:
  27. [0] - I/O port base address
  28. Configuration options - PCI263:
  29. [0] - PCI bus of device (optional)
  30. [1] - PCI slot of device (optional)
  31. If bus/slot is not specified, the first available PCI device will be
  32. used.
  33. Each board appears as one subdevice, with 16 digital outputs, each
  34. connected to a reed-relay. Relay contacts are closed when output is 1.
  35. The state of the outputs can be read.
  36. */
  37. #include "../comedidev.h"
  38. #include "comedi_pci.h"
  39. #define PC263_DRIVER_NAME "amplc_pc263"
  40. /* PCI263 PCI configuration register information */
  41. #define PCI_VENDOR_ID_AMPLICON 0x14dc
  42. #define PCI_DEVICE_ID_AMPLICON_PCI263 0x000c
  43. #define PCI_DEVICE_ID_INVALID 0xffff
  44. /* PC263 / PCI263 registers */
  45. #define PC263_IO_SIZE 2
  46. /*
  47. * Board descriptions for Amplicon PC263 / PCI263.
  48. */
  49. enum pc263_bustype { isa_bustype, pci_bustype };
  50. enum pc263_model { pc263_model, pci263_model, anypci_model };
  51. struct pc263_board {
  52. const char *name;
  53. const char *fancy_name;
  54. unsigned short devid;
  55. enum pc263_bustype bustype;
  56. enum pc263_model model;
  57. };
  58. static const struct pc263_board pc263_boards[] = {
  59. {
  60. .name = "pc263",
  61. .fancy_name = "PC263",
  62. .bustype = isa_bustype,
  63. .model = pc263_model,
  64. },
  65. #ifdef CONFIG_COMEDI_PCI
  66. {
  67. .name = "pci263",
  68. .fancy_name = "PCI263",
  69. .devid = PCI_DEVICE_ID_AMPLICON_PCI263,
  70. .bustype = pci_bustype,
  71. .model = pci263_model,
  72. },
  73. #endif
  74. #ifdef CONFIG_COMEDI_PCI
  75. {
  76. .name = PC263_DRIVER_NAME,
  77. .fancy_name = PC263_DRIVER_NAME,
  78. .devid = PCI_DEVICE_ID_INVALID,
  79. .bustype = pci_bustype,
  80. .model = anypci_model, /* wildcard */
  81. },
  82. #endif
  83. };
  84. #ifdef CONFIG_COMEDI_PCI
  85. static DEFINE_PCI_DEVICE_TABLE(pc263_pci_table) = {
  86. { PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI263) },
  87. {0}
  88. };
  89. MODULE_DEVICE_TABLE(pci, pc263_pci_table);
  90. #endif /* CONFIG_COMEDI_PCI */
  91. /*
  92. * Useful for shorthand access to the particular board structure
  93. */
  94. #define thisboard ((const struct pc263_board *)dev->board_ptr)
  95. /* this structure is for data unique to this hardware driver. If
  96. several hardware drivers keep similar information in this structure,
  97. feel free to suggest moving the variable to the struct comedi_device struct.
  98. */
  99. #ifdef CONFIG_COMEDI_PCI
  100. struct pc263_private {
  101. /* PCI device. */
  102. struct pci_dev *pci_dev;
  103. };
  104. #define devpriv ((struct pc263_private *)dev->private)
  105. #endif /* CONFIG_COMEDI_PCI */
  106. /*
  107. * The struct comedi_driver structure tells the Comedi core module
  108. * which functions to call to configure/deconfigure (attach/detach)
  109. * the board, and also about the kernel module that contains
  110. * the device code.
  111. */
  112. static int pc263_attach(struct comedi_device *dev, struct comedi_devconfig *it);
  113. static int pc263_detach(struct comedi_device *dev);
  114. static struct comedi_driver driver_amplc_pc263 = {
  115. .driver_name = PC263_DRIVER_NAME,
  116. .module = THIS_MODULE,
  117. .attach = pc263_attach,
  118. .detach = pc263_detach,
  119. .board_name = &pc263_boards[0].name,
  120. .offset = sizeof(struct pc263_board),
  121. .num_names = ARRAY_SIZE(pc263_boards),
  122. };
  123. static int pc263_request_region(unsigned minor, unsigned long from,
  124. unsigned long extent);
  125. static int pc263_dio_insn_bits(struct comedi_device *dev,
  126. struct comedi_subdevice *s,
  127. struct comedi_insn *insn, unsigned int *data);
  128. static int pc263_dio_insn_config(struct comedi_device *dev,
  129. struct comedi_subdevice *s,
  130. struct comedi_insn *insn, unsigned int *data);
  131. /*
  132. * This function looks for a PCI device matching the requested board name,
  133. * bus and slot.
  134. */
  135. #ifdef CONFIG_COMEDI_PCI
  136. static int
  137. pc263_find_pci(struct comedi_device *dev, int bus, int slot,
  138. struct pci_dev **pci_dev_p)
  139. {
  140. struct pci_dev *pci_dev = NULL;
  141. *pci_dev_p = NULL;
  142. /* Look for matching PCI device. */
  143. for (pci_dev = pci_get_device(PCI_VENDOR_ID_AMPLICON, PCI_ANY_ID, NULL);
  144. pci_dev != NULL;
  145. pci_dev = pci_get_device(PCI_VENDOR_ID_AMPLICON,
  146. PCI_ANY_ID, pci_dev)) {
  147. /* If bus/slot specified, check them. */
  148. if (bus || slot) {
  149. if (bus != pci_dev->bus->number
  150. || slot != PCI_SLOT(pci_dev->devfn))
  151. continue;
  152. }
  153. if (thisboard->model == anypci_model) {
  154. /* Match any supported model. */
  155. int i;
  156. for (i = 0; i < ARRAY_SIZE(pc263_boards); i++) {
  157. if (pc263_boards[i].bustype != pci_bustype)
  158. continue;
  159. if (pci_dev->device == pc263_boards[i].devid) {
  160. /* Change board_ptr to matched board. */
  161. dev->board_ptr = &pc263_boards[i];
  162. break;
  163. }
  164. }
  165. if (i == ARRAY_SIZE(pc263_boards))
  166. continue;
  167. } else {
  168. /* Match specific model name. */
  169. if (pci_dev->device != thisboard->devid)
  170. continue;
  171. }
  172. /* Found a match. */
  173. *pci_dev_p = pci_dev;
  174. return 0;
  175. }
  176. /* No match found. */
  177. if (bus || slot) {
  178. printk(KERN_ERR
  179. "comedi%d: error! no %s found at pci %02x:%02x!\n",
  180. dev->minor, thisboard->name, bus, slot);
  181. } else {
  182. printk(KERN_ERR "comedi%d: error! no %s found!\n",
  183. dev->minor, thisboard->name);
  184. }
  185. return -EIO;
  186. }
  187. #endif
  188. /*
  189. * Attach is called by the Comedi core to configure the driver
  190. * for a particular board. If you specified a board_name array
  191. * in the driver structure, dev->board_ptr contains that
  192. * address.
  193. */
  194. static int pc263_attach(struct comedi_device *dev, struct comedi_devconfig *it)
  195. {
  196. struct comedi_subdevice *s;
  197. unsigned long iobase = 0;
  198. #ifdef CONFIG_COMEDI_PCI
  199. struct pci_dev *pci_dev = NULL;
  200. int bus = 0, slot = 0;
  201. #endif
  202. int ret;
  203. printk(KERN_DEBUG "comedi%d: %s: attach\n", dev->minor,
  204. PC263_DRIVER_NAME);
  205. /*
  206. * Allocate the private structure area. alloc_private() is a
  207. * convenient macro defined in comedidev.h.
  208. */
  209. #ifdef CONFIG_COMEDI_PCI
  210. ret = alloc_private(dev, sizeof(struct pc263_private));
  211. if (ret < 0) {
  212. printk(KERN_ERR "comedi%d: error! out of memory!\n",
  213. dev->minor);
  214. return ret;
  215. }
  216. #endif
  217. /* Process options. */
  218. switch (thisboard->bustype) {
  219. case isa_bustype:
  220. iobase = it->options[0];
  221. break;
  222. #ifdef CONFIG_COMEDI_PCI
  223. case pci_bustype:
  224. bus = it->options[0];
  225. slot = it->options[1];
  226. ret = pc263_find_pci(dev, bus, slot, &pci_dev);
  227. if (ret < 0)
  228. return ret;
  229. devpriv->pci_dev = pci_dev;
  230. break;
  231. #endif /* CONFIG_COMEDI_PCI */
  232. default:
  233. printk(KERN_ERR
  234. "comedi%d: %s: BUG! cannot determine board type!\n",
  235. dev->minor, PC263_DRIVER_NAME);
  236. return -EINVAL;
  237. break;
  238. }
  239. /*
  240. * Initialize dev->board_name.
  241. */
  242. dev->board_name = thisboard->name;
  243. /* Enable device and reserve I/O spaces. */
  244. #ifdef CONFIG_COMEDI_PCI
  245. if (pci_dev) {
  246. ret = comedi_pci_enable(pci_dev, PC263_DRIVER_NAME);
  247. if (ret < 0) {
  248. printk(KERN_ERR
  249. "comedi%d: error! cannot enable PCI device and "
  250. "request regions!\n",
  251. dev->minor);
  252. return ret;
  253. }
  254. iobase = pci_resource_start(pci_dev, 2);
  255. } else
  256. #endif
  257. {
  258. ret = pc263_request_region(dev->minor, iobase, PC263_IO_SIZE);
  259. if (ret < 0)
  260. return ret;
  261. }
  262. dev->iobase = iobase;
  263. /*
  264. * Allocate the subdevice structures. alloc_subdevice() is a
  265. * convenient macro defined in comedidev.h.
  266. */
  267. ret = alloc_subdevices(dev, 1);
  268. if (ret < 0) {
  269. printk(KERN_ERR "comedi%d: error! out of memory!\n",
  270. dev->minor);
  271. return ret;
  272. }
  273. s = dev->subdevices + 0;
  274. /* digital i/o subdevice */
  275. s->type = COMEDI_SUBD_DIO;
  276. s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
  277. s->n_chan = 16;
  278. s->maxdata = 1;
  279. s->range_table = &range_digital;
  280. s->insn_bits = pc263_dio_insn_bits;
  281. s->insn_config = pc263_dio_insn_config;
  282. /* all outputs */
  283. s->io_bits = 0xffff;
  284. /* read initial relay state */
  285. s->state = inb(dev->iobase);
  286. s->state = s->state | (inb(dev->iobase) << 8);
  287. printk(KERN_INFO "comedi%d: %s ", dev->minor, dev->board_name);
  288. if (thisboard->bustype == isa_bustype) {
  289. printk("(base %#lx) ", iobase);
  290. } else {
  291. #ifdef CONFIG_COMEDI_PCI
  292. printk("(pci %s) ", pci_name(pci_dev));
  293. #endif
  294. }
  295. printk("attached\n");
  296. return 1;
  297. }
  298. /*
  299. * _detach is called to deconfigure a device. It should deallocate
  300. * resources.
  301. * This function is also called when _attach() fails, so it should be
  302. * careful not to release resources that were not necessarily
  303. * allocated by _attach(). dev->private and dev->subdevices are
  304. * deallocated automatically by the core.
  305. */
  306. static int pc263_detach(struct comedi_device *dev)
  307. {
  308. printk(KERN_DEBUG "comedi%d: %s: detach\n", dev->minor,
  309. PC263_DRIVER_NAME);
  310. #ifdef CONFIG_COMEDI_PCI
  311. if (devpriv) {
  312. #endif
  313. #ifdef CONFIG_COMEDI_PCI
  314. if (devpriv->pci_dev) {
  315. if (dev->iobase)
  316. comedi_pci_disable(devpriv->pci_dev);
  317. pci_dev_put(devpriv->pci_dev);
  318. } else
  319. #endif
  320. {
  321. if (dev->iobase)
  322. release_region(dev->iobase, PC263_IO_SIZE);
  323. }
  324. }
  325. if (dev->board_name) {
  326. printk(KERN_INFO "comedi%d: %s removed\n",
  327. dev->minor, dev->board_name);
  328. }
  329. return 0;
  330. }
  331. /*
  332. * This function checks and requests an I/O region, reporting an error
  333. * if there is a conflict.
  334. */
  335. static int pc263_request_region(unsigned minor, unsigned long from,
  336. unsigned long extent)
  337. {
  338. if (!from || !request_region(from, extent, PC263_DRIVER_NAME)) {
  339. printk(KERN_ERR "comedi%d: I/O port conflict (%#lx,%lu)!\n",
  340. minor, from, extent);
  341. return -EIO;
  342. }
  343. return 0;
  344. }
  345. /* DIO devices are slightly special. Although it is possible to
  346. * implement the insn_read/insn_write interface, it is much more
  347. * useful to applications if you implement the insn_bits interface.
  348. * This allows packed reading/writing of the DIO channels. The
  349. * comedi core can convert between insn_bits and insn_read/write */
  350. static int pc263_dio_insn_bits(struct comedi_device *dev,
  351. struct comedi_subdevice *s,
  352. struct comedi_insn *insn, unsigned int *data)
  353. {
  354. if (insn->n != 2)
  355. return -EINVAL;
  356. /* The insn data is a mask in data[0] and the new data
  357. * in data[1], each channel cooresponding to a bit. */
  358. if (data[0]) {
  359. s->state &= ~data[0];
  360. s->state |= data[0] & data[1];
  361. /* Write out the new digital output lines */
  362. outb(s->state & 0xFF, dev->iobase);
  363. outb(s->state >> 8, dev->iobase + 1);
  364. }
  365. /* on return, data[1] contains the value of the digital
  366. * input and output lines. */
  367. /* or we could just return the software copy of the output values if
  368. * it was a purely digital output subdevice */
  369. data[1] = s->state;
  370. return 2;
  371. }
  372. static int pc263_dio_insn_config(struct comedi_device *dev,
  373. struct comedi_subdevice *s,
  374. struct comedi_insn *insn, unsigned int *data)
  375. {
  376. if (insn->n != 1)
  377. return -EINVAL;
  378. return 1;
  379. }
  380. /*
  381. * A convenient macro that defines init_module() and cleanup_module(),
  382. * as necessary.
  383. */
  384. #ifdef CONFIG_COMEDI_PCI
  385. static int __devinit driver_amplc_pc263_pci_probe(struct pci_dev *dev,
  386. const struct pci_device_id
  387. *ent)
  388. {
  389. return comedi_pci_auto_config(dev, driver_amplc_pc263.driver_name);
  390. }
  391. static void __devexit driver_amplc_pc263_pci_remove(struct pci_dev *dev)
  392. {
  393. comedi_pci_auto_unconfig(dev);
  394. }
  395. static struct pci_driver driver_amplc_pc263_pci_driver = {
  396. .id_table = pc263_pci_table,
  397. .probe = &driver_amplc_pc263_pci_probe,
  398. .remove = __devexit_p(&driver_amplc_pc263_pci_remove)
  399. };
  400. static int __init driver_amplc_pc263_init_module(void)
  401. {
  402. int retval;
  403. retval = comedi_driver_register(&driver_amplc_pc263);
  404. if (retval < 0)
  405. return retval;
  406. driver_amplc_pc263_pci_driver.name =
  407. (char *)driver_amplc_pc263.driver_name;
  408. return pci_register_driver(&driver_amplc_pc263_pci_driver);
  409. }
  410. static void __exit driver_amplc_pc263_cleanup_module(void)
  411. {
  412. pci_unregister_driver(&driver_amplc_pc263_pci_driver);
  413. comedi_driver_unregister(&driver_amplc_pc263);
  414. }
  415. module_init(driver_amplc_pc263_init_module);
  416. module_exit(driver_amplc_pc263_cleanup_module);
  417. #else
  418. static int __init driver_amplc_pc263_init_module(void)
  419. {
  420. return comedi_driver_register(&driver_amplc_pc263);
  421. }
  422. static void __exit driver_amplc_pc263_cleanup_module(void)
  423. {
  424. comedi_driver_unregister(&driver_amplc_pc263);
  425. }
  426. module_init(driver_amplc_pc263_init_module);
  427. module_exit(driver_amplc_pc263_cleanup_module);
  428. #endif
  429. MODULE_AUTHOR("Comedi http://www.comedi.org");
  430. MODULE_DESCRIPTION("Comedi low-level driver");
  431. MODULE_LICENSE("GPL");