mixart.c 40 KB

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