asihpi.c 79 KB

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