mixart.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * Driver for Digigram miXart soundcards
  3. *
  4. * main file with alsa callbacks
  5. *
  6. * Copyright (c) 2003 by Digigram <alsa@digigram.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/pci.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/slab.h>
  29. #include <sound/core.h>
  30. #include <sound/initval.h>
  31. #include <sound/info.h>
  32. #include <sound/control.h>
  33. #include <sound/pcm.h>
  34. #include <sound/pcm_params.h>
  35. #include "mixart.h"
  36. #include "mixart_hwdep.h"
  37. #include "mixart_core.h"
  38. #include "mixart_mixer.h"
  39. #define CARD_NAME "miXart"
  40. MODULE_AUTHOR("Digigram <alsa@digigram.com>");
  41. MODULE_DESCRIPTION("Digigram " CARD_NAME);
  42. MODULE_LICENSE("GPL");
  43. MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");
  44. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  45. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  46. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  47. module_param_array(index, int, NULL, 0444);
  48. MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
  49. module_param_array(id, charp, NULL, 0444);
  50. MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
  51. module_param_array(enable, bool, NULL, 0444);
  52. MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
  53. /*
  54. */
  55. static DEFINE_PCI_DEVICE_TABLE(snd_mixart_ids) = {
  56. { PCI_VDEVICE(MOTOROLA, 0x0003), 0, }, /* MC8240 */
  57. { 0, }
  58. };
  59. MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
  60. static int mixart_set_pipe_state(struct mixart_mgr *mgr,
  61. struct mixart_pipe *pipe, int start)
  62. {
  63. struct mixart_group_state_req group_state;
  64. struct mixart_group_state_resp group_state_resp;
  65. struct mixart_msg request;
  66. int err;
  67. u32 system_msg_uid;
  68. switch(pipe->status) {
  69. case PIPE_RUNNING:
  70. case PIPE_CLOCK_SET:
  71. if(start) return 0; /* already started */
  72. break;
  73. case PIPE_STOPPED:
  74. if(!start) return 0; /* already stopped */
  75. break;
  76. default:
  77. snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
  78. return -EINVAL; /* function called with wrong pipe status */
  79. }
  80. system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
  81. /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
  82. request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
  83. request.uid = (struct mixart_uid){0,0};
  84. request.data = &system_msg_uid;
  85. request.size = sizeof(system_msg_uid);
  86. err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
  87. if(err) {
  88. snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
  89. return err;
  90. }
  91. /* start or stop the pipe (1 pipe) */
  92. memset(&group_state, 0, sizeof(group_state));
  93. group_state.pipe_count = 1;
  94. group_state.pipe_uid[0] = pipe->group_uid;
  95. if(start)
  96. request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
  97. else
  98. request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
  99. request.uid = pipe->group_uid; /*(struct mixart_uid){0,0};*/
  100. request.data = &group_state;
  101. request.size = sizeof(group_state);
  102. err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
  103. if (err < 0 || group_state_resp.txx_status != 0) {
  104. snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
  105. return -EINVAL;
  106. }
  107. if(start) {
  108. u32 stat;
  109. group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
  110. err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
  111. if (err < 0 || group_state_resp.txx_status != 0) {
  112. snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
  113. return -EINVAL;
  114. }
  115. /* in case of start send a synchro top */
  116. request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
  117. request.uid = (struct mixart_uid){0,0};
  118. request.data = NULL;
  119. request.size = 0;
  120. err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
  121. if (err < 0 || stat != 0) {
  122. snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
  123. return -EINVAL;
  124. }
  125. pipe->status = PIPE_RUNNING;
  126. }
  127. else /* !start */
  128. pipe->status = PIPE_STOPPED;
  129. return 0;
  130. }
  131. static int mixart_set_clock(struct mixart_mgr *mgr,
  132. struct mixart_pipe *pipe, unsigned int rate)
  133. {
  134. struct mixart_msg request;
  135. struct mixart_clock_properties clock_properties;
  136. struct mixart_clock_properties_resp clock_prop_resp;
  137. int err;
  138. switch(pipe->status) {
  139. case PIPE_CLOCK_SET:
  140. break;
  141. case PIPE_RUNNING:
  142. if(rate != 0)
  143. break;
  144. default:
  145. if(rate == 0)
  146. return 0; /* nothing to do */
  147. else {
  148. snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
  149. return -EINVAL;
  150. }
  151. }
  152. memset(&clock_properties, 0, sizeof(clock_properties));
  153. clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
  154. clock_properties.clock_mode = CM_STANDALONE;
  155. clock_properties.frequency = rate;
  156. clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
  157. clock_properties.uid_caller[0] = pipe->group_uid;
  158. snd_printdd("mixart_set_clock to %d kHz\n", rate);
  159. request.message_id = MSG_CLOCK_SET_PROPERTIES;
  160. request.uid = mgr->uid_console_manager;
  161. request.data = &clock_properties;
  162. request.size = sizeof(clock_properties);
  163. err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
  164. if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
  165. snd_printk(KERN_ERR "error MSG_CLOCK_SET_PROPERTIES err=%x stat=%x mod=%x !\n", err, clock_prop_resp.status, clock_prop_resp.clock_mode);
  166. return -EINVAL;
  167. }
  168. if(rate) pipe->status = PIPE_CLOCK_SET;
  169. else pipe->status = PIPE_RUNNING;
  170. return 0;
  171. }
  172. /*
  173. * Allocate or reference output pipe for analog IOs (pcmp0/1)
  174. */
  175. struct mixart_pipe *
  176. snd_mixart_add_ref_pipe(struct snd_mixart *chip, int pcm_number, int capture,
  177. int monitoring)
  178. {
  179. int stream_count;
  180. struct mixart_pipe *pipe;
  181. struct mixart_msg request;
  182. if(capture) {
  183. if (pcm_number == MIXART_PCM_ANALOG) {
  184. pipe = &(chip->pipe_in_ana); /* analog inputs */
  185. } else {
  186. pipe = &(chip->pipe_in_dig); /* digital inputs */
  187. }
  188. request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
  189. stream_count = MIXART_CAPTURE_STREAMS;
  190. } else {
  191. if (pcm_number == MIXART_PCM_ANALOG) {
  192. pipe = &(chip->pipe_out_ana); /* analog outputs */
  193. } else {
  194. pipe = &(chip->pipe_out_dig); /* digital outputs */
  195. }
  196. request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
  197. stream_count = MIXART_PLAYBACK_STREAMS;
  198. }
  199. /* a new stream is opened and there are already all streams in use */
  200. if( (monitoring == 0) && (pipe->references >= stream_count) ) {
  201. return NULL;
  202. }
  203. /* pipe is not yet defined */
  204. if( pipe->status == PIPE_UNDEFINED ) {
  205. int err, i;
  206. struct {
  207. struct mixart_streaming_group_req sgroup_req;
  208. struct mixart_streaming_group sgroup_resp;
  209. } *buf;
  210. snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
  211. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  212. if (!buf)
  213. return NULL;
  214. request.uid = (struct mixart_uid){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */
  215. request.data = &buf->sgroup_req;
  216. request.size = sizeof(buf->sgroup_req);
  217. memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
  218. buf->sgroup_req.stream_count = stream_count;
  219. buf->sgroup_req.channel_count = 2;
  220. buf->sgroup_req.latency = 256;
  221. buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */
  222. for (i=0; i<stream_count; i++) {
  223. int j;
  224. struct mixart_flowinfo *flowinfo;
  225. struct mixart_bufferinfo *bufferinfo;
  226. /* we don't yet know the format, so config 16 bit pcm audio for instance */
  227. buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
  228. buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
  229. buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
  230. /* find the right bufferinfo_array */
  231. j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
  232. if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
  233. buf->sgroup_req.flow_entry[i] = j;
  234. flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
  235. flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(struct mixart_bufferinfo));
  236. flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */
  237. bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
  238. bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */
  239. bufferinfo[j].available_length = 0; /* buffer is not yet allocated */
  240. /* construct the identifier of the stream buffer received in the interrupts ! */
  241. bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
  242. if(capture) {
  243. bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
  244. }
  245. }
  246. err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
  247. if((err < 0) || (buf->sgroup_resp.status != 0)) {
  248. snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
  249. kfree(buf);
  250. return NULL;
  251. }
  252. pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */
  253. pipe->stream_count = buf->sgroup_resp.stream_count;
  254. /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
  255. pipe->status = PIPE_STOPPED;
  256. kfree(buf);
  257. }
  258. if(monitoring) pipe->monitoring = 1;
  259. else pipe->references++;
  260. return pipe;
  261. }
  262. int snd_mixart_kill_ref_pipe(struct mixart_mgr *mgr,
  263. struct mixart_pipe *pipe, int monitoring)
  264. {
  265. int err = 0;
  266. if(pipe->status == PIPE_UNDEFINED)
  267. return 0;
  268. if(monitoring)
  269. pipe->monitoring = 0;
  270. else
  271. pipe->references--;
  272. if((pipe->references <= 0) && (pipe->monitoring == 0)) {
  273. struct mixart_msg request;
  274. struct mixart_delete_group_resp delete_resp;
  275. /* release the clock */
  276. err = mixart_set_clock( mgr, pipe, 0);
  277. if( err < 0 ) {
  278. snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
  279. }
  280. /* stop the pipe */
  281. err = mixart_set_pipe_state(mgr, pipe, 0);
  282. if( err < 0 ) {
  283. snd_printk(KERN_ERR "error stopping pipe!\n");
  284. }
  285. request.message_id = MSG_STREAM_DELETE_GROUP;
  286. request.uid = (struct mixart_uid){0,0};
  287. request.data = &pipe->group_uid; /* the streaming group ! */
  288. request.size = sizeof(pipe->group_uid);
  289. /* delete the pipe */
  290. err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
  291. if ((err < 0) || (delete_resp.status != 0)) {
  292. snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
  293. }
  294. pipe->group_uid = (struct mixart_uid){0,0};
  295. pipe->stream_count = 0;
  296. pipe->status = PIPE_UNDEFINED;
  297. }
  298. return err;
  299. }
  300. static int mixart_set_stream_state(struct mixart_stream *stream, int start)
  301. {
  302. struct snd_mixart *chip;
  303. struct mixart_stream_state_req stream_state_req;
  304. struct mixart_msg request;
  305. if(!stream->substream)
  306. return -EINVAL;
  307. memset(&stream_state_req, 0, sizeof(stream_state_req));
  308. stream_state_req.stream_count = 1;
  309. stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
  310. stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
  311. if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  312. request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
  313. else
  314. request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
  315. request.uid = (struct mixart_uid){0,0};
  316. request.data = &stream_state_req;
  317. request.size = sizeof(stream_state_req);
  318. stream->abs_period_elapsed = 0; /* reset stream pos */
  319. stream->buf_periods = 0;
  320. stream->buf_period_frag = 0;
  321. chip = snd_pcm_substream_chip(stream->substream);
  322. return snd_mixart_send_msg_nonblock(chip->mgr, &request);
  323. }
  324. /*
  325. * Trigger callback
  326. */
  327. static int snd_mixart_trigger(struct snd_pcm_substream *subs, int cmd)
  328. {
  329. struct mixart_stream *stream = subs->runtime->private_data;
  330. switch (cmd) {
  331. case SNDRV_PCM_TRIGGER_START:
  332. snd_printdd("SNDRV_PCM_TRIGGER_START\n");
  333. /* START_STREAM */
  334. if( mixart_set_stream_state(stream, 1) )
  335. return -EINVAL;
  336. stream->status = MIXART_STREAM_STATUS_RUNNING;
  337. break;
  338. case SNDRV_PCM_TRIGGER_STOP:
  339. /* STOP_STREAM */
  340. if( mixart_set_stream_state(stream, 0) )
  341. return -EINVAL;
  342. stream->status = MIXART_STREAM_STATUS_OPEN;
  343. snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
  344. break;
  345. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  346. /* TODO */
  347. stream->status = MIXART_STREAM_STATUS_PAUSE;
  348. snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
  349. break;
  350. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  351. /* TODO */
  352. stream->status = MIXART_STREAM_STATUS_RUNNING;
  353. snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. return 0;
  359. }
  360. static int mixart_sync_nonblock_events(struct mixart_mgr *mgr)
  361. {
  362. unsigned long timeout = jiffies + HZ;
  363. while (atomic_read(&mgr->msg_processed) > 0) {
  364. if (time_after(jiffies, timeout)) {
  365. snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
  366. return -EBUSY;
  367. }
  368. schedule_timeout_uninterruptible(1);
  369. }
  370. return 0;
  371. }
  372. /*
  373. * prepare callback for all pcms
  374. */
  375. static int snd_mixart_prepare(struct snd_pcm_substream *subs)
  376. {
  377. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  378. struct mixart_stream *stream = subs->runtime->private_data;
  379. /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
  380. snd_printdd("snd_mixart_prepare\n");
  381. mixart_sync_nonblock_events(chip->mgr);
  382. /* only the first stream can choose the sample rate */
  383. /* the further opened streams will be limited to its frequency (see open) */
  384. if(chip->mgr->ref_count_rate == 1)
  385. chip->mgr->sample_rate = subs->runtime->rate;
  386. /* set the clock only once (first stream) on the same pipe */
  387. if(stream->pipe->references == 1) {
  388. if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
  389. return -EINVAL;
  390. }
  391. return 0;
  392. }
  393. static int mixart_set_format(struct mixart_stream *stream, snd_pcm_format_t format)
  394. {
  395. int err;
  396. struct snd_mixart *chip;
  397. struct mixart_msg request;
  398. struct mixart_stream_param_desc stream_param;
  399. struct mixart_return_uid resp;
  400. chip = snd_pcm_substream_chip(stream->substream);
  401. memset(&stream_param, 0, sizeof(stream_param));
  402. stream_param.coding_type = CT_LINEAR;
  403. stream_param.number_of_channel = stream->channels;
  404. stream_param.sampling_freq = chip->mgr->sample_rate;
  405. if(stream_param.sampling_freq == 0)
  406. stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
  407. switch(format){
  408. case SNDRV_PCM_FORMAT_U8:
  409. stream_param.sample_type = ST_INTEGER_8;
  410. stream_param.sample_size = 8;
  411. break;
  412. case SNDRV_PCM_FORMAT_S16_LE:
  413. stream_param.sample_type = ST_INTEGER_16LE;
  414. stream_param.sample_size = 16;
  415. break;
  416. case SNDRV_PCM_FORMAT_S16_BE:
  417. stream_param.sample_type = ST_INTEGER_16BE;
  418. stream_param.sample_size = 16;
  419. break;
  420. case SNDRV_PCM_FORMAT_S24_3LE:
  421. stream_param.sample_type = ST_INTEGER_24LE;
  422. stream_param.sample_size = 24;
  423. break;
  424. case SNDRV_PCM_FORMAT_S24_3BE:
  425. stream_param.sample_type = ST_INTEGER_24BE;
  426. stream_param.sample_size = 24;
  427. break;
  428. case SNDRV_PCM_FORMAT_FLOAT_LE:
  429. stream_param.sample_type = ST_FLOATING_POINT_32LE;
  430. stream_param.sample_size = 32;
  431. break;
  432. case SNDRV_PCM_FORMAT_FLOAT_BE:
  433. stream_param.sample_type = ST_FLOATING_POINT_32BE;
  434. stream_param.sample_size = 32;
  435. break;
  436. default:
  437. snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
  438. return -EINVAL;
  439. }
  440. snd_printdd("set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
  441. stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
  442. /* TODO: what else to configure ? */
  443. /* stream_param.samples_per_frame = 2; */
  444. /* stream_param.bytes_per_frame = 4; */
  445. /* stream_param.bytes_per_sample = 2; */
  446. stream_param.pipe_count = 1; /* set to 1 */
  447. stream_param.stream_count = 1; /* set to 1 */
  448. stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
  449. stream_param.stream_desc[0].stream_idx = stream->substream->number;
  450. request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
  451. request.uid = (struct mixart_uid){0,0};
  452. request.data = &stream_param;
  453. request.size = sizeof(stream_param);
  454. err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
  455. if((err < 0) || resp.error_code) {
  456. snd_printk(KERN_ERR "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", err, resp.error_code);
  457. return -EINVAL;
  458. }
  459. return 0;
  460. }
  461. /*
  462. * HW_PARAMS callback for all pcms
  463. */
  464. static int snd_mixart_hw_params(struct snd_pcm_substream *subs,
  465. struct snd_pcm_hw_params *hw)
  466. {
  467. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  468. struct mixart_mgr *mgr = chip->mgr;
  469. struct mixart_stream *stream = subs->runtime->private_data;
  470. snd_pcm_format_t format;
  471. int err;
  472. int channels;
  473. /* set up channels */
  474. channels = params_channels(hw);
  475. /* set up format for the stream */
  476. format = params_format(hw);
  477. mutex_lock(&mgr->setup_mutex);
  478. /* update the stream levels */
  479. if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
  480. int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
  481. if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
  482. mixart_update_playback_stream_level(chip, is_aes, subs->number);
  483. else
  484. mixart_update_capture_stream_level( chip, is_aes);
  485. }
  486. stream->channels = channels;
  487. /* set the format to the board */
  488. err = mixart_set_format(stream, format);
  489. if(err < 0) {
  490. mutex_unlock(&mgr->setup_mutex);
  491. return err;
  492. }
  493. /* allocate buffer */
  494. err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
  495. if (err > 0) {
  496. struct mixart_bufferinfo *bufferinfo;
  497. int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number;
  498. if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) {
  499. i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */
  500. }
  501. bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
  502. bufferinfo[i].buffer_address = subs->runtime->dma_addr;
  503. bufferinfo[i].available_length = subs->runtime->dma_bytes;
  504. /* bufferinfo[i].buffer_id is already defined */
  505. snd_printdd("snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", i,
  506. bufferinfo[i].buffer_address,
  507. bufferinfo[i].available_length,
  508. subs->number);
  509. }
  510. mutex_unlock(&mgr->setup_mutex);
  511. return err;
  512. }
  513. static int snd_mixart_hw_free(struct snd_pcm_substream *subs)
  514. {
  515. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  516. snd_pcm_lib_free_pages(subs);
  517. mixart_sync_nonblock_events(chip->mgr);
  518. return 0;
  519. }
  520. /*
  521. * TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
  522. */
  523. static struct snd_pcm_hardware snd_mixart_analog_caps =
  524. {
  525. .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  526. SNDRV_PCM_INFO_MMAP_VALID |
  527. SNDRV_PCM_INFO_PAUSE),
  528. .formats = ( SNDRV_PCM_FMTBIT_U8 |
  529. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  530. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
  531. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
  532. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  533. .rate_min = 8000,
  534. .rate_max = 48000,
  535. .channels_min = 1,
  536. .channels_max = 2,
  537. .buffer_bytes_max = (32*1024),
  538. .period_bytes_min = 256, /* 256 frames U8 mono*/
  539. .period_bytes_max = (16*1024),
  540. .periods_min = 2,
  541. .periods_max = (32*1024/256),
  542. };
  543. static struct snd_pcm_hardware snd_mixart_digital_caps =
  544. {
  545. .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  546. SNDRV_PCM_INFO_MMAP_VALID |
  547. SNDRV_PCM_INFO_PAUSE),
  548. .formats = ( SNDRV_PCM_FMTBIT_U8 |
  549. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  550. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
  551. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
  552. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  553. .rate_min = 32000,
  554. .rate_max = 48000,
  555. .channels_min = 1,
  556. .channels_max = 2,
  557. .buffer_bytes_max = (32*1024),
  558. .period_bytes_min = 256, /* 256 frames U8 mono*/
  559. .period_bytes_max = (16*1024),
  560. .periods_min = 2,
  561. .periods_max = (32*1024/256),
  562. };
  563. static int snd_mixart_playback_open(struct snd_pcm_substream *subs)
  564. {
  565. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  566. struct mixart_mgr *mgr = chip->mgr;
  567. struct snd_pcm_runtime *runtime = subs->runtime;
  568. struct snd_pcm *pcm = subs->pcm;
  569. struct mixart_stream *stream;
  570. struct mixart_pipe *pipe;
  571. int err = 0;
  572. int pcm_number;
  573. mutex_lock(&mgr->setup_mutex);
  574. if ( pcm == chip->pcm ) {
  575. pcm_number = MIXART_PCM_ANALOG;
  576. runtime->hw = snd_mixart_analog_caps;
  577. } else {
  578. snd_BUG_ON(pcm != chip->pcm_dig);
  579. pcm_number = MIXART_PCM_DIGITAL;
  580. runtime->hw = snd_mixart_digital_caps;
  581. }
  582. snd_printdd("snd_mixart_playback_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
  583. /* get stream info */
  584. stream = &(chip->playback_stream[pcm_number][subs->number]);
  585. if (stream->status != MIXART_STREAM_STATUS_FREE){
  586. /* streams in use */
  587. snd_printk(KERN_ERR "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
  588. err = -EBUSY;
  589. goto _exit_open;
  590. }
  591. /* get pipe pointer (out pipe) */
  592. pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
  593. if (pipe == NULL) {
  594. err = -EINVAL;
  595. goto _exit_open;
  596. }
  597. /* start the pipe if necessary */
  598. err = mixart_set_pipe_state(chip->mgr, pipe, 1);
  599. if( err < 0 ) {
  600. snd_printk(KERN_ERR "error starting pipe!\n");
  601. snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
  602. err = -EINVAL;
  603. goto _exit_open;
  604. }
  605. stream->pipe = pipe;
  606. stream->pcm_number = pcm_number;
  607. stream->status = MIXART_STREAM_STATUS_OPEN;
  608. stream->substream = subs;
  609. stream->channels = 0; /* not configured yet */
  610. runtime->private_data = stream;
  611. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  612. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
  613. /* if a sample rate is already used, another stream cannot change */
  614. if(mgr->ref_count_rate++) {
  615. if(mgr->sample_rate) {
  616. runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
  617. }
  618. }
  619. _exit_open:
  620. mutex_unlock(&mgr->setup_mutex);
  621. return err;
  622. }
  623. static int snd_mixart_capture_open(struct snd_pcm_substream *subs)
  624. {
  625. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  626. struct mixart_mgr *mgr = chip->mgr;
  627. struct snd_pcm_runtime *runtime = subs->runtime;
  628. struct snd_pcm *pcm = subs->pcm;
  629. struct mixart_stream *stream;
  630. struct mixart_pipe *pipe;
  631. int err = 0;
  632. int pcm_number;
  633. mutex_lock(&mgr->setup_mutex);
  634. if ( pcm == chip->pcm ) {
  635. pcm_number = MIXART_PCM_ANALOG;
  636. runtime->hw = snd_mixart_analog_caps;
  637. } else {
  638. snd_BUG_ON(pcm != chip->pcm_dig);
  639. pcm_number = MIXART_PCM_DIGITAL;
  640. runtime->hw = snd_mixart_digital_caps;
  641. }
  642. runtime->hw.channels_min = 2; /* for instance, no mono */
  643. snd_printdd("snd_mixart_capture_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
  644. /* get stream info */
  645. stream = &(chip->capture_stream[pcm_number]);
  646. if (stream->status != MIXART_STREAM_STATUS_FREE){
  647. /* streams in use */
  648. snd_printk(KERN_ERR "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
  649. err = -EBUSY;
  650. goto _exit_open;
  651. }
  652. /* get pipe pointer (in pipe) */
  653. pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
  654. if (pipe == NULL) {
  655. err = -EINVAL;
  656. goto _exit_open;
  657. }
  658. /* start the pipe if necessary */
  659. err = mixart_set_pipe_state(chip->mgr, pipe, 1);
  660. if( err < 0 ) {
  661. snd_printk(KERN_ERR "error starting pipe!\n");
  662. snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
  663. err = -EINVAL;
  664. goto _exit_open;
  665. }
  666. stream->pipe = pipe;
  667. stream->pcm_number = pcm_number;
  668. stream->status = MIXART_STREAM_STATUS_OPEN;
  669. stream->substream = subs;
  670. stream->channels = 0; /* not configured yet */
  671. runtime->private_data = stream;
  672. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  673. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
  674. /* if a sample rate is already used, another stream cannot change */
  675. if(mgr->ref_count_rate++) {
  676. if(mgr->sample_rate) {
  677. runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
  678. }
  679. }
  680. _exit_open:
  681. mutex_unlock(&mgr->setup_mutex);
  682. return err;
  683. }
  684. static int snd_mixart_close(struct snd_pcm_substream *subs)
  685. {
  686. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  687. struct mixart_mgr *mgr = chip->mgr;
  688. struct mixart_stream *stream = subs->runtime->private_data;
  689. mutex_lock(&mgr->setup_mutex);
  690. snd_printdd("snd_mixart_close C%d/P%d/Sub%d\n", chip->chip_idx, stream->pcm_number, subs->number);
  691. /* sample rate released */
  692. if(--mgr->ref_count_rate == 0) {
  693. mgr->sample_rate = 0;
  694. }
  695. /* delete pipe */
  696. if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
  697. snd_printk(KERN_ERR "error snd_mixart_kill_ref_pipe C%dP%d\n", chip->chip_idx, stream->pcm_number);
  698. }
  699. stream->pipe = NULL;
  700. stream->status = MIXART_STREAM_STATUS_FREE;
  701. stream->substream = NULL;
  702. mutex_unlock(&mgr->setup_mutex);
  703. return 0;
  704. }
  705. static snd_pcm_uframes_t snd_mixart_stream_pointer(struct snd_pcm_substream *subs)
  706. {
  707. struct snd_pcm_runtime *runtime = subs->runtime;
  708. struct mixart_stream *stream = runtime->private_data;
  709. return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
  710. }
  711. static struct snd_pcm_ops snd_mixart_playback_ops = {
  712. .open = snd_mixart_playback_open,
  713. .close = snd_mixart_close,
  714. .ioctl = snd_pcm_lib_ioctl,
  715. .prepare = snd_mixart_prepare,
  716. .hw_params = snd_mixart_hw_params,
  717. .hw_free = snd_mixart_hw_free,
  718. .trigger = snd_mixart_trigger,
  719. .pointer = snd_mixart_stream_pointer,
  720. };
  721. static struct snd_pcm_ops snd_mixart_capture_ops = {
  722. .open = snd_mixart_capture_open,
  723. .close = snd_mixart_close,
  724. .ioctl = snd_pcm_lib_ioctl,
  725. .prepare = snd_mixart_prepare,
  726. .hw_params = snd_mixart_hw_params,
  727. .hw_free = snd_mixart_hw_free,
  728. .trigger = snd_mixart_trigger,
  729. .pointer = snd_mixart_stream_pointer,
  730. };
  731. static void preallocate_buffers(struct snd_mixart *chip, struct snd_pcm *pcm)
  732. {
  733. #if 0
  734. struct snd_pcm_substream *subs;
  735. int stream;
  736. for (stream = 0; stream < 2; stream++) {
  737. int idx = 0;
  738. for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
  739. /* set up the unique device id with the chip index */
  740. subs->dma_device.id = subs->pcm->device << 16 |
  741. subs->stream << 8 | (subs->number + 1) |
  742. (chip->chip_idx + 1) << 24;
  743. }
  744. #endif
  745. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  746. snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
  747. }
  748. /*
  749. */
  750. static int snd_mixart_pcm_analog(struct snd_mixart *chip)
  751. {
  752. int err;
  753. struct snd_pcm *pcm;
  754. char name[32];
  755. sprintf(name, "miXart analog %d", chip->chip_idx);
  756. if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
  757. MIXART_PLAYBACK_STREAMS,
  758. MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
  759. snd_printk(KERN_ERR "cannot create the analog pcm %d\n", chip->chip_idx);
  760. return err;
  761. }
  762. pcm->private_data = chip;
  763. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
  764. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
  765. pcm->info_flags = 0;
  766. strcpy(pcm->name, name);
  767. preallocate_buffers(chip, pcm);
  768. chip->pcm = pcm;
  769. return 0;
  770. }
  771. /*
  772. */
  773. static int snd_mixart_pcm_digital(struct snd_mixart *chip)
  774. {
  775. int err;
  776. struct snd_pcm *pcm;
  777. char name[32];
  778. sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
  779. if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
  780. MIXART_PLAYBACK_STREAMS,
  781. MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
  782. snd_printk(KERN_ERR "cannot create the digital pcm %d\n", chip->chip_idx);
  783. return err;
  784. }
  785. pcm->private_data = chip;
  786. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
  787. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
  788. pcm->info_flags = 0;
  789. strcpy(pcm->name, name);
  790. preallocate_buffers(chip, pcm);
  791. chip->pcm_dig = pcm;
  792. return 0;
  793. }
  794. static int snd_mixart_chip_free(struct snd_mixart *chip)
  795. {
  796. kfree(chip);
  797. return 0;
  798. }
  799. static int snd_mixart_chip_dev_free(struct snd_device *device)
  800. {
  801. struct snd_mixart *chip = device->device_data;
  802. return snd_mixart_chip_free(chip);
  803. }
  804. /*
  805. */
  806. static int __devinit snd_mixart_create(struct mixart_mgr *mgr, struct snd_card *card, int idx)
  807. {
  808. int err;
  809. struct snd_mixart *chip;
  810. static struct snd_device_ops ops = {
  811. .dev_free = snd_mixart_chip_dev_free,
  812. };
  813. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  814. if (! chip) {
  815. snd_printk(KERN_ERR "cannot allocate chip\n");
  816. return -ENOMEM;
  817. }
  818. chip->card = card;
  819. chip->chip_idx = idx;
  820. chip->mgr = mgr;
  821. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  822. snd_mixart_chip_free(chip);
  823. return err;
  824. }
  825. mgr->chip[idx] = chip;
  826. snd_card_set_dev(card, &mgr->pci->dev);
  827. return 0;
  828. }
  829. int snd_mixart_create_pcm(struct snd_mixart* chip)
  830. {
  831. int err;
  832. err = snd_mixart_pcm_analog(chip);
  833. if (err < 0)
  834. return err;
  835. if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
  836. err = snd_mixart_pcm_digital(chip);
  837. if (err < 0)
  838. return err;
  839. }
  840. return err;
  841. }
  842. /*
  843. * release all the cards assigned to a manager instance
  844. */
  845. static int snd_mixart_free(struct mixart_mgr *mgr)
  846. {
  847. unsigned int i;
  848. for (i = 0; i < mgr->num_cards; i++) {
  849. if (mgr->chip[i])
  850. snd_card_free(mgr->chip[i]->card);
  851. }
  852. /* stop mailbox */
  853. snd_mixart_exit_mailbox(mgr);
  854. /* release irq */
  855. if (mgr->irq >= 0)
  856. free_irq(mgr->irq, mgr);
  857. /* reset board if some firmware was loaded */
  858. if(mgr->dsp_loaded) {
  859. snd_mixart_reset_board(mgr);
  860. snd_printdd("reset miXart !\n");
  861. }
  862. /* release the i/o ports */
  863. for (i = 0; i < 2; i++) {
  864. if (mgr->mem[i].virt)
  865. iounmap(mgr->mem[i].virt);
  866. }
  867. pci_release_regions(mgr->pci);
  868. /* free flowarray */
  869. if(mgr->flowinfo.area) {
  870. snd_dma_free_pages(&mgr->flowinfo);
  871. mgr->flowinfo.area = NULL;
  872. }
  873. /* free bufferarray */
  874. if(mgr->bufferinfo.area) {
  875. snd_dma_free_pages(&mgr->bufferinfo);
  876. mgr->bufferinfo.area = NULL;
  877. }
  878. pci_disable_device(mgr->pci);
  879. kfree(mgr);
  880. return 0;
  881. }
  882. /*
  883. * proc interface
  884. */
  885. /*
  886. mixart_BA0 proc interface for BAR 0 - read callback
  887. */
  888. static ssize_t snd_mixart_BA0_read(struct snd_info_entry *entry,
  889. void *file_private_data,
  890. struct file *file, char __user *buf,
  891. size_t count, loff_t pos)
  892. {
  893. struct mixart_mgr *mgr = entry->private_data;
  894. count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
  895. if (copy_to_user_fromio(buf, MIXART_MEM(mgr, pos), count))
  896. return -EFAULT;
  897. return count;
  898. }
  899. /*
  900. mixart_BA1 proc interface for BAR 1 - read callback
  901. */
  902. static ssize_t snd_mixart_BA1_read(struct snd_info_entry *entry,
  903. void *file_private_data,
  904. struct file *file, char __user *buf,
  905. size_t count, loff_t pos)
  906. {
  907. struct mixart_mgr *mgr = entry->private_data;
  908. count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
  909. if (copy_to_user_fromio(buf, MIXART_REG(mgr, pos), count))
  910. return -EFAULT;
  911. return count;
  912. }
  913. static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = {
  914. .read = snd_mixart_BA0_read,
  915. };
  916. static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = {
  917. .read = snd_mixart_BA1_read,
  918. };
  919. static void snd_mixart_proc_read(struct snd_info_entry *entry,
  920. struct snd_info_buffer *buffer)
  921. {
  922. struct snd_mixart *chip = entry->private_data;
  923. u32 ref;
  924. snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx);
  925. /* stats available when embedded OS is running */
  926. if (chip->mgr->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) {
  927. snd_iprintf(buffer, "- hardware -\n");
  928. switch (chip->mgr->board_type ) {
  929. case MIXART_DAUGHTER_TYPE_NONE : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break;
  930. case MIXART_DAUGHTER_TYPE_AES : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break;
  931. case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break;
  932. default: snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break;
  933. }
  934. snd_iprintf(buffer, "- system load -\n");
  935. /* get perf reference */
  936. ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET));
  937. if (ref) {
  938. u32 mailbox = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref;
  939. u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref;
  940. u32 interr = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref;
  941. snd_iprintf(buffer, "\tstreaming : %d\n", streaming);
  942. snd_iprintf(buffer, "\tmailbox : %d\n", mailbox);
  943. snd_iprintf(buffer, "\tinterrups handling : %d\n\n", interr);
  944. }
  945. } /* endif elf loaded */
  946. }
  947. static void __devinit snd_mixart_proc_init(struct snd_mixart *chip)
  948. {
  949. struct snd_info_entry *entry;
  950. /* text interface to read perf and temp meters */
  951. if (! snd_card_proc_new(chip->card, "board_info", &entry)) {
  952. entry->private_data = chip;
  953. entry->c.text.read = snd_mixart_proc_read;
  954. }
  955. if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
  956. entry->content = SNDRV_INFO_CONTENT_DATA;
  957. entry->private_data = chip->mgr;
  958. entry->c.ops = &snd_mixart_proc_ops_BA0;
  959. entry->size = MIXART_BA0_SIZE;
  960. }
  961. if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
  962. entry->content = SNDRV_INFO_CONTENT_DATA;
  963. entry->private_data = chip->mgr;
  964. entry->c.ops = &snd_mixart_proc_ops_BA1;
  965. entry->size = MIXART_BA1_SIZE;
  966. }
  967. }
  968. /* end of proc interface */
  969. /*
  970. * probe function - creates the card manager
  971. */
  972. static int __devinit snd_mixart_probe(struct pci_dev *pci,
  973. const struct pci_device_id *pci_id)
  974. {
  975. static int dev;
  976. struct mixart_mgr *mgr;
  977. unsigned int i;
  978. int err;
  979. size_t size;
  980. /*
  981. */
  982. if (dev >= SNDRV_CARDS)
  983. return -ENODEV;
  984. if (! enable[dev]) {
  985. dev++;
  986. return -ENOENT;
  987. }
  988. /* enable PCI device */
  989. if ((err = pci_enable_device(pci)) < 0)
  990. return err;
  991. pci_set_master(pci);
  992. /* check if we can restrict PCI DMA transfers to 32 bits */
  993. if (pci_set_dma_mask(pci, DMA_BIT_MASK(32)) < 0) {
  994. snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
  995. pci_disable_device(pci);
  996. return -ENXIO;
  997. }
  998. /*
  999. */
  1000. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  1001. if (! mgr) {
  1002. pci_disable_device(pci);
  1003. return -ENOMEM;
  1004. }
  1005. mgr->pci = pci;
  1006. mgr->irq = -1;
  1007. /* resource assignment */
  1008. if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
  1009. kfree(mgr);
  1010. pci_disable_device(pci);
  1011. return err;
  1012. }
  1013. for (i = 0; i < 2; i++) {
  1014. mgr->mem[i].phys = pci_resource_start(pci, i);
  1015. mgr->mem[i].virt = pci_ioremap_bar(pci, i);
  1016. if (!mgr->mem[i].virt) {
  1017. printk(KERN_ERR "unable to remap resource 0x%lx\n",
  1018. mgr->mem[i].phys);
  1019. snd_mixart_free(mgr);
  1020. return -EBUSY;
  1021. }
  1022. }
  1023. if (request_irq(pci->irq, snd_mixart_interrupt, IRQF_SHARED,
  1024. KBUILD_MODNAME, mgr)) {
  1025. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1026. snd_mixart_free(mgr);
  1027. return -EBUSY;
  1028. }
  1029. mgr->irq = pci->irq;
  1030. sprintf(mgr->shortname, "Digigram miXart");
  1031. sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
  1032. /* ISR spinlock */
  1033. spin_lock_init(&mgr->lock);
  1034. /* init mailbox */
  1035. mgr->msg_fifo_readptr = 0;
  1036. mgr->msg_fifo_writeptr = 0;
  1037. spin_lock_init(&mgr->msg_lock);
  1038. mutex_init(&mgr->msg_mutex);
  1039. init_waitqueue_head(&mgr->msg_sleep);
  1040. atomic_set(&mgr->msg_processed, 0);
  1041. /* init setup mutex*/
  1042. mutex_init(&mgr->setup_mutex);
  1043. /* init message taslket */
  1044. tasklet_init(&mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
  1045. /* card assignment */
  1046. mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */
  1047. for (i = 0; i < mgr->num_cards; i++) {
  1048. struct snd_card *card;
  1049. char tmpid[16];
  1050. int idx;
  1051. if (index[dev] < 0)
  1052. idx = index[dev];
  1053. else
  1054. idx = index[dev] + i;
  1055. snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
  1056. err = snd_card_create(idx, tmpid, THIS_MODULE, 0, &card);
  1057. if (err < 0) {
  1058. snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
  1059. snd_mixart_free(mgr);
  1060. return err;
  1061. }
  1062. strcpy(card->driver, CARD_NAME);
  1063. sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
  1064. sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
  1065. if ((err = snd_mixart_create(mgr, card, i)) < 0) {
  1066. snd_card_free(card);
  1067. snd_mixart_free(mgr);
  1068. return err;
  1069. }
  1070. if(i==0) {
  1071. /* init proc interface only for chip0 */
  1072. snd_mixart_proc_init(mgr->chip[i]);
  1073. }
  1074. if ((err = snd_card_register(card)) < 0) {
  1075. snd_mixart_free(mgr);
  1076. return err;
  1077. }
  1078. }
  1079. /* init firmware status (mgr->dsp_loaded reset in hwdep_new) */
  1080. mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
  1081. /* create array of streaminfo */
  1082. size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
  1083. sizeof(struct mixart_flowinfo)) );
  1084. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1085. size, &mgr->flowinfo) < 0) {
  1086. snd_mixart_free(mgr);
  1087. return -ENOMEM;
  1088. }
  1089. /* init streaminfo_array */
  1090. memset(mgr->flowinfo.area, 0, size);
  1091. /* create array of bufferinfo */
  1092. size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
  1093. sizeof(struct mixart_bufferinfo)) );
  1094. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1095. size, &mgr->bufferinfo) < 0) {
  1096. snd_mixart_free(mgr);
  1097. return -ENOMEM;
  1098. }
  1099. /* init bufferinfo_array */
  1100. memset(mgr->bufferinfo.area, 0, size);
  1101. /* set up firmware */
  1102. err = snd_mixart_setup_firmware(mgr);
  1103. if (err < 0) {
  1104. snd_mixart_free(mgr);
  1105. return err;
  1106. }
  1107. pci_set_drvdata(pci, mgr);
  1108. dev++;
  1109. return 0;
  1110. }
  1111. static void __devexit snd_mixart_remove(struct pci_dev *pci)
  1112. {
  1113. snd_mixart_free(pci_get_drvdata(pci));
  1114. pci_set_drvdata(pci, NULL);
  1115. }
  1116. static struct pci_driver driver = {
  1117. .name = KBUILD_MODNAME,
  1118. .id_table = snd_mixart_ids,
  1119. .probe = snd_mixart_probe,
  1120. .remove = __devexit_p(snd_mixart_remove),
  1121. };
  1122. static int __init alsa_card_mixart_init(void)
  1123. {
  1124. return pci_register_driver(&driver);
  1125. }
  1126. static void __exit alsa_card_mixart_exit(void)
  1127. {
  1128. pci_unregister_driver(&driver);
  1129. }
  1130. module_init(alsa_card_mixart_init)
  1131. module_exit(alsa_card_mixart_exit)