asihpi.c 80 KB

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