bebob_stream.c 26 KB

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