mixart.c 38 KB

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