asihpi.c 80 KB

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