bebob_stream.c 24 KB

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