dice.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/firewire.h>
  10. #include <linux/firewire-constants.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/mutex.h>
  14. #include <linux/slab.h>
  15. #include <sound/control.h>
  16. #include <sound/core.h>
  17. #include <sound/hwdep.h>
  18. #include <sound/initval.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include "amdtp.h"
  22. #include "iso-resources.h"
  23. #include "lib.h"
  24. #define DICE_PRIVATE_SPACE 0xffffe0000000uLL
  25. /* offset from DICE_PRIVATE_SPACE; offsets and sizes in quadlets */
  26. #define DICE_GLOBAL_OFFSET 0x00
  27. #define DICE_GLOBAL_SIZE 0x04
  28. #define DICE_TX_OFFSET 0x08
  29. #define DICE_TX_SIZE 0x0c
  30. #define DICE_RX_OFFSET 0x10
  31. #define DICE_RX_SIZE 0x14
  32. /* pointed to by DICE_GLOBAL_OFFSET */
  33. #define GLOBAL_OWNER 0x000
  34. #define OWNER_NO_OWNER 0xffff000000000000uLL
  35. #define OWNER_NODE_SHIFT 48
  36. #define GLOBAL_NOTIFICATION 0x008
  37. #define NOTIFY_RX_CFG_CHG 0x00000001
  38. #define NOTIFY_TX_CFG_CHG 0x00000002
  39. #define NOTIFY_DUP_ISOC 0x00000004
  40. #define NOTIFY_BW_ERR 0x00000008
  41. #define NOTIFY_LOCK_CHG 0x00000010
  42. #define NOTIFY_CLOCK_ACCEPTED 0x00000020
  43. #define NOTIFY_INTERFACE_CHG 0x00000040
  44. #define NOTIFY_MESSAGE 0x00100000
  45. #define GLOBAL_NICK_NAME 0x00c
  46. #define NICK_NAME_SIZE 64
  47. #define GLOBAL_CLOCK_SELECT 0x04c
  48. #define CLOCK_SOURCE_MASK 0x000000ff
  49. #define CLOCK_SOURCE_AES1 0x00000000
  50. #define CLOCK_SOURCE_AES2 0x00000001
  51. #define CLOCK_SOURCE_AES3 0x00000002
  52. #define CLOCK_SOURCE_AES4 0x00000003
  53. #define CLOCK_SOURCE_AES_ANY 0x00000004
  54. #define CLOCK_SOURCE_ADAT 0x00000005
  55. #define CLOCK_SOURCE_TDIF 0x00000006
  56. #define CLOCK_SOURCE_WC 0x00000007
  57. #define CLOCK_SOURCE_ARX1 0x00000008
  58. #define CLOCK_SOURCE_ARX2 0x00000009
  59. #define CLOCK_SOURCE_ARX3 0x0000000a
  60. #define CLOCK_SOURCE_ARX4 0x0000000b
  61. #define CLOCK_SOURCE_INTERNAL 0x0000000c
  62. #define CLOCK_RATE_MASK 0x0000ff00
  63. #define CLOCK_RATE_32000 0x00000000
  64. #define CLOCK_RATE_44100 0x00000100
  65. #define CLOCK_RATE_48000 0x00000200
  66. #define CLOCK_RATE_88200 0x00000300
  67. #define CLOCK_RATE_96000 0x00000400
  68. #define CLOCK_RATE_176400 0x00000500
  69. #define CLOCK_RATE_192000 0x00000600
  70. #define CLOCK_RATE_ANY_LOW 0x00000700
  71. #define CLOCK_RATE_ANY_MID 0x00000800
  72. #define CLOCK_RATE_ANY_HIGH 0x00000900
  73. #define CLOCK_RATE_NONE 0x00000a00
  74. #define CLOCK_RATE_SHIFT 8
  75. #define GLOBAL_ENABLE 0x050
  76. #define ENABLE 0x00000001
  77. #define GLOBAL_STATUS 0x054
  78. #define STATUS_SOURCE_LOCKED 0x00000001
  79. #define STATUS_RATE_CONFLICT 0x00000002
  80. #define STATUS_NOMINAL_RATE_MASK 0x0000ff00
  81. #define GLOBAL_EXTENDED_STATUS 0x058
  82. #define EXT_STATUS_AES1_LOCKED 0x00000001
  83. #define EXT_STATUS_AES2_LOCKED 0x00000002
  84. #define EXT_STATUS_AES3_LOCKED 0x00000004
  85. #define EXT_STATUS_AES4_LOCKED 0x00000008
  86. #define EXT_STATUS_ADAT_LOCKED 0x00000010
  87. #define EXT_STATUS_TDIF_LOCKED 0x00000020
  88. #define EXT_STATUS_ARX1_LOCKED 0x00000040
  89. #define EXT_STATUS_ARX2_LOCKED 0x00000080
  90. #define EXT_STATUS_ARX3_LOCKED 0x00000100
  91. #define EXT_STATUS_ARX4_LOCKED 0x00000200
  92. #define EXT_STATUS_WC_LOCKED 0x00000400
  93. #define EXT_STATUS_AES1_SLIP 0x00010000
  94. #define EXT_STATUS_AES2_SLIP 0x00020000
  95. #define EXT_STATUS_AES3_SLIP 0x00040000
  96. #define EXT_STATUS_AES4_SLIP 0x00080000
  97. #define EXT_STATUS_ADAT_SLIP 0x00100000
  98. #define EXT_STATUS_TDIF_SLIP 0x00200000
  99. #define EXT_STATUS_ARX1_SLIP 0x00400000
  100. #define EXT_STATUS_ARX2_SLIP 0x00800000
  101. #define EXT_STATUS_ARX3_SLIP 0x01000000
  102. #define EXT_STATUS_ARX4_SLIP 0x02000000
  103. #define EXT_STATUS_WC_SLIP 0x04000000
  104. #define GLOBAL_SAMPLE_RATE 0x05c
  105. #define GLOBAL_VERSION 0x060
  106. #define GLOBAL_CLOCK_CAPABILITIES 0x064
  107. #define CLOCK_CAP_RATE_32000 0x00000001
  108. #define CLOCK_CAP_RATE_44100 0x00000002
  109. #define CLOCK_CAP_RATE_48000 0x00000004
  110. #define CLOCK_CAP_RATE_88200 0x00000008
  111. #define CLOCK_CAP_RATE_96000 0x00000010
  112. #define CLOCK_CAP_RATE_176400 0x00000020
  113. #define CLOCK_CAP_RATE_192000 0x00000040
  114. #define CLOCK_CAP_SOURCE_AES1 0x00010000
  115. #define CLOCK_CAP_SOURCE_AES2 0x00020000
  116. #define CLOCK_CAP_SOURCE_AES3 0x00040000
  117. #define CLOCK_CAP_SOURCE_AES4 0x00080000
  118. #define CLOCK_CAP_SOURCE_AES_ANY 0x00100000
  119. #define CLOCK_CAP_SOURCE_ADAT 0x00200000
  120. #define CLOCK_CAP_SOURCE_TDIF 0x00400000
  121. #define CLOCK_CAP_SOURCE_WC 0x00800000
  122. #define CLOCK_CAP_SOURCE_ARX1 0x01000000
  123. #define CLOCK_CAP_SOURCE_ARX2 0x02000000
  124. #define CLOCK_CAP_SOURCE_ARX3 0x04000000
  125. #define CLOCK_CAP_SOURCE_ARX4 0x08000000
  126. #define CLOCK_CAP_SOURCE_INTERNAL 0x10000000
  127. #define GLOBAL_CLOCK_SOURCE_NAMES 0x068
  128. #define CLOCK_SOURCE_NAMES_SIZE 256
  129. /* pointed to by DICE_TX_OFFSET */
  130. #define TX_NUMBER 0x000
  131. #define TX_SIZE 0x004
  132. /* repeated TX_NUMBER times, offset by TX_SIZE quadlets */
  133. #define TX_ISOCHRONOUS 0x008
  134. #define TX_NUMBER_AUDIO 0x00c
  135. #define TX_NUMBER_MIDI 0x010
  136. #define TX_SPEED 0x014
  137. #define TX_NAMES 0x018
  138. #define TX_NAMES_SIZE 256
  139. #define TX_AC3_CAPABILITIES 0x118
  140. #define TX_AC3_ENABLE 0x11c
  141. /* pointed to by DICE_RX_OFFSET */
  142. #define RX_NUMBER 0x000
  143. #define RX_SIZE 0x004
  144. /* repeated RX_NUMBER times, offset by RX_SIZE quadlets */
  145. #define RX_ISOCHRONOUS 0x008
  146. #define RX_SEQ_START 0x00c
  147. #define RX_NUMBER_AUDIO 0x010
  148. #define RX_NUMBER_MIDI 0x014
  149. #define RX_NAMES 0x018
  150. #define RX_NAMES_SIZE 256
  151. #define RX_AC3_CAPABILITIES 0x118
  152. #define RX_AC3_ENABLE 0x11c
  153. #define FIRMWARE_LOAD_SPACE 0xffffe0100000uLL
  154. /* offset from FIRMWARE_LOAD_SPACE */
  155. #define FIRMWARE_VERSION 0x000
  156. #define FIRMWARE_OPCODE 0x004
  157. #define OPCODE_MASK 0x00000fff
  158. #define OPCODE_GET_IMAGE_DESC 0x00000000
  159. #define OPCODE_DELETE_IMAGE 0x00000001
  160. #define OPCODE_CREATE_IMAGE 0x00000002
  161. #define OPCODE_UPLOAD 0x00000003
  162. #define OPCODE_UPLOAD_STAT 0x00000004
  163. #define OPCODE_RESET_IMAGE 0x00000005
  164. #define OPCODE_TEST_ACTION 0x00000006
  165. #define OPCODE_GET_RUNNING_IMAGE_VINFO 0x0000000a
  166. #define OPCODE_EXECUTE 0x80000000
  167. #define FIRMWARE_RETURN_STATUS 0x008
  168. #define FIRMWARE_PROGRESS 0x00c
  169. #define PROGRESS_CURR_MASK 0x00000fff
  170. #define PROGRESS_MAX_MASK 0x00fff000
  171. #define PROGRESS_TOUT_MASK 0x0f000000
  172. #define PROGRESS_FLAG 0x80000000
  173. #define FIRMWARE_CAPABILITIES 0x010
  174. #define FL_CAP_AUTOERASE 0x00000001
  175. #define FL_CAP_PROGRESS 0x00000002
  176. #define FIRMWARE_DATA 0x02c
  177. #define TEST_CMD_POKE 0x00000001
  178. #define TEST_CMD_PEEK 0x00000002
  179. #define CMD_GET_AVS_CNT 0x00000003
  180. #define CMD_CLR_AVS_CNT 0x00000004
  181. #define CMD_SET_MODE 0x00000005
  182. #define CMD_SET_MIDIBP 0x00000006
  183. #define CMD_GET_AVSPHASE 0x00000007
  184. #define CMD_ENABLE_BNC_SYNC 0x00000008
  185. #define CMD_PULSE_BNC_SYNC 0x00000009
  186. #define CMD_EMUL_SLOW_CMD 0x0000000a
  187. #define FIRMWARE_TEST_DELAY 0xfd8
  188. #define FIRMWARE_TEST_BUF 0xfdc
  189. /* EAP */
  190. #define EAP_PRIVATE_SPACE 0xffffe0200000uLL
  191. #define EAP_CAPABILITY_OFFSET 0x000
  192. #define EAP_CAPABILITY_SIZE 0x004
  193. /* ... */
  194. #define EAP_ROUTER_CAPS 0x000
  195. #define ROUTER_EXPOSED 0x00000001
  196. #define ROUTER_READ_ONLY 0x00000002
  197. #define ROUTER_FLASH 0x00000004
  198. #define MAX_ROUTES_MASK 0xffff0000
  199. #define EAP_MIXER_CAPS 0x004
  200. #define MIXER_EXPOSED 0x00000001
  201. #define MIXER_READ_ONLY 0x00000002
  202. #define MIXER_FLASH 0x00000004
  203. #define MIXER_IN_DEV_MASK 0x000000f0
  204. #define MIXER_OUT_DEV_MASK 0x00000f00
  205. #define MIXER_INPUTS_MASK 0x00ff0000
  206. #define MIXER_OUTPUTS_MASK 0xff000000
  207. #define EAP_GENERAL_CAPS 0x008
  208. #define GENERAL_STREAM_CONFIG 0x00000001
  209. #define GENERAL_FLASH 0x00000002
  210. #define GENERAL_PEAK 0x00000004
  211. #define GENERAL_MAX_TX_STREAMS_MASK 0x000000f0
  212. #define GENERAL_MAX_RX_STREAMS_MASK 0x00000f00
  213. #define GENERAL_STREAM_CONFIG_FLASH 0x00001000
  214. #define GENERAL_CHIP_MASK 0x00ff0000
  215. #define GENERAL_CHIP_DICE_II 0x00000000
  216. #define GENERAL_CHIP_DICE_MINI 0x00010000
  217. #define GENERAL_CHIP_DICE_JR 0x00020000
  218. struct dice {
  219. struct snd_card *card;
  220. struct fw_unit *unit;
  221. struct mutex mutex;
  222. unsigned int global_offset;
  223. unsigned int rx_offset;
  224. struct fw_address_handler notification_handler;
  225. int owner_generation;
  226. bool global_enabled;
  227. bool stream_running;
  228. struct snd_pcm_substream *pcm;
  229. struct fw_iso_resources resources;
  230. struct amdtp_out_stream stream;
  231. };
  232. MODULE_DESCRIPTION("DICE driver");
  233. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  234. MODULE_LICENSE("GPL v2");
  235. static const unsigned int dice_rates[] = {
  236. [0] = 32000,
  237. [1] = 44100,
  238. [2] = 48000,
  239. [3] = 88200,
  240. [4] = 96000,
  241. [5] = 176400,
  242. [6] = 192000,
  243. };
  244. static inline u64 global_address(struct dice *dice, unsigned int offset)
  245. {
  246. return DICE_PRIVATE_SPACE + dice->global_offset + offset;
  247. }
  248. // TODO: rx index
  249. static inline u64 rx_address(struct dice *dice, unsigned int offset)
  250. {
  251. return DICE_PRIVATE_SPACE + dice->rx_offset + offset;
  252. }
  253. static int dice_owner_set(struct dice *dice)
  254. {
  255. struct fw_device *device = fw_parent_device(dice->unit);
  256. __be64 *buffer;
  257. int rcode, err, errors = 0;
  258. buffer = kmalloc(2 * 8, GFP_KERNEL);
  259. if (!buffer)
  260. return -ENOMEM;
  261. for (;;) {
  262. buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
  263. buffer[1] = cpu_to_be64(
  264. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  265. dice->notification_handler.offset);
  266. dice->owner_generation = device->generation;
  267. smp_rmb(); /* node_id vs. generation */
  268. rcode = fw_run_transaction(device->card,
  269. TCODE_LOCK_COMPARE_SWAP,
  270. device->node_id,
  271. dice->owner_generation,
  272. device->max_speed,
  273. global_address(dice, GLOBAL_OWNER),
  274. buffer, 2 * 8);
  275. if (rcode == RCODE_COMPLETE) {
  276. if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER)) {
  277. err = 0;
  278. } else {
  279. dev_err(&dice->unit->device,
  280. "device is already in use\n");
  281. err = -EBUSY;
  282. }
  283. break;
  284. }
  285. if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
  286. dev_err(&dice->unit->device,
  287. "setting device owner failed: %s\n",
  288. fw_rcode_string(rcode));
  289. err = -EIO;
  290. break;
  291. }
  292. msleep(20);
  293. }
  294. kfree(buffer);
  295. return err;
  296. }
  297. static int dice_owner_update(struct dice *dice)
  298. {
  299. struct fw_device *device = fw_parent_device(dice->unit);
  300. __be64 *buffer;
  301. int rcode, err, errors = 0;
  302. if (dice->owner_generation == -1)
  303. return 0;
  304. buffer = kmalloc(2 * 8, GFP_KERNEL);
  305. if (!buffer)
  306. return -ENOMEM;
  307. for (;;) {
  308. buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
  309. buffer[1] = cpu_to_be64(
  310. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  311. dice->notification_handler.offset);
  312. dice->owner_generation = device->generation;
  313. smp_rmb(); /* node_id vs. generation */
  314. rcode = fw_run_transaction(device->card,
  315. TCODE_LOCK_COMPARE_SWAP,
  316. device->node_id,
  317. dice->owner_generation,
  318. device->max_speed,
  319. global_address(dice, GLOBAL_OWNER),
  320. buffer, 2 * 8);
  321. if (rcode == RCODE_COMPLETE) {
  322. if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER)) {
  323. err = 0;
  324. } else {
  325. dev_err(&dice->unit->device,
  326. "device is already in use\n");
  327. err = -EBUSY;
  328. }
  329. break;
  330. }
  331. if (rcode == RCODE_GENERATION) {
  332. err = 0; /* try again later */
  333. break;
  334. }
  335. if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
  336. dev_err(&dice->unit->device,
  337. "setting device owner failed: %s\n",
  338. fw_rcode_string(rcode));
  339. err = -EIO;
  340. break;
  341. }
  342. msleep(20);
  343. }
  344. kfree(buffer);
  345. if (err < 0)
  346. dice->owner_generation = -1;
  347. return err;
  348. }
  349. static void dice_owner_clear(struct dice *dice)
  350. {
  351. struct fw_device *device = fw_parent_device(dice->unit);
  352. __be64 *buffer;
  353. int rcode, errors = 0;
  354. buffer = kmalloc(2 * 8, GFP_KERNEL);
  355. if (!buffer)
  356. return;
  357. for (;;) {
  358. buffer[0] = cpu_to_be64(
  359. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  360. dice->notification_handler.offset);
  361. buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
  362. rcode = fw_run_transaction(device->card,
  363. TCODE_LOCK_COMPARE_SWAP,
  364. device->node_id,
  365. dice->owner_generation,
  366. device->max_speed,
  367. global_address(dice, GLOBAL_OWNER),
  368. buffer, 2 * 8);
  369. if (rcode == RCODE_COMPLETE)
  370. break;
  371. if (rcode == RCODE_GENERATION)
  372. break;
  373. if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
  374. dev_err(&dice->unit->device,
  375. "clearing device owner failed: %s\n",
  376. fw_rcode_string(rcode));
  377. break;
  378. }
  379. msleep(20);
  380. }
  381. kfree(buffer);
  382. dice->owner_generation = -1;
  383. }
  384. static int dice_enable_set(struct dice *dice)
  385. {
  386. struct fw_device *device = fw_parent_device(dice->unit);
  387. __be32 value;
  388. int rcode, err, errors = 0;
  389. value = cpu_to_be32(ENABLE);
  390. for (;;) {
  391. rcode = fw_run_transaction(device->card,
  392. TCODE_WRITE_QUADLET_REQUEST,
  393. device->node_id,
  394. dice->owner_generation,
  395. device->max_speed,
  396. global_address(dice, GLOBAL_ENABLE),
  397. &value, 4);
  398. if (rcode == RCODE_COMPLETE) {
  399. dice->global_enabled = true;
  400. err = 0;
  401. break;
  402. }
  403. if (rcode == RCODE_GENERATION) {
  404. err = -EAGAIN;
  405. break;
  406. }
  407. if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
  408. dev_err(&dice->unit->device,
  409. "device enabling failed: %s\n",
  410. fw_rcode_string(rcode));
  411. err = -EIO;
  412. break;
  413. }
  414. msleep(20);
  415. }
  416. return err;
  417. }
  418. static void dice_enable_clear(struct dice *dice)
  419. {
  420. struct fw_device *device = fw_parent_device(dice->unit);
  421. __be32 value;
  422. int rcode, errors = 0;
  423. value = 0;
  424. for (;;) {
  425. rcode = fw_run_transaction(device->card,
  426. TCODE_WRITE_QUADLET_REQUEST,
  427. device->node_id,
  428. dice->owner_generation,
  429. device->max_speed,
  430. global_address(dice, GLOBAL_ENABLE),
  431. &value, 4);
  432. if (rcode == RCODE_COMPLETE ||
  433. rcode == RCODE_GENERATION)
  434. break;
  435. if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
  436. dev_err(&dice->unit->device,
  437. "device disabling failed: %s\n",
  438. fw_rcode_string(rcode));
  439. break;
  440. }
  441. msleep(20);
  442. }
  443. dice->global_enabled = false;
  444. }
  445. static void dice_notification(struct fw_card *card, struct fw_request *request,
  446. int tcode, int destination, int source,
  447. int generation, unsigned long long offset,
  448. void *data, size_t length, void *callback_data)
  449. {
  450. struct dice *dice = callback_data;
  451. if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
  452. fw_send_response(card, request, RCODE_TYPE_ERROR);
  453. return;
  454. }
  455. if ((offset & 3) != 0) {
  456. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  457. return;
  458. }
  459. dev_dbg(&dice->unit->device,
  460. "notification: %08x\n", be32_to_cpup(data));
  461. fw_send_response(card, request, RCODE_COMPLETE);
  462. }
  463. static int dice_open(struct snd_pcm_substream *substream)
  464. {
  465. static const struct snd_pcm_hardware hardware = {
  466. .info = SNDRV_PCM_INFO_MMAP |
  467. SNDRV_PCM_INFO_MMAP_VALID |
  468. SNDRV_PCM_INFO_BATCH |
  469. SNDRV_PCM_INFO_INTERLEAVED |
  470. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  471. .formats = AMDTP_OUT_PCM_FORMAT_BITS,
  472. .buffer_bytes_max = 16 * 1024 * 1024,
  473. .period_bytes_min = 1,
  474. .period_bytes_max = UINT_MAX,
  475. .periods_min = 1,
  476. .periods_max = UINT_MAX,
  477. };
  478. struct dice *dice = substream->private_data;
  479. struct snd_pcm_runtime *runtime = substream->runtime;
  480. __be32 clock_sel, number_audio, number_midi;
  481. unsigned int rate;
  482. int err;
  483. err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
  484. global_address(dice, GLOBAL_CLOCK_SELECT),
  485. &clock_sel, 4);
  486. if (err < 0)
  487. return err;
  488. rate = (be32_to_cpu(clock_sel) & CLOCK_RATE_MASK) >> CLOCK_RATE_SHIFT;
  489. if (rate >= ARRAY_SIZE(dice_rates))
  490. return -ENXIO;
  491. rate = dice_rates[rate];
  492. err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
  493. rx_address(dice, RX_NUMBER_AUDIO),
  494. &number_audio, 4);
  495. if (err < 0)
  496. return err;
  497. err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
  498. rx_address(dice, RX_NUMBER_MIDI),
  499. &number_midi, 4);
  500. if (err < 0)
  501. return err;
  502. runtime->hw = hardware;
  503. runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
  504. snd_pcm_limit_hw_rates(runtime);
  505. runtime->hw.channels_min = be32_to_cpu(number_audio);
  506. runtime->hw.channels_max = be32_to_cpu(number_audio);
  507. amdtp_out_stream_set_rate(&dice->stream, rate);
  508. amdtp_out_stream_set_pcm(&dice->stream, be32_to_cpu(number_audio));
  509. amdtp_out_stream_set_midi(&dice->stream, be32_to_cpu(number_midi));
  510. err = snd_pcm_hw_constraint_minmax(runtime,
  511. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  512. 5000, 8192000);
  513. if (err < 0)
  514. return err;
  515. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  516. if (err < 0)
  517. return err;
  518. return 0;
  519. }
  520. static int dice_close(struct snd_pcm_substream *substream)
  521. {
  522. return 0;
  523. }
  524. static int dice_stream_start_packets(struct dice *dice)
  525. {
  526. int err;
  527. if (dice->stream_running)
  528. return 0;
  529. err = amdtp_out_stream_start(&dice->stream, dice->resources.channel,
  530. fw_parent_device(dice->unit)->max_speed);
  531. if (err < 0)
  532. return err;
  533. err = dice_enable_set(dice);
  534. if (err < 0) {
  535. amdtp_out_stream_stop(&dice->stream);
  536. return err;
  537. }
  538. dice->stream_running = true;
  539. return 0;
  540. }
  541. static int dice_stream_start(struct dice *dice)
  542. {
  543. __be32 channel;
  544. int err;
  545. if (!dice->resources.allocated) {
  546. err = fw_iso_resources_allocate(&dice->resources,
  547. amdtp_out_stream_get_max_payload(&dice->stream),
  548. fw_parent_device(dice->unit)->max_speed);
  549. if (err < 0)
  550. goto error;
  551. channel = cpu_to_be32(dice->resources.channel);
  552. err = snd_fw_transaction(dice->unit,
  553. TCODE_WRITE_QUADLET_REQUEST,
  554. rx_address(dice, RX_ISOCHRONOUS),
  555. &channel, 4);
  556. if (err < 0)
  557. goto err_resources;
  558. }
  559. err = dice_stream_start_packets(dice);
  560. if (err < 0)
  561. goto err_rx_channel;
  562. return 0;
  563. err_rx_channel:
  564. channel = cpu_to_be32((u32)-1);
  565. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  566. rx_address(dice, RX_ISOCHRONOUS), &channel, 4);
  567. err_resources:
  568. fw_iso_resources_free(&dice->resources);
  569. error:
  570. return err;
  571. }
  572. static void dice_stream_stop_packets(struct dice *dice)
  573. {
  574. if (!dice->stream_running)
  575. return;
  576. dice_enable_clear(dice);
  577. amdtp_out_stream_stop(&dice->stream);
  578. dice->stream_running = false;
  579. }
  580. static void dice_stream_stop(struct dice *dice)
  581. {
  582. __be32 channel;
  583. dice_stream_stop_packets(dice);
  584. if (!dice->resources.allocated)
  585. return;
  586. channel = cpu_to_be32((u32)-1);
  587. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  588. rx_address(dice, RX_ISOCHRONOUS), &channel, 4);
  589. fw_iso_resources_free(&dice->resources);
  590. }
  591. static int dice_hw_params(struct snd_pcm_substream *substream,
  592. struct snd_pcm_hw_params *hw_params)
  593. {
  594. struct dice *dice = substream->private_data;
  595. int err;
  596. mutex_lock(&dice->mutex);
  597. dice_stream_stop(dice);
  598. mutex_unlock(&dice->mutex);
  599. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  600. params_buffer_bytes(hw_params));
  601. if (err < 0)
  602. goto error;
  603. amdtp_out_stream_set_pcm_format(&dice->stream,
  604. params_format(hw_params));
  605. return 0;
  606. error:
  607. return err;
  608. }
  609. static int dice_hw_free(struct snd_pcm_substream *substream)
  610. {
  611. struct dice *dice = substream->private_data;
  612. mutex_lock(&dice->mutex);
  613. dice_stream_stop(dice);
  614. mutex_unlock(&dice->mutex);
  615. return snd_pcm_lib_free_vmalloc_buffer(substream);
  616. }
  617. static int dice_prepare(struct snd_pcm_substream *substream)
  618. {
  619. struct dice *dice = substream->private_data;
  620. int err;
  621. mutex_lock(&dice->mutex);
  622. if (amdtp_out_streaming_error(&dice->stream))
  623. dice_stream_stop_packets(dice);
  624. err = dice_stream_start(dice);
  625. if (err < 0) {
  626. mutex_unlock(&dice->mutex);
  627. return err;
  628. }
  629. mutex_unlock(&dice->mutex);
  630. amdtp_out_stream_pcm_prepare(&dice->stream);
  631. return 0;
  632. }
  633. static int dice_trigger(struct snd_pcm_substream *substream, int cmd)
  634. {
  635. struct dice *dice = substream->private_data;
  636. struct snd_pcm_substream *pcm;
  637. switch (cmd) {
  638. case SNDRV_PCM_TRIGGER_START:
  639. pcm = substream;
  640. break;
  641. case SNDRV_PCM_TRIGGER_STOP:
  642. pcm = NULL;
  643. break;
  644. default:
  645. return -EINVAL;
  646. }
  647. amdtp_out_stream_pcm_trigger(&dice->stream, pcm);
  648. return 0;
  649. }
  650. static snd_pcm_uframes_t dice_pointer(struct snd_pcm_substream *substream)
  651. {
  652. struct dice *dice = substream->private_data;
  653. return amdtp_out_stream_pcm_pointer(&dice->stream);
  654. }
  655. static int dice_create_pcm(struct dice *dice)
  656. {
  657. static struct snd_pcm_ops ops = {
  658. .open = dice_open,
  659. .close = dice_close,
  660. .ioctl = snd_pcm_lib_ioctl,
  661. .hw_params = dice_hw_params,
  662. .hw_free = dice_hw_free,
  663. .prepare = dice_prepare,
  664. .trigger = dice_trigger,
  665. .pointer = dice_pointer,
  666. .page = snd_pcm_lib_get_vmalloc_page,
  667. .mmap = snd_pcm_lib_mmap_vmalloc,
  668. };
  669. struct snd_pcm *pcm;
  670. int err;
  671. err = snd_pcm_new(dice->card, "DICE", 0, 1, 0, &pcm);
  672. if (err < 0)
  673. return err;
  674. pcm->private_data = dice;
  675. strcpy(pcm->name, dice->card->shortname);
  676. dice->pcm = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  677. dice->pcm->ops = &ops;
  678. return 0;
  679. }
  680. // TODO: implement these
  681. static long dice_hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
  682. long count, loff_t *offset)
  683. {
  684. return -EIO;
  685. }
  686. static int dice_hwdep_open(struct snd_hwdep *hwdep, struct file *file)
  687. {
  688. return -EIO;
  689. }
  690. static int dice_hwdep_release(struct snd_hwdep *hwdep, struct file *file)
  691. {
  692. return 0;
  693. }
  694. static unsigned int dice_hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
  695. poll_table *wait)
  696. {
  697. return POLLERR | POLLHUP;
  698. }
  699. static int dice_hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
  700. unsigned int cmd, unsigned long arg)
  701. {
  702. return -EIO;
  703. }
  704. static int dice_create_hwdep(struct dice *dice)
  705. {
  706. static const struct snd_hwdep_ops ops = {
  707. .read = dice_hwdep_read,
  708. .open = dice_hwdep_open,
  709. .release = dice_hwdep_release,
  710. .poll = dice_hwdep_poll,
  711. .ioctl = dice_hwdep_ioctl,
  712. .ioctl_compat = dice_hwdep_ioctl,
  713. };
  714. struct snd_hwdep *hwdep;
  715. int err;
  716. err = snd_hwdep_new(dice->card, "DICE", 0, &hwdep);
  717. if (err < 0)
  718. return err;
  719. strcpy(hwdep->name, "DICE");
  720. hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
  721. hwdep->ops = ops;
  722. hwdep->private_data = dice;
  723. hwdep->exclusive = true;
  724. return 0;
  725. }
  726. static void dice_card_free(struct snd_card *card)
  727. {
  728. struct dice *dice = card->private_data;
  729. amdtp_out_stream_destroy(&dice->stream);
  730. fw_core_remove_address_handler(&dice->notification_handler);
  731. mutex_destroy(&dice->mutex);
  732. }
  733. static int dice_init_offsets(struct dice *dice)
  734. {
  735. __be32 pointers[6];
  736. unsigned int global_size, rx_size;
  737. int err;
  738. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  739. DICE_PRIVATE_SPACE, &pointers, 6 * 4);
  740. if (err < 0)
  741. return err;
  742. dice->global_offset = be32_to_cpu(pointers[0]) * 4;
  743. global_size = be32_to_cpu(pointers[1]);
  744. dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
  745. rx_size = be32_to_cpu(pointers[5]);
  746. /* some sanity checks to ensure that we actually have a DICE */
  747. if (dice->global_offset < 10 * 4 || global_size < 0x168 / 4 ||
  748. dice->rx_offset < 10 * 4 || rx_size < 0x120 / 4) {
  749. dev_err(&dice->unit->device, "invalid register pointers\n");
  750. return -ENXIO;
  751. }
  752. return 0;
  753. }
  754. static void dice_card_strings(struct dice *dice)
  755. {
  756. struct snd_card *card = dice->card;
  757. struct fw_device *dev = fw_parent_device(dice->unit);
  758. char vendor[32], model[32];
  759. unsigned int i;
  760. int err;
  761. strcpy(card->driver, "DICE");
  762. strcpy(card->shortname, "DICE");
  763. BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
  764. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  765. global_address(dice, GLOBAL_NICK_NAME),
  766. card->shortname, sizeof(card->shortname));
  767. if (err >= 0) {
  768. /* DICE strings are returned in "always-wrong" endianness */
  769. BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
  770. for (i = 0; i < sizeof(card->shortname); i += 4)
  771. swab32s((u32 *)&card->shortname[i]);
  772. card->shortname[sizeof(card->shortname) - 1] = '\0';
  773. }
  774. strcpy(vendor, "?");
  775. fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
  776. strcpy(model, "?");
  777. fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
  778. snprintf(card->longname, sizeof(card->longname),
  779. "%s %s, GUID %08x%08x at %s, S%d",
  780. vendor, model, dev->config_rom[3], dev->config_rom[4],
  781. dev_name(&dice->unit->device), 100 << dev->max_speed);
  782. strcpy(card->mixername, "DICE");
  783. }
  784. static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
  785. {
  786. struct snd_card *card;
  787. struct dice *dice;
  788. __be32 clock_sel;
  789. int err;
  790. err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*dice), &card);
  791. if (err < 0)
  792. return err;
  793. snd_card_set_dev(card, &unit->device);
  794. dice = card->private_data;
  795. dice->card = card;
  796. mutex_init(&dice->mutex);
  797. dice->unit = unit;
  798. err = dice_init_offsets(dice);
  799. if (err < 0)
  800. goto err_mutex;
  801. dice->notification_handler.length = 4;
  802. dice->notification_handler.address_callback = dice_notification;
  803. dice->notification_handler.callback_data = dice;
  804. err = fw_core_add_address_handler(&dice->notification_handler,
  805. &fw_high_memory_region);
  806. if (err < 0)
  807. goto err_mutex;
  808. err = fw_iso_resources_init(&dice->resources, unit);
  809. if (err < 0)
  810. goto err_notification_handler;
  811. dice->resources.channels_mask = 0x00000000ffffffffuLL;
  812. err = amdtp_out_stream_init(&dice->stream, unit, CIP_BLOCKING);
  813. if (err < 0)
  814. goto err_resources;
  815. err = dice_owner_set(dice);
  816. if (err < 0)
  817. goto err_stream;
  818. card->private_free = dice_card_free;
  819. dice_card_strings(dice);
  820. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  821. global_address(dice, GLOBAL_CLOCK_SELECT),
  822. &clock_sel, 4);
  823. if (err < 0)
  824. goto error;
  825. clock_sel &= cpu_to_be32(~CLOCK_SOURCE_MASK);
  826. clock_sel |= cpu_to_be32(CLOCK_SOURCE_ARX1);
  827. err = snd_fw_transaction(unit, TCODE_WRITE_QUADLET_REQUEST,
  828. global_address(dice, GLOBAL_CLOCK_SELECT),
  829. &clock_sel, 4);
  830. if (err < 0)
  831. goto error;
  832. err = dice_create_pcm(dice);
  833. if (err < 0)
  834. goto error;
  835. err = dice_create_hwdep(dice);
  836. if (err < 0)
  837. goto error;
  838. err = snd_card_register(card);
  839. if (err < 0)
  840. goto error;
  841. dev_set_drvdata(&unit->device, dice);
  842. return 0;
  843. err_stream:
  844. amdtp_out_stream_destroy(&dice->stream);
  845. err_resources:
  846. fw_iso_resources_destroy(&dice->resources);
  847. err_notification_handler:
  848. fw_core_remove_address_handler(&dice->notification_handler);
  849. err_mutex:
  850. mutex_destroy(&dice->mutex);
  851. error:
  852. snd_card_free(card);
  853. return err;
  854. }
  855. static void dice_remove(struct fw_unit *unit)
  856. {
  857. struct dice *dice = dev_get_drvdata(&unit->device);
  858. snd_card_disconnect(dice->card);
  859. mutex_lock(&dice->mutex);
  860. amdtp_out_stream_pcm_abort(&dice->stream);
  861. dice_stream_stop(dice);
  862. dice_owner_clear(dice);
  863. mutex_unlock(&dice->mutex);
  864. snd_card_free_when_closed(dice->card);
  865. }
  866. static void dice_bus_reset(struct fw_unit *unit)
  867. {
  868. struct dice *dice = dev_get_drvdata(&unit->device);
  869. mutex_lock(&dice->mutex);
  870. /*
  871. * On a bus reset, the DICE firmware disables streaming and then goes
  872. * off contemplating its own navel for hundreds of milliseconds before
  873. * it can react to any of our attempts to reenable streaming. This
  874. * means that we lose synchronization anyway, so we force our streams
  875. * to stop so that the application can restart them in an orderly
  876. * manner.
  877. */
  878. amdtp_out_stream_pcm_abort(&dice->stream);
  879. dice_stream_stop_packets(dice);
  880. dice_owner_update(dice);
  881. fw_iso_resources_update(&dice->resources);
  882. mutex_unlock(&dice->mutex);
  883. }
  884. #define TC_OUI 0x000166
  885. #define DICE_INTERFACE 0x000001
  886. static const struct ieee1394_device_id dice_id_table[] = {
  887. {
  888. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  889. IEEE1394_MATCH_VERSION,
  890. .specifier_id = TC_OUI,
  891. .version = DICE_INTERFACE,
  892. },
  893. { }
  894. };
  895. MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
  896. static struct fw_driver dice_driver = {
  897. .driver = {
  898. .owner = THIS_MODULE,
  899. .name = KBUILD_MODNAME,
  900. .bus = &fw_bus_type,
  901. },
  902. .probe = dice_probe,
  903. .update = dice_bus_reset,
  904. .remove = dice_remove,
  905. .id_table = dice_id_table,
  906. };
  907. static int __init alsa_dice_init(void)
  908. {
  909. return driver_register(&dice_driver.driver);
  910. }
  911. static void __exit alsa_dice_exit(void)
  912. {
  913. driver_unregister(&dice_driver.driver);
  914. }
  915. module_init(alsa_dice_init);
  916. module_exit(alsa_dice_exit);