dtlk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /* -*- linux-c -*-
  2. * dtlk.c - DoubleTalk PC driver for Linux
  3. *
  4. * Original author: Chris Pallotta <chris@allmedia.com>
  5. * Current maintainer: Jim Van Zandt <jrv@vanzandt.mv.com>
  6. *
  7. * 2000-03-18 Jim Van Zandt: Fix polling.
  8. * Eliminate dtlk_timer_active flag and separate dtlk_stop_timer
  9. * function. Don't restart timer in dtlk_timer_tick. Restart timer
  10. * in dtlk_poll after every poll. dtlk_poll returns mask (duh).
  11. * Eliminate unused function dtlk_write_byte. Misc. code cleanups.
  12. */
  13. /* This driver is for the DoubleTalk PC, a speech synthesizer
  14. manufactured by RC Systems (http://www.rcsys.com/). It was written
  15. based on documentation in their User's Manual file and Developer's
  16. Tools disk.
  17. The DoubleTalk PC contains four voice synthesizers: text-to-speech
  18. (TTS), linear predictive coding (LPC), PCM/ADPCM, and CVSD. It
  19. also has a tone generator. Output data for LPC are written to the
  20. LPC port, and output data for the other modes are written to the
  21. TTS port.
  22. Two kinds of data can be read from the DoubleTalk: status
  23. information (in response to the "\001?" interrogation command) is
  24. read from the TTS port, and index markers (which mark the progress
  25. of the speech) are read from the LPC port. Not all models of the
  26. DoubleTalk PC implement index markers. Both the TTS and LPC ports
  27. can also display status flags.
  28. The DoubleTalk PC generates no interrupts.
  29. These characteristics are mapped into the Unix stream I/O model as
  30. follows:
  31. "write" sends bytes to the TTS port. It is the responsibility of
  32. the user program to switch modes among TTS, PCM/ADPCM, and CVSD.
  33. This driver was written for use with the text-to-speech
  34. synthesizer. If LPC output is needed some day, other minor device
  35. numbers can be used to select among output modes.
  36. "read" gets index markers from the LPC port. If the device does
  37. not implement index markers, the read will fail with error EINVAL.
  38. Status information is available using the DTLK_INTERROGATE ioctl.
  39. */
  40. #include <linux/module.h>
  41. #define KERNEL
  42. #include <linux/types.h>
  43. #include <linux/fs.h>
  44. #include <linux/mm.h>
  45. #include <linux/errno.h> /* for -EBUSY */
  46. #include <linux/ioport.h> /* for request_region */
  47. #include <linux/delay.h> /* for loops_per_jiffy */
  48. #include <asm/io.h> /* for inb_p, outb_p, inb, outb, etc. */
  49. #include <asm/uaccess.h> /* for get_user, etc. */
  50. #include <linux/wait.h> /* for wait_queue */
  51. #include <linux/init.h> /* for __init, module_{init,exit} */
  52. #include <linux/poll.h> /* for POLLIN, etc. */
  53. #include <linux/dtlk.h> /* local header file for DoubleTalk values */
  54. #include <linux/smp_lock.h>
  55. #ifdef TRACING
  56. #define TRACE_TEXT(str) printk(str);
  57. #define TRACE_RET printk(")")
  58. #else /* !TRACING */
  59. #define TRACE_TEXT(str) ((void) 0)
  60. #define TRACE_RET ((void) 0)
  61. #endif /* TRACING */
  62. static void dtlk_timer_tick(unsigned long data);
  63. static int dtlk_major;
  64. static int dtlk_port_lpc;
  65. static int dtlk_port_tts;
  66. static int dtlk_busy;
  67. static int dtlk_has_indexing;
  68. static unsigned int dtlk_portlist[] =
  69. {0x25e, 0x29e, 0x2de, 0x31e, 0x35e, 0x39e, 0};
  70. static wait_queue_head_t dtlk_process_list;
  71. static DEFINE_TIMER(dtlk_timer, dtlk_timer_tick, 0, 0);
  72. /* prototypes for file_operations struct */
  73. static ssize_t dtlk_read(struct file *, char __user *,
  74. size_t nbytes, loff_t * ppos);
  75. static ssize_t dtlk_write(struct file *, const char __user *,
  76. size_t nbytes, loff_t * ppos);
  77. static unsigned int dtlk_poll(struct file *, poll_table *);
  78. static int dtlk_open(struct inode *, struct file *);
  79. static int dtlk_release(struct inode *, struct file *);
  80. static int dtlk_ioctl(struct inode *inode, struct file *file,
  81. unsigned int cmd, unsigned long arg);
  82. static const struct file_operations dtlk_fops =
  83. {
  84. .owner = THIS_MODULE,
  85. .read = dtlk_read,
  86. .write = dtlk_write,
  87. .poll = dtlk_poll,
  88. .ioctl = dtlk_ioctl,
  89. .open = dtlk_open,
  90. .release = dtlk_release,
  91. };
  92. /* local prototypes */
  93. static int dtlk_dev_probe(void);
  94. static struct dtlk_settings *dtlk_interrogate(void);
  95. static int dtlk_readable(void);
  96. static char dtlk_read_lpc(void);
  97. static char dtlk_read_tts(void);
  98. static int dtlk_writeable(void);
  99. static char dtlk_write_bytes(const char *buf, int n);
  100. static char dtlk_write_tts(char);
  101. /*
  102. static void dtlk_handle_error(char, char, unsigned int);
  103. */
  104. static ssize_t dtlk_read(struct file *file, char __user *buf,
  105. size_t count, loff_t * ppos)
  106. {
  107. unsigned int minor = iminor(file->f_path.dentry->d_inode);
  108. char ch;
  109. int i = 0, retries;
  110. TRACE_TEXT("(dtlk_read");
  111. /* printk("DoubleTalk PC - dtlk_read()\n"); */
  112. if (minor != DTLK_MINOR || !dtlk_has_indexing)
  113. return -EINVAL;
  114. for (retries = 0; retries < loops_per_jiffy; retries++) {
  115. while (i < count && dtlk_readable()) {
  116. ch = dtlk_read_lpc();
  117. /* printk("dtlk_read() reads 0x%02x\n", ch); */
  118. if (put_user(ch, buf++))
  119. return -EFAULT;
  120. i++;
  121. }
  122. if (i)
  123. return i;
  124. if (file->f_flags & O_NONBLOCK)
  125. break;
  126. msleep_interruptible(100);
  127. }
  128. if (retries == loops_per_jiffy)
  129. printk(KERN_ERR "dtlk_read times out\n");
  130. TRACE_RET;
  131. return -EAGAIN;
  132. }
  133. static ssize_t dtlk_write(struct file *file, const char __user *buf,
  134. size_t count, loff_t * ppos)
  135. {
  136. int i = 0, retries = 0, ch;
  137. TRACE_TEXT("(dtlk_write");
  138. #ifdef TRACING
  139. printk(" \"");
  140. {
  141. int i, ch;
  142. for (i = 0; i < count; i++) {
  143. if (get_user(ch, buf + i))
  144. return -EFAULT;
  145. if (' ' <= ch && ch <= '~')
  146. printk("%c", ch);
  147. else
  148. printk("\\%03o", ch);
  149. }
  150. printk("\"");
  151. }
  152. #endif
  153. if (iminor(file->f_path.dentry->d_inode) != DTLK_MINOR)
  154. return -EINVAL;
  155. while (1) {
  156. while (i < count && !get_user(ch, buf) &&
  157. (ch == DTLK_CLEAR || dtlk_writeable())) {
  158. dtlk_write_tts(ch);
  159. buf++;
  160. i++;
  161. if (i % 5 == 0)
  162. /* We yield our time until scheduled
  163. again. This reduces the transfer
  164. rate to 500 bytes/sec, but that's
  165. still enough to keep up with the
  166. speech synthesizer. */
  167. msleep_interruptible(1);
  168. else {
  169. /* the RDY bit goes zero 2-3 usec
  170. after writing, and goes 1 again
  171. 180-190 usec later. Here, we wait
  172. up to 250 usec for the RDY bit to
  173. go nonzero. */
  174. for (retries = 0;
  175. retries < loops_per_jiffy / (4000/HZ);
  176. retries++)
  177. if (inb_p(dtlk_port_tts) &
  178. TTS_WRITABLE)
  179. break;
  180. }
  181. retries = 0;
  182. }
  183. if (i == count)
  184. return i;
  185. if (file->f_flags & O_NONBLOCK)
  186. break;
  187. msleep_interruptible(1);
  188. if (++retries > 10 * HZ) { /* wait no more than 10 sec
  189. from last write */
  190. printk("dtlk: write timeout. "
  191. "inb_p(dtlk_port_tts) = 0x%02x\n",
  192. inb_p(dtlk_port_tts));
  193. TRACE_RET;
  194. return -EBUSY;
  195. }
  196. }
  197. TRACE_RET;
  198. return -EAGAIN;
  199. }
  200. static unsigned int dtlk_poll(struct file *file, poll_table * wait)
  201. {
  202. int mask = 0;
  203. unsigned long expires;
  204. TRACE_TEXT(" dtlk_poll");
  205. /*
  206. static long int j;
  207. printk(".");
  208. printk("<%ld>", jiffies-j);
  209. j=jiffies;
  210. */
  211. poll_wait(file, &dtlk_process_list, wait);
  212. if (dtlk_has_indexing && dtlk_readable()) {
  213. del_timer(&dtlk_timer);
  214. mask = POLLIN | POLLRDNORM;
  215. }
  216. if (dtlk_writeable()) {
  217. del_timer(&dtlk_timer);
  218. mask |= POLLOUT | POLLWRNORM;
  219. }
  220. /* there are no exception conditions */
  221. /* There won't be any interrupts, so we set a timer instead. */
  222. expires = jiffies + 3*HZ / 100;
  223. mod_timer(&dtlk_timer, expires);
  224. return mask;
  225. }
  226. static void dtlk_timer_tick(unsigned long data)
  227. {
  228. TRACE_TEXT(" dtlk_timer_tick");
  229. wake_up_interruptible(&dtlk_process_list);
  230. }
  231. static int dtlk_ioctl(struct inode *inode,
  232. struct file *file,
  233. unsigned int cmd,
  234. unsigned long arg)
  235. {
  236. char __user *argp = (char __user *)arg;
  237. struct dtlk_settings *sp;
  238. char portval;
  239. TRACE_TEXT(" dtlk_ioctl");
  240. switch (cmd) {
  241. case DTLK_INTERROGATE:
  242. sp = dtlk_interrogate();
  243. if (copy_to_user(argp, sp, sizeof(struct dtlk_settings)))
  244. return -EINVAL;
  245. return 0;
  246. case DTLK_STATUS:
  247. portval = inb_p(dtlk_port_tts);
  248. return put_user(portval, argp);
  249. default:
  250. return -EINVAL;
  251. }
  252. }
  253. static int dtlk_open(struct inode *inode, struct file *file)
  254. {
  255. TRACE_TEXT("(dtlk_open");
  256. nonseekable_open(inode, file);
  257. switch (iminor(inode)) {
  258. case DTLK_MINOR:
  259. if (dtlk_busy)
  260. return -EBUSY;
  261. return nonseekable_open(inode, file);
  262. default:
  263. return -ENXIO;
  264. }
  265. }
  266. static int dtlk_release(struct inode *inode, struct file *file)
  267. {
  268. TRACE_TEXT("(dtlk_release");
  269. switch (iminor(inode)) {
  270. case DTLK_MINOR:
  271. break;
  272. default:
  273. break;
  274. }
  275. TRACE_RET;
  276. del_timer_sync(&dtlk_timer);
  277. return 0;
  278. }
  279. static int __init dtlk_init(void)
  280. {
  281. dtlk_port_lpc = 0;
  282. dtlk_port_tts = 0;
  283. dtlk_busy = 0;
  284. dtlk_major = register_chrdev(0, "dtlk", &dtlk_fops);
  285. if (dtlk_major == 0) {
  286. printk(KERN_ERR "DoubleTalk PC - cannot register device\n");
  287. return 0;
  288. }
  289. if (dtlk_dev_probe() == 0)
  290. printk(", MAJOR %d\n", dtlk_major);
  291. init_waitqueue_head(&dtlk_process_list);
  292. return 0;
  293. }
  294. static void __exit dtlk_cleanup (void)
  295. {
  296. dtlk_write_bytes("goodbye", 8);
  297. msleep_interruptible(500); /* nap 0.50 sec but
  298. could be awakened
  299. earlier by
  300. signals... */
  301. dtlk_write_tts(DTLK_CLEAR);
  302. unregister_chrdev(dtlk_major, "dtlk");
  303. release_region(dtlk_port_lpc, DTLK_IO_EXTENT);
  304. }
  305. module_init(dtlk_init);
  306. module_exit(dtlk_cleanup);
  307. /* ------------------------------------------------------------------------ */
  308. static int dtlk_readable(void)
  309. {
  310. #ifdef TRACING
  311. printk(" dtlk_readable=%u@%u", inb_p(dtlk_port_lpc) != 0x7f, jiffies);
  312. #endif
  313. return inb_p(dtlk_port_lpc) != 0x7f;
  314. }
  315. static int dtlk_writeable(void)
  316. {
  317. /* TRACE_TEXT(" dtlk_writeable"); */
  318. #ifdef TRACINGMORE
  319. printk(" dtlk_writeable=%u", (inb_p(dtlk_port_tts) & TTS_WRITABLE)!=0);
  320. #endif
  321. return inb_p(dtlk_port_tts) & TTS_WRITABLE;
  322. }
  323. static int __init dtlk_dev_probe(void)
  324. {
  325. unsigned int testval = 0;
  326. int i = 0;
  327. struct dtlk_settings *sp;
  328. if (dtlk_port_lpc | dtlk_port_tts)
  329. return -EBUSY;
  330. for (i = 0; dtlk_portlist[i]; i++) {
  331. #if 0
  332. printk("DoubleTalk PC - Port %03x = %04x\n",
  333. dtlk_portlist[i], (testval = inw_p(dtlk_portlist[i])));
  334. #endif
  335. if (!request_region(dtlk_portlist[i], DTLK_IO_EXTENT,
  336. "dtlk"))
  337. continue;
  338. testval = inw_p(dtlk_portlist[i]);
  339. if ((testval &= 0xfbff) == 0x107f) {
  340. dtlk_port_lpc = dtlk_portlist[i];
  341. dtlk_port_tts = dtlk_port_lpc + 1;
  342. sp = dtlk_interrogate();
  343. printk("DoubleTalk PC at %03x-%03x, "
  344. "ROM version %s, serial number %u",
  345. dtlk_portlist[i], dtlk_portlist[i] +
  346. DTLK_IO_EXTENT - 1,
  347. sp->rom_version, sp->serial_number);
  348. /* put LPC port into known state, so
  349. dtlk_readable() gives valid result */
  350. outb_p(0xff, dtlk_port_lpc);
  351. /* INIT string and index marker */
  352. dtlk_write_bytes("\036\1@\0\0012I\r", 8);
  353. /* posting an index takes 18 msec. Here, we
  354. wait up to 100 msec to see whether it
  355. appears. */
  356. msleep_interruptible(100);
  357. dtlk_has_indexing = dtlk_readable();
  358. #ifdef TRACING
  359. printk(", indexing %d\n", dtlk_has_indexing);
  360. #endif
  361. #ifdef INSCOPE
  362. {
  363. /* This macro records ten samples read from the LPC port, for later display */
  364. #define LOOK \
  365. for (i = 0; i < 10; i++) \
  366. { \
  367. buffer[b++] = inb_p(dtlk_port_lpc); \
  368. __delay(loops_per_jiffy/(1000000/HZ)); \
  369. }
  370. char buffer[1000];
  371. int b = 0, i, j;
  372. LOOK
  373. outb_p(0xff, dtlk_port_lpc);
  374. buffer[b++] = 0;
  375. LOOK
  376. dtlk_write_bytes("\0012I\r", 4);
  377. buffer[b++] = 0;
  378. __delay(50 * loops_per_jiffy / (1000/HZ));
  379. outb_p(0xff, dtlk_port_lpc);
  380. buffer[b++] = 0;
  381. LOOK
  382. printk("\n");
  383. for (j = 0; j < b; j++)
  384. printk(" %02x", buffer[j]);
  385. printk("\n");
  386. }
  387. #endif /* INSCOPE */
  388. #ifdef OUTSCOPE
  389. {
  390. /* This macro records ten samples read from the TTS port, for later display */
  391. #define LOOK \
  392. for (i = 0; i < 10; i++) \
  393. { \
  394. buffer[b++] = inb_p(dtlk_port_tts); \
  395. __delay(loops_per_jiffy/(1000000/HZ)); /* 1 us */ \
  396. }
  397. char buffer[1000];
  398. int b = 0, i, j;
  399. mdelay(10); /* 10 ms */
  400. LOOK
  401. outb_p(0x03, dtlk_port_tts);
  402. buffer[b++] = 0;
  403. LOOK
  404. LOOK
  405. printk("\n");
  406. for (j = 0; j < b; j++)
  407. printk(" %02x", buffer[j]);
  408. printk("\n");
  409. }
  410. #endif /* OUTSCOPE */
  411. dtlk_write_bytes("Double Talk found", 18);
  412. return 0;
  413. }
  414. release_region(dtlk_portlist[i], DTLK_IO_EXTENT);
  415. }
  416. printk(KERN_INFO "DoubleTalk PC - not found\n");
  417. return -ENODEV;
  418. }
  419. /*
  420. static void dtlk_handle_error(char op, char rc, unsigned int minor)
  421. {
  422. printk(KERN_INFO"\nDoubleTalk PC - MINOR: %d, OPCODE: %d, ERROR: %d\n",
  423. minor, op, rc);
  424. return;
  425. }
  426. */
  427. /* interrogate the DoubleTalk PC and return its settings */
  428. static struct dtlk_settings *dtlk_interrogate(void)
  429. {
  430. unsigned char *t;
  431. static char buf[sizeof(struct dtlk_settings) + 1];
  432. int total, i;
  433. static struct dtlk_settings status;
  434. TRACE_TEXT("(dtlk_interrogate");
  435. dtlk_write_bytes("\030\001?", 3);
  436. for (total = 0, i = 0; i < 50; i++) {
  437. buf[total] = dtlk_read_tts();
  438. if (total > 2 && buf[total] == 0x7f)
  439. break;
  440. if (total < sizeof(struct dtlk_settings))
  441. total++;
  442. }
  443. /*
  444. if (i==50) printk("interrogate() read overrun\n");
  445. for (i=0; i<sizeof(buf); i++)
  446. printk(" %02x", buf[i]);
  447. printk("\n");
  448. */
  449. t = buf;
  450. status.serial_number = t[0] + t[1] * 256; /* serial number is
  451. little endian */
  452. t += 2;
  453. i = 0;
  454. while (*t != '\r') {
  455. status.rom_version[i] = *t;
  456. if (i < sizeof(status.rom_version) - 1)
  457. i++;
  458. t++;
  459. }
  460. status.rom_version[i] = 0;
  461. t++;
  462. status.mode = *t++;
  463. status.punc_level = *t++;
  464. status.formant_freq = *t++;
  465. status.pitch = *t++;
  466. status.speed = *t++;
  467. status.volume = *t++;
  468. status.tone = *t++;
  469. status.expression = *t++;
  470. status.ext_dict_loaded = *t++;
  471. status.ext_dict_status = *t++;
  472. status.free_ram = *t++;
  473. status.articulation = *t++;
  474. status.reverb = *t++;
  475. status.eob = *t++;
  476. status.has_indexing = dtlk_has_indexing;
  477. TRACE_RET;
  478. return &status;
  479. }
  480. static char dtlk_read_tts(void)
  481. {
  482. int portval, retries = 0;
  483. char ch;
  484. TRACE_TEXT("(dtlk_read_tts");
  485. /* verify DT is ready, read char, wait for ACK */
  486. do {
  487. portval = inb_p(dtlk_port_tts);
  488. } while ((portval & TTS_READABLE) == 0 &&
  489. retries++ < DTLK_MAX_RETRIES);
  490. if (retries == DTLK_MAX_RETRIES)
  491. printk(KERN_ERR "dtlk_read_tts() timeout\n");
  492. ch = inb_p(dtlk_port_tts); /* input from TTS port */
  493. ch &= 0x7f;
  494. outb_p(ch, dtlk_port_tts);
  495. retries = 0;
  496. do {
  497. portval = inb_p(dtlk_port_tts);
  498. } while ((portval & TTS_READABLE) != 0 &&
  499. retries++ < DTLK_MAX_RETRIES);
  500. if (retries == DTLK_MAX_RETRIES)
  501. printk(KERN_ERR "dtlk_read_tts() timeout\n");
  502. TRACE_RET;
  503. return ch;
  504. }
  505. static char dtlk_read_lpc(void)
  506. {
  507. int retries = 0;
  508. char ch;
  509. TRACE_TEXT("(dtlk_read_lpc");
  510. /* no need to test -- this is only called when the port is readable */
  511. ch = inb_p(dtlk_port_lpc); /* input from LPC port */
  512. outb_p(0xff, dtlk_port_lpc);
  513. /* acknowledging a read takes 3-4
  514. usec. Here, we wait up to 20 usec
  515. for the acknowledgement */
  516. retries = (loops_per_jiffy * 20) / (1000000/HZ);
  517. while (inb_p(dtlk_port_lpc) != 0x7f && --retries > 0);
  518. if (retries == 0)
  519. printk(KERN_ERR "dtlk_read_lpc() timeout\n");
  520. TRACE_RET;
  521. return ch;
  522. }
  523. /* write n bytes to tts port */
  524. static char dtlk_write_bytes(const char *buf, int n)
  525. {
  526. char val = 0;
  527. /* printk("dtlk_write_bytes(\"%-*s\", %d)\n", n, buf, n); */
  528. TRACE_TEXT("(dtlk_write_bytes");
  529. while (n-- > 0)
  530. val = dtlk_write_tts(*buf++);
  531. TRACE_RET;
  532. return val;
  533. }
  534. static char dtlk_write_tts(char ch)
  535. {
  536. int retries = 0;
  537. #ifdef TRACINGMORE
  538. printk(" dtlk_write_tts(");
  539. if (' ' <= ch && ch <= '~')
  540. printk("'%c'", ch);
  541. else
  542. printk("0x%02x", ch);
  543. #endif
  544. if (ch != DTLK_CLEAR) /* no flow control for CLEAR command */
  545. while ((inb_p(dtlk_port_tts) & TTS_WRITABLE) == 0 &&
  546. retries++ < DTLK_MAX_RETRIES) /* DT ready? */
  547. ;
  548. if (retries == DTLK_MAX_RETRIES)
  549. printk(KERN_ERR "dtlk_write_tts() timeout\n");
  550. outb_p(ch, dtlk_port_tts); /* output to TTS port */
  551. /* the RDY bit goes zero 2-3 usec after writing, and goes
  552. 1 again 180-190 usec later. Here, we wait up to 10
  553. usec for the RDY bit to go zero. */
  554. for (retries = 0; retries < loops_per_jiffy / (100000/HZ); retries++)
  555. if ((inb_p(dtlk_port_tts) & TTS_WRITABLE) == 0)
  556. break;
  557. #ifdef TRACINGMORE
  558. printk(")\n");
  559. #endif
  560. return 0;
  561. }
  562. MODULE_LICENSE("GPL");