asihpi.c 83 KB

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