mixart.c 39 KB

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