mixart.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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 const 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 const 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. return -ENOMEM;
  855. chip->card = card;
  856. chip->chip_idx = idx;
  857. chip->mgr = mgr;
  858. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  859. snd_mixart_chip_free(chip);
  860. return err;
  861. }
  862. mgr->chip[idx] = chip;
  863. return 0;
  864. }
  865. int snd_mixart_create_pcm(struct snd_mixart* chip)
  866. {
  867. int err;
  868. err = snd_mixart_pcm_analog(chip);
  869. if (err < 0)
  870. return err;
  871. if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
  872. err = snd_mixart_pcm_digital(chip);
  873. if (err < 0)
  874. return err;
  875. }
  876. return err;
  877. }
  878. /*
  879. * release all the cards assigned to a manager instance
  880. */
  881. static int snd_mixart_free(struct mixart_mgr *mgr)
  882. {
  883. unsigned int i;
  884. for (i = 0; i < mgr->num_cards; i++) {
  885. if (mgr->chip[i])
  886. snd_card_free(mgr->chip[i]->card);
  887. }
  888. /* stop mailbox */
  889. snd_mixart_exit_mailbox(mgr);
  890. /* release irq */
  891. if (mgr->irq >= 0)
  892. free_irq(mgr->irq, mgr);
  893. /* reset board if some firmware was loaded */
  894. if(mgr->dsp_loaded) {
  895. snd_mixart_reset_board(mgr);
  896. dev_dbg(&mgr->pci->dev, "reset miXart !\n");
  897. }
  898. /* release the i/o ports */
  899. for (i = 0; i < 2; ++i)
  900. iounmap(mgr->mem[i].virt);
  901. pci_release_regions(mgr->pci);
  902. /* free flowarray */
  903. if(mgr->flowinfo.area) {
  904. snd_dma_free_pages(&mgr->flowinfo);
  905. mgr->flowinfo.area = NULL;
  906. }
  907. /* free bufferarray */
  908. if(mgr->bufferinfo.area) {
  909. snd_dma_free_pages(&mgr->bufferinfo);
  910. mgr->bufferinfo.area = NULL;
  911. }
  912. pci_disable_device(mgr->pci);
  913. kfree(mgr);
  914. return 0;
  915. }
  916. /*
  917. * proc interface
  918. */
  919. /*
  920. mixart_BA0 proc interface for BAR 0 - read callback
  921. */
  922. static ssize_t snd_mixart_BA0_read(struct snd_info_entry *entry,
  923. void *file_private_data,
  924. struct file *file, char __user *buf,
  925. size_t count, loff_t pos)
  926. {
  927. struct mixart_mgr *mgr = entry->private_data;
  928. count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
  929. if (copy_to_user_fromio(buf, MIXART_MEM(mgr, pos), count))
  930. return -EFAULT;
  931. return count;
  932. }
  933. /*
  934. mixart_BA1 proc interface for BAR 1 - read callback
  935. */
  936. static ssize_t snd_mixart_BA1_read(struct snd_info_entry *entry,
  937. void *file_private_data,
  938. struct file *file, char __user *buf,
  939. size_t count, loff_t pos)
  940. {
  941. struct mixart_mgr *mgr = entry->private_data;
  942. count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
  943. if (copy_to_user_fromio(buf, MIXART_REG(mgr, pos), count))
  944. return -EFAULT;
  945. return count;
  946. }
  947. static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = {
  948. .read = snd_mixart_BA0_read,
  949. };
  950. static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = {
  951. .read = snd_mixart_BA1_read,
  952. };
  953. static void snd_mixart_proc_read(struct snd_info_entry *entry,
  954. struct snd_info_buffer *buffer)
  955. {
  956. struct snd_mixart *chip = entry->private_data;
  957. u32 ref;
  958. snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx);
  959. /* stats available when embedded OS is running */
  960. if (chip->mgr->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) {
  961. snd_iprintf(buffer, "- hardware -\n");
  962. switch (chip->mgr->board_type ) {
  963. case MIXART_DAUGHTER_TYPE_NONE : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break;
  964. case MIXART_DAUGHTER_TYPE_AES : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break;
  965. case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break;
  966. default: snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break;
  967. }
  968. snd_iprintf(buffer, "- system load -\n");
  969. /* get perf reference */
  970. ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET));
  971. if (ref) {
  972. u32 mailbox = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref;
  973. u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref;
  974. u32 interr = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref;
  975. snd_iprintf(buffer, "\tstreaming : %d\n", streaming);
  976. snd_iprintf(buffer, "\tmailbox : %d\n", mailbox);
  977. snd_iprintf(buffer, "\tinterrupts handling : %d\n\n", interr);
  978. }
  979. } /* endif elf loaded */
  980. }
  981. static void snd_mixart_proc_init(struct snd_mixart *chip)
  982. {
  983. struct snd_info_entry *entry;
  984. /* text interface to read perf and temp meters */
  985. if (! snd_card_proc_new(chip->card, "board_info", &entry)) {
  986. entry->private_data = chip;
  987. entry->c.text.read = snd_mixart_proc_read;
  988. }
  989. if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
  990. entry->content = SNDRV_INFO_CONTENT_DATA;
  991. entry->private_data = chip->mgr;
  992. entry->c.ops = &snd_mixart_proc_ops_BA0;
  993. entry->size = MIXART_BA0_SIZE;
  994. }
  995. if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
  996. entry->content = SNDRV_INFO_CONTENT_DATA;
  997. entry->private_data = chip->mgr;
  998. entry->c.ops = &snd_mixart_proc_ops_BA1;
  999. entry->size = MIXART_BA1_SIZE;
  1000. }
  1001. }
  1002. /* end of proc interface */
  1003. /*
  1004. * probe function - creates the card manager
  1005. */
  1006. static int snd_mixart_probe(struct pci_dev *pci,
  1007. const struct pci_device_id *pci_id)
  1008. {
  1009. static int dev;
  1010. struct mixart_mgr *mgr;
  1011. unsigned int i;
  1012. int err;
  1013. size_t size;
  1014. /*
  1015. */
  1016. if (dev >= SNDRV_CARDS)
  1017. return -ENODEV;
  1018. if (! enable[dev]) {
  1019. dev++;
  1020. return -ENOENT;
  1021. }
  1022. /* enable PCI device */
  1023. if ((err = pci_enable_device(pci)) < 0)
  1024. return err;
  1025. pci_set_master(pci);
  1026. /* check if we can restrict PCI DMA transfers to 32 bits */
  1027. if (dma_set_mask(&pci->dev, DMA_BIT_MASK(32)) < 0) {
  1028. dev_err(&pci->dev,
  1029. "architecture does not support 32bit PCI busmaster DMA\n");
  1030. pci_disable_device(pci);
  1031. return -ENXIO;
  1032. }
  1033. /*
  1034. */
  1035. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  1036. if (! mgr) {
  1037. pci_disable_device(pci);
  1038. return -ENOMEM;
  1039. }
  1040. mgr->pci = pci;
  1041. mgr->irq = -1;
  1042. /* resource assignment */
  1043. if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
  1044. kfree(mgr);
  1045. pci_disable_device(pci);
  1046. return err;
  1047. }
  1048. for (i = 0; i < 2; i++) {
  1049. mgr->mem[i].phys = pci_resource_start(pci, i);
  1050. mgr->mem[i].virt = pci_ioremap_bar(pci, i);
  1051. if (!mgr->mem[i].virt) {
  1052. dev_err(&pci->dev, "unable to remap resource 0x%lx\n",
  1053. mgr->mem[i].phys);
  1054. snd_mixart_free(mgr);
  1055. return -EBUSY;
  1056. }
  1057. }
  1058. if (request_threaded_irq(pci->irq, snd_mixart_interrupt,
  1059. snd_mixart_threaded_irq, IRQF_SHARED,
  1060. KBUILD_MODNAME, mgr)) {
  1061. dev_err(&pci->dev, "unable to grab IRQ %d\n", pci->irq);
  1062. snd_mixart_free(mgr);
  1063. return -EBUSY;
  1064. }
  1065. mgr->irq = pci->irq;
  1066. /* init mailbox */
  1067. mgr->msg_fifo_readptr = 0;
  1068. mgr->msg_fifo_writeptr = 0;
  1069. mutex_init(&mgr->lock);
  1070. mutex_init(&mgr->msg_lock);
  1071. init_waitqueue_head(&mgr->msg_sleep);
  1072. atomic_set(&mgr->msg_processed, 0);
  1073. /* init setup mutex*/
  1074. mutex_init(&mgr->setup_mutex);
  1075. /* card assignment */
  1076. mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */
  1077. for (i = 0; i < mgr->num_cards; i++) {
  1078. struct snd_card *card;
  1079. char tmpid[16];
  1080. int idx;
  1081. if (index[dev] < 0)
  1082. idx = index[dev];
  1083. else
  1084. idx = index[dev] + i;
  1085. snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
  1086. err = snd_card_new(&pci->dev, idx, tmpid, THIS_MODULE,
  1087. 0, &card);
  1088. if (err < 0) {
  1089. dev_err(&pci->dev, "cannot allocate the card %d\n", i);
  1090. snd_mixart_free(mgr);
  1091. return err;
  1092. }
  1093. strcpy(card->driver, CARD_NAME);
  1094. snprintf(card->shortname, sizeof(card->shortname),
  1095. "Digigram miXart [PCM #%d]", i);
  1096. snprintf(card->longname, sizeof(card->longname),
  1097. "Digigram miXart at 0x%lx & 0x%lx, irq %i [PCM #%d]",
  1098. mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq, i);
  1099. if ((err = snd_mixart_create(mgr, card, i)) < 0) {
  1100. snd_card_free(card);
  1101. snd_mixart_free(mgr);
  1102. return err;
  1103. }
  1104. if(i==0) {
  1105. /* init proc interface only for chip0 */
  1106. snd_mixart_proc_init(mgr->chip[i]);
  1107. }
  1108. if ((err = snd_card_register(card)) < 0) {
  1109. snd_mixart_free(mgr);
  1110. return err;
  1111. }
  1112. }
  1113. /* init firmware status (mgr->dsp_loaded reset in hwdep_new) */
  1114. mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
  1115. /* create array of streaminfo */
  1116. size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
  1117. sizeof(struct mixart_flowinfo)) );
  1118. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1119. size, &mgr->flowinfo) < 0) {
  1120. snd_mixart_free(mgr);
  1121. return -ENOMEM;
  1122. }
  1123. /* init streaminfo_array */
  1124. memset(mgr->flowinfo.area, 0, size);
  1125. /* create array of bufferinfo */
  1126. size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
  1127. sizeof(struct mixart_bufferinfo)) );
  1128. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1129. size, &mgr->bufferinfo) < 0) {
  1130. snd_mixart_free(mgr);
  1131. return -ENOMEM;
  1132. }
  1133. /* init bufferinfo_array */
  1134. memset(mgr->bufferinfo.area, 0, size);
  1135. /* set up firmware */
  1136. err = snd_mixart_setup_firmware(mgr);
  1137. if (err < 0) {
  1138. snd_mixart_free(mgr);
  1139. return err;
  1140. }
  1141. pci_set_drvdata(pci, mgr);
  1142. dev++;
  1143. return 0;
  1144. }
  1145. static void snd_mixart_remove(struct pci_dev *pci)
  1146. {
  1147. snd_mixart_free(pci_get_drvdata(pci));
  1148. }
  1149. static struct pci_driver mixart_driver = {
  1150. .name = KBUILD_MODNAME,
  1151. .id_table = snd_mixart_ids,
  1152. .probe = snd_mixart_probe,
  1153. .remove = snd_mixart_remove,
  1154. };
  1155. module_pci_driver(mixart_driver);