asihpi.c 81 KB

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