bebob_stream.c 26 KB

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