asihpi.c 81 KB

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