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