PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/staging/comedi/drivers/das1800.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 1813 lines | 1381 code | 169 blank | 263 comment | 252 complexity | 7b708fd31a89e15aae801d2d9de66e7f MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /*
  2. comedi/drivers/das1800.c
  3. Driver for Keitley das1700/das1800 series boards
  4. Copyright (C) 2000 Frank Mori Hess <fmhess@users.sourceforge.net>
  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. /*
  21. Driver: das1800
  22. Description: Keithley Metrabyte DAS1800 (& compatibles)
  23. Author: Frank Mori Hess <fmhess@users.sourceforge.net>
  24. Devices: [Keithley Metrabyte] DAS-1701ST (das-1701st),
  25. DAS-1701ST-DA (das-1701st-da), DAS-1701/AO (das-1701ao),
  26. DAS-1702ST (das-1702st), DAS-1702ST-DA (das-1702st-da),
  27. DAS-1702HR (das-1702hr), DAS-1702HR-DA (das-1702hr-da),
  28. DAS-1702/AO (das-1702ao), DAS-1801ST (das-1801st),
  29. DAS-1801ST-DA (das-1801st-da), DAS-1801HC (das-1801hc),
  30. DAS-1801AO (das-1801ao), DAS-1802ST (das-1802st),
  31. DAS-1802ST-DA (das-1802st-da), DAS-1802HR (das-1802hr),
  32. DAS-1802HR-DA (das-1802hr-da), DAS-1802HC (das-1802hc),
  33. DAS-1802AO (das-1802ao)
  34. Status: works
  35. The waveform analog output on the 'ao' cards is not supported.
  36. If you need it, send me (Frank Hess) an email.
  37. Configuration options:
  38. [0] - I/O port base address
  39. [1] - IRQ (optional, required for timed or externally triggered conversions)
  40. [2] - DMA0 (optional, requires irq)
  41. [3] - DMA1 (optional, requires irq and dma0)
  42. */
  43. /*
  44. This driver supports the following Keithley boards:
  45. das-1701st
  46. das-1701st-da
  47. das-1701ao
  48. das-1702st
  49. das-1702st-da
  50. das-1702hr
  51. das-1702hr-da
  52. das-1702ao
  53. das-1801st
  54. das-1801st-da
  55. das-1801hc
  56. das-1801ao
  57. das-1802st
  58. das-1802st-da
  59. das-1802hr
  60. das-1802hr-da
  61. das-1802hc
  62. das-1802ao
  63. Options:
  64. [0] - base io address
  65. [1] - irq (optional, required for timed or externally triggered conversions)
  66. [2] - dma0 (optional, requires irq)
  67. [3] - dma1 (optional, requires irq and dma0)
  68. irq can be omitted, although the cmd interface will not work without it.
  69. analog input cmd triggers supported:
  70. start_src: TRIG_NOW | TRIG_EXT
  71. scan_begin_src: TRIG_FOLLOW | TRIG_TIMER | TRIG_EXT
  72. scan_end_src: TRIG_COUNT
  73. convert_src: TRIG_TIMER | TRIG_EXT (TRIG_EXT requires scan_begin_src == TRIG_FOLLOW)
  74. stop_src: TRIG_COUNT | TRIG_EXT | TRIG_NONE
  75. scan_begin_src triggers TRIG_TIMER and TRIG_EXT use the card's
  76. 'burst mode' which limits the valid conversion time to 64 microseconds
  77. (convert_arg <= 64000). This limitation does not apply if scan_begin_src
  78. is TRIG_FOLLOW.
  79. NOTES:
  80. Only the DAS-1801ST has been tested by me.
  81. Unipolar and bipolar ranges cannot be mixed in the channel/gain list.
  82. TODO:
  83. Make it automatically allocate irq and dma channels if they are not specified
  84. Add support for analog out on 'ao' cards
  85. read insn for analog out
  86. */
  87. #include <linux/interrupt.h>
  88. #include <linux/slab.h>
  89. #include <linux/io.h>
  90. #include "../comedidev.h"
  91. #include <linux/ioport.h>
  92. #include <asm/dma.h>
  93. #include "8253.h"
  94. #include "comedi_fc.h"
  95. /* misc. defines */
  96. #define DAS1800_SIZE 16 /* uses 16 io addresses */
  97. #define FIFO_SIZE 1024 /* 1024 sample fifo */
  98. #define TIMER_BASE 200 /* 5 Mhz master clock */
  99. #define UNIPOLAR 0x4 /* bit that determines whether input range is uni/bipolar */
  100. #define DMA_BUF_SIZE 0x1ff00 /* size in bytes of dma buffers */
  101. /* Registers for the das1800 */
  102. #define DAS1800_FIFO 0x0
  103. #define DAS1800_QRAM 0x0
  104. #define DAS1800_DAC 0x0
  105. #define DAS1800_SELECT 0x2
  106. #define ADC 0x0
  107. #define QRAM 0x1
  108. #define DAC(a) (0x2 + a)
  109. #define DAS1800_DIGITAL 0x3
  110. #define DAS1800_CONTROL_A 0x4
  111. #define FFEN 0x1
  112. #define CGEN 0x4
  113. #define CGSL 0x8
  114. #define TGEN 0x10
  115. #define TGSL 0x20
  116. #define ATEN 0x80
  117. #define DAS1800_CONTROL_B 0x5
  118. #define DMA_CH5 0x1
  119. #define DMA_CH6 0x2
  120. #define DMA_CH7 0x3
  121. #define DMA_CH5_CH6 0x5
  122. #define DMA_CH6_CH7 0x6
  123. #define DMA_CH7_CH5 0x7
  124. #define DMA_ENABLED 0x3 /* mask used to determine if dma is enabled */
  125. #define DMA_DUAL 0x4
  126. #define IRQ3 0x8
  127. #define IRQ5 0x10
  128. #define IRQ7 0x18
  129. #define IRQ10 0x28
  130. #define IRQ11 0x30
  131. #define IRQ15 0x38
  132. #define FIMD 0x40
  133. #define DAS1800_CONTROL_C 0X6
  134. #define IPCLK 0x1
  135. #define XPCLK 0x3
  136. #define BMDE 0x4
  137. #define CMEN 0x8
  138. #define UQEN 0x10
  139. #define SD 0x40
  140. #define UB 0x80
  141. #define DAS1800_STATUS 0x7
  142. /* bits that prevent interrupt status bits (and CVEN) from being cleared on write */
  143. #define CLEAR_INTR_MASK (CVEN_MASK | 0x1f)
  144. #define INT 0x1
  145. #define DMATC 0x2
  146. #define CT0TC 0x8
  147. #define OVF 0x10
  148. #define FHF 0x20
  149. #define FNE 0x40
  150. #define CVEN_MASK 0x40 /* masks CVEN on write */
  151. #define CVEN 0x80
  152. #define DAS1800_BURST_LENGTH 0x8
  153. #define DAS1800_BURST_RATE 0x9
  154. #define DAS1800_QRAM_ADDRESS 0xa
  155. #define DAS1800_COUNTER 0xc
  156. #define IOBASE2 0x400 /* offset of additional ioports used on 'ao' cards */
  157. enum {
  158. das1701st, das1701st_da, das1702st, das1702st_da, das1702hr,
  159. das1702hr_da,
  160. das1701ao, das1702ao, das1801st, das1801st_da, das1802st, das1802st_da,
  161. das1802hr, das1802hr_da, das1801hc, das1802hc, das1801ao, das1802ao
  162. };
  163. static int das1800_attach(struct comedi_device *dev,
  164. struct comedi_devconfig *it);
  165. static int das1800_detach(struct comedi_device *dev);
  166. static int das1800_probe(struct comedi_device *dev);
  167. static int das1800_cancel(struct comedi_device *dev,
  168. struct comedi_subdevice *s);
  169. static irqreturn_t das1800_interrupt(int irq, void *d);
  170. static int das1800_ai_poll(struct comedi_device *dev,
  171. struct comedi_subdevice *s);
  172. static void das1800_ai_handler(struct comedi_device *dev);
  173. static void das1800_handle_dma(struct comedi_device *dev,
  174. struct comedi_subdevice *s, unsigned int status);
  175. static void das1800_flush_dma(struct comedi_device *dev,
  176. struct comedi_subdevice *s);
  177. static void das1800_flush_dma_channel(struct comedi_device *dev,
  178. struct comedi_subdevice *s,
  179. unsigned int channel, uint16_t *buffer);
  180. static void das1800_handle_fifo_half_full(struct comedi_device *dev,
  181. struct comedi_subdevice *s);
  182. static void das1800_handle_fifo_not_empty(struct comedi_device *dev,
  183. struct comedi_subdevice *s);
  184. static int das1800_ai_do_cmdtest(struct comedi_device *dev,
  185. struct comedi_subdevice *s,
  186. struct comedi_cmd *cmd);
  187. static int das1800_ai_do_cmd(struct comedi_device *dev,
  188. struct comedi_subdevice *s);
  189. static int das1800_ai_rinsn(struct comedi_device *dev,
  190. struct comedi_subdevice *s,
  191. struct comedi_insn *insn, unsigned int *data);
  192. static int das1800_ao_winsn(struct comedi_device *dev,
  193. struct comedi_subdevice *s,
  194. struct comedi_insn *insn, unsigned int *data);
  195. static int das1800_di_rbits(struct comedi_device *dev,
  196. struct comedi_subdevice *s,
  197. struct comedi_insn *insn, unsigned int *data);
  198. static int das1800_do_wbits(struct comedi_device *dev,
  199. struct comedi_subdevice *s,
  200. struct comedi_insn *insn, unsigned int *data);
  201. static int das1800_set_frequency(struct comedi_device *dev);
  202. static unsigned int burst_convert_arg(unsigned int convert_arg, int round_mode);
  203. static unsigned int suggest_transfer_size(struct comedi_cmd *cmd);
  204. /* analog input ranges */
  205. static const struct comedi_lrange range_ai_das1801 = {
  206. 8,
  207. {
  208. RANGE(-5, 5),
  209. RANGE(-1, 1),
  210. RANGE(-0.1, 0.1),
  211. RANGE(-0.02, 0.02),
  212. RANGE(0, 5),
  213. RANGE(0, 1),
  214. RANGE(0, 0.1),
  215. RANGE(0, 0.02),
  216. }
  217. };
  218. static const struct comedi_lrange range_ai_das1802 = {
  219. 8,
  220. {
  221. RANGE(-10, 10),
  222. RANGE(-5, 5),
  223. RANGE(-2.5, 2.5),
  224. RANGE(-1.25, 1.25),
  225. RANGE(0, 10),
  226. RANGE(0, 5),
  227. RANGE(0, 2.5),
  228. RANGE(0, 1.25),
  229. }
  230. };
  231. struct das1800_board {
  232. const char *name;
  233. int ai_speed; /* max conversion period in nanoseconds */
  234. int resolution; /* bits of ai resolution */
  235. int qram_len; /* length of card's channel / gain queue */
  236. int common; /* supports AREF_COMMON flag */
  237. int do_n_chan; /* number of digital output channels */
  238. int ao_ability; /* 0 == no analog out, 1 == basic analog out, 2 == waveform analog out */
  239. int ao_n_chan; /* number of analog out channels */
  240. const struct comedi_lrange *range_ai; /* available input ranges */
  241. };
  242. /* Warning: the maximum conversion speeds listed below are
  243. * not always achievable depending on board setup (see
  244. * user manual.)
  245. */
  246. static const struct das1800_board das1800_boards[] = {
  247. {
  248. .name = "das-1701st",
  249. .ai_speed = 6250,
  250. .resolution = 12,
  251. .qram_len = 256,
  252. .common = 1,
  253. .do_n_chan = 4,
  254. .ao_ability = 0,
  255. .ao_n_chan = 0,
  256. .range_ai = &range_ai_das1801,
  257. },
  258. {
  259. .name = "das-1701st-da",
  260. .ai_speed = 6250,
  261. .resolution = 12,
  262. .qram_len = 256,
  263. .common = 1,
  264. .do_n_chan = 4,
  265. .ao_ability = 1,
  266. .ao_n_chan = 4,
  267. .range_ai = &range_ai_das1801,
  268. },
  269. {
  270. .name = "das-1702st",
  271. .ai_speed = 6250,
  272. .resolution = 12,
  273. .qram_len = 256,
  274. .common = 1,
  275. .do_n_chan = 4,
  276. .ao_ability = 0,
  277. .ao_n_chan = 0,
  278. .range_ai = &range_ai_das1802,
  279. },
  280. {
  281. .name = "das-1702st-da",
  282. .ai_speed = 6250,
  283. .resolution = 12,
  284. .qram_len = 256,
  285. .common = 1,
  286. .do_n_chan = 4,
  287. .ao_ability = 1,
  288. .ao_n_chan = 4,
  289. .range_ai = &range_ai_das1802,
  290. },
  291. {
  292. .name = "das-1702hr",
  293. .ai_speed = 20000,
  294. .resolution = 16,
  295. .qram_len = 256,
  296. .common = 1,
  297. .do_n_chan = 4,
  298. .ao_ability = 0,
  299. .ao_n_chan = 0,
  300. .range_ai = &range_ai_das1802,
  301. },
  302. {
  303. .name = "das-1702hr-da",
  304. .ai_speed = 20000,
  305. .resolution = 16,
  306. .qram_len = 256,
  307. .common = 1,
  308. .do_n_chan = 4,
  309. .ao_ability = 1,
  310. .ao_n_chan = 2,
  311. .range_ai = &range_ai_das1802,
  312. },
  313. {
  314. .name = "das-1701ao",
  315. .ai_speed = 6250,
  316. .resolution = 12,
  317. .qram_len = 256,
  318. .common = 1,
  319. .do_n_chan = 4,
  320. .ao_ability = 2,
  321. .ao_n_chan = 2,
  322. .range_ai = &range_ai_das1801,
  323. },
  324. {
  325. .name = "das-1702ao",
  326. .ai_speed = 6250,
  327. .resolution = 12,
  328. .qram_len = 256,
  329. .common = 1,
  330. .do_n_chan = 4,
  331. .ao_ability = 2,
  332. .ao_n_chan = 2,
  333. .range_ai = &range_ai_das1802,
  334. },
  335. {
  336. .name = "das-1801st",
  337. .ai_speed = 3000,
  338. .resolution = 12,
  339. .qram_len = 256,
  340. .common = 1,
  341. .do_n_chan = 4,
  342. .ao_ability = 0,
  343. .ao_n_chan = 0,
  344. .range_ai = &range_ai_das1801,
  345. },
  346. {
  347. .name = "das-1801st-da",
  348. .ai_speed = 3000,
  349. .resolution = 12,
  350. .qram_len = 256,
  351. .common = 1,
  352. .do_n_chan = 4,
  353. .ao_ability = 0,
  354. .ao_n_chan = 4,
  355. .range_ai = &range_ai_das1801,
  356. },
  357. {
  358. .name = "das-1802st",
  359. .ai_speed = 3000,
  360. .resolution = 12,
  361. .qram_len = 256,
  362. .common = 1,
  363. .do_n_chan = 4,
  364. .ao_ability = 0,
  365. .ao_n_chan = 0,
  366. .range_ai = &range_ai_das1802,
  367. },
  368. {
  369. .name = "das-1802st-da",
  370. .ai_speed = 3000,
  371. .resolution = 12,
  372. .qram_len = 256,
  373. .common = 1,
  374. .do_n_chan = 4,
  375. .ao_ability = 1,
  376. .ao_n_chan = 4,
  377. .range_ai = &range_ai_das1802,
  378. },
  379. {
  380. .name = "das-1802hr",
  381. .ai_speed = 10000,
  382. .resolution = 16,
  383. .qram_len = 256,
  384. .common = 1,
  385. .do_n_chan = 4,
  386. .ao_ability = 0,
  387. .ao_n_chan = 0,
  388. .range_ai = &range_ai_das1802,
  389. },
  390. {
  391. .name = "das-1802hr-da",
  392. .ai_speed = 10000,
  393. .resolution = 16,
  394. .qram_len = 256,
  395. .common = 1,
  396. .do_n_chan = 4,
  397. .ao_ability = 1,
  398. .ao_n_chan = 2,
  399. .range_ai = &range_ai_das1802,
  400. },
  401. {
  402. .name = "das-1801hc",
  403. .ai_speed = 3000,
  404. .resolution = 12,
  405. .qram_len = 64,
  406. .common = 0,
  407. .do_n_chan = 8,
  408. .ao_ability = 1,
  409. .ao_n_chan = 2,
  410. .range_ai = &range_ai_das1801,
  411. },
  412. {
  413. .name = "das-1802hc",
  414. .ai_speed = 3000,
  415. .resolution = 12,
  416. .qram_len = 64,
  417. .common = 0,
  418. .do_n_chan = 8,
  419. .ao_ability = 1,
  420. .ao_n_chan = 2,
  421. .range_ai = &range_ai_das1802,
  422. },
  423. {
  424. .name = "das-1801ao",
  425. .ai_speed = 3000,
  426. .resolution = 12,
  427. .qram_len = 256,
  428. .common = 1,
  429. .do_n_chan = 4,
  430. .ao_ability = 2,
  431. .ao_n_chan = 2,
  432. .range_ai = &range_ai_das1801,
  433. },
  434. {
  435. .name = "das-1802ao",
  436. .ai_speed = 3000,
  437. .resolution = 12,
  438. .qram_len = 256,
  439. .common = 1,
  440. .do_n_chan = 4,
  441. .ao_ability = 2,
  442. .ao_n_chan = 2,
  443. .range_ai = &range_ai_das1802,
  444. },
  445. };
  446. /*
  447. * Useful for shorthand access to the particular board structure
  448. */
  449. #define thisboard ((const struct das1800_board *)dev->board_ptr)
  450. struct das1800_private {
  451. volatile unsigned int count; /* number of data points left to be taken */
  452. unsigned int divisor1; /* value to load into board's counter 1 for timed conversions */
  453. unsigned int divisor2; /* value to load into board's counter 2 for timed conversions */
  454. int do_bits; /* digital output bits */
  455. int irq_dma_bits; /* bits for control register b */
  456. /* dma bits for control register b, stored so that dma can be
  457. * turned on and off */
  458. int dma_bits;
  459. unsigned int dma0; /* dma channels used */
  460. unsigned int dma1;
  461. volatile unsigned int dma_current; /* dma channel currently in use */
  462. uint16_t *ai_buf0; /* pointers to dma buffers */
  463. uint16_t *ai_buf1;
  464. uint16_t *dma_current_buf; /* pointer to dma buffer currently being used */
  465. unsigned int dma_transfer_size; /* size of transfer currently used, in bytes */
  466. unsigned long iobase2; /* secondary io address used for analog out on 'ao' boards */
  467. short ao_update_bits; /* remembers the last write to the 'update' dac */
  468. };
  469. #define devpriv ((struct das1800_private *)dev->private)
  470. /* analog out range for boards with basic analog out */
  471. static const struct comedi_lrange range_ao_1 = {
  472. 1,
  473. {
  474. RANGE(-10, 10),
  475. }
  476. };
  477. /* analog out range for 'ao' boards */
  478. /*
  479. static const struct comedi_lrange range_ao_2 = {
  480. 2,
  481. {
  482. RANGE(-10, 10),
  483. RANGE(-5, 5),
  484. }
  485. };
  486. */
  487. static struct comedi_driver driver_das1800 = {
  488. .driver_name = "das1800",
  489. .module = THIS_MODULE,
  490. .attach = das1800_attach,
  491. .detach = das1800_detach,
  492. .num_names = ARRAY_SIZE(das1800_boards),
  493. .board_name = &das1800_boards[0].name,
  494. .offset = sizeof(struct das1800_board),
  495. };
  496. /*
  497. * A convenient macro that defines init_module() and cleanup_module(),
  498. * as necessary.
  499. */
  500. static int __init driver_das1800_init_module(void)
  501. {
  502. return comedi_driver_register(&driver_das1800);
  503. }
  504. static void __exit driver_das1800_cleanup_module(void)
  505. {
  506. comedi_driver_unregister(&driver_das1800);
  507. }
  508. module_init(driver_das1800_init_module);
  509. module_exit(driver_das1800_cleanup_module);
  510. static int das1800_init_dma(struct comedi_device *dev, unsigned int dma0,
  511. unsigned int dma1)
  512. {
  513. unsigned long flags;
  514. /* need an irq to do dma */
  515. if (dev->irq && dma0) {
  516. /* encode dma0 and dma1 into 2 digit hexadecimal for switch */
  517. switch ((dma0 & 0x7) | (dma1 << 4)) {
  518. case 0x5: /* dma0 == 5 */
  519. devpriv->dma_bits |= DMA_CH5;
  520. break;
  521. case 0x6: /* dma0 == 6 */
  522. devpriv->dma_bits |= DMA_CH6;
  523. break;
  524. case 0x7: /* dma0 == 7 */
  525. devpriv->dma_bits |= DMA_CH7;
  526. break;
  527. case 0x65: /* dma0 == 5, dma1 == 6 */
  528. devpriv->dma_bits |= DMA_CH5_CH6;
  529. break;
  530. case 0x76: /* dma0 == 6, dma1 == 7 */
  531. devpriv->dma_bits |= DMA_CH6_CH7;
  532. break;
  533. case 0x57: /* dma0 == 7, dma1 == 5 */
  534. devpriv->dma_bits |= DMA_CH7_CH5;
  535. break;
  536. default:
  537. printk(" only supports dma channels 5 through 7\n"
  538. " Dual dma only allows the following combinations:\n"
  539. " dma 5,6 / 6,7 / or 7,5\n");
  540. return -EINVAL;
  541. break;
  542. }
  543. if (request_dma(dma0, driver_das1800.driver_name)) {
  544. printk(" failed to allocate dma channel %i\n", dma0);
  545. return -EINVAL;
  546. }
  547. devpriv->dma0 = dma0;
  548. devpriv->dma_current = dma0;
  549. if (dma1) {
  550. if (request_dma(dma1, driver_das1800.driver_name)) {
  551. printk(" failed to allocate dma channel %i\n",
  552. dma1);
  553. return -EINVAL;
  554. }
  555. devpriv->dma1 = dma1;
  556. }
  557. devpriv->ai_buf0 = kmalloc(DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA);
  558. if (devpriv->ai_buf0 == NULL)
  559. return -ENOMEM;
  560. devpriv->dma_current_buf = devpriv->ai_buf0;
  561. if (dma1) {
  562. devpriv->ai_buf1 =
  563. kmalloc(DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA);
  564. if (devpriv->ai_buf1 == NULL)
  565. return -ENOMEM;
  566. }
  567. flags = claim_dma_lock();
  568. disable_dma(devpriv->dma0);
  569. set_dma_mode(devpriv->dma0, DMA_MODE_READ);
  570. if (dma1) {
  571. disable_dma(devpriv->dma1);
  572. set_dma_mode(devpriv->dma1, DMA_MODE_READ);
  573. }
  574. release_dma_lock(flags);
  575. }
  576. return 0;
  577. }
  578. static int das1800_attach(struct comedi_device *dev,
  579. struct comedi_devconfig *it)
  580. {
  581. struct comedi_subdevice *s;
  582. unsigned long iobase = it->options[0];
  583. unsigned int irq = it->options[1];
  584. unsigned int dma0 = it->options[2];
  585. unsigned int dma1 = it->options[3];
  586. unsigned long iobase2;
  587. int board;
  588. int retval;
  589. /* allocate and initialize dev->private */
  590. if (alloc_private(dev, sizeof(struct das1800_private)) < 0)
  591. return -ENOMEM;
  592. printk("comedi%d: %s: io 0x%lx", dev->minor, driver_das1800.driver_name,
  593. iobase);
  594. if (irq) {
  595. printk(", irq %u", irq);
  596. if (dma0) {
  597. printk(", dma %u", dma0);
  598. if (dma1)
  599. printk(" and %u", dma1);
  600. }
  601. }
  602. printk("\n");
  603. if (iobase == 0) {
  604. printk(" io base address required\n");
  605. return -EINVAL;
  606. }
  607. /* check if io addresses are available */
  608. if (!request_region(iobase, DAS1800_SIZE, driver_das1800.driver_name)) {
  609. printk
  610. (" I/O port conflict: failed to allocate ports 0x%lx to 0x%lx\n",
  611. iobase, iobase + DAS1800_SIZE - 1);
  612. return -EIO;
  613. }
  614. dev->iobase = iobase;
  615. board = das1800_probe(dev);
  616. if (board < 0) {
  617. printk(" unable to determine board type\n");
  618. return -ENODEV;
  619. }
  620. dev->board_ptr = das1800_boards + board;
  621. dev->board_name = thisboard->name;
  622. /* if it is an 'ao' board with fancy analog out then we need extra io ports */
  623. if (thisboard->ao_ability == 2) {
  624. iobase2 = iobase + IOBASE2;
  625. if (!request_region(iobase2, DAS1800_SIZE,
  626. driver_das1800.driver_name)) {
  627. printk
  628. (" I/O port conflict: failed to allocate ports 0x%lx to 0x%lx\n",
  629. iobase2, iobase2 + DAS1800_SIZE - 1);
  630. return -EIO;
  631. }
  632. devpriv->iobase2 = iobase2;
  633. }
  634. /* grab our IRQ */
  635. if (irq) {
  636. if (request_irq(irq, das1800_interrupt, 0,
  637. driver_das1800.driver_name, dev)) {
  638. printk(" unable to allocate irq %u\n", irq);
  639. return -EINVAL;
  640. }
  641. }
  642. dev->irq = irq;
  643. /* set bits that tell card which irq to use */
  644. switch (irq) {
  645. case 0:
  646. break;
  647. case 3:
  648. devpriv->irq_dma_bits |= 0x8;
  649. break;
  650. case 5:
  651. devpriv->irq_dma_bits |= 0x10;
  652. break;
  653. case 7:
  654. devpriv->irq_dma_bits |= 0x18;
  655. break;
  656. case 10:
  657. devpriv->irq_dma_bits |= 0x28;
  658. break;
  659. case 11:
  660. devpriv->irq_dma_bits |= 0x30;
  661. break;
  662. case 15:
  663. devpriv->irq_dma_bits |= 0x38;
  664. break;
  665. default:
  666. printk(" irq out of range\n");
  667. return -EINVAL;
  668. break;
  669. }
  670. retval = das1800_init_dma(dev, dma0, dma1);
  671. if (retval < 0)
  672. return retval;
  673. if (devpriv->ai_buf0 == NULL) {
  674. devpriv->ai_buf0 =
  675. kmalloc(FIFO_SIZE * sizeof(uint16_t), GFP_KERNEL);
  676. if (devpriv->ai_buf0 == NULL)
  677. return -ENOMEM;
  678. }
  679. if (alloc_subdevices(dev, 4) < 0)
  680. return -ENOMEM;
  681. /* analog input subdevice */
  682. s = dev->subdevices + 0;
  683. dev->read_subdev = s;
  684. s->type = COMEDI_SUBD_AI;
  685. s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_GROUND | SDF_CMD_READ;
  686. if (thisboard->common)
  687. s->subdev_flags |= SDF_COMMON;
  688. s->n_chan = thisboard->qram_len;
  689. s->len_chanlist = thisboard->qram_len;
  690. s->maxdata = (1 << thisboard->resolution) - 1;
  691. s->range_table = thisboard->range_ai;
  692. s->do_cmd = das1800_ai_do_cmd;
  693. s->do_cmdtest = das1800_ai_do_cmdtest;
  694. s->insn_read = das1800_ai_rinsn;
  695. s->poll = das1800_ai_poll;
  696. s->cancel = das1800_cancel;
  697. /* analog out */
  698. s = dev->subdevices + 1;
  699. if (thisboard->ao_ability == 1) {
  700. s->type = COMEDI_SUBD_AO;
  701. s->subdev_flags = SDF_WRITABLE;
  702. s->n_chan = thisboard->ao_n_chan;
  703. s->maxdata = (1 << thisboard->resolution) - 1;
  704. s->range_table = &range_ao_1;
  705. s->insn_write = das1800_ao_winsn;
  706. } else {
  707. s->type = COMEDI_SUBD_UNUSED;
  708. }
  709. /* di */
  710. s = dev->subdevices + 2;
  711. s->type = COMEDI_SUBD_DI;
  712. s->subdev_flags = SDF_READABLE;
  713. s->n_chan = 4;
  714. s->maxdata = 1;
  715. s->range_table = &range_digital;
  716. s->insn_bits = das1800_di_rbits;
  717. /* do */
  718. s = dev->subdevices + 3;
  719. s->type = COMEDI_SUBD_DO;
  720. s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
  721. s->n_chan = thisboard->do_n_chan;
  722. s->maxdata = 1;
  723. s->range_table = &range_digital;
  724. s->insn_bits = das1800_do_wbits;
  725. das1800_cancel(dev, dev->read_subdev);
  726. /* initialize digital out channels */
  727. outb(devpriv->do_bits, dev->iobase + DAS1800_DIGITAL);
  728. /* initialize analog out channels */
  729. if (thisboard->ao_ability == 1) {
  730. /* select 'update' dac channel for baseAddress + 0x0 */
  731. outb(DAC(thisboard->ao_n_chan - 1),
  732. dev->iobase + DAS1800_SELECT);
  733. outw(devpriv->ao_update_bits, dev->iobase + DAS1800_DAC);
  734. }
  735. return 0;
  736. };
  737. static int das1800_detach(struct comedi_device *dev)
  738. {
  739. /* only free stuff if it has been allocated by _attach */
  740. if (dev->iobase)
  741. release_region(dev->iobase, DAS1800_SIZE);
  742. if (dev->irq)
  743. free_irq(dev->irq, dev);
  744. if (dev->private) {
  745. if (devpriv->iobase2)
  746. release_region(devpriv->iobase2, DAS1800_SIZE);
  747. if (devpriv->dma0)
  748. free_dma(devpriv->dma0);
  749. if (devpriv->dma1)
  750. free_dma(devpriv->dma1);
  751. kfree(devpriv->ai_buf0);
  752. kfree(devpriv->ai_buf1);
  753. }
  754. printk("comedi%d: %s: remove\n", dev->minor,
  755. driver_das1800.driver_name);
  756. return 0;
  757. };
  758. /* probes and checks das-1800 series board type
  759. */
  760. static int das1800_probe(struct comedi_device *dev)
  761. {
  762. int id;
  763. int board;
  764. id = (inb(dev->iobase + DAS1800_DIGITAL) >> 4) & 0xf; /* get id bits */
  765. board = ((struct das1800_board *)dev->board_ptr) - das1800_boards;
  766. switch (id) {
  767. case 0x3:
  768. if (board == das1801st_da || board == das1802st_da ||
  769. board == das1701st_da || board == das1702st_da) {
  770. printk(" Board model: %s\n",
  771. das1800_boards[board].name);
  772. return board;
  773. }
  774. printk
  775. (" Board model (probed, not recommended): das-1800st-da series\n");
  776. return das1801st;
  777. break;
  778. case 0x4:
  779. if (board == das1802hr_da || board == das1702hr_da) {
  780. printk(" Board model: %s\n",
  781. das1800_boards[board].name);
  782. return board;
  783. }
  784. printk
  785. (" Board model (probed, not recommended): das-1802hr-da\n");
  786. return das1802hr;
  787. break;
  788. case 0x5:
  789. if (board == das1801ao || board == das1802ao ||
  790. board == das1701ao || board == das1702ao) {
  791. printk(" Board model: %s\n",
  792. das1800_boards[board].name);
  793. return board;
  794. }
  795. printk
  796. (" Board model (probed, not recommended): das-1800ao series\n");
  797. return das1801ao;
  798. break;
  799. case 0x6:
  800. if (board == das1802hr || board == das1702hr) {
  801. printk(" Board model: %s\n",
  802. das1800_boards[board].name);
  803. return board;
  804. }
  805. printk(" Board model (probed, not recommended): das-1802hr\n");
  806. return das1802hr;
  807. break;
  808. case 0x7:
  809. if (board == das1801st || board == das1802st ||
  810. board == das1701st || board == das1702st) {
  811. printk(" Board model: %s\n",
  812. das1800_boards[board].name);
  813. return board;
  814. }
  815. printk
  816. (" Board model (probed, not recommended): das-1800st series\n");
  817. return das1801st;
  818. break;
  819. case 0x8:
  820. if (board == das1801hc || board == das1802hc) {
  821. printk(" Board model: %s\n",
  822. das1800_boards[board].name);
  823. return board;
  824. }
  825. printk
  826. (" Board model (probed, not recommended): das-1800hc series\n");
  827. return das1801hc;
  828. break;
  829. default:
  830. printk
  831. (" Board model: probe returned 0x%x (unknown, please report)\n",
  832. id);
  833. return board;
  834. break;
  835. }
  836. return -1;
  837. }
  838. static int das1800_ai_poll(struct comedi_device *dev,
  839. struct comedi_subdevice *s)
  840. {
  841. unsigned long flags;
  842. /* prevent race with interrupt handler */
  843. spin_lock_irqsave(&dev->spinlock, flags);
  844. das1800_ai_handler(dev);
  845. spin_unlock_irqrestore(&dev->spinlock, flags);
  846. return s->async->buf_write_count - s->async->buf_read_count;
  847. }
  848. static irqreturn_t das1800_interrupt(int irq, void *d)
  849. {
  850. struct comedi_device *dev = d;
  851. unsigned int status;
  852. if (dev->attached == 0) {
  853. comedi_error(dev, "premature interrupt");
  854. return IRQ_HANDLED;
  855. }
  856. /* Prevent race with das1800_ai_poll() on multi processor systems.
  857. * Also protects indirect addressing in das1800_ai_handler */
  858. spin_lock(&dev->spinlock);
  859. status = inb(dev->iobase + DAS1800_STATUS);
  860. /* if interrupt was not caused by das-1800 */
  861. if (!(status & INT)) {
  862. spin_unlock(&dev->spinlock);
  863. return IRQ_NONE;
  864. }
  865. /* clear the interrupt status bit INT */
  866. outb(CLEAR_INTR_MASK & ~INT, dev->iobase + DAS1800_STATUS);
  867. /* handle interrupt */
  868. das1800_ai_handler(dev);
  869. spin_unlock(&dev->spinlock);
  870. return IRQ_HANDLED;
  871. }
  872. /* the guts of the interrupt handler, that is shared with das1800_ai_poll */
  873. static void das1800_ai_handler(struct comedi_device *dev)
  874. {
  875. struct comedi_subdevice *s = dev->subdevices + 0; /* analog input subdevice */
  876. struct comedi_async *async = s->async;
  877. struct comedi_cmd *cmd = &async->cmd;
  878. unsigned int status = inb(dev->iobase + DAS1800_STATUS);
  879. async->events = 0;
  880. /* select adc for base address + 0 */
  881. outb(ADC, dev->iobase + DAS1800_SELECT);
  882. /* dma buffer full */
  883. if (devpriv->irq_dma_bits & DMA_ENABLED) {
  884. /* look for data from dma transfer even if dma terminal count hasn't happened yet */
  885. das1800_handle_dma(dev, s, status);
  886. } else if (status & FHF) { /* if fifo half full */
  887. das1800_handle_fifo_half_full(dev, s);
  888. } else if (status & FNE) { /* if fifo not empty */
  889. das1800_handle_fifo_not_empty(dev, s);
  890. }
  891. async->events |= COMEDI_CB_BLOCK;
  892. /* if the card's fifo has overflowed */
  893. if (status & OVF) {
  894. /* clear OVF interrupt bit */
  895. outb(CLEAR_INTR_MASK & ~OVF, dev->iobase + DAS1800_STATUS);
  896. comedi_error(dev, "DAS1800 FIFO overflow");
  897. das1800_cancel(dev, s);
  898. async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
  899. comedi_event(dev, s);
  900. return;
  901. }
  902. /* stop taking data if appropriate */
  903. /* stop_src TRIG_EXT */
  904. if (status & CT0TC) {
  905. /* clear CT0TC interrupt bit */
  906. outb(CLEAR_INTR_MASK & ~CT0TC, dev->iobase + DAS1800_STATUS);
  907. /* make sure we get all remaining data from board before quitting */
  908. if (devpriv->irq_dma_bits & DMA_ENABLED)
  909. das1800_flush_dma(dev, s);
  910. else
  911. das1800_handle_fifo_not_empty(dev, s);
  912. das1800_cancel(dev, s); /* disable hardware conversions */
  913. async->events |= COMEDI_CB_EOA;
  914. } else if (cmd->stop_src == TRIG_COUNT && devpriv->count == 0) { /* stop_src TRIG_COUNT */
  915. das1800_cancel(dev, s); /* disable hardware conversions */
  916. async->events |= COMEDI_CB_EOA;
  917. }
  918. comedi_event(dev, s);
  919. return;
  920. }
  921. static void das1800_handle_dma(struct comedi_device *dev,
  922. struct comedi_subdevice *s, unsigned int status)
  923. {
  924. unsigned long flags;
  925. const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
  926. flags = claim_dma_lock();
  927. das1800_flush_dma_channel(dev, s, devpriv->dma_current,
  928. devpriv->dma_current_buf);
  929. /* re-enable dma channel */
  930. set_dma_addr(devpriv->dma_current,
  931. virt_to_bus(devpriv->dma_current_buf));
  932. set_dma_count(devpriv->dma_current, devpriv->dma_transfer_size);
  933. enable_dma(devpriv->dma_current);
  934. release_dma_lock(flags);
  935. if (status & DMATC) {
  936. /* clear DMATC interrupt bit */
  937. outb(CLEAR_INTR_MASK & ~DMATC, dev->iobase + DAS1800_STATUS);
  938. /* switch dma channels for next time, if appropriate */
  939. if (dual_dma) {
  940. /* read data from the other channel next time */
  941. if (devpriv->dma_current == devpriv->dma0) {
  942. devpriv->dma_current = devpriv->dma1;
  943. devpriv->dma_current_buf = devpriv->ai_buf1;
  944. } else {
  945. devpriv->dma_current = devpriv->dma0;
  946. devpriv->dma_current_buf = devpriv->ai_buf0;
  947. }
  948. }
  949. }
  950. return;
  951. }
  952. static inline uint16_t munge_bipolar_sample(const struct comedi_device *dev,
  953. uint16_t sample)
  954. {
  955. sample += 1 << (thisboard->resolution - 1);
  956. return sample;
  957. }
  958. static void munge_data(struct comedi_device *dev, uint16_t * array,
  959. unsigned int num_elements)
  960. {
  961. unsigned int i;
  962. int unipolar;
  963. /* see if card is using a unipolar or bipolar range so we can munge data correctly */
  964. unipolar = inb(dev->iobase + DAS1800_CONTROL_C) & UB;
  965. /* convert to unsigned type if we are in a bipolar mode */
  966. if (!unipolar) {
  967. for (i = 0; i < num_elements; i++)
  968. array[i] = munge_bipolar_sample(dev, array[i]);
  969. }
  970. }
  971. /* Utility function used by das1800_flush_dma() and das1800_handle_dma().
  972. * Assumes dma lock is held */
  973. static void das1800_flush_dma_channel(struct comedi_device *dev,
  974. struct comedi_subdevice *s,
  975. unsigned int channel, uint16_t *buffer)
  976. {
  977. unsigned int num_bytes, num_samples;
  978. struct comedi_cmd *cmd = &s->async->cmd;
  979. disable_dma(channel);
  980. /* clear flip-flop to make sure 2-byte registers
  981. * get set correctly */
  982. clear_dma_ff(channel);
  983. /* figure out how many points to read */
  984. num_bytes = devpriv->dma_transfer_size - get_dma_residue(channel);
  985. num_samples = num_bytes / sizeof(short);
  986. /* if we only need some of the points */
  987. if (cmd->stop_src == TRIG_COUNT && devpriv->count < num_samples)
  988. num_samples = devpriv->count;
  989. munge_data(dev, buffer, num_samples);
  990. cfc_write_array_to_buffer(s, buffer, num_bytes);
  991. if (s->async->cmd.stop_src == TRIG_COUNT)
  992. devpriv->count -= num_samples;
  993. return;
  994. }
  995. /* flushes remaining data from board when external trigger has stopped acquisition
  996. * and we are using dma transfers */
  997. static void das1800_flush_dma(struct comedi_device *dev,
  998. struct comedi_subdevice *s)
  999. {
  1000. unsigned long flags;
  1001. const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
  1002. flags = claim_dma_lock();
  1003. das1800_flush_dma_channel(dev, s, devpriv->dma_current,
  1004. devpriv->dma_current_buf);
  1005. if (dual_dma) {
  1006. /* switch to other channel and flush it */
  1007. if (devpriv->dma_current == devpriv->dma0) {
  1008. devpriv->dma_current = devpriv->dma1;
  1009. devpriv->dma_current_buf = devpriv->ai_buf1;
  1010. } else {
  1011. devpriv->dma_current = devpriv->dma0;
  1012. devpriv->dma_current_buf = devpriv->ai_buf0;
  1013. }
  1014. das1800_flush_dma_channel(dev, s, devpriv->dma_current,
  1015. devpriv->dma_current_buf);
  1016. }
  1017. release_dma_lock(flags);
  1018. /* get any remaining samples in fifo */
  1019. das1800_handle_fifo_not_empty(dev, s);
  1020. return;
  1021. }
  1022. static void das1800_handle_fifo_half_full(struct comedi_device *dev,
  1023. struct comedi_subdevice *s)
  1024. {
  1025. int numPoints = 0; /* number of points to read */
  1026. struct comedi_cmd *cmd = &s->async->cmd;
  1027. numPoints = FIFO_SIZE / 2;
  1028. /* if we only need some of the points */
  1029. if (cmd->stop_src == TRIG_COUNT && devpriv->count < numPoints)
  1030. numPoints = devpriv->count;
  1031. insw(dev->iobase + DAS1800_FIFO, devpriv->ai_buf0, numPoints);
  1032. munge_data(dev, devpriv->ai_buf0, numPoints);
  1033. cfc_write_array_to_buffer(s, devpriv->ai_buf0,
  1034. numPoints * sizeof(devpriv->ai_buf0[0]));
  1035. if (cmd->stop_src == TRIG_COUNT)
  1036. devpriv->count -= numPoints;
  1037. return;
  1038. }
  1039. static void das1800_handle_fifo_not_empty(struct comedi_device *dev,
  1040. struct comedi_subdevice *s)
  1041. {
  1042. short dpnt;
  1043. int unipolar;
  1044. struct comedi_cmd *cmd = &s->async->cmd;
  1045. unipolar = inb(dev->iobase + DAS1800_CONTROL_C) & UB;
  1046. while (inb(dev->iobase + DAS1800_STATUS) & FNE) {
  1047. if (cmd->stop_src == TRIG_COUNT && devpriv->count == 0)
  1048. break;
  1049. dpnt = inw(dev->iobase + DAS1800_FIFO);
  1050. /* convert to unsigned type if we are in a bipolar mode */
  1051. if (!unipolar)
  1052. ;
  1053. dpnt = munge_bipolar_sample(dev, dpnt);
  1054. cfc_write_to_buffer(s, dpnt);
  1055. if (cmd->stop_src == TRIG_COUNT)
  1056. devpriv->count--;
  1057. }
  1058. return;
  1059. }
  1060. static int das1800_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
  1061. {
  1062. outb(0x0, dev->iobase + DAS1800_STATUS); /* disable conversions */
  1063. outb(0x0, dev->iobase + DAS1800_CONTROL_B); /* disable interrupts and dma */
  1064. outb(0x0, dev->iobase + DAS1800_CONTROL_A); /* disable and clear fifo and stop triggering */
  1065. if (devpriv->dma0)
  1066. disable_dma(devpriv->dma0);
  1067. if (devpriv->dma1)
  1068. disable_dma(devpriv->dma1);
  1069. return 0;
  1070. }
  1071. /* test analog input cmd */
  1072. static int das1800_ai_do_cmdtest(struct comedi_device *dev,
  1073. struct comedi_subdevice *s,
  1074. struct comedi_cmd *cmd)
  1075. {
  1076. int err = 0;
  1077. int tmp;
  1078. unsigned int tmp_arg;
  1079. int i;
  1080. int unipolar;
  1081. /* step 1: make sure trigger sources are trivially valid */
  1082. tmp = cmd->start_src;
  1083. cmd->start_src &= TRIG_NOW | TRIG_EXT;
  1084. if (!cmd->start_src || tmp != cmd->start_src)
  1085. err++;
  1086. tmp = cmd->scan_begin_src;
  1087. cmd->scan_begin_src &= TRIG_FOLLOW | TRIG_TIMER | TRIG_EXT;
  1088. if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
  1089. err++;
  1090. tmp = cmd->convert_src;
  1091. cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
  1092. if (!cmd->convert_src || tmp != cmd->convert_src)
  1093. err++;
  1094. tmp = cmd->scan_end_src;
  1095. cmd->scan_end_src &= TRIG_COUNT;
  1096. if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
  1097. err++;
  1098. tmp = cmd->stop_src;
  1099. cmd->stop_src &= TRIG_COUNT | TRIG_EXT | TRIG_NONE;
  1100. if (!cmd->stop_src || tmp != cmd->stop_src)
  1101. err++;
  1102. if (err)
  1103. return 1;
  1104. /* step 2: make sure trigger sources are unique and mutually compatible */
  1105. /* uniqueness check */
  1106. if (cmd->start_src != TRIG_NOW && cmd->start_src != TRIG_EXT)
  1107. err++;
  1108. if (cmd->scan_begin_src != TRIG_FOLLOW &&
  1109. cmd->scan_begin_src != TRIG_TIMER &&
  1110. cmd->scan_begin_src != TRIG_EXT)
  1111. err++;
  1112. if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
  1113. err++;
  1114. if (cmd->stop_src != TRIG_COUNT &&
  1115. cmd->stop_src != TRIG_NONE && cmd->stop_src != TRIG_EXT)
  1116. err++;
  1117. /* compatibility check */
  1118. if (cmd->scan_begin_src != TRIG_FOLLOW &&
  1119. cmd->convert_src != TRIG_TIMER)
  1120. err++;
  1121. if (err)
  1122. return 2;
  1123. /* step 3: make sure arguments are trivially compatible */
  1124. if (cmd->start_arg != 0) {
  1125. cmd->start_arg = 0;
  1126. err++;
  1127. }
  1128. if (cmd->convert_src == TRIG_TIMER) {
  1129. if (cmd->convert_arg < thisboard->ai_speed) {
  1130. cmd->convert_arg = thisboard->ai_speed;
  1131. err++;
  1132. }
  1133. }
  1134. if (!cmd->chanlist_len) {
  1135. cmd->chanlist_len = 1;
  1136. err++;
  1137. }
  1138. if (cmd->scan_end_arg != cmd->chanlist_len) {
  1139. cmd->scan_end_arg = cmd->chanlist_len;
  1140. err++;
  1141. }
  1142. switch (cmd->stop_src) {
  1143. case TRIG_COUNT:
  1144. if (!cmd->stop_arg) {
  1145. cmd->stop_arg = 1;
  1146. err++;
  1147. }
  1148. break;
  1149. case TRIG_NONE:
  1150. if (cmd->stop_arg != 0) {
  1151. cmd->stop_arg = 0;
  1152. err++;
  1153. }
  1154. break;
  1155. default:
  1156. break;
  1157. }
  1158. if (err)
  1159. return 3;
  1160. /* step 4: fix up any arguments */
  1161. if (cmd->convert_src == TRIG_TIMER) {
  1162. /* if we are not in burst mode */
  1163. if (cmd->scan_begin_src == TRIG_FOLLOW) {
  1164. tmp_arg = cmd->convert_arg;
  1165. /* calculate counter values that give desired timing */
  1166. i8253_cascade_ns_to_timer_2div(TIMER_BASE,
  1167. &(devpriv->divisor1),
  1168. &(devpriv->divisor2),
  1169. &(cmd->convert_arg),
  1170. cmd->
  1171. flags & TRIG_ROUND_MASK);
  1172. if (tmp_arg != cmd->convert_arg)
  1173. err++;
  1174. }
  1175. /* if we are in burst mode */
  1176. else {
  1177. /* check that convert_arg is compatible */
  1178. tmp_arg = cmd->convert_arg;
  1179. cmd->convert_arg =
  1180. burst_convert_arg(cmd->convert_arg,
  1181. cmd->flags & TRIG_ROUND_MASK);
  1182. if (tmp_arg != cmd->convert_arg)
  1183. err++;
  1184. if (cmd->scan_begin_src == TRIG_TIMER) {
  1185. /* if scans are timed faster than conversion rate allows */
  1186. if (cmd->convert_arg * cmd->chanlist_len >
  1187. cmd->scan_begin_arg) {
  1188. cmd->scan_begin_arg =
  1189. cmd->convert_arg *
  1190. cmd->chanlist_len;
  1191. err++;
  1192. }
  1193. tmp_arg = cmd->scan_begin_arg;
  1194. /* calculate counter values that give desired timing */
  1195. i8253_cascade_ns_to_timer_2div(TIMER_BASE,
  1196. &(devpriv->
  1197. divisor1),
  1198. &(devpriv->
  1199. divisor2),
  1200. &(cmd->
  1201. scan_begin_arg),
  1202. cmd->
  1203. flags &
  1204. TRIG_ROUND_MASK);
  1205. if (tmp_arg != cmd->scan_begin_arg)
  1206. err++;
  1207. }
  1208. }
  1209. }
  1210. if (err)
  1211. return 4;
  1212. /* make sure user is not trying to mix unipolar and bipolar ranges */
  1213. if (cmd->chanlist) {
  1214. unipolar = CR_RANGE(cmd->chanlist[0]) & UNIPOLAR;
  1215. for (i = 1; i < cmd->chanlist_len; i++) {
  1216. if (unipolar != (CR_RANGE(cmd->chanlist[i]) & UNIPOLAR)) {
  1217. comedi_error(dev,
  1218. "unipolar and bipolar ranges cannot be mixed in the chanlist");
  1219. err++;
  1220. break;
  1221. }
  1222. }
  1223. }
  1224. if (err)
  1225. return 5;
  1226. return 0;
  1227. }
  1228. /* analog input cmd interface */
  1229. /* first, some utility functions used in the main ai_do_cmd() */
  1230. /* returns appropriate bits for control register a, depending on command */
  1231. static int control_a_bits(struct comedi_cmd cmd)
  1232. {
  1233. int control_a;
  1234. control_a = FFEN; /* enable fifo */
  1235. if (cmd.stop_src == TRIG_EXT)
  1236. control_a |= ATEN;
  1237. switch (cmd.start_src) {
  1238. case TRIG_EXT:
  1239. control_a |= TGEN | CGSL;
  1240. break;
  1241. case TRIG_NOW:
  1242. control_a |= CGEN;
  1243. break;
  1244. default:
  1245. break;
  1246. }
  1247. return control_a;
  1248. }
  1249. /* returns appropriate bits for control register c, depending on command */
  1250. static int control_c_bits(struct comedi_cmd cmd)
  1251. {
  1252. int control_c;
  1253. int aref;
  1254. /* set clock source to internal or external, select analog reference,
  1255. * select unipolar / bipolar
  1256. */
  1257. aref = CR_AREF(cmd.chanlist[0]);
  1258. control_c = UQEN; /* enable upper qram addresses */
  1259. if (aref != AREF_DIFF)
  1260. control_c |= SD;
  1261. if (aref == AREF_COMMON)
  1262. control_c |= CMEN;
  1263. /* if a unipolar range was selected */
  1264. if (CR_RANGE(cmd.chanlist[0]) & UNIPOLAR)
  1265. control_c |= UB;
  1266. switch (cmd.scan_begin_src) {
  1267. case TRIG_FOLLOW: /* not in burst mode */
  1268. switch (cmd.convert_src) {
  1269. case TRIG_TIMER:
  1270. /* trig on cascaded counters */
  1271. control_c |= IPCLK;
  1272. break;
  1273. case TRIG_EXT:
  1274. /* trig on falling edge of external trigger */
  1275. control_c |= XPCLK;
  1276. break;
  1277. default:
  1278. break;
  1279. }
  1280. break;
  1281. case TRIG_TIMER:
  1282. /* burst mode with internal pacer clock */
  1283. control_c |= BMDE | IPCLK;
  1284. break;
  1285. case TRIG_EXT:
  1286. /* burst mode with external trigger */
  1287. control_c |= BMDE | XPCLK;
  1288. break;
  1289. default:
  1290. break;
  1291. }
  1292. return control_c;
  1293. }
  1294. /* sets up counters */
  1295. static int setup_counters(struct comedi_device *dev, struct comedi_cmd cmd)
  1296. {
  1297. /* setup cascaded counters for conversion/scan frequency */
  1298. switch (cmd.scan_begin_src) {
  1299. case TRIG_FOLLOW: /* not in burst mode */
  1300. if (cmd.convert_src == TRIG_TIMER) {
  1301. /* set conversion frequency */
  1302. i8253_cascade_ns_to_timer_2div(TIMER_BASE,
  1303. &(devpriv->divisor1),
  1304. &(devpriv->divisor2),
  1305. &(cmd.convert_arg),
  1306. cmd.
  1307. flags & TRIG_ROUND_MASK);
  1308. if (das1800_set_frequency(dev) < 0)
  1309. return -1;
  1310. }
  1311. break;
  1312. case TRIG_TIMER: /* in burst mode */
  1313. /* set scan frequency */
  1314. i8253_cascade_ns_to_timer_2div(TIMER_BASE, &(devpriv->divisor1),
  1315. &(devpriv->divisor2),
  1316. &(cmd.scan_begin_arg),
  1317. cmd.flags & TRIG_ROUND_MASK);
  1318. if (das1800_set_frequency(dev) < 0)
  1319. return -1;
  1320. break;
  1321. default:
  1322. break;
  1323. }
  1324. /* setup counter 0 for 'about triggering' */
  1325. if (cmd.stop_src == TRIG_EXT) {
  1326. /* load counter 0 in mode 0 */
  1327. i8254_load(dev->iobase + DAS1800_COUNTER, 0, 0, 1, 0);
  1328. }
  1329. return 0;
  1330. }
  1331. /* sets up dma */
  1332. static void setup_dma(struct comedi_device *dev, struct comedi_cmd cmd)
  1333. {
  1334. unsigned long lock_flags;
  1335. const int dual_dma = devpriv->irq_dma_bits & DMA_DUAL;
  1336. if ((devpriv->irq_dma_bits & DMA_ENABLED) == 0)
  1337. return;
  1338. /* determine a reasonable dma transfer size */
  1339. devpriv->dma_transfer_size = suggest_transfer_size(&cmd);
  1340. lock_flags = claim_dma_lock();
  1341. disable_dma(devpriv->dma0);
  1342. /* clear flip-flop to make sure 2-byte registers for
  1343. * count and address get set correctly */
  1344. clear_dma_ff(devpriv->dma0);
  1345. set_dma_addr(devpriv->dma0, virt_to_bus(devpriv->ai_buf0));
  1346. /* set appropriate size of transfer */
  1347. set_dma_count(devpriv->dma0, devpriv->dma_transfer_size);
  1348. devpriv->dma_current = devpriv->dma0;
  1349. devpriv->dma_current_buf = devpriv->ai_buf0;
  1350. enable_dma(devpriv->dma0);
  1351. /* set up dual dma if appropriate */
  1352. if (dual_dma) {
  1353. disable_dma(devpriv->dma1);
  1354. /* clear flip-flop to make sure 2-byte registers for
  1355. * count and address get set correctly */
  1356. clear_dma_ff(devpriv->dma1);
  1357. set_dma_addr(devpriv->dma1, virt_to_bus(devpriv->ai_buf1));
  1358. /* set appropriate size of transfer */
  1359. set_dma_count(devpriv->dma1, devpriv->dma_transfer_size);
  1360. enable_dma(devpriv->dma1);
  1361. }
  1362. release_dma_lock(lock_flags);
  1363. return;
  1364. }
  1365. /* programs channel/gain list into card */
  1366. static void program_chanlist(struct comedi_device *dev, struct comedi_cmd cmd)
  1367. {
  1368. int i, n, chan_range;
  1369. unsigned long irq_flags;
  1370. const int range_mask = 0x3; /* masks unipolar/bipolar bit off range */
  1371. const int range_bitshift = 8;
  1372. n = cmd.chanlist_len;
  1373. /* spinlock protects indirect addressing */
  1374. spin_lock_irqsave(&dev->spinlock, irq_flags);
  1375. outb(QRAM, dev->iobase + DAS1800_SELECT); /* select QRAM for baseAddress + 0x0 */
  1376. outb(n - 1, dev->iobase + DAS1800_QRAM_ADDRESS); /*set QRAM address start */
  1377. /* make channel / gain list */
  1378. for (i = 0; i < n; i++) {
  1379. chan_range =
  1380. CR_CHAN(cmd.
  1381. chanlist[i]) | ((CR_RANGE(cmd.chanlist[i]) &
  1382. range_mask) << range_bitshift);
  1383. outw(chan_range, dev->iobase + DAS1800_QRAM);
  1384. }
  1385. outb(n - 1, dev->iobase + DAS1800_QRAM_ADDRESS); /*finish write to QRAM */
  1386. spin_unlock_irqrestore(&dev->spinlock, irq_flags);
  1387. return;
  1388. }
  1389. /* analog input do_cmd */
  1390. static int das1800_ai_do_cmd(struct comedi_device *dev,
  1391. struct comedi_subdevice *s)
  1392. {
  1393. int ret;
  1394. int control_a, control_c;
  1395. struct comedi_async *async = s->async;
  1396. struct comedi_cmd cmd = async->cmd;
  1397. if (!dev->irq) {
  1398. comedi_error(dev,
  1399. "no irq assigned for das-1800, cannot do hardware conversions");
  1400. return -1;
  1401. }
  1402. /* disable dma on TRIG_WAKE_EOS, or TRIG_RT
  1403. * (because dma in handler is unsafe at hard real-time priority) */
  1404. if (cmd.flags & (TRIG_WAKE_EOS | TRIG_RT))
  1405. devpriv->irq_dma_bits &= ~DMA_ENABLED;
  1406. else
  1407. devpriv->irq_dma_bits |= devpriv->dma_bits;
  1408. /* interrupt on end of conversion for TRIG_WAKE_EOS */
  1409. if (cmd.flags & TRIG_WAKE_EOS) {
  1410. /* interrupt fifo not empty */
  1411. devpriv->irq_dma_bits &= ~FIMD;
  1412. } else {
  1413. /* interrupt fifo half full */
  1414. devpriv->irq_dma_bits |= FIMD;
  1415. }
  1416. /* determine how many conversions we need */
  1417. if (cmd.stop_src == TRIG_COUNT)
  1418. devpriv->count = cmd.stop_arg * cmd.chanlist_len;
  1419. das1800_cancel(dev, s);
  1420. /* determine proper bits for control registers */
  1421. control_a = control_a_bits(cmd);
  1422. control_c = control_c_bits(cmd);
  1423. /* setup card and start */
  1424. program_chanlist(dev, cmd);
  1425. ret = setup_counters(dev, cmd);
  1426. if (ret < 0) {
  1427. comedi_error(dev, "Error setting up counters");
  1428. return ret;
  1429. }
  1430. setup_dma(dev, cmd);
  1431. outb(control_c, dev->iobase + DAS1800_CONTROL_C);
  1432. /* set conversion rate and length for burst mode */
  1433. if (control_c & BMDE) {
  1434. /* program conversion period with number of microseconds minus 1 */
  1435. outb(cmd.convert_arg / 1000 - 1,
  1436. dev->iobase + DAS1800_BURST_RATE);
  1437. outb(cmd.chanlist_len - 1, dev->iobase + DAS1800_BURST_LENGTH);
  1438. }
  1439. outb(devpriv->irq_dma_bits, dev->iobase + DAS1800_CONTROL_B); /* enable irq/dma */
  1440. outb(control_a, dev->iobase + DAS1800_CONTROL_A); /* enable fifo and triggering */
  1441. outb(CVEN, dev->iobase + DAS1800_STATUS); /* enable conversions */
  1442. return 0;
  1443. }
  1444. /* read analog input */
  1445. static int das1800_ai_rinsn(struct comedi_device *dev,
  1446. struct comedi_subdevice *s,
  1447. struct comedi_insn *insn, unsigned int *data)
  1448. {
  1449. int i, n;
  1450. int chan, range, aref, chan_range;
  1451. int timeout = 1000;
  1452. short dpnt;
  1453. int conv_flags = 0;
  1454. unsigned long irq_flags;
  1455. /* set up analog reference and unipolar / bipolar mode */
  1456. aref = CR_AREF(insn->chanspec);
  1457. conv_flags |= UQEN;
  1458. if (aref != AREF_DIFF)
  1459. conv_flags |= SD;
  1460. if (aref == AREF_COMMON)
  1461. conv_flags |= CMEN;
  1462. /* if a unipolar range was selected */
  1463. if (CR_RANGE(insn->chanspec) & UNIPOLAR)
  1464. conv_flags |= UB;
  1465. outb(conv_flags, dev->iobase + DAS1800_CONTROL_C); /* software conversion enabled */
  1466. outb(CVEN, dev->iobase + DAS1800_STATUS); /* enable conversions */
  1467. outb(0x0, dev->iobase + DAS1800_CONTROL_A); /* reset fifo */
  1468. outb(FFEN, dev->iobase + DAS1800_CONTROL_A);
  1469. chan = CR_CHAN(insn->chanspec);
  1470. /* mask of unipolar/bipolar bit from range */
  1471. range = CR_RANGE(insn->chanspec) & 0x3;
  1472. chan_range = chan | (range << 8);
  1473. spin_lock_irqsave(&dev->spinlock, irq_flags);
  1474. outb(QRAM, dev->iobase + DAS1800_SELECT); /* select QRAM for baseAddress + 0x0 */
  1475. outb(0x0, dev->iobase + DAS1800_QRAM_ADDRESS); /* set QRAM address start */
  1476. outw(chan_range, dev->iobase + DAS1800_QRAM);
  1477. outb(0x0, dev->iobase + DAS1800_QRAM_ADDRESS); /*finish write to QRAM */
  1478. outb(ADC, dev->iobase + DAS1800_SELECT); /* select ADC for baseAddress + 0x0 */
  1479. for (n = 0; n < insn->n; n++) {
  1480. /* trigger conversion */
  1481. outb(0, dev->iobase + DAS1800_FIFO);
  1482. for (i = 0; i < timeout; i++) {
  1483. if (inb(dev->iobase + DAS1800_STATUS) & FNE)
  1484. break;
  1485. }
  1486. if (i == timeout) {
  1487. comedi_error(dev, "timeout");
  1488. n = -ETIME;
  1489. goto exit;
  1490. }
  1491. dpnt = inw(dev->iobase + DAS1800_FIFO);
  1492. /* shift data to offset binary for bipolar ranges */
  1493. if ((conv_flags & UB) == 0)
  1494. dpnt += 1 << (thisboard->resolution - 1);
  1495. data[n] = dpnt;
  1496. }
  1497. exit:
  1498. spin_unlock_irqrestore(&dev->spinlock, irq_flags);
  1499. return n;
  1500. }
  1501. /* writes to an analog output channel */
  1502. static int das1800_ao_winsn(struct comedi_device *dev,
  1503. struct comedi_subdevice *s,
  1504. struct comedi_insn *insn, unsigned int *data)
  1505. {
  1506. int chan = CR_CHAN(insn->chanspec);
  1507. /* int range = CR_RANGE(insn->chanspec); */
  1508. int update_chan = thisboard->ao_n_chan - 1;
  1509. short output;
  1510. unsigned long irq_flags;
  1511. /* card expects two's complement data */
  1512. output = data[0] - (1 << (thisboard->resolution - 1));
  1513. /* if the write is to the 'update' channel, we need to remember its value */
  1514. if (chan == update_chan)
  1515. devpriv->ao_update_bits = output;
  1516. /* write to channel */
  1517. spin_lock_irqsave(&dev->spinlock, irq_flags);
  1518. outb(DAC(chan), dev->iobase + DAS1800_SELECT); /* select dac channel for baseAddress + 0x0 */
  1519. outw(output, dev->iobase + DAS1800_DAC);
  1520. /* now we need to write to 'update' channel to update all dac channels */
  1521. if (chan != update_chan) {
  1522. outb(DAC(update_chan), dev->iobase + DAS1800_SELECT); /* select 'update' channel for baseAddress + 0x0 */
  1523. outw(devpriv->ao_update_bits, dev->iobase + DAS1800_DAC);
  1524. }
  1525. spin_unlock_irqrestore(&dev->spinlock, irq_flags);
  1526. return 1;
  1527. }
  1528. /* reads from digital input channels */
  1529. static int das1800_di_rbits(struct comedi_device *dev,
  1530. struct comedi_subdevice *s,
  1531. struct comedi_insn *insn, unsigned int *data)
  1532. {
  1533. data[1] = inb(dev->iobase + DAS1800_DIGITAL) & 0xf;
  1534. data[0] = 0;
  1535. return 2;
  1536. }
  1537. /* writes to digital output channels */
  1538. static int das1800_do_wbits(struct comedi_device *dev,
  1539. struct comedi_subdevice *s,
  1540. struct comedi_insn *insn, unsigned int *data)
  1541. {
  1542. unsigned int wbits;
  1543. /* only set bits that have been masked */
  1544. data[0] &= (1 << s->n_chan) - 1;
  1545. wbits = devpriv->do_bits;
  1546. wbits &= ~data[0];
  1547. wbits |= data[0] & data[1];
  1548. devpriv->do_bits = wbits;
  1549. outb(devpriv->do_bits, dev->iobase + DAS1800_DIGITAL);
  1550. data[1] = devpriv->do_bits;
  1551. return 2;
  1552. }
  1553. /* loads counters with divisor1, divisor2 from private structure */
  1554. static int das1800_set_frequency(struct comedi_device *dev)
  1555. {
  1556. int err = 0;
  1557. /* counter 1, mode 2 */
  1558. if (i8254_load(dev->iobase + DAS1800_COUNTER, 0, 1, devpriv->divisor1,
  1559. 2))
  1560. err++;
  1561. /* counter 2, mode 2 */
  1562. if (i8254_load(dev->iobase + DAS1800_COUNTER, 0, 2, devpriv->divisor2,
  1563. 2))
  1564. err++;
  1565. if (err)
  1566. return -1;
  1567. return 0;
  1568. }
  1569. /* converts requested conversion timing to timing compatible with
  1570. * hardware, used only when card is in 'burst mode'
  1571. */
  1572. static unsigned int burst_convert_arg(unsigned int convert_arg, int round_mode)
  1573. {
  1574. unsigned int micro_sec;
  1575. /* in burst mode, the maximum conversion time is 64 microseconds */
  1576. if (convert_arg > 64000)
  1577. convert_arg = 64000;
  1578. /* the conversion time must be an integral number of microseconds */
  1579. switch (round_mode) {
  1580. case TRIG_ROUND_NEAREST:
  1581. default:
  1582. micro_sec = (convert_arg + 500) / 1000;
  1583. break;
  1584. case TRIG_ROUND_DOWN:
  1585. micro_sec = convert_arg / 1000;
  1586. break;
  1587. case TRIG_ROUND_UP:
  1588. micro_sec = (convert_arg - 1) / 1000 + 1;
  1589. break;
  1590. }
  1591. /* return number of nanoseconds */
  1592. return micro_sec * 1000;
  1593. }
  1594. /* utility function that suggests a dma transfer size based on the conversion period 'ns' */
  1595. static unsigned int suggest_transfer_size(struct comedi_cmd *cmd)
  1596. {
  1597. unsigned int size = DMA_BUF_SIZE;
  1598. static const int sample_size = 2; /* size in bytes of one sample from board */
  1599. unsigned int fill_time = 300000000; /* target time in nanoseconds for filling dma buffer */
  1600. unsigned int max_size; /* maximum size we will allow for a transfer */
  1601. /* make dma buffer fill in 0.3 seconds for timed modes */
  1602. switch (cmd->scan_begin_src) {
  1603. case TRIG_FOLLOW: /* not in burst mode */
  1604. if (cmd->convert_src == TRIG_TIMER)
  1605. size = (fill_time / cmd->convert_arg) * sample_size;
  1606. break;
  1607. case TRIG_TIMER:
  1608. size = (fill_time / (cmd->scan_begin_arg * cmd->chanlist_len)) *
  1609. sample_size;
  1610. break;
  1611. default:
  1612. size = DMA_BUF_SIZE;
  1613. break;
  1614. }
  1615. /* set a minimum and maximum size allowed */
  1616. max_size = DMA_BUF_SIZE;
  1617. /* if we are taking limited number of conversions, limit transfer size to that */
  1618. if (cmd->stop_src == TRIG_COUNT &&
  1619. cmd->stop_arg * cmd->chanlist_len * sample_size < max_size)
  1620. max_size = cmd->stop_arg * cmd->chanlist_len * sample_size;
  1621. if (size > max_size)
  1622. size = max_size;
  1623. if (size < sample_size)
  1624. size = sample_size;
  1625. return size;
  1626. }
  1627. MODULE_AUTHOR("Comedi http://www.comedi.org");
  1628. MODULE_DESCRIPTION("Comedi low-level driver");
  1629. MODULE_LICENSE("GPL");