dice.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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. INIT_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 DICE_CATEGORY_ID 0x04
  934. static int dice_interface_check(struct fw_unit *unit)
  935. {
  936. static const int min_values[10] = {
  937. 10, 0x64 / 4,
  938. 10, 0x18 / 4,
  939. 10, 0x18 / 4,
  940. 0, 0,
  941. 0, 0,
  942. };
  943. struct fw_device *device = fw_parent_device(unit);
  944. struct fw_csr_iterator it;
  945. int key, value, vendor = -1, model = -1, err;
  946. unsigned int i;
  947. __be32 pointers[ARRAY_SIZE(min_values)];
  948. __be32 version;
  949. /*
  950. * Check that GUID and unit directory are constructed according to DICE
  951. * rules, i.e., that the specifier ID is the GUID's OUI, and that the
  952. * GUID chip ID consists of the 8-bit DICE category ID, the 10-bit
  953. * product ID, and a 22-bit serial number.
  954. */
  955. fw_csr_iterator_init(&it, unit->directory);
  956. while (fw_csr_iterator_next(&it, &key, &value)) {
  957. switch (key) {
  958. case CSR_SPECIFIER_ID:
  959. vendor = value;
  960. break;
  961. case CSR_MODEL:
  962. model = value;
  963. break;
  964. }
  965. }
  966. if (device->config_rom[3] != ((vendor << 8) | DICE_CATEGORY_ID) ||
  967. device->config_rom[4] >> 22 != model)
  968. return -ENODEV;
  969. /*
  970. * Check that the sub address spaces exist and are located inside the
  971. * private address space. The minimum values are chosen so that all
  972. * minimally required registers are included.
  973. */
  974. err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
  975. DICE_PRIVATE_SPACE,
  976. pointers, sizeof(pointers), 0);
  977. if (err < 0)
  978. return -ENODEV;
  979. for (i = 0; i < ARRAY_SIZE(pointers); ++i) {
  980. value = be32_to_cpu(pointers[i]);
  981. if (value < min_values[i] || value >= 0x40000)
  982. return -ENODEV;
  983. }
  984. /*
  985. * Check that the implemented DICE driver specification major version
  986. * number matches.
  987. */
  988. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  989. DICE_PRIVATE_SPACE +
  990. be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
  991. &version, 4, 0);
  992. if (err < 0)
  993. return -ENODEV;
  994. if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
  995. dev_err(&unit->device,
  996. "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
  997. return -ENODEV;
  998. }
  999. return 0;
  1000. }
  1001. static int highest_supported_mode_rate(struct dice *dice, unsigned int mode)
  1002. {
  1003. int i;
  1004. for (i = ARRAY_SIZE(dice_rates) - 1; i >= 0; --i)
  1005. if ((dice->clock_caps & (1 << i)) &&
  1006. rate_index_to_mode(i) == mode)
  1007. return i;
  1008. return -1;
  1009. }
  1010. static int dice_read_mode_params(struct dice *dice, unsigned int mode)
  1011. {
  1012. __be32 values[2];
  1013. int rate_index, err;
  1014. rate_index = highest_supported_mode_rate(dice, mode);
  1015. if (rate_index < 0) {
  1016. dice->rx_channels[mode] = 0;
  1017. dice->rx_midi_ports[mode] = 0;
  1018. return 0;
  1019. }
  1020. err = dice_change_rate(dice, rate_index << CLOCK_RATE_SHIFT);
  1021. if (err < 0)
  1022. return err;
  1023. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  1024. rx_address(dice, RX_NUMBER_AUDIO),
  1025. values, 2 * 4, 0);
  1026. if (err < 0)
  1027. return err;
  1028. dice->rx_channels[mode] = be32_to_cpu(values[0]);
  1029. dice->rx_midi_ports[mode] = be32_to_cpu(values[1]);
  1030. return 0;
  1031. }
  1032. static int dice_read_params(struct dice *dice)
  1033. {
  1034. __be32 pointers[6];
  1035. __be32 value;
  1036. int mode, err;
  1037. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  1038. DICE_PRIVATE_SPACE,
  1039. pointers, sizeof(pointers), 0);
  1040. if (err < 0)
  1041. return err;
  1042. dice->global_offset = be32_to_cpu(pointers[0]) * 4;
  1043. dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
  1044. /* some very old firmwares don't tell about their clock support */
  1045. if (be32_to_cpu(pointers[1]) * 4 >= GLOBAL_CLOCK_CAPABILITIES + 4) {
  1046. err = snd_fw_transaction(
  1047. dice->unit, TCODE_READ_QUADLET_REQUEST,
  1048. global_address(dice, GLOBAL_CLOCK_CAPABILITIES),
  1049. &value, 4, 0);
  1050. if (err < 0)
  1051. return err;
  1052. dice->clock_caps = be32_to_cpu(value);
  1053. } else {
  1054. /* this should be supported by any device */
  1055. dice->clock_caps = CLOCK_CAP_RATE_44100 |
  1056. CLOCK_CAP_RATE_48000 |
  1057. CLOCK_CAP_SOURCE_ARX1 |
  1058. CLOCK_CAP_SOURCE_INTERNAL;
  1059. }
  1060. for (mode = 2; mode >= 0; --mode) {
  1061. err = dice_read_mode_params(dice, mode);
  1062. if (err < 0)
  1063. return err;
  1064. }
  1065. return 0;
  1066. }
  1067. static void dice_card_strings(struct dice *dice)
  1068. {
  1069. struct snd_card *card = dice->card;
  1070. struct fw_device *dev = fw_parent_device(dice->unit);
  1071. char vendor[32], model[32];
  1072. unsigned int i;
  1073. int err;
  1074. strcpy(card->driver, "DICE");
  1075. strcpy(card->shortname, "DICE");
  1076. BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
  1077. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  1078. global_address(dice, GLOBAL_NICK_NAME),
  1079. card->shortname, sizeof(card->shortname), 0);
  1080. if (err >= 0) {
  1081. /* DICE strings are returned in "always-wrong" endianness */
  1082. BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
  1083. for (i = 0; i < sizeof(card->shortname); i += 4)
  1084. swab32s((u32 *)&card->shortname[i]);
  1085. card->shortname[sizeof(card->shortname) - 1] = '\0';
  1086. }
  1087. strcpy(vendor, "?");
  1088. fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
  1089. strcpy(model, "?");
  1090. fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
  1091. snprintf(card->longname, sizeof(card->longname),
  1092. "%s %s (serial %u) at %s, S%d",
  1093. vendor, model, dev->config_rom[4] & 0x3fffff,
  1094. dev_name(&dice->unit->device), 100 << dev->max_speed);
  1095. strcpy(card->mixername, "DICE");
  1096. }
  1097. static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
  1098. {
  1099. struct snd_card *card;
  1100. struct dice *dice;
  1101. __be32 clock_sel;
  1102. int err;
  1103. err = dice_interface_check(unit);
  1104. if (err < 0)
  1105. return err;
  1106. err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*dice), &card);
  1107. if (err < 0)
  1108. return err;
  1109. snd_card_set_dev(card, &unit->device);
  1110. dice = card->private_data;
  1111. dice->card = card;
  1112. spin_lock_init(&dice->lock);
  1113. mutex_init(&dice->mutex);
  1114. dice->unit = unit;
  1115. init_completion(&dice->clock_accepted);
  1116. init_waitqueue_head(&dice->hwdep_wait);
  1117. dice->notification_handler.length = 4;
  1118. dice->notification_handler.address_callback = dice_notification;
  1119. dice->notification_handler.callback_data = dice;
  1120. err = fw_core_add_address_handler(&dice->notification_handler,
  1121. &fw_high_memory_region);
  1122. if (err < 0)
  1123. goto err_mutex;
  1124. err = dice_owner_set(dice);
  1125. if (err < 0)
  1126. goto err_notification_handler;
  1127. err = dice_read_params(dice);
  1128. if (err < 0)
  1129. goto err_owner;
  1130. err = fw_iso_resources_init(&dice->resources, unit);
  1131. if (err < 0)
  1132. goto err_owner;
  1133. dice->resources.channels_mask = 0x00000000ffffffffuLL;
  1134. err = amdtp_out_stream_init(&dice->stream, unit,
  1135. CIP_BLOCKING | CIP_HI_DUALWIRE);
  1136. if (err < 0)
  1137. goto err_resources;
  1138. card->private_free = dice_card_free;
  1139. dice_card_strings(dice);
  1140. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  1141. global_address(dice, GLOBAL_CLOCK_SELECT),
  1142. &clock_sel, 4, 0);
  1143. if (err < 0)
  1144. goto error;
  1145. clock_sel &= cpu_to_be32(~CLOCK_SOURCE_MASK);
  1146. clock_sel |= cpu_to_be32(CLOCK_SOURCE_ARX1);
  1147. err = snd_fw_transaction(unit, TCODE_WRITE_QUADLET_REQUEST,
  1148. global_address(dice, GLOBAL_CLOCK_SELECT),
  1149. &clock_sel, 4, 0);
  1150. if (err < 0)
  1151. goto error;
  1152. err = dice_create_pcm(dice);
  1153. if (err < 0)
  1154. goto error;
  1155. err = dice_create_hwdep(dice);
  1156. if (err < 0)
  1157. goto error;
  1158. dice_create_proc(dice);
  1159. err = snd_card_register(card);
  1160. if (err < 0)
  1161. goto error;
  1162. dev_set_drvdata(&unit->device, dice);
  1163. return 0;
  1164. err_resources:
  1165. fw_iso_resources_destroy(&dice->resources);
  1166. err_owner:
  1167. dice_owner_clear(dice);
  1168. err_notification_handler:
  1169. fw_core_remove_address_handler(&dice->notification_handler);
  1170. err_mutex:
  1171. mutex_destroy(&dice->mutex);
  1172. error:
  1173. snd_card_free(card);
  1174. return err;
  1175. }
  1176. static void dice_remove(struct fw_unit *unit)
  1177. {
  1178. struct dice *dice = dev_get_drvdata(&unit->device);
  1179. amdtp_out_stream_pcm_abort(&dice->stream);
  1180. snd_card_disconnect(dice->card);
  1181. mutex_lock(&dice->mutex);
  1182. dice_stream_stop(dice);
  1183. dice_owner_clear(dice);
  1184. mutex_unlock(&dice->mutex);
  1185. snd_card_free_when_closed(dice->card);
  1186. }
  1187. static void dice_bus_reset(struct fw_unit *unit)
  1188. {
  1189. struct dice *dice = dev_get_drvdata(&unit->device);
  1190. /*
  1191. * On a bus reset, the DICE firmware disables streaming and then goes
  1192. * off contemplating its own navel for hundreds of milliseconds before
  1193. * it can react to any of our attempts to reenable streaming. This
  1194. * means that we lose synchronization anyway, so we force our streams
  1195. * to stop so that the application can restart them in an orderly
  1196. * manner.
  1197. */
  1198. amdtp_out_stream_pcm_abort(&dice->stream);
  1199. mutex_lock(&dice->mutex);
  1200. dice->global_enabled = false;
  1201. dice_stream_stop_packets(dice);
  1202. dice_owner_update(dice);
  1203. fw_iso_resources_update(&dice->resources);
  1204. mutex_unlock(&dice->mutex);
  1205. }
  1206. #define DICE_INTERFACE 0x000001
  1207. static const struct ieee1394_device_id dice_id_table[] = {
  1208. {
  1209. .match_flags = IEEE1394_MATCH_VERSION,
  1210. .version = DICE_INTERFACE,
  1211. },
  1212. { }
  1213. };
  1214. MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
  1215. static struct fw_driver dice_driver = {
  1216. .driver = {
  1217. .owner = THIS_MODULE,
  1218. .name = KBUILD_MODNAME,
  1219. .bus = &fw_bus_type,
  1220. },
  1221. .probe = dice_probe,
  1222. .update = dice_bus_reset,
  1223. .remove = dice_remove,
  1224. .id_table = dice_id_table,
  1225. };
  1226. static int __init alsa_dice_init(void)
  1227. {
  1228. return driver_register(&dice_driver.driver);
  1229. }
  1230. static void __exit alsa_dice_exit(void)
  1231. {
  1232. driver_unregister(&dice_driver.driver);
  1233. }
  1234. module_init(alsa_dice_init);
  1235. module_exit(alsa_dice_exit);