dice.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/firewire.h>
  11. #include <linux/firewire-constants.h>
  12. #include <linux/module.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/mutex.h>
  15. #include <linux/slab.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/wait.h>
  18. #include <sound/control.h>
  19. #include <sound/core.h>
  20. #include <sound/firewire.h>
  21. #include <sound/hwdep.h>
  22. #include <sound/initval.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include "amdtp.h"
  26. #include "iso-resources.h"
  27. #include "lib.h"
  28. #include "dice-interface.h"
  29. struct dice {
  30. struct snd_card *card;
  31. struct fw_unit *unit;
  32. spinlock_t lock;
  33. struct mutex mutex;
  34. unsigned int global_offset;
  35. unsigned int rx_offset;
  36. unsigned int clock_caps;
  37. struct fw_address_handler notification_handler;
  38. int owner_generation;
  39. int dev_lock_count; /* > 0 driver, < 0 userspace */
  40. bool dev_lock_changed;
  41. bool global_enabled;
  42. wait_queue_head_t hwdep_wait;
  43. u32 notification_bits;
  44. struct fw_iso_resources resources;
  45. struct amdtp_out_stream stream;
  46. };
  47. MODULE_DESCRIPTION("DICE driver");
  48. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  49. MODULE_LICENSE("GPL v2");
  50. static const unsigned int dice_rates[] = {
  51. [0] = 32000,
  52. [1] = 44100,
  53. [2] = 48000,
  54. [3] = 88200,
  55. [4] = 96000,
  56. [5] = 176400,
  57. [6] = 192000,
  58. };
  59. static void dice_lock_changed(struct dice *dice)
  60. {
  61. dice->dev_lock_changed = true;
  62. wake_up(&dice->hwdep_wait);
  63. }
  64. static int dice_try_lock(struct dice *dice)
  65. {
  66. int err;
  67. spin_lock_irq(&dice->lock);
  68. if (dice->dev_lock_count < 0) {
  69. err = -EBUSY;
  70. goto out;
  71. }
  72. if (dice->dev_lock_count++ == 0)
  73. dice_lock_changed(dice);
  74. err = 0;
  75. out:
  76. spin_unlock_irq(&dice->lock);
  77. return err;
  78. }
  79. static void dice_unlock(struct dice *dice)
  80. {
  81. spin_lock_irq(&dice->lock);
  82. if (WARN_ON(dice->dev_lock_count <= 0))
  83. goto out;
  84. if (--dice->dev_lock_count == 0)
  85. dice_lock_changed(dice);
  86. out:
  87. spin_unlock_irq(&dice->lock);
  88. }
  89. static inline u64 global_address(struct dice *dice, unsigned int offset)
  90. {
  91. return DICE_PRIVATE_SPACE + dice->global_offset + offset;
  92. }
  93. // TODO: rx index
  94. static inline u64 rx_address(struct dice *dice, unsigned int offset)
  95. {
  96. return DICE_PRIVATE_SPACE + dice->rx_offset + offset;
  97. }
  98. static int dice_owner_set(struct dice *dice)
  99. {
  100. struct fw_device *device = fw_parent_device(dice->unit);
  101. __be64 *buffer;
  102. int err, errors = 0;
  103. buffer = kmalloc(2 * 8, GFP_KERNEL);
  104. if (!buffer)
  105. return -ENOMEM;
  106. for (;;) {
  107. buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
  108. buffer[1] = cpu_to_be64(
  109. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  110. dice->notification_handler.offset);
  111. dice->owner_generation = device->generation;
  112. smp_rmb(); /* node_id vs. generation */
  113. err = snd_fw_transaction(dice->unit,
  114. TCODE_LOCK_COMPARE_SWAP,
  115. global_address(dice, GLOBAL_OWNER),
  116. buffer, 2 * 8,
  117. FW_FIXED_GENERATION |
  118. dice->owner_generation);
  119. if (err == 0) {
  120. if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
  121. dev_err(&dice->unit->device,
  122. "device is already in use\n");
  123. err = -EBUSY;
  124. }
  125. break;
  126. }
  127. if (err != -EAGAIN || ++errors >= 3)
  128. break;
  129. msleep(20);
  130. }
  131. kfree(buffer);
  132. return err;
  133. }
  134. static int dice_owner_update(struct dice *dice)
  135. {
  136. struct fw_device *device = fw_parent_device(dice->unit);
  137. __be64 *buffer;
  138. int err;
  139. if (dice->owner_generation == -1)
  140. return 0;
  141. buffer = kmalloc(2 * 8, GFP_KERNEL);
  142. if (!buffer)
  143. return -ENOMEM;
  144. buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
  145. buffer[1] = cpu_to_be64(
  146. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  147. dice->notification_handler.offset);
  148. dice->owner_generation = device->generation;
  149. smp_rmb(); /* node_id vs. generation */
  150. err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  151. global_address(dice, GLOBAL_OWNER),
  152. buffer, 2 * 8,
  153. FW_FIXED_GENERATION | dice->owner_generation);
  154. if (err == 0) {
  155. if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
  156. dev_err(&dice->unit->device,
  157. "device is already in use\n");
  158. err = -EBUSY;
  159. }
  160. } else if (err == -EAGAIN) {
  161. err = 0; /* try again later */
  162. }
  163. kfree(buffer);
  164. if (err < 0)
  165. dice->owner_generation = -1;
  166. return err;
  167. }
  168. static void dice_owner_clear(struct dice *dice)
  169. {
  170. struct fw_device *device = fw_parent_device(dice->unit);
  171. __be64 *buffer;
  172. buffer = kmalloc(2 * 8, GFP_KERNEL);
  173. if (!buffer)
  174. return;
  175. buffer[0] = cpu_to_be64(
  176. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  177. dice->notification_handler.offset);
  178. buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
  179. snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  180. global_address(dice, GLOBAL_OWNER),
  181. buffer, 2 * 8, FW_QUIET |
  182. FW_FIXED_GENERATION | dice->owner_generation);
  183. kfree(buffer);
  184. dice->owner_generation = -1;
  185. }
  186. static int dice_enable_set(struct dice *dice)
  187. {
  188. __be32 value;
  189. int err;
  190. value = cpu_to_be32(1);
  191. err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  192. global_address(dice, GLOBAL_ENABLE),
  193. &value, 4,
  194. FW_FIXED_GENERATION | dice->owner_generation);
  195. if (err < 0)
  196. return err;
  197. dice->global_enabled = true;
  198. return 0;
  199. }
  200. static void dice_enable_clear(struct dice *dice)
  201. {
  202. __be32 value;
  203. if (!dice->global_enabled)
  204. return;
  205. value = 0;
  206. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  207. global_address(dice, GLOBAL_ENABLE),
  208. &value, 4, FW_QUIET |
  209. FW_FIXED_GENERATION | dice->owner_generation);
  210. dice->global_enabled = false;
  211. }
  212. static void dice_notification(struct fw_card *card, struct fw_request *request,
  213. int tcode, int destination, int source,
  214. int generation, unsigned long long offset,
  215. void *data, size_t length, void *callback_data)
  216. {
  217. struct dice *dice = callback_data;
  218. unsigned long flags;
  219. if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
  220. fw_send_response(card, request, RCODE_TYPE_ERROR);
  221. return;
  222. }
  223. if ((offset & 3) != 0) {
  224. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  225. return;
  226. }
  227. spin_lock_irqsave(&dice->lock, flags);
  228. dice->notification_bits |= be32_to_cpup(data);
  229. spin_unlock_irqrestore(&dice->lock, flags);
  230. fw_send_response(card, request, RCODE_COMPLETE);
  231. wake_up(&dice->hwdep_wait);
  232. }
  233. static int dice_open(struct snd_pcm_substream *substream)
  234. {
  235. static const struct snd_pcm_hardware hardware = {
  236. .info = SNDRV_PCM_INFO_MMAP |
  237. SNDRV_PCM_INFO_MMAP_VALID |
  238. SNDRV_PCM_INFO_BATCH |
  239. SNDRV_PCM_INFO_INTERLEAVED |
  240. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  241. .formats = AMDTP_OUT_PCM_FORMAT_BITS,
  242. .buffer_bytes_max = 16 * 1024 * 1024,
  243. .period_bytes_min = 1,
  244. .period_bytes_max = UINT_MAX,
  245. .periods_min = 1,
  246. .periods_max = UINT_MAX,
  247. };
  248. struct dice *dice = substream->private_data;
  249. struct snd_pcm_runtime *runtime = substream->runtime;
  250. __be32 clock_sel, data[2];
  251. unsigned int rate_index, number_audio, number_midi;
  252. int err;
  253. err = dice_try_lock(dice);
  254. if (err < 0)
  255. goto error;
  256. err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
  257. global_address(dice, GLOBAL_CLOCK_SELECT),
  258. &clock_sel, 4, 0);
  259. if (err < 0)
  260. goto err_lock;
  261. rate_index = (be32_to_cpu(clock_sel) & CLOCK_RATE_MASK)
  262. >> CLOCK_RATE_SHIFT;
  263. if (rate_index >= ARRAY_SIZE(dice_rates)) {
  264. err = -ENXIO;
  265. goto err_lock;
  266. }
  267. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  268. rx_address(dice, RX_NUMBER_AUDIO),
  269. data, 2 * 4, 0);
  270. if (err < 0)
  271. goto err_lock;
  272. number_audio = be32_to_cpu(data[0]);
  273. number_midi = be32_to_cpu(data[1]);
  274. runtime->hw = hardware;
  275. runtime->hw.rates = snd_pcm_rate_to_rate_bit(dice_rates[rate_index]);
  276. snd_pcm_limit_hw_rates(runtime);
  277. runtime->hw.channels_min = number_audio;
  278. runtime->hw.channels_max = number_audio;
  279. amdtp_out_stream_set_parameters(&dice->stream, dice_rates[rate_index],
  280. number_audio, number_midi);
  281. err = snd_pcm_hw_constraint_step(runtime, 0,
  282. SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  283. amdtp_syt_intervals[rate_index]);
  284. if (err < 0)
  285. goto err_lock;
  286. err = snd_pcm_hw_constraint_step(runtime, 0,
  287. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  288. amdtp_syt_intervals[rate_index]);
  289. if (err < 0)
  290. goto err_lock;
  291. err = snd_pcm_hw_constraint_minmax(runtime,
  292. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  293. 5000, UINT_MAX);
  294. if (err < 0)
  295. goto err_lock;
  296. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  297. if (err < 0)
  298. goto err_lock;
  299. return 0;
  300. err_lock:
  301. dice_unlock(dice);
  302. error:
  303. return err;
  304. }
  305. static int dice_close(struct snd_pcm_substream *substream)
  306. {
  307. struct dice *dice = substream->private_data;
  308. dice_unlock(dice);
  309. return 0;
  310. }
  311. static int dice_stream_start_packets(struct dice *dice)
  312. {
  313. int err;
  314. if (amdtp_out_stream_running(&dice->stream))
  315. return 0;
  316. err = amdtp_out_stream_start(&dice->stream, dice->resources.channel,
  317. fw_parent_device(dice->unit)->max_speed);
  318. if (err < 0)
  319. return err;
  320. err = dice_enable_set(dice);
  321. if (err < 0) {
  322. amdtp_out_stream_stop(&dice->stream);
  323. return err;
  324. }
  325. return 0;
  326. }
  327. static int dice_stream_start(struct dice *dice)
  328. {
  329. __be32 channel;
  330. int err;
  331. if (!dice->resources.allocated) {
  332. err = fw_iso_resources_allocate(&dice->resources,
  333. amdtp_out_stream_get_max_payload(&dice->stream),
  334. fw_parent_device(dice->unit)->max_speed);
  335. if (err < 0)
  336. goto error;
  337. channel = cpu_to_be32(dice->resources.channel);
  338. err = snd_fw_transaction(dice->unit,
  339. TCODE_WRITE_QUADLET_REQUEST,
  340. rx_address(dice, RX_ISOCHRONOUS),
  341. &channel, 4, 0);
  342. if (err < 0)
  343. goto err_resources;
  344. }
  345. err = dice_stream_start_packets(dice);
  346. if (err < 0)
  347. goto err_rx_channel;
  348. return 0;
  349. err_rx_channel:
  350. channel = cpu_to_be32((u32)-1);
  351. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  352. rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
  353. err_resources:
  354. fw_iso_resources_free(&dice->resources);
  355. error:
  356. return err;
  357. }
  358. static void dice_stream_stop_packets(struct dice *dice)
  359. {
  360. if (amdtp_out_stream_running(&dice->stream)) {
  361. dice_enable_clear(dice);
  362. amdtp_out_stream_stop(&dice->stream);
  363. }
  364. }
  365. static void dice_stream_stop(struct dice *dice)
  366. {
  367. __be32 channel;
  368. dice_stream_stop_packets(dice);
  369. if (!dice->resources.allocated)
  370. return;
  371. channel = cpu_to_be32((u32)-1);
  372. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  373. rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
  374. fw_iso_resources_free(&dice->resources);
  375. }
  376. static int dice_hw_params(struct snd_pcm_substream *substream,
  377. struct snd_pcm_hw_params *hw_params)
  378. {
  379. struct dice *dice = substream->private_data;
  380. int err;
  381. mutex_lock(&dice->mutex);
  382. dice_stream_stop(dice);
  383. mutex_unlock(&dice->mutex);
  384. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  385. params_buffer_bytes(hw_params));
  386. if (err < 0)
  387. goto error;
  388. amdtp_out_stream_set_pcm_format(&dice->stream,
  389. params_format(hw_params));
  390. return 0;
  391. error:
  392. return err;
  393. }
  394. static int dice_hw_free(struct snd_pcm_substream *substream)
  395. {
  396. struct dice *dice = substream->private_data;
  397. mutex_lock(&dice->mutex);
  398. dice_stream_stop(dice);
  399. mutex_unlock(&dice->mutex);
  400. return snd_pcm_lib_free_vmalloc_buffer(substream);
  401. }
  402. static int dice_prepare(struct snd_pcm_substream *substream)
  403. {
  404. struct dice *dice = substream->private_data;
  405. int err;
  406. mutex_lock(&dice->mutex);
  407. if (amdtp_out_streaming_error(&dice->stream))
  408. dice_stream_stop_packets(dice);
  409. err = dice_stream_start(dice);
  410. if (err < 0) {
  411. mutex_unlock(&dice->mutex);
  412. return err;
  413. }
  414. mutex_unlock(&dice->mutex);
  415. amdtp_out_stream_pcm_prepare(&dice->stream);
  416. return 0;
  417. }
  418. static int dice_trigger(struct snd_pcm_substream *substream, int cmd)
  419. {
  420. struct dice *dice = substream->private_data;
  421. struct snd_pcm_substream *pcm;
  422. switch (cmd) {
  423. case SNDRV_PCM_TRIGGER_START:
  424. pcm = substream;
  425. break;
  426. case SNDRV_PCM_TRIGGER_STOP:
  427. pcm = NULL;
  428. break;
  429. default:
  430. return -EINVAL;
  431. }
  432. amdtp_out_stream_pcm_trigger(&dice->stream, pcm);
  433. return 0;
  434. }
  435. static snd_pcm_uframes_t dice_pointer(struct snd_pcm_substream *substream)
  436. {
  437. struct dice *dice = substream->private_data;
  438. return amdtp_out_stream_pcm_pointer(&dice->stream);
  439. }
  440. static int dice_create_pcm(struct dice *dice)
  441. {
  442. static struct snd_pcm_ops ops = {
  443. .open = dice_open,
  444. .close = dice_close,
  445. .ioctl = snd_pcm_lib_ioctl,
  446. .hw_params = dice_hw_params,
  447. .hw_free = dice_hw_free,
  448. .prepare = dice_prepare,
  449. .trigger = dice_trigger,
  450. .pointer = dice_pointer,
  451. .page = snd_pcm_lib_get_vmalloc_page,
  452. .mmap = snd_pcm_lib_mmap_vmalloc,
  453. };
  454. struct snd_pcm *pcm;
  455. int err;
  456. err = snd_pcm_new(dice->card, "DICE", 0, 1, 0, &pcm);
  457. if (err < 0)
  458. return err;
  459. pcm->private_data = dice;
  460. strcpy(pcm->name, dice->card->shortname);
  461. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->ops = &ops;
  462. return 0;
  463. }
  464. static long dice_hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
  465. long count, loff_t *offset)
  466. {
  467. struct dice *dice = hwdep->private_data;
  468. DEFINE_WAIT(wait);
  469. union snd_firewire_event event;
  470. spin_lock_irq(&dice->lock);
  471. while (!dice->dev_lock_changed && dice->notification_bits == 0) {
  472. prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
  473. spin_unlock_irq(&dice->lock);
  474. schedule();
  475. finish_wait(&dice->hwdep_wait, &wait);
  476. if (signal_pending(current))
  477. return -ERESTARTSYS;
  478. spin_lock_irq(&dice->lock);
  479. }
  480. memset(&event, 0, sizeof(event));
  481. if (dice->dev_lock_changed) {
  482. event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
  483. event.lock_status.status = dice->dev_lock_count > 0;
  484. dice->dev_lock_changed = false;
  485. count = min(count, (long)sizeof(event.lock_status));
  486. } else {
  487. event.dice_notification.type = SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
  488. event.dice_notification.notification = dice->notification_bits;
  489. dice->notification_bits = 0;
  490. count = min(count, (long)sizeof(event.dice_notification));
  491. }
  492. spin_unlock_irq(&dice->lock);
  493. if (copy_to_user(buf, &event, count))
  494. return -EFAULT;
  495. return count;
  496. }
  497. static unsigned int dice_hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
  498. poll_table *wait)
  499. {
  500. struct dice *dice = hwdep->private_data;
  501. unsigned int events;
  502. poll_wait(file, &dice->hwdep_wait, wait);
  503. spin_lock_irq(&dice->lock);
  504. if (dice->dev_lock_changed || dice->notification_bits != 0)
  505. events = POLLIN | POLLRDNORM;
  506. else
  507. events = 0;
  508. spin_unlock_irq(&dice->lock);
  509. return events;
  510. }
  511. static int dice_hwdep_get_info(struct dice *dice, void __user *arg)
  512. {
  513. struct fw_device *dev = fw_parent_device(dice->unit);
  514. struct snd_firewire_get_info info;
  515. memset(&info, 0, sizeof(info));
  516. info.type = SNDRV_FIREWIRE_TYPE_DICE;
  517. info.card = dev->card->index;
  518. *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
  519. *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
  520. strlcpy(info.device_name, dev_name(&dev->device),
  521. sizeof(info.device_name));
  522. if (copy_to_user(arg, &info, sizeof(info)))
  523. return -EFAULT;
  524. return 0;
  525. }
  526. static int dice_hwdep_lock(struct dice *dice)
  527. {
  528. int err;
  529. spin_lock_irq(&dice->lock);
  530. if (dice->dev_lock_count == 0) {
  531. dice->dev_lock_count = -1;
  532. err = 0;
  533. } else {
  534. err = -EBUSY;
  535. }
  536. spin_unlock_irq(&dice->lock);
  537. return err;
  538. }
  539. static int dice_hwdep_unlock(struct dice *dice)
  540. {
  541. int err;
  542. spin_lock_irq(&dice->lock);
  543. if (dice->dev_lock_count == -1) {
  544. dice->dev_lock_count = 0;
  545. err = 0;
  546. } else {
  547. err = -EBADFD;
  548. }
  549. spin_unlock_irq(&dice->lock);
  550. return err;
  551. }
  552. static int dice_hwdep_release(struct snd_hwdep *hwdep, struct file *file)
  553. {
  554. struct dice *dice = hwdep->private_data;
  555. spin_lock_irq(&dice->lock);
  556. if (dice->dev_lock_count == -1)
  557. dice->dev_lock_count = 0;
  558. spin_unlock_irq(&dice->lock);
  559. return 0;
  560. }
  561. static int dice_hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
  562. unsigned int cmd, unsigned long arg)
  563. {
  564. struct dice *dice = hwdep->private_data;
  565. switch (cmd) {
  566. case SNDRV_FIREWIRE_IOCTL_GET_INFO:
  567. return dice_hwdep_get_info(dice, (void __user *)arg);
  568. case SNDRV_FIREWIRE_IOCTL_LOCK:
  569. return dice_hwdep_lock(dice);
  570. case SNDRV_FIREWIRE_IOCTL_UNLOCK:
  571. return dice_hwdep_unlock(dice);
  572. default:
  573. return -ENOIOCTLCMD;
  574. }
  575. }
  576. #ifdef CONFIG_COMPAT
  577. static int dice_hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
  578. unsigned int cmd, unsigned long arg)
  579. {
  580. return dice_hwdep_ioctl(hwdep, file, cmd,
  581. (unsigned long)compat_ptr(arg));
  582. }
  583. #else
  584. #define dice_hwdep_compat_ioctl NULL
  585. #endif
  586. static int dice_create_hwdep(struct dice *dice)
  587. {
  588. static const struct snd_hwdep_ops ops = {
  589. .read = dice_hwdep_read,
  590. .release = dice_hwdep_release,
  591. .poll = dice_hwdep_poll,
  592. .ioctl = dice_hwdep_ioctl,
  593. .ioctl_compat = dice_hwdep_compat_ioctl,
  594. };
  595. struct snd_hwdep *hwdep;
  596. int err;
  597. err = snd_hwdep_new(dice->card, "DICE", 0, &hwdep);
  598. if (err < 0)
  599. return err;
  600. strcpy(hwdep->name, "DICE");
  601. hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
  602. hwdep->ops = ops;
  603. hwdep->private_data = dice;
  604. hwdep->exclusive = true;
  605. return 0;
  606. }
  607. static void dice_card_free(struct snd_card *card)
  608. {
  609. struct dice *dice = card->private_data;
  610. amdtp_out_stream_destroy(&dice->stream);
  611. fw_core_remove_address_handler(&dice->notification_handler);
  612. mutex_destroy(&dice->mutex);
  613. }
  614. #define DICE_CATEGORY_ID 0x04
  615. static int dice_interface_check(struct fw_unit *unit)
  616. {
  617. static const int min_values[10] = {
  618. 10, 0x64 / 4,
  619. 10, 0x18 / 4,
  620. 10, 0x18 / 4,
  621. 0, 0,
  622. 0, 0,
  623. };
  624. struct fw_device *device = fw_parent_device(unit);
  625. struct fw_csr_iterator it;
  626. int key, value, vendor = -1, model = -1, err;
  627. unsigned int i;
  628. __be32 pointers[ARRAY_SIZE(min_values)];
  629. __be32 version;
  630. /*
  631. * Check that GUID and unit directory are constructed according to DICE
  632. * rules, i.e., that the specifier ID is the GUID's OUI, and that the
  633. * GUID chip ID consists of the 8-bit DICE category ID, the 10-bit
  634. * product ID, and a 22-bit serial number.
  635. */
  636. fw_csr_iterator_init(&it, unit->directory);
  637. while (fw_csr_iterator_next(&it, &key, &value)) {
  638. switch (key) {
  639. case CSR_SPECIFIER_ID:
  640. vendor = value;
  641. break;
  642. case CSR_MODEL:
  643. model = value;
  644. break;
  645. }
  646. }
  647. if (device->config_rom[3] != ((vendor << 8) | DICE_CATEGORY_ID) ||
  648. device->config_rom[4] >> 22 != model)
  649. return -ENODEV;
  650. /*
  651. * Check that the sub address spaces exist and are located inside the
  652. * private address space. The minimum values are chosen so that all
  653. * minimally required registers are included.
  654. */
  655. err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
  656. DICE_PRIVATE_SPACE,
  657. pointers, sizeof(pointers), 0);
  658. if (err < 0)
  659. return -ENODEV;
  660. for (i = 0; i < ARRAY_SIZE(pointers); ++i) {
  661. value = be32_to_cpu(pointers[i]);
  662. if (value < min_values[i] || value >= 0x40000)
  663. return -ENODEV;
  664. }
  665. /*
  666. * Check that the implemented DICE driver specification major version
  667. * number matches.
  668. */
  669. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  670. DICE_PRIVATE_SPACE +
  671. be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
  672. &version, 4, 0);
  673. if (err < 0)
  674. return -ENODEV;
  675. if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
  676. dev_err(&unit->device,
  677. "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
  678. return -ENODEV;
  679. }
  680. return 0;
  681. }
  682. static int dice_read_params(struct dice *dice)
  683. {
  684. __be32 pointers[6];
  685. __be32 value;
  686. int err;
  687. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  688. DICE_PRIVATE_SPACE,
  689. pointers, sizeof(pointers), 0);
  690. if (err < 0)
  691. return err;
  692. dice->global_offset = be32_to_cpu(pointers[0]) * 4;
  693. dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
  694. /* some very old firmwares don't tell about their clock support */
  695. if (be32_to_cpu(pointers[1]) * 4 >= GLOBAL_CLOCK_CAPABILITIES + 4) {
  696. err = snd_fw_transaction(
  697. dice->unit, TCODE_READ_QUADLET_REQUEST,
  698. global_address(dice, GLOBAL_CLOCK_CAPABILITIES),
  699. &value, 4, 0);
  700. if (err < 0)
  701. return err;
  702. dice->clock_caps = be32_to_cpu(value);
  703. } else {
  704. /* this should be supported by any device */
  705. dice->clock_caps = CLOCK_CAP_RATE_44100 |
  706. CLOCK_CAP_RATE_48000 |
  707. CLOCK_CAP_SOURCE_ARX1 |
  708. CLOCK_CAP_SOURCE_INTERNAL;
  709. }
  710. return 0;
  711. }
  712. static void dice_card_strings(struct dice *dice)
  713. {
  714. struct snd_card *card = dice->card;
  715. struct fw_device *dev = fw_parent_device(dice->unit);
  716. char vendor[32], model[32];
  717. unsigned int i;
  718. int err;
  719. strcpy(card->driver, "DICE");
  720. strcpy(card->shortname, "DICE");
  721. BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
  722. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  723. global_address(dice, GLOBAL_NICK_NAME),
  724. card->shortname, sizeof(card->shortname), 0);
  725. if (err >= 0) {
  726. /* DICE strings are returned in "always-wrong" endianness */
  727. BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
  728. for (i = 0; i < sizeof(card->shortname); i += 4)
  729. swab32s((u32 *)&card->shortname[i]);
  730. card->shortname[sizeof(card->shortname) - 1] = '\0';
  731. }
  732. strcpy(vendor, "?");
  733. fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
  734. strcpy(model, "?");
  735. fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
  736. snprintf(card->longname, sizeof(card->longname),
  737. "%s %s (serial %u) at %s, S%d",
  738. vendor, model, dev->config_rom[4] & 0x3fffff,
  739. dev_name(&dice->unit->device), 100 << dev->max_speed);
  740. strcpy(card->mixername, "DICE");
  741. }
  742. static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
  743. {
  744. struct snd_card *card;
  745. struct dice *dice;
  746. __be32 clock_sel;
  747. int err;
  748. err = dice_interface_check(unit);
  749. if (err < 0)
  750. return err;
  751. err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*dice), &card);
  752. if (err < 0)
  753. return err;
  754. snd_card_set_dev(card, &unit->device);
  755. dice = card->private_data;
  756. dice->card = card;
  757. spin_lock_init(&dice->lock);
  758. mutex_init(&dice->mutex);
  759. dice->unit = unit;
  760. init_waitqueue_head(&dice->hwdep_wait);
  761. err = dice_read_params(dice);
  762. if (err < 0)
  763. goto err_mutex;
  764. dice->notification_handler.length = 4;
  765. dice->notification_handler.address_callback = dice_notification;
  766. dice->notification_handler.callback_data = dice;
  767. err = fw_core_add_address_handler(&dice->notification_handler,
  768. &fw_high_memory_region);
  769. if (err < 0)
  770. goto err_mutex;
  771. err = fw_iso_resources_init(&dice->resources, unit);
  772. if (err < 0)
  773. goto err_notification_handler;
  774. dice->resources.channels_mask = 0x00000000ffffffffuLL;
  775. err = amdtp_out_stream_init(&dice->stream, unit,
  776. CIP_BLOCKING | CIP_HI_DUALWIRE);
  777. if (err < 0)
  778. goto err_resources;
  779. err = dice_owner_set(dice);
  780. if (err < 0)
  781. goto err_stream;
  782. card->private_free = dice_card_free;
  783. dice_card_strings(dice);
  784. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  785. global_address(dice, GLOBAL_CLOCK_SELECT),
  786. &clock_sel, 4, 0);
  787. if (err < 0)
  788. goto error;
  789. clock_sel &= cpu_to_be32(~CLOCK_SOURCE_MASK);
  790. clock_sel |= cpu_to_be32(CLOCK_SOURCE_ARX1);
  791. err = snd_fw_transaction(unit, TCODE_WRITE_QUADLET_REQUEST,
  792. global_address(dice, GLOBAL_CLOCK_SELECT),
  793. &clock_sel, 4, 0);
  794. if (err < 0)
  795. goto error;
  796. err = dice_create_pcm(dice);
  797. if (err < 0)
  798. goto error;
  799. err = dice_create_hwdep(dice);
  800. if (err < 0)
  801. goto error;
  802. err = snd_card_register(card);
  803. if (err < 0)
  804. goto error;
  805. dev_set_drvdata(&unit->device, dice);
  806. return 0;
  807. err_stream:
  808. amdtp_out_stream_destroy(&dice->stream);
  809. err_resources:
  810. fw_iso_resources_destroy(&dice->resources);
  811. err_notification_handler:
  812. fw_core_remove_address_handler(&dice->notification_handler);
  813. err_mutex:
  814. mutex_destroy(&dice->mutex);
  815. error:
  816. snd_card_free(card);
  817. return err;
  818. }
  819. static void dice_remove(struct fw_unit *unit)
  820. {
  821. struct dice *dice = dev_get_drvdata(&unit->device);
  822. amdtp_out_stream_pcm_abort(&dice->stream);
  823. snd_card_disconnect(dice->card);
  824. mutex_lock(&dice->mutex);
  825. dice_stream_stop(dice);
  826. dice_owner_clear(dice);
  827. mutex_unlock(&dice->mutex);
  828. snd_card_free_when_closed(dice->card);
  829. }
  830. static void dice_bus_reset(struct fw_unit *unit)
  831. {
  832. struct dice *dice = dev_get_drvdata(&unit->device);
  833. /*
  834. * On a bus reset, the DICE firmware disables streaming and then goes
  835. * off contemplating its own navel for hundreds of milliseconds before
  836. * it can react to any of our attempts to reenable streaming. This
  837. * means that we lose synchronization anyway, so we force our streams
  838. * to stop so that the application can restart them in an orderly
  839. * manner.
  840. */
  841. amdtp_out_stream_pcm_abort(&dice->stream);
  842. mutex_lock(&dice->mutex);
  843. dice->global_enabled = false;
  844. dice_stream_stop_packets(dice);
  845. dice_owner_update(dice);
  846. fw_iso_resources_update(&dice->resources);
  847. mutex_unlock(&dice->mutex);
  848. }
  849. #define DICE_INTERFACE 0x000001
  850. static const struct ieee1394_device_id dice_id_table[] = {
  851. {
  852. .match_flags = IEEE1394_MATCH_VERSION,
  853. .version = DICE_INTERFACE,
  854. },
  855. { }
  856. };
  857. MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
  858. static struct fw_driver dice_driver = {
  859. .driver = {
  860. .owner = THIS_MODULE,
  861. .name = KBUILD_MODNAME,
  862. .bus = &fw_bus_type,
  863. },
  864. .probe = dice_probe,
  865. .update = dice_bus_reset,
  866. .remove = dice_remove,
  867. .id_table = dice_id_table,
  868. };
  869. static int __init alsa_dice_init(void)
  870. {
  871. return driver_register(&dice_driver.driver);
  872. }
  873. static void __exit alsa_dice_exit(void)
  874. {
  875. driver_unregister(&dice_driver.driver);
  876. }
  877. module_init(alsa_dice_init);
  878. module_exit(alsa_dice_exit);