bebob_stream.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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. break_both_connections(bebob);
  359. cmp_connection_destroy(&bebob->in_conn);
  360. cmp_connection_destroy(&bebob->out_conn);
  361. }
  362. static int
  363. get_sync_mode(struct snd_bebob *bebob, enum cip_flags *sync_mode)
  364. {
  365. /* currently this module doesn't support SYT-Match mode */
  366. *sync_mode = CIP_SYNC_TO_DEVICE;
  367. return 0;
  368. }
  369. static int
  370. start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
  371. unsigned int rate)
  372. {
  373. struct cmp_connection *conn;
  374. int err = 0;
  375. if (stream == &bebob->rx_stream)
  376. conn = &bebob->in_conn;
  377. else
  378. conn = &bebob->out_conn;
  379. /* channel mapping */
  380. if (bebob->maudio_special_quirk == NULL) {
  381. err = map_data_channels(bebob, stream);
  382. if (err < 0)
  383. goto end;
  384. }
  385. /* start amdtp stream */
  386. err = amdtp_stream_start(stream,
  387. conn->resources.channel,
  388. conn->speed);
  389. end:
  390. return err;
  391. }
  392. int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
  393. {
  394. int err;
  395. err = init_both_connections(bebob);
  396. if (err < 0)
  397. goto end;
  398. err = amdtp_stream_init(&bebob->tx_stream, bebob->unit,
  399. AMDTP_IN_STREAM, CIP_BLOCKING);
  400. if (err < 0) {
  401. amdtp_stream_destroy(&bebob->tx_stream);
  402. destroy_both_connections(bebob);
  403. goto end;
  404. }
  405. /* See comments in next function */
  406. init_completion(&bebob->bus_reset);
  407. bebob->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
  408. /*
  409. * At high sampling rate, M-Audio special firmware transmits empty
  410. * packet with the value of dbc incremented by 8 but the others are
  411. * valid to IEC 61883-1.
  412. */
  413. if (bebob->maudio_special_quirk)
  414. bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
  415. err = amdtp_stream_init(&bebob->rx_stream, bebob->unit,
  416. AMDTP_OUT_STREAM, CIP_BLOCKING);
  417. if (err < 0) {
  418. amdtp_stream_destroy(&bebob->tx_stream);
  419. amdtp_stream_destroy(&bebob->rx_stream);
  420. destroy_both_connections(bebob);
  421. }
  422. /*
  423. * The firmware for these devices ignore MIDI messages in more than
  424. * first 8 data blocks of an received AMDTP packet.
  425. */
  426. if (bebob->spec == &maudio_fw410_spec ||
  427. bebob->spec == &maudio_special_spec)
  428. bebob->rx_stream.rx_blocks_for_midi = 8;
  429. end:
  430. return err;
  431. }
  432. int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate)
  433. {
  434. struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
  435. struct amdtp_stream *master, *slave;
  436. atomic_t *slave_substreams;
  437. enum cip_flags sync_mode;
  438. unsigned int curr_rate;
  439. bool updated = false;
  440. int err = 0;
  441. /*
  442. * Normal BeBoB firmware has a quirk at bus reset to transmits packets
  443. * with discontinuous value in dbc field.
  444. *
  445. * This 'struct completion' is used to call .update() at first to update
  446. * connections/streams. Next following codes handle streaming error.
  447. */
  448. if (amdtp_streaming_error(&bebob->tx_stream)) {
  449. if (completion_done(&bebob->bus_reset))
  450. reinit_completion(&bebob->bus_reset);
  451. updated = (wait_for_completion_interruptible_timeout(
  452. &bebob->bus_reset,
  453. msecs_to_jiffies(FW_ISO_RESOURCE_DELAY)) > 0);
  454. }
  455. mutex_lock(&bebob->mutex);
  456. /* Need no substreams */
  457. if (atomic_read(&bebob->playback_substreams) == 0 &&
  458. atomic_read(&bebob->capture_substreams) == 0)
  459. goto end;
  460. err = get_sync_mode(bebob, &sync_mode);
  461. if (err < 0)
  462. goto end;
  463. if (sync_mode == CIP_SYNC_TO_DEVICE) {
  464. master = &bebob->tx_stream;
  465. slave = &bebob->rx_stream;
  466. slave_substreams = &bebob->playback_substreams;
  467. } else {
  468. master = &bebob->rx_stream;
  469. slave = &bebob->tx_stream;
  470. slave_substreams = &bebob->capture_substreams;
  471. }
  472. /*
  473. * Considering JACK/FFADO streaming:
  474. * TODO: This can be removed hwdep functionality becomes popular.
  475. */
  476. err = check_connection_used_by_others(bebob, master);
  477. if (err < 0)
  478. goto end;
  479. /*
  480. * packet queueing error or detecting discontinuity
  481. *
  482. * At bus reset, connections should not be broken here. So streams need
  483. * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
  484. */
  485. if (amdtp_streaming_error(master))
  486. amdtp_stream_stop(master);
  487. if (amdtp_streaming_error(slave))
  488. amdtp_stream_stop(slave);
  489. if (!updated &&
  490. !amdtp_stream_running(master) && !amdtp_stream_running(slave))
  491. break_both_connections(bebob);
  492. /* stop streams if rate is different */
  493. err = rate_spec->get(bebob, &curr_rate);
  494. if (err < 0) {
  495. dev_err(&bebob->unit->device,
  496. "fail to get sampling rate: %d\n", err);
  497. goto end;
  498. }
  499. if (rate == 0)
  500. rate = curr_rate;
  501. if (rate != curr_rate) {
  502. amdtp_stream_stop(master);
  503. amdtp_stream_stop(slave);
  504. break_both_connections(bebob);
  505. }
  506. /* master should be always running */
  507. if (!amdtp_stream_running(master)) {
  508. amdtp_stream_set_sync(sync_mode, master, slave);
  509. bebob->master = master;
  510. /*
  511. * NOTE:
  512. * If establishing connections at first, Yamaha GO46
  513. * (and maybe Terratec X24) don't generate sound.
  514. *
  515. * For firmware customized by M-Audio, refer to next NOTE.
  516. */
  517. if (bebob->maudio_special_quirk == NULL) {
  518. err = rate_spec->set(bebob, rate);
  519. if (err < 0) {
  520. dev_err(&bebob->unit->device,
  521. "fail to set sampling rate: %d\n",
  522. err);
  523. goto end;
  524. }
  525. }
  526. err = make_both_connections(bebob, rate);
  527. if (err < 0)
  528. goto end;
  529. err = start_stream(bebob, master, rate);
  530. if (err < 0) {
  531. dev_err(&bebob->unit->device,
  532. "fail to run AMDTP master stream:%d\n", err);
  533. break_both_connections(bebob);
  534. goto end;
  535. }
  536. /*
  537. * NOTE:
  538. * The firmware customized by M-Audio uses these commands to
  539. * start transmitting stream. This is not usual way.
  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 ensure sampling rate: %d\n",
  546. err);
  547. amdtp_stream_stop(master);
  548. break_both_connections(bebob);
  549. goto end;
  550. }
  551. }
  552. /* wait first callback */
  553. if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
  554. amdtp_stream_stop(master);
  555. break_both_connections(bebob);
  556. err = -ETIMEDOUT;
  557. goto end;
  558. }
  559. }
  560. /* start slave if needed */
  561. if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
  562. err = start_stream(bebob, slave, rate);
  563. if (err < 0) {
  564. dev_err(&bebob->unit->device,
  565. "fail to run AMDTP slave stream:%d\n", err);
  566. amdtp_stream_stop(master);
  567. break_both_connections(bebob);
  568. goto end;
  569. }
  570. /* wait first callback */
  571. if (!amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
  572. amdtp_stream_stop(slave);
  573. amdtp_stream_stop(master);
  574. break_both_connections(bebob);
  575. err = -ETIMEDOUT;
  576. }
  577. }
  578. end:
  579. mutex_unlock(&bebob->mutex);
  580. return err;
  581. }
  582. void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
  583. {
  584. struct amdtp_stream *master, *slave;
  585. atomic_t *master_substreams, *slave_substreams;
  586. if (bebob->master == &bebob->rx_stream) {
  587. slave = &bebob->tx_stream;
  588. master = &bebob->rx_stream;
  589. slave_substreams = &bebob->capture_substreams;
  590. master_substreams = &bebob->playback_substreams;
  591. } else {
  592. slave = &bebob->rx_stream;
  593. master = &bebob->tx_stream;
  594. slave_substreams = &bebob->playback_substreams;
  595. master_substreams = &bebob->capture_substreams;
  596. }
  597. mutex_lock(&bebob->mutex);
  598. if (atomic_read(slave_substreams) == 0) {
  599. amdtp_stream_pcm_abort(slave);
  600. amdtp_stream_stop(slave);
  601. if (atomic_read(master_substreams) == 0) {
  602. amdtp_stream_pcm_abort(master);
  603. amdtp_stream_stop(master);
  604. break_both_connections(bebob);
  605. }
  606. }
  607. mutex_unlock(&bebob->mutex);
  608. }
  609. void snd_bebob_stream_update_duplex(struct snd_bebob *bebob)
  610. {
  611. /* vs. XRUN recovery due to discontinuity at bus reset */
  612. mutex_lock(&bebob->mutex);
  613. if ((cmp_connection_update(&bebob->in_conn) < 0) ||
  614. (cmp_connection_update(&bebob->out_conn) < 0)) {
  615. amdtp_stream_pcm_abort(&bebob->rx_stream);
  616. amdtp_stream_pcm_abort(&bebob->tx_stream);
  617. amdtp_stream_stop(&bebob->rx_stream);
  618. amdtp_stream_stop(&bebob->tx_stream);
  619. break_both_connections(bebob);
  620. } else {
  621. amdtp_stream_update(&bebob->rx_stream);
  622. amdtp_stream_update(&bebob->tx_stream);
  623. }
  624. /* wake up stream_start_duplex() */
  625. if (!completion_done(&bebob->bus_reset))
  626. complete_all(&bebob->bus_reset);
  627. mutex_unlock(&bebob->mutex);
  628. }
  629. void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
  630. {
  631. mutex_lock(&bebob->mutex);
  632. amdtp_stream_pcm_abort(&bebob->rx_stream);
  633. amdtp_stream_pcm_abort(&bebob->tx_stream);
  634. amdtp_stream_stop(&bebob->rx_stream);
  635. amdtp_stream_stop(&bebob->tx_stream);
  636. amdtp_stream_destroy(&bebob->rx_stream);
  637. amdtp_stream_destroy(&bebob->tx_stream);
  638. destroy_both_connections(bebob);
  639. mutex_unlock(&bebob->mutex);
  640. }
  641. /*
  642. * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
  643. * in Additional AVC commands (Nov 2003, BridgeCo)
  644. * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
  645. */
  646. static int
  647. parse_stream_formation(u8 *buf, unsigned int len,
  648. struct snd_bebob_stream_formation *formation)
  649. {
  650. unsigned int i, e, channels, format;
  651. /*
  652. * this module can support a hierarchy combination that:
  653. * Root: Audio and Music (0x90)
  654. * Level 1: AM824 Compound (0x40)
  655. */
  656. if ((buf[0] != 0x90) || (buf[1] != 0x40))
  657. return -ENOSYS;
  658. /* check sampling rate */
  659. for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
  660. if (buf[2] == bridgeco_freq_table[i])
  661. break;
  662. }
  663. if (i == ARRAY_SIZE(bridgeco_freq_table))
  664. return -ENOSYS;
  665. /* Avoid double count by different entries for the same rate. */
  666. memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
  667. for (e = 0; e < buf[4]; e++) {
  668. channels = buf[5 + e * 2];
  669. format = buf[6 + e * 2];
  670. switch (format) {
  671. /* IEC 60958 Conformant, currently handled as MBLA */
  672. case 0x00:
  673. /* Multi bit linear audio */
  674. case 0x06: /* Raw */
  675. formation[i].pcm += channels;
  676. break;
  677. /* MIDI Conformant */
  678. case 0x0d:
  679. formation[i].midi += channels;
  680. break;
  681. /* IEC 61937-3 to 7 */
  682. case 0x01:
  683. case 0x02:
  684. case 0x03:
  685. case 0x04:
  686. case 0x05:
  687. /* Multi bit linear audio */
  688. case 0x07: /* DVD-Audio */
  689. case 0x0c: /* High Precision */
  690. /* One Bit Audio */
  691. case 0x08: /* (Plain) Raw */
  692. case 0x09: /* (Plain) SACD */
  693. case 0x0a: /* (Encoded) Raw */
  694. case 0x0b: /* (Encoded) SACD */
  695. /* Synchronization Stream (Stereo Raw audio) */
  696. case 0x40:
  697. /* Don't care */
  698. case 0xff:
  699. default:
  700. return -ENOSYS; /* not supported */
  701. }
  702. }
  703. if (formation[i].pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
  704. formation[i].midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
  705. return -ENOSYS;
  706. return 0;
  707. }
  708. static int
  709. fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
  710. unsigned short pid)
  711. {
  712. u8 *buf;
  713. struct snd_bebob_stream_formation *formations;
  714. unsigned int len, eid;
  715. u8 addr[AVC_BRIDGECO_ADDR_BYTES];
  716. int err;
  717. buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
  718. if (buf == NULL)
  719. return -ENOMEM;
  720. if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
  721. formations = bebob->rx_stream_formations;
  722. else
  723. formations = bebob->tx_stream_formations;
  724. for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
  725. len = FORMAT_MAXIMUM_LENGTH;
  726. avc_bridgeco_fill_unit_addr(addr, dir,
  727. AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
  728. err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
  729. &len, eid);
  730. /* No entries remained. */
  731. if (err == -EINVAL && eid > 0) {
  732. err = 0;
  733. break;
  734. } else if (err < 0) {
  735. dev_err(&bebob->unit->device,
  736. "fail to get stream format %d for isoc %s plug %d:%d\n",
  737. eid,
  738. (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
  739. "out",
  740. pid, err);
  741. break;
  742. }
  743. err = parse_stream_formation(buf, len, formations);
  744. if (err < 0)
  745. break;
  746. }
  747. kfree(buf);
  748. return err;
  749. }
  750. static int
  751. seek_msu_sync_input_plug(struct snd_bebob *bebob)
  752. {
  753. u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
  754. unsigned int i;
  755. enum avc_bridgeco_plug_type type;
  756. int err;
  757. /* Get the number of Music Sub Unit for both direction. */
  758. err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
  759. if (err < 0) {
  760. dev_err(&bebob->unit->device,
  761. "fail to get info for MSU in/out plugs: %d\n",
  762. err);
  763. goto end;
  764. }
  765. /* seek destination plugs for 'MSU sync input' */
  766. bebob->sync_input_plug = -1;
  767. for (i = 0; i < plugs[0]; i++) {
  768. avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
  769. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  770. if (err < 0) {
  771. dev_err(&bebob->unit->device,
  772. "fail to get type for MSU in plug %d: %d\n",
  773. i, err);
  774. goto end;
  775. }
  776. if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
  777. bebob->sync_input_plug = i;
  778. break;
  779. }
  780. }
  781. end:
  782. return err;
  783. }
  784. int snd_bebob_stream_discover(struct snd_bebob *bebob)
  785. {
  786. struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
  787. u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
  788. enum avc_bridgeco_plug_type type;
  789. unsigned int i;
  790. int err;
  791. /* the number of plugs for isoc in/out, ext in/out */
  792. err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
  793. if (err < 0) {
  794. dev_err(&bebob->unit->device,
  795. "fail to get info for isoc/external in/out plugs: %d\n",
  796. err);
  797. goto end;
  798. }
  799. /*
  800. * This module supports at least one isoc input plug and one isoc
  801. * output plug.
  802. */
  803. if ((plugs[0] == 0) || (plugs[1] == 0)) {
  804. err = -ENOSYS;
  805. goto end;
  806. }
  807. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
  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 in 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_IN, 0);
  819. if (err < 0)
  820. goto end;
  821. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
  822. AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  823. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  824. if (err < 0) {
  825. dev_err(&bebob->unit->device,
  826. "fail to get type for isoc out plug 0: %d\n", err);
  827. goto end;
  828. } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
  829. err = -ENOSYS;
  830. goto end;
  831. }
  832. err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
  833. if (err < 0)
  834. goto end;
  835. /* count external input plugs for MIDI */
  836. bebob->midi_input_ports = 0;
  837. for (i = 0; i < plugs[2]; i++) {
  838. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
  839. AVC_BRIDGECO_PLUG_UNIT_EXT, i);
  840. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  841. if (err < 0) {
  842. dev_err(&bebob->unit->device,
  843. "fail to get type for external in plug %d: %d\n",
  844. i, err);
  845. goto end;
  846. } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
  847. bebob->midi_input_ports++;
  848. }
  849. }
  850. /* count external output plugs for MIDI */
  851. bebob->midi_output_ports = 0;
  852. for (i = 0; i < plugs[3]; i++) {
  853. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
  854. AVC_BRIDGECO_PLUG_UNIT_EXT, i);
  855. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  856. if (err < 0) {
  857. dev_err(&bebob->unit->device,
  858. "fail to get type for external out plug %d: %d\n",
  859. i, err);
  860. goto end;
  861. } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
  862. bebob->midi_output_ports++;
  863. }
  864. }
  865. /* for check source of clock later */
  866. if (!clk_spec)
  867. err = seek_msu_sync_input_plug(bebob);
  868. end:
  869. return err;
  870. }
  871. void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
  872. {
  873. bebob->dev_lock_changed = true;
  874. wake_up(&bebob->hwdep_wait);
  875. }
  876. int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
  877. {
  878. int err;
  879. spin_lock_irq(&bebob->lock);
  880. /* user land lock this */
  881. if (bebob->dev_lock_count < 0) {
  882. err = -EBUSY;
  883. goto end;
  884. }
  885. /* this is the first time */
  886. if (bebob->dev_lock_count++ == 0)
  887. snd_bebob_stream_lock_changed(bebob);
  888. err = 0;
  889. end:
  890. spin_unlock_irq(&bebob->lock);
  891. return err;
  892. }
  893. void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
  894. {
  895. spin_lock_irq(&bebob->lock);
  896. if (WARN_ON(bebob->dev_lock_count <= 0))
  897. goto end;
  898. if (--bebob->dev_lock_count == 0)
  899. snd_bebob_stream_lock_changed(bebob);
  900. end:
  901. spin_unlock_irq(&bebob->lock);
  902. }