dice.c 27 KB

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