bebob_stream.c 25 KB

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