dice.c 37 KB

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