pcxhr.c 46 KB

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