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