asihpi.c 80 KB

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