asihpi.c 81 KB

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