dice.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. /*
  2. * TC Applied Technologies Digital Interface Communications Engine driver
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Licensed under the terms of the GNU General Public License, version 2.
  6. */
  7. #include <linux/compat.h>
  8. #include <linux/completion.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/firewire.h>
  12. #include <linux/firewire-constants.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/wait.h>
  20. #include <sound/control.h>
  21. #include <sound/core.h>
  22. #include <sound/firewire.h>
  23. #include <sound/hwdep.h>
  24. #include <sound/info.h>
  25. #include <sound/initval.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include "amdtp.h"
  29. #include "iso-resources.h"
  30. #include "lib.h"
  31. #include "dice-interface.h"
  32. struct dice {
  33. struct snd_card *card;
  34. struct fw_unit *unit;
  35. spinlock_t lock;
  36. struct mutex mutex;
  37. unsigned int global_offset;
  38. unsigned int rx_offset;
  39. unsigned int clock_caps;
  40. unsigned int rx_channels[3];
  41. unsigned int rx_midi_ports[3];
  42. struct fw_address_handler notification_handler;
  43. int owner_generation;
  44. int dev_lock_count; /* > 0 driver, < 0 userspace */
  45. bool dev_lock_changed;
  46. bool global_enabled;
  47. struct completion clock_accepted;
  48. wait_queue_head_t hwdep_wait;
  49. u32 notification_bits;
  50. struct fw_iso_resources resources;
  51. struct amdtp_stream stream;
  52. };
  53. MODULE_DESCRIPTION("DICE driver");
  54. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  55. MODULE_LICENSE("GPL v2");
  56. static const unsigned int dice_rates[] = {
  57. /* mode 0 */
  58. [0] = 32000,
  59. [1] = 44100,
  60. [2] = 48000,
  61. /* mode 1 */
  62. [3] = 88200,
  63. [4] = 96000,
  64. /* mode 2 */
  65. [5] = 176400,
  66. [6] = 192000,
  67. };
  68. static unsigned int rate_to_index(unsigned int rate)
  69. {
  70. unsigned int i;
  71. for (i = 0; i < ARRAY_SIZE(dice_rates); ++i)
  72. if (dice_rates[i] == rate)
  73. return i;
  74. return 0;
  75. }
  76. static unsigned int rate_index_to_mode(unsigned int rate_index)
  77. {
  78. return ((int)rate_index - 1) / 2;
  79. }
  80. static void dice_lock_changed(struct dice *dice)
  81. {
  82. dice->dev_lock_changed = true;
  83. wake_up(&dice->hwdep_wait);
  84. }
  85. static int dice_try_lock(struct dice *dice)
  86. {
  87. int err;
  88. spin_lock_irq(&dice->lock);
  89. if (dice->dev_lock_count < 0) {
  90. err = -EBUSY;
  91. goto out;
  92. }
  93. if (dice->dev_lock_count++ == 0)
  94. dice_lock_changed(dice);
  95. err = 0;
  96. out:
  97. spin_unlock_irq(&dice->lock);
  98. return err;
  99. }
  100. static void dice_unlock(struct dice *dice)
  101. {
  102. spin_lock_irq(&dice->lock);
  103. if (WARN_ON(dice->dev_lock_count <= 0))
  104. goto out;
  105. if (--dice->dev_lock_count == 0)
  106. dice_lock_changed(dice);
  107. out:
  108. spin_unlock_irq(&dice->lock);
  109. }
  110. static inline u64 global_address(struct dice *dice, unsigned int offset)
  111. {
  112. return DICE_PRIVATE_SPACE + dice->global_offset + offset;
  113. }
  114. // TODO: rx index
  115. static inline u64 rx_address(struct dice *dice, unsigned int offset)
  116. {
  117. return DICE_PRIVATE_SPACE + dice->rx_offset + offset;
  118. }
  119. static int dice_owner_set(struct dice *dice)
  120. {
  121. struct fw_device *device = fw_parent_device(dice->unit);
  122. __be64 *buffer;
  123. int err, errors = 0;
  124. buffer = kmalloc(2 * 8, GFP_KERNEL);
  125. if (!buffer)
  126. return -ENOMEM;
  127. for (;;) {
  128. buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
  129. buffer[1] = cpu_to_be64(
  130. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  131. dice->notification_handler.offset);
  132. dice->owner_generation = device->generation;
  133. smp_rmb(); /* node_id vs. generation */
  134. err = snd_fw_transaction(dice->unit,
  135. TCODE_LOCK_COMPARE_SWAP,
  136. global_address(dice, GLOBAL_OWNER),
  137. buffer, 2 * 8,
  138. FW_FIXED_GENERATION |
  139. dice->owner_generation);
  140. if (err == 0) {
  141. if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
  142. dev_err(&dice->unit->device,
  143. "device is already in use\n");
  144. err = -EBUSY;
  145. }
  146. break;
  147. }
  148. if (err != -EAGAIN || ++errors >= 3)
  149. break;
  150. msleep(20);
  151. }
  152. kfree(buffer);
  153. return err;
  154. }
  155. static int dice_owner_update(struct dice *dice)
  156. {
  157. struct fw_device *device = fw_parent_device(dice->unit);
  158. __be64 *buffer;
  159. int err;
  160. if (dice->owner_generation == -1)
  161. return 0;
  162. buffer = kmalloc(2 * 8, GFP_KERNEL);
  163. if (!buffer)
  164. return -ENOMEM;
  165. buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
  166. buffer[1] = cpu_to_be64(
  167. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  168. dice->notification_handler.offset);
  169. dice->owner_generation = device->generation;
  170. smp_rmb(); /* node_id vs. generation */
  171. err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  172. global_address(dice, GLOBAL_OWNER),
  173. buffer, 2 * 8,
  174. FW_FIXED_GENERATION | dice->owner_generation);
  175. if (err == 0) {
  176. if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
  177. dev_err(&dice->unit->device,
  178. "device is already in use\n");
  179. err = -EBUSY;
  180. }
  181. } else if (err == -EAGAIN) {
  182. err = 0; /* try again later */
  183. }
  184. kfree(buffer);
  185. if (err < 0)
  186. dice->owner_generation = -1;
  187. return err;
  188. }
  189. static void dice_owner_clear(struct dice *dice)
  190. {
  191. struct fw_device *device = fw_parent_device(dice->unit);
  192. __be64 *buffer;
  193. buffer = kmalloc(2 * 8, GFP_KERNEL);
  194. if (!buffer)
  195. return;
  196. buffer[0] = cpu_to_be64(
  197. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  198. dice->notification_handler.offset);
  199. buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
  200. snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  201. global_address(dice, GLOBAL_OWNER),
  202. buffer, 2 * 8, FW_QUIET |
  203. FW_FIXED_GENERATION | dice->owner_generation);
  204. kfree(buffer);
  205. dice->owner_generation = -1;
  206. }
  207. static int dice_enable_set(struct dice *dice)
  208. {
  209. __be32 value;
  210. int err;
  211. value = cpu_to_be32(1);
  212. err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  213. global_address(dice, GLOBAL_ENABLE),
  214. &value, 4,
  215. FW_FIXED_GENERATION | dice->owner_generation);
  216. if (err < 0)
  217. return err;
  218. dice->global_enabled = true;
  219. return 0;
  220. }
  221. static void dice_enable_clear(struct dice *dice)
  222. {
  223. __be32 value;
  224. if (!dice->global_enabled)
  225. return;
  226. value = 0;
  227. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  228. global_address(dice, GLOBAL_ENABLE),
  229. &value, 4, FW_QUIET |
  230. FW_FIXED_GENERATION | dice->owner_generation);
  231. dice->global_enabled = false;
  232. }
  233. static void dice_notification(struct fw_card *card, struct fw_request *request,
  234. int tcode, int destination, int source,
  235. int generation, unsigned long long offset,
  236. void *data, size_t length, void *callback_data)
  237. {
  238. struct dice *dice = callback_data;
  239. u32 bits;
  240. unsigned long flags;
  241. if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
  242. fw_send_response(card, request, RCODE_TYPE_ERROR);
  243. return;
  244. }
  245. if ((offset & 3) != 0) {
  246. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  247. return;
  248. }
  249. bits = be32_to_cpup(data);
  250. spin_lock_irqsave(&dice->lock, flags);
  251. dice->notification_bits |= bits;
  252. spin_unlock_irqrestore(&dice->lock, flags);
  253. fw_send_response(card, request, RCODE_COMPLETE);
  254. if (bits & NOTIFY_CLOCK_ACCEPTED)
  255. complete(&dice->clock_accepted);
  256. wake_up(&dice->hwdep_wait);
  257. }
  258. static int dice_rate_constraint(struct snd_pcm_hw_params *params,
  259. struct snd_pcm_hw_rule *rule)
  260. {
  261. struct dice *dice = rule->private;
  262. const struct snd_interval *channels =
  263. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  264. struct snd_interval *rate =
  265. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  266. struct snd_interval allowed_rates = {
  267. .min = UINT_MAX, .max = 0, .integer = 1
  268. };
  269. unsigned int i, mode;
  270. for (i = 0; i < ARRAY_SIZE(dice_rates); ++i) {
  271. mode = rate_index_to_mode(i);
  272. if ((dice->clock_caps & (1 << i)) &&
  273. snd_interval_test(channels, dice->rx_channels[mode])) {
  274. allowed_rates.min = min(allowed_rates.min,
  275. dice_rates[i]);
  276. allowed_rates.max = max(allowed_rates.max,
  277. dice_rates[i]);
  278. }
  279. }
  280. return snd_interval_refine(rate, &allowed_rates);
  281. }
  282. static int dice_channels_constraint(struct snd_pcm_hw_params *params,
  283. struct snd_pcm_hw_rule *rule)
  284. {
  285. struct dice *dice = rule->private;
  286. const struct snd_interval *rate =
  287. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
  288. struct snd_interval *channels =
  289. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  290. struct snd_interval allowed_channels = {
  291. .min = UINT_MAX, .max = 0, .integer = 1
  292. };
  293. unsigned int i, mode;
  294. for (i = 0; i < ARRAY_SIZE(dice_rates); ++i)
  295. if ((dice->clock_caps & (1 << i)) &&
  296. snd_interval_test(rate, dice_rates[i])) {
  297. mode = rate_index_to_mode(i);
  298. allowed_channels.min = min(allowed_channels.min,
  299. dice->rx_channels[mode]);
  300. allowed_channels.max = max(allowed_channels.max,
  301. dice->rx_channels[mode]);
  302. }
  303. return snd_interval_refine(channels, &allowed_channels);
  304. }
  305. static int dice_open(struct snd_pcm_substream *substream)
  306. {
  307. static const struct snd_pcm_hardware hardware = {
  308. .info = SNDRV_PCM_INFO_MMAP |
  309. SNDRV_PCM_INFO_MMAP_VALID |
  310. SNDRV_PCM_INFO_BATCH |
  311. SNDRV_PCM_INFO_INTERLEAVED |
  312. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  313. .formats = AMDTP_OUT_PCM_FORMAT_BITS,
  314. .channels_min = UINT_MAX,
  315. .channels_max = 0,
  316. .buffer_bytes_max = 16 * 1024 * 1024,
  317. .period_bytes_min = 1,
  318. .period_bytes_max = UINT_MAX,
  319. .periods_min = 1,
  320. .periods_max = UINT_MAX,
  321. };
  322. struct dice *dice = substream->private_data;
  323. struct snd_pcm_runtime *runtime = substream->runtime;
  324. unsigned int i;
  325. int err;
  326. err = dice_try_lock(dice);
  327. if (err < 0)
  328. goto error;
  329. runtime->hw = hardware;
  330. for (i = 0; i < ARRAY_SIZE(dice_rates); ++i)
  331. if (dice->clock_caps & (1 << i))
  332. runtime->hw.rates |=
  333. snd_pcm_rate_to_rate_bit(dice_rates[i]);
  334. snd_pcm_limit_hw_rates(runtime);
  335. for (i = 0; i < 3; ++i)
  336. if (dice->rx_channels[i]) {
  337. runtime->hw.channels_min = min(runtime->hw.channels_min,
  338. dice->rx_channels[i]);
  339. runtime->hw.channels_max = max(runtime->hw.channels_max,
  340. dice->rx_channels[i]);
  341. }
  342. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  343. dice_rate_constraint, dice,
  344. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  345. if (err < 0)
  346. goto err_lock;
  347. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  348. dice_channels_constraint, dice,
  349. SNDRV_PCM_HW_PARAM_RATE, -1);
  350. if (err < 0)
  351. goto err_lock;
  352. err = amdtp_stream_add_pcm_hw_constraints(&dice->stream, runtime);
  353. if (err < 0)
  354. goto err_lock;
  355. return 0;
  356. err_lock:
  357. dice_unlock(dice);
  358. error:
  359. return err;
  360. }
  361. static int dice_close(struct snd_pcm_substream *substream)
  362. {
  363. struct dice *dice = substream->private_data;
  364. dice_unlock(dice);
  365. return 0;
  366. }
  367. static int dice_stream_start_packets(struct dice *dice)
  368. {
  369. int err;
  370. if (amdtp_stream_running(&dice->stream))
  371. return 0;
  372. err = amdtp_stream_start(&dice->stream, dice->resources.channel,
  373. fw_parent_device(dice->unit)->max_speed);
  374. if (err < 0)
  375. return err;
  376. err = dice_enable_set(dice);
  377. if (err < 0) {
  378. amdtp_stream_stop(&dice->stream);
  379. return err;
  380. }
  381. return 0;
  382. }
  383. static int dice_stream_start(struct dice *dice)
  384. {
  385. __be32 channel;
  386. int err;
  387. if (!dice->resources.allocated) {
  388. err = fw_iso_resources_allocate(&dice->resources,
  389. amdtp_stream_get_max_payload(&dice->stream),
  390. fw_parent_device(dice->unit)->max_speed);
  391. if (err < 0)
  392. goto error;
  393. channel = cpu_to_be32(dice->resources.channel);
  394. err = snd_fw_transaction(dice->unit,
  395. TCODE_WRITE_QUADLET_REQUEST,
  396. rx_address(dice, RX_ISOCHRONOUS),
  397. &channel, 4, 0);
  398. if (err < 0)
  399. goto err_resources;
  400. }
  401. err = dice_stream_start_packets(dice);
  402. if (err < 0)
  403. goto err_rx_channel;
  404. return 0;
  405. err_rx_channel:
  406. channel = cpu_to_be32((u32)-1);
  407. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  408. rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
  409. err_resources:
  410. fw_iso_resources_free(&dice->resources);
  411. error:
  412. return err;
  413. }
  414. static void dice_stream_stop_packets(struct dice *dice)
  415. {
  416. if (amdtp_stream_running(&dice->stream)) {
  417. dice_enable_clear(dice);
  418. amdtp_stream_stop(&dice->stream);
  419. }
  420. }
  421. static void dice_stream_stop(struct dice *dice)
  422. {
  423. __be32 channel;
  424. dice_stream_stop_packets(dice);
  425. if (!dice->resources.allocated)
  426. return;
  427. channel = cpu_to_be32((u32)-1);
  428. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  429. rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
  430. fw_iso_resources_free(&dice->resources);
  431. }
  432. static int dice_change_rate(struct dice *dice, unsigned int clock_rate)
  433. {
  434. __be32 value;
  435. int err;
  436. reinit_completion(&dice->clock_accepted);
  437. value = cpu_to_be32(clock_rate | CLOCK_SOURCE_ARX1);
  438. err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  439. global_address(dice, GLOBAL_CLOCK_SELECT),
  440. &value, 4, 0);
  441. if (err < 0)
  442. return err;
  443. if (!wait_for_completion_timeout(&dice->clock_accepted,
  444. msecs_to_jiffies(100)))
  445. dev_warn(&dice->unit->device, "clock change timed out\n");
  446. return 0;
  447. }
  448. static int dice_hw_params(struct snd_pcm_substream *substream,
  449. struct snd_pcm_hw_params *hw_params)
  450. {
  451. struct dice *dice = substream->private_data;
  452. unsigned int rate_index, mode, rate, channels, i;
  453. int err;
  454. mutex_lock(&dice->mutex);
  455. dice_stream_stop(dice);
  456. mutex_unlock(&dice->mutex);
  457. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  458. params_buffer_bytes(hw_params));
  459. if (err < 0)
  460. return err;
  461. rate = params_rate(hw_params);
  462. rate_index = rate_to_index(rate);
  463. err = dice_change_rate(dice, rate_index << CLOCK_RATE_SHIFT);
  464. if (err < 0)
  465. return err;
  466. /*
  467. * At rates above 96 kHz, pretend that the stream runs at half the
  468. * actual sample rate with twice the number of channels; two samples
  469. * of a channel are stored consecutively in the packet. Requires
  470. * blocking mode and PCM buffer size should be aligned to SYT_INTERVAL.
  471. */
  472. channels = params_channels(hw_params);
  473. if (rate_index > 4) {
  474. if (channels > AMDTP_MAX_CHANNELS_FOR_PCM / 2) {
  475. err = -ENOSYS;
  476. return err;
  477. }
  478. for (i = 0; i < channels; i++) {
  479. dice->stream.pcm_positions[i * 2] = i;
  480. dice->stream.pcm_positions[i * 2 + 1] = i + channels;
  481. }
  482. rate /= 2;
  483. channels *= 2;
  484. }
  485. mode = rate_index_to_mode(rate_index);
  486. amdtp_stream_set_parameters(&dice->stream, rate, channels,
  487. dice->rx_midi_ports[mode]);
  488. amdtp_stream_set_pcm_format(&dice->stream,
  489. params_format(hw_params));
  490. return 0;
  491. }
  492. static int dice_hw_free(struct snd_pcm_substream *substream)
  493. {
  494. struct dice *dice = substream->private_data;
  495. mutex_lock(&dice->mutex);
  496. dice_stream_stop(dice);
  497. mutex_unlock(&dice->mutex);
  498. return snd_pcm_lib_free_vmalloc_buffer(substream);
  499. }
  500. static int dice_prepare(struct snd_pcm_substream *substream)
  501. {
  502. struct dice *dice = substream->private_data;
  503. int err;
  504. mutex_lock(&dice->mutex);
  505. if (amdtp_streaming_error(&dice->stream))
  506. dice_stream_stop_packets(dice);
  507. err = dice_stream_start(dice);
  508. if (err < 0) {
  509. mutex_unlock(&dice->mutex);
  510. return err;
  511. }
  512. mutex_unlock(&dice->mutex);
  513. amdtp_stream_pcm_prepare(&dice->stream);
  514. return 0;
  515. }
  516. static int dice_trigger(struct snd_pcm_substream *substream, int cmd)
  517. {
  518. struct dice *dice = substream->private_data;
  519. struct snd_pcm_substream *pcm;
  520. switch (cmd) {
  521. case SNDRV_PCM_TRIGGER_START:
  522. pcm = substream;
  523. break;
  524. case SNDRV_PCM_TRIGGER_STOP:
  525. pcm = NULL;
  526. break;
  527. default:
  528. return -EINVAL;
  529. }
  530. amdtp_stream_pcm_trigger(&dice->stream, pcm);
  531. return 0;
  532. }
  533. static snd_pcm_uframes_t dice_pointer(struct snd_pcm_substream *substream)
  534. {
  535. struct dice *dice = substream->private_data;
  536. return amdtp_stream_pcm_pointer(&dice->stream);
  537. }
  538. static int dice_create_pcm(struct dice *dice)
  539. {
  540. static struct snd_pcm_ops ops = {
  541. .open = dice_open,
  542. .close = dice_close,
  543. .ioctl = snd_pcm_lib_ioctl,
  544. .hw_params = dice_hw_params,
  545. .hw_free = dice_hw_free,
  546. .prepare = dice_prepare,
  547. .trigger = dice_trigger,
  548. .pointer = dice_pointer,
  549. .page = snd_pcm_lib_get_vmalloc_page,
  550. .mmap = snd_pcm_lib_mmap_vmalloc,
  551. };
  552. struct snd_pcm *pcm;
  553. int err;
  554. err = snd_pcm_new(dice->card, "DICE", 0, 1, 0, &pcm);
  555. if (err < 0)
  556. return err;
  557. pcm->private_data = dice;
  558. strcpy(pcm->name, dice->card->shortname);
  559. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->ops = &ops;
  560. return 0;
  561. }
  562. static long dice_hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
  563. long count, loff_t *offset)
  564. {
  565. struct dice *dice = hwdep->private_data;
  566. DEFINE_WAIT(wait);
  567. union snd_firewire_event event;
  568. spin_lock_irq(&dice->lock);
  569. while (!dice->dev_lock_changed && dice->notification_bits == 0) {
  570. prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
  571. spin_unlock_irq(&dice->lock);
  572. schedule();
  573. finish_wait(&dice->hwdep_wait, &wait);
  574. if (signal_pending(current))
  575. return -ERESTARTSYS;
  576. spin_lock_irq(&dice->lock);
  577. }
  578. memset(&event, 0, sizeof(event));
  579. if (dice->dev_lock_changed) {
  580. event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
  581. event.lock_status.status = dice->dev_lock_count > 0;
  582. dice->dev_lock_changed = false;
  583. count = min(count, (long)sizeof(event.lock_status));
  584. } else {
  585. event.dice_notification.type = SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
  586. event.dice_notification.notification = dice->notification_bits;
  587. dice->notification_bits = 0;
  588. count = min(count, (long)sizeof(event.dice_notification));
  589. }
  590. spin_unlock_irq(&dice->lock);
  591. if (copy_to_user(buf, &event, count))
  592. return -EFAULT;
  593. return count;
  594. }
  595. static unsigned int dice_hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
  596. poll_table *wait)
  597. {
  598. struct dice *dice = hwdep->private_data;
  599. unsigned int events;
  600. poll_wait(file, &dice->hwdep_wait, wait);
  601. spin_lock_irq(&dice->lock);
  602. if (dice->dev_lock_changed || dice->notification_bits != 0)
  603. events = POLLIN | POLLRDNORM;
  604. else
  605. events = 0;
  606. spin_unlock_irq(&dice->lock);
  607. return events;
  608. }
  609. static int dice_hwdep_get_info(struct dice *dice, void __user *arg)
  610. {
  611. struct fw_device *dev = fw_parent_device(dice->unit);
  612. struct snd_firewire_get_info info;
  613. memset(&info, 0, sizeof(info));
  614. info.type = SNDRV_FIREWIRE_TYPE_DICE;
  615. info.card = dev->card->index;
  616. *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
  617. *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
  618. strlcpy(info.device_name, dev_name(&dev->device),
  619. sizeof(info.device_name));
  620. if (copy_to_user(arg, &info, sizeof(info)))
  621. return -EFAULT;
  622. return 0;
  623. }
  624. static int dice_hwdep_lock(struct dice *dice)
  625. {
  626. int err;
  627. spin_lock_irq(&dice->lock);
  628. if (dice->dev_lock_count == 0) {
  629. dice->dev_lock_count = -1;
  630. err = 0;
  631. } else {
  632. err = -EBUSY;
  633. }
  634. spin_unlock_irq(&dice->lock);
  635. return err;
  636. }
  637. static int dice_hwdep_unlock(struct dice *dice)
  638. {
  639. int err;
  640. spin_lock_irq(&dice->lock);
  641. if (dice->dev_lock_count == -1) {
  642. dice->dev_lock_count = 0;
  643. err = 0;
  644. } else {
  645. err = -EBADFD;
  646. }
  647. spin_unlock_irq(&dice->lock);
  648. return err;
  649. }
  650. static int dice_hwdep_release(struct snd_hwdep *hwdep, struct file *file)
  651. {
  652. struct dice *dice = hwdep->private_data;
  653. spin_lock_irq(&dice->lock);
  654. if (dice->dev_lock_count == -1)
  655. dice->dev_lock_count = 0;
  656. spin_unlock_irq(&dice->lock);
  657. return 0;
  658. }
  659. static int dice_hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
  660. unsigned int cmd, unsigned long arg)
  661. {
  662. struct dice *dice = hwdep->private_data;
  663. switch (cmd) {
  664. case SNDRV_FIREWIRE_IOCTL_GET_INFO:
  665. return dice_hwdep_get_info(dice, (void __user *)arg);
  666. case SNDRV_FIREWIRE_IOCTL_LOCK:
  667. return dice_hwdep_lock(dice);
  668. case SNDRV_FIREWIRE_IOCTL_UNLOCK:
  669. return dice_hwdep_unlock(dice);
  670. default:
  671. return -ENOIOCTLCMD;
  672. }
  673. }
  674. #ifdef CONFIG_COMPAT
  675. static int dice_hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
  676. unsigned int cmd, unsigned long arg)
  677. {
  678. return dice_hwdep_ioctl(hwdep, file, cmd,
  679. (unsigned long)compat_ptr(arg));
  680. }
  681. #else
  682. #define dice_hwdep_compat_ioctl NULL
  683. #endif
  684. static int dice_create_hwdep(struct dice *dice)
  685. {
  686. static const struct snd_hwdep_ops ops = {
  687. .read = dice_hwdep_read,
  688. .release = dice_hwdep_release,
  689. .poll = dice_hwdep_poll,
  690. .ioctl = dice_hwdep_ioctl,
  691. .ioctl_compat = dice_hwdep_compat_ioctl,
  692. };
  693. struct snd_hwdep *hwdep;
  694. int err;
  695. err = snd_hwdep_new(dice->card, "DICE", 0, &hwdep);
  696. if (err < 0)
  697. return err;
  698. strcpy(hwdep->name, "DICE");
  699. hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
  700. hwdep->ops = ops;
  701. hwdep->private_data = dice;
  702. hwdep->exclusive = true;
  703. return 0;
  704. }
  705. static int dice_proc_read_mem(struct dice *dice, void *buffer,
  706. unsigned int offset_q, unsigned int quadlets)
  707. {
  708. unsigned int i;
  709. int err;
  710. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  711. DICE_PRIVATE_SPACE + 4 * offset_q,
  712. buffer, 4 * quadlets, 0);
  713. if (err < 0)
  714. return err;
  715. for (i = 0; i < quadlets; ++i)
  716. be32_to_cpus(&((u32 *)buffer)[i]);
  717. return 0;
  718. }
  719. static const char *str_from_array(const char *const strs[], unsigned int count,
  720. unsigned int i)
  721. {
  722. if (i < count)
  723. return strs[i];
  724. else
  725. return "(unknown)";
  726. }
  727. static void dice_proc_fixup_string(char *s, unsigned int size)
  728. {
  729. unsigned int i;
  730. for (i = 0; i < size; i += 4)
  731. cpu_to_le32s((u32 *)(s + i));
  732. for (i = 0; i < size - 2; ++i) {
  733. if (s[i] == '\0')
  734. return;
  735. if (s[i] == '\\' && s[i + 1] == '\\') {
  736. s[i + 2] = '\0';
  737. return;
  738. }
  739. }
  740. s[size - 1] = '\0';
  741. }
  742. static void dice_proc_read(struct snd_info_entry *entry,
  743. struct snd_info_buffer *buffer)
  744. {
  745. static const char *const section_names[5] = {
  746. "global", "tx", "rx", "ext_sync", "unused2"
  747. };
  748. static const char *const clock_sources[] = {
  749. "aes1", "aes2", "aes3", "aes4", "aes", "adat", "tdif",
  750. "wc", "arx1", "arx2", "arx3", "arx4", "internal"
  751. };
  752. static const char *const rates[] = {
  753. "32000", "44100", "48000", "88200", "96000", "176400", "192000",
  754. "any low", "any mid", "any high", "none"
  755. };
  756. struct dice *dice = entry->private_data;
  757. u32 sections[ARRAY_SIZE(section_names) * 2];
  758. struct {
  759. u32 number;
  760. u32 size;
  761. } tx_rx_header;
  762. union {
  763. struct {
  764. u32 owner_hi, owner_lo;
  765. u32 notification;
  766. char nick_name[NICK_NAME_SIZE];
  767. u32 clock_select;
  768. u32 enable;
  769. u32 status;
  770. u32 extended_status;
  771. u32 sample_rate;
  772. u32 version;
  773. u32 clock_caps;
  774. char clock_source_names[CLOCK_SOURCE_NAMES_SIZE];
  775. } global;
  776. struct {
  777. u32 iso;
  778. u32 number_audio;
  779. u32 number_midi;
  780. u32 speed;
  781. char names[TX_NAMES_SIZE];
  782. u32 ac3_caps;
  783. u32 ac3_enable;
  784. } tx;
  785. struct {
  786. u32 iso;
  787. u32 seq_start;
  788. u32 number_audio;
  789. u32 number_midi;
  790. char names[RX_NAMES_SIZE];
  791. u32 ac3_caps;
  792. u32 ac3_enable;
  793. } rx;
  794. struct {
  795. u32 clock_source;
  796. u32 locked;
  797. u32 rate;
  798. u32 adat_user_data;
  799. } ext_sync;
  800. } buf;
  801. unsigned int quadlets, stream, i;
  802. if (dice_proc_read_mem(dice, sections, 0, ARRAY_SIZE(sections)) < 0)
  803. return;
  804. snd_iprintf(buffer, "sections:\n");
  805. for (i = 0; i < ARRAY_SIZE(section_names); ++i)
  806. snd_iprintf(buffer, " %s: offset %u, size %u\n",
  807. section_names[i],
  808. sections[i * 2], sections[i * 2 + 1]);
  809. quadlets = min_t(u32, sections[1], sizeof(buf.global) / 4);
  810. if (dice_proc_read_mem(dice, &buf.global, sections[0], quadlets) < 0)
  811. return;
  812. snd_iprintf(buffer, "global:\n");
  813. snd_iprintf(buffer, " owner: %04x:%04x%08x\n",
  814. buf.global.owner_hi >> 16,
  815. buf.global.owner_hi & 0xffff, buf.global.owner_lo);
  816. snd_iprintf(buffer, " notification: %08x\n", buf.global.notification);
  817. dice_proc_fixup_string(buf.global.nick_name, NICK_NAME_SIZE);
  818. snd_iprintf(buffer, " nick name: %s\n", buf.global.nick_name);
  819. snd_iprintf(buffer, " clock select: %s %s\n",
  820. str_from_array(clock_sources, ARRAY_SIZE(clock_sources),
  821. buf.global.clock_select & CLOCK_SOURCE_MASK),
  822. str_from_array(rates, ARRAY_SIZE(rates),
  823. (buf.global.clock_select & CLOCK_RATE_MASK)
  824. >> CLOCK_RATE_SHIFT));
  825. snd_iprintf(buffer, " enable: %u\n", buf.global.enable);
  826. snd_iprintf(buffer, " status: %slocked %s\n",
  827. buf.global.status & STATUS_SOURCE_LOCKED ? "" : "un",
  828. str_from_array(rates, ARRAY_SIZE(rates),
  829. (buf.global.status &
  830. STATUS_NOMINAL_RATE_MASK)
  831. >> CLOCK_RATE_SHIFT));
  832. snd_iprintf(buffer, " ext status: %08x\n", buf.global.extended_status);
  833. snd_iprintf(buffer, " sample rate: %u\n", buf.global.sample_rate);
  834. snd_iprintf(buffer, " version: %u.%u.%u.%u\n",
  835. (buf.global.version >> 24) & 0xff,
  836. (buf.global.version >> 16) & 0xff,
  837. (buf.global.version >> 8) & 0xff,
  838. (buf.global.version >> 0) & 0xff);
  839. if (quadlets >= 90) {
  840. snd_iprintf(buffer, " clock caps:");
  841. for (i = 0; i <= 6; ++i)
  842. if (buf.global.clock_caps & (1 << i))
  843. snd_iprintf(buffer, " %s", rates[i]);
  844. for (i = 0; i <= 12; ++i)
  845. if (buf.global.clock_caps & (1 << (16 + i)))
  846. snd_iprintf(buffer, " %s", clock_sources[i]);
  847. snd_iprintf(buffer, "\n");
  848. dice_proc_fixup_string(buf.global.clock_source_names,
  849. CLOCK_SOURCE_NAMES_SIZE);
  850. snd_iprintf(buffer, " clock source names: %s\n",
  851. buf.global.clock_source_names);
  852. }
  853. if (dice_proc_read_mem(dice, &tx_rx_header, sections[2], 2) < 0)
  854. return;
  855. quadlets = min_t(u32, tx_rx_header.size, sizeof(buf.tx) / 4);
  856. for (stream = 0; stream < tx_rx_header.number; ++stream) {
  857. if (dice_proc_read_mem(dice, &buf.tx, sections[2] + 2 +
  858. stream * tx_rx_header.size,
  859. quadlets) < 0)
  860. break;
  861. snd_iprintf(buffer, "tx %u:\n", stream);
  862. snd_iprintf(buffer, " iso channel: %d\n", (int)buf.tx.iso);
  863. snd_iprintf(buffer, " audio channels: %u\n",
  864. buf.tx.number_audio);
  865. snd_iprintf(buffer, " midi ports: %u\n", buf.tx.number_midi);
  866. snd_iprintf(buffer, " speed: S%u\n", 100u << buf.tx.speed);
  867. if (quadlets >= 68) {
  868. dice_proc_fixup_string(buf.tx.names, TX_NAMES_SIZE);
  869. snd_iprintf(buffer, " names: %s\n", buf.tx.names);
  870. }
  871. if (quadlets >= 70) {
  872. snd_iprintf(buffer, " ac3 caps: %08x\n",
  873. buf.tx.ac3_caps);
  874. snd_iprintf(buffer, " ac3 enable: %08x\n",
  875. buf.tx.ac3_enable);
  876. }
  877. }
  878. if (dice_proc_read_mem(dice, &tx_rx_header, sections[4], 2) < 0)
  879. return;
  880. quadlets = min_t(u32, tx_rx_header.size, sizeof(buf.rx) / 4);
  881. for (stream = 0; stream < tx_rx_header.number; ++stream) {
  882. if (dice_proc_read_mem(dice, &buf.rx, sections[4] + 2 +
  883. stream * tx_rx_header.size,
  884. quadlets) < 0)
  885. break;
  886. snd_iprintf(buffer, "rx %u:\n", stream);
  887. snd_iprintf(buffer, " iso channel: %d\n", (int)buf.rx.iso);
  888. snd_iprintf(buffer, " sequence start: %u\n", buf.rx.seq_start);
  889. snd_iprintf(buffer, " audio channels: %u\n",
  890. buf.rx.number_audio);
  891. snd_iprintf(buffer, " midi ports: %u\n", buf.rx.number_midi);
  892. if (quadlets >= 68) {
  893. dice_proc_fixup_string(buf.rx.names, RX_NAMES_SIZE);
  894. snd_iprintf(buffer, " names: %s\n", buf.rx.names);
  895. }
  896. if (quadlets >= 70) {
  897. snd_iprintf(buffer, " ac3 caps: %08x\n",
  898. buf.rx.ac3_caps);
  899. snd_iprintf(buffer, " ac3 enable: %08x\n",
  900. buf.rx.ac3_enable);
  901. }
  902. }
  903. quadlets = min_t(u32, sections[7], sizeof(buf.ext_sync) / 4);
  904. if (quadlets >= 4) {
  905. if (dice_proc_read_mem(dice, &buf.ext_sync,
  906. sections[6], 4) < 0)
  907. return;
  908. snd_iprintf(buffer, "ext status:\n");
  909. snd_iprintf(buffer, " clock source: %s\n",
  910. str_from_array(clock_sources,
  911. ARRAY_SIZE(clock_sources),
  912. buf.ext_sync.clock_source));
  913. snd_iprintf(buffer, " locked: %u\n", buf.ext_sync.locked);
  914. snd_iprintf(buffer, " rate: %s\n",
  915. str_from_array(rates, ARRAY_SIZE(rates),
  916. buf.ext_sync.rate));
  917. snd_iprintf(buffer, " adat user data: ");
  918. if (buf.ext_sync.adat_user_data & ADAT_USER_DATA_NO_DATA)
  919. snd_iprintf(buffer, "-\n");
  920. else
  921. snd_iprintf(buffer, "%x\n",
  922. buf.ext_sync.adat_user_data);
  923. }
  924. }
  925. static void dice_create_proc(struct dice *dice)
  926. {
  927. struct snd_info_entry *entry;
  928. if (!snd_card_proc_new(dice->card, "dice", &entry))
  929. snd_info_set_text_ops(entry, dice, dice_proc_read);
  930. }
  931. static void dice_card_free(struct snd_card *card)
  932. {
  933. struct dice *dice = card->private_data;
  934. amdtp_stream_destroy(&dice->stream);
  935. fw_core_remove_address_handler(&dice->notification_handler);
  936. mutex_destroy(&dice->mutex);
  937. }
  938. #define OUI_WEISS 0x001c6a
  939. #define DICE_CATEGORY_ID 0x04
  940. #define WEISS_CATEGORY_ID 0x00
  941. static int dice_interface_check(struct fw_unit *unit)
  942. {
  943. static const int min_values[10] = {
  944. 10, 0x64 / 4,
  945. 10, 0x18 / 4,
  946. 10, 0x18 / 4,
  947. 0, 0,
  948. 0, 0,
  949. };
  950. struct fw_device *device = fw_parent_device(unit);
  951. struct fw_csr_iterator it;
  952. int key, value, vendor = -1, model = -1, err;
  953. unsigned int category, i;
  954. __be32 pointers[ARRAY_SIZE(min_values)];
  955. __be32 tx_data[4];
  956. __be32 version;
  957. /*
  958. * Check that GUID and unit directory are constructed according to DICE
  959. * rules, i.e., that the specifier ID is the GUID's OUI, and that the
  960. * GUID chip ID consists of the 8-bit category ID, the 10-bit product
  961. * ID, and a 22-bit serial number.
  962. */
  963. fw_csr_iterator_init(&it, unit->directory);
  964. while (fw_csr_iterator_next(&it, &key, &value)) {
  965. switch (key) {
  966. case CSR_SPECIFIER_ID:
  967. vendor = value;
  968. break;
  969. case CSR_MODEL:
  970. model = value;
  971. break;
  972. }
  973. }
  974. if (vendor == OUI_WEISS)
  975. category = WEISS_CATEGORY_ID;
  976. else
  977. category = DICE_CATEGORY_ID;
  978. if (device->config_rom[3] != ((vendor << 8) | category) ||
  979. device->config_rom[4] >> 22 != model)
  980. return -ENODEV;
  981. /*
  982. * Check that the sub address spaces exist and are located inside the
  983. * private address space. The minimum values are chosen so that all
  984. * minimally required registers are included.
  985. */
  986. err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
  987. DICE_PRIVATE_SPACE,
  988. pointers, sizeof(pointers), 0);
  989. if (err < 0)
  990. return -ENODEV;
  991. for (i = 0; i < ARRAY_SIZE(pointers); ++i) {
  992. value = be32_to_cpu(pointers[i]);
  993. if (value < min_values[i] || value >= 0x40000)
  994. return -ENODEV;
  995. }
  996. /* We support playback only. Let capture devices be handled by FFADO. */
  997. err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
  998. DICE_PRIVATE_SPACE +
  999. be32_to_cpu(pointers[2]) * 4,
  1000. tx_data, sizeof(tx_data), 0);
  1001. if (err < 0 || (tx_data[0] && tx_data[3]))
  1002. return -ENODEV;
  1003. /*
  1004. * Check that the implemented DICE driver specification major version
  1005. * number matches.
  1006. */
  1007. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  1008. DICE_PRIVATE_SPACE +
  1009. be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
  1010. &version, 4, 0);
  1011. if (err < 0)
  1012. return -ENODEV;
  1013. if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
  1014. dev_err(&unit->device,
  1015. "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
  1016. return -ENODEV;
  1017. }
  1018. return 0;
  1019. }
  1020. static int highest_supported_mode_rate(struct dice *dice, unsigned int mode)
  1021. {
  1022. int i;
  1023. for (i = ARRAY_SIZE(dice_rates) - 1; i >= 0; --i)
  1024. if ((dice->clock_caps & (1 << i)) &&
  1025. rate_index_to_mode(i) == mode)
  1026. return i;
  1027. return -1;
  1028. }
  1029. static int dice_read_mode_params(struct dice *dice, unsigned int mode)
  1030. {
  1031. __be32 values[2];
  1032. int rate_index, err;
  1033. rate_index = highest_supported_mode_rate(dice, mode);
  1034. if (rate_index < 0) {
  1035. dice->rx_channels[mode] = 0;
  1036. dice->rx_midi_ports[mode] = 0;
  1037. return 0;
  1038. }
  1039. err = dice_change_rate(dice, rate_index << CLOCK_RATE_SHIFT);
  1040. if (err < 0)
  1041. return err;
  1042. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  1043. rx_address(dice, RX_NUMBER_AUDIO),
  1044. values, 2 * 4, 0);
  1045. if (err < 0)
  1046. return err;
  1047. dice->rx_channels[mode] = be32_to_cpu(values[0]);
  1048. dice->rx_midi_ports[mode] = be32_to_cpu(values[1]);
  1049. return 0;
  1050. }
  1051. static int dice_read_params(struct dice *dice)
  1052. {
  1053. __be32 pointers[6];
  1054. __be32 value;
  1055. int mode, err;
  1056. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  1057. DICE_PRIVATE_SPACE,
  1058. pointers, sizeof(pointers), 0);
  1059. if (err < 0)
  1060. return err;
  1061. dice->global_offset = be32_to_cpu(pointers[0]) * 4;
  1062. dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
  1063. /* some very old firmwares don't tell about their clock support */
  1064. if (be32_to_cpu(pointers[1]) * 4 >= GLOBAL_CLOCK_CAPABILITIES + 4) {
  1065. err = snd_fw_transaction(
  1066. dice->unit, TCODE_READ_QUADLET_REQUEST,
  1067. global_address(dice, GLOBAL_CLOCK_CAPABILITIES),
  1068. &value, 4, 0);
  1069. if (err < 0)
  1070. return err;
  1071. dice->clock_caps = be32_to_cpu(value);
  1072. } else {
  1073. /* this should be supported by any device */
  1074. dice->clock_caps = CLOCK_CAP_RATE_44100 |
  1075. CLOCK_CAP_RATE_48000 |
  1076. CLOCK_CAP_SOURCE_ARX1 |
  1077. CLOCK_CAP_SOURCE_INTERNAL;
  1078. }
  1079. for (mode = 2; mode >= 0; --mode) {
  1080. err = dice_read_mode_params(dice, mode);
  1081. if (err < 0)
  1082. return err;
  1083. }
  1084. return 0;
  1085. }
  1086. static void dice_card_strings(struct dice *dice)
  1087. {
  1088. struct snd_card *card = dice->card;
  1089. struct fw_device *dev = fw_parent_device(dice->unit);
  1090. char vendor[32], model[32];
  1091. unsigned int i;
  1092. int err;
  1093. strcpy(card->driver, "DICE");
  1094. strcpy(card->shortname, "DICE");
  1095. BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
  1096. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  1097. global_address(dice, GLOBAL_NICK_NAME),
  1098. card->shortname, sizeof(card->shortname), 0);
  1099. if (err >= 0) {
  1100. /* DICE strings are returned in "always-wrong" endianness */
  1101. BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
  1102. for (i = 0; i < sizeof(card->shortname); i += 4)
  1103. swab32s((u32 *)&card->shortname[i]);
  1104. card->shortname[sizeof(card->shortname) - 1] = '\0';
  1105. }
  1106. strcpy(vendor, "?");
  1107. fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
  1108. strcpy(model, "?");
  1109. fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
  1110. snprintf(card->longname, sizeof(card->longname),
  1111. "%s %s (serial %u) at %s, S%d",
  1112. vendor, model, dev->config_rom[4] & 0x3fffff,
  1113. dev_name(&dice->unit->device), 100 << dev->max_speed);
  1114. strcpy(card->mixername, "DICE");
  1115. }
  1116. static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
  1117. {
  1118. struct snd_card *card;
  1119. struct dice *dice;
  1120. __be32 clock_sel;
  1121. int err;
  1122. err = dice_interface_check(unit);
  1123. if (err < 0)
  1124. return err;
  1125. err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
  1126. sizeof(*dice), &card);
  1127. if (err < 0)
  1128. return err;
  1129. dice = card->private_data;
  1130. dice->card = card;
  1131. spin_lock_init(&dice->lock);
  1132. mutex_init(&dice->mutex);
  1133. dice->unit = unit;
  1134. init_completion(&dice->clock_accepted);
  1135. init_waitqueue_head(&dice->hwdep_wait);
  1136. dice->notification_handler.length = 4;
  1137. dice->notification_handler.address_callback = dice_notification;
  1138. dice->notification_handler.callback_data = dice;
  1139. err = fw_core_add_address_handler(&dice->notification_handler,
  1140. &fw_high_memory_region);
  1141. if (err < 0)
  1142. goto err_mutex;
  1143. err = dice_owner_set(dice);
  1144. if (err < 0)
  1145. goto err_notification_handler;
  1146. err = dice_read_params(dice);
  1147. if (err < 0)
  1148. goto err_owner;
  1149. err = fw_iso_resources_init(&dice->resources, unit);
  1150. if (err < 0)
  1151. goto err_owner;
  1152. dice->resources.channels_mask = 0x00000000ffffffffuLL;
  1153. err = amdtp_stream_init(&dice->stream, unit, AMDTP_OUT_STREAM,
  1154. CIP_BLOCKING);
  1155. if (err < 0)
  1156. goto err_resources;
  1157. card->private_free = dice_card_free;
  1158. dice_card_strings(dice);
  1159. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  1160. global_address(dice, GLOBAL_CLOCK_SELECT),
  1161. &clock_sel, 4, 0);
  1162. if (err < 0)
  1163. goto error;
  1164. clock_sel &= cpu_to_be32(~CLOCK_SOURCE_MASK);
  1165. clock_sel |= cpu_to_be32(CLOCK_SOURCE_ARX1);
  1166. err = snd_fw_transaction(unit, TCODE_WRITE_QUADLET_REQUEST,
  1167. global_address(dice, GLOBAL_CLOCK_SELECT),
  1168. &clock_sel, 4, 0);
  1169. if (err < 0)
  1170. goto error;
  1171. err = dice_create_pcm(dice);
  1172. if (err < 0)
  1173. goto error;
  1174. err = dice_create_hwdep(dice);
  1175. if (err < 0)
  1176. goto error;
  1177. dice_create_proc(dice);
  1178. err = snd_card_register(card);
  1179. if (err < 0)
  1180. goto error;
  1181. dev_set_drvdata(&unit->device, dice);
  1182. return 0;
  1183. err_resources:
  1184. fw_iso_resources_destroy(&dice->resources);
  1185. err_owner:
  1186. dice_owner_clear(dice);
  1187. err_notification_handler:
  1188. fw_core_remove_address_handler(&dice->notification_handler);
  1189. err_mutex:
  1190. mutex_destroy(&dice->mutex);
  1191. error:
  1192. snd_card_free(card);
  1193. return err;
  1194. }
  1195. static void dice_remove(struct fw_unit *unit)
  1196. {
  1197. struct dice *dice = dev_get_drvdata(&unit->device);
  1198. amdtp_stream_pcm_abort(&dice->stream);
  1199. snd_card_disconnect(dice->card);
  1200. mutex_lock(&dice->mutex);
  1201. dice_stream_stop(dice);
  1202. dice_owner_clear(dice);
  1203. mutex_unlock(&dice->mutex);
  1204. snd_card_free_when_closed(dice->card);
  1205. }
  1206. static void dice_bus_reset(struct fw_unit *unit)
  1207. {
  1208. struct dice *dice = dev_get_drvdata(&unit->device);
  1209. /*
  1210. * On a bus reset, the DICE firmware disables streaming and then goes
  1211. * off contemplating its own navel for hundreds of milliseconds before
  1212. * it can react to any of our attempts to reenable streaming. This
  1213. * means that we lose synchronization anyway, so we force our streams
  1214. * to stop so that the application can restart them in an orderly
  1215. * manner.
  1216. */
  1217. amdtp_stream_pcm_abort(&dice->stream);
  1218. mutex_lock(&dice->mutex);
  1219. dice->global_enabled = false;
  1220. dice_stream_stop_packets(dice);
  1221. dice_owner_update(dice);
  1222. fw_iso_resources_update(&dice->resources);
  1223. mutex_unlock(&dice->mutex);
  1224. }
  1225. #define DICE_INTERFACE 0x000001
  1226. static const struct ieee1394_device_id dice_id_table[] = {
  1227. {
  1228. .match_flags = IEEE1394_MATCH_VERSION,
  1229. .version = DICE_INTERFACE,
  1230. },
  1231. { }
  1232. };
  1233. MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
  1234. static struct fw_driver dice_driver = {
  1235. .driver = {
  1236. .owner = THIS_MODULE,
  1237. .name = KBUILD_MODNAME,
  1238. .bus = &fw_bus_type,
  1239. },
  1240. .probe = dice_probe,
  1241. .update = dice_bus_reset,
  1242. .remove = dice_remove,
  1243. .id_table = dice_id_table,
  1244. };
  1245. static int __init alsa_dice_init(void)
  1246. {
  1247. return driver_register(&dice_driver.driver);
  1248. }
  1249. static void __exit alsa_dice_exit(void)
  1250. {
  1251. driver_unregister(&dice_driver.driver);
  1252. }
  1253. module_init(alsa_dice_init);
  1254. module_exit(alsa_dice_exit);