bebob_stream.c 25 KB

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