pcxhr.c 47 KB

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