mixart.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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 <sound/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/pci.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/mutex.h>
  29. #include <linux/dma-mapping.h>
  30. #include <sound/core.h>
  31. #include <sound/initval.h>
  32. #include <sound/info.h>
  33. #include <sound/control.h>
  34. #include <sound/pcm.h>
  35. #include <sound/pcm_params.h>
  36. #include "mixart.h"
  37. #include "mixart_hwdep.h"
  38. #include "mixart_core.h"
  39. #include "mixart_mixer.h"
  40. #define CARD_NAME "miXart"
  41. MODULE_AUTHOR("Digigram <alsa@digigram.com>");
  42. MODULE_DESCRIPTION("Digigram " CARD_NAME);
  43. MODULE_LICENSE("GPL");
  44. MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");
  45. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  46. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  47. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  48. module_param_array(index, int, NULL, 0444);
  49. MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
  50. module_param_array(id, charp, NULL, 0444);
  51. MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
  52. module_param_array(enable, bool, NULL, 0444);
  53. MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
  54. /*
  55. */
  56. static struct pci_device_id snd_mixart_ids[] = {
  57. { 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
  58. { 0, }
  59. };
  60. MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
  61. static int mixart_set_pipe_state(struct mixart_mgr *mgr,
  62. struct mixart_pipe *pipe, int start)
  63. {
  64. struct mixart_group_state_req group_state;
  65. struct mixart_group_state_resp group_state_resp;
  66. struct mixart_msg request;
  67. int err;
  68. u32 system_msg_uid;
  69. switch(pipe->status) {
  70. case PIPE_RUNNING:
  71. case PIPE_CLOCK_SET:
  72. if(start) return 0; /* already started */
  73. break;
  74. case PIPE_STOPPED:
  75. if(!start) return 0; /* already stopped */
  76. break;
  77. default:
  78. snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
  79. return -EINVAL; /* function called with wrong pipe status */
  80. }
  81. system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
  82. /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
  83. request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
  84. request.uid = (struct mixart_uid){0,0};
  85. request.data = &system_msg_uid;
  86. request.size = sizeof(system_msg_uid);
  87. err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
  88. if(err) {
  89. snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
  90. return err;
  91. }
  92. /* start or stop the pipe (1 pipe) */
  93. memset(&group_state, 0, sizeof(group_state));
  94. group_state.pipe_count = 1;
  95. group_state.pipe_uid[0] = pipe->group_uid;
  96. if(start)
  97. request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
  98. else
  99. request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
  100. request.uid = pipe->group_uid; /*(struct mixart_uid){0,0};*/
  101. request.data = &group_state;
  102. request.size = sizeof(group_state);
  103. err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
  104. if (err < 0 || group_state_resp.txx_status != 0) {
  105. snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
  106. return -EINVAL;
  107. }
  108. if(start) {
  109. u32 stat;
  110. group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
  111. err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
  112. if (err < 0 || group_state_resp.txx_status != 0) {
  113. snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
  114. return -EINVAL;
  115. }
  116. /* in case of start send a synchro top */
  117. request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
  118. request.uid = (struct mixart_uid){0,0};
  119. request.data = NULL;
  120. request.size = 0;
  121. err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
  122. if (err < 0 || stat != 0) {
  123. snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
  124. return -EINVAL;
  125. }
  126. pipe->status = PIPE_RUNNING;
  127. }
  128. else /* !start */
  129. pipe->status = PIPE_STOPPED;
  130. return 0;
  131. }
  132. static int mixart_set_clock(struct mixart_mgr *mgr,
  133. struct mixart_pipe *pipe, unsigned int rate)
  134. {
  135. struct mixart_msg request;
  136. struct mixart_clock_properties clock_properties;
  137. struct mixart_clock_properties_resp clock_prop_resp;
  138. int err;
  139. switch(pipe->status) {
  140. case PIPE_CLOCK_SET:
  141. break;
  142. case PIPE_RUNNING:
  143. if(rate != 0)
  144. break;
  145. default:
  146. if(rate == 0)
  147. return 0; /* nothing to do */
  148. else {
  149. snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
  150. return -EINVAL;
  151. }
  152. }
  153. memset(&clock_properties, 0, sizeof(clock_properties));
  154. clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
  155. clock_properties.clock_mode = CM_STANDALONE;
  156. clock_properties.frequency = rate;
  157. clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
  158. clock_properties.uid_caller[0] = pipe->group_uid;
  159. snd_printdd("mixart_set_clock to %d kHz\n", rate);
  160. request.message_id = MSG_CLOCK_SET_PROPERTIES;
  161. request.uid = mgr->uid_console_manager;
  162. request.data = &clock_properties;
  163. request.size = sizeof(clock_properties);
  164. err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
  165. if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
  166. 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);
  167. return -EINVAL;
  168. }
  169. if(rate) pipe->status = PIPE_CLOCK_SET;
  170. else pipe->status = PIPE_RUNNING;
  171. return 0;
  172. }
  173. /*
  174. * Allocate or reference output pipe for analog IOs (pcmp0/1)
  175. */
  176. struct mixart_pipe *
  177. snd_mixart_add_ref_pipe(struct snd_mixart *chip, int pcm_number, int capture,
  178. int monitoring)
  179. {
  180. int stream_count;
  181. struct mixart_pipe *pipe;
  182. struct mixart_msg request;
  183. if(capture) {
  184. if (pcm_number == MIXART_PCM_ANALOG) {
  185. pipe = &(chip->pipe_in_ana); /* analog inputs */
  186. } else {
  187. pipe = &(chip->pipe_in_dig); /* digital inputs */
  188. }
  189. request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
  190. stream_count = MIXART_CAPTURE_STREAMS;
  191. } else {
  192. if (pcm_number == MIXART_PCM_ANALOG) {
  193. pipe = &(chip->pipe_out_ana); /* analog outputs */
  194. } else {
  195. pipe = &(chip->pipe_out_dig); /* digital outputs */
  196. }
  197. request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
  198. stream_count = MIXART_PLAYBACK_STREAMS;
  199. }
  200. /* a new stream is opened and there are already all streams in use */
  201. if( (monitoring == 0) && (pipe->references >= stream_count) ) {
  202. return NULL;
  203. }
  204. /* pipe is not yet defined */
  205. if( pipe->status == PIPE_UNDEFINED ) {
  206. int err, i;
  207. struct {
  208. struct mixart_streaming_group_req sgroup_req;
  209. struct mixart_streaming_group sgroup_resp;
  210. } *buf;
  211. snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
  212. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  213. if (!buf)
  214. return NULL;
  215. request.uid = (struct mixart_uid){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */
  216. request.data = &buf->sgroup_req;
  217. request.size = sizeof(buf->sgroup_req);
  218. memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
  219. buf->sgroup_req.stream_count = stream_count;
  220. buf->sgroup_req.channel_count = 2;
  221. buf->sgroup_req.latency = 256;
  222. buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */
  223. for (i=0; i<stream_count; i++) {
  224. int j;
  225. struct mixart_flowinfo *flowinfo;
  226. struct mixart_bufferinfo *bufferinfo;
  227. /* we don't yet know the format, so config 16 bit pcm audio for instance */
  228. buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
  229. buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
  230. buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
  231. /* find the right bufferinfo_array */
  232. j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
  233. if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
  234. buf->sgroup_req.flow_entry[i] = j;
  235. flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
  236. flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(struct mixart_bufferinfo));
  237. flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */
  238. bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
  239. bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */
  240. bufferinfo[j].available_length = 0; /* buffer is not yet allocated */
  241. /* construct the identifier of the stream buffer received in the interrupts ! */
  242. bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
  243. if(capture) {
  244. bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
  245. }
  246. }
  247. err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
  248. if((err < 0) || (buf->sgroup_resp.status != 0)) {
  249. snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
  250. kfree(buf);
  251. return NULL;
  252. }
  253. pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */
  254. pipe->stream_count = buf->sgroup_resp.stream_count;
  255. /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
  256. pipe->status = PIPE_STOPPED;
  257. kfree(buf);
  258. }
  259. if(monitoring) pipe->monitoring = 1;
  260. else pipe->references++;
  261. return pipe;
  262. }
  263. int snd_mixart_kill_ref_pipe(struct mixart_mgr *mgr,
  264. struct mixart_pipe *pipe, int monitoring)
  265. {
  266. int err = 0;
  267. if(pipe->status == PIPE_UNDEFINED)
  268. return 0;
  269. if(monitoring)
  270. pipe->monitoring = 0;
  271. else
  272. pipe->references--;
  273. if((pipe->references <= 0) && (pipe->monitoring == 0)) {
  274. struct mixart_msg request;
  275. struct mixart_delete_group_resp delete_resp;
  276. /* release the clock */
  277. err = mixart_set_clock( mgr, pipe, 0);
  278. if( err < 0 ) {
  279. snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
  280. }
  281. /* stop the pipe */
  282. err = mixart_set_pipe_state(mgr, pipe, 0);
  283. if( err < 0 ) {
  284. snd_printk(KERN_ERR "error stopping pipe!\n");
  285. }
  286. request.message_id = MSG_STREAM_DELETE_GROUP;
  287. request.uid = (struct mixart_uid){0,0};
  288. request.data = &pipe->group_uid; /* the streaming group ! */
  289. request.size = sizeof(pipe->group_uid);
  290. /* delete the pipe */
  291. err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
  292. if ((err < 0) || (delete_resp.status != 0)) {
  293. snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
  294. }
  295. pipe->group_uid = (struct mixart_uid){0,0};
  296. pipe->stream_count = 0;
  297. pipe->status = PIPE_UNDEFINED;
  298. }
  299. return err;
  300. }
  301. static int mixart_set_stream_state(struct mixart_stream *stream, int start)
  302. {
  303. struct snd_mixart *chip;
  304. struct mixart_stream_state_req stream_state_req;
  305. struct mixart_msg request;
  306. if(!stream->substream)
  307. return -EINVAL;
  308. memset(&stream_state_req, 0, sizeof(stream_state_req));
  309. stream_state_req.stream_count = 1;
  310. stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
  311. stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
  312. if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  313. request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
  314. else
  315. request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
  316. request.uid = (struct mixart_uid){0,0};
  317. request.data = &stream_state_req;
  318. request.size = sizeof(stream_state_req);
  319. stream->abs_period_elapsed = 0; /* reset stream pos */
  320. stream->buf_periods = 0;
  321. stream->buf_period_frag = 0;
  322. chip = snd_pcm_substream_chip(stream->substream);
  323. return snd_mixart_send_msg_nonblock(chip->mgr, &request);
  324. }
  325. /*
  326. * Trigger callback
  327. */
  328. static int snd_mixart_trigger(struct snd_pcm_substream *subs, int cmd)
  329. {
  330. struct mixart_stream *stream = subs->runtime->private_data;
  331. switch (cmd) {
  332. case SNDRV_PCM_TRIGGER_START:
  333. snd_printdd("SNDRV_PCM_TRIGGER_START\n");
  334. /* START_STREAM */
  335. if( mixart_set_stream_state(stream, 1) )
  336. return -EINVAL;
  337. stream->status = MIXART_STREAM_STATUS_RUNNING;
  338. break;
  339. case SNDRV_PCM_TRIGGER_STOP:
  340. /* STOP_STREAM */
  341. if( mixart_set_stream_state(stream, 0) )
  342. return -EINVAL;
  343. stream->status = MIXART_STREAM_STATUS_OPEN;
  344. snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
  345. break;
  346. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  347. /* TODO */
  348. stream->status = MIXART_STREAM_STATUS_PAUSE;
  349. snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
  350. break;
  351. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  352. /* TODO */
  353. stream->status = MIXART_STREAM_STATUS_RUNNING;
  354. snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
  355. break;
  356. default:
  357. return -EINVAL;
  358. }
  359. return 0;
  360. }
  361. static int mixart_sync_nonblock_events(struct mixart_mgr *mgr)
  362. {
  363. unsigned long timeout = jiffies + HZ;
  364. while (atomic_read(&mgr->msg_processed) > 0) {
  365. if (time_after(jiffies, timeout)) {
  366. snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
  367. return -EBUSY;
  368. }
  369. schedule_timeout_uninterruptible(1);
  370. }
  371. return 0;
  372. }
  373. /*
  374. * prepare callback for all pcms
  375. */
  376. static int snd_mixart_prepare(struct snd_pcm_substream *subs)
  377. {
  378. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  379. struct mixart_stream *stream = subs->runtime->private_data;
  380. /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
  381. snd_printdd("snd_mixart_prepare\n");
  382. mixart_sync_nonblock_events(chip->mgr);
  383. /* only the first stream can choose the sample rate */
  384. /* the further opened streams will be limited to its frequency (see open) */
  385. if(chip->mgr->ref_count_rate == 1)
  386. chip->mgr->sample_rate = subs->runtime->rate;
  387. /* set the clock only once (first stream) on the same pipe */
  388. if(stream->pipe->references == 1) {
  389. if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. static int mixart_set_format(struct mixart_stream *stream, snd_pcm_format_t format)
  395. {
  396. int err;
  397. struct snd_mixart *chip;
  398. struct mixart_msg request;
  399. struct mixart_stream_param_desc stream_param;
  400. struct mixart_return_uid resp;
  401. chip = snd_pcm_substream_chip(stream->substream);
  402. memset(&stream_param, 0, sizeof(stream_param));
  403. stream_param.coding_type = CT_LINEAR;
  404. stream_param.number_of_channel = stream->channels;
  405. stream_param.sampling_freq = chip->mgr->sample_rate;
  406. if(stream_param.sampling_freq == 0)
  407. stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
  408. switch(format){
  409. case SNDRV_PCM_FORMAT_U8:
  410. stream_param.sample_type = ST_INTEGER_8;
  411. stream_param.sample_size = 8;
  412. break;
  413. case SNDRV_PCM_FORMAT_S16_LE:
  414. stream_param.sample_type = ST_INTEGER_16LE;
  415. stream_param.sample_size = 16;
  416. break;
  417. case SNDRV_PCM_FORMAT_S16_BE:
  418. stream_param.sample_type = ST_INTEGER_16BE;
  419. stream_param.sample_size = 16;
  420. break;
  421. case SNDRV_PCM_FORMAT_S24_3LE:
  422. stream_param.sample_type = ST_INTEGER_24LE;
  423. stream_param.sample_size = 24;
  424. break;
  425. case SNDRV_PCM_FORMAT_S24_3BE:
  426. stream_param.sample_type = ST_INTEGER_24BE;
  427. stream_param.sample_size = 24;
  428. break;
  429. case SNDRV_PCM_FORMAT_FLOAT_LE:
  430. stream_param.sample_type = ST_FLOATING_POINT_32LE;
  431. stream_param.sample_size = 32;
  432. break;
  433. case SNDRV_PCM_FORMAT_FLOAT_BE:
  434. stream_param.sample_type = ST_FLOATING_POINT_32BE;
  435. stream_param.sample_size = 32;
  436. break;
  437. default:
  438. snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
  439. return -EINVAL;
  440. }
  441. snd_printdd("set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
  442. stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
  443. /* TODO: what else to configure ? */
  444. /* stream_param.samples_per_frame = 2; */
  445. /* stream_param.bytes_per_frame = 4; */
  446. /* stream_param.bytes_per_sample = 2; */
  447. stream_param.pipe_count = 1; /* set to 1 */
  448. stream_param.stream_count = 1; /* set to 1 */
  449. stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
  450. stream_param.stream_desc[0].stream_idx = stream->substream->number;
  451. request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
  452. request.uid = (struct mixart_uid){0,0};
  453. request.data = &stream_param;
  454. request.size = sizeof(stream_param);
  455. err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
  456. if((err < 0) || resp.error_code) {
  457. snd_printk(KERN_ERR "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", err, resp.error_code);
  458. return -EINVAL;
  459. }
  460. return 0;
  461. }
  462. /*
  463. * HW_PARAMS callback for all pcms
  464. */
  465. static int snd_mixart_hw_params(struct snd_pcm_substream *subs,
  466. struct snd_pcm_hw_params *hw)
  467. {
  468. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  469. struct mixart_mgr *mgr = chip->mgr;
  470. struct mixart_stream *stream = subs->runtime->private_data;
  471. snd_pcm_format_t format;
  472. int err;
  473. int channels;
  474. /* set up channels */
  475. channels = params_channels(hw);
  476. /* set up format for the stream */
  477. format = params_format(hw);
  478. mutex_lock(&mgr->setup_mutex);
  479. /* update the stream levels */
  480. if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
  481. int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
  482. if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
  483. mixart_update_playback_stream_level(chip, is_aes, subs->number);
  484. else
  485. mixart_update_capture_stream_level( chip, is_aes);
  486. }
  487. stream->channels = channels;
  488. /* set the format to the board */
  489. err = mixart_set_format(stream, format);
  490. if(err < 0) {
  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 | SNDRV_PCM_INFO_SYNC_START |
  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 | SNDRV_PCM_INFO_SYNC_START |
  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_assert ( 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_assert ( 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. mgr->chip[idx] = 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. 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, (void *)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 0: /* SEEK_SET */
  893. file->f_pos = offset;
  894. break;
  895. case 1: /* SEEK_CUR */
  896. file->f_pos += offset;
  897. break;
  898. case 2: /* 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 0: /* SEEK_SET */
  917. file->f_pos = offset;
  918. break;
  919. case 1: /* SEEK_CUR */
  920. file->f_pos += offset;
  921. break;
  922. case 2: /* 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_size = 1024;
  1009. entry->c.text.read = snd_mixart_proc_read;
  1010. }
  1011. if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
  1012. entry->content = SNDRV_INFO_CONTENT_DATA;
  1013. entry->private_data = chip->mgr;
  1014. entry->c.ops = &snd_mixart_proc_ops_BA0;
  1015. entry->size = MIXART_BA0_SIZE;
  1016. }
  1017. if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
  1018. entry->content = SNDRV_INFO_CONTENT_DATA;
  1019. entry->private_data = chip->mgr;
  1020. entry->c.ops = &snd_mixart_proc_ops_BA1;
  1021. entry->size = MIXART_BA1_SIZE;
  1022. }
  1023. }
  1024. /* end of proc interface */
  1025. /*
  1026. * probe function - creates the card manager
  1027. */
  1028. static int __devinit snd_mixart_probe(struct pci_dev *pci,
  1029. const struct pci_device_id *pci_id)
  1030. {
  1031. static int dev;
  1032. struct mixart_mgr *mgr;
  1033. unsigned int i;
  1034. int err;
  1035. size_t size;
  1036. /*
  1037. */
  1038. if (dev >= SNDRV_CARDS)
  1039. return -ENODEV;
  1040. if (! enable[dev]) {
  1041. dev++;
  1042. return -ENOENT;
  1043. }
  1044. /* enable PCI device */
  1045. if ((err = pci_enable_device(pci)) < 0)
  1046. return err;
  1047. pci_set_master(pci);
  1048. /* check if we can restrict PCI DMA transfers to 32 bits */
  1049. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0) {
  1050. snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
  1051. pci_disable_device(pci);
  1052. return -ENXIO;
  1053. }
  1054. /*
  1055. */
  1056. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  1057. if (! mgr) {
  1058. pci_disable_device(pci);
  1059. return -ENOMEM;
  1060. }
  1061. mgr->pci = pci;
  1062. mgr->irq = -1;
  1063. /* resource assignment */
  1064. if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
  1065. kfree(mgr);
  1066. pci_disable_device(pci);
  1067. return err;
  1068. }
  1069. for (i = 0; i < 2; i++) {
  1070. mgr->mem[i].phys = pci_resource_start(pci, i);
  1071. mgr->mem[i].virt = ioremap_nocache(mgr->mem[i].phys,
  1072. pci_resource_len(pci, i));
  1073. }
  1074. if (request_irq(pci->irq, snd_mixart_interrupt, SA_INTERRUPT|SA_SHIRQ, CARD_NAME, (void *)mgr)) {
  1075. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1076. snd_mixart_free(mgr);
  1077. return -EBUSY;
  1078. }
  1079. mgr->irq = pci->irq;
  1080. sprintf(mgr->shortname, "Digigram miXart");
  1081. sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
  1082. /* ISR spinlock */
  1083. spin_lock_init(&mgr->lock);
  1084. /* init mailbox */
  1085. mgr->msg_fifo_readptr = 0;
  1086. mgr->msg_fifo_writeptr = 0;
  1087. spin_lock_init(&mgr->msg_lock);
  1088. mutex_init(&mgr->msg_mutex);
  1089. init_waitqueue_head(&mgr->msg_sleep);
  1090. atomic_set(&mgr->msg_processed, 0);
  1091. /* init setup mutex*/
  1092. mutex_init(&mgr->setup_mutex);
  1093. /* init message taslket */
  1094. tasklet_init(&mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
  1095. /* card assignment */
  1096. mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */
  1097. for (i = 0; i < mgr->num_cards; i++) {
  1098. struct snd_card *card;
  1099. char tmpid[16];
  1100. int idx;
  1101. if (index[dev] < 0)
  1102. idx = index[dev];
  1103. else
  1104. idx = index[dev] + i;
  1105. snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
  1106. card = snd_card_new(idx, tmpid, THIS_MODULE, 0);
  1107. if (! card) {
  1108. snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
  1109. snd_mixart_free(mgr);
  1110. return -ENOMEM;
  1111. }
  1112. strcpy(card->driver, CARD_NAME);
  1113. sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
  1114. sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
  1115. if ((err = snd_mixart_create(mgr, card, i)) < 0) {
  1116. snd_mixart_free(mgr);
  1117. return err;
  1118. }
  1119. if(i==0) {
  1120. /* init proc interface only for chip0 */
  1121. snd_mixart_proc_init(mgr->chip[i]);
  1122. }
  1123. if ((err = snd_card_register(card)) < 0) {
  1124. snd_mixart_free(mgr);
  1125. return err;
  1126. }
  1127. }
  1128. /* init firmware status (mgr->dsp_loaded reset in hwdep_new) */
  1129. mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
  1130. /* create array of streaminfo */
  1131. size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
  1132. sizeof(struct mixart_flowinfo)) );
  1133. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1134. size, &mgr->flowinfo) < 0) {
  1135. snd_mixart_free(mgr);
  1136. return -ENOMEM;
  1137. }
  1138. /* init streaminfo_array */
  1139. memset(mgr->flowinfo.area, 0, size);
  1140. /* create array of bufferinfo */
  1141. size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
  1142. sizeof(struct mixart_bufferinfo)) );
  1143. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1144. size, &mgr->bufferinfo) < 0) {
  1145. snd_mixart_free(mgr);
  1146. return -ENOMEM;
  1147. }
  1148. /* init bufferinfo_array */
  1149. memset(mgr->bufferinfo.area, 0, size);
  1150. /* set up firmware */
  1151. err = snd_mixart_setup_firmware(mgr);
  1152. if (err < 0) {
  1153. snd_mixart_free(mgr);
  1154. return err;
  1155. }
  1156. pci_set_drvdata(pci, mgr);
  1157. dev++;
  1158. return 0;
  1159. }
  1160. static void __devexit snd_mixart_remove(struct pci_dev *pci)
  1161. {
  1162. snd_mixart_free(pci_get_drvdata(pci));
  1163. pci_set_drvdata(pci, NULL);
  1164. }
  1165. static struct pci_driver driver = {
  1166. .name = "Digigram miXart",
  1167. .id_table = snd_mixart_ids,
  1168. .probe = snd_mixart_probe,
  1169. .remove = __devexit_p(snd_mixart_remove),
  1170. };
  1171. static int __init alsa_card_mixart_init(void)
  1172. {
  1173. return pci_register_driver(&driver);
  1174. }
  1175. static void __exit alsa_card_mixart_exit(void)
  1176. {
  1177. pci_unregister_driver(&driver);
  1178. }
  1179. module_init(alsa_card_mixart_init)
  1180. module_exit(alsa_card_mixart_exit)