bebob_stream.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * bebob_stream.c - a part of driver for BeBoB based devices
  4. *
  5. * Copyright (c) 2013-2014 Takashi Sakamoto
  6. */
  7. #include "./bebob.h"
  8. #define READY_TIMEOUT_MS 4000
  9. /*
  10. * NOTE;
  11. * For BeBoB streams, Both of input and output CMP connection are important.
  12. *
  13. * For most devices, each CMP connection starts to transmit/receive a
  14. * corresponding stream. But for a few devices, both of CMP connection needs
  15. * to start transmitting stream. An example is 'M-Audio Firewire 410'.
  16. */
  17. /* 128 is an arbitrary length but it seems to be enough */
  18. #define FORMAT_MAXIMUM_LENGTH 128
  19. const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES] = {
  20. [0] = 32000,
  21. [1] = 44100,
  22. [2] = 48000,
  23. [3] = 88200,
  24. [4] = 96000,
  25. [5] = 176400,
  26. [6] = 192000,
  27. };
  28. /*
  29. * See: Table 51: Extended Stream Format Info ‘Sampling Frequency’
  30. * in Additional AVC commands (Nov 2003, BridgeCo)
  31. */
  32. static const unsigned int bridgeco_freq_table[] = {
  33. [0] = 0x02,
  34. [1] = 0x03,
  35. [2] = 0x04,
  36. [3] = 0x0a,
  37. [4] = 0x05,
  38. [5] = 0x06,
  39. [6] = 0x07,
  40. };
  41. static int
  42. get_formation_index(unsigned int rate, unsigned int *index)
  43. {
  44. unsigned int i;
  45. for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
  46. if (snd_bebob_rate_table[i] == rate) {
  47. *index = i;
  48. return 0;
  49. }
  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. const 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 int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
  227. {
  228. unsigned int sec, sections, ch, channels;
  229. unsigned int pcm, midi, location;
  230. unsigned int stm_pos, sec_loc, pos;
  231. u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
  232. enum avc_bridgeco_plug_dir dir;
  233. int err;
  234. /*
  235. * The length of return value of this command cannot be expected. Here
  236. * use the maximum length of FCP.
  237. */
  238. buf = kzalloc(256, GFP_KERNEL);
  239. if (buf == NULL)
  240. return -ENOMEM;
  241. if (s == &bebob->tx_stream)
  242. dir = AVC_BRIDGECO_PLUG_DIR_OUT;
  243. else
  244. dir = AVC_BRIDGECO_PLUG_DIR_IN;
  245. avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  246. err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
  247. if (err < 0) {
  248. dev_err(&bebob->unit->device,
  249. "fail to get channel position for isoc %s plug 0: %d\n",
  250. (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
  251. err);
  252. goto end;
  253. }
  254. pos = 0;
  255. /* positions in I/O buffer */
  256. pcm = 0;
  257. midi = 0;
  258. /* the number of sections in AMDTP packet */
  259. sections = buf[pos++];
  260. for (sec = 0; sec < sections; sec++) {
  261. /* type of this section */
  262. avc_bridgeco_fill_unit_addr(addr, dir,
  263. AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
  264. err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
  265. sec, &type);
  266. if (err < 0) {
  267. dev_err(&bebob->unit->device,
  268. "fail to get section type for isoc %s plug 0: %d\n",
  269. (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
  270. "out",
  271. err);
  272. goto end;
  273. }
  274. /* NoType */
  275. if (type == 0xff) {
  276. err = -ENOSYS;
  277. goto end;
  278. }
  279. /* the number of channels in this section */
  280. channels = buf[pos++];
  281. for (ch = 0; ch < channels; ch++) {
  282. /* position of this channel in AMDTP packet */
  283. stm_pos = buf[pos++] - 1;
  284. /* location of this channel in this section */
  285. sec_loc = buf[pos++] - 1;
  286. /*
  287. * Basically the number of location is within the
  288. * number of channels in this section. But some models
  289. * of M-Audio don't follow this. Its location for MIDI
  290. * is the position of MIDI channels in AMDTP packet.
  291. */
  292. if (sec_loc >= channels)
  293. sec_loc = ch;
  294. switch (type) {
  295. /* for MIDI conformant data channel */
  296. case 0x0a:
  297. /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
  298. if ((midi > 0) && (stm_pos != midi)) {
  299. err = -ENOSYS;
  300. goto end;
  301. }
  302. amdtp_am824_set_midi_position(s, stm_pos);
  303. midi = stm_pos;
  304. break;
  305. /* for PCM data channel */
  306. case 0x01: /* Headphone */
  307. case 0x02: /* Microphone */
  308. case 0x03: /* Line */
  309. case 0x04: /* SPDIF */
  310. case 0x05: /* ADAT */
  311. case 0x06: /* TDIF */
  312. case 0x07: /* MADI */
  313. /* for undefined/changeable signal */
  314. case 0x08: /* Analog */
  315. case 0x09: /* Digital */
  316. default:
  317. location = pcm + sec_loc;
  318. if (location >= AM824_MAX_CHANNELS_FOR_PCM) {
  319. err = -ENOSYS;
  320. goto end;
  321. }
  322. amdtp_am824_set_pcm_position(s, location,
  323. 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. check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
  338. {
  339. struct cmp_connection *conn;
  340. bool used;
  341. int err;
  342. if (s == &bebob->tx_stream)
  343. conn = &bebob->out_conn;
  344. else
  345. conn = &bebob->in_conn;
  346. err = cmp_connection_check_used(conn, &used);
  347. if ((err >= 0) && used && !amdtp_stream_running(s)) {
  348. dev_err(&bebob->unit->device,
  349. "Connection established by others: %cPCR[%d]\n",
  350. (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
  351. conn->pcr_index);
  352. err = -EBUSY;
  353. }
  354. return err;
  355. }
  356. static void break_both_connections(struct snd_bebob *bebob)
  357. {
  358. cmp_connection_break(&bebob->in_conn);
  359. cmp_connection_break(&bebob->out_conn);
  360. }
  361. static int start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream)
  362. {
  363. struct cmp_connection *conn;
  364. int err = 0;
  365. if (stream == &bebob->rx_stream)
  366. conn = &bebob->in_conn;
  367. else
  368. conn = &bebob->out_conn;
  369. // channel mapping.
  370. if (bebob->maudio_special_quirk == NULL) {
  371. err = map_data_channels(bebob, stream);
  372. if (err < 0)
  373. return err;
  374. }
  375. err = cmp_connection_establish(conn);
  376. if (err < 0)
  377. return err;
  378. return amdtp_domain_add_stream(&bebob->domain, stream,
  379. conn->resources.channel, conn->speed);
  380. }
  381. static int init_stream(struct snd_bebob *bebob, struct amdtp_stream *stream)
  382. {
  383. unsigned int flags = CIP_BLOCKING;
  384. enum amdtp_stream_direction dir_stream;
  385. struct cmp_connection *conn;
  386. enum cmp_direction dir_conn;
  387. int err;
  388. if (stream == &bebob->tx_stream) {
  389. dir_stream = AMDTP_IN_STREAM;
  390. conn = &bebob->out_conn;
  391. dir_conn = CMP_OUTPUT;
  392. } else {
  393. dir_stream = AMDTP_OUT_STREAM;
  394. conn = &bebob->in_conn;
  395. dir_conn = CMP_INPUT;
  396. }
  397. if (stream == &bebob->tx_stream) {
  398. if (bebob->quirks & SND_BEBOB_QUIRK_WRONG_DBC)
  399. flags |= CIP_EMPTY_HAS_WRONG_DBC;
  400. }
  401. err = cmp_connection_init(conn, bebob->unit, dir_conn, 0);
  402. if (err < 0)
  403. return err;
  404. err = amdtp_am824_init(stream, bebob->unit, dir_stream, flags);
  405. if (err < 0) {
  406. cmp_connection_destroy(conn);
  407. return err;
  408. }
  409. return 0;
  410. }
  411. static void destroy_stream(struct snd_bebob *bebob, struct amdtp_stream *stream)
  412. {
  413. amdtp_stream_destroy(stream);
  414. if (stream == &bebob->tx_stream)
  415. cmp_connection_destroy(&bebob->out_conn);
  416. else
  417. cmp_connection_destroy(&bebob->in_conn);
  418. }
  419. int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
  420. {
  421. int err;
  422. err = init_stream(bebob, &bebob->tx_stream);
  423. if (err < 0)
  424. return err;
  425. err = init_stream(bebob, &bebob->rx_stream);
  426. if (err < 0) {
  427. destroy_stream(bebob, &bebob->tx_stream);
  428. return err;
  429. }
  430. err = amdtp_domain_init(&bebob->domain);
  431. if (err < 0) {
  432. destroy_stream(bebob, &bebob->tx_stream);
  433. destroy_stream(bebob, &bebob->rx_stream);
  434. }
  435. return err;
  436. }
  437. static int keep_resources(struct snd_bebob *bebob, struct amdtp_stream *stream,
  438. unsigned int rate, unsigned int index)
  439. {
  440. unsigned int pcm_channels;
  441. unsigned int midi_ports;
  442. struct cmp_connection *conn;
  443. int err;
  444. if (stream == &bebob->tx_stream) {
  445. pcm_channels = bebob->tx_stream_formations[index].pcm;
  446. midi_ports = bebob->midi_input_ports;
  447. conn = &bebob->out_conn;
  448. } else {
  449. pcm_channels = bebob->rx_stream_formations[index].pcm;
  450. midi_ports = bebob->midi_output_ports;
  451. conn = &bebob->in_conn;
  452. }
  453. err = amdtp_am824_set_parameters(stream, rate, pcm_channels, midi_ports, false);
  454. if (err < 0)
  455. return err;
  456. return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
  457. }
  458. int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate,
  459. unsigned int frames_per_period,
  460. unsigned int frames_per_buffer)
  461. {
  462. unsigned int curr_rate;
  463. int err;
  464. // Considering JACK/FFADO streaming:
  465. // TODO: This can be removed hwdep functionality becomes popular.
  466. err = check_connection_used_by_others(bebob, &bebob->rx_stream);
  467. if (err < 0)
  468. return err;
  469. err = bebob->spec->rate->get(bebob, &curr_rate);
  470. if (err < 0)
  471. return err;
  472. if (rate == 0)
  473. rate = curr_rate;
  474. if (curr_rate != rate) {
  475. amdtp_domain_stop(&bebob->domain);
  476. break_both_connections(bebob);
  477. cmp_connection_release(&bebob->out_conn);
  478. cmp_connection_release(&bebob->in_conn);
  479. }
  480. if (bebob->substreams_counter == 0 || curr_rate != rate) {
  481. unsigned int index;
  482. // NOTE:
  483. // If establishing connections at first, Yamaha GO46
  484. // (and maybe Terratec X24) don't generate sound.
  485. //
  486. // For firmware customized by M-Audio, refer to next NOTE.
  487. err = bebob->spec->rate->set(bebob, rate);
  488. if (err < 0) {
  489. dev_err(&bebob->unit->device,
  490. "fail to set sampling rate: %d\n",
  491. err);
  492. return err;
  493. }
  494. err = get_formation_index(rate, &index);
  495. if (err < 0)
  496. return err;
  497. err = keep_resources(bebob, &bebob->tx_stream, rate, index);
  498. if (err < 0)
  499. return err;
  500. err = keep_resources(bebob, &bebob->rx_stream, rate, index);
  501. if (err < 0) {
  502. cmp_connection_release(&bebob->out_conn);
  503. return err;
  504. }
  505. err = amdtp_domain_set_events_per_period(&bebob->domain,
  506. frames_per_period, frames_per_buffer);
  507. if (err < 0) {
  508. cmp_connection_release(&bebob->out_conn);
  509. cmp_connection_release(&bebob->in_conn);
  510. return err;
  511. }
  512. }
  513. return 0;
  514. }
  515. int snd_bebob_stream_start_duplex(struct snd_bebob *bebob)
  516. {
  517. int err;
  518. // Need no substreams.
  519. if (bebob->substreams_counter == 0)
  520. return -EIO;
  521. // packet queueing error or detecting discontinuity
  522. if (amdtp_streaming_error(&bebob->rx_stream) ||
  523. amdtp_streaming_error(&bebob->tx_stream)) {
  524. amdtp_domain_stop(&bebob->domain);
  525. break_both_connections(bebob);
  526. }
  527. if (!amdtp_stream_running(&bebob->rx_stream)) {
  528. enum snd_bebob_clock_type src;
  529. unsigned int curr_rate;
  530. unsigned int tx_init_skip_cycles;
  531. if (bebob->maudio_special_quirk) {
  532. err = bebob->spec->rate->get(bebob, &curr_rate);
  533. if (err < 0)
  534. return err;
  535. }
  536. err = snd_bebob_stream_get_clock_src(bebob, &src);
  537. if (err < 0)
  538. return err;
  539. err = start_stream(bebob, &bebob->rx_stream);
  540. if (err < 0)
  541. goto error;
  542. err = start_stream(bebob, &bebob->tx_stream);
  543. if (err < 0)
  544. goto error;
  545. if (!(bebob->quirks & SND_BEBOB_QUIRK_INITIAL_DISCONTINUOUS_DBC))
  546. tx_init_skip_cycles = 0;
  547. else
  548. tx_init_skip_cycles = 16000;
  549. // MEMO: Some devices start packet transmission long enough after establishment of
  550. // CMP connection. In the early stage of packet streaming, any device transfers
  551. // NODATA packets. After several hundred cycles, it begins to multiplex event into
  552. // the packet with adequate value of syt field in CIP header. Some devices are
  553. // strictly to generate any discontinuity in the sequence of tx packet when they
  554. // receives inadequate sequence of value in syt field of CIP header. In the case,
  555. // the request to break CMP connection is often corrupted, then any transaction
  556. // results in unrecoverable error, sometimes generate bus-reset.
  557. err = amdtp_domain_start(&bebob->domain, tx_init_skip_cycles, true, false);
  558. if (err < 0)
  559. goto error;
  560. // NOTE:
  561. // The firmware customized by M-Audio uses these commands to
  562. // start transmitting stream. This is not usual way.
  563. if (bebob->maudio_special_quirk) {
  564. err = bebob->spec->rate->set(bebob, curr_rate);
  565. if (err < 0) {
  566. dev_err(&bebob->unit->device,
  567. "fail to ensure sampling rate: %d\n",
  568. err);
  569. goto error;
  570. }
  571. }
  572. // Some devices postpone start of transmission mostly for 1 sec after receives
  573. // packets firstly.
  574. if (!amdtp_domain_wait_ready(&bebob->domain, READY_TIMEOUT_MS)) {
  575. err = -ETIMEDOUT;
  576. goto error;
  577. }
  578. }
  579. return 0;
  580. error:
  581. amdtp_domain_stop(&bebob->domain);
  582. break_both_connections(bebob);
  583. return err;
  584. }
  585. void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
  586. {
  587. if (bebob->substreams_counter == 0) {
  588. amdtp_domain_stop(&bebob->domain);
  589. break_both_connections(bebob);
  590. cmp_connection_release(&bebob->out_conn);
  591. cmp_connection_release(&bebob->in_conn);
  592. }
  593. }
  594. /*
  595. * This function should be called before starting streams or after stopping
  596. * streams.
  597. */
  598. void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
  599. {
  600. amdtp_domain_destroy(&bebob->domain);
  601. destroy_stream(bebob, &bebob->tx_stream);
  602. destroy_stream(bebob, &bebob->rx_stream);
  603. }
  604. /*
  605. * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
  606. * in Additional AVC commands (Nov 2003, BridgeCo)
  607. * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
  608. */
  609. static int
  610. parse_stream_formation(u8 *buf, unsigned int len,
  611. struct snd_bebob_stream_formation *formation)
  612. {
  613. unsigned int i, e, channels, format;
  614. /*
  615. * this module can support a hierarchy combination that:
  616. * Root: Audio and Music (0x90)
  617. * Level 1: AM824 Compound (0x40)
  618. */
  619. if ((buf[0] != 0x90) || (buf[1] != 0x40))
  620. return -ENOSYS;
  621. /* check sampling rate */
  622. for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
  623. if (buf[2] == bridgeco_freq_table[i])
  624. break;
  625. }
  626. if (i == ARRAY_SIZE(bridgeco_freq_table))
  627. return -ENOSYS;
  628. /* Avoid double count by different entries for the same rate. */
  629. memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
  630. for (e = 0; e < buf[4]; e++) {
  631. channels = buf[5 + e * 2];
  632. format = buf[6 + e * 2];
  633. switch (format) {
  634. /* IEC 60958 Conformant, currently handled as MBLA */
  635. case 0x00:
  636. /* Multi bit linear audio */
  637. case 0x06: /* Raw */
  638. formation[i].pcm += channels;
  639. break;
  640. /* MIDI Conformant */
  641. case 0x0d:
  642. formation[i].midi += channels;
  643. break;
  644. /* IEC 61937-3 to 7 */
  645. case 0x01:
  646. case 0x02:
  647. case 0x03:
  648. case 0x04:
  649. case 0x05:
  650. /* Multi bit linear audio */
  651. case 0x07: /* DVD-Audio */
  652. case 0x0c: /* High Precision */
  653. /* One Bit Audio */
  654. case 0x08: /* (Plain) Raw */
  655. case 0x09: /* (Plain) SACD */
  656. case 0x0a: /* (Encoded) Raw */
  657. case 0x0b: /* (Encoded) SACD */
  658. /* Synchronization Stream (Stereo Raw audio) */
  659. case 0x40:
  660. /* Don't care */
  661. case 0xff:
  662. default:
  663. return -ENOSYS; /* not supported */
  664. }
  665. }
  666. if (formation[i].pcm > AM824_MAX_CHANNELS_FOR_PCM ||
  667. formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI)
  668. return -ENOSYS;
  669. return 0;
  670. }
  671. static int fill_stream_formations(struct snd_bebob *bebob, u8 addr[AVC_BRIDGECO_ADDR_BYTES],
  672. enum avc_bridgeco_plug_dir plug_dir, unsigned int plug_id,
  673. struct snd_bebob_stream_formation *formations)
  674. {
  675. enum avc_bridgeco_plug_type plug_type;
  676. u8 *buf;
  677. unsigned int len, eid;
  678. int err;
  679. avc_bridgeco_fill_unit_addr(addr, plug_dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, plug_id);
  680. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &plug_type);
  681. if (err < 0) {
  682. dev_err(&bebob->unit->device,
  683. "Fail to get type for isoc %d plug 0: %d\n", plug_dir, err);
  684. return err;
  685. } else if (plug_type != AVC_BRIDGECO_PLUG_TYPE_ISOC)
  686. return -ENXIO;
  687. buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
  688. if (buf == NULL)
  689. return -ENOMEM;
  690. for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; ++eid) {
  691. avc_bridgeco_fill_unit_addr(addr, plug_dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, plug_id);
  692. len = FORMAT_MAXIMUM_LENGTH;
  693. err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf, &len, eid);
  694. // No entries remained.
  695. if (err == -EINVAL && eid > 0) {
  696. err = 0;
  697. break;
  698. } else if (err < 0) {
  699. dev_err(&bebob->unit->device,
  700. "fail to get stream format %d for isoc %d plug %d:%d\n",
  701. eid, plug_dir, plug_id, err);
  702. break;
  703. }
  704. err = parse_stream_formation(buf, len, formations);
  705. if (err < 0)
  706. break;
  707. }
  708. kfree(buf);
  709. return err;
  710. }
  711. static int detect_midi_ports(struct snd_bebob *bebob,
  712. const struct snd_bebob_stream_formation *formats,
  713. u8 addr[AVC_BRIDGECO_ADDR_BYTES], enum avc_bridgeco_plug_dir plug_dir,
  714. unsigned int plug_count, unsigned int *midi_ports)
  715. {
  716. int i;
  717. int err = 0;
  718. *midi_ports = 0;
  719. /// Detect the number of available MIDI ports when packet has MIDI conformant data channel.
  720. for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; ++i) {
  721. if (formats[i].midi > 0)
  722. break;
  723. }
  724. if (i >= SND_BEBOB_STRM_FMT_ENTRIES)
  725. return 0;
  726. for (i = 0; i < plug_count; ++i) {
  727. enum avc_bridgeco_plug_type plug_type;
  728. unsigned int ch_count;
  729. avc_bridgeco_fill_unit_addr(addr, plug_dir, AVC_BRIDGECO_PLUG_UNIT_EXT, i);
  730. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &plug_type);
  731. if (err < 0) {
  732. dev_err(&bebob->unit->device,
  733. "fail to get type for external %d plug %d: %d\n",
  734. plug_dir, i, err);
  735. break;
  736. } else if (plug_type != AVC_BRIDGECO_PLUG_TYPE_MIDI) {
  737. continue;
  738. }
  739. err = avc_bridgeco_get_plug_ch_count(bebob->unit, addr, &ch_count);
  740. if (err < 0)
  741. break;
  742. // Yamaha GO44, GO46, Terratec Phase 24, Phase x24 reports 0 for the number of
  743. // channels in external output plug 3 (MIDI type) even if it has a pair of physical
  744. // MIDI jacks. As a workaround, assume it as one.
  745. if (ch_count == 0)
  746. ch_count = 1;
  747. *midi_ports += ch_count;
  748. }
  749. return err;
  750. }
  751. static int
  752. seek_msu_sync_input_plug(struct snd_bebob *bebob)
  753. {
  754. u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
  755. unsigned int i;
  756. enum avc_bridgeco_plug_type type;
  757. int err;
  758. /* Get the number of Music Sub Unit for both direction. */
  759. err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
  760. if (err < 0) {
  761. dev_err(&bebob->unit->device,
  762. "fail to get info for MSU in/out plugs: %d\n",
  763. err);
  764. goto end;
  765. }
  766. /* seek destination plugs for 'MSU sync input' */
  767. bebob->sync_input_plug = -1;
  768. for (i = 0; i < plugs[0]; i++) {
  769. avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
  770. err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
  771. if (err < 0) {
  772. dev_err(&bebob->unit->device,
  773. "fail to get type for MSU in plug %d: %d\n",
  774. i, err);
  775. goto end;
  776. }
  777. if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
  778. bebob->sync_input_plug = i;
  779. break;
  780. }
  781. }
  782. end:
  783. return err;
  784. }
  785. int snd_bebob_stream_discover(struct snd_bebob *bebob)
  786. {
  787. const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
  788. u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
  789. int err;
  790. /* the number of plugs for isoc in/out, ext in/out */
  791. err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
  792. if (err < 0) {
  793. dev_err(&bebob->unit->device,
  794. "fail to get info for isoc/external in/out plugs: %d\n",
  795. err);
  796. goto end;
  797. }
  798. /*
  799. * This module supports at least one isoc input plug and one isoc
  800. * output plug.
  801. */
  802. if ((plugs[0] == 0) || (plugs[1] == 0)) {
  803. err = -ENOSYS;
  804. goto end;
  805. }
  806. err = fill_stream_formations(bebob, addr, AVC_BRIDGECO_PLUG_DIR_IN, 0,
  807. bebob->rx_stream_formations);
  808. if (err < 0)
  809. goto end;
  810. err = fill_stream_formations(bebob, addr, AVC_BRIDGECO_PLUG_DIR_OUT, 0,
  811. bebob->tx_stream_formations);
  812. if (err < 0)
  813. goto end;
  814. err = detect_midi_ports(bebob, bebob->tx_stream_formations, addr, AVC_BRIDGECO_PLUG_DIR_IN,
  815. plugs[2], &bebob->midi_input_ports);
  816. if (err < 0)
  817. goto end;
  818. err = detect_midi_ports(bebob, bebob->rx_stream_formations, addr, AVC_BRIDGECO_PLUG_DIR_OUT,
  819. plugs[3], &bebob->midi_output_ports);
  820. if (err < 0)
  821. goto end;
  822. /* for check source of clock later */
  823. if (!clk_spec)
  824. err = seek_msu_sync_input_plug(bebob);
  825. end:
  826. return err;
  827. }
  828. void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
  829. {
  830. bebob->dev_lock_changed = true;
  831. wake_up(&bebob->hwdep_wait);
  832. }
  833. int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
  834. {
  835. int err;
  836. spin_lock_irq(&bebob->lock);
  837. /* user land lock this */
  838. if (bebob->dev_lock_count < 0) {
  839. err = -EBUSY;
  840. goto end;
  841. }
  842. /* this is the first time */
  843. if (bebob->dev_lock_count++ == 0)
  844. snd_bebob_stream_lock_changed(bebob);
  845. err = 0;
  846. end:
  847. spin_unlock_irq(&bebob->lock);
  848. return err;
  849. }
  850. void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
  851. {
  852. spin_lock_irq(&bebob->lock);
  853. if (WARN_ON(bebob->dev_lock_count <= 0))
  854. goto end;
  855. if (--bebob->dev_lock_count == 0)
  856. snd_bebob_stream_lock_changed(bebob);
  857. end:
  858. spin_unlock_irq(&bebob->lock);
  859. }