dice.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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_out_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 = snd_pcm_hw_constraint_step(runtime, 0,
  353. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
  354. if (err < 0)
  355. goto err_lock;
  356. err = snd_pcm_hw_constraint_step(runtime, 0,
  357. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
  358. if (err < 0)
  359. goto err_lock;
  360. err = snd_pcm_hw_constraint_minmax(runtime,
  361. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  362. 5000, UINT_MAX);
  363. if (err < 0)
  364. goto err_lock;
  365. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  366. if (err < 0)
  367. goto err_lock;
  368. return 0;
  369. err_lock:
  370. dice_unlock(dice);
  371. error:
  372. return err;
  373. }
  374. static int dice_close(struct snd_pcm_substream *substream)
  375. {
  376. struct dice *dice = substream->private_data;
  377. dice_unlock(dice);
  378. return 0;
  379. }
  380. static int dice_stream_start_packets(struct dice *dice)
  381. {
  382. int err;
  383. if (amdtp_out_stream_running(&dice->stream))
  384. return 0;
  385. err = amdtp_out_stream_start(&dice->stream, dice->resources.channel,
  386. fw_parent_device(dice->unit)->max_speed);
  387. if (err < 0)
  388. return err;
  389. err = dice_enable_set(dice);
  390. if (err < 0) {
  391. amdtp_out_stream_stop(&dice->stream);
  392. return err;
  393. }
  394. return 0;
  395. }
  396. static int dice_stream_start(struct dice *dice)
  397. {
  398. __be32 channel;
  399. int err;
  400. if (!dice->resources.allocated) {
  401. err = fw_iso_resources_allocate(&dice->resources,
  402. amdtp_out_stream_get_max_payload(&dice->stream),
  403. fw_parent_device(dice->unit)->max_speed);
  404. if (err < 0)
  405. goto error;
  406. channel = cpu_to_be32(dice->resources.channel);
  407. err = snd_fw_transaction(dice->unit,
  408. TCODE_WRITE_QUADLET_REQUEST,
  409. rx_address(dice, RX_ISOCHRONOUS),
  410. &channel, 4, 0);
  411. if (err < 0)
  412. goto err_resources;
  413. }
  414. err = dice_stream_start_packets(dice);
  415. if (err < 0)
  416. goto err_rx_channel;
  417. return 0;
  418. err_rx_channel:
  419. channel = cpu_to_be32((u32)-1);
  420. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  421. rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
  422. err_resources:
  423. fw_iso_resources_free(&dice->resources);
  424. error:
  425. return err;
  426. }
  427. static void dice_stream_stop_packets(struct dice *dice)
  428. {
  429. if (amdtp_out_stream_running(&dice->stream)) {
  430. dice_enable_clear(dice);
  431. amdtp_out_stream_stop(&dice->stream);
  432. }
  433. }
  434. static void dice_stream_stop(struct dice *dice)
  435. {
  436. __be32 channel;
  437. dice_stream_stop_packets(dice);
  438. if (!dice->resources.allocated)
  439. return;
  440. channel = cpu_to_be32((u32)-1);
  441. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  442. rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
  443. fw_iso_resources_free(&dice->resources);
  444. }
  445. static int dice_change_rate(struct dice *dice, unsigned int clock_rate)
  446. {
  447. __be32 value;
  448. int err;
  449. reinit_completion(&dice->clock_accepted);
  450. value = cpu_to_be32(clock_rate | CLOCK_SOURCE_ARX1);
  451. err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  452. global_address(dice, GLOBAL_CLOCK_SELECT),
  453. &value, 4, 0);
  454. if (err < 0)
  455. return err;
  456. if (!wait_for_completion_timeout(&dice->clock_accepted,
  457. msecs_to_jiffies(100)))
  458. dev_warn(&dice->unit->device, "clock change timed out\n");
  459. return 0;
  460. }
  461. static int dice_hw_params(struct snd_pcm_substream *substream,
  462. struct snd_pcm_hw_params *hw_params)
  463. {
  464. struct dice *dice = substream->private_data;
  465. unsigned int rate_index, mode;
  466. int err;
  467. mutex_lock(&dice->mutex);
  468. dice_stream_stop(dice);
  469. mutex_unlock(&dice->mutex);
  470. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  471. params_buffer_bytes(hw_params));
  472. if (err < 0)
  473. return err;
  474. rate_index = rate_to_index(params_rate(hw_params));
  475. err = dice_change_rate(dice, rate_index << CLOCK_RATE_SHIFT);
  476. if (err < 0)
  477. return err;
  478. mode = rate_index_to_mode(rate_index);
  479. amdtp_out_stream_set_parameters(&dice->stream,
  480. params_rate(hw_params),
  481. params_channels(hw_params),
  482. dice->rx_midi_ports[mode]);
  483. amdtp_out_stream_set_pcm_format(&dice->stream,
  484. params_format(hw_params));
  485. return 0;
  486. }
  487. static int dice_hw_free(struct snd_pcm_substream *substream)
  488. {
  489. struct dice *dice = substream->private_data;
  490. mutex_lock(&dice->mutex);
  491. dice_stream_stop(dice);
  492. mutex_unlock(&dice->mutex);
  493. return snd_pcm_lib_free_vmalloc_buffer(substream);
  494. }
  495. static int dice_prepare(struct snd_pcm_substream *substream)
  496. {
  497. struct dice *dice = substream->private_data;
  498. int err;
  499. mutex_lock(&dice->mutex);
  500. if (amdtp_out_streaming_error(&dice->stream))
  501. dice_stream_stop_packets(dice);
  502. err = dice_stream_start(dice);
  503. if (err < 0) {
  504. mutex_unlock(&dice->mutex);
  505. return err;
  506. }
  507. mutex_unlock(&dice->mutex);
  508. amdtp_out_stream_pcm_prepare(&dice->stream);
  509. return 0;
  510. }
  511. static int dice_trigger(struct snd_pcm_substream *substream, int cmd)
  512. {
  513. struct dice *dice = substream->private_data;
  514. struct snd_pcm_substream *pcm;
  515. switch (cmd) {
  516. case SNDRV_PCM_TRIGGER_START:
  517. pcm = substream;
  518. break;
  519. case SNDRV_PCM_TRIGGER_STOP:
  520. pcm = NULL;
  521. break;
  522. default:
  523. return -EINVAL;
  524. }
  525. amdtp_out_stream_pcm_trigger(&dice->stream, pcm);
  526. return 0;
  527. }
  528. static snd_pcm_uframes_t dice_pointer(struct snd_pcm_substream *substream)
  529. {
  530. struct dice *dice = substream->private_data;
  531. return amdtp_out_stream_pcm_pointer(&dice->stream);
  532. }
  533. static int dice_create_pcm(struct dice *dice)
  534. {
  535. static struct snd_pcm_ops ops = {
  536. .open = dice_open,
  537. .close = dice_close,
  538. .ioctl = snd_pcm_lib_ioctl,
  539. .hw_params = dice_hw_params,
  540. .hw_free = dice_hw_free,
  541. .prepare = dice_prepare,
  542. .trigger = dice_trigger,
  543. .pointer = dice_pointer,
  544. .page = snd_pcm_lib_get_vmalloc_page,
  545. .mmap = snd_pcm_lib_mmap_vmalloc,
  546. };
  547. struct snd_pcm *pcm;
  548. int err;
  549. err = snd_pcm_new(dice->card, "DICE", 0, 1, 0, &pcm);
  550. if (err < 0)
  551. return err;
  552. pcm->private_data = dice;
  553. strcpy(pcm->name, dice->card->shortname);
  554. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->ops = &ops;
  555. return 0;
  556. }
  557. static long dice_hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
  558. long count, loff_t *offset)
  559. {
  560. struct dice *dice = hwdep->private_data;
  561. DEFINE_WAIT(wait);
  562. union snd_firewire_event event;
  563. spin_lock_irq(&dice->lock);
  564. while (!dice->dev_lock_changed && dice->notification_bits == 0) {
  565. prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
  566. spin_unlock_irq(&dice->lock);
  567. schedule();
  568. finish_wait(&dice->hwdep_wait, &wait);
  569. if (signal_pending(current))
  570. return -ERESTARTSYS;
  571. spin_lock_irq(&dice->lock);
  572. }
  573. memset(&event, 0, sizeof(event));
  574. if (dice->dev_lock_changed) {
  575. event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
  576. event.lock_status.status = dice->dev_lock_count > 0;
  577. dice->dev_lock_changed = false;
  578. count = min(count, (long)sizeof(event.lock_status));
  579. } else {
  580. event.dice_notification.type = SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
  581. event.dice_notification.notification = dice->notification_bits;
  582. dice->notification_bits = 0;
  583. count = min(count, (long)sizeof(event.dice_notification));
  584. }
  585. spin_unlock_irq(&dice->lock);
  586. if (copy_to_user(buf, &event, count))
  587. return -EFAULT;
  588. return count;
  589. }
  590. static unsigned int dice_hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
  591. poll_table *wait)
  592. {
  593. struct dice *dice = hwdep->private_data;
  594. unsigned int events;
  595. poll_wait(file, &dice->hwdep_wait, wait);
  596. spin_lock_irq(&dice->lock);
  597. if (dice->dev_lock_changed || dice->notification_bits != 0)
  598. events = POLLIN | POLLRDNORM;
  599. else
  600. events = 0;
  601. spin_unlock_irq(&dice->lock);
  602. return events;
  603. }
  604. static int dice_hwdep_get_info(struct dice *dice, void __user *arg)
  605. {
  606. struct fw_device *dev = fw_parent_device(dice->unit);
  607. struct snd_firewire_get_info info;
  608. memset(&info, 0, sizeof(info));
  609. info.type = SNDRV_FIREWIRE_TYPE_DICE;
  610. info.card = dev->card->index;
  611. *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
  612. *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
  613. strlcpy(info.device_name, dev_name(&dev->device),
  614. sizeof(info.device_name));
  615. if (copy_to_user(arg, &info, sizeof(info)))
  616. return -EFAULT;
  617. return 0;
  618. }
  619. static int dice_hwdep_lock(struct dice *dice)
  620. {
  621. int err;
  622. spin_lock_irq(&dice->lock);
  623. if (dice->dev_lock_count == 0) {
  624. dice->dev_lock_count = -1;
  625. err = 0;
  626. } else {
  627. err = -EBUSY;
  628. }
  629. spin_unlock_irq(&dice->lock);
  630. return err;
  631. }
  632. static int dice_hwdep_unlock(struct dice *dice)
  633. {
  634. int err;
  635. spin_lock_irq(&dice->lock);
  636. if (dice->dev_lock_count == -1) {
  637. dice->dev_lock_count = 0;
  638. err = 0;
  639. } else {
  640. err = -EBADFD;
  641. }
  642. spin_unlock_irq(&dice->lock);
  643. return err;
  644. }
  645. static int dice_hwdep_release(struct snd_hwdep *hwdep, struct file *file)
  646. {
  647. struct dice *dice = hwdep->private_data;
  648. spin_lock_irq(&dice->lock);
  649. if (dice->dev_lock_count == -1)
  650. dice->dev_lock_count = 0;
  651. spin_unlock_irq(&dice->lock);
  652. return 0;
  653. }
  654. static int dice_hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
  655. unsigned int cmd, unsigned long arg)
  656. {
  657. struct dice *dice = hwdep->private_data;
  658. switch (cmd) {
  659. case SNDRV_FIREWIRE_IOCTL_GET_INFO:
  660. return dice_hwdep_get_info(dice, (void __user *)arg);
  661. case SNDRV_FIREWIRE_IOCTL_LOCK:
  662. return dice_hwdep_lock(dice);
  663. case SNDRV_FIREWIRE_IOCTL_UNLOCK:
  664. return dice_hwdep_unlock(dice);
  665. default:
  666. return -ENOIOCTLCMD;
  667. }
  668. }
  669. #ifdef CONFIG_COMPAT
  670. static int dice_hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
  671. unsigned int cmd, unsigned long arg)
  672. {
  673. return dice_hwdep_ioctl(hwdep, file, cmd,
  674. (unsigned long)compat_ptr(arg));
  675. }
  676. #else
  677. #define dice_hwdep_compat_ioctl NULL
  678. #endif
  679. static int dice_create_hwdep(struct dice *dice)
  680. {
  681. static const struct snd_hwdep_ops ops = {
  682. .read = dice_hwdep_read,
  683. .release = dice_hwdep_release,
  684. .poll = dice_hwdep_poll,
  685. .ioctl = dice_hwdep_ioctl,
  686. .ioctl_compat = dice_hwdep_compat_ioctl,
  687. };
  688. struct snd_hwdep *hwdep;
  689. int err;
  690. err = snd_hwdep_new(dice->card, "DICE", 0, &hwdep);
  691. if (err < 0)
  692. return err;
  693. strcpy(hwdep->name, "DICE");
  694. hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
  695. hwdep->ops = ops;
  696. hwdep->private_data = dice;
  697. hwdep->exclusive = true;
  698. return 0;
  699. }
  700. static int dice_proc_read_mem(struct dice *dice, void *buffer,
  701. unsigned int offset_q, unsigned int quadlets)
  702. {
  703. unsigned int i;
  704. int err;
  705. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  706. DICE_PRIVATE_SPACE + 4 * offset_q,
  707. buffer, 4 * quadlets, 0);
  708. if (err < 0)
  709. return err;
  710. for (i = 0; i < quadlets; ++i)
  711. be32_to_cpus(&((u32 *)buffer)[i]);
  712. return 0;
  713. }
  714. static const char *str_from_array(const char *const strs[], unsigned int count,
  715. unsigned int i)
  716. {
  717. if (i < count)
  718. return strs[i];
  719. else
  720. return "(unknown)";
  721. }
  722. static void dice_proc_fixup_string(char *s, unsigned int size)
  723. {
  724. unsigned int i;
  725. for (i = 0; i < size; i += 4)
  726. cpu_to_le32s((u32 *)(s + i));
  727. for (i = 0; i < size - 2; ++i) {
  728. if (s[i] == '\0')
  729. return;
  730. if (s[i] == '\\' && s[i + 1] == '\\') {
  731. s[i + 2] = '\0';
  732. return;
  733. }
  734. }
  735. s[size - 1] = '\0';
  736. }
  737. static void dice_proc_read(struct snd_info_entry *entry,
  738. struct snd_info_buffer *buffer)
  739. {
  740. static const char *const section_names[5] = {
  741. "global", "tx", "rx", "ext_sync", "unused2"
  742. };
  743. static const char *const clock_sources[] = {
  744. "aes1", "aes2", "aes3", "aes4", "aes", "adat", "tdif",
  745. "wc", "arx1", "arx2", "arx3", "arx4", "internal"
  746. };
  747. static const char *const rates[] = {
  748. "32000", "44100", "48000", "88200", "96000", "176400", "192000",
  749. "any low", "any mid", "any high", "none"
  750. };
  751. struct dice *dice = entry->private_data;
  752. u32 sections[ARRAY_SIZE(section_names) * 2];
  753. struct {
  754. u32 number;
  755. u32 size;
  756. } tx_rx_header;
  757. union {
  758. struct {
  759. u32 owner_hi, owner_lo;
  760. u32 notification;
  761. char nick_name[NICK_NAME_SIZE];
  762. u32 clock_select;
  763. u32 enable;
  764. u32 status;
  765. u32 extended_status;
  766. u32 sample_rate;
  767. u32 version;
  768. u32 clock_caps;
  769. char clock_source_names[CLOCK_SOURCE_NAMES_SIZE];
  770. } global;
  771. struct {
  772. u32 iso;
  773. u32 number_audio;
  774. u32 number_midi;
  775. u32 speed;
  776. char names[TX_NAMES_SIZE];
  777. u32 ac3_caps;
  778. u32 ac3_enable;
  779. } tx;
  780. struct {
  781. u32 iso;
  782. u32 seq_start;
  783. u32 number_audio;
  784. u32 number_midi;
  785. char names[RX_NAMES_SIZE];
  786. u32 ac3_caps;
  787. u32 ac3_enable;
  788. } rx;
  789. struct {
  790. u32 clock_source;
  791. u32 locked;
  792. u32 rate;
  793. u32 adat_user_data;
  794. } ext_sync;
  795. } buf;
  796. unsigned int quadlets, stream, i;
  797. if (dice_proc_read_mem(dice, sections, 0, ARRAY_SIZE(sections)) < 0)
  798. return;
  799. snd_iprintf(buffer, "sections:\n");
  800. for (i = 0; i < ARRAY_SIZE(section_names); ++i)
  801. snd_iprintf(buffer, " %s: offset %u, size %u\n",
  802. section_names[i],
  803. sections[i * 2], sections[i * 2 + 1]);
  804. quadlets = min_t(u32, sections[1], sizeof(buf.global) / 4);
  805. if (dice_proc_read_mem(dice, &buf.global, sections[0], quadlets) < 0)
  806. return;
  807. snd_iprintf(buffer, "global:\n");
  808. snd_iprintf(buffer, " owner: %04x:%04x%08x\n",
  809. buf.global.owner_hi >> 16,
  810. buf.global.owner_hi & 0xffff, buf.global.owner_lo);
  811. snd_iprintf(buffer, " notification: %08x\n", buf.global.notification);
  812. dice_proc_fixup_string(buf.global.nick_name, NICK_NAME_SIZE);
  813. snd_iprintf(buffer, " nick name: %s\n", buf.global.nick_name);
  814. snd_iprintf(buffer, " clock select: %s %s\n",
  815. str_from_array(clock_sources, ARRAY_SIZE(clock_sources),
  816. buf.global.clock_select & CLOCK_SOURCE_MASK),
  817. str_from_array(rates, ARRAY_SIZE(rates),
  818. (buf.global.clock_select & CLOCK_RATE_MASK)
  819. >> CLOCK_RATE_SHIFT));
  820. snd_iprintf(buffer, " enable: %u\n", buf.global.enable);
  821. snd_iprintf(buffer, " status: %slocked %s\n",
  822. buf.global.status & STATUS_SOURCE_LOCKED ? "" : "un",
  823. str_from_array(rates, ARRAY_SIZE(rates),
  824. (buf.global.status &
  825. STATUS_NOMINAL_RATE_MASK)
  826. >> CLOCK_RATE_SHIFT));
  827. snd_iprintf(buffer, " ext status: %08x\n", buf.global.extended_status);
  828. snd_iprintf(buffer, " sample rate: %u\n", buf.global.sample_rate);
  829. snd_iprintf(buffer, " version: %u.%u.%u.%u\n",
  830. (buf.global.version >> 24) & 0xff,
  831. (buf.global.version >> 16) & 0xff,
  832. (buf.global.version >> 8) & 0xff,
  833. (buf.global.version >> 0) & 0xff);
  834. if (quadlets >= 90) {
  835. snd_iprintf(buffer, " clock caps:");
  836. for (i = 0; i <= 6; ++i)
  837. if (buf.global.clock_caps & (1 << i))
  838. snd_iprintf(buffer, " %s", rates[i]);
  839. for (i = 0; i <= 12; ++i)
  840. if (buf.global.clock_caps & (1 << (16 + i)))
  841. snd_iprintf(buffer, " %s", clock_sources[i]);
  842. snd_iprintf(buffer, "\n");
  843. dice_proc_fixup_string(buf.global.clock_source_names,
  844. CLOCK_SOURCE_NAMES_SIZE);
  845. snd_iprintf(buffer, " clock source names: %s\n",
  846. buf.global.clock_source_names);
  847. }
  848. if (dice_proc_read_mem(dice, &tx_rx_header, sections[2], 2) < 0)
  849. return;
  850. quadlets = min_t(u32, tx_rx_header.size, sizeof(buf.tx));
  851. for (stream = 0; stream < tx_rx_header.number; ++stream) {
  852. if (dice_proc_read_mem(dice, &buf.tx, sections[2] + 2 +
  853. stream * tx_rx_header.size,
  854. quadlets) < 0)
  855. break;
  856. snd_iprintf(buffer, "tx %u:\n", stream);
  857. snd_iprintf(buffer, " iso channel: %d\n", (int)buf.tx.iso);
  858. snd_iprintf(buffer, " audio channels: %u\n",
  859. buf.tx.number_audio);
  860. snd_iprintf(buffer, " midi ports: %u\n", buf.tx.number_midi);
  861. snd_iprintf(buffer, " speed: S%u\n", 100u << buf.tx.speed);
  862. if (quadlets >= 68) {
  863. dice_proc_fixup_string(buf.tx.names, TX_NAMES_SIZE);
  864. snd_iprintf(buffer, " names: %s\n", buf.tx.names);
  865. }
  866. if (quadlets >= 70) {
  867. snd_iprintf(buffer, " ac3 caps: %08x\n",
  868. buf.tx.ac3_caps);
  869. snd_iprintf(buffer, " ac3 enable: %08x\n",
  870. buf.tx.ac3_enable);
  871. }
  872. }
  873. if (dice_proc_read_mem(dice, &tx_rx_header, sections[4], 2) < 0)
  874. return;
  875. quadlets = min_t(u32, tx_rx_header.size, sizeof(buf.rx));
  876. for (stream = 0; stream < tx_rx_header.number; ++stream) {
  877. if (dice_proc_read_mem(dice, &buf.rx, sections[4] + 2 +
  878. stream * tx_rx_header.size,
  879. quadlets) < 0)
  880. break;
  881. snd_iprintf(buffer, "rx %u:\n", stream);
  882. snd_iprintf(buffer, " iso channel: %d\n", (int)buf.rx.iso);
  883. snd_iprintf(buffer, " sequence start: %u\n", buf.rx.seq_start);
  884. snd_iprintf(buffer, " audio channels: %u\n",
  885. buf.rx.number_audio);
  886. snd_iprintf(buffer, " midi ports: %u\n", buf.rx.number_midi);
  887. if (quadlets >= 68) {
  888. dice_proc_fixup_string(buf.rx.names, RX_NAMES_SIZE);
  889. snd_iprintf(buffer, " names: %s\n", buf.rx.names);
  890. }
  891. if (quadlets >= 70) {
  892. snd_iprintf(buffer, " ac3 caps: %08x\n",
  893. buf.rx.ac3_caps);
  894. snd_iprintf(buffer, " ac3 enable: %08x\n",
  895. buf.rx.ac3_enable);
  896. }
  897. }
  898. quadlets = min_t(u32, sections[7], sizeof(buf.ext_sync) / 4);
  899. if (quadlets >= 4) {
  900. if (dice_proc_read_mem(dice, &buf.ext_sync,
  901. sections[6], 4) < 0)
  902. return;
  903. snd_iprintf(buffer, "ext status:\n");
  904. snd_iprintf(buffer, " clock source: %s\n",
  905. str_from_array(clock_sources,
  906. ARRAY_SIZE(clock_sources),
  907. buf.ext_sync.clock_source));
  908. snd_iprintf(buffer, " locked: %u\n", buf.ext_sync.locked);
  909. snd_iprintf(buffer, " rate: %s\n",
  910. str_from_array(rates, ARRAY_SIZE(rates),
  911. buf.ext_sync.rate));
  912. snd_iprintf(buffer, " adat user data: ");
  913. if (buf.ext_sync.adat_user_data & ADAT_USER_DATA_NO_DATA)
  914. snd_iprintf(buffer, "-\n");
  915. else
  916. snd_iprintf(buffer, "%x\n",
  917. buf.ext_sync.adat_user_data);
  918. }
  919. }
  920. static void dice_create_proc(struct dice *dice)
  921. {
  922. struct snd_info_entry *entry;
  923. if (!snd_card_proc_new(dice->card, "dice", &entry))
  924. snd_info_set_text_ops(entry, dice, dice_proc_read);
  925. }
  926. static void dice_card_free(struct snd_card *card)
  927. {
  928. struct dice *dice = card->private_data;
  929. amdtp_out_stream_destroy(&dice->stream);
  930. fw_core_remove_address_handler(&dice->notification_handler);
  931. mutex_destroy(&dice->mutex);
  932. }
  933. #define OUI_WEISS 0x001c6a
  934. #define DICE_CATEGORY_ID 0x04
  935. #define WEISS_CATEGORY_ID 0x00
  936. static int dice_interface_check(struct fw_unit *unit)
  937. {
  938. static const int min_values[10] = {
  939. 10, 0x64 / 4,
  940. 10, 0x18 / 4,
  941. 10, 0x18 / 4,
  942. 0, 0,
  943. 0, 0,
  944. };
  945. struct fw_device *device = fw_parent_device(unit);
  946. struct fw_csr_iterator it;
  947. int key, value, vendor = -1, model = -1, err;
  948. unsigned int category, i;
  949. __be32 pointers[ARRAY_SIZE(min_values)];
  950. __be32 tx_data[4];
  951. __be32 version;
  952. /*
  953. * Check that GUID and unit directory are constructed according to DICE
  954. * rules, i.e., that the specifier ID is the GUID's OUI, and that the
  955. * GUID chip ID consists of the 8-bit category ID, the 10-bit product
  956. * ID, and a 22-bit serial number.
  957. */
  958. fw_csr_iterator_init(&it, unit->directory);
  959. while (fw_csr_iterator_next(&it, &key, &value)) {
  960. switch (key) {
  961. case CSR_SPECIFIER_ID:
  962. vendor = value;
  963. break;
  964. case CSR_MODEL:
  965. model = value;
  966. break;
  967. }
  968. }
  969. if (vendor == OUI_WEISS)
  970. category = WEISS_CATEGORY_ID;
  971. else
  972. category = DICE_CATEGORY_ID;
  973. if (device->config_rom[3] != ((vendor << 8) | category) ||
  974. device->config_rom[4] >> 22 != model)
  975. return -ENODEV;
  976. /*
  977. * Check that the sub address spaces exist and are located inside the
  978. * private address space. The minimum values are chosen so that all
  979. * minimally required registers are included.
  980. */
  981. err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
  982. DICE_PRIVATE_SPACE,
  983. pointers, sizeof(pointers), 0);
  984. if (err < 0)
  985. return -ENODEV;
  986. for (i = 0; i < ARRAY_SIZE(pointers); ++i) {
  987. value = be32_to_cpu(pointers[i]);
  988. if (value < min_values[i] || value >= 0x40000)
  989. return -ENODEV;
  990. }
  991. /* We support playback only. Let capture devices be handled by FFADO. */
  992. err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
  993. DICE_PRIVATE_SPACE +
  994. be32_to_cpu(pointers[2]) * 4,
  995. tx_data, sizeof(tx_data), 0);
  996. if (err < 0 || (tx_data[0] && tx_data[3]))
  997. return -ENODEV;
  998. /*
  999. * Check that the implemented DICE driver specification major version
  1000. * number matches.
  1001. */
  1002. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  1003. DICE_PRIVATE_SPACE +
  1004. be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
  1005. &version, 4, 0);
  1006. if (err < 0)
  1007. return -ENODEV;
  1008. if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
  1009. dev_err(&unit->device,
  1010. "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
  1011. return -ENODEV;
  1012. }
  1013. return 0;
  1014. }
  1015. static int highest_supported_mode_rate(struct dice *dice, unsigned int mode)
  1016. {
  1017. int i;
  1018. for (i = ARRAY_SIZE(dice_rates) - 1; i >= 0; --i)
  1019. if ((dice->clock_caps & (1 << i)) &&
  1020. rate_index_to_mode(i) == mode)
  1021. return i;
  1022. return -1;
  1023. }
  1024. static int dice_read_mode_params(struct dice *dice, unsigned int mode)
  1025. {
  1026. __be32 values[2];
  1027. int rate_index, err;
  1028. rate_index = highest_supported_mode_rate(dice, mode);
  1029. if (rate_index < 0) {
  1030. dice->rx_channels[mode] = 0;
  1031. dice->rx_midi_ports[mode] = 0;
  1032. return 0;
  1033. }
  1034. err = dice_change_rate(dice, rate_index << CLOCK_RATE_SHIFT);
  1035. if (err < 0)
  1036. return err;
  1037. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  1038. rx_address(dice, RX_NUMBER_AUDIO),
  1039. values, 2 * 4, 0);
  1040. if (err < 0)
  1041. return err;
  1042. dice->rx_channels[mode] = be32_to_cpu(values[0]);
  1043. dice->rx_midi_ports[mode] = be32_to_cpu(values[1]);
  1044. return 0;
  1045. }
  1046. static int dice_read_params(struct dice *dice)
  1047. {
  1048. __be32 pointers[6];
  1049. __be32 value;
  1050. int mode, err;
  1051. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  1052. DICE_PRIVATE_SPACE,
  1053. pointers, sizeof(pointers), 0);
  1054. if (err < 0)
  1055. return err;
  1056. dice->global_offset = be32_to_cpu(pointers[0]) * 4;
  1057. dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
  1058. /* some very old firmwares don't tell about their clock support */
  1059. if (be32_to_cpu(pointers[1]) * 4 >= GLOBAL_CLOCK_CAPABILITIES + 4) {
  1060. err = snd_fw_transaction(
  1061. dice->unit, TCODE_READ_QUADLET_REQUEST,
  1062. global_address(dice, GLOBAL_CLOCK_CAPABILITIES),
  1063. &value, 4, 0);
  1064. if (err < 0)
  1065. return err;
  1066. dice->clock_caps = be32_to_cpu(value);
  1067. } else {
  1068. /* this should be supported by any device */
  1069. dice->clock_caps = CLOCK_CAP_RATE_44100 |
  1070. CLOCK_CAP_RATE_48000 |
  1071. CLOCK_CAP_SOURCE_ARX1 |
  1072. CLOCK_CAP_SOURCE_INTERNAL;
  1073. }
  1074. for (mode = 2; mode >= 0; --mode) {
  1075. err = dice_read_mode_params(dice, mode);
  1076. if (err < 0)
  1077. return err;
  1078. }
  1079. return 0;
  1080. }
  1081. static void dice_card_strings(struct dice *dice)
  1082. {
  1083. struct snd_card *card = dice->card;
  1084. struct fw_device *dev = fw_parent_device(dice->unit);
  1085. char vendor[32], model[32];
  1086. unsigned int i;
  1087. int err;
  1088. strcpy(card->driver, "DICE");
  1089. strcpy(card->shortname, "DICE");
  1090. BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
  1091. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  1092. global_address(dice, GLOBAL_NICK_NAME),
  1093. card->shortname, sizeof(card->shortname), 0);
  1094. if (err >= 0) {
  1095. /* DICE strings are returned in "always-wrong" endianness */
  1096. BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
  1097. for (i = 0; i < sizeof(card->shortname); i += 4)
  1098. swab32s((u32 *)&card->shortname[i]);
  1099. card->shortname[sizeof(card->shortname) - 1] = '\0';
  1100. }
  1101. strcpy(vendor, "?");
  1102. fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
  1103. strcpy(model, "?");
  1104. fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
  1105. snprintf(card->longname, sizeof(card->longname),
  1106. "%s %s (serial %u) at %s, S%d",
  1107. vendor, model, dev->config_rom[4] & 0x3fffff,
  1108. dev_name(&dice->unit->device), 100 << dev->max_speed);
  1109. strcpy(card->mixername, "DICE");
  1110. }
  1111. static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
  1112. {
  1113. struct snd_card *card;
  1114. struct dice *dice;
  1115. __be32 clock_sel;
  1116. int err;
  1117. err = dice_interface_check(unit);
  1118. if (err < 0)
  1119. return err;
  1120. err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*dice), &card);
  1121. if (err < 0)
  1122. return err;
  1123. snd_card_set_dev(card, &unit->device);
  1124. dice = card->private_data;
  1125. dice->card = card;
  1126. spin_lock_init(&dice->lock);
  1127. mutex_init(&dice->mutex);
  1128. dice->unit = unit;
  1129. init_completion(&dice->clock_accepted);
  1130. init_waitqueue_head(&dice->hwdep_wait);
  1131. dice->notification_handler.length = 4;
  1132. dice->notification_handler.address_callback = dice_notification;
  1133. dice->notification_handler.callback_data = dice;
  1134. err = fw_core_add_address_handler(&dice->notification_handler,
  1135. &fw_high_memory_region);
  1136. if (err < 0)
  1137. goto err_mutex;
  1138. err = dice_owner_set(dice);
  1139. if (err < 0)
  1140. goto err_notification_handler;
  1141. err = dice_read_params(dice);
  1142. if (err < 0)
  1143. goto err_owner;
  1144. err = fw_iso_resources_init(&dice->resources, unit);
  1145. if (err < 0)
  1146. goto err_owner;
  1147. dice->resources.channels_mask = 0x00000000ffffffffuLL;
  1148. err = amdtp_out_stream_init(&dice->stream, unit,
  1149. CIP_BLOCKING | CIP_HI_DUALWIRE);
  1150. if (err < 0)
  1151. goto err_resources;
  1152. card->private_free = dice_card_free;
  1153. dice_card_strings(dice);
  1154. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  1155. global_address(dice, GLOBAL_CLOCK_SELECT),
  1156. &clock_sel, 4, 0);
  1157. if (err < 0)
  1158. goto error;
  1159. clock_sel &= cpu_to_be32(~CLOCK_SOURCE_MASK);
  1160. clock_sel |= cpu_to_be32(CLOCK_SOURCE_ARX1);
  1161. err = snd_fw_transaction(unit, TCODE_WRITE_QUADLET_REQUEST,
  1162. global_address(dice, GLOBAL_CLOCK_SELECT),
  1163. &clock_sel, 4, 0);
  1164. if (err < 0)
  1165. goto error;
  1166. err = dice_create_pcm(dice);
  1167. if (err < 0)
  1168. goto error;
  1169. err = dice_create_hwdep(dice);
  1170. if (err < 0)
  1171. goto error;
  1172. dice_create_proc(dice);
  1173. err = snd_card_register(card);
  1174. if (err < 0)
  1175. goto error;
  1176. dev_set_drvdata(&unit->device, dice);
  1177. return 0;
  1178. err_resources:
  1179. fw_iso_resources_destroy(&dice->resources);
  1180. err_owner:
  1181. dice_owner_clear(dice);
  1182. err_notification_handler:
  1183. fw_core_remove_address_handler(&dice->notification_handler);
  1184. err_mutex:
  1185. mutex_destroy(&dice->mutex);
  1186. error:
  1187. snd_card_free(card);
  1188. return err;
  1189. }
  1190. static void dice_remove(struct fw_unit *unit)
  1191. {
  1192. struct dice *dice = dev_get_drvdata(&unit->device);
  1193. amdtp_out_stream_pcm_abort(&dice->stream);
  1194. snd_card_disconnect(dice->card);
  1195. mutex_lock(&dice->mutex);
  1196. dice_stream_stop(dice);
  1197. dice_owner_clear(dice);
  1198. mutex_unlock(&dice->mutex);
  1199. snd_card_free_when_closed(dice->card);
  1200. }
  1201. static void dice_bus_reset(struct fw_unit *unit)
  1202. {
  1203. struct dice *dice = dev_get_drvdata(&unit->device);
  1204. /*
  1205. * On a bus reset, the DICE firmware disables streaming and then goes
  1206. * off contemplating its own navel for hundreds of milliseconds before
  1207. * it can react to any of our attempts to reenable streaming. This
  1208. * means that we lose synchronization anyway, so we force our streams
  1209. * to stop so that the application can restart them in an orderly
  1210. * manner.
  1211. */
  1212. amdtp_out_stream_pcm_abort(&dice->stream);
  1213. mutex_lock(&dice->mutex);
  1214. dice->global_enabled = false;
  1215. dice_stream_stop_packets(dice);
  1216. dice_owner_update(dice);
  1217. fw_iso_resources_update(&dice->resources);
  1218. mutex_unlock(&dice->mutex);
  1219. }
  1220. #define DICE_INTERFACE 0x000001
  1221. static const struct ieee1394_device_id dice_id_table[] = {
  1222. {
  1223. .match_flags = IEEE1394_MATCH_VERSION,
  1224. .version = DICE_INTERFACE,
  1225. },
  1226. { }
  1227. };
  1228. MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
  1229. static struct fw_driver dice_driver = {
  1230. .driver = {
  1231. .owner = THIS_MODULE,
  1232. .name = KBUILD_MODNAME,
  1233. .bus = &fw_bus_type,
  1234. },
  1235. .probe = dice_probe,
  1236. .update = dice_bus_reset,
  1237. .remove = dice_remove,
  1238. .id_table = dice_id_table,
  1239. };
  1240. static int __init alsa_dice_init(void)
  1241. {
  1242. return driver_register(&dice_driver.driver);
  1243. }
  1244. static void __exit alsa_dice_exit(void)
  1245. {
  1246. driver_unregister(&dice_driver.driver);
  1247. }
  1248. module_init(alsa_dice_init);
  1249. module_exit(alsa_dice_exit);