bebob_stream.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. /*
  11. * NOTE;
  12. * For BeBoB streams, Both of input and output CMP connection are important.
  13. *
  14. * For most devices, each CMP connection starts to transmit/receive a
  15. * corresponding stream. But for a few devices, both of CMP connection needs
  16. * to start transmitting stream. An example is 'M-Audio Firewire 410'.
  17. */
  18. /* 128 is an arbitrary length but it seems to be enough */
  19. #define FORMAT_MAXIMUM_LENGTH 128
  20. const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES] = {
  21. [0] = 32000,
  22. [1] = 44100,
  23. [2] = 48000,
  24. [3] = 88200,
  25. [4] = 96000,
  26. [5] = 176400,
  27. [6] = 192000,
  28. };
  29. /*
  30. * See: Table 51: Extended Stream Format Info ‘Sampling Frequency’
  31. * in Additional AVC commands (Nov 2003, BridgeCo)
  32. */
  33. static const unsigned int bridgeco_freq_table[] = {
  34. [0] = 0x02,
  35. [1] = 0x03,
  36. [2] = 0x04,
  37. [3] = 0x0a,
  38. [4] = 0x05,
  39. [5] = 0x06,
  40. [6] = 0x07,
  41. };
  42. static unsigned int
  43. get_formation_index(unsigned int rate)
  44. {
  45. unsigned int i;
  46. for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
  47. if (snd_bebob_rate_table[i] == rate)
  48. return i;
  49. }
  50. return -EINVAL;
  51. }
  52. int
  53. snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *curr_rate)
  54. {
  55. unsigned int tx_rate, rx_rate, trials;
  56. int err;
  57. trials = 0;
  58. do {
  59. err = avc_general_get_sig_fmt(bebob->unit, &tx_rate,
  60. AVC_GENERAL_PLUG_DIR_OUT, 0);
  61. } while (err == -EAGAIN && ++trials < 3);
  62. if (err < 0)
  63. goto end;
  64. trials = 0;
  65. do {
  66. err = avc_general_get_sig_fmt(bebob->unit, &rx_rate,
  67. AVC_GENERAL_PLUG_DIR_IN, 0);
  68. } while (err == -EAGAIN && ++trials < 3);
  69. if (err < 0)
  70. goto end;
  71. *curr_rate = rx_rate;
  72. if (rx_rate == tx_rate)
  73. goto end;
  74. /* synchronize receive stream rate to transmit stream rate */
  75. err = avc_general_set_sig_fmt(bebob->unit, rx_rate,
  76. AVC_GENERAL_PLUG_DIR_IN, 0);
  77. end:
  78. return err;
  79. }
  80. int
  81. snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate)
  82. {
  83. int err;
  84. err = avc_general_set_sig_fmt(bebob->unit, rate,
  85. AVC_GENERAL_PLUG_DIR_OUT, 0);
  86. if (err < 0)
  87. goto end;
  88. err = avc_general_set_sig_fmt(bebob->unit, rate,
  89. AVC_GENERAL_PLUG_DIR_IN, 0);
  90. if (err < 0)
  91. goto end;
  92. /*
  93. * Some devices need a bit time for transition.
  94. * 300msec is got by some experiments.
  95. */
  96. msleep(300);
  97. end:
  98. return err;
  99. }
  100. int
  101. snd_bebob_stream_check_internal_clock(struct snd_bebob *bebob, bool *internal)
  102. {
  103. u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
  104. int err = 0;
  105. *internal = false;
  106. /*
  107. * 1.The device don't support to switch source of clock then assumed
  108. * to use internal clock always
  109. */
  110. if (bebob->sync_input_plug < 0) {
  111. *internal = true;
  112. goto end;
  113. }
  114. /*
  115. * 2.The device supports to switch source of clock by an usual way.
  116. * Let's check input for 'Music Sub Unit Sync Input' plug.
  117. */
  118. avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
  119. bebob->sync_input_plug);
  120. err = avc_bridgeco_get_plug_input(bebob->unit, addr, input);
  121. if (err < 0) {
  122. dev_err(&bebob->unit->device,
  123. "fail to get an input for MSU in plug %d: %d\n",
  124. bebob->sync_input_plug, err);
  125. goto end;
  126. }
  127. /*
  128. * If there are no input plugs, all of fields are 0xff.
  129. * Here check the first field. This field is used for direction.
  130. */
  131. if (input[0] == 0xff) {
  132. *internal = true;
  133. goto end;
  134. }
  135. /*
  136. * If source of clock is internal CSR, Music Sub Unit Sync Input is
  137. * a destination of Music Sub Unit Sync Output.
  138. */
  139. *internal = ((input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) &&
  140. (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT) &&
  141. (input[2] == 0x0c) &&
  142. (input[3] == 0x00));
  143. end:
  144. return err;
  145. }
  146. static unsigned int
  147. map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
  148. {
  149. unsigned int sec, sections, ch, channels;
  150. unsigned int pcm, midi, location;
  151. unsigned int stm_pos, sec_loc, pos;
  152. u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
  153. enum avc_bridgeco_plug_dir dir;
  154. int err;
  155. /*
  156. * The length of return value of this command cannot be expected. Here
  157. * use the maximum length of FCP.
  158. */
  159. buf = kzalloc(256, GFP_KERNEL);
  160. if (buf == NULL)
  161. return -ENOMEM;
  162. if (s == &bebob->tx_stream)
  163. dir = AVC_BRIDGECO_PLUG_DIR_OUT;
  164. else
  165. dir = AVC_BRIDGECO_PLUG_DIR_IN;
  166. avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  167. err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
  168. if (err < 0) {
  169. dev_err(&bebob->unit->device,
  170. "fail to get channel position for isoc %s plug 0: %d\n",
  171. (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
  172. err);
  173. goto end;
  174. }
  175. pos = 0;
  176. /* positions in I/O buffer */
  177. pcm = 0;
  178. midi = 0;
  179. /* the number of sections in AMDTP packet */
  180. sections = buf[pos++];
  181. for (sec = 0; sec < sections; sec++) {
  182. /* type of this section */
  183. avc_bridgeco_fill_unit_addr(addr, dir,
  184. AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  185. err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
  186. sec, &type);
  187. if (err < 0) {
  188. dev_err(&bebob->unit->device,
  189. "fail to get section type for isoc %s plug 0: %d\n",
  190. (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
  191. "out",
  192. err);
  193. goto end;
  194. }
  195. /* NoType */
  196. if (type == 0xff) {
  197. err = -ENOSYS;
  198. goto end;
  199. }
  200. /* the number of channels in this section */
  201. channels = buf[pos++];
  202. for (ch = 0; ch < channels; ch++) {
  203. /* position of this channel in AMDTP packet */
  204. stm_pos = buf[pos++] - 1;
  205. /* location of this channel in this section */
  206. sec_loc = buf[pos++] - 1;
  207. switch (type) {
  208. /* for MIDI conformant data channel */
  209. case 0x0a:
  210. /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
  211. if ((midi > 0) && (stm_pos != midi)) {
  212. err = -ENOSYS;
  213. goto end;
  214. }
  215. s->midi_position = stm_pos;
  216. midi = stm_pos;
  217. break;
  218. /* for PCM data channel */
  219. case 0x01: /* Headphone */
  220. case 0x02: /* Microphone */
  221. case 0x03: /* Line */
  222. case 0x04: /* SPDIF */
  223. case 0x05: /* ADAT */
  224. case 0x06: /* TDIF */
  225. case 0x07: /* MADI */
  226. /* for undefined/changeable signal */
  227. case 0x08: /* Analog */
  228. case 0x09: /* Digital */
  229. default:
  230. location = pcm + sec_loc;
  231. if (location >= AMDTP_MAX_CHANNELS_FOR_PCM) {
  232. err = -ENOSYS;
  233. goto end;
  234. }
  235. s->pcm_positions[location] = stm_pos;
  236. break;
  237. }
  238. }
  239. if (type != 0x0a)
  240. pcm += channels;
  241. else
  242. midi += channels;
  243. }
  244. end:
  245. kfree(buf);
  246. return err;
  247. }
  248. static int
  249. init_both_connections(struct snd_bebob *bebob)
  250. {
  251. int err;
  252. err = cmp_connection_init(&bebob->in_conn,
  253. bebob->unit, CMP_INPUT, 0);
  254. if (err < 0)
  255. goto end;
  256. err = cmp_connection_init(&bebob->out_conn,
  257. bebob->unit, CMP_OUTPUT, 0);
  258. if (err < 0)
  259. cmp_connection_destroy(&bebob->in_conn);
  260. end:
  261. return err;
  262. }
  263. static int
  264. check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
  265. {
  266. struct cmp_connection *conn;
  267. bool used;
  268. int err;
  269. if (s == &bebob->tx_stream)
  270. conn = &bebob->out_conn;
  271. else
  272. conn = &bebob->in_conn;
  273. err = cmp_connection_check_used(conn, &used);
  274. if ((err >= 0) && used && !amdtp_stream_running(s)) {
  275. dev_err(&bebob->unit->device,
  276. "Connection established by others: %cPCR[%d]\n",
  277. (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
  278. conn->pcr_index);
  279. err = -EBUSY;
  280. }
  281. return err;
  282. }
  283. static int
  284. make_both_connections(struct snd_bebob *bebob, unsigned int rate)
  285. {
  286. int index, pcm_channels, midi_channels, err;
  287. /* confirm params for both streams */
  288. index = get_formation_index(rate);
  289. pcm_channels = bebob->tx_stream_formations[index].pcm;
  290. midi_channels = bebob->tx_stream_formations[index].midi;
  291. amdtp_stream_set_parameters(&bebob->tx_stream,
  292. rate, pcm_channels, midi_channels * 8);
  293. pcm_channels = bebob->rx_stream_formations[index].pcm;
  294. midi_channels = bebob->rx_stream_formations[index].midi;
  295. amdtp_stream_set_parameters(&bebob->rx_stream,
  296. rate, pcm_channels, midi_channels * 8);
  297. /* establish connections for both streams */
  298. err = cmp_connection_establish(&bebob->out_conn,
  299. amdtp_stream_get_max_payload(&bebob->tx_stream));
  300. if (err < 0)
  301. goto end;
  302. err = cmp_connection_establish(&bebob->in_conn,
  303. amdtp_stream_get_max_payload(&bebob->rx_stream));
  304. if (err < 0)
  305. cmp_connection_break(&bebob->out_conn);
  306. end:
  307. return err;
  308. }
  309. static void
  310. break_both_connections(struct snd_bebob *bebob)
  311. {
  312. cmp_connection_break(&bebob->in_conn);
  313. cmp_connection_break(&bebob->out_conn);
  314. }
  315. static void
  316. destroy_both_connections(struct snd_bebob *bebob)
  317. {
  318. break_both_connections(bebob);
  319. cmp_connection_destroy(&bebob->in_conn);
  320. cmp_connection_destroy(&bebob->out_conn);
  321. }
  322. static int
  323. get_sync_mode(struct snd_bebob *bebob, enum cip_flags *sync_mode)
  324. {
  325. /* currently this module doesn't support SYT-Match mode */
  326. *sync_mode = CIP_SYNC_TO_DEVICE;
  327. return 0;
  328. }
  329. static int
  330. start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
  331. unsigned int rate)
  332. {
  333. struct cmp_connection *conn;
  334. int err = 0;
  335. if (stream == &bebob->rx_stream)
  336. conn = &bebob->in_conn;
  337. else
  338. conn = &bebob->out_conn;
  339. /* channel mapping */
  340. err = map_data_channels(bebob, stream);
  341. if (err < 0)
  342. goto end;
  343. /* start amdtp stream */
  344. err = amdtp_stream_start(stream,
  345. conn->resources.channel,
  346. conn->speed);
  347. end:
  348. return err;
  349. }
  350. int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
  351. {
  352. int err;
  353. err = init_both_connections(bebob);
  354. if (err < 0)
  355. goto end;
  356. err = amdtp_stream_init(&bebob->tx_stream, bebob->unit,
  357. AMDTP_IN_STREAM, CIP_BLOCKING);
  358. if (err < 0) {
  359. amdtp_stream_destroy(&bebob->tx_stream);
  360. destroy_both_connections(bebob);
  361. goto end;
  362. }
  363. err = amdtp_stream_init(&bebob->rx_stream, bebob->unit,
  364. AMDTP_OUT_STREAM, CIP_BLOCKING);
  365. if (err < 0) {
  366. amdtp_stream_destroy(&bebob->tx_stream);
  367. amdtp_stream_destroy(&bebob->rx_stream);
  368. destroy_both_connections(bebob);
  369. }
  370. end:
  371. return err;
  372. }
  373. int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, int rate)
  374. {
  375. struct amdtp_stream *master, *slave;
  376. atomic_t *slave_substreams;
  377. enum cip_flags sync_mode;
  378. unsigned int curr_rate;
  379. int err = 0;
  380. mutex_lock(&bebob->mutex);
  381. /* Need no substreams */
  382. if (atomic_read(&bebob->playback_substreams) == 0 &&
  383. atomic_read(&bebob->capture_substreams) == 0)
  384. goto end;
  385. err = get_sync_mode(bebob, &sync_mode);
  386. if (err < 0)
  387. goto end;
  388. if (sync_mode == CIP_SYNC_TO_DEVICE) {
  389. master = &bebob->tx_stream;
  390. slave = &bebob->rx_stream;
  391. slave_substreams = &bebob->playback_substreams;
  392. } else {
  393. master = &bebob->rx_stream;
  394. slave = &bebob->tx_stream;
  395. slave_substreams = &bebob->capture_substreams;
  396. }
  397. /*
  398. * Considering JACK/FFADO streaming:
  399. * TODO: This can be removed hwdep functionality becomes popular.
  400. */
  401. err = check_connection_used_by_others(bebob, master);
  402. if (err < 0)
  403. goto end;
  404. /* packet queueing error */
  405. if (amdtp_streaming_error(master)) {
  406. amdtp_stream_stop(master);
  407. amdtp_stream_stop(slave);
  408. }
  409. if (amdtp_streaming_error(slave))
  410. amdtp_stream_stop(slave);
  411. /* stop streams if rate is different */
  412. err = snd_bebob_stream_get_rate(bebob, &curr_rate);
  413. if (err < 0) {
  414. dev_err(&bebob->unit->device,
  415. "fail to get sampling rate: %d\n", err);
  416. goto end;
  417. }
  418. if (rate == 0)
  419. rate = curr_rate;
  420. if (rate != curr_rate) {
  421. amdtp_stream_stop(master);
  422. amdtp_stream_stop(slave);
  423. break_both_connections(bebob);
  424. }
  425. /* master should be always running */
  426. if (!amdtp_stream_running(master)) {
  427. amdtp_stream_set_sync(sync_mode, master, slave);
  428. bebob->master = master;
  429. /*
  430. * NOTE:
  431. * If establishing connections at first, Yamaha GO46
  432. * (and maybe Terratec X24) don't generate sound.
  433. */
  434. err = snd_bebob_stream_set_rate(bebob, rate);
  435. if (err < 0) {
  436. dev_err(&bebob->unit->device,
  437. "fail to set sampling rate: %d\n",
  438. err);
  439. goto end;
  440. }
  441. err = make_both_connections(bebob, rate);
  442. if (err < 0)
  443. goto end;
  444. err = start_stream(bebob, master, rate);
  445. if (err < 0) {
  446. dev_err(&bebob->unit->device,
  447. "fail to run AMDTP master stream:%d\n", err);
  448. break_both_connections(bebob);
  449. goto end;
  450. }
  451. /* wait first callback */
  452. if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
  453. amdtp_stream_stop(master);
  454. break_both_connections(bebob);
  455. err = -ETIMEDOUT;
  456. goto end;
  457. }
  458. }
  459. /* start slave if needed */
  460. if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
  461. err = start_stream(bebob, slave, rate);
  462. if (err < 0) {
  463. dev_err(&bebob->unit->device,
  464. "fail to run AMDTP slave stream:%d\n", err);
  465. amdtp_stream_stop(master);
  466. break_both_connections(bebob);
  467. goto end;
  468. }
  469. /* wait first callback */
  470. if (!amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
  471. amdtp_stream_stop(slave);
  472. amdtp_stream_stop(master);
  473. break_both_connections(bebob);
  474. err = -ETIMEDOUT;
  475. }
  476. }
  477. end:
  478. mutex_unlock(&bebob->mutex);
  479. return err;
  480. }
  481. void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
  482. {
  483. struct amdtp_stream *master, *slave;
  484. atomic_t *master_substreams, *slave_substreams;
  485. mutex_lock(&bebob->mutex);
  486. if (bebob->master == &bebob->rx_stream) {
  487. slave = &bebob->tx_stream;
  488. master = &bebob->rx_stream;
  489. slave_substreams = &bebob->capture_substreams;
  490. master_substreams = &bebob->playback_substreams;
  491. } else {
  492. slave = &bebob->rx_stream;
  493. master = &bebob->tx_stream;
  494. slave_substreams = &bebob->playback_substreams;
  495. master_substreams = &bebob->capture_substreams;
  496. }
  497. if (atomic_read(slave_substreams) == 0) {
  498. amdtp_stream_pcm_abort(slave);
  499. amdtp_stream_stop(slave);
  500. if (atomic_read(master_substreams) == 0) {
  501. amdtp_stream_pcm_abort(master);
  502. amdtp_stream_stop(master);
  503. break_both_connections(bebob);
  504. }
  505. }
  506. mutex_unlock(&bebob->mutex);
  507. }
  508. void snd_bebob_stream_update_duplex(struct snd_bebob *bebob)
  509. {
  510. /* vs. XRUN recovery due to discontinuity at bus reset */
  511. mutex_lock(&bebob->mutex);
  512. if ((cmp_connection_update(&bebob->in_conn) < 0) ||
  513. (cmp_connection_update(&bebob->out_conn) < 0)) {
  514. amdtp_stream_pcm_abort(&bebob->rx_stream);
  515. amdtp_stream_pcm_abort(&bebob->tx_stream);
  516. amdtp_stream_stop(&bebob->rx_stream);
  517. amdtp_stream_stop(&bebob->tx_stream);
  518. break_both_connections(bebob);
  519. } else {
  520. amdtp_stream_update(&bebob->rx_stream);
  521. amdtp_stream_update(&bebob->tx_stream);
  522. }
  523. mutex_unlock(&bebob->mutex);
  524. }
  525. void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
  526. {
  527. mutex_lock(&bebob->mutex);
  528. amdtp_stream_pcm_abort(&bebob->rx_stream);
  529. amdtp_stream_pcm_abort(&bebob->tx_stream);
  530. amdtp_stream_stop(&bebob->rx_stream);
  531. amdtp_stream_stop(&bebob->tx_stream);
  532. amdtp_stream_destroy(&bebob->rx_stream);
  533. amdtp_stream_destroy(&bebob->tx_stream);
  534. destroy_both_connections(bebob);
  535. mutex_unlock(&bebob->mutex);
  536. }
  537. /*
  538. * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
  539. * in Additional AVC commands (Nov 2003, BridgeCo)
  540. * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
  541. */
  542. static int
  543. parse_stream_formation(u8 *buf, unsigned int len,
  544. struct snd_bebob_stream_formation *formation)
  545. {
  546. unsigned int i, e, channels, format;
  547. /*
  548. * this module can support a hierarchy combination that:
  549. * Root: Audio and Music (0x90)
  550. * Level 1: AM824 Compound (0x40)
  551. */
  552. if ((buf[0] != 0x90) || (buf[1] != 0x40))
  553. return -ENOSYS;
  554. /* check sampling rate */
  555. for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
  556. if (buf[2] == bridgeco_freq_table[i])
  557. break;
  558. }
  559. if (i == sizeof(bridgeco_freq_table))
  560. return -ENOSYS;
  561. /* Avoid double count by different entries for the same rate. */
  562. memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
  563. for (e = 0; e < buf[4]; e++) {
  564. channels = buf[5 + e * 2];
  565. format = buf[6 + e * 2];
  566. switch (format) {
  567. /* IEC 60958-3, currently handle as MBLA */
  568. case 0x00:
  569. /* Multi bit linear audio */
  570. case 0x06: /* Raw */
  571. formation[i].pcm += channels;
  572. break;
  573. /* MIDI Conformant */
  574. case 0x0d:
  575. formation[i].midi += channels;
  576. break;
  577. /* IEC 61937-3 to 7 */
  578. case 0x01:
  579. case 0x02:
  580. case 0x03:
  581. case 0x04:
  582. case 0x05:
  583. /* Multi bit linear audio */
  584. case 0x07: /* DVD-Audio */
  585. case 0x0c: /* High Precision */
  586. /* One Bit Audio */
  587. case 0x08: /* (Plain) Raw */
  588. case 0x09: /* (Plain) SACD */
  589. case 0x0a: /* (Encoded) Raw */
  590. case 0x0b: /* (Encoded) SACD */
  591. /* Synchronization Stream (Stereo Raw audio) */
  592. case 0x40:
  593. /* Don't care */
  594. case 0xff:
  595. default:
  596. return -ENOSYS; /* not supported */
  597. }
  598. }
  599. if (formation[i].pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
  600. formation[i].midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
  601. return -ENOSYS;
  602. return 0;
  603. }
  604. static int
  605. fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
  606. unsigned short pid)
  607. {
  608. u8 *buf;
  609. struct snd_bebob_stream_formation *formations;
  610. unsigned int len, eid;
  611. u8 addr[AVC_BRIDGECO_ADDR_BYTES];
  612. int err;
  613. buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
  614. if (buf == NULL)
  615. return -ENOMEM;
  616. if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
  617. formations = bebob->rx_stream_formations;
  618. else
  619. formations = bebob->tx_stream_formations;
  620. for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
  621. len = FORMAT_MAXIMUM_LENGTH;
  622. avc_bridgeco_fill_unit_addr(addr, dir,
  623. AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
  624. err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
  625. &len, eid);
  626. /* No entries remained. */
  627. if (err == -EINVAL && eid > 0) {
  628. err = 0;
  629. break;
  630. } else if (err < 0) {
  631. dev_err(&bebob->unit->device,
  632. "fail to get stream format %d for isoc %s plug %d:%d\n",
  633. eid,
  634. (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
  635. "out",
  636. pid, err);
  637. break;
  638. }
  639. err = parse_stream_formation(buf, len, formations);
  640. if (err < 0)
  641. break;
  642. }
  643. kfree(buf);
  644. return err;
  645. }
  646. static int
  647. seek_msu_sync_input_plug(struct snd_bebob *bebob)
  648. {
  649. u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
  650. unsigned int i, type;
  651. int err;
  652. /* Get the number of Music Sub Unit for both direction. */
  653. err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
  654. if (err < 0) {
  655. dev_err(&bebob->unit->device,
  656. "fail to get info for MSU in/out plugs: %d\n",
  657. err);
  658. goto end;
  659. }
  660. /* seek destination plugs for 'MSU sync input' */
  661. bebob->sync_input_plug = -1;
  662. for (i = 0; i < plugs[0]; i++) {
  663. avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
  664. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  665. if (err < 0) {
  666. dev_err(&bebob->unit->device,
  667. "fail to get type for MSU in plug %d: %d\n",
  668. i, err);
  669. goto end;
  670. }
  671. if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
  672. bebob->sync_input_plug = i;
  673. break;
  674. }
  675. }
  676. end:
  677. return err;
  678. }
  679. int snd_bebob_stream_discover(struct snd_bebob *bebob)
  680. {
  681. u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
  682. enum avc_bridgeco_plug_type type;
  683. unsigned int i;
  684. int err;
  685. /* the number of plugs for isoc in/out, ext in/out */
  686. err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
  687. if (err < 0) {
  688. dev_err(&bebob->unit->device,
  689. "fail to get info for isoc/external in/out plugs: %d\n",
  690. err);
  691. goto end;
  692. }
  693. /*
  694. * This module supports at least one isoc input plug and one isoc
  695. * output plug.
  696. */
  697. if ((plugs[0] == 0) || (plugs[1] == 0)) {
  698. err = -ENOSYS;
  699. goto end;
  700. }
  701. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
  702. AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  703. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  704. if (err < 0) {
  705. dev_err(&bebob->unit->device,
  706. "fail to get type for isoc in plug 0: %d\n", err);
  707. goto end;
  708. } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
  709. err = -ENOSYS;
  710. goto end;
  711. }
  712. err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
  713. if (err < 0)
  714. goto end;
  715. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
  716. AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  717. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  718. if (err < 0) {
  719. dev_err(&bebob->unit->device,
  720. "fail to get type for isoc out plug 0: %d\n", err);
  721. goto end;
  722. } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
  723. err = -ENOSYS;
  724. goto end;
  725. }
  726. err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
  727. if (err < 0)
  728. goto end;
  729. /* count external input plugs for MIDI */
  730. bebob->midi_input_ports = 0;
  731. for (i = 0; i < plugs[2]; i++) {
  732. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
  733. AVC_BRIDGECO_PLUG_UNIT_EXT, i);
  734. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  735. if (err < 0) {
  736. dev_err(&bebob->unit->device,
  737. "fail to get type for external in plug %d: %d\n",
  738. i, err);
  739. goto end;
  740. } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
  741. bebob->midi_input_ports++;
  742. }
  743. }
  744. /* count external output plugs for MIDI */
  745. bebob->midi_output_ports = 0;
  746. for (i = 0; i < plugs[3]; i++) {
  747. avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
  748. AVC_BRIDGECO_PLUG_UNIT_EXT, i);
  749. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  750. if (err < 0) {
  751. dev_err(&bebob->unit->device,
  752. "fail to get type for external out plug %d: %d\n",
  753. i, err);
  754. goto end;
  755. } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
  756. bebob->midi_output_ports++;
  757. }
  758. }
  759. /* for check source of clock later */
  760. err = seek_msu_sync_input_plug(bebob);
  761. end:
  762. return err;
  763. }