pcxhr.c 44 KB

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