pcxhr.c 38 KB

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