asihpi.c 81 KB

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