asihpi.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981
  1. /*
  2. * Asihpi soundcard
  3. * Copyright (c) by AudioScience Inc <alsa@audioscience.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation;
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * The following is not a condition of use, merely a request:
  20. * If you modify this program, particularly if you fix errors, AudioScience Inc
  21. * would appreciate it if you grant us the right to use those modifications
  22. * for any purpose including commercial applications.
  23. */
  24. #include "hpi_internal.h"
  25. #include "hpimsginit.h"
  26. #include "hpioctl.h"
  27. #include <linux/pci.h>
  28. #include <linux/init.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/slab.h>
  31. #include <linux/time.h>
  32. #include <linux/wait.h>
  33. #include <sound/core.h>
  34. #include <sound/control.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/info.h>
  38. #include <sound/initval.h>
  39. #include <sound/tlv.h>
  40. #include <sound/hwdep.h>
  41. MODULE_LICENSE("GPL");
  42. MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
  43. MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
  44. #if defined CONFIG_SND_DEBUG_VERBOSE
  45. /**
  46. * snd_printddd - very verbose debug printk
  47. * @format: format string
  48. *
  49. * Works like snd_printk() for debugging purposes.
  50. * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
  51. * Must set snd module debug parameter to 3 to enable at runtime.
  52. */
  53. #define snd_printddd(format, args...) \
  54. __snd_printk(3, __FILE__, __LINE__, format, ##args)
  55. #else
  56. #define snd_printddd(format, args...) do { } while (0)
  57. #endif
  58. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
  59. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  60. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  61. static int enable_hpi_hwdep = 1;
  62. module_param_array(index, int, NULL, S_IRUGO);
  63. MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
  64. module_param_array(id, charp, NULL, S_IRUGO);
  65. MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
  66. module_param_array(enable, bool, NULL, S_IRUGO);
  67. MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
  68. module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
  69. MODULE_PARM_DESC(enable_hpi_hwdep,
  70. "ALSA enable HPI hwdep for AudioScience soundcard ");
  71. /* identify driver */
  72. #ifdef KERNEL_ALSA_BUILD
  73. static char *build_info = "Built using headers from kernel source";
  74. module_param(build_info, charp, S_IRUGO);
  75. MODULE_PARM_DESC(build_info, "built using headers from kernel source");
  76. #else
  77. static char *build_info = "Built within ALSA source";
  78. module_param(build_info, charp, S_IRUGO);
  79. MODULE_PARM_DESC(build_info, "built within ALSA source");
  80. #endif
  81. /* set to 1 to dump every control from adapter to log */
  82. static const int mixer_dump;
  83. #define DEFAULT_SAMPLERATE 44100
  84. static int adapter_fs = DEFAULT_SAMPLERATE;
  85. /* defaults */
  86. #define PERIODS_MIN 2
  87. #define PERIOD_BYTES_MIN 2048
  88. #define BUFFER_BYTES_MAX (512 * 1024)
  89. #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
  90. struct clk_source {
  91. int source;
  92. int index;
  93. char *name;
  94. };
  95. struct clk_cache {
  96. int count;
  97. int has_local;
  98. struct clk_source s[MAX_CLOCKSOURCES];
  99. };
  100. /* Per card data */
  101. struct snd_card_asihpi {
  102. struct snd_card *card;
  103. struct pci_dev *pci;
  104. u16 adapter_index;
  105. u32 serial_number;
  106. u16 type;
  107. u16 version;
  108. u16 num_outstreams;
  109. u16 num_instreams;
  110. u32 h_mixer;
  111. struct clk_cache cc;
  112. u16 can_dma;
  113. u16 support_grouping;
  114. u16 support_mrx;
  115. u16 update_interval_frames;
  116. u16 in_max_chans;
  117. u16 out_max_chans;
  118. };
  119. /* Per stream data */
  120. struct snd_card_asihpi_pcm {
  121. struct timer_list timer;
  122. unsigned int respawn_timer;
  123. unsigned int hpi_buffer_attached;
  124. unsigned int buffer_bytes;
  125. unsigned int period_bytes;
  126. unsigned int bytes_per_sec;
  127. unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
  128. unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
  129. unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
  130. unsigned int drained_count;
  131. struct snd_pcm_substream *substream;
  132. u32 h_stream;
  133. struct hpi_format format;
  134. };
  135. /* universal stream verbs work with out or in stream handles */
  136. /* Functions to allow driver to give a buffer to HPI for busmastering */
  137. static u16 hpi_stream_host_buffer_attach(
  138. u32 h_stream, /* handle to outstream. */
  139. u32 size_in_bytes, /* size in bytes of bus mastering buffer */
  140. u32 pci_address
  141. )
  142. {
  143. struct hpi_message hm;
  144. struct hpi_response hr;
  145. unsigned int obj = hpi_handle_object(h_stream);
  146. if (!h_stream)
  147. return HPI_ERROR_INVALID_OBJ;
  148. hpi_init_message_response(&hm, &hr, obj,
  149. obj == HPI_OBJ_OSTREAM ?
  150. HPI_OSTREAM_HOSTBUFFER_ALLOC :
  151. HPI_ISTREAM_HOSTBUFFER_ALLOC);
  152. hpi_handle_to_indexes(h_stream, &hm.adapter_index,
  153. &hm.obj_index);
  154. hm.u.d.u.buffer.buffer_size = size_in_bytes;
  155. hm.u.d.u.buffer.pci_address = pci_address;
  156. hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
  157. hpi_send_recv(&hm, &hr);
  158. return hr.error;
  159. }
  160. static u16 hpi_stream_host_buffer_detach(u32 h_stream)
  161. {
  162. struct hpi_message hm;
  163. struct hpi_response hr;
  164. unsigned int obj = hpi_handle_object(h_stream);
  165. if (!h_stream)
  166. return HPI_ERROR_INVALID_OBJ;
  167. hpi_init_message_response(&hm, &hr, obj,
  168. obj == HPI_OBJ_OSTREAM ?
  169. HPI_OSTREAM_HOSTBUFFER_FREE :
  170. HPI_ISTREAM_HOSTBUFFER_FREE);
  171. hpi_handle_to_indexes(h_stream, &hm.adapter_index,
  172. &hm.obj_index);
  173. hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
  174. hpi_send_recv(&hm, &hr);
  175. return hr.error;
  176. }
  177. static inline u16 hpi_stream_start(u32 h_stream)
  178. {
  179. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  180. return hpi_outstream_start(h_stream);
  181. else
  182. return hpi_instream_start(h_stream);
  183. }
  184. static inline u16 hpi_stream_stop(u32 h_stream)
  185. {
  186. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  187. return hpi_outstream_stop(h_stream);
  188. else
  189. return hpi_instream_stop(h_stream);
  190. }
  191. static inline u16 hpi_stream_get_info_ex(
  192. u32 h_stream,
  193. u16 *pw_state,
  194. u32 *pbuffer_size,
  195. u32 *pdata_in_buffer,
  196. u32 *psample_count,
  197. u32 *pauxiliary_data
  198. )
  199. {
  200. u16 e;
  201. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  202. e = hpi_outstream_get_info_ex(h_stream, pw_state,
  203. pbuffer_size, pdata_in_buffer,
  204. psample_count, pauxiliary_data);
  205. else
  206. e = hpi_instream_get_info_ex(h_stream, pw_state,
  207. pbuffer_size, pdata_in_buffer,
  208. psample_count, pauxiliary_data);
  209. return e;
  210. }
  211. static inline u16 hpi_stream_group_add(
  212. u32 h_master,
  213. u32 h_stream)
  214. {
  215. if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
  216. return hpi_outstream_group_add(h_master, h_stream);
  217. else
  218. return hpi_instream_group_add(h_master, h_stream);
  219. }
  220. static inline u16 hpi_stream_group_reset(u32 h_stream)
  221. {
  222. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  223. return hpi_outstream_group_reset(h_stream);
  224. else
  225. return hpi_instream_group_reset(h_stream);
  226. }
  227. static inline u16 hpi_stream_group_get_map(
  228. u32 h_stream, u32 *mo, u32 *mi)
  229. {
  230. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  231. return hpi_outstream_group_get_map(h_stream, mo, mi);
  232. else
  233. return hpi_instream_group_get_map(h_stream, mo, mi);
  234. }
  235. static u16 handle_error(u16 err, int line, char *filename)
  236. {
  237. if (err)
  238. printk(KERN_WARNING
  239. "in file %s, line %d: HPI error %d\n",
  240. filename, line, err);
  241. return err;
  242. }
  243. #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
  244. /***************************** GENERAL PCM ****************/
  245. static void print_hwparams(struct snd_pcm_substream *substream,
  246. struct snd_pcm_hw_params *p)
  247. {
  248. char name[16];
  249. snd_pcm_debug_name(substream, name, sizeof(name));
  250. snd_printd("%s HWPARAMS\n", name);
  251. snd_printd(" samplerate %d Hz\n", params_rate(p));
  252. snd_printd(" channels %d\n", params_channels(p));
  253. snd_printd(" format %d\n", params_format(p));
  254. snd_printd(" subformat %d\n", params_subformat(p));
  255. snd_printd(" buffer %d B\n", params_buffer_bytes(p));
  256. snd_printd(" period %d B\n", params_period_bytes(p));
  257. snd_printd(" access %d\n", params_access(p));
  258. snd_printd(" period_size %d\n", params_period_size(p));
  259. snd_printd(" periods %d\n", params_periods(p));
  260. snd_printd(" buffer_size %d\n", params_buffer_size(p));
  261. snd_printd(" %d B/s\n", params_rate(p) *
  262. params_channels(p) *
  263. snd_pcm_format_width(params_format(p)) / 8);
  264. }
  265. static snd_pcm_format_t hpi_to_alsa_formats[] = {
  266. -1, /* INVALID */
  267. SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
  268. SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
  269. -1, /* HPI_FORMAT_MPEG_L1 3 */
  270. SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
  271. SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
  272. -1, /* HPI_FORMAT_DOLBY_AC2 6 */
  273. -1, /* HPI_FORMAT_DOLBY_AC3 7 */
  274. SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
  275. -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
  276. -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
  277. SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
  278. -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
  279. -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
  280. SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
  281. #if 1
  282. /* ALSA can't handle 3 byte sample size together with power-of-2
  283. * constraint on buffer_bytes, so disable this format
  284. */
  285. -1
  286. #else
  287. /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
  288. #endif
  289. };
  290. static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
  291. u16 *hpi_format)
  292. {
  293. u16 format;
  294. for (format = HPI_FORMAT_PCM8_UNSIGNED;
  295. format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  296. if (hpi_to_alsa_formats[format] == alsa_format) {
  297. *hpi_format = format;
  298. return 0;
  299. }
  300. }
  301. snd_printd(KERN_WARNING "failed match for alsa format %d\n",
  302. alsa_format);
  303. *hpi_format = 0;
  304. return -EINVAL;
  305. }
  306. static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
  307. struct snd_pcm_hardware *pcmhw)
  308. {
  309. u16 err;
  310. u32 h_control;
  311. u32 sample_rate;
  312. int idx;
  313. unsigned int rate_min = 200000;
  314. unsigned int rate_max = 0;
  315. unsigned int rates = 0;
  316. if (asihpi->support_mrx) {
  317. rates |= SNDRV_PCM_RATE_CONTINUOUS;
  318. rates |= SNDRV_PCM_RATE_8000_96000;
  319. rate_min = 8000;
  320. rate_max = 100000;
  321. } else {
  322. /* on cards without SRC,
  323. valid rates are determined by sampleclock */
  324. err = hpi_mixer_get_control(asihpi->h_mixer,
  325. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  326. HPI_CONTROL_SAMPLECLOCK, &h_control);
  327. if (err) {
  328. snd_printk(KERN_ERR
  329. "No local sampleclock, err %d\n", err);
  330. }
  331. for (idx = -1; idx < 100; idx++) {
  332. if (idx == -1) {
  333. if (hpi_sample_clock_get_sample_rate(h_control,
  334. &sample_rate))
  335. continue;
  336. } else if (hpi_sample_clock_query_local_rate(h_control,
  337. idx, &sample_rate)) {
  338. break;
  339. }
  340. rate_min = min(rate_min, sample_rate);
  341. rate_max = max(rate_max, sample_rate);
  342. switch (sample_rate) {
  343. case 5512:
  344. rates |= SNDRV_PCM_RATE_5512;
  345. break;
  346. case 8000:
  347. rates |= SNDRV_PCM_RATE_8000;
  348. break;
  349. case 11025:
  350. rates |= SNDRV_PCM_RATE_11025;
  351. break;
  352. case 16000:
  353. rates |= SNDRV_PCM_RATE_16000;
  354. break;
  355. case 22050:
  356. rates |= SNDRV_PCM_RATE_22050;
  357. break;
  358. case 32000:
  359. rates |= SNDRV_PCM_RATE_32000;
  360. break;
  361. case 44100:
  362. rates |= SNDRV_PCM_RATE_44100;
  363. break;
  364. case 48000:
  365. rates |= SNDRV_PCM_RATE_48000;
  366. break;
  367. case 64000:
  368. rates |= SNDRV_PCM_RATE_64000;
  369. break;
  370. case 88200:
  371. rates |= SNDRV_PCM_RATE_88200;
  372. break;
  373. case 96000:
  374. rates |= SNDRV_PCM_RATE_96000;
  375. break;
  376. case 176400:
  377. rates |= SNDRV_PCM_RATE_176400;
  378. break;
  379. case 192000:
  380. rates |= SNDRV_PCM_RATE_192000;
  381. break;
  382. default: /* some other rate */
  383. rates |= SNDRV_PCM_RATE_KNOT;
  384. }
  385. }
  386. }
  387. pcmhw->rates = rates;
  388. pcmhw->rate_min = rate_min;
  389. pcmhw->rate_max = rate_max;
  390. }
  391. static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
  392. struct snd_pcm_hw_params *params)
  393. {
  394. struct snd_pcm_runtime *runtime = substream->runtime;
  395. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  396. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  397. int err;
  398. u16 format;
  399. int width;
  400. unsigned int bytes_per_sec;
  401. print_hwparams(substream, params);
  402. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  403. if (err < 0)
  404. return err;
  405. err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
  406. if (err)
  407. return err;
  408. hpi_handle_error(hpi_format_create(&dpcm->format,
  409. params_channels(params),
  410. format, params_rate(params), 0, 0));
  411. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  412. if (hpi_instream_reset(dpcm->h_stream) != 0)
  413. return -EINVAL;
  414. if (hpi_instream_set_format(
  415. dpcm->h_stream, &dpcm->format) != 0)
  416. return -EINVAL;
  417. }
  418. dpcm->hpi_buffer_attached = 0;
  419. if (card->can_dma) {
  420. err = hpi_stream_host_buffer_attach(dpcm->h_stream,
  421. params_buffer_bytes(params), runtime->dma_addr);
  422. if (err == 0) {
  423. snd_printdd(
  424. "stream_host_buffer_attach succeeded %u %lu\n",
  425. params_buffer_bytes(params),
  426. (unsigned long)runtime->dma_addr);
  427. } else {
  428. snd_printd("stream_host_buffer_attach error %d\n",
  429. err);
  430. return -ENOMEM;
  431. }
  432. err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
  433. &dpcm->hpi_buffer_attached,
  434. NULL, NULL, NULL);
  435. snd_printdd("stream_host_buffer_attach status 0x%x\n",
  436. dpcm->hpi_buffer_attached);
  437. }
  438. bytes_per_sec = params_rate(params) * params_channels(params);
  439. width = snd_pcm_format_width(params_format(params));
  440. bytes_per_sec *= width;
  441. bytes_per_sec /= 8;
  442. if (width < 0 || bytes_per_sec == 0)
  443. return -EINVAL;
  444. dpcm->bytes_per_sec = bytes_per_sec;
  445. dpcm->buffer_bytes = params_buffer_bytes(params);
  446. dpcm->period_bytes = params_period_bytes(params);
  447. return 0;
  448. }
  449. static int
  450. snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
  451. {
  452. struct snd_pcm_runtime *runtime = substream->runtime;
  453. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  454. if (dpcm->hpi_buffer_attached)
  455. hpi_stream_host_buffer_detach(dpcm->h_stream);
  456. snd_pcm_lib_free_pages(substream);
  457. return 0;
  458. }
  459. static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
  460. {
  461. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  462. kfree(dpcm);
  463. }
  464. static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
  465. substream)
  466. {
  467. struct snd_pcm_runtime *runtime = substream->runtime;
  468. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  469. int expiry;
  470. expiry = HZ / 200;
  471. /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
  472. expiry = max(expiry, 1); /* don't let it be zero! */
  473. dpcm->timer.expires = jiffies + expiry;
  474. dpcm->respawn_timer = 1;
  475. add_timer(&dpcm->timer);
  476. }
  477. static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
  478. {
  479. struct snd_pcm_runtime *runtime = substream->runtime;
  480. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  481. dpcm->respawn_timer = 0;
  482. del_timer(&dpcm->timer);
  483. }
  484. static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
  485. int cmd)
  486. {
  487. struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
  488. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  489. struct snd_pcm_substream *s;
  490. u16 e;
  491. char name[16];
  492. snd_pcm_debug_name(substream, name, sizeof(name));
  493. snd_printdd("%s trigger\n", name);
  494. switch (cmd) {
  495. case SNDRV_PCM_TRIGGER_START:
  496. snd_pcm_group_for_each_entry(s, substream) {
  497. struct snd_pcm_runtime *runtime = s->runtime;
  498. struct snd_card_asihpi_pcm *ds = runtime->private_data;
  499. if (snd_pcm_substream_chip(s) != card)
  500. continue;
  501. /* don't link Cap and Play */
  502. if (substream->stream != s->stream)
  503. continue;
  504. ds->drained_count = 0;
  505. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  506. /* How do I know how much valid data is present
  507. * in buffer? Must be at least one period!
  508. * Guessing 2 periods, but if
  509. * buffer is bigger it may contain even more
  510. * data??
  511. */
  512. unsigned int preload = ds->period_bytes * 1;
  513. snd_printddd("%d preload x%x\n", s->number, preload);
  514. hpi_handle_error(hpi_outstream_write_buf(
  515. ds->h_stream,
  516. &runtime->dma_area[0],
  517. preload,
  518. &ds->format));
  519. ds->pcm_buf_host_rw_ofs = preload;
  520. }
  521. if (card->support_grouping) {
  522. snd_printdd("%d group\n", s->number);
  523. e = hpi_stream_group_add(
  524. dpcm->h_stream,
  525. ds->h_stream);
  526. if (!e) {
  527. snd_pcm_trigger_done(s, substream);
  528. } else {
  529. hpi_handle_error(e);
  530. break;
  531. }
  532. } else
  533. break;
  534. }
  535. snd_printdd("start\n");
  536. /* start the master stream */
  537. snd_card_asihpi_pcm_timer_start(substream);
  538. if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
  539. !card->can_dma)
  540. hpi_handle_error(hpi_stream_start(dpcm->h_stream));
  541. break;
  542. case SNDRV_PCM_TRIGGER_STOP:
  543. snd_card_asihpi_pcm_timer_stop(substream);
  544. snd_pcm_group_for_each_entry(s, substream) {
  545. if (snd_pcm_substream_chip(s) != card)
  546. continue;
  547. /* don't link Cap and Play */
  548. if (substream->stream != s->stream)
  549. continue;
  550. /*? workaround linked streams don't
  551. transition to SETUP 20070706*/
  552. s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
  553. if (card->support_grouping) {
  554. snd_printdd("%d group\n", s->number);
  555. snd_pcm_trigger_done(s, substream);
  556. } else
  557. break;
  558. }
  559. snd_printdd("stop\n");
  560. /* _prepare and _hwparams reset the stream */
  561. hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
  562. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  563. hpi_handle_error(
  564. hpi_outstream_reset(dpcm->h_stream));
  565. if (card->support_grouping)
  566. hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
  567. break;
  568. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  569. snd_printdd("pause release\n");
  570. hpi_handle_error(hpi_stream_start(dpcm->h_stream));
  571. snd_card_asihpi_pcm_timer_start(substream);
  572. break;
  573. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  574. snd_printdd("pause\n");
  575. snd_card_asihpi_pcm_timer_stop(substream);
  576. hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
  577. break;
  578. default:
  579. snd_printd(KERN_ERR "\tINVALID\n");
  580. return -EINVAL;
  581. }
  582. return 0;
  583. }
  584. /*algorithm outline
  585. Without linking degenerates to getting single stream pos etc
  586. Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
  587. */
  588. /*
  589. pcm_buf_dma_ofs=get_buf_pos(s);
  590. for_each_linked_stream(s) {
  591. pcm_buf_dma_ofs=get_buf_pos(s);
  592. min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
  593. new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
  594. }
  595. timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
  596. for_each_linked_stream(s) {
  597. s->pcm_buf_dma_ofs = min_buf_pos;
  598. if (new_data > period_bytes) {
  599. if (mmap) {
  600. irq_pos = (irq_pos + period_bytes) % buffer_bytes;
  601. if (playback) {
  602. write(period_bytes);
  603. } else {
  604. read(period_bytes);
  605. }
  606. }
  607. snd_pcm_period_elapsed(s);
  608. }
  609. }
  610. */
  611. /** Minimum of 2 modulo values. Works correctly when the difference between
  612. * the values is less than half the modulus
  613. */
  614. static inline unsigned int modulo_min(unsigned int a, unsigned int b,
  615. unsigned long int modulus)
  616. {
  617. unsigned int result;
  618. if (((a-b) % modulus) < (modulus/2))
  619. result = b;
  620. else
  621. result = a;
  622. return result;
  623. }
  624. /** Timer function, equivalent to interrupt service routine for cards
  625. */
  626. static void snd_card_asihpi_timer_function(unsigned long data)
  627. {
  628. struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
  629. struct snd_pcm_substream *substream = dpcm->substream;
  630. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  631. struct snd_pcm_runtime *runtime;
  632. struct snd_pcm_substream *s;
  633. unsigned int newdata = 0;
  634. unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
  635. unsigned int remdata, xfercount, next_jiffies;
  636. int first = 1;
  637. int loops = 0;
  638. u16 state;
  639. u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
  640. char name[16];
  641. snd_pcm_debug_name(substream, name, sizeof(name));
  642. snd_printdd("%s snd_card_asihpi_timer_function\n", name);
  643. /* find minimum newdata and buffer pos in group */
  644. snd_pcm_group_for_each_entry(s, substream) {
  645. struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
  646. runtime = s->runtime;
  647. if (snd_pcm_substream_chip(s) != card)
  648. continue;
  649. /* don't link Cap and Play */
  650. if (substream->stream != s->stream)
  651. continue;
  652. hpi_handle_error(hpi_stream_get_info_ex(
  653. ds->h_stream, &state,
  654. &buffer_size, &bytes_avail,
  655. &samples_played, &on_card_bytes));
  656. /* number of bytes in on-card buffer */
  657. runtime->delay = on_card_bytes;
  658. if (!card->can_dma)
  659. on_card_bytes = bytes_avail;
  660. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  661. pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
  662. if (state == HPI_STATE_STOPPED) {
  663. if ((bytes_avail == 0) &&
  664. (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
  665. hpi_handle_error(hpi_stream_start(ds->h_stream));
  666. snd_printdd("P%d start\n", s->number);
  667. ds->drained_count = 0;
  668. }
  669. } else if (state == HPI_STATE_DRAINED) {
  670. snd_printd(KERN_WARNING "P%d drained\n",
  671. s->number);
  672. ds->drained_count++;
  673. if (ds->drained_count > 2) {
  674. snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
  675. continue;
  676. }
  677. } else {
  678. ds->drained_count = 0;
  679. }
  680. } else
  681. pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
  682. if (first) {
  683. /* can't statically init min when wrap is involved */
  684. min_buf_pos = pcm_buf_dma_ofs;
  685. newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
  686. first = 0;
  687. } else {
  688. min_buf_pos =
  689. modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
  690. newdata = min(
  691. (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
  692. newdata);
  693. }
  694. snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
  695. (unsigned long)frames_to_bytes(runtime,
  696. runtime->status->hw_ptr),
  697. (unsigned long)frames_to_bytes(runtime,
  698. runtime->control->appl_ptr));
  699. snd_printdd("%d S=%d, "
  700. "rw=0x%04X, dma=0x%04X, left=0x%04X, "
  701. "aux=0x%04X space=0x%04X\n",
  702. s->number, state,
  703. ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
  704. (int)bytes_avail,
  705. (int)on_card_bytes, buffer_size-bytes_avail);
  706. loops++;
  707. }
  708. pcm_buf_dma_ofs = min_buf_pos;
  709. remdata = newdata % dpcm->period_bytes;
  710. xfercount = newdata - remdata; /* a multiple of period_bytes */
  711. /* come back when on_card_bytes has decreased enough to allow
  712. write to happen, or when data has been consumed to make another
  713. period
  714. */
  715. if (xfercount && (on_card_bytes > dpcm->period_bytes))
  716. next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
  717. else
  718. next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
  719. next_jiffies = max(next_jiffies, 1U);
  720. dpcm->timer.expires = jiffies + next_jiffies;
  721. snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
  722. next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
  723. snd_pcm_group_for_each_entry(s, substream) {
  724. struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
  725. /* don't link Cap and Play */
  726. if (substream->stream != s->stream)
  727. continue;
  728. ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
  729. if (xfercount &&
  730. /* Limit use of on card fifo for playback */
  731. ((on_card_bytes <= ds->period_bytes) ||
  732. (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
  733. {
  734. unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
  735. unsigned int xfer1, xfer2;
  736. char *pd = &s->runtime->dma_area[buf_ofs];
  737. if (card->can_dma) { /* buffer wrap is handled at lower level */
  738. xfer1 = xfercount;
  739. xfer2 = 0;
  740. } else {
  741. xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
  742. xfer2 = xfercount - xfer1;
  743. }
  744. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  745. snd_printddd("P%d write1 0x%04X 0x%04X\n",
  746. s->number, xfer1, buf_ofs);
  747. hpi_handle_error(
  748. hpi_outstream_write_buf(
  749. ds->h_stream, pd, xfer1,
  750. &ds->format));
  751. if (xfer2) {
  752. pd = s->runtime->dma_area;
  753. snd_printddd("P%d write2 0x%04X 0x%04X\n",
  754. s->number,
  755. xfercount - xfer1, buf_ofs);
  756. hpi_handle_error(
  757. hpi_outstream_write_buf(
  758. ds->h_stream, pd,
  759. xfercount - xfer1,
  760. &ds->format));
  761. }
  762. } else {
  763. snd_printddd("C%d read1 0x%04x\n",
  764. s->number, xfer1);
  765. hpi_handle_error(
  766. hpi_instream_read_buf(
  767. ds->h_stream,
  768. pd, xfer1));
  769. if (xfer2) {
  770. pd = s->runtime->dma_area;
  771. snd_printddd("C%d read2 0x%04x\n",
  772. s->number, xfer2);
  773. hpi_handle_error(
  774. hpi_instream_read_buf(
  775. ds->h_stream,
  776. pd, xfer2));
  777. }
  778. }
  779. ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
  780. ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
  781. snd_pcm_period_elapsed(s);
  782. }
  783. }
  784. if (dpcm->respawn_timer)
  785. add_timer(&dpcm->timer);
  786. }
  787. /***************************** PLAYBACK OPS ****************/
  788. static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
  789. unsigned int cmd, void *arg)
  790. {
  791. snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
  792. return snd_pcm_lib_ioctl(substream, cmd, arg);
  793. }
  794. static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
  795. substream)
  796. {
  797. struct snd_pcm_runtime *runtime = substream->runtime;
  798. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  799. snd_printdd("P%d prepare\n", substream->number);
  800. hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
  801. dpcm->pcm_buf_host_rw_ofs = 0;
  802. dpcm->pcm_buf_dma_ofs = 0;
  803. dpcm->pcm_buf_elapsed_dma_ofs = 0;
  804. return 0;
  805. }
  806. static snd_pcm_uframes_t
  807. snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
  808. {
  809. struct snd_pcm_runtime *runtime = substream->runtime;
  810. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  811. snd_pcm_uframes_t ptr;
  812. ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
  813. snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
  814. return ptr;
  815. }
  816. static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
  817. u32 h_stream,
  818. struct snd_pcm_hardware *pcmhw)
  819. {
  820. struct hpi_format hpi_format;
  821. u16 format;
  822. u16 err;
  823. u32 h_control;
  824. u32 sample_rate = 48000;
  825. /* on cards without SRC, must query at valid rate,
  826. * maybe set by external sync
  827. */
  828. err = hpi_mixer_get_control(asihpi->h_mixer,
  829. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  830. HPI_CONTROL_SAMPLECLOCK, &h_control);
  831. if (!err)
  832. err = hpi_sample_clock_get_sample_rate(h_control,
  833. &sample_rate);
  834. for (format = HPI_FORMAT_PCM8_UNSIGNED;
  835. format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  836. err = hpi_format_create(&hpi_format,
  837. 2, format, sample_rate, 128000, 0);
  838. if (!err)
  839. err = hpi_outstream_query_format(h_stream,
  840. &hpi_format);
  841. if (!err && (hpi_to_alsa_formats[format] != -1))
  842. pcmhw->formats |=
  843. (1ULL << hpi_to_alsa_formats[format]);
  844. }
  845. }
  846. static struct snd_pcm_hardware snd_card_asihpi_playback = {
  847. .channels_min = 1,
  848. .channels_max = 2,
  849. .buffer_bytes_max = BUFFER_BYTES_MAX,
  850. .period_bytes_min = PERIOD_BYTES_MIN,
  851. .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
  852. .periods_min = PERIODS_MIN,
  853. .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
  854. .fifo_size = 0,
  855. };
  856. static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
  857. {
  858. struct snd_pcm_runtime *runtime = substream->runtime;
  859. struct snd_card_asihpi_pcm *dpcm;
  860. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  861. int err;
  862. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  863. if (dpcm == NULL)
  864. return -ENOMEM;
  865. err =
  866. hpi_outstream_open(card->adapter_index,
  867. substream->number, &dpcm->h_stream);
  868. hpi_handle_error(err);
  869. if (err)
  870. kfree(dpcm);
  871. if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
  872. return -EBUSY;
  873. if (err)
  874. return -EIO;
  875. /*? also check ASI5000 samplerate source
  876. If external, only support external rate.
  877. If internal and other stream playing, can't switch
  878. */
  879. init_timer(&dpcm->timer);
  880. dpcm->timer.data = (unsigned long) dpcm;
  881. dpcm->timer.function = snd_card_asihpi_timer_function;
  882. dpcm->substream = substream;
  883. runtime->private_data = dpcm;
  884. runtime->private_free = snd_card_asihpi_runtime_free;
  885. snd_card_asihpi_playback.channels_max = card->out_max_chans;
  886. /*?snd_card_asihpi_playback.period_bytes_min =
  887. card->out_max_chans * 4096; */
  888. snd_card_asihpi_playback_format(card, dpcm->h_stream,
  889. &snd_card_asihpi_playback);
  890. snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
  891. snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
  892. SNDRV_PCM_INFO_DOUBLE |
  893. SNDRV_PCM_INFO_BATCH |
  894. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  895. SNDRV_PCM_INFO_PAUSE |
  896. SNDRV_PCM_INFO_MMAP |
  897. SNDRV_PCM_INFO_MMAP_VALID;
  898. if (card->support_grouping)
  899. snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
  900. /* struct is copied, so can create initializer dynamically */
  901. runtime->hw = snd_card_asihpi_playback;
  902. if (card->can_dma)
  903. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  904. SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  905. if (err < 0)
  906. return err;
  907. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  908. card->update_interval_frames);
  909. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  910. card->update_interval_frames * 2, UINT_MAX);
  911. snd_pcm_set_sync(substream);
  912. snd_printdd("playback open\n");
  913. return 0;
  914. }
  915. static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
  916. {
  917. struct snd_pcm_runtime *runtime = substream->runtime;
  918. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  919. hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
  920. snd_printdd("playback close\n");
  921. return 0;
  922. }
  923. static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
  924. .open = snd_card_asihpi_playback_open,
  925. .close = snd_card_asihpi_playback_close,
  926. .ioctl = snd_card_asihpi_playback_ioctl,
  927. .hw_params = snd_card_asihpi_pcm_hw_params,
  928. .hw_free = snd_card_asihpi_hw_free,
  929. .prepare = snd_card_asihpi_playback_prepare,
  930. .trigger = snd_card_asihpi_trigger,
  931. .pointer = snd_card_asihpi_playback_pointer,
  932. };
  933. /***************************** CAPTURE OPS ****************/
  934. static snd_pcm_uframes_t
  935. snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
  936. {
  937. struct snd_pcm_runtime *runtime = substream->runtime;
  938. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  939. snd_printddd("capture pointer %d=%d\n",
  940. substream->number, dpcm->pcm_buf_dma_ofs);
  941. /* NOTE Unlike playback can't use actual samples_played
  942. for the capture position, because those samples aren't yet in
  943. the local buffer available for reading.
  944. */
  945. return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
  946. }
  947. static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
  948. unsigned int cmd, void *arg)
  949. {
  950. return snd_pcm_lib_ioctl(substream, cmd, arg);
  951. }
  952. static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
  953. {
  954. struct snd_pcm_runtime *runtime = substream->runtime;
  955. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  956. hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
  957. dpcm->pcm_buf_host_rw_ofs = 0;
  958. dpcm->pcm_buf_dma_ofs = 0;
  959. dpcm->pcm_buf_elapsed_dma_ofs = 0;
  960. snd_printdd("Capture Prepare %d\n", substream->number);
  961. return 0;
  962. }
  963. static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
  964. u32 h_stream,
  965. struct snd_pcm_hardware *pcmhw)
  966. {
  967. struct hpi_format hpi_format;
  968. u16 format;
  969. u16 err;
  970. u32 h_control;
  971. u32 sample_rate = 48000;
  972. /* on cards without SRC, must query at valid rate,
  973. maybe set by external sync */
  974. err = hpi_mixer_get_control(asihpi->h_mixer,
  975. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  976. HPI_CONTROL_SAMPLECLOCK, &h_control);
  977. if (!err)
  978. err = hpi_sample_clock_get_sample_rate(h_control,
  979. &sample_rate);
  980. for (format = HPI_FORMAT_PCM8_UNSIGNED;
  981. format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  982. err = hpi_format_create(&hpi_format, 2, format,
  983. sample_rate, 128000, 0);
  984. if (!err)
  985. err = hpi_instream_query_format(h_stream,
  986. &hpi_format);
  987. if (!err)
  988. pcmhw->formats |=
  989. (1ULL << hpi_to_alsa_formats[format]);
  990. }
  991. }
  992. static struct snd_pcm_hardware snd_card_asihpi_capture = {
  993. .channels_min = 1,
  994. .channels_max = 2,
  995. .buffer_bytes_max = BUFFER_BYTES_MAX,
  996. .period_bytes_min = PERIOD_BYTES_MIN,
  997. .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
  998. .periods_min = PERIODS_MIN,
  999. .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
  1000. .fifo_size = 0,
  1001. };
  1002. static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
  1003. {
  1004. struct snd_pcm_runtime *runtime = substream->runtime;
  1005. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  1006. struct snd_card_asihpi_pcm *dpcm;
  1007. int err;
  1008. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  1009. if (dpcm == NULL)
  1010. return -ENOMEM;
  1011. snd_printdd("capture open adapter %d stream %d\n",
  1012. card->adapter_index, substream->number);
  1013. err = hpi_handle_error(
  1014. hpi_instream_open(card->adapter_index,
  1015. substream->number, &dpcm->h_stream));
  1016. if (err)
  1017. kfree(dpcm);
  1018. if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
  1019. return -EBUSY;
  1020. if (err)
  1021. return -EIO;
  1022. init_timer(&dpcm->timer);
  1023. dpcm->timer.data = (unsigned long) dpcm;
  1024. dpcm->timer.function = snd_card_asihpi_timer_function;
  1025. dpcm->substream = substream;
  1026. runtime->private_data = dpcm;
  1027. runtime->private_free = snd_card_asihpi_runtime_free;
  1028. snd_card_asihpi_capture.channels_max = card->in_max_chans;
  1029. snd_card_asihpi_capture_format(card, dpcm->h_stream,
  1030. &snd_card_asihpi_capture);
  1031. snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
  1032. snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
  1033. SNDRV_PCM_INFO_MMAP |
  1034. SNDRV_PCM_INFO_MMAP_VALID;
  1035. if (card->support_grouping)
  1036. snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
  1037. runtime->hw = snd_card_asihpi_capture;
  1038. if (card->can_dma)
  1039. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  1040. SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  1041. if (err < 0)
  1042. return err;
  1043. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1044. card->update_interval_frames);
  1045. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1046. card->update_interval_frames * 2, UINT_MAX);
  1047. snd_pcm_set_sync(substream);
  1048. return 0;
  1049. }
  1050. static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
  1051. {
  1052. struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
  1053. hpi_handle_error(hpi_instream_close(dpcm->h_stream));
  1054. return 0;
  1055. }
  1056. static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
  1057. .open = snd_card_asihpi_capture_open,
  1058. .close = snd_card_asihpi_capture_close,
  1059. .ioctl = snd_card_asihpi_capture_ioctl,
  1060. .hw_params = snd_card_asihpi_pcm_hw_params,
  1061. .hw_free = snd_card_asihpi_hw_free,
  1062. .prepare = snd_card_asihpi_capture_prepare,
  1063. .trigger = snd_card_asihpi_trigger,
  1064. .pointer = snd_card_asihpi_capture_pointer,
  1065. };
  1066. static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
  1067. int device, int substreams)
  1068. {
  1069. struct snd_pcm *pcm;
  1070. int err;
  1071. err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
  1072. asihpi->num_outstreams, asihpi->num_instreams,
  1073. &pcm);
  1074. if (err < 0)
  1075. return err;
  1076. /* pointer to ops struct is stored, dont change ops afterwards! */
  1077. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1078. &snd_card_asihpi_playback_mmap_ops);
  1079. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1080. &snd_card_asihpi_capture_mmap_ops);
  1081. pcm->private_data = asihpi;
  1082. pcm->info_flags = 0;
  1083. strcpy(pcm->name, "Asihpi PCM");
  1084. /*? do we want to emulate MMAP for non-BBM cards?
  1085. Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
  1086. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1087. snd_dma_pci_data(asihpi->pci),
  1088. 64*1024, BUFFER_BYTES_MAX);
  1089. return 0;
  1090. }
  1091. /***************************** MIXER CONTROLS ****************/
  1092. struct hpi_control {
  1093. u32 h_control;
  1094. u16 control_type;
  1095. u16 src_node_type;
  1096. u16 src_node_index;
  1097. u16 dst_node_type;
  1098. u16 dst_node_index;
  1099. u16 band;
  1100. char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
  1101. };
  1102. static const char * const asihpi_tuner_band_names[] = {
  1103. "invalid",
  1104. "AM",
  1105. "FM mono",
  1106. "TV NTSC-M",
  1107. "FM stereo",
  1108. "AUX",
  1109. "TV PAL BG",
  1110. "TV PAL I",
  1111. "TV PAL DK",
  1112. "TV SECAM",
  1113. };
  1114. compile_time_assert(
  1115. (ARRAY_SIZE(asihpi_tuner_band_names) ==
  1116. (HPI_TUNER_BAND_LAST+1)),
  1117. assert_tuner_band_names_size);
  1118. static const char * const asihpi_src_names[] = {
  1119. "no source",
  1120. "PCM",
  1121. "Line",
  1122. "Digital",
  1123. "Tuner",
  1124. "RF",
  1125. "Clock",
  1126. "Bitstream",
  1127. "Mic",
  1128. "Net",
  1129. "Analog",
  1130. "Adapter",
  1131. "RTP",
  1132. "GPI",
  1133. };
  1134. compile_time_assert(
  1135. (ARRAY_SIZE(asihpi_src_names) ==
  1136. (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
  1137. assert_src_names_size);
  1138. static const char * const asihpi_dst_names[] = {
  1139. "no destination",
  1140. "PCM",
  1141. "Line",
  1142. "Digital",
  1143. "RF",
  1144. "Speaker",
  1145. "Net",
  1146. "Analog",
  1147. "RTP",
  1148. "GPO",
  1149. };
  1150. compile_time_assert(
  1151. (ARRAY_SIZE(asihpi_dst_names) ==
  1152. (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
  1153. assert_dst_names_size);
  1154. static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
  1155. struct snd_card_asihpi *asihpi)
  1156. {
  1157. int err;
  1158. err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
  1159. if (err < 0)
  1160. return err;
  1161. else if (mixer_dump)
  1162. snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
  1163. return 0;
  1164. }
  1165. /* Convert HPI control name and location into ALSA control name */
  1166. static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
  1167. struct hpi_control *hpi_ctl,
  1168. char *name)
  1169. {
  1170. char *dir;
  1171. memset(snd_control, 0, sizeof(*snd_control));
  1172. snd_control->name = hpi_ctl->name;
  1173. snd_control->private_value = hpi_ctl->h_control;
  1174. snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1175. snd_control->index = 0;
  1176. if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
  1177. dir = ""; /* clock is neither capture nor playback */
  1178. else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
  1179. dir = "Capture "; /* On or towards a PCM capture destination*/
  1180. else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
  1181. (!hpi_ctl->dst_node_type))
  1182. dir = "Capture "; /* On a source node that is not PCM playback */
  1183. else if (hpi_ctl->src_node_type &&
  1184. (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
  1185. (hpi_ctl->dst_node_type))
  1186. dir = "Monitor Playback "; /* Between an input and an output */
  1187. else
  1188. dir = "Playback "; /* PCM Playback source, or output node */
  1189. if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
  1190. sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
  1191. asihpi_src_names[hpi_ctl->src_node_type],
  1192. hpi_ctl->src_node_index,
  1193. asihpi_dst_names[hpi_ctl->dst_node_type],
  1194. hpi_ctl->dst_node_index,
  1195. dir, name);
  1196. else if (hpi_ctl->dst_node_type) {
  1197. sprintf(hpi_ctl->name, "%s %d %s%s",
  1198. asihpi_dst_names[hpi_ctl->dst_node_type],
  1199. hpi_ctl->dst_node_index,
  1200. dir, name);
  1201. } else {
  1202. sprintf(hpi_ctl->name, "%s %d %s%s",
  1203. asihpi_src_names[hpi_ctl->src_node_type],
  1204. hpi_ctl->src_node_index,
  1205. dir, name);
  1206. }
  1207. /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
  1208. hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
  1209. }
  1210. /*------------------------------------------------------------
  1211. Volume controls
  1212. ------------------------------------------------------------*/
  1213. #define VOL_STEP_mB 1
  1214. static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
  1215. struct snd_ctl_elem_info *uinfo)
  1216. {
  1217. u32 h_control = kcontrol->private_value;
  1218. u16 err;
  1219. /* native gains are in millibels */
  1220. short min_gain_mB;
  1221. short max_gain_mB;
  1222. short step_gain_mB;
  1223. err = hpi_volume_query_range(h_control,
  1224. &min_gain_mB, &max_gain_mB, &step_gain_mB);
  1225. if (err) {
  1226. max_gain_mB = 0;
  1227. min_gain_mB = -10000;
  1228. step_gain_mB = VOL_STEP_mB;
  1229. }
  1230. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1231. uinfo->count = 2;
  1232. uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
  1233. uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
  1234. uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
  1235. return 0;
  1236. }
  1237. static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
  1238. struct snd_ctl_elem_value *ucontrol)
  1239. {
  1240. u32 h_control = kcontrol->private_value;
  1241. short an_gain_mB[HPI_MAX_CHANNELS];
  1242. hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
  1243. ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
  1244. ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
  1245. return 0;
  1246. }
  1247. static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
  1248. struct snd_ctl_elem_value *ucontrol)
  1249. {
  1250. int change;
  1251. u32 h_control = kcontrol->private_value;
  1252. short an_gain_mB[HPI_MAX_CHANNELS];
  1253. an_gain_mB[0] =
  1254. (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
  1255. an_gain_mB[1] =
  1256. (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
  1257. /* change = asihpi->mixer_volume[addr][0] != left ||
  1258. asihpi->mixer_volume[addr][1] != right;
  1259. */
  1260. change = 1;
  1261. hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
  1262. return change;
  1263. }
  1264. static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
  1265. #define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
  1266. static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
  1267. struct snd_ctl_elem_value *ucontrol)
  1268. {
  1269. u32 h_control = kcontrol->private_value;
  1270. u32 mute;
  1271. hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
  1272. ucontrol->value.integer.value[0] = mute ? 0 : 1;
  1273. return 0;
  1274. }
  1275. static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
  1276. struct snd_ctl_elem_value *ucontrol)
  1277. {
  1278. u32 h_control = kcontrol->private_value;
  1279. int change = 1;
  1280. /* HPI currently only supports all or none muting of multichannel volume
  1281. ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
  1282. */
  1283. int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
  1284. hpi_handle_error(hpi_volume_set_mute(h_control, mute));
  1285. return change;
  1286. }
  1287. static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
  1288. struct hpi_control *hpi_ctl)
  1289. {
  1290. struct snd_card *card = asihpi->card;
  1291. struct snd_kcontrol_new snd_control;
  1292. int err;
  1293. u32 mute;
  1294. asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
  1295. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1296. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1297. snd_control.info = snd_asihpi_volume_info;
  1298. snd_control.get = snd_asihpi_volume_get;
  1299. snd_control.put = snd_asihpi_volume_put;
  1300. snd_control.tlv.p = db_scale_100;
  1301. err = ctl_add(card, &snd_control, asihpi);
  1302. if (err)
  1303. return err;
  1304. if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
  1305. asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
  1306. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1307. snd_control.info = snd_asihpi_volume_mute_info;
  1308. snd_control.get = snd_asihpi_volume_mute_get;
  1309. snd_control.put = snd_asihpi_volume_mute_put;
  1310. err = ctl_add(card, &snd_control, asihpi);
  1311. }
  1312. return err;
  1313. }
  1314. /*------------------------------------------------------------
  1315. Level controls
  1316. ------------------------------------------------------------*/
  1317. static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
  1318. struct snd_ctl_elem_info *uinfo)
  1319. {
  1320. u32 h_control = kcontrol->private_value;
  1321. u16 err;
  1322. short min_gain_mB;
  1323. short max_gain_mB;
  1324. short step_gain_mB;
  1325. err =
  1326. hpi_level_query_range(h_control, &min_gain_mB,
  1327. &max_gain_mB, &step_gain_mB);
  1328. if (err) {
  1329. max_gain_mB = 2400;
  1330. min_gain_mB = -1000;
  1331. step_gain_mB = 100;
  1332. }
  1333. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1334. uinfo->count = 2;
  1335. uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
  1336. uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
  1337. uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
  1338. return 0;
  1339. }
  1340. static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
  1341. struct snd_ctl_elem_value *ucontrol)
  1342. {
  1343. u32 h_control = kcontrol->private_value;
  1344. short an_gain_mB[HPI_MAX_CHANNELS];
  1345. hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
  1346. ucontrol->value.integer.value[0] =
  1347. an_gain_mB[0] / HPI_UNITS_PER_dB;
  1348. ucontrol->value.integer.value[1] =
  1349. an_gain_mB[1] / HPI_UNITS_PER_dB;
  1350. return 0;
  1351. }
  1352. static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
  1353. struct snd_ctl_elem_value *ucontrol)
  1354. {
  1355. int change;
  1356. u32 h_control = kcontrol->private_value;
  1357. short an_gain_mB[HPI_MAX_CHANNELS];
  1358. an_gain_mB[0] =
  1359. (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
  1360. an_gain_mB[1] =
  1361. (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
  1362. /* change = asihpi->mixer_level[addr][0] != left ||
  1363. asihpi->mixer_level[addr][1] != right;
  1364. */
  1365. change = 1;
  1366. hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
  1367. return change;
  1368. }
  1369. static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
  1370. static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
  1371. struct hpi_control *hpi_ctl)
  1372. {
  1373. struct snd_card *card = asihpi->card;
  1374. struct snd_kcontrol_new snd_control;
  1375. /* can't use 'volume' cos some nodes have volume as well */
  1376. asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
  1377. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1378. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1379. snd_control.info = snd_asihpi_level_info;
  1380. snd_control.get = snd_asihpi_level_get;
  1381. snd_control.put = snd_asihpi_level_put;
  1382. snd_control.tlv.p = db_scale_level;
  1383. return ctl_add(card, &snd_control, asihpi);
  1384. }
  1385. /*------------------------------------------------------------
  1386. AESEBU controls
  1387. ------------------------------------------------------------*/
  1388. /* AESEBU format */
  1389. static const char * const asihpi_aesebu_format_names[] = {
  1390. "N/A", "S/PDIF", "AES/EBU" };
  1391. static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
  1392. struct snd_ctl_elem_info *uinfo)
  1393. {
  1394. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1395. uinfo->count = 1;
  1396. uinfo->value.enumerated.items = 3;
  1397. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  1398. uinfo->value.enumerated.item =
  1399. uinfo->value.enumerated.items - 1;
  1400. strcpy(uinfo->value.enumerated.name,
  1401. asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
  1402. return 0;
  1403. }
  1404. static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
  1405. struct snd_ctl_elem_value *ucontrol,
  1406. u16 (*func)(u32, u16 *))
  1407. {
  1408. u32 h_control = kcontrol->private_value;
  1409. u16 source, err;
  1410. err = func(h_control, &source);
  1411. /* default to N/A */
  1412. ucontrol->value.enumerated.item[0] = 0;
  1413. /* return success but set the control to N/A */
  1414. if (err)
  1415. return 0;
  1416. if (source == HPI_AESEBU_FORMAT_SPDIF)
  1417. ucontrol->value.enumerated.item[0] = 1;
  1418. if (source == HPI_AESEBU_FORMAT_AESEBU)
  1419. ucontrol->value.enumerated.item[0] = 2;
  1420. return 0;
  1421. }
  1422. static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
  1423. struct snd_ctl_elem_value *ucontrol,
  1424. u16 (*func)(u32, u16))
  1425. {
  1426. u32 h_control = kcontrol->private_value;
  1427. /* default to S/PDIF */
  1428. u16 source = HPI_AESEBU_FORMAT_SPDIF;
  1429. if (ucontrol->value.enumerated.item[0] == 1)
  1430. source = HPI_AESEBU_FORMAT_SPDIF;
  1431. if (ucontrol->value.enumerated.item[0] == 2)
  1432. source = HPI_AESEBU_FORMAT_AESEBU;
  1433. if (func(h_control, source) != 0)
  1434. return -EINVAL;
  1435. return 1;
  1436. }
  1437. static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
  1438. struct snd_ctl_elem_value *ucontrol) {
  1439. return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
  1440. hpi_aesebu_receiver_get_format);
  1441. }
  1442. static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
  1443. struct snd_ctl_elem_value *ucontrol) {
  1444. return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
  1445. hpi_aesebu_receiver_set_format);
  1446. }
  1447. static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
  1448. struct snd_ctl_elem_info *uinfo)
  1449. {
  1450. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1451. uinfo->count = 1;
  1452. uinfo->value.integer.min = 0;
  1453. uinfo->value.integer.max = 0X1F;
  1454. uinfo->value.integer.step = 1;
  1455. return 0;
  1456. }
  1457. static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
  1458. struct snd_ctl_elem_value *ucontrol) {
  1459. u32 h_control = kcontrol->private_value;
  1460. u16 status;
  1461. hpi_handle_error(hpi_aesebu_receiver_get_error_status(
  1462. h_control, &status));
  1463. ucontrol->value.integer.value[0] = status;
  1464. return 0;
  1465. }
  1466. static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
  1467. struct hpi_control *hpi_ctl)
  1468. {
  1469. struct snd_card *card = asihpi->card;
  1470. struct snd_kcontrol_new snd_control;
  1471. asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
  1472. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1473. snd_control.info = snd_asihpi_aesebu_format_info;
  1474. snd_control.get = snd_asihpi_aesebu_rx_format_get;
  1475. snd_control.put = snd_asihpi_aesebu_rx_format_put;
  1476. if (ctl_add(card, &snd_control, asihpi) < 0)
  1477. return -EINVAL;
  1478. asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
  1479. snd_control.access =
  1480. SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  1481. snd_control.info = snd_asihpi_aesebu_rxstatus_info;
  1482. snd_control.get = snd_asihpi_aesebu_rxstatus_get;
  1483. return ctl_add(card, &snd_control, asihpi);
  1484. }
  1485. static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
  1486. struct snd_ctl_elem_value *ucontrol) {
  1487. return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
  1488. hpi_aesebu_transmitter_get_format);
  1489. }
  1490. static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
  1491. struct snd_ctl_elem_value *ucontrol) {
  1492. return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
  1493. hpi_aesebu_transmitter_set_format);
  1494. }
  1495. static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
  1496. struct hpi_control *hpi_ctl)
  1497. {
  1498. struct snd_card *card = asihpi->card;
  1499. struct snd_kcontrol_new snd_control;
  1500. asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
  1501. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1502. snd_control.info = snd_asihpi_aesebu_format_info;
  1503. snd_control.get = snd_asihpi_aesebu_tx_format_get;
  1504. snd_control.put = snd_asihpi_aesebu_tx_format_put;
  1505. return ctl_add(card, &snd_control, asihpi);
  1506. }
  1507. /*------------------------------------------------------------
  1508. Tuner controls
  1509. ------------------------------------------------------------*/
  1510. /* Gain */
  1511. static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
  1512. struct snd_ctl_elem_info *uinfo)
  1513. {
  1514. u32 h_control = kcontrol->private_value;
  1515. u16 err;
  1516. short idx;
  1517. u16 gain_range[3];
  1518. for (idx = 0; idx < 3; idx++) {
  1519. err = hpi_tuner_query_gain(h_control,
  1520. idx, &gain_range[idx]);
  1521. if (err != 0)
  1522. return err;
  1523. }
  1524. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1525. uinfo->count = 1;
  1526. uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
  1527. uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
  1528. uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
  1529. return 0;
  1530. }
  1531. static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
  1532. struct snd_ctl_elem_value *ucontrol)
  1533. {
  1534. /*
  1535. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1536. */
  1537. u32 h_control = kcontrol->private_value;
  1538. short gain;
  1539. hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
  1540. ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
  1541. return 0;
  1542. }
  1543. static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
  1544. struct snd_ctl_elem_value *ucontrol)
  1545. {
  1546. /*
  1547. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1548. */
  1549. u32 h_control = kcontrol->private_value;
  1550. short gain;
  1551. gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
  1552. hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
  1553. return 1;
  1554. }
  1555. /* Band */
  1556. static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
  1557. u16 *band_list, u32 len) {
  1558. u32 h_control = kcontrol->private_value;
  1559. u16 err = 0;
  1560. u32 i;
  1561. for (i = 0; i < len; i++) {
  1562. err = hpi_tuner_query_band(
  1563. h_control, i, &band_list[i]);
  1564. if (err != 0)
  1565. break;
  1566. }
  1567. if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
  1568. return -EIO;
  1569. return i;
  1570. }
  1571. static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
  1572. struct snd_ctl_elem_info *uinfo)
  1573. {
  1574. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1575. int num_bands = 0;
  1576. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1577. HPI_TUNER_BAND_LAST);
  1578. if (num_bands < 0)
  1579. return num_bands;
  1580. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1581. uinfo->count = 1;
  1582. uinfo->value.enumerated.items = num_bands;
  1583. if (num_bands > 0) {
  1584. if (uinfo->value.enumerated.item >=
  1585. uinfo->value.enumerated.items)
  1586. uinfo->value.enumerated.item =
  1587. uinfo->value.enumerated.items - 1;
  1588. strcpy(uinfo->value.enumerated.name,
  1589. asihpi_tuner_band_names[
  1590. tuner_bands[uinfo->value.enumerated.item]]);
  1591. }
  1592. return 0;
  1593. }
  1594. static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
  1595. struct snd_ctl_elem_value *ucontrol)
  1596. {
  1597. u32 h_control = kcontrol->private_value;
  1598. /*
  1599. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1600. */
  1601. u16 band, idx;
  1602. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1603. u32 num_bands = 0;
  1604. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1605. HPI_TUNER_BAND_LAST);
  1606. hpi_handle_error(hpi_tuner_get_band(h_control, &band));
  1607. ucontrol->value.enumerated.item[0] = -1;
  1608. for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
  1609. if (tuner_bands[idx] == band) {
  1610. ucontrol->value.enumerated.item[0] = idx;
  1611. break;
  1612. }
  1613. return 0;
  1614. }
  1615. static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
  1616. struct snd_ctl_elem_value *ucontrol)
  1617. {
  1618. /*
  1619. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1620. */
  1621. u32 h_control = kcontrol->private_value;
  1622. u16 band;
  1623. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1624. u32 num_bands = 0;
  1625. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1626. HPI_TUNER_BAND_LAST);
  1627. band = tuner_bands[ucontrol->value.enumerated.item[0]];
  1628. hpi_handle_error(hpi_tuner_set_band(h_control, band));
  1629. return 1;
  1630. }
  1631. /* Freq */
  1632. static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
  1633. struct snd_ctl_elem_info *uinfo)
  1634. {
  1635. u32 h_control = kcontrol->private_value;
  1636. u16 err;
  1637. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1638. u16 num_bands = 0, band_iter, idx;
  1639. u32 freq_range[3], temp_freq_range[3];
  1640. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1641. HPI_TUNER_BAND_LAST);
  1642. freq_range[0] = INT_MAX;
  1643. freq_range[1] = 0;
  1644. freq_range[2] = INT_MAX;
  1645. for (band_iter = 0; band_iter < num_bands; band_iter++) {
  1646. for (idx = 0; idx < 3; idx++) {
  1647. err = hpi_tuner_query_frequency(h_control,
  1648. idx, tuner_bands[band_iter],
  1649. &temp_freq_range[idx]);
  1650. if (err != 0)
  1651. return err;
  1652. }
  1653. /* skip band with bogus stepping */
  1654. if (temp_freq_range[2] <= 0)
  1655. continue;
  1656. if (temp_freq_range[0] < freq_range[0])
  1657. freq_range[0] = temp_freq_range[0];
  1658. if (temp_freq_range[1] > freq_range[1])
  1659. freq_range[1] = temp_freq_range[1];
  1660. if (temp_freq_range[2] < freq_range[2])
  1661. freq_range[2] = temp_freq_range[2];
  1662. }
  1663. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1664. uinfo->count = 1;
  1665. uinfo->value.integer.min = ((int)freq_range[0]);
  1666. uinfo->value.integer.max = ((int)freq_range[1]);
  1667. uinfo->value.integer.step = ((int)freq_range[2]);
  1668. return 0;
  1669. }
  1670. static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
  1671. struct snd_ctl_elem_value *ucontrol)
  1672. {
  1673. u32 h_control = kcontrol->private_value;
  1674. u32 freq;
  1675. hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
  1676. ucontrol->value.integer.value[0] = freq;
  1677. return 0;
  1678. }
  1679. static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
  1680. struct snd_ctl_elem_value *ucontrol)
  1681. {
  1682. u32 h_control = kcontrol->private_value;
  1683. u32 freq;
  1684. freq = ucontrol->value.integer.value[0];
  1685. hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
  1686. return 1;
  1687. }
  1688. /* Tuner control group initializer */
  1689. static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
  1690. struct hpi_control *hpi_ctl)
  1691. {
  1692. struct snd_card *card = asihpi->card;
  1693. struct snd_kcontrol_new snd_control;
  1694. snd_control.private_value = hpi_ctl->h_control;
  1695. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1696. if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
  1697. asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
  1698. snd_control.info = snd_asihpi_tuner_gain_info;
  1699. snd_control.get = snd_asihpi_tuner_gain_get;
  1700. snd_control.put = snd_asihpi_tuner_gain_put;
  1701. if (ctl_add(card, &snd_control, asihpi) < 0)
  1702. return -EINVAL;
  1703. }
  1704. asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
  1705. snd_control.info = snd_asihpi_tuner_band_info;
  1706. snd_control.get = snd_asihpi_tuner_band_get;
  1707. snd_control.put = snd_asihpi_tuner_band_put;
  1708. if (ctl_add(card, &snd_control, asihpi) < 0)
  1709. return -EINVAL;
  1710. asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
  1711. snd_control.info = snd_asihpi_tuner_freq_info;
  1712. snd_control.get = snd_asihpi_tuner_freq_get;
  1713. snd_control.put = snd_asihpi_tuner_freq_put;
  1714. return ctl_add(card, &snd_control, asihpi);
  1715. }
  1716. /*------------------------------------------------------------
  1717. Meter controls
  1718. ------------------------------------------------------------*/
  1719. static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
  1720. struct snd_ctl_elem_info *uinfo)
  1721. {
  1722. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1723. uinfo->count = HPI_MAX_CHANNELS;
  1724. uinfo->value.integer.min = 0;
  1725. uinfo->value.integer.max = 0x7FFFFFFF;
  1726. return 0;
  1727. }
  1728. /* linear values for 10dB steps */
  1729. static int log2lin[] = {
  1730. 0x7FFFFFFF, /* 0dB */
  1731. 679093956,
  1732. 214748365,
  1733. 67909396,
  1734. 21474837,
  1735. 6790940,
  1736. 2147484, /* -60dB */
  1737. 679094,
  1738. 214748, /* -80 */
  1739. 67909,
  1740. 21475, /* -100 */
  1741. 6791,
  1742. 2147,
  1743. 679,
  1744. 214,
  1745. 68,
  1746. 21,
  1747. 7,
  1748. 2
  1749. };
  1750. static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
  1751. struct snd_ctl_elem_value *ucontrol)
  1752. {
  1753. u32 h_control = kcontrol->private_value;
  1754. short an_gain_mB[HPI_MAX_CHANNELS], i;
  1755. u16 err;
  1756. err = hpi_meter_get_peak(h_control, an_gain_mB);
  1757. for (i = 0; i < HPI_MAX_CHANNELS; i++) {
  1758. if (err) {
  1759. ucontrol->value.integer.value[i] = 0;
  1760. } else if (an_gain_mB[i] >= 0) {
  1761. ucontrol->value.integer.value[i] =
  1762. an_gain_mB[i] << 16;
  1763. } else {
  1764. /* -ve is log value in millibels < -60dB,
  1765. * convert to (roughly!) linear,
  1766. */
  1767. ucontrol->value.integer.value[i] =
  1768. log2lin[an_gain_mB[i] / -1000];
  1769. }
  1770. }
  1771. return 0;
  1772. }
  1773. static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
  1774. struct hpi_control *hpi_ctl, int subidx)
  1775. {
  1776. struct snd_card *card = asihpi->card;
  1777. struct snd_kcontrol_new snd_control;
  1778. asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
  1779. snd_control.access =
  1780. SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  1781. snd_control.info = snd_asihpi_meter_info;
  1782. snd_control.get = snd_asihpi_meter_get;
  1783. snd_control.index = subidx;
  1784. return ctl_add(card, &snd_control, asihpi);
  1785. }
  1786. /*------------------------------------------------------------
  1787. Multiplexer controls
  1788. ------------------------------------------------------------*/
  1789. static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
  1790. {
  1791. u32 h_control = snd_control->private_value;
  1792. struct hpi_control hpi_ctl;
  1793. int s, err;
  1794. for (s = 0; s < 32; s++) {
  1795. err = hpi_multiplexer_query_source(h_control, s,
  1796. &hpi_ctl.
  1797. src_node_type,
  1798. &hpi_ctl.
  1799. src_node_index);
  1800. if (err)
  1801. break;
  1802. }
  1803. return s;
  1804. }
  1805. static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
  1806. struct snd_ctl_elem_info *uinfo)
  1807. {
  1808. int err;
  1809. u16 src_node_type, src_node_index;
  1810. u32 h_control = kcontrol->private_value;
  1811. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1812. uinfo->count = 1;
  1813. uinfo->value.enumerated.items =
  1814. snd_card_asihpi_mux_count_sources(kcontrol);
  1815. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  1816. uinfo->value.enumerated.item =
  1817. uinfo->value.enumerated.items - 1;
  1818. err =
  1819. hpi_multiplexer_query_source(h_control,
  1820. uinfo->value.enumerated.item,
  1821. &src_node_type, &src_node_index);
  1822. sprintf(uinfo->value.enumerated.name, "%s %d",
  1823. asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
  1824. src_node_index);
  1825. return 0;
  1826. }
  1827. static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
  1828. struct snd_ctl_elem_value *ucontrol)
  1829. {
  1830. u32 h_control = kcontrol->private_value;
  1831. u16 source_type, source_index;
  1832. u16 src_node_type, src_node_index;
  1833. int s;
  1834. hpi_handle_error(hpi_multiplexer_get_source(h_control,
  1835. &source_type, &source_index));
  1836. /* Should cache this search result! */
  1837. for (s = 0; s < 256; s++) {
  1838. if (hpi_multiplexer_query_source(h_control, s,
  1839. &src_node_type, &src_node_index))
  1840. break;
  1841. if ((source_type == src_node_type)
  1842. && (source_index == src_node_index)) {
  1843. ucontrol->value.enumerated.item[0] = s;
  1844. return 0;
  1845. }
  1846. }
  1847. snd_printd(KERN_WARNING
  1848. "Control %x failed to match mux source %hu %hu\n",
  1849. h_control, source_type, source_index);
  1850. ucontrol->value.enumerated.item[0] = 0;
  1851. return 0;
  1852. }
  1853. static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
  1854. struct snd_ctl_elem_value *ucontrol)
  1855. {
  1856. int change;
  1857. u32 h_control = kcontrol->private_value;
  1858. u16 source_type, source_index;
  1859. u16 e;
  1860. change = 1;
  1861. e = hpi_multiplexer_query_source(h_control,
  1862. ucontrol->value.enumerated.item[0],
  1863. &source_type, &source_index);
  1864. if (!e)
  1865. hpi_handle_error(
  1866. hpi_multiplexer_set_source(h_control,
  1867. source_type, source_index));
  1868. return change;
  1869. }
  1870. static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
  1871. struct hpi_control *hpi_ctl)
  1872. {
  1873. struct snd_card *card = asihpi->card;
  1874. struct snd_kcontrol_new snd_control;
  1875. asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
  1876. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1877. snd_control.info = snd_asihpi_mux_info;
  1878. snd_control.get = snd_asihpi_mux_get;
  1879. snd_control.put = snd_asihpi_mux_put;
  1880. return ctl_add(card, &snd_control, asihpi);
  1881. }
  1882. /*------------------------------------------------------------
  1883. Channel mode controls
  1884. ------------------------------------------------------------*/
  1885. static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
  1886. struct snd_ctl_elem_info *uinfo)
  1887. {
  1888. static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
  1889. "invalid",
  1890. "Normal", "Swap",
  1891. "From Left", "From Right",
  1892. "To Left", "To Right"
  1893. };
  1894. u32 h_control = kcontrol->private_value;
  1895. u16 mode;
  1896. int i;
  1897. u16 mode_map[6];
  1898. int valid_modes = 0;
  1899. /* HPI channel mode values can be from 1 to 6
  1900. Some adapters only support a contiguous subset
  1901. */
  1902. for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
  1903. if (!hpi_channel_mode_query_mode(
  1904. h_control, i, &mode)) {
  1905. mode_map[valid_modes] = mode;
  1906. valid_modes++;
  1907. }
  1908. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1909. uinfo->count = 1;
  1910. uinfo->value.enumerated.items = valid_modes;
  1911. if (uinfo->value.enumerated.item >= valid_modes)
  1912. uinfo->value.enumerated.item = valid_modes - 1;
  1913. strcpy(uinfo->value.enumerated.name,
  1914. mode_names[mode_map[uinfo->value.enumerated.item]]);
  1915. return 0;
  1916. }
  1917. static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
  1918. struct snd_ctl_elem_value *ucontrol)
  1919. {
  1920. u32 h_control = kcontrol->private_value;
  1921. u16 mode;
  1922. if (hpi_channel_mode_get(h_control, &mode))
  1923. mode = 1;
  1924. ucontrol->value.enumerated.item[0] = mode - 1;
  1925. return 0;
  1926. }
  1927. static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
  1928. struct snd_ctl_elem_value *ucontrol)
  1929. {
  1930. int change;
  1931. u32 h_control = kcontrol->private_value;
  1932. change = 1;
  1933. hpi_handle_error(hpi_channel_mode_set(h_control,
  1934. ucontrol->value.enumerated.item[0] + 1));
  1935. return change;
  1936. }
  1937. static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
  1938. struct hpi_control *hpi_ctl)
  1939. {
  1940. struct snd_card *card = asihpi->card;
  1941. struct snd_kcontrol_new snd_control;
  1942. asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
  1943. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1944. snd_control.info = snd_asihpi_cmode_info;
  1945. snd_control.get = snd_asihpi_cmode_get;
  1946. snd_control.put = snd_asihpi_cmode_put;
  1947. return ctl_add(card, &snd_control, asihpi);
  1948. }
  1949. /*------------------------------------------------------------
  1950. Sampleclock source controls
  1951. ------------------------------------------------------------*/
  1952. static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
  1953. "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
  1954. "SMPTE", "Digital1", "Auto", "Network", "Invalid",
  1955. "Prev Module",
  1956. "Digital2", "Digital3", "Digital4", "Digital5",
  1957. "Digital6", "Digital7", "Digital8"};
  1958. static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
  1959. struct snd_ctl_elem_info *uinfo)
  1960. {
  1961. struct snd_card_asihpi *asihpi =
  1962. (struct snd_card_asihpi *)(kcontrol->private_data);
  1963. struct clk_cache *clkcache = &asihpi->cc;
  1964. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1965. uinfo->count = 1;
  1966. uinfo->value.enumerated.items = clkcache->count;
  1967. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  1968. uinfo->value.enumerated.item =
  1969. uinfo->value.enumerated.items - 1;
  1970. strcpy(uinfo->value.enumerated.name,
  1971. clkcache->s[uinfo->value.enumerated.item].name);
  1972. return 0;
  1973. }
  1974. static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
  1975. struct snd_ctl_elem_value *ucontrol)
  1976. {
  1977. struct snd_card_asihpi *asihpi =
  1978. (struct snd_card_asihpi *)(kcontrol->private_data);
  1979. struct clk_cache *clkcache = &asihpi->cc;
  1980. u32 h_control = kcontrol->private_value;
  1981. u16 source, srcindex = 0;
  1982. int i;
  1983. ucontrol->value.enumerated.item[0] = 0;
  1984. if (hpi_sample_clock_get_source(h_control, &source))
  1985. source = 0;
  1986. if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  1987. if (hpi_sample_clock_get_source_index(h_control, &srcindex))
  1988. srcindex = 0;
  1989. for (i = 0; i < clkcache->count; i++)
  1990. if ((clkcache->s[i].source == source) &&
  1991. (clkcache->s[i].index == srcindex))
  1992. break;
  1993. ucontrol->value.enumerated.item[0] = i;
  1994. return 0;
  1995. }
  1996. static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
  1997. struct snd_ctl_elem_value *ucontrol)
  1998. {
  1999. struct snd_card_asihpi *asihpi =
  2000. (struct snd_card_asihpi *)(kcontrol->private_data);
  2001. struct clk_cache *clkcache = &asihpi->cc;
  2002. int change, item;
  2003. u32 h_control = kcontrol->private_value;
  2004. change = 1;
  2005. item = ucontrol->value.enumerated.item[0];
  2006. if (item >= clkcache->count)
  2007. item = clkcache->count-1;
  2008. hpi_handle_error(hpi_sample_clock_set_source(
  2009. h_control, clkcache->s[item].source));
  2010. if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  2011. hpi_handle_error(hpi_sample_clock_set_source_index(
  2012. h_control, clkcache->s[item].index));
  2013. return change;
  2014. }
  2015. /*------------------------------------------------------------
  2016. Clkrate controls
  2017. ------------------------------------------------------------*/
  2018. /* Need to change this to enumerated control with list of rates */
  2019. static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
  2020. struct snd_ctl_elem_info *uinfo)
  2021. {
  2022. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2023. uinfo->count = 1;
  2024. uinfo->value.integer.min = 8000;
  2025. uinfo->value.integer.max = 192000;
  2026. uinfo->value.integer.step = 100;
  2027. return 0;
  2028. }
  2029. static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
  2030. struct snd_ctl_elem_value *ucontrol)
  2031. {
  2032. u32 h_control = kcontrol->private_value;
  2033. u32 rate;
  2034. u16 e;
  2035. e = hpi_sample_clock_get_local_rate(h_control, &rate);
  2036. if (!e)
  2037. ucontrol->value.integer.value[0] = rate;
  2038. else
  2039. ucontrol->value.integer.value[0] = 0;
  2040. return 0;
  2041. }
  2042. static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
  2043. struct snd_ctl_elem_value *ucontrol)
  2044. {
  2045. int change;
  2046. u32 h_control = kcontrol->private_value;
  2047. /* change = asihpi->mixer_clkrate[addr][0] != left ||
  2048. asihpi->mixer_clkrate[addr][1] != right;
  2049. */
  2050. change = 1;
  2051. hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
  2052. ucontrol->value.integer.value[0]));
  2053. return change;
  2054. }
  2055. static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
  2056. struct snd_ctl_elem_info *uinfo)
  2057. {
  2058. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2059. uinfo->count = 1;
  2060. uinfo->value.integer.min = 8000;
  2061. uinfo->value.integer.max = 192000;
  2062. uinfo->value.integer.step = 100;
  2063. return 0;
  2064. }
  2065. static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
  2066. struct snd_ctl_elem_value *ucontrol)
  2067. {
  2068. u32 h_control = kcontrol->private_value;
  2069. u32 rate;
  2070. u16 e;
  2071. e = hpi_sample_clock_get_sample_rate(h_control, &rate);
  2072. if (!e)
  2073. ucontrol->value.integer.value[0] = rate;
  2074. else
  2075. ucontrol->value.integer.value[0] = 0;
  2076. return 0;
  2077. }
  2078. static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
  2079. struct hpi_control *hpi_ctl)
  2080. {
  2081. struct snd_card *card = asihpi->card;
  2082. struct snd_kcontrol_new snd_control;
  2083. struct clk_cache *clkcache = &asihpi->cc;
  2084. u32 hSC = hpi_ctl->h_control;
  2085. int has_aes_in = 0;
  2086. int i, j;
  2087. u16 source;
  2088. snd_control.private_value = hpi_ctl->h_control;
  2089. clkcache->has_local = 0;
  2090. for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
  2091. if (hpi_sample_clock_query_source(hSC,
  2092. i, &source))
  2093. break;
  2094. clkcache->s[i].source = source;
  2095. clkcache->s[i].index = 0;
  2096. clkcache->s[i].name = sampleclock_sources[source];
  2097. if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  2098. has_aes_in = 1;
  2099. if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
  2100. clkcache->has_local = 1;
  2101. }
  2102. if (has_aes_in)
  2103. /* already will have picked up index 0 above */
  2104. for (j = 1; j < 8; j++) {
  2105. if (hpi_sample_clock_query_source_index(hSC,
  2106. j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
  2107. &source))
  2108. break;
  2109. clkcache->s[i].source =
  2110. HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
  2111. clkcache->s[i].index = j;
  2112. clkcache->s[i].name = sampleclock_sources[
  2113. j+HPI_SAMPLECLOCK_SOURCE_LAST];
  2114. i++;
  2115. }
  2116. clkcache->count = i;
  2117. asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
  2118. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
  2119. snd_control.info = snd_asihpi_clksrc_info;
  2120. snd_control.get = snd_asihpi_clksrc_get;
  2121. snd_control.put = snd_asihpi_clksrc_put;
  2122. if (ctl_add(card, &snd_control, asihpi) < 0)
  2123. return -EINVAL;
  2124. if (clkcache->has_local) {
  2125. asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
  2126. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
  2127. snd_control.info = snd_asihpi_clklocal_info;
  2128. snd_control.get = snd_asihpi_clklocal_get;
  2129. snd_control.put = snd_asihpi_clklocal_put;
  2130. if (ctl_add(card, &snd_control, asihpi) < 0)
  2131. return -EINVAL;
  2132. }
  2133. asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
  2134. snd_control.access =
  2135. SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  2136. snd_control.info = snd_asihpi_clkrate_info;
  2137. snd_control.get = snd_asihpi_clkrate_get;
  2138. return ctl_add(card, &snd_control, asihpi);
  2139. }
  2140. /*------------------------------------------------------------
  2141. Mixer
  2142. ------------------------------------------------------------*/
  2143. static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
  2144. {
  2145. struct snd_card *card = asihpi->card;
  2146. unsigned int idx = 0;
  2147. unsigned int subindex = 0;
  2148. int err;
  2149. struct hpi_control hpi_ctl, prev_ctl;
  2150. if (snd_BUG_ON(!asihpi))
  2151. return -EINVAL;
  2152. strcpy(card->mixername, "Asihpi Mixer");
  2153. err =
  2154. hpi_mixer_open(asihpi->adapter_index,
  2155. &asihpi->h_mixer);
  2156. hpi_handle_error(err);
  2157. if (err)
  2158. return -err;
  2159. memset(&prev_ctl, 0, sizeof(prev_ctl));
  2160. prev_ctl.control_type = -1;
  2161. for (idx = 0; idx < 2000; idx++) {
  2162. err = hpi_mixer_get_control_by_index(
  2163. asihpi->h_mixer,
  2164. idx,
  2165. &hpi_ctl.src_node_type,
  2166. &hpi_ctl.src_node_index,
  2167. &hpi_ctl.dst_node_type,
  2168. &hpi_ctl.dst_node_index,
  2169. &hpi_ctl.control_type,
  2170. &hpi_ctl.h_control);
  2171. if (err) {
  2172. if (err == HPI_ERROR_CONTROL_DISABLED) {
  2173. if (mixer_dump)
  2174. snd_printk(KERN_INFO
  2175. "Disabled HPI Control(%d)\n",
  2176. idx);
  2177. continue;
  2178. } else
  2179. break;
  2180. }
  2181. hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
  2182. hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
  2183. /* ASI50xx in SSX mode has multiple meters on the same node.
  2184. Use subindex to create distinct ALSA controls
  2185. for any duplicated controls.
  2186. */
  2187. if ((hpi_ctl.control_type == prev_ctl.control_type) &&
  2188. (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
  2189. (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
  2190. (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
  2191. (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
  2192. subindex++;
  2193. else
  2194. subindex = 0;
  2195. prev_ctl = hpi_ctl;
  2196. switch (hpi_ctl.control_type) {
  2197. case HPI_CONTROL_VOLUME:
  2198. err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
  2199. break;
  2200. case HPI_CONTROL_LEVEL:
  2201. err = snd_asihpi_level_add(asihpi, &hpi_ctl);
  2202. break;
  2203. case HPI_CONTROL_MULTIPLEXER:
  2204. err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
  2205. break;
  2206. case HPI_CONTROL_CHANNEL_MODE:
  2207. err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
  2208. break;
  2209. case HPI_CONTROL_METER:
  2210. err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
  2211. break;
  2212. case HPI_CONTROL_SAMPLECLOCK:
  2213. err = snd_asihpi_sampleclock_add(
  2214. asihpi, &hpi_ctl);
  2215. break;
  2216. case HPI_CONTROL_CONNECTION: /* ignore these */
  2217. continue;
  2218. case HPI_CONTROL_TUNER:
  2219. err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
  2220. break;
  2221. case HPI_CONTROL_AESEBU_TRANSMITTER:
  2222. err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
  2223. break;
  2224. case HPI_CONTROL_AESEBU_RECEIVER:
  2225. err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
  2226. break;
  2227. case HPI_CONTROL_VOX:
  2228. case HPI_CONTROL_BITSTREAM:
  2229. case HPI_CONTROL_MICROPHONE:
  2230. case HPI_CONTROL_PARAMETRIC_EQ:
  2231. case HPI_CONTROL_COMPANDER:
  2232. default:
  2233. if (mixer_dump)
  2234. snd_printk(KERN_INFO
  2235. "Untranslated HPI Control"
  2236. "(%d) %d %d %d %d %d\n",
  2237. idx,
  2238. hpi_ctl.control_type,
  2239. hpi_ctl.src_node_type,
  2240. hpi_ctl.src_node_index,
  2241. hpi_ctl.dst_node_type,
  2242. hpi_ctl.dst_node_index);
  2243. continue;
  2244. };
  2245. if (err < 0)
  2246. return err;
  2247. }
  2248. if (HPI_ERROR_INVALID_OBJ_INDEX != err)
  2249. hpi_handle_error(err);
  2250. snd_printk(KERN_INFO "%d mixer controls found\n", idx);
  2251. return 0;
  2252. }
  2253. /*------------------------------------------------------------
  2254. /proc interface
  2255. ------------------------------------------------------------*/
  2256. static void
  2257. snd_asihpi_proc_read(struct snd_info_entry *entry,
  2258. struct snd_info_buffer *buffer)
  2259. {
  2260. struct snd_card_asihpi *asihpi = entry->private_data;
  2261. u16 version;
  2262. u32 h_control;
  2263. u32 rate = 0;
  2264. u16 source = 0;
  2265. int err;
  2266. snd_iprintf(buffer, "ASIHPI driver proc file\n");
  2267. snd_iprintf(buffer,
  2268. "adapter ID=%4X\n_index=%d\n"
  2269. "num_outstreams=%d\n_num_instreams=%d\n",
  2270. asihpi->type, asihpi->adapter_index,
  2271. asihpi->num_outstreams, asihpi->num_instreams);
  2272. version = asihpi->version;
  2273. snd_iprintf(buffer,
  2274. "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
  2275. asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
  2276. version & 0x7,
  2277. ((version >> 13) * 100) + ((version >> 7) & 0x3f));
  2278. err = hpi_mixer_get_control(asihpi->h_mixer,
  2279. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  2280. HPI_CONTROL_SAMPLECLOCK, &h_control);
  2281. if (!err) {
  2282. err = hpi_sample_clock_get_sample_rate(
  2283. h_control, &rate);
  2284. err += hpi_sample_clock_get_source(h_control, &source);
  2285. if (!err)
  2286. snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
  2287. rate, sampleclock_sources[source]);
  2288. }
  2289. }
  2290. static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
  2291. {
  2292. struct snd_info_entry *entry;
  2293. if (!snd_card_proc_new(asihpi->card, "info", &entry))
  2294. snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
  2295. }
  2296. /*------------------------------------------------------------
  2297. HWDEP
  2298. ------------------------------------------------------------*/
  2299. static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
  2300. {
  2301. if (enable_hpi_hwdep)
  2302. return 0;
  2303. else
  2304. return -ENODEV;
  2305. }
  2306. static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
  2307. {
  2308. if (enable_hpi_hwdep)
  2309. return asihpi_hpi_release(file);
  2310. else
  2311. return -ENODEV;
  2312. }
  2313. static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
  2314. unsigned int cmd, unsigned long arg)
  2315. {
  2316. if (enable_hpi_hwdep)
  2317. return asihpi_hpi_ioctl(file, cmd, arg);
  2318. else
  2319. return -ENODEV;
  2320. }
  2321. /* results in /dev/snd/hwC#D0 file for each card with index #
  2322. also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
  2323. */
  2324. static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
  2325. int device, struct snd_hwdep **rhwdep)
  2326. {
  2327. struct snd_hwdep *hw;
  2328. int err;
  2329. if (rhwdep)
  2330. *rhwdep = NULL;
  2331. err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
  2332. if (err < 0)
  2333. return err;
  2334. strcpy(hw->name, "asihpi (HPI)");
  2335. hw->iface = SNDRV_HWDEP_IFACE_LAST;
  2336. hw->ops.open = snd_asihpi_hpi_open;
  2337. hw->ops.ioctl = snd_asihpi_hpi_ioctl;
  2338. hw->ops.release = snd_asihpi_hpi_release;
  2339. hw->private_data = asihpi;
  2340. if (rhwdep)
  2341. *rhwdep = hw;
  2342. return 0;
  2343. }
  2344. /*------------------------------------------------------------
  2345. CARD
  2346. ------------------------------------------------------------*/
  2347. static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
  2348. const struct pci_device_id *pci_id)
  2349. {
  2350. int err;
  2351. u16 version;
  2352. int pcm_substreams;
  2353. struct hpi_adapter *hpi_card;
  2354. struct snd_card *card;
  2355. struct snd_card_asihpi *asihpi;
  2356. u32 h_control;
  2357. u32 h_stream;
  2358. static int dev;
  2359. if (dev >= SNDRV_CARDS)
  2360. return -ENODEV;
  2361. /* Should this be enable[hpi_card->index] ? */
  2362. if (!enable[dev]) {
  2363. dev++;
  2364. return -ENOENT;
  2365. }
  2366. err = asihpi_adapter_probe(pci_dev, pci_id);
  2367. if (err < 0)
  2368. return err;
  2369. hpi_card = pci_get_drvdata(pci_dev);
  2370. /* first try to give the card the same index as its hardware index */
  2371. err = snd_card_create(hpi_card->index,
  2372. id[hpi_card->index], THIS_MODULE,
  2373. sizeof(struct snd_card_asihpi),
  2374. &card);
  2375. if (err < 0) {
  2376. /* if that fails, try the default index==next available */
  2377. err =
  2378. snd_card_create(index[dev], id[dev],
  2379. THIS_MODULE,
  2380. sizeof(struct snd_card_asihpi),
  2381. &card);
  2382. if (err < 0)
  2383. return err;
  2384. snd_printk(KERN_WARNING
  2385. "**** WARNING **** Adapter index %d->ALSA index %d\n",
  2386. hpi_card->index, card->number);
  2387. }
  2388. snd_card_set_dev(card, &pci_dev->dev);
  2389. asihpi = (struct snd_card_asihpi *) card->private_data;
  2390. asihpi->card = card;
  2391. asihpi->pci = pci_dev;
  2392. asihpi->adapter_index = hpi_card->index;
  2393. hpi_handle_error(hpi_adapter_get_info(
  2394. asihpi->adapter_index,
  2395. &asihpi->num_outstreams,
  2396. &asihpi->num_instreams,
  2397. &asihpi->version,
  2398. &asihpi->serial_number, &asihpi->type));
  2399. version = asihpi->version;
  2400. snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
  2401. "num_instreams=%d S/N=%d\n"
  2402. "Hw Version %c%d DSP code version %03d\n",
  2403. asihpi->type, asihpi->adapter_index,
  2404. asihpi->num_outstreams,
  2405. asihpi->num_instreams, asihpi->serial_number,
  2406. ((version >> 3) & 0xf) + 'A',
  2407. version & 0x7,
  2408. ((version >> 13) * 100) + ((version >> 7) & 0x3f));
  2409. pcm_substreams = asihpi->num_outstreams;
  2410. if (pcm_substreams < asihpi->num_instreams)
  2411. pcm_substreams = asihpi->num_instreams;
  2412. err = hpi_adapter_get_property(asihpi->adapter_index,
  2413. HPI_ADAPTER_PROPERTY_CAPS1,
  2414. NULL, &asihpi->support_grouping);
  2415. if (err)
  2416. asihpi->support_grouping = 0;
  2417. err = hpi_adapter_get_property(asihpi->adapter_index,
  2418. HPI_ADAPTER_PROPERTY_CAPS2,
  2419. &asihpi->support_mrx, NULL);
  2420. if (err)
  2421. asihpi->support_mrx = 0;
  2422. err = hpi_adapter_get_property(asihpi->adapter_index,
  2423. HPI_ADAPTER_PROPERTY_INTERVAL,
  2424. NULL, &asihpi->update_interval_frames);
  2425. if (err)
  2426. asihpi->update_interval_frames = 512;
  2427. if (!asihpi->can_dma)
  2428. asihpi->update_interval_frames *= 2;
  2429. hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
  2430. 0, &h_stream));
  2431. err = hpi_instream_host_buffer_free(h_stream);
  2432. asihpi->can_dma = (!err);
  2433. hpi_handle_error(hpi_instream_close(h_stream));
  2434. err = hpi_adapter_get_property(asihpi->adapter_index,
  2435. HPI_ADAPTER_PROPERTY_CURCHANNELS,
  2436. &asihpi->in_max_chans, &asihpi->out_max_chans);
  2437. if (err) {
  2438. asihpi->in_max_chans = 2;
  2439. asihpi->out_max_chans = 2;
  2440. }
  2441. snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
  2442. asihpi->can_dma,
  2443. asihpi->support_grouping,
  2444. asihpi->support_mrx
  2445. );
  2446. err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
  2447. if (err < 0) {
  2448. snd_printk(KERN_ERR "pcm_new failed\n");
  2449. goto __nodev;
  2450. }
  2451. err = snd_card_asihpi_mixer_new(asihpi);
  2452. if (err < 0) {
  2453. snd_printk(KERN_ERR "mixer_new failed\n");
  2454. goto __nodev;
  2455. }
  2456. err = hpi_mixer_get_control(asihpi->h_mixer,
  2457. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  2458. HPI_CONTROL_SAMPLECLOCK, &h_control);
  2459. if (!err)
  2460. err = hpi_sample_clock_set_local_rate(
  2461. h_control, adapter_fs);
  2462. snd_asihpi_proc_init(asihpi);
  2463. /* always create, can be enabled or disabled dynamically
  2464. by enable_hwdep module param*/
  2465. snd_asihpi_hpi_new(asihpi, 0, NULL);
  2466. strcpy(card->driver, "ASIHPI");
  2467. sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
  2468. sprintf(card->longname, "%s %i",
  2469. card->shortname, asihpi->adapter_index);
  2470. err = snd_card_register(card);
  2471. if (!err) {
  2472. hpi_card->snd_card_asihpi = card;
  2473. dev++;
  2474. return 0;
  2475. }
  2476. __nodev:
  2477. snd_card_free(card);
  2478. snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
  2479. return err;
  2480. }
  2481. static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
  2482. {
  2483. struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
  2484. snd_card_free(hpi_card->snd_card_asihpi);
  2485. hpi_card->snd_card_asihpi = NULL;
  2486. asihpi_adapter_remove(pci_dev);
  2487. }
  2488. static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
  2489. {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
  2490. HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
  2491. (kernel_ulong_t)HPI_6205},
  2492. {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
  2493. HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
  2494. (kernel_ulong_t)HPI_6000},
  2495. {0,}
  2496. };
  2497. MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
  2498. static struct pci_driver driver = {
  2499. .name = KBUILD_MODNAME,
  2500. .id_table = asihpi_pci_tbl,
  2501. .probe = snd_asihpi_probe,
  2502. .remove = __devexit_p(snd_asihpi_remove),
  2503. #ifdef CONFIG_PM
  2504. /* .suspend = snd_asihpi_suspend,
  2505. .resume = snd_asihpi_resume, */
  2506. #endif
  2507. };
  2508. static int __init snd_asihpi_init(void)
  2509. {
  2510. asihpi_init();
  2511. return pci_register_driver(&driver);
  2512. }
  2513. static void __exit snd_asihpi_exit(void)
  2514. {
  2515. pci_unregister_driver(&driver);
  2516. asihpi_exit();
  2517. }
  2518. module_init(snd_asihpi_init)
  2519. module_exit(snd_asihpi_exit)