bebob_stream.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * bebob_stream.c - a part of driver for BeBoB based devices
  3. *
  4. * Copyright (c) 2013-2014 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "./bebob.h"
  9. #define CALLBACK_TIMEOUT 2000
  10. #define FW_ISO_RESOURCE_DELAY 1000
  11. /*
  12. * NOTE;
  13. * For BeBoB streams, Both of input and output CMP connection are important.
  14. *
  15. * For most devices, each CMP connection starts to transmit/receive a
  16. * corresponding stream. But for a few devices, both of CMP connection needs
  17. * to start transmitting stream. An example is 'M-Audio Firewire 410'.
  18. */
  19. /* 128 is an arbitrary length but it seems to be enough */
  20. #define FORMAT_MAXIMUM_LENGTH 128
  21. const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES] = {
  22. [0] = 32000,
  23. [1] = 44100,
  24. [2] = 48000,
  25. [3] = 88200,
  26. [4] = 96000,
  27. [5] = 176400,
  28. [6] = 192000,
  29. };
  30. /*
  31. * See: Table 51: Extended Stream Format Info ‘Sampling Frequency’
  32. * in Additional AVC commands (Nov 2003, BridgeCo)
  33. */
  34. static const unsigned int bridgeco_freq_table[] = {
  35. [0] = 0x02,
  36. [1] = 0x03,
  37. [2] = 0x04,
  38. [3] = 0x0a,
  39. [4] = 0x05,
  40. [5] = 0x06,
  41. [6] = 0x07,
  42. };
  43. static int
  44. get_formation_index(unsigned int rate, unsigned int *index)
  45. {
  46. unsigned int i;
  47. for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
  48. if (snd_bebob_rate_table[i] == rate) {
  49. *index = i;
  50. return 0;
  51. }
  52. }
  53. return -EINVAL;
  54. }
  55. int
  56. snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *curr_rate)
  57. {
  58. unsigned int tx_rate, rx_rate, trials;
  59. int err;
  60. trials = 0;
  61. do {
  62. err = avc_general_get_sig_fmt(bebob->unit, &tx_rate,
  63. AVC_GENERAL_PLUG_DIR_OUT, 0);
  64. } while (err == -EAGAIN && ++trials < 3);
  65. if (err < 0)
  66. goto end;
  67. trials = 0;
  68. do {
  69. err = avc_general_get_sig_fmt(bebob->unit, &rx_rate,
  70. AVC_GENERAL_PLUG_DIR_IN, 0);
  71. } while (err == -EAGAIN && ++trials < 3);
  72. if (err < 0)
  73. goto end;
  74. *curr_rate = rx_rate;
  75. if (rx_rate == tx_rate)
  76. goto end;
  77. /* synchronize receive stream rate to transmit stream rate */
  78. err = avc_general_set_sig_fmt(bebob->unit, rx_rate,
  79. AVC_GENERAL_PLUG_DIR_IN, 0);
  80. end:
  81. return err;
  82. }
  83. int
  84. snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate)
  85. {
  86. int err;
  87. err = avc_general_set_sig_fmt(bebob->unit, rate,
  88. AVC_GENERAL_PLUG_DIR_OUT, 0);
  89. if (err < 0)
  90. goto end;
  91. err = avc_general_set_sig_fmt(bebob->unit, rate,
  92. AVC_GENERAL_PLUG_DIR_IN, 0);
  93. if (err < 0)
  94. goto end;
  95. /*
  96. * Some devices need a bit time for transition.
  97. * 300msec is got by some experiments.
  98. */
  99. msleep(300);
  100. end:
  101. return err;
  102. }
  103. int snd_bebob_stream_get_clock_src(struct snd_bebob *bebob,
  104. enum snd_bebob_clock_type *src)
  105. {
  106. const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
  107. u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
  108. unsigned int id;
  109. enum avc_bridgeco_plug_type type;
  110. int err = 0;
  111. /* 1.The device has its own operation to switch source of clock */
  112. if (clk_spec) {
  113. err = clk_spec->get(bebob, &id);
  114. if (err < 0) {
  115. dev_err(&bebob->unit->device,
  116. "fail to get clock source: %d\n", err);
  117. goto end;
  118. }
  119. if (id >= clk_spec->num) {
  120. dev_err(&bebob->unit->device,
  121. "clock source %d out of range 0..%d\n",
  122. id, clk_spec->num - 1);
  123. err = -EIO;
  124. goto end;
  125. }
  126. *src = clk_spec->types[id];
  127. goto end;
  128. }
  129. /*
  130. * 2.The device don't support to switch source of clock then assumed
  131. * to use internal clock always
  132. */
  133. if (bebob->sync_input_plug < 0) {
  134. *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
  135. goto end;
  136. }
  137. /*
  138. * 3.The device supports to switch source of clock by an usual way.
  139. * Let's check input for 'Music Sub Unit Sync Input' plug.
  140. */
  141. avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
  142. bebob->sync_input_plug);
  143. err = avc_bridgeco_get_plug_input(bebob->unit, addr, input);
  144. if (err < 0) {
  145. dev_err(&bebob->unit->device,
  146. "fail to get an input for MSU in plug %d: %d\n",
  147. bebob->sync_input_plug, err);
  148. goto end;
  149. }
  150. /*
  151. * If there are no input plugs, all of fields are 0xff.
  152. * Here check the first field. This field is used for direction.
  153. */
  154. if (input[0] == 0xff) {
  155. *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
  156. goto end;
  157. }
  158. /* The source from any output plugs is for one purpose only. */
  159. if (input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) {
  160. /*
  161. * In BeBoB architecture, the source from music subunit may
  162. * bypass from oPCR[0]. This means that this source gives
  163. * synchronization to IEEE 1394 cycle start packet.
  164. */
  165. if (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT &&
  166. input[2] == 0x0c) {
  167. *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
  168. goto end;
  169. }
  170. /* The source from any input units is for several purposes. */
  171. } else if (input[1] == AVC_BRIDGECO_PLUG_MODE_UNIT) {
  172. if (input[2] == AVC_BRIDGECO_PLUG_UNIT_ISOC) {
  173. if (input[3] == 0x00) {
  174. /*
  175. * This source comes from iPCR[0]. This means
  176. * that presentation timestamp calculated by
  177. * SYT series of the received packets. In
  178. * short, this driver is the master of
  179. * synchronization.
  180. */
  181. *src = SND_BEBOB_CLOCK_TYPE_SYT;
  182. goto end;
  183. } else {
  184. /*
  185. * This source comes from iPCR[1-29]. This
  186. * means that the synchronization stream is not
  187. * the Audio/MIDI compound stream.
  188. */
  189. *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
  190. goto end;
  191. }
  192. } else if (input[2] == AVC_BRIDGECO_PLUG_UNIT_EXT) {
  193. /* Check type of this plug. */
  194. avc_bridgeco_fill_unit_addr(addr,
  195. AVC_BRIDGECO_PLUG_DIR_IN,
  196. AVC_BRIDGECO_PLUG_UNIT_EXT,
  197. input[3]);
  198. err = avc_bridgeco_get_plug_type(bebob->unit, addr,
  199. &type);
  200. if (err < 0)
  201. goto end;
  202. if (type == AVC_BRIDGECO_PLUG_TYPE_DIG) {
  203. /*
  204. * SPDIF/ADAT or sometimes (not always) word
  205. * clock.
  206. */
  207. *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
  208. goto end;
  209. } else if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
  210. /* Often word clock. */
  211. *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
  212. goto end;
  213. } else if (type == AVC_BRIDGECO_PLUG_TYPE_ADDITION) {
  214. /*
  215. * Not standard.
  216. * Mostly, additional internal clock.
  217. */
  218. *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
  219. goto end;
  220. }
  221. }
  222. }
  223. /* Not supported. */
  224. err = -EIO;
  225. end:
  226. return err;
  227. }
  228. static int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
  229. {
  230. unsigned int sec, sections, ch, channels;
  231. unsigned int pcm, midi, location;
  232. unsigned int stm_pos, sec_loc, pos;
  233. u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
  234. enum avc_bridgeco_plug_dir dir;
  235. int err;
  236. /*
  237. * The length of return value of this command cannot be expected. Here
  238. * use the maximum length of FCP.
  239. */
  240. buf = kzalloc(256, GFP_KERNEL);
  241. if (buf == NULL)
  242. return -ENOMEM;
  243. if (s == &bebob->tx_stream)
  244. dir = AVC_BRIDGECO_PLUG_DIR_OUT;
  245. else
  246. dir = AVC_BRIDGECO_PLUG_DIR_IN;
  247. avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  248. err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
  249. if (err < 0) {
  250. dev_err(&bebob->unit->device,
  251. "fail to get channel position for isoc %s plug 0: %d\n",
  252. (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
  253. err);
  254. goto end;
  255. }
  256. pos = 0;
  257. /* positions in I/O buffer */
  258. pcm = 0;
  259. midi = 0;
  260. /* the number of sections in AMDTP packet */
  261. sections = buf[pos++];
  262. for (sec = 0; sec < sections; sec++) {
  263. /* type of this section */
  264. avc_bridgeco_fill_unit_addr(addr, dir,
  265. AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  266. err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
  267. sec, &type);
  268. if (err < 0) {
  269. dev_err(&bebob->unit->device,
  270. "fail to get section type for isoc %s plug 0: %d\n",
  271. (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
  272. "out",
  273. err);
  274. goto end;
  275. }
  276. /* NoType */
  277. if (type == 0xff) {
  278. err = -ENOSYS;
  279. goto end;
  280. }
  281. /* the number of channels in this section */
  282. channels = buf[pos++];
  283. for (ch = 0; ch < channels; ch++) {
  284. /* position of this channel in AMDTP packet */
  285. stm_pos = buf[pos++] - 1;
  286. /* location of this channel in this section */
  287. sec_loc = buf[pos++] - 1;
  288. /*
  289. * Basically the number of location is within the
  290. * number of channels in this section. But some models
  291. * of M-Audio don't follow this. Its location for MIDI
  292. * is the position of MIDI channels in AMDTP packet.
  293. */
  294. if (sec_loc >= channels)
  295. sec_loc = ch;
  296. switch (type) {
  297. /* for MIDI conformant data channel */
  298. case 0x0a:
  299. /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
  300. if ((midi > 0) && (stm_pos != midi)) {
  301. err = -ENOSYS;
  302. goto end;
  303. }
  304. amdtp_am824_set_midi_position(s, stm_pos);
  305. midi = stm_pos;
  306. break;
  307. /* for PCM data channel */
  308. case 0x01: /* Headphone */
  309. case 0x02: /* Microphone */
  310. case 0x03: /* Line */
  311. case 0x04: /* SPDIF */
  312. case 0x05: /* ADAT */
  313. case 0x06: /* TDIF */
  314. case 0x07: /* MADI */
  315. /* for undefined/changeable signal */
  316. case 0x08: /* Analog */
  317. case 0x09: /* Digital */
  318. default:
  319. location = pcm + sec_loc;
  320. if (location >= AM824_MAX_CHANNELS_FOR_PCM) {
  321. err = -ENOSYS;
  322. goto end;
  323. }
  324. amdtp_am824_set_pcm_position(s, location,
  325. stm_pos);
  326. break;
  327. }
  328. }
  329. if (type != 0x0a)
  330. pcm += channels;
  331. else
  332. midi += channels;
  333. }
  334. end:
  335. kfree(buf);
  336. return err;
  337. }
  338. static int
  339. init_both_connections(struct snd_bebob *bebob)
  340. {
  341. int err;
  342. err = cmp_connection_init(&bebob->in_conn,
  343. bebob->unit, CMP_INPUT, 0);
  344. if (err < 0)
  345. goto end;
  346. err = cmp_connection_init(&bebob->out_conn,
  347. bebob->unit, CMP_OUTPUT, 0);
  348. if (err < 0)
  349. cmp_connection_destroy(&bebob->in_conn);
  350. end:
  351. return err;
  352. }
  353. static int
  354. check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
  355. {
  356. struct cmp_connection *conn;
  357. bool used;
  358. int err;
  359. if (s == &bebob->tx_stream)
  360. conn = &bebob->out_conn;
  361. else
  362. conn = &bebob->in_conn;
  363. err = cmp_connection_check_used(conn, &used);
  364. if ((err >= 0) && used && !amdtp_stream_running(s)) {
  365. dev_err(&bebob->unit->device,
  366. "Connection established by others: %cPCR[%d]\n",
  367. (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
  368. conn->pcr_index);
  369. err = -EBUSY;
  370. }
  371. return err;
  372. }
  373. static int
  374. make_both_connections(struct snd_bebob *bebob, unsigned int rate)
  375. {
  376. int index, pcm_channels, midi_channels, err = 0;
  377. if (bebob->connected)
  378. goto end;
  379. /* confirm params for both streams */
  380. err = get_formation_index(rate, &index);
  381. if (err < 0)
  382. goto end;
  383. pcm_channels = bebob->tx_stream_formations[index].pcm;
  384. midi_channels = bebob->tx_stream_formations[index].midi;
  385. err = amdtp_am824_set_parameters(&bebob->tx_stream, rate,
  386. pcm_channels, midi_channels * 8,
  387. false);
  388. if (err < 0)
  389. goto end;
  390. pcm_channels = bebob->rx_stream_formations[index].pcm;
  391. midi_channels = bebob->rx_stream_formations[index].midi;
  392. err = amdtp_am824_set_parameters(&bebob->rx_stream, rate,
  393. pcm_channels, midi_channels * 8,
  394. false);
  395. if (err < 0)
  396. goto end;
  397. /* establish connections for both streams */
  398. err = cmp_connection_establish(&bebob->out_conn,
  399. amdtp_stream_get_max_payload(&bebob->tx_stream));
  400. if (err < 0)
  401. goto end;
  402. err = cmp_connection_establish(&bebob->in_conn,
  403. amdtp_stream_get_max_payload(&bebob->rx_stream));
  404. if (err < 0) {
  405. cmp_connection_break(&bebob->out_conn);
  406. goto end;
  407. }
  408. bebob->connected = true;
  409. end:
  410. return err;
  411. }
  412. static void
  413. break_both_connections(struct snd_bebob *bebob)
  414. {
  415. cmp_connection_break(&bebob->in_conn);
  416. cmp_connection_break(&bebob->out_conn);
  417. bebob->connected = false;
  418. /* These models seems to be in transition state for a longer time. */
  419. if (bebob->maudio_special_quirk != NULL)
  420. msleep(200);
  421. }
  422. static void
  423. destroy_both_connections(struct snd_bebob *bebob)
  424. {
  425. cmp_connection_destroy(&bebob->in_conn);
  426. cmp_connection_destroy(&bebob->out_conn);
  427. }
  428. static int
  429. start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
  430. unsigned int rate)
  431. {
  432. struct cmp_connection *conn;
  433. int err = 0;
  434. if (stream == &bebob->rx_stream)
  435. conn = &bebob->in_conn;
  436. else
  437. conn = &bebob->out_conn;
  438. /* channel mapping */
  439. if (bebob->maudio_special_quirk == NULL) {
  440. err = map_data_channels(bebob, stream);
  441. if (err < 0)
  442. goto end;
  443. }
  444. /* start amdtp stream */
  445. err = amdtp_stream_start(stream,
  446. conn->resources.channel,
  447. conn->speed);
  448. end:
  449. return err;
  450. }
  451. int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
  452. {
  453. int err;
  454. err = init_both_connections(bebob);
  455. if (err < 0)
  456. goto end;
  457. err = amdtp_am824_init(&bebob->tx_stream, bebob->unit,
  458. AMDTP_IN_STREAM, CIP_BLOCKING);
  459. if (err < 0) {
  460. amdtp_stream_destroy(&bebob->tx_stream);
  461. destroy_both_connections(bebob);
  462. goto end;
  463. }
  464. /*
  465. * BeBoB v3 transfers packets with these qurks:
  466. * - In the beginning of streaming, the value of dbc is incremented
  467. * even if no data blocks are transferred.
  468. * - The value of dbc is reset suddenly.
  469. */
  470. if (bebob->version > 2)
  471. bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC |
  472. CIP_SKIP_DBC_ZERO_CHECK;
  473. /*
  474. * At high sampling rate, M-Audio special firmware transmits empty
  475. * packet with the value of dbc incremented by 8 but the others are
  476. * valid to IEC 61883-1.
  477. */
  478. if (bebob->maudio_special_quirk)
  479. bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
  480. err = amdtp_am824_init(&bebob->rx_stream, bebob->unit,
  481. AMDTP_OUT_STREAM, CIP_BLOCKING);
  482. if (err < 0) {
  483. amdtp_stream_destroy(&bebob->tx_stream);
  484. amdtp_stream_destroy(&bebob->rx_stream);
  485. destroy_both_connections(bebob);
  486. }
  487. end:
  488. return err;
  489. }
  490. int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate)
  491. {
  492. const struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
  493. unsigned int curr_rate;
  494. int err = 0;
  495. /* Need no substreams */
  496. if (bebob->substreams_counter == 0)
  497. goto end;
  498. /*
  499. * Considering JACK/FFADO streaming:
  500. * TODO: This can be removed hwdep functionality becomes popular.
  501. */
  502. err = check_connection_used_by_others(bebob, &bebob->rx_stream);
  503. if (err < 0)
  504. goto end;
  505. /*
  506. * packet queueing error or detecting discontinuity
  507. *
  508. * At bus reset, connections should not be broken here. So streams need
  509. * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
  510. */
  511. if (amdtp_streaming_error(&bebob->rx_stream))
  512. amdtp_stream_stop(&bebob->rx_stream);
  513. if (amdtp_streaming_error(&bebob->tx_stream))
  514. amdtp_stream_stop(&bebob->tx_stream);
  515. if (!amdtp_stream_running(&bebob->rx_stream) &&
  516. !amdtp_stream_running(&bebob->tx_stream))
  517. break_both_connections(bebob);
  518. /* stop streams if rate is different */
  519. err = rate_spec->get(bebob, &curr_rate);
  520. if (err < 0) {
  521. dev_err(&bebob->unit->device,
  522. "fail to get sampling rate: %d\n", err);
  523. goto end;
  524. }
  525. if (rate == 0)
  526. rate = curr_rate;
  527. if (rate != curr_rate) {
  528. amdtp_stream_stop(&bebob->rx_stream);
  529. amdtp_stream_stop(&bebob->tx_stream);
  530. break_both_connections(bebob);
  531. }
  532. /* master should be always running */
  533. if (!amdtp_stream_running(&bebob->rx_stream)) {
  534. /*
  535. * NOTE:
  536. * If establishing connections at first, Yamaha GO46
  537. * (and maybe Terratec X24) don't generate sound.
  538. *
  539. * For firmware customized by M-Audio, refer to next NOTE.
  540. */
  541. if (bebob->maudio_special_quirk == NULL) {
  542. err = rate_spec->set(bebob, rate);
  543. if (err < 0) {
  544. dev_err(&bebob->unit->device,
  545. "fail to set sampling rate: %d\n",
  546. err);
  547. goto end;
  548. }
  549. }
  550. err = make_both_connections(bebob, rate);
  551. if (err < 0)
  552. goto end;
  553. err = start_stream(bebob, &bebob->rx_stream, rate);
  554. if (err < 0) {
  555. dev_err(&bebob->unit->device,
  556. "fail to run AMDTP master stream:%d\n", err);
  557. break_both_connections(bebob);
  558. goto end;
  559. }
  560. /*
  561. * NOTE:
  562. * The firmware customized by M-Audio uses these commands to
  563. * start transmitting stream. This is not usual way.
  564. */
  565. if (bebob->maudio_special_quirk != NULL) {
  566. err = rate_spec->set(bebob, rate);
  567. if (err < 0) {
  568. dev_err(&bebob->unit->device,
  569. "fail to ensure sampling rate: %d\n",
  570. err);
  571. amdtp_stream_stop(&bebob->rx_stream);
  572. break_both_connections(bebob);
  573. goto end;
  574. }
  575. }
  576. /* wait first callback */
  577. if (!amdtp_stream_wait_callback(&bebob->rx_stream,
  578. CALLBACK_TIMEOUT)) {
  579. amdtp_stream_stop(&bebob->rx_stream);
  580. break_both_connections(bebob);
  581. err = -ETIMEDOUT;
  582. goto end;
  583. }
  584. }
  585. /* start slave if needed */
  586. if (!amdtp_stream_running(&bebob->tx_stream)) {
  587. err = start_stream(bebob, &bebob->tx_stream, rate);
  588. if (err < 0) {
  589. dev_err(&bebob->unit->device,
  590. "fail to run AMDTP slave stream:%d\n", err);
  591. amdtp_stream_stop(&bebob->rx_stream);
  592. break_both_connections(bebob);
  593. goto end;
  594. }
  595. /* wait first callback */
  596. if (!amdtp_stream_wait_callback(&bebob->tx_stream,
  597. CALLBACK_TIMEOUT)) {
  598. amdtp_stream_stop(&bebob->tx_stream);
  599. amdtp_stream_stop(&bebob->rx_stream);
  600. break_both_connections(bebob);
  601. err = -ETIMEDOUT;
  602. }
  603. }
  604. end:
  605. return err;
  606. }
  607. void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
  608. {
  609. if (bebob->substreams_counter == 0) {
  610. amdtp_stream_pcm_abort(&bebob->rx_stream);
  611. amdtp_stream_stop(&bebob->rx_stream);
  612. amdtp_stream_pcm_abort(&bebob->tx_stream);
  613. amdtp_stream_stop(&bebob->tx_stream);
  614. break_both_connections(bebob);
  615. }
  616. }
  617. /*
  618. * This function should be called before starting streams or after stopping
  619. * streams.
  620. */
  621. void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
  622. {
  623. amdtp_stream_destroy(&bebob->rx_stream);
  624. amdtp_stream_destroy(&bebob->tx_stream);
  625. destroy_both_connections(bebob);
  626. }
  627. /*
  628. * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
  629. * in Additional AVC commands (Nov 2003, BridgeCo)
  630. * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
  631. */
  632. static int
  633. parse_stream_formation(u8 *buf, unsigned int len,
  634. struct snd_bebob_stream_formation *formation)
  635. {
  636. unsigned int i, e, channels, format;
  637. /*
  638. * this module can support a hierarchy combination that:
  639. * Root: Audio and Music (0x90)
  640. * Level 1: AM824 Compound (0x40)
  641. */
  642. if ((buf[0] != 0x90) || (buf[1] != 0x40))
  643. return -ENOSYS;
  644. /* check sampling rate */
  645. for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
  646. if (buf[2] == bridgeco_freq_table[i])
  647. break;
  648. }
  649. if (i == ARRAY_SIZE(bridgeco_freq_table))
  650. return -ENOSYS;
  651. /* Avoid double count by different entries for the same rate. */
  652. memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
  653. for (e = 0; e < buf[4]; e++) {
  654. channels = buf[5 + e * 2];
  655. format = buf[6 + e * 2];
  656. switch (format) {
  657. /* IEC 60958 Conformant, currently handled as MBLA */
  658. case 0x00:
  659. /* Multi bit linear audio */
  660. case 0x06: /* Raw */
  661. formation[i].pcm += channels;
  662. break;
  663. /* MIDI Conformant */
  664. case 0x0d:
  665. formation[i].midi += channels;
  666. break;
  667. /* IEC 61937-3 to 7 */
  668. case 0x01:
  669. case 0x02:
  670. case 0x03:
  671. case 0x04:
  672. case 0x05:
  673. /* Multi bit linear audio */
  674. case 0x07: /* DVD-Audio */
  675. case 0x0c: /* High Precision */
  676. /* One Bit Audio */
  677. case 0x08: /* (Plain) Raw */
  678. case 0x09: /* (Plain) SACD */
  679. case 0x0a: /* (Encoded) Raw */
  680. case 0x0b: /* (Encoded) SACD */
  681. /* Synchronization Stream (Stereo Raw audio) */
  682. case 0x40:
  683. /* Don't care */
  684. case 0xff:
  685. default:
  686. return -ENOSYS; /* not supported */
  687. }
  688. }
  689. if (formation[i].pcm > AM824_MAX_CHANNELS_FOR_PCM ||
  690. formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI)
  691. return -ENOSYS;
  692. return 0;
  693. }
  694. static int
  695. fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
  696. unsigned short pid)
  697. {
  698. u8 *buf;
  699. struct snd_bebob_stream_formation *formations;
  700. unsigned int len, eid;
  701. u8 addr[AVC_BRIDGECO_ADDR_BYTES];
  702. int err;
  703. buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
  704. if (buf == NULL)
  705. return -ENOMEM;
  706. if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
  707. formations = bebob->rx_stream_formations;
  708. else
  709. formations = bebob->tx_stream_formations;
  710. for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
  711. len = FORMAT_MAXIMUM_LENGTH;
  712. avc_bridgeco_fill_unit_addr(addr, dir,
  713. AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
  714. err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
  715. &len, eid);
  716. /* No entries remained. */
  717. if (err == -EINVAL && eid > 0) {
  718. err = 0;
  719. break;
  720. } else if (err < 0) {
  721. dev_err(&bebob->unit->device,
  722. "fail to get stream format %d for isoc %s plug %d:%d\n",
  723. eid,
  724. (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
  725. "out",
  726. pid, err);
  727. break;
  728. }
  729. err = parse_stream_formation(buf, len, formations);
  730. if (err < 0)
  731. break;
  732. }
  733. kfree(buf);
  734. return err;
  735. }
  736. static int
  737. seek_msu_sync_input_plug(struct snd_bebob *bebob)
  738. {
  739. u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
  740. unsigned int i;
  741. enum avc_bridgeco_plug_type type;
  742. int err;
  743. /* Get the number of Music Sub Unit for both direction. */
  744. err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
  745. if (err < 0) {
  746. dev_err(&bebob->unit->device,
  747. "fail to get info for MSU in/out plugs: %d\n",
  748. err);
  749. goto end;
  750. }
  751. /* seek destination plugs for 'MSU sync input' */
  752. bebob->sync_input_plug = -1;
  753. for (i = 0; i < plugs[0]; i++) {
  754. avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
  755. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  756. if (err < 0) {
  757. dev_err(&bebob->unit->device,
  758. "fail to get type for MSU in plug %d: %d\n",
  759. i, err);
  760. goto end;
  761. }
  762. if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
  763. bebob->sync_input_plug = i;
  764. break;
  765. }
  766. }
  767. end:
  768. return err;
  769. }
  770. int snd_bebob_stream_discover(struct snd_bebob *bebob)
  771. {
  772. const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
  773. u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
  774. enum avc_bridgeco_plug_type type;
  775. unsigned int i;
  776. int err;
  777. /* the number of plugs for isoc in/out, ext in/out */
  778. err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
  779. if (err < 0) {
  780. dev_err(&bebob->unit->device,
  781. "fail to get info for isoc/external in/out plugs: %d\n",
  782. err);
  783. goto end;
  784. }
  785. /*
  786. * This module supports at least one isoc input plug and one isoc
  787. * output plug.
  788. */
  789. if ((plugs[0] == 0) || (plugs[1] == 0)) {
  790. err = -ENOSYS;
  791. goto end;
  792. }
  793. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
  794. AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  795. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  796. if (err < 0) {
  797. dev_err(&bebob->unit->device,
  798. "fail to get type for isoc in plug 0: %d\n", err);
  799. goto end;
  800. } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
  801. err = -ENOSYS;
  802. goto end;
  803. }
  804. err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
  805. if (err < 0)
  806. goto end;
  807. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
  808. AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  809. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  810. if (err < 0) {
  811. dev_err(&bebob->unit->device,
  812. "fail to get type for isoc out plug 0: %d\n", err);
  813. goto end;
  814. } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
  815. err = -ENOSYS;
  816. goto end;
  817. }
  818. err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
  819. if (err < 0)
  820. goto end;
  821. /* count external input plugs for MIDI */
  822. bebob->midi_input_ports = 0;
  823. for (i = 0; i < plugs[2]; i++) {
  824. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
  825. AVC_BRIDGECO_PLUG_UNIT_EXT, i);
  826. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  827. if (err < 0) {
  828. dev_err(&bebob->unit->device,
  829. "fail to get type for external in plug %d: %d\n",
  830. i, err);
  831. goto end;
  832. } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
  833. bebob->midi_input_ports++;
  834. }
  835. }
  836. /* count external output plugs for MIDI */
  837. bebob->midi_output_ports = 0;
  838. for (i = 0; i < plugs[3]; i++) {
  839. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
  840. AVC_BRIDGECO_PLUG_UNIT_EXT, i);
  841. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  842. if (err < 0) {
  843. dev_err(&bebob->unit->device,
  844. "fail to get type for external out plug %d: %d\n",
  845. i, err);
  846. goto end;
  847. } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
  848. bebob->midi_output_ports++;
  849. }
  850. }
  851. /* for check source of clock later */
  852. if (!clk_spec)
  853. err = seek_msu_sync_input_plug(bebob);
  854. end:
  855. return err;
  856. }
  857. void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
  858. {
  859. bebob->dev_lock_changed = true;
  860. wake_up(&bebob->hwdep_wait);
  861. }
  862. int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
  863. {
  864. int err;
  865. spin_lock_irq(&bebob->lock);
  866. /* user land lock this */
  867. if (bebob->dev_lock_count < 0) {
  868. err = -EBUSY;
  869. goto end;
  870. }
  871. /* this is the first time */
  872. if (bebob->dev_lock_count++ == 0)
  873. snd_bebob_stream_lock_changed(bebob);
  874. err = 0;
  875. end:
  876. spin_unlock_irq(&bebob->lock);
  877. return err;
  878. }
  879. void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
  880. {
  881. spin_lock_irq(&bebob->lock);
  882. if (WARN_ON(bebob->dev_lock_count <= 0))
  883. goto end;
  884. if (--bebob->dev_lock_count == 0)
  885. snd_bebob_stream_lock_changed(bebob);
  886. end:
  887. spin_unlock_irq(&bebob->lock);
  888. }