pcxhr.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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);