pcxhr.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /*
  2. * Driver for Digigram pcxhr compatible soundcards
  3. *
  4. * main file with alsa callbacks
  5. *
  6. * Copyright (c) 2004 by Digigram <alsa@digigram.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <sound/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/slab.h>
  26. #include <linux/pci.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/delay.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/mutex.h>
  31. #include <sound/core.h>
  32. #include <sound/initval.h>
  33. #include <sound/info.h>
  34. #include <sound/control.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include "pcxhr.h"
  38. #include "pcxhr_mixer.h"
  39. #include "pcxhr_hwdep.h"
  40. #include "pcxhr_core.h"
  41. #define DRIVER_NAME "pcxhr"
  42. MODULE_AUTHOR("Markus Bollinger <bollinger@digigram.com>");
  43. MODULE_DESCRIPTION("Digigram " DRIVER_NAME " " PCXHR_DRIVER_VERSION_STRING);
  44. MODULE_LICENSE("GPL");
  45. MODULE_SUPPORTED_DEVICE("{{Digigram," DRIVER_NAME "}}");
  46. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  47. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  48. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  49. static int mono[SNDRV_CARDS]; /* capture in mono only */
  50. module_param_array(index, int, NULL, 0444);
  51. MODULE_PARM_DESC(index, "Index value for Digigram " DRIVER_NAME " soundcard");
  52. module_param_array(id, charp, NULL, 0444);
  53. MODULE_PARM_DESC(id, "ID string for Digigram " DRIVER_NAME " soundcard");
  54. module_param_array(enable, bool, NULL, 0444);
  55. MODULE_PARM_DESC(enable, "Enable Digigram " DRIVER_NAME " soundcard");
  56. module_param_array(mono, bool, NULL, 0444);
  57. MODULE_PARM_DESC(mono, "Mono capture mode (default is stereo)");
  58. enum {
  59. PCI_ID_VX882HR,
  60. PCI_ID_PCX882HR,
  61. PCI_ID_VX881HR,
  62. PCI_ID_PCX881HR,
  63. PCI_ID_PCX1222HR,
  64. PCI_ID_PCX1221HR,
  65. PCI_ID_LAST
  66. };
  67. static struct pci_device_id pcxhr_ids[] = {
  68. { 0x10b5, 0x9656, 0x1369, 0xb001, 0, 0, PCI_ID_VX882HR, }, /* VX882HR */
  69. { 0x10b5, 0x9656, 0x1369, 0xb101, 0, 0, PCI_ID_PCX882HR, }, /* PCX882HR */
  70. { 0x10b5, 0x9656, 0x1369, 0xb201, 0, 0, PCI_ID_VX881HR, }, /* VX881HR */
  71. { 0x10b5, 0x9656, 0x1369, 0xb301, 0, 0, PCI_ID_PCX881HR, }, /* PCX881HR */
  72. { 0x10b5, 0x9656, 0x1369, 0xb501, 0, 0, PCI_ID_PCX1222HR, }, /* PCX1222HR */
  73. { 0x10b5, 0x9656, 0x1369, 0xb701, 0, 0, PCI_ID_PCX1221HR, }, /* PCX1221HR */
  74. { 0, }
  75. };
  76. MODULE_DEVICE_TABLE(pci, pcxhr_ids);
  77. struct board_parameters {
  78. char* board_name;
  79. short playback_chips;
  80. short capture_chips;
  81. short firmware_num;
  82. };
  83. static struct board_parameters pcxhr_board_params[] = {
  84. [PCI_ID_VX882HR] = { "VX882HR", 4, 4, 41, },
  85. [PCI_ID_PCX882HR] = { "PCX882HR", 4, 4, 41, },
  86. [PCI_ID_VX881HR] = { "VX881HR", 4, 4, 41, },
  87. [PCI_ID_PCX881HR] = { "PCX881HR", 4, 4, 41, },
  88. [PCI_ID_PCX1222HR] = { "PCX1222HR", 6, 1, 42, },
  89. [PCI_ID_PCX1221HR] = { "PCX1221HR", 6, 1, 42, },
  90. };
  91. static int pcxhr_pll_freq_register(unsigned int freq, unsigned int* pllreg,
  92. unsigned int* realfreq)
  93. {
  94. unsigned int reg;
  95. if (freq < 6900 || freq > 110250)
  96. return -EINVAL;
  97. reg = (28224000 * 10) / freq;
  98. reg = (reg + 5) / 10;
  99. if (reg < 0x200)
  100. *pllreg = reg + 0x800;
  101. else if (reg < 0x400)
  102. *pllreg = reg & 0x1ff;
  103. else if (reg < 0x800) {
  104. *pllreg = ((reg >> 1) & 0x1ff) + 0x200;
  105. reg &= ~1;
  106. } else {
  107. *pllreg = ((reg >> 2) & 0x1ff) + 0x400;
  108. reg &= ~3;
  109. }
  110. if (realfreq)
  111. *realfreq = ((28224000 * 10) / reg + 5) / 10;
  112. return 0;
  113. }
  114. #define PCXHR_FREQ_REG_MASK 0x1f
  115. #define PCXHR_FREQ_QUARTZ_48000 0x00
  116. #define PCXHR_FREQ_QUARTZ_24000 0x01
  117. #define PCXHR_FREQ_QUARTZ_12000 0x09
  118. #define PCXHR_FREQ_QUARTZ_32000 0x08
  119. #define PCXHR_FREQ_QUARTZ_16000 0x04
  120. #define PCXHR_FREQ_QUARTZ_8000 0x0c
  121. #define PCXHR_FREQ_QUARTZ_44100 0x02
  122. #define PCXHR_FREQ_QUARTZ_22050 0x0a
  123. #define PCXHR_FREQ_QUARTZ_11025 0x06
  124. #define PCXHR_FREQ_PLL 0x05
  125. #define PCXHR_FREQ_QUARTZ_192000 0x10
  126. #define PCXHR_FREQ_QUARTZ_96000 0x18
  127. #define PCXHR_FREQ_QUARTZ_176400 0x14
  128. #define PCXHR_FREQ_QUARTZ_88200 0x1c
  129. #define PCXHR_FREQ_QUARTZ_128000 0x12
  130. #define PCXHR_FREQ_QUARTZ_64000 0x1a
  131. #define PCXHR_FREQ_WORD_CLOCK 0x0f
  132. #define PCXHR_FREQ_SYNC_AES 0x0e
  133. #define PCXHR_FREQ_AES_1 0x07
  134. #define PCXHR_FREQ_AES_2 0x0b
  135. #define PCXHR_FREQ_AES_3 0x03
  136. #define PCXHR_FREQ_AES_4 0x0d
  137. #define PCXHR_MODIFY_CLOCK_S_BIT 0x04
  138. #define PCXHR_IRQ_TIMER_FREQ 92000
  139. #define PCXHR_IRQ_TIMER_PERIOD 48
  140. static int pcxhr_get_clock_reg(struct pcxhr_mgr *mgr, unsigned int rate,
  141. unsigned int *reg, unsigned int *freq)
  142. {
  143. unsigned int val, realfreq, pllreg;
  144. struct pcxhr_rmh rmh;
  145. int err;
  146. realfreq = rate;
  147. switch (mgr->use_clock_type) {
  148. case PCXHR_CLOCK_TYPE_INTERNAL : /* clock by quartz or pll */
  149. switch (rate) {
  150. case 48000 : val = PCXHR_FREQ_QUARTZ_48000; break;
  151. case 24000 : val = PCXHR_FREQ_QUARTZ_24000; break;
  152. case 12000 : val = PCXHR_FREQ_QUARTZ_12000; break;
  153. case 32000 : val = PCXHR_FREQ_QUARTZ_32000; break;
  154. case 16000 : val = PCXHR_FREQ_QUARTZ_16000; break;
  155. case 8000 : val = PCXHR_FREQ_QUARTZ_8000; break;
  156. case 44100 : val = PCXHR_FREQ_QUARTZ_44100; break;
  157. case 22050 : val = PCXHR_FREQ_QUARTZ_22050; break;
  158. case 11025 : val = PCXHR_FREQ_QUARTZ_11025; break;
  159. case 192000 : val = PCXHR_FREQ_QUARTZ_192000; break;
  160. case 96000 : val = PCXHR_FREQ_QUARTZ_96000; break;
  161. case 176400 : val = PCXHR_FREQ_QUARTZ_176400; break;
  162. case 88200 : val = PCXHR_FREQ_QUARTZ_88200; break;
  163. case 128000 : val = PCXHR_FREQ_QUARTZ_128000; break;
  164. case 64000 : val = PCXHR_FREQ_QUARTZ_64000; break;
  165. default :
  166. val = PCXHR_FREQ_PLL;
  167. /* get the value for the pll register */
  168. err = pcxhr_pll_freq_register(rate, &pllreg, &realfreq);
  169. if (err)
  170. return err;
  171. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
  172. rmh.cmd[0] |= IO_NUM_REG_GENCLK;
  173. rmh.cmd[1] = pllreg & MASK_DSP_WORD;
  174. rmh.cmd[2] = pllreg >> 24;
  175. rmh.cmd_len = 3;
  176. err = pcxhr_send_msg(mgr, &rmh);
  177. if (err < 0) {
  178. snd_printk(KERN_ERR
  179. "error CMD_ACCESS_IO_WRITE for PLL register : %x!\n",
  180. err );
  181. return err;
  182. }
  183. }
  184. break;
  185. case PCXHR_CLOCK_TYPE_WORD_CLOCK : val = PCXHR_FREQ_WORD_CLOCK; break;
  186. case PCXHR_CLOCK_TYPE_AES_SYNC : val = PCXHR_FREQ_SYNC_AES; break;
  187. case PCXHR_CLOCK_TYPE_AES_1 : val = PCXHR_FREQ_AES_1; break;
  188. case PCXHR_CLOCK_TYPE_AES_2 : val = PCXHR_FREQ_AES_2; break;
  189. case PCXHR_CLOCK_TYPE_AES_3 : val = PCXHR_FREQ_AES_3; break;
  190. case PCXHR_CLOCK_TYPE_AES_4 : val = PCXHR_FREQ_AES_4; break;
  191. default : return -EINVAL;
  192. }
  193. *reg = val;
  194. *freq = realfreq;
  195. return 0;
  196. }
  197. int pcxhr_set_clock(struct pcxhr_mgr *mgr, unsigned int rate)
  198. {
  199. unsigned int val, realfreq, speed;
  200. struct pcxhr_rmh rmh;
  201. int err, changed;
  202. if (rate == 0)
  203. return 0; /* nothing to do */
  204. err = pcxhr_get_clock_reg(mgr, rate, &val, &realfreq);
  205. if (err)
  206. return err;
  207. /* codec speed modes */
  208. if (rate < 55000)
  209. speed = 0; /* single speed */
  210. else if (rate < 100000)
  211. speed = 1; /* dual speed */
  212. else
  213. speed = 2; /* quad speed */
  214. if (mgr->codec_speed != speed) {
  215. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE); /* mute outputs */
  216. rmh.cmd[0] |= IO_NUM_REG_MUTE_OUT;
  217. err = pcxhr_send_msg(mgr, &rmh);
  218. if (err)
  219. return err;
  220. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE); /* set speed ratio */
  221. rmh.cmd[0] |= IO_NUM_SPEED_RATIO;
  222. rmh.cmd[1] = speed;
  223. rmh.cmd_len = 2;
  224. err = pcxhr_send_msg(mgr, &rmh);
  225. if (err)
  226. return err;
  227. }
  228. /* set the new frequency */
  229. snd_printdd("clock register : set %x\n", val);
  230. err = pcxhr_write_io_num_reg_cont(mgr, PCXHR_FREQ_REG_MASK, val, &changed);
  231. if (err)
  232. return err;
  233. mgr->sample_rate_real = realfreq;
  234. mgr->cur_clock_type = mgr->use_clock_type;
  235. /* unmute after codec speed modes */
  236. if (mgr->codec_speed != speed) {
  237. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ); /* unmute outputs */
  238. rmh.cmd[0] |= IO_NUM_REG_MUTE_OUT;
  239. err = pcxhr_send_msg(mgr, &rmh);
  240. if (err)
  241. return err;
  242. mgr->codec_speed = speed; /* save new codec speed */
  243. }
  244. if (changed) {
  245. pcxhr_init_rmh(&rmh, CMD_MODIFY_CLOCK);
  246. rmh.cmd[0] |= PCXHR_MODIFY_CLOCK_S_BIT; /* resync fifos */
  247. if (rate < PCXHR_IRQ_TIMER_FREQ)
  248. rmh.cmd[1] = PCXHR_IRQ_TIMER_PERIOD;
  249. else
  250. rmh.cmd[1] = PCXHR_IRQ_TIMER_PERIOD * 2;
  251. rmh.cmd[2] = rate;
  252. rmh.cmd_len = 3;
  253. err = pcxhr_send_msg(mgr, &rmh);
  254. if (err)
  255. return err;
  256. }
  257. snd_printdd("pcxhr_set_clock to %dHz (realfreq=%d)\n", rate, realfreq);
  258. return 0;
  259. }
  260. int pcxhr_get_external_clock(struct pcxhr_mgr *mgr, enum pcxhr_clock_type clock_type,
  261. int *sample_rate)
  262. {
  263. struct pcxhr_rmh rmh;
  264. unsigned char reg;
  265. int err, rate;
  266. switch (clock_type) {
  267. case PCXHR_CLOCK_TYPE_WORD_CLOCK : reg = REG_STATUS_WORD_CLOCK; break;
  268. case PCXHR_CLOCK_TYPE_AES_SYNC : reg = REG_STATUS_AES_SYNC; break;
  269. case PCXHR_CLOCK_TYPE_AES_1 : reg = REG_STATUS_AES_1; break;
  270. case PCXHR_CLOCK_TYPE_AES_2 : reg = REG_STATUS_AES_2; break;
  271. case PCXHR_CLOCK_TYPE_AES_3 : reg = REG_STATUS_AES_3; break;
  272. case PCXHR_CLOCK_TYPE_AES_4 : reg = REG_STATUS_AES_4; break;
  273. default : return -EINVAL;
  274. }
  275. pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
  276. rmh.cmd_len = 2;
  277. rmh.cmd[0] |= IO_NUM_REG_STATUS;
  278. if (mgr->last_reg_stat != reg) {
  279. rmh.cmd[1] = reg;
  280. err = pcxhr_send_msg(mgr, &rmh);
  281. if (err)
  282. return err;
  283. udelay(100); /* wait minimum 2 sample_frames at 32kHz ! */
  284. mgr->last_reg_stat = reg;
  285. }
  286. rmh.cmd[1] = REG_STATUS_CURRENT;
  287. err = pcxhr_send_msg(mgr, &rmh);
  288. if (err)
  289. return err;
  290. switch (rmh.stat[1] & 0x0f) {
  291. case REG_STATUS_SYNC_32000 : rate = 32000; break;
  292. case REG_STATUS_SYNC_44100 : rate = 44100; break;
  293. case REG_STATUS_SYNC_48000 : rate = 48000; break;
  294. case REG_STATUS_SYNC_64000 : rate = 64000; break;
  295. case REG_STATUS_SYNC_88200 : rate = 88200; break;
  296. case REG_STATUS_SYNC_96000 : rate = 96000; break;
  297. case REG_STATUS_SYNC_128000 : rate = 128000; break;
  298. case REG_STATUS_SYNC_176400 : rate = 176400; break;
  299. case REG_STATUS_SYNC_192000 : rate = 192000; break;
  300. default: rate = 0;
  301. }
  302. snd_printdd("External clock is at %d Hz\n", rate);
  303. *sample_rate = rate;
  304. return 0;
  305. }
  306. /*
  307. * start or stop playback/capture substream
  308. */
  309. static int pcxhr_set_stream_state(struct pcxhr_stream *stream)
  310. {
  311. int err;
  312. struct snd_pcxhr *chip;
  313. struct pcxhr_rmh rmh;
  314. int stream_mask, start;
  315. if (stream->status == PCXHR_STREAM_STATUS_SCHEDULE_RUN)
  316. start = 1;
  317. else {
  318. if (stream->status != PCXHR_STREAM_STATUS_SCHEDULE_STOP) {
  319. snd_printk(KERN_ERR "ERROR pcxhr_set_stream_state CANNOT be stopped\n");
  320. return -EINVAL;
  321. }
  322. start = 0;
  323. }
  324. if (!stream->substream)
  325. return -EINVAL;
  326. stream->timer_abs_periods = 0;
  327. stream->timer_period_frag = 0; /* reset theoretical stream pos */
  328. stream->timer_buf_periods = 0;
  329. stream->timer_is_synced = 0;
  330. stream_mask = stream->pipe->is_capture ? 1 : 1<<stream->substream->number;
  331. pcxhr_init_rmh(&rmh, start ? CMD_START_STREAM : CMD_STOP_STREAM);
  332. pcxhr_set_pipe_cmd_params(&rmh, stream->pipe->is_capture,
  333. stream->pipe->first_audio, 0, stream_mask);
  334. chip = snd_pcm_substream_chip(stream->substream);
  335. err = pcxhr_send_msg(chip->mgr, &rmh);
  336. if (err)
  337. snd_printk(KERN_ERR "ERROR pcxhr_set_stream_state err=%x;\n", err);
  338. stream->status = start ? PCXHR_STREAM_STATUS_STARTED : PCXHR_STREAM_STATUS_STOPPED;
  339. return err;
  340. }
  341. #define HEADER_FMT_BASE_LIN 0xfed00000
  342. #define HEADER_FMT_BASE_FLOAT 0xfad00000
  343. #define HEADER_FMT_INTEL 0x00008000
  344. #define HEADER_FMT_24BITS 0x00004000
  345. #define HEADER_FMT_16BITS 0x00002000
  346. #define HEADER_FMT_UPTO11 0x00000200
  347. #define HEADER_FMT_UPTO32 0x00000100
  348. #define HEADER_FMT_MONO 0x00000080
  349. static int pcxhr_set_format(struct pcxhr_stream *stream)
  350. {
  351. int err, is_capture, sample_rate, stream_num;
  352. struct snd_pcxhr *chip;
  353. struct pcxhr_rmh rmh;
  354. unsigned int header;
  355. switch (stream->format) {
  356. case SNDRV_PCM_FORMAT_U8:
  357. header = HEADER_FMT_BASE_LIN;
  358. break;
  359. case SNDRV_PCM_FORMAT_S16_LE:
  360. header = HEADER_FMT_BASE_LIN | HEADER_FMT_16BITS | HEADER_FMT_INTEL;
  361. break;
  362. case SNDRV_PCM_FORMAT_S16_BE:
  363. header = HEADER_FMT_BASE_LIN | HEADER_FMT_16BITS;
  364. break;
  365. case SNDRV_PCM_FORMAT_S24_3LE:
  366. header = HEADER_FMT_BASE_LIN | HEADER_FMT_24BITS | HEADER_FMT_INTEL;
  367. break;
  368. case SNDRV_PCM_FORMAT_S24_3BE:
  369. header = HEADER_FMT_BASE_LIN | HEADER_FMT_24BITS;
  370. break;
  371. case SNDRV_PCM_FORMAT_FLOAT_LE:
  372. header = HEADER_FMT_BASE_FLOAT | HEADER_FMT_INTEL;
  373. break;
  374. default:
  375. snd_printk(KERN_ERR "error pcxhr_set_format() : unknown format\n");
  376. return -EINVAL;
  377. }
  378. chip = snd_pcm_substream_chip(stream->substream);
  379. sample_rate = chip->mgr->sample_rate;
  380. if (sample_rate <= 32000 && sample_rate !=0) {
  381. if (sample_rate <= 11025)
  382. header |= HEADER_FMT_UPTO11;
  383. else
  384. header |= HEADER_FMT_UPTO32;
  385. }
  386. if (stream->channels == 1)
  387. header |= HEADER_FMT_MONO;
  388. is_capture = stream->pipe->is_capture;
  389. stream_num = is_capture ? 0 : stream->substream->number;
  390. pcxhr_init_rmh(&rmh, is_capture ? CMD_FORMAT_STREAM_IN : CMD_FORMAT_STREAM_OUT);
  391. pcxhr_set_pipe_cmd_params(&rmh, is_capture, stream->pipe->first_audio, stream_num, 0);
  392. if (is_capture)
  393. rmh.cmd[0] |= 1<<12;
  394. rmh.cmd[1] = 0;
  395. rmh.cmd[2] = header >> 8;
  396. rmh.cmd[3] = (header & 0xff) << 16;
  397. rmh.cmd_len = 4;
  398. err = pcxhr_send_msg(chip->mgr, &rmh);
  399. if (err)
  400. snd_printk(KERN_ERR "ERROR pcxhr_set_format err=%x;\n", err);
  401. return err;
  402. }
  403. static int pcxhr_update_r_buffer(struct pcxhr_stream *stream)
  404. {
  405. int err, is_capture, stream_num;
  406. struct pcxhr_rmh rmh;
  407. struct snd_pcm_substream *subs = stream->substream;
  408. struct snd_pcxhr *chip = snd_pcm_substream_chip(subs);
  409. is_capture = (subs->stream == SNDRV_PCM_STREAM_CAPTURE);
  410. stream_num = is_capture ? 0 : subs->number;
  411. snd_printdd("pcxhr_update_r_buffer(pcm%c%d) : addr(%p) bytes(%zx) subs(%d)\n",
  412. is_capture ? 'c' : 'p',
  413. chip->chip_idx, (void*)subs->runtime->dma_addr,
  414. subs->runtime->dma_bytes, subs->number);
  415. pcxhr_init_rmh(&rmh, CMD_UPDATE_R_BUFFERS);
  416. pcxhr_set_pipe_cmd_params(&rmh, is_capture, stream->pipe->first_audio, stream_num, 0);
  417. snd_assert(subs->runtime->dma_bytes < 0x200000); /* max buffer size is 2 MByte */
  418. rmh.cmd[1] = subs->runtime->dma_bytes * 8; /* size in bits */
  419. rmh.cmd[2] = subs->runtime->dma_addr >> 24; /* most significant byte */
  420. rmh.cmd[2] |= 1<<19; /* this is a circular buffer */
  421. rmh.cmd[3] = subs->runtime->dma_addr & MASK_DSP_WORD; /* least 3 significant bytes */
  422. rmh.cmd_len = 4;
  423. err = pcxhr_send_msg(chip->mgr, &rmh);
  424. if (err)
  425. snd_printk(KERN_ERR "ERROR CMD_UPDATE_R_BUFFERS err=%x;\n", err);
  426. return err;
  427. }
  428. #if 0
  429. static int pcxhr_pipe_sample_count(struct pcxhr_stream *stream, snd_pcm_uframes_t *sample_count)
  430. {
  431. struct pcxhr_rmh rmh;
  432. int err;
  433. pcxhr_t *chip = snd_pcm_substream_chip(stream->substream);
  434. pcxhr_init_rmh(&rmh, CMD_PIPE_SAMPLE_COUNT);
  435. pcxhr_set_pipe_cmd_params(&rmh, stream->pipe->is_capture, 0, 0,
  436. 1<<stream->pipe->first_audio);
  437. err = pcxhr_send_msg(chip->mgr, &rmh);
  438. if (err == 0) {
  439. *sample_count = ((snd_pcm_uframes_t)rmh.stat[0]) << 24;
  440. *sample_count += (snd_pcm_uframes_t)rmh.stat[1];
  441. }
  442. snd_printdd("PIPE_SAMPLE_COUNT = %lx\n", *sample_count);
  443. return err;
  444. }
  445. #endif
  446. static inline int pcxhr_stream_scheduled_get_pipe(struct pcxhr_stream *stream,
  447. struct pcxhr_pipe **pipe)
  448. {
  449. if (stream->status == PCXHR_STREAM_STATUS_SCHEDULE_RUN) {
  450. *pipe = stream->pipe;
  451. return 1;
  452. }
  453. return 0;
  454. }
  455. static void pcxhr_trigger_tasklet(unsigned long arg)
  456. {
  457. unsigned long flags;
  458. int i, j, err;
  459. struct pcxhr_pipe *pipe;
  460. struct snd_pcxhr *chip;
  461. struct pcxhr_mgr *mgr = (struct pcxhr_mgr*)(arg);
  462. int capture_mask = 0;
  463. int playback_mask = 0;
  464. #ifdef CONFIG_SND_DEBUG_DETECT
  465. struct timeval my_tv1, my_tv2;
  466. do_gettimeofday(&my_tv1);
  467. #endif
  468. mutex_lock(&mgr->setup_mutex);
  469. /* check the pipes concerned and build pipe_array */
  470. for (i = 0; i < mgr->num_cards; i++) {
  471. chip = mgr->chip[i];
  472. for (j = 0; j < chip->nb_streams_capt; j++) {
  473. if (pcxhr_stream_scheduled_get_pipe(&chip->capture_stream[j], &pipe))
  474. capture_mask |= (1 << pipe->first_audio);
  475. }
  476. for (j = 0; j < chip->nb_streams_play; j++) {
  477. if (pcxhr_stream_scheduled_get_pipe(&chip->playback_stream[j], &pipe)) {
  478. playback_mask |= (1 << pipe->first_audio);
  479. break; /* add only once, as all playback streams of
  480. * one chip use the same pipe
  481. */
  482. }
  483. }
  484. }
  485. if (capture_mask == 0 && playback_mask == 0) {
  486. mutex_unlock(&mgr->setup_mutex);
  487. snd_printk(KERN_ERR "pcxhr_trigger_tasklet : no pipes\n");
  488. return;
  489. }
  490. snd_printdd("pcxhr_trigger_tasklet : playback_mask=%x capture_mask=%x\n",
  491. playback_mask, capture_mask);
  492. /* synchronous stop of all the pipes concerned */
  493. err = pcxhr_set_pipe_state(mgr, playback_mask, capture_mask, 0);
  494. if (err) {
  495. mutex_unlock(&mgr->setup_mutex);
  496. snd_printk(KERN_ERR "pcxhr_trigger_tasklet : error stop pipes (P%x C%x)\n",
  497. playback_mask, capture_mask);
  498. return;
  499. }
  500. /* unfortunately the dsp lost format and buffer info with the stop pipe */
  501. for (i = 0; i < mgr->num_cards; i++) {
  502. struct pcxhr_stream *stream;
  503. chip = mgr->chip[i];
  504. for (j = 0; j < chip->nb_streams_capt; j++) {
  505. stream = &chip->capture_stream[j];
  506. if (pcxhr_stream_scheduled_get_pipe(stream, &pipe)) {
  507. err = pcxhr_set_format(stream);
  508. err = pcxhr_update_r_buffer(stream);
  509. }
  510. }
  511. for (j = 0; j < chip->nb_streams_play; j++) {
  512. stream = &chip->playback_stream[j];
  513. if (pcxhr_stream_scheduled_get_pipe(stream, &pipe)) {
  514. err = pcxhr_set_format(stream);
  515. err = pcxhr_update_r_buffer(stream);
  516. }
  517. }
  518. }
  519. /* start all the streams */
  520. for (i = 0; i < mgr->num_cards; i++) {
  521. struct pcxhr_stream *stream;
  522. chip = mgr->chip[i];
  523. for (j = 0; j < chip->nb_streams_capt; j++) {
  524. stream = &chip->capture_stream[j];
  525. if (pcxhr_stream_scheduled_get_pipe(stream, &pipe))
  526. err = pcxhr_set_stream_state(stream);
  527. }
  528. for (j = 0; j < chip->nb_streams_play; j++) {
  529. stream = &chip->playback_stream[j];
  530. if (pcxhr_stream_scheduled_get_pipe(stream, &pipe))
  531. err = pcxhr_set_stream_state(stream);
  532. }
  533. }
  534. /* synchronous start of all the pipes concerned */
  535. err = pcxhr_set_pipe_state(mgr, playback_mask, capture_mask, 1);
  536. if (err) {
  537. mutex_unlock(&mgr->setup_mutex);
  538. snd_printk(KERN_ERR "pcxhr_trigger_tasklet : error start pipes (P%x C%x)\n",
  539. playback_mask, capture_mask);
  540. return;
  541. }
  542. /* put the streams into the running state now (increment pointer by interrupt) */
  543. spin_lock_irqsave(&mgr->lock, flags);
  544. for ( i =0; i < mgr->num_cards; i++) {
  545. struct pcxhr_stream *stream;
  546. chip = mgr->chip[i];
  547. for(j = 0; j < chip->nb_streams_capt; j++) {
  548. stream = &chip->capture_stream[j];
  549. if(stream->status == PCXHR_STREAM_STATUS_STARTED)
  550. stream->status = PCXHR_STREAM_STATUS_RUNNING;
  551. }
  552. for (j = 0; j < chip->nb_streams_play; j++) {
  553. stream = &chip->playback_stream[j];
  554. if (stream->status == PCXHR_STREAM_STATUS_STARTED) {
  555. /* playback will already have advanced ! */
  556. stream->timer_period_frag += PCXHR_GRANULARITY;
  557. stream->status = PCXHR_STREAM_STATUS_RUNNING;
  558. }
  559. }
  560. }
  561. spin_unlock_irqrestore(&mgr->lock, flags);
  562. mutex_unlock(&mgr->setup_mutex);
  563. #ifdef CONFIG_SND_DEBUG_DETECT
  564. do_gettimeofday(&my_tv2);
  565. snd_printdd("***TRIGGER TASKLET*** TIME = %ld (err = %x)\n",
  566. my_tv2.tv_usec - my_tv1.tv_usec, err);
  567. #endif
  568. }
  569. /*
  570. * trigger callback
  571. */
  572. static int pcxhr_trigger(struct snd_pcm_substream *subs, int cmd)
  573. {
  574. struct pcxhr_stream *stream;
  575. struct list_head *pos;
  576. struct snd_pcm_substream *s;
  577. int i;
  578. switch (cmd) {
  579. case SNDRV_PCM_TRIGGER_START:
  580. snd_printdd("SNDRV_PCM_TRIGGER_START\n");
  581. i = 0;
  582. snd_pcm_group_for_each(pos, subs) {
  583. s = snd_pcm_group_substream_entry(pos);
  584. stream = s->runtime->private_data;
  585. stream->status = PCXHR_STREAM_STATUS_SCHEDULE_RUN;
  586. snd_pcm_trigger_done(s, subs);
  587. i++;
  588. }
  589. if (i==1) {
  590. snd_printdd("Only one Substream %c %d\n",
  591. stream->pipe->is_capture ? 'C' : 'P',
  592. stream->pipe->first_audio);
  593. if (pcxhr_set_format(stream))
  594. return -EINVAL;
  595. if (pcxhr_update_r_buffer(stream))
  596. return -EINVAL;
  597. if (pcxhr_set_stream_state(stream))
  598. return -EINVAL;
  599. stream->status = PCXHR_STREAM_STATUS_RUNNING;
  600. } else {
  601. struct snd_pcxhr *chip = snd_pcm_substream_chip(subs);
  602. tasklet_hi_schedule(&chip->mgr->trigger_taskq);
  603. }
  604. break;
  605. case SNDRV_PCM_TRIGGER_STOP:
  606. snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
  607. snd_pcm_group_for_each(pos, subs) {
  608. s = snd_pcm_group_substream_entry(pos);
  609. stream = s->runtime->private_data;
  610. stream->status = PCXHR_STREAM_STATUS_SCHEDULE_STOP;
  611. if (pcxhr_set_stream_state(stream))
  612. return -EINVAL;
  613. snd_pcm_trigger_done(s, subs);
  614. }
  615. break;
  616. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  617. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  618. /* TODO */
  619. default:
  620. return -EINVAL;
  621. }
  622. return 0;
  623. }
  624. static int pcxhr_hardware_timer(struct pcxhr_mgr *mgr, int start)
  625. {
  626. struct pcxhr_rmh rmh;
  627. int err;
  628. pcxhr_init_rmh(&rmh, CMD_SET_TIMER_INTERRUPT);
  629. if (start) {
  630. mgr->dsp_time_last = PCXHR_DSP_TIME_INVALID; /* last dsp time invalid */
  631. rmh.cmd[0] |= PCXHR_GRANULARITY;
  632. }
  633. err = pcxhr_send_msg(mgr, &rmh);
  634. if (err < 0)
  635. snd_printk(KERN_ERR "error pcxhr_hardware_timer err(%x)\n", err);
  636. return err;
  637. }
  638. /*
  639. * prepare callback for all pcms
  640. */
  641. static int pcxhr_prepare(struct snd_pcm_substream *subs)
  642. {
  643. struct snd_pcxhr *chip = snd_pcm_substream_chip(subs);
  644. struct pcxhr_mgr *mgr = chip->mgr;
  645. /*
  646. struct pcxhr_stream *stream = (pcxhr_stream_t*)subs->runtime->private_data;
  647. */
  648. int err = 0;
  649. snd_printdd("pcxhr_prepare : period_size(%lx) periods(%x) buffer_size(%lx)\n",
  650. subs->runtime->period_size, subs->runtime->periods,
  651. subs->runtime->buffer_size);
  652. /*
  653. if(subs->runtime->period_size <= PCXHR_GRANULARITY) {
  654. snd_printk(KERN_ERR "pcxhr_prepare : error period_size too small (%x)\n",
  655. (unsigned int)subs->runtime->period_size);
  656. return -EINVAL;
  657. }
  658. */
  659. mutex_lock(&mgr->setup_mutex);
  660. do {
  661. /* if the stream was stopped before, format and buffer were reset */
  662. /*
  663. if(stream->status == PCXHR_STREAM_STATUS_STOPPED) {
  664. err = pcxhr_set_format(stream);
  665. if(err) break;
  666. err = pcxhr_update_r_buffer(stream);
  667. if(err) break;
  668. }
  669. */
  670. /* only the first stream can choose the sample rate */
  671. /* the further opened streams will be limited to its frequency (see open) */
  672. /* set the clock only once (first stream) */
  673. if (mgr->sample_rate != subs->runtime->rate) {
  674. err = pcxhr_set_clock(mgr, subs->runtime->rate);
  675. if (err)
  676. break;
  677. if (mgr->sample_rate == 0)
  678. /* start the DSP-timer */
  679. err = pcxhr_hardware_timer(mgr, 1);
  680. mgr->sample_rate = subs->runtime->rate;
  681. }
  682. } while(0); /* do only once (so we can use break instead of goto) */
  683. mutex_unlock(&mgr->setup_mutex);
  684. return err;
  685. }
  686. /*
  687. * HW_PARAMS callback for all pcms
  688. */
  689. static int pcxhr_hw_params(struct snd_pcm_substream *subs,
  690. struct snd_pcm_hw_params *hw)
  691. {
  692. struct snd_pcxhr *chip = snd_pcm_substream_chip(subs);
  693. struct pcxhr_mgr *mgr = chip->mgr;
  694. struct pcxhr_stream *stream = subs->runtime->private_data;
  695. snd_pcm_format_t format;
  696. int err;
  697. int channels;
  698. /* set up channels */
  699. channels = params_channels(hw);
  700. /* set up format for the stream */
  701. format = params_format(hw);
  702. mutex_lock(&mgr->setup_mutex);
  703. stream->channels = channels;
  704. stream->format = format;
  705. /* set the format to the board */
  706. /*
  707. err = pcxhr_set_format(stream);
  708. if(err) {
  709. mutex_unlock(&mgr->setup_mutex);
  710. return err;
  711. }
  712. */
  713. /* allocate buffer */
  714. err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
  715. /*
  716. if (err > 0) {
  717. err = pcxhr_update_r_buffer(stream);
  718. }
  719. */
  720. mutex_unlock(&mgr->setup_mutex);
  721. return err;
  722. }
  723. static int pcxhr_hw_free(struct snd_pcm_substream *subs)
  724. {
  725. snd_pcm_lib_free_pages(subs);
  726. return 0;
  727. }
  728. /*
  729. * CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
  730. */
  731. static struct snd_pcm_hardware pcxhr_caps =
  732. {
  733. .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  734. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  735. 0 /*SNDRV_PCM_INFO_PAUSE*/),
  736. .formats = ( SNDRV_PCM_FMTBIT_U8 |
  737. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  738. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
  739. SNDRV_PCM_FMTBIT_FLOAT_LE ),
  740. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
  741. .rate_min = 8000,
  742. .rate_max = 192000,
  743. .channels_min = 1,
  744. .channels_max = 2,
  745. .buffer_bytes_max = (32*1024),
  746. /* 1 byte == 1 frame U8 mono (PCXHR_GRANULARITY is frames!) */
  747. .period_bytes_min = (2*PCXHR_GRANULARITY),
  748. .period_bytes_max = (16*1024),
  749. .periods_min = 2,
  750. .periods_max = (32*1024/PCXHR_GRANULARITY),
  751. };
  752. static int pcxhr_open(struct snd_pcm_substream *subs)
  753. {
  754. struct snd_pcxhr *chip = snd_pcm_substream_chip(subs);
  755. struct pcxhr_mgr *mgr = chip->mgr;
  756. struct snd_pcm_runtime *runtime = subs->runtime;
  757. struct pcxhr_stream *stream;
  758. int is_capture;
  759. mutex_lock(&mgr->setup_mutex);
  760. /* copy the struct snd_pcm_hardware struct */
  761. runtime->hw = pcxhr_caps;
  762. if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK ) {
  763. snd_printdd("pcxhr_open playback chip%d subs%d\n",
  764. chip->chip_idx, subs->number);
  765. is_capture = 0;
  766. stream = &chip->playback_stream[subs->number];
  767. } else {
  768. snd_printdd("pcxhr_open capture chip%d subs%d\n",
  769. chip->chip_idx, subs->number);
  770. is_capture = 1;
  771. if (mgr->mono_capture)
  772. runtime->hw.channels_max = 1;
  773. else
  774. runtime->hw.channels_min = 2;
  775. stream = &chip->capture_stream[subs->number];
  776. }
  777. if (stream->status != PCXHR_STREAM_STATUS_FREE){
  778. /* streams in use */
  779. snd_printk(KERN_ERR "pcxhr_open chip%d subs%d in use\n",
  780. chip->chip_idx, subs->number);
  781. mutex_unlock(&mgr->setup_mutex);
  782. return -EBUSY;
  783. }
  784. /* if a sample rate is already used or fixed by external clock,
  785. * the stream cannot change
  786. */
  787. if (mgr->sample_rate)
  788. runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
  789. else {
  790. if (mgr->use_clock_type != PCXHR_CLOCK_TYPE_INTERNAL) {
  791. int external_rate;
  792. if (pcxhr_get_external_clock(mgr, mgr->use_clock_type,
  793. &external_rate) ||
  794. external_rate == 0) {
  795. /* cannot detect the external clock rate */
  796. mutex_unlock(&mgr->setup_mutex);
  797. return -EBUSY;
  798. }
  799. runtime->hw.rate_min = runtime->hw.rate_max = external_rate;
  800. }
  801. }
  802. stream->status = PCXHR_STREAM_STATUS_OPEN;
  803. stream->substream = subs;
  804. stream->channels = 0; /* not configured yet */
  805. runtime->private_data = stream;
  806. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
  807. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
  808. mgr->ref_count_rate++;
  809. mutex_unlock(&mgr->setup_mutex);
  810. return 0;
  811. }
  812. static int pcxhr_close(struct snd_pcm_substream *subs)
  813. {
  814. struct snd_pcxhr *chip = snd_pcm_substream_chip(subs);
  815. struct pcxhr_mgr *mgr = chip->mgr;
  816. struct pcxhr_stream *stream = subs->runtime->private_data;
  817. mutex_lock(&mgr->setup_mutex);
  818. snd_printdd("pcxhr_close chip%d subs%d\n", chip->chip_idx, subs->number);
  819. /* sample rate released */
  820. if (--mgr->ref_count_rate == 0) {
  821. mgr->sample_rate = 0; /* the sample rate is no more locked */
  822. pcxhr_hardware_timer(mgr, 0); /* stop the DSP-timer */
  823. }
  824. stream->status = PCXHR_STREAM_STATUS_FREE;
  825. stream->substream = NULL;
  826. mutex_unlock(&mgr->setup_mutex);
  827. return 0;
  828. }
  829. static snd_pcm_uframes_t pcxhr_stream_pointer(struct snd_pcm_substream *subs)
  830. {
  831. unsigned long flags;
  832. u_int32_t timer_period_frag;
  833. int timer_buf_periods;
  834. struct snd_pcxhr *chip = snd_pcm_substream_chip(subs);
  835. struct snd_pcm_runtime *runtime = subs->runtime;
  836. struct pcxhr_stream *stream = runtime->private_data;
  837. spin_lock_irqsave(&chip->mgr->lock, flags);
  838. /* get the period fragment and the nb of periods in the buffer */
  839. timer_period_frag = stream->timer_period_frag;
  840. timer_buf_periods = stream->timer_buf_periods;
  841. spin_unlock_irqrestore(&chip->mgr->lock, flags);
  842. return (snd_pcm_uframes_t)((timer_buf_periods * runtime->period_size) +
  843. timer_period_frag);
  844. }
  845. static struct snd_pcm_ops pcxhr_ops = {
  846. .open = pcxhr_open,
  847. .close = pcxhr_close,
  848. .ioctl = snd_pcm_lib_ioctl,
  849. .prepare = pcxhr_prepare,
  850. .hw_params = pcxhr_hw_params,
  851. .hw_free = pcxhr_hw_free,
  852. .trigger = pcxhr_trigger,
  853. .pointer = pcxhr_stream_pointer,
  854. };
  855. /*
  856. */
  857. int pcxhr_create_pcm(struct snd_pcxhr *chip)
  858. {
  859. int err;
  860. struct snd_pcm *pcm;
  861. char name[32];
  862. sprintf(name, "pcxhr %d", chip->chip_idx);
  863. if ((err = snd_pcm_new(chip->card, name, 0,
  864. chip->nb_streams_play,
  865. chip->nb_streams_capt, &pcm)) < 0) {
  866. snd_printk(KERN_ERR "cannot create pcm %s\n", name);
  867. return err;
  868. }
  869. pcm->private_data = chip;
  870. if (chip->nb_streams_play)
  871. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcxhr_ops);
  872. if (chip->nb_streams_capt)
  873. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcxhr_ops);
  874. pcm->info_flags = 0;
  875. strcpy(pcm->name, name);
  876. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  877. snd_dma_pci_data(chip->mgr->pci),
  878. 32*1024, 32*1024);
  879. chip->pcm = pcm;
  880. return 0;
  881. }
  882. static int pcxhr_chip_free(struct snd_pcxhr *chip)
  883. {
  884. kfree(chip);
  885. return 0;
  886. }
  887. static int pcxhr_chip_dev_free(struct snd_device *device)
  888. {
  889. struct snd_pcxhr *chip = device->device_data;
  890. return pcxhr_chip_free(chip);
  891. }
  892. /*
  893. */
  894. static int __devinit pcxhr_create(struct pcxhr_mgr *mgr, struct snd_card *card, int idx)
  895. {
  896. int err;
  897. struct snd_pcxhr *chip;
  898. static struct snd_device_ops ops = {
  899. .dev_free = pcxhr_chip_dev_free,
  900. };
  901. mgr->chip[idx] = chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  902. if (! chip) {
  903. snd_printk(KERN_ERR "cannot allocate chip\n");
  904. return -ENOMEM;
  905. }
  906. chip->card = card;
  907. chip->chip_idx = idx;
  908. chip->mgr = mgr;
  909. if (idx < mgr->playback_chips)
  910. /* stereo or mono streams */
  911. chip->nb_streams_play = PCXHR_PLAYBACK_STREAMS;
  912. if (idx < mgr->capture_chips) {
  913. if (mgr->mono_capture)
  914. chip->nb_streams_capt = 2; /* 2 mono streams (left+right) */
  915. else
  916. chip->nb_streams_capt = 1; /* or 1 stereo stream */
  917. }
  918. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  919. pcxhr_chip_free(chip);
  920. return err;
  921. }
  922. snd_card_set_dev(card, &mgr->pci->dev);
  923. return 0;
  924. }
  925. /* proc interface */
  926. static void pcxhr_proc_info(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  927. {
  928. struct snd_pcxhr *chip = entry->private_data;
  929. struct pcxhr_mgr *mgr = chip->mgr;
  930. snd_iprintf(buffer, "\n%s\n", mgr->longname);
  931. /* stats available when embedded DSP is running */
  932. if (mgr->dsp_loaded & (1 << PCXHR_FIRMWARE_DSP_MAIN_INDEX)) {
  933. struct pcxhr_rmh rmh;
  934. short ver_maj = (mgr->dsp_version >> 16) & 0xff;
  935. short ver_min = (mgr->dsp_version >> 8) & 0xff;
  936. short ver_build = mgr->dsp_version & 0xff;
  937. snd_iprintf(buffer, "module version %s\n", PCXHR_DRIVER_VERSION_STRING);
  938. snd_iprintf(buffer, "dsp version %d.%d.%d\n", ver_maj, ver_min, ver_build);
  939. if (mgr->board_has_analog)
  940. snd_iprintf(buffer, "analog io available\n");
  941. else
  942. snd_iprintf(buffer, "digital only board\n");
  943. /* calc cpu load of the dsp */
  944. pcxhr_init_rmh(&rmh, CMD_GET_DSP_RESOURCES);
  945. if( ! pcxhr_send_msg(mgr, &rmh) ) {
  946. int cur = rmh.stat[0];
  947. int ref = rmh.stat[1];
  948. if (ref > 0) {
  949. if (mgr->sample_rate_real != 0 &&
  950. mgr->sample_rate_real != 48000) {
  951. ref = (ref * 48000) / mgr->sample_rate_real;
  952. if (mgr->sample_rate_real >= PCXHR_IRQ_TIMER_FREQ)
  953. ref *= 2;
  954. }
  955. cur = 100 - (100 * cur) / ref;
  956. snd_iprintf(buffer, "cpu load %d%%\n", cur);
  957. snd_iprintf(buffer, "buffer pool %d/%d kWords\n",
  958. rmh.stat[2], rmh.stat[3]);
  959. }
  960. }
  961. snd_iprintf(buffer, "dma granularity : %d\n", PCXHR_GRANULARITY);
  962. snd_iprintf(buffer, "dsp time errors : %d\n", mgr->dsp_time_err);
  963. snd_iprintf(buffer, "dsp async pipe xrun errors : %d\n",
  964. mgr->async_err_pipe_xrun);
  965. snd_iprintf(buffer, "dsp async stream xrun errors : %d\n",
  966. mgr->async_err_stream_xrun);
  967. snd_iprintf(buffer, "dsp async last other error : %x\n",
  968. mgr->async_err_other_last);
  969. /* debug zone dsp */
  970. rmh.cmd[0] = 0x4200 + PCXHR_SIZE_MAX_STATUS;
  971. rmh.cmd_len = 1;
  972. rmh.stat_len = PCXHR_SIZE_MAX_STATUS;
  973. rmh.dsp_stat = 0;
  974. rmh.cmd_idx = CMD_LAST_INDEX;
  975. if( ! pcxhr_send_msg(mgr, &rmh) ) {
  976. int i;
  977. for (i = 0; i < rmh.stat_len; i++)
  978. snd_iprintf(buffer, "debug[%02d] = %06x\n", i, rmh.stat[i]);
  979. }
  980. } else
  981. snd_iprintf(buffer, "no firmware loaded\n");
  982. snd_iprintf(buffer, "\n");
  983. }
  984. static void pcxhr_proc_sync(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  985. {
  986. struct snd_pcxhr *chip = entry->private_data;
  987. struct pcxhr_mgr *mgr = chip->mgr;
  988. static char *texts[7] = {
  989. "Internal", "Word", "AES Sync", "AES 1", "AES 2", "AES 3", "AES 4"
  990. };
  991. snd_iprintf(buffer, "\n%s\n", mgr->longname);
  992. snd_iprintf(buffer, "Current Sample Clock\t: %s\n", texts[mgr->cur_clock_type]);
  993. snd_iprintf(buffer, "Current Sample Rate\t= %d\n", mgr->sample_rate_real);
  994. /* commands available when embedded DSP is running */
  995. if (mgr->dsp_loaded & (1 << PCXHR_FIRMWARE_DSP_MAIN_INDEX)) {
  996. int i, err, sample_rate;
  997. for (i = PCXHR_CLOCK_TYPE_WORD_CLOCK; i< (3 + mgr->capture_chips); i++) {
  998. err = pcxhr_get_external_clock(mgr, i, &sample_rate);
  999. if (err)
  1000. break;
  1001. snd_iprintf(buffer, "%s Clock\t\t= %d\n", texts[i], sample_rate);
  1002. }
  1003. } else
  1004. snd_iprintf(buffer, "no firmware loaded\n");
  1005. snd_iprintf(buffer, "\n");
  1006. }
  1007. static void __devinit pcxhr_proc_init(struct snd_pcxhr *chip)
  1008. {
  1009. struct snd_info_entry *entry;
  1010. if (! snd_card_proc_new(chip->card, "info", &entry))
  1011. snd_info_set_text_ops(entry, chip, pcxhr_proc_info);
  1012. if (! snd_card_proc_new(chip->card, "sync", &entry))
  1013. snd_info_set_text_ops(entry, chip, pcxhr_proc_sync);
  1014. }
  1015. /* end of proc interface */
  1016. /*
  1017. * release all the cards assigned to a manager instance
  1018. */
  1019. static int pcxhr_free(struct pcxhr_mgr *mgr)
  1020. {
  1021. unsigned int i;
  1022. for (i = 0; i < mgr->num_cards; i++) {
  1023. if (mgr->chip[i])
  1024. snd_card_free(mgr->chip[i]->card);
  1025. }
  1026. /* reset board if some firmware was loaded */
  1027. if(mgr->dsp_loaded) {
  1028. pcxhr_reset_board(mgr);
  1029. snd_printdd("reset pcxhr !\n");
  1030. }
  1031. /* release irq */
  1032. if (mgr->irq >= 0)
  1033. free_irq(mgr->irq, mgr);
  1034. pci_release_regions(mgr->pci);
  1035. /* free hostport purgebuffer */
  1036. if (mgr->hostport.area) {
  1037. snd_dma_free_pages(&mgr->hostport);
  1038. mgr->hostport.area = NULL;
  1039. }
  1040. kfree(mgr->prmh);
  1041. pci_disable_device(mgr->pci);
  1042. kfree(mgr);
  1043. return 0;
  1044. }
  1045. /*
  1046. * probe function - creates the card manager
  1047. */
  1048. static int __devinit pcxhr_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  1049. {
  1050. static int dev;
  1051. struct pcxhr_mgr *mgr;
  1052. unsigned int i;
  1053. int err;
  1054. size_t size;
  1055. char *card_name;
  1056. if (dev >= SNDRV_CARDS)
  1057. return -ENODEV;
  1058. if (! enable[dev]) {
  1059. dev++;
  1060. return -ENOENT;
  1061. }
  1062. /* enable PCI device */
  1063. if ((err = pci_enable_device(pci)) < 0)
  1064. return err;
  1065. pci_set_master(pci);
  1066. /* check if we can restrict PCI DMA transfers to 32 bits */
  1067. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0) {
  1068. snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
  1069. pci_disable_device(pci);
  1070. return -ENXIO;
  1071. }
  1072. /* alloc card manager */
  1073. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  1074. if (! mgr) {
  1075. pci_disable_device(pci);
  1076. return -ENOMEM;
  1077. }
  1078. snd_assert(pci_id->driver_data < PCI_ID_LAST, return -ENODEV);
  1079. card_name = pcxhr_board_params[pci_id->driver_data].board_name;
  1080. mgr->playback_chips = pcxhr_board_params[pci_id->driver_data].playback_chips;
  1081. mgr->capture_chips = pcxhr_board_params[pci_id->driver_data].capture_chips;
  1082. mgr->firmware_num = pcxhr_board_params[pci_id->driver_data].firmware_num;
  1083. mgr->mono_capture = mono[dev];
  1084. /* resource assignment */
  1085. if ((err = pci_request_regions(pci, card_name)) < 0) {
  1086. kfree(mgr);
  1087. pci_disable_device(pci);
  1088. return err;
  1089. }
  1090. for (i = 0; i < 3; i++)
  1091. mgr->port[i] = pci_resource_start(pci, i);
  1092. mgr->pci = pci;
  1093. mgr->irq = -1;
  1094. if (request_irq(pci->irq, pcxhr_interrupt, IRQF_DISABLED|IRQF_SHARED,
  1095. card_name, mgr)) {
  1096. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1097. pcxhr_free(mgr);
  1098. return -EBUSY;
  1099. }
  1100. mgr->irq = pci->irq;
  1101. sprintf(mgr->shortname, "Digigram %s", card_name);
  1102. sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, 0x%lx irq %i", mgr->shortname,
  1103. mgr->port[0], mgr->port[1], mgr->port[2], mgr->irq);
  1104. /* ISR spinlock */
  1105. spin_lock_init(&mgr->lock);
  1106. spin_lock_init(&mgr->msg_lock);
  1107. /* init setup mutex*/
  1108. mutex_init(&mgr->setup_mutex);
  1109. /* init taslket */
  1110. tasklet_init(&mgr->msg_taskq, pcxhr_msg_tasklet, (unsigned long) mgr);
  1111. tasklet_init(&mgr->trigger_taskq, pcxhr_trigger_tasklet, (unsigned long) mgr);
  1112. mgr->prmh = kmalloc(sizeof(*mgr->prmh) +
  1113. sizeof(u32) * (PCXHR_SIZE_MAX_LONG_STATUS - PCXHR_SIZE_MAX_STATUS),
  1114. GFP_KERNEL);
  1115. if (! mgr->prmh) {
  1116. pcxhr_free(mgr);
  1117. return -ENOMEM;
  1118. }
  1119. for (i=0; i < PCXHR_MAX_CARDS; i++) {
  1120. struct snd_card *card;
  1121. char tmpid[16];
  1122. int idx;
  1123. if (i >= max(mgr->playback_chips, mgr->capture_chips))
  1124. break;
  1125. mgr->num_cards++;
  1126. if (index[dev] < 0)
  1127. idx = index[dev];
  1128. else
  1129. idx = index[dev] + i;
  1130. snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : card_name, i);
  1131. card = snd_card_new(idx, tmpid, THIS_MODULE, 0);
  1132. if (! card) {
  1133. snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
  1134. pcxhr_free(mgr);
  1135. return -ENOMEM;
  1136. }
  1137. strcpy(card->driver, DRIVER_NAME);
  1138. sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
  1139. sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
  1140. if ((err = pcxhr_create(mgr, card, i)) < 0) {
  1141. pcxhr_free(mgr);
  1142. return err;
  1143. }
  1144. if (i == 0)
  1145. /* init proc interface only for chip0 */
  1146. pcxhr_proc_init(mgr->chip[i]);
  1147. if ((err = snd_card_register(card)) < 0) {
  1148. pcxhr_free(mgr);
  1149. return err;
  1150. }
  1151. }
  1152. /* create hostport purgebuffer */
  1153. size = PAGE_ALIGN(sizeof(struct pcxhr_hostport));
  1154. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1155. size, &mgr->hostport) < 0) {
  1156. pcxhr_free(mgr);
  1157. return -ENOMEM;
  1158. }
  1159. /* init purgebuffer */
  1160. memset(mgr->hostport.area, 0, size);
  1161. /* create a DSP loader */
  1162. err = pcxhr_setup_firmware(mgr);
  1163. if (err < 0) {
  1164. pcxhr_free(mgr);
  1165. return err;
  1166. }
  1167. pci_set_drvdata(pci, mgr);
  1168. dev++;
  1169. return 0;
  1170. }
  1171. static void __devexit pcxhr_remove(struct pci_dev *pci)
  1172. {
  1173. pcxhr_free(pci_get_drvdata(pci));
  1174. pci_set_drvdata(pci, NULL);
  1175. }
  1176. static struct pci_driver driver = {
  1177. .name = "Digigram pcxhr",
  1178. .id_table = pcxhr_ids,
  1179. .probe = pcxhr_probe,
  1180. .remove = __devexit_p(pcxhr_remove),
  1181. };
  1182. static int __init pcxhr_module_init(void)
  1183. {
  1184. return pci_register_driver(&driver);
  1185. }
  1186. static void __exit pcxhr_module_exit(void)
  1187. {
  1188. pci_unregister_driver(&driver);
  1189. }
  1190. module_init(pcxhr_module_init)
  1191. module_exit(pcxhr_module_exit)