oxfw-stream.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * oxfw_stream.c - a part of driver for OXFW970/971 based devices
  3. *
  4. * Copyright (c) 2014 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "oxfw.h"
  9. #include <linux/delay.h>
  10. #define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512
  11. #define CALLBACK_TIMEOUT 200
  12. /*
  13. * According to datasheet of Oxford Semiconductor:
  14. * OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O
  15. * OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O
  16. */
  17. static const unsigned int oxfw_rate_table[] = {
  18. [0] = 32000,
  19. [1] = 44100,
  20. [2] = 48000,
  21. [3] = 88200,
  22. [4] = 96000,
  23. [5] = 192000,
  24. };
  25. /*
  26. * See Table 5.7 – Sampling frequency for Multi-bit Audio
  27. * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
  28. */
  29. static const unsigned int avc_stream_rate_table[] = {
  30. [0] = 0x02,
  31. [1] = 0x03,
  32. [2] = 0x04,
  33. [3] = 0x0a,
  34. [4] = 0x05,
  35. [5] = 0x07,
  36. };
  37. static int set_rate(struct snd_oxfw *oxfw, unsigned int rate)
  38. {
  39. int err;
  40. err = avc_general_set_sig_fmt(oxfw->unit, rate,
  41. AVC_GENERAL_PLUG_DIR_IN, 0);
  42. if (err < 0)
  43. goto end;
  44. if (oxfw->has_output)
  45. err = avc_general_set_sig_fmt(oxfw->unit, rate,
  46. AVC_GENERAL_PLUG_DIR_OUT, 0);
  47. end:
  48. return err;
  49. }
  50. static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s,
  51. unsigned int rate, unsigned int pcm_channels)
  52. {
  53. u8 **formats;
  54. struct snd_oxfw_stream_formation formation;
  55. enum avc_general_plug_dir dir;
  56. unsigned int len;
  57. int i, err;
  58. if (s == &oxfw->tx_stream) {
  59. formats = oxfw->tx_stream_formats;
  60. dir = AVC_GENERAL_PLUG_DIR_OUT;
  61. } else {
  62. formats = oxfw->rx_stream_formats;
  63. dir = AVC_GENERAL_PLUG_DIR_IN;
  64. }
  65. /* Seek stream format for requirements. */
  66. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  67. err = snd_oxfw_stream_parse_format(formats[i], &formation);
  68. if (err < 0)
  69. return err;
  70. if ((formation.rate == rate) && (formation.pcm == pcm_channels))
  71. break;
  72. }
  73. if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
  74. return -EINVAL;
  75. /* If assumed, just change rate. */
  76. if (oxfw->assumed)
  77. return set_rate(oxfw, rate);
  78. /* Calculate format length. */
  79. len = 5 + formats[i][4] * 2;
  80. err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
  81. if (err < 0)
  82. return err;
  83. /* Some requests just after changing format causes freezing. */
  84. msleep(100);
  85. return 0;
  86. }
  87. static void stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
  88. {
  89. amdtp_stream_pcm_abort(stream);
  90. amdtp_stream_stop(stream);
  91. if (stream == &oxfw->tx_stream)
  92. cmp_connection_break(&oxfw->out_conn);
  93. else
  94. cmp_connection_break(&oxfw->in_conn);
  95. }
  96. static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream,
  97. unsigned int rate, unsigned int pcm_channels)
  98. {
  99. u8 **formats;
  100. struct cmp_connection *conn;
  101. struct snd_oxfw_stream_formation formation;
  102. unsigned int i, midi_ports;
  103. int err;
  104. if (stream == &oxfw->rx_stream) {
  105. formats = oxfw->rx_stream_formats;
  106. conn = &oxfw->in_conn;
  107. } else {
  108. formats = oxfw->tx_stream_formats;
  109. conn = &oxfw->out_conn;
  110. }
  111. /* Get stream format */
  112. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  113. if (formats[i] == NULL)
  114. break;
  115. err = snd_oxfw_stream_parse_format(formats[i], &formation);
  116. if (err < 0)
  117. goto end;
  118. if (rate != formation.rate)
  119. continue;
  120. if (pcm_channels == 0 || pcm_channels == formation.pcm)
  121. break;
  122. }
  123. if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) {
  124. err = -EINVAL;
  125. goto end;
  126. }
  127. pcm_channels = formation.pcm;
  128. midi_ports = formation.midi * 8;
  129. /* The stream should have one pcm channels at least */
  130. if (pcm_channels == 0) {
  131. err = -EINVAL;
  132. goto end;
  133. }
  134. err = amdtp_am824_set_parameters(stream, rate, pcm_channels, midi_ports,
  135. false);
  136. if (err < 0)
  137. goto end;
  138. err = cmp_connection_establish(conn,
  139. amdtp_stream_get_max_payload(stream));
  140. if (err < 0)
  141. goto end;
  142. err = amdtp_stream_start(stream,
  143. conn->resources.channel,
  144. conn->speed);
  145. if (err < 0) {
  146. cmp_connection_break(conn);
  147. goto end;
  148. }
  149. /* Wait first packet */
  150. if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
  151. stop_stream(oxfw, stream);
  152. err = -ETIMEDOUT;
  153. }
  154. end:
  155. return err;
  156. }
  157. static int check_connection_used_by_others(struct snd_oxfw *oxfw,
  158. struct amdtp_stream *stream)
  159. {
  160. struct cmp_connection *conn;
  161. bool used;
  162. int err;
  163. if (stream == &oxfw->tx_stream)
  164. conn = &oxfw->out_conn;
  165. else
  166. conn = &oxfw->in_conn;
  167. err = cmp_connection_check_used(conn, &used);
  168. if ((err >= 0) && used && !amdtp_stream_running(stream)) {
  169. dev_err(&oxfw->unit->device,
  170. "Connection established by others: %cPCR[%d]\n",
  171. (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
  172. conn->pcr_index);
  173. err = -EBUSY;
  174. }
  175. return err;
  176. }
  177. int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw,
  178. struct amdtp_stream *stream)
  179. {
  180. struct cmp_connection *conn;
  181. enum cmp_direction c_dir;
  182. enum amdtp_stream_direction s_dir;
  183. int err;
  184. if (stream == &oxfw->tx_stream) {
  185. conn = &oxfw->out_conn;
  186. c_dir = CMP_OUTPUT;
  187. s_dir = AMDTP_IN_STREAM;
  188. } else {
  189. conn = &oxfw->in_conn;
  190. c_dir = CMP_INPUT;
  191. s_dir = AMDTP_OUT_STREAM;
  192. }
  193. err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
  194. if (err < 0)
  195. goto end;
  196. err = amdtp_am824_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
  197. if (err < 0) {
  198. amdtp_stream_destroy(stream);
  199. cmp_connection_destroy(conn);
  200. goto end;
  201. }
  202. /*
  203. * OXFW starts to transmit packets with non-zero dbc.
  204. * OXFW postpone transferring packets till handling any asynchronous
  205. * packets. As a result, next isochronous packet includes more data
  206. * blocks than IEC 61883-6 defines.
  207. */
  208. if (stream == &oxfw->tx_stream) {
  209. oxfw->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK |
  210. CIP_JUMBO_PAYLOAD;
  211. if (oxfw->wrong_dbs)
  212. oxfw->tx_stream.flags |= CIP_WRONG_DBS;
  213. }
  214. end:
  215. return err;
  216. }
  217. int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw,
  218. struct amdtp_stream *stream,
  219. unsigned int rate, unsigned int pcm_channels)
  220. {
  221. struct amdtp_stream *opposite;
  222. struct snd_oxfw_stream_formation formation;
  223. enum avc_general_plug_dir dir;
  224. unsigned int substreams, opposite_substreams;
  225. int err = 0;
  226. if (stream == &oxfw->tx_stream) {
  227. substreams = oxfw->capture_substreams;
  228. opposite = &oxfw->rx_stream;
  229. opposite_substreams = oxfw->playback_substreams;
  230. dir = AVC_GENERAL_PLUG_DIR_OUT;
  231. } else {
  232. substreams = oxfw->playback_substreams;
  233. opposite_substreams = oxfw->capture_substreams;
  234. if (oxfw->has_output)
  235. opposite = &oxfw->rx_stream;
  236. else
  237. opposite = NULL;
  238. dir = AVC_GENERAL_PLUG_DIR_IN;
  239. }
  240. if (substreams == 0)
  241. goto end;
  242. /*
  243. * Considering JACK/FFADO streaming:
  244. * TODO: This can be removed hwdep functionality becomes popular.
  245. */
  246. err = check_connection_used_by_others(oxfw, stream);
  247. if (err < 0)
  248. goto end;
  249. /* packet queueing error */
  250. if (amdtp_streaming_error(stream))
  251. stop_stream(oxfw, stream);
  252. err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
  253. if (err < 0)
  254. goto end;
  255. if (rate == 0)
  256. rate = formation.rate;
  257. if (pcm_channels == 0)
  258. pcm_channels = formation.pcm;
  259. if ((formation.rate != rate) || (formation.pcm != pcm_channels)) {
  260. if (opposite != NULL) {
  261. err = check_connection_used_by_others(oxfw, opposite);
  262. if (err < 0)
  263. goto end;
  264. stop_stream(oxfw, opposite);
  265. }
  266. stop_stream(oxfw, stream);
  267. err = set_stream_format(oxfw, stream, rate, pcm_channels);
  268. if (err < 0) {
  269. dev_err(&oxfw->unit->device,
  270. "fail to set stream format: %d\n", err);
  271. goto end;
  272. }
  273. /* Start opposite stream if needed. */
  274. if (opposite && !amdtp_stream_running(opposite) &&
  275. (opposite_substreams > 0)) {
  276. err = start_stream(oxfw, opposite, rate, 0);
  277. if (err < 0) {
  278. dev_err(&oxfw->unit->device,
  279. "fail to restart stream: %d\n", err);
  280. goto end;
  281. }
  282. }
  283. }
  284. /* Start requested stream. */
  285. if (!amdtp_stream_running(stream)) {
  286. err = start_stream(oxfw, stream, rate, pcm_channels);
  287. if (err < 0)
  288. dev_err(&oxfw->unit->device,
  289. "fail to start stream: %d\n", err);
  290. }
  291. end:
  292. return err;
  293. }
  294. void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw,
  295. struct amdtp_stream *stream)
  296. {
  297. if (((stream == &oxfw->tx_stream) && (oxfw->capture_substreams > 0)) ||
  298. ((stream == &oxfw->rx_stream) && (oxfw->playback_substreams > 0)))
  299. return;
  300. stop_stream(oxfw, stream);
  301. }
  302. /*
  303. * This function should be called before starting the stream or after stopping
  304. * the streams.
  305. */
  306. void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw,
  307. struct amdtp_stream *stream)
  308. {
  309. struct cmp_connection *conn;
  310. if (stream == &oxfw->tx_stream)
  311. conn = &oxfw->out_conn;
  312. else
  313. conn = &oxfw->in_conn;
  314. amdtp_stream_destroy(stream);
  315. cmp_connection_destroy(conn);
  316. }
  317. void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw,
  318. struct amdtp_stream *stream)
  319. {
  320. struct cmp_connection *conn;
  321. if (stream == &oxfw->tx_stream)
  322. conn = &oxfw->out_conn;
  323. else
  324. conn = &oxfw->in_conn;
  325. if (cmp_connection_update(conn) < 0)
  326. stop_stream(oxfw, stream);
  327. else
  328. amdtp_stream_update(stream);
  329. }
  330. int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
  331. enum avc_general_plug_dir dir,
  332. struct snd_oxfw_stream_formation *formation)
  333. {
  334. u8 *format;
  335. unsigned int len;
  336. int err;
  337. len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
  338. format = kmalloc(len, GFP_KERNEL);
  339. if (format == NULL)
  340. return -ENOMEM;
  341. err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
  342. if (err < 0)
  343. goto end;
  344. if (len < 3) {
  345. err = -EIO;
  346. goto end;
  347. }
  348. err = snd_oxfw_stream_parse_format(format, formation);
  349. end:
  350. kfree(format);
  351. return err;
  352. }
  353. /*
  354. * See Table 6.16 - AM824 Stream Format
  355. * Figure 6.19 - format_information field for AM824 Compound
  356. * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
  357. * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
  358. */
  359. int snd_oxfw_stream_parse_format(u8 *format,
  360. struct snd_oxfw_stream_formation *formation)
  361. {
  362. unsigned int i, e, channels, type;
  363. memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
  364. /*
  365. * this module can support a hierarchy combination that:
  366. * Root: Audio and Music (0x90)
  367. * Level 1: AM824 Compound (0x40)
  368. */
  369. if ((format[0] != 0x90) || (format[1] != 0x40))
  370. return -ENOSYS;
  371. /* check the sampling rate */
  372. for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
  373. if (format[2] == avc_stream_rate_table[i])
  374. break;
  375. }
  376. if (i == ARRAY_SIZE(avc_stream_rate_table))
  377. return -ENOSYS;
  378. formation->rate = oxfw_rate_table[i];
  379. for (e = 0; e < format[4]; e++) {
  380. channels = format[5 + e * 2];
  381. type = format[6 + e * 2];
  382. switch (type) {
  383. /* IEC 60958 Conformant, currently handled as MBLA */
  384. case 0x00:
  385. /* Multi Bit Linear Audio (Raw) */
  386. case 0x06:
  387. formation->pcm += channels;
  388. break;
  389. /* MIDI Conformant */
  390. case 0x0d:
  391. formation->midi = channels;
  392. break;
  393. /* IEC 61937-3 to 7 */
  394. case 0x01:
  395. case 0x02:
  396. case 0x03:
  397. case 0x04:
  398. case 0x05:
  399. /* Multi Bit Linear Audio */
  400. case 0x07: /* DVD-Audio */
  401. case 0x0c: /* High Precision */
  402. /* One Bit Audio */
  403. case 0x08: /* (Plain) Raw */
  404. case 0x09: /* (Plain) SACD */
  405. case 0x0a: /* (Encoded) Raw */
  406. case 0x0b: /* (Encoded) SACD */
  407. /* SMPTE Time-Code conformant */
  408. case 0x0e:
  409. /* Sample Count */
  410. case 0x0f:
  411. /* Anciliary Data */
  412. case 0x10:
  413. /* Synchronization Stream (Stereo Raw audio) */
  414. case 0x40:
  415. /* Don't care */
  416. case 0xff:
  417. default:
  418. return -ENOSYS; /* not supported */
  419. }
  420. }
  421. if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM ||
  422. formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
  423. return -ENOSYS;
  424. return 0;
  425. }
  426. static int
  427. assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
  428. unsigned int pid, u8 *buf, unsigned int *len,
  429. u8 **formats)
  430. {
  431. struct snd_oxfw_stream_formation formation;
  432. unsigned int i, eid;
  433. int err;
  434. /* get format at current sampling rate */
  435. err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
  436. if (err < 0) {
  437. dev_err(&oxfw->unit->device,
  438. "fail to get current stream format for isoc %s plug %d:%d\n",
  439. (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
  440. pid, err);
  441. goto end;
  442. }
  443. /* parse and set stream format */
  444. eid = 0;
  445. err = snd_oxfw_stream_parse_format(buf, &formation);
  446. if (err < 0)
  447. goto end;
  448. formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
  449. if (formats[eid] == NULL) {
  450. err = -ENOMEM;
  451. goto end;
  452. }
  453. /* apply the format for each available sampling rate */
  454. for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
  455. if (formation.rate == oxfw_rate_table[i])
  456. continue;
  457. err = avc_general_inquiry_sig_fmt(oxfw->unit,
  458. oxfw_rate_table[i],
  459. dir, pid);
  460. if (err < 0)
  461. continue;
  462. eid++;
  463. formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
  464. if (formats[eid] == NULL) {
  465. err = -ENOMEM;
  466. goto end;
  467. }
  468. formats[eid][2] = avc_stream_rate_table[i];
  469. }
  470. err = 0;
  471. oxfw->assumed = true;
  472. end:
  473. return err;
  474. }
  475. static int fill_stream_formats(struct snd_oxfw *oxfw,
  476. enum avc_general_plug_dir dir,
  477. unsigned short pid)
  478. {
  479. u8 *buf, **formats;
  480. unsigned int len, eid = 0;
  481. struct snd_oxfw_stream_formation dummy;
  482. int err;
  483. buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
  484. if (buf == NULL)
  485. return -ENOMEM;
  486. if (dir == AVC_GENERAL_PLUG_DIR_OUT)
  487. formats = oxfw->tx_stream_formats;
  488. else
  489. formats = oxfw->rx_stream_formats;
  490. /* get first entry */
  491. len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
  492. err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
  493. if (err == -ENOSYS) {
  494. /* LIST subfunction is not implemented */
  495. len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
  496. err = assume_stream_formats(oxfw, dir, pid, buf, &len,
  497. formats);
  498. goto end;
  499. } else if (err < 0) {
  500. dev_err(&oxfw->unit->device,
  501. "fail to get stream format %d for isoc %s plug %d:%d\n",
  502. eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
  503. pid, err);
  504. goto end;
  505. }
  506. /* LIST subfunction is implemented */
  507. while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
  508. /* The format is too short. */
  509. if (len < 3) {
  510. err = -EIO;
  511. break;
  512. }
  513. /* parse and set stream format */
  514. err = snd_oxfw_stream_parse_format(buf, &dummy);
  515. if (err < 0)
  516. break;
  517. formats[eid] = kmemdup(buf, len, GFP_KERNEL);
  518. if (formats[eid] == NULL) {
  519. err = -ENOMEM;
  520. break;
  521. }
  522. /* get next entry */
  523. len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
  524. err = avc_stream_get_format_list(oxfw->unit, dir, 0,
  525. buf, &len, ++eid);
  526. /* No entries remained. */
  527. if (err == -EINVAL) {
  528. err = 0;
  529. break;
  530. } else if (err < 0) {
  531. dev_err(&oxfw->unit->device,
  532. "fail to get stream format %d for isoc %s plug %d:%d\n",
  533. eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
  534. "out",
  535. pid, err);
  536. break;
  537. }
  538. }
  539. end:
  540. kfree(buf);
  541. return err;
  542. }
  543. int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
  544. {
  545. u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
  546. struct snd_oxfw_stream_formation formation;
  547. u8 *format;
  548. unsigned int i;
  549. int err;
  550. /* the number of plugs for isoc in/out, ext in/out */
  551. err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
  552. if (err < 0) {
  553. dev_err(&oxfw->unit->device,
  554. "fail to get info for isoc/external in/out plugs: %d\n",
  555. err);
  556. goto end;
  557. } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
  558. err = -ENOSYS;
  559. goto end;
  560. }
  561. /* use oPCR[0] if exists */
  562. if (plugs[1] > 0) {
  563. err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
  564. if (err < 0)
  565. goto end;
  566. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  567. format = oxfw->tx_stream_formats[i];
  568. if (format == NULL)
  569. continue;
  570. err = snd_oxfw_stream_parse_format(format, &formation);
  571. if (err < 0)
  572. continue;
  573. /* Add one MIDI port. */
  574. if (formation.midi > 0)
  575. oxfw->midi_input_ports = 1;
  576. }
  577. oxfw->has_output = true;
  578. }
  579. /* use iPCR[0] if exists */
  580. if (plugs[0] > 0) {
  581. err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
  582. if (err < 0)
  583. goto end;
  584. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  585. format = oxfw->rx_stream_formats[i];
  586. if (format == NULL)
  587. continue;
  588. err = snd_oxfw_stream_parse_format(format, &formation);
  589. if (err < 0)
  590. continue;
  591. /* Add one MIDI port. */
  592. if (formation.midi > 0)
  593. oxfw->midi_output_ports = 1;
  594. }
  595. }
  596. end:
  597. return err;
  598. }
  599. void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
  600. {
  601. oxfw->dev_lock_changed = true;
  602. wake_up(&oxfw->hwdep_wait);
  603. }
  604. int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
  605. {
  606. int err;
  607. spin_lock_irq(&oxfw->lock);
  608. /* user land lock this */
  609. if (oxfw->dev_lock_count < 0) {
  610. err = -EBUSY;
  611. goto end;
  612. }
  613. /* this is the first time */
  614. if (oxfw->dev_lock_count++ == 0)
  615. snd_oxfw_stream_lock_changed(oxfw);
  616. err = 0;
  617. end:
  618. spin_unlock_irq(&oxfw->lock);
  619. return err;
  620. }
  621. void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
  622. {
  623. spin_lock_irq(&oxfw->lock);
  624. if (WARN_ON(oxfw->dev_lock_count <= 0))
  625. goto end;
  626. if (--oxfw->dev_lock_count == 0)
  627. snd_oxfw_stream_lock_changed(oxfw);
  628. end:
  629. spin_unlock_irq(&oxfw->lock);
  630. }