asihpi.c 80 KB

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